// Trim() , Ltrim() , RTrim() 
// trim blank space at the string 
String.prototype.Trim = function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
  
// trim blank space at the beginning 
String.prototype.LTrim = function() 
{ 
	return this.replace(/(^\s*)/g, ""); 
} 
  
// trim blank space at the end 
String.prototype.RTrim = function() 
{ 
	return this.replace(/(\s*$)/g, ""); 
} 

// Check if string is non-blank 
var isNonblank_re    = /\S/; 
function IsNonblank (s) 
{ 
   return String (s).search (isNonblank_re) != -1 
}

// Check if string is a whole number(digits only). 
var isWhole_re       = /^\s*\d+\s*$/; 
function IsWhole (s) 
{ 
   return String(s).search (isWhole_re) != -1 
}

// check 0-9 digit 
function IsDigit(fData) 
{ 
    var reg = new RegExp("^[0-9]$"); 
    return (reg.test(fData)); 
}

// checks that an input string is an integer, with an optional +/- sign character. 
var isInteger_re     = /^\s*(\+|-)?\d+\s*$/; 
function IsInteger (s)
{ 
   return String(s).search (isInteger_re) != -1 
}

// Checks that an input string is a decimal number, with an optional +/- sign character. 
var isDecimal_re     = /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/; 
function IsDecimal (s) 
{ 
   return String(s).search (isDecimal_re) != -1 
}


// Check if string is a valid email address 
function IsEmail(email, required) 
{
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

// this is submethod for validate email
function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

// Check for valid credit card type/number 
var creditCardList = [ 
   //type      prefix   length 
   ["amex",    "34",    15], 
   ["amex",    "37",    15], 
   ["disc",    "6011",  16], 
   ["mc",      "51",    16], 
   ["mc",      "52",    16], 
   ["mc",      "53",    16], 
   ["mc",      "54",    16], 
   ["mc",      "55",    16], 
   ["visa",    "4",     13], 
   ["visa",    "4",     16] 
]; 
function isValidCC (cctype, ccnumber) 
{ 
   var cc = getdigits (ccnumber); 
   if (luhn (cc)) { 
      for (var i in creditCardList) { 
         if (creditCardList [i][0] == (cctype.toLowerCase ())) { 
            if (cc.indexOf (creditCardList [i][1]) == 0) { 
               if (creditCardList [i][2] == cc.length) { 
                  return true; 
               } 
            } 
         } 
      } 
   } 
   return false; 
}

// This is the luhn checksum algorithm, used to validate such things as credit card numbers and bank routing numbers.

function luhn (cc) 
{ 
   var sum = 0; 
   var i; 
  
   for (i = cc.length - 2; i >= 0; i -= 2) { 
      sum += Array (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) [parseInt (cc.charAt (i), 10)]; 
   } 
   for (i = cc.length - 1; i >= 0; i -= 2) { 
      sum += parseInt (cc.charAt (i), 10); 
   } 
   return (sum % 10) == 0; 
}


// This returns a string with everything but the digits removed. 
function getdigits (s) 
{ 
   return s.replace (/[^\d]/g, ""); 
}

function numbersonly(e)
{
	var unicode=e.charCode? e.charCode : e.keyCode
	//alert(unicode);
	
	if (unicode != 8 && 	// not backspace
		unicode != 9 &&		// not TAB
		unicode != 46 ) 	// not delete
	{ //if the key isn't the backspace key (which we should allow)
		
		if(unicode>57)
		{
			return false //disable key press
		}
		else if(unicode >= 37 && unicode <= 40)
		{
			
		}
		else if(unicode < 48)
		{
			return false //disable key press
		}
				
	}
}

function IsThaiPid(id)
{
	if (id.length != 13)
		return false;

	var sum = 0;

	/* x13, x12, x11, ... */
	var i = 0;
	for (i=0; i < 12; i++)
	{
		sum += id.charAt(i) * (13 - i);
	}
	/* complements(12, sum mod 11) */
	return id.charAt(12) - '0' == ((11 - (sum % 11)) % 10);
}

function LeadingZero(value, digits)
{	
	value = parseInt(value);
	var output = "";
	var leading = "";
	var celling = 0;
	var x = 0;
    for(x = 1; x <= digits; x++)
	{
        ceiling = Math.pow(10,x)
        if(value < ceiling)
		{
            zeros = digits - x;
            for(y = 1; y <= zeros; y++)
			{
                leading = leading + "0";
            }
       	 	x = digits + 1;
        }
    }
    output = leading + value;
    return output;
}

function RedirectPage(url)
{
	window.location = url;
}