﻿function CheckNumber(number)
{
	var pattern = "0123456789.";
	var len = number.value.length;
	if (len != 0)
	{
		var index = 0;
		
		while ((index < len) && (len != 0))
			if (pattern.indexOf(number.value.charAt(index)) == -1)
			{
				if (index == len-1)
					number.value = number.value.substring(0, len-1);
				else if (index == 0)
					 	number.value = number.value.substring(1, len);
					 else number.value = number.value.substring(0, index)+number.value.substring(index+1, len);
				index = 0;
				len = number.value.length;
			}
			else index++;
	}
}

function isEmpty(Str) {
	empty = (Str === "") ? true :  false;
	return empty;
}

//-------------------------------------------------------------------------------
function isNumber(Digit) {
	return /^\d+[\.\d*]?$/.test(Digit);
}

//------------------------------------------------------------------------------
function isAlphabet(Digit) {
	return /^[a-zA-Z]$/.test(Digit);
}

//-------------------------------------------------------------------------------
function isInteger(Str) {
	return /^[+-]?\d+$/.test(Str);
}

//-------------------------------------------------------------------------------
function isFloat(Str) {
		return /^[+-]?\d+\.{1}\d*$/.test(Str);
}

//-------------------------------------------------------------------------------
function isCurrency(Str) {
		return /^\d+[.]{1}[0-9]{2,}$/.test(Str);
}

function isDomain (Str) {
	// The pattern for matching all special characters. 
  	//These characters include ( ) < > [ ] " | \ / ~ ! @ # $ % ^ & ? ` ' : ; , 
	var specialChars="\\(\\)<>#\\$&\\*!`\\^\\?~|/@,;:\\\\\\\"\\.\\[\\]";
	// The range of characters allowed in a username or domainname. 
	// It really states which chars aren't allowed. 
	var validChars="\[^\\s" + specialChars + "\]";
	 // An atom (basically a series of  non-special characters.) 
	var atom=validChars + '+';
	// The structure of a normal domain 
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	// Check if IP
	var ipDomainPat=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var IPArray=Str.match(ipDomainPat);
	if (IPArray!=null) {
  	// this is an IP address
	 	 for (var i=1;i<=4;i++) {
	    		if (IPArray[i]>255) {
	 			return false
	   		 }
   		 }
	}
	// Check Domain
	var domainArray=Str.match(domainPat)
	if (domainArray==null) {
    		return false;
	}

	/* domain name seems valid, but now make sure that it ends in a
	 three-letter word (like com, edu, gov ... ) or a two-letter word,
   	representing country (uk, vn) or a four-letter word (.info), and that there's a hostname preceding 
   	the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
	it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=Str.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
	 // the address must end in a two letter or three letter word or four-letter word.
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
   		 return false;
	}

	return true;
}

function isUser (Str) {
	var specialChars="\\(\\)<>#\\$&\\*!`\\^\\?~|/@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	/* The pattern applies if the "user" is a quoted string (in
   	which case, there are no rules about which characters are allowed
   	and which aren't; anything goes).  E.g. "le nguyen vu"@webtome.com
   	is a valid (legal) e-mail address. */
	var quotedUser="(\"[^\"]*\")";
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	// See if "user" is valid 
	if (Str.match(userPat)==null) {
    		return false ;
	}
	return true;
}

function isEmail (emailStr) {
	
	/* The pattern for matching fits the user@domain format. */
	var emailPat=/^(.+)@(.+)$/ ;
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
 	 /* Too many/few @'s or something; basically, this address doesn't
    	 even fit the general mould of a valid e-mail address. */
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	// See if "user" is valid 
	if (!isUser(user)) {
    	// user is not valid
   		 return false ;
	}

	// Check Domain
	if (!isDomain(domain)) {
   		return false;
	}
	return true;
}

function FormatNumber(str){
	            var strTemp = GetNumber(str);
	            if(strTemp.length <= 3)
		            return strTemp;
	            strResult = "";
	            for(var i =0; i< strTemp.length; i++)
		            strTemp = strTemp.replace(",", "");
	            for(var i = strTemp.length; i>=0; i--)
	            {
		            if(strResult.length >0 && (strTemp.length - i -1) % 3 == 0)
			            strResult = "," + strResult;
		            strResult = strTemp.substring(i, i + 1) + strResult;
	            }	
	            return strResult;
            }
            function GetNumber(str)
            {
	            for(var i = 0; i < str.length; i++)
	            {	
		            var temp = str.substring(i, i + 1);		
		            if(!(temp == "," || temp == "." || (temp >= 0 && temp <=9)))
		            {
			            alert("Vui lòng nhập số (0-9)!");
			            return str.substring(0, i);
		            }
		            if(temp == " ")
		                return str.substring(0, i);
	            }
	            return str;
            }
             function IsNumberInt(str)
            {
	            for(var i = 0; i < str.length; i++)
	            {	
		            var temp = str.substring(i, i + 1);		
		            if(!(temp == "," || temp == "." || (temp >= 0 && temp <=9)))
		            {
			            alert("Vui lòng nhập số (0-9)!");
			            return str.substring(0, i);
		            }
		            if(temp == " " || temp == ",")
		                return str.substring(0, i);
	            }
	            return str;
            }
            function ConvertPriceText(strTemp)
            {
		       strTemp        = strTemp.replace(/,/g, "");
		       var priceTy    = parseInt(strTemp/1000000000,0)
		       var priceTrieu = parseInt((strTemp % 1000000000)/1000000,0)
		       var priceNgan  = parseInt(((strTemp % 1000000000))%1000000/1000,0)
		       var priceDong  = parseInt(((strTemp % 1000000000))%1000000%1000,0)
		       var strTextPrice = ""      
		       if(priceTy > 0 && parseInt(strTemp,0) > 900000000)
		        strTextPrice = strTextPrice  + "<b>" + priceTy + "</b> tỷ "
		       if(priceTrieu > 0)
		        strTextPrice = strTextPrice  + "<b>" + priceTrieu + "</b> triệu "
		       if(priceNgan > 0)
		        strTextPrice = strTextPrice  + "</b>" + priceNgan + "</b> ngàn "
               if(document.getElementById("txtGia").value == "vnd1")
               {
                    if(priceTy > 0 || priceTrieu > 0 || priceNgan > 0 || priceDong > 0)
	                    strTextPrice = strTextPrice  + "<b>VNĐ</b>"
               }
               if(document.getElementById("txtGia").value == "sjc")
               {
                     if(priceDong > 0)
                        strTextPrice = strTextPrice + priceDong
                     if(priceTy > 0 || priceTrieu > 0 || priceNgan > 0 || priceDong > 0)
		                strTextPrice = FormatNumber(strTemp) + "<b> lượng SJC</b>"
               }
               if(document.getElementById("txtGia").value == "usd")
               {
                    if(priceDong > 0)
                        strTextPrice = strTextPrice + priceDong
                    if(priceTy > 0 || priceTrieu > 0 || priceNgan > 0 || priceDong > 0)
		                strTextPrice = FormatNumber(strTemp) + "<b> USD</b>"
               }
               document.getElementById("txtGia").innerHTML = strTextPrice
            }
