var reWhitespace = /^\s+$/
var reLetter = /^[a-zA-Z]$/
var reAlpha = /^[a-zA-Z]+$/
var reAlphanumeric = /^[a-zA-Z0-9]+$/
var reDigit = /^\d/
var reNonDigits = /\D/g;
var reLetterOrDigit = /^([a-zA-Z]|\d)$/
var reInteger = /^\d+$/
var reSignedInteger = /^(\+|\-)?\d+$/
var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
var reSignedFloat = /^(((\+|\-)?\d+(\.\d*)?)|((\+|\-)?(\d*\.)?\d+))$/
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r"
var defaultEmptyOK = false
var reValidPhoneChars = /^[\+\.\/\(\)\- \d]+$/
var reUSPhone = /^1?[2-9]\d{9}$/
var reIntlPhone = /^011\d{4,15}$/
var reEmail = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/
var reProdNum = /^((6|7)\d{2}[bcdmopstuBCDMOPSTU]\d{3})$|^(\d{7})$/
var reUserID = /^([guGU])[a-zA-Z0-9]{6}$/
var reNatlAcctNum = /^\d{4}$|^\d{7}$/
var reNonsig = /^\d{6}$|^\d{9}$/
var rePrefFleetNum = /^[pP]\d{4}$/
var reCustNum = /^\d{6}$/

function isEmpty(s)
{ return ((s == null) || (s.length == 0) || reWhitespace.test(s)) }

function isWhitespace (s)
{ return (isEmpty(s) || reWhitespace.test(s)) }

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) { return true }
    }
    return false
}

function isLetter (c)
{ return reLetter.test(c) }

function isDigit (c)
{ return reDigit.test(c) }

function isLetterOrDigit (c)
{ return reLetterOrDigit.test(c) }

function isInteger (s)
{   var i
    if (isEmpty(s))
       if (isInteger.arguments.length == 1) { return defaultEmptyOK }
       else { return (isInteger.arguments[1] == true) }

    return reInteger.test(s)
}

function isSignedInteger (s)
{  if (isEmpty(s))
	{
		if (isSignedInteger.arguments.length == 1)
			{ return defaultEmptyOK }
      else
      	{ return (isSignedInteger.arguments[1] == true) }
	}
	else
	{	return reSignedInteger.test(s) }
}

function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK

    if (isPositiveInteger.arguments.length > 1)
       { secondArg = isPositiveInteger.arguments[1] }

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) )
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK

    if (isNonnegativeInteger.arguments.length > 1)
       { secondArg = isNonnegativeInteger.arguments[1] }

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) )
}

function isNegativeInteger (s)
{   var secondArg = defaultEmptyOK

    if (isNegativeInteger.arguments.length > 1)
       { secondArg = isNegativeInteger.arguments[1] }

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) < 0) ) )
}

function isNonpositiveInteger (s)
{   var secondArg = defaultEmptyOK

    if (isNonpositiveInteger.arguments.length > 1)
       { secondArg = isNonpositiveInteger.arguments[1] }

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) <= 0) ) )
}

function isFloat (s)
{  if (isEmpty(s))
	{
		if (isFloat.arguments.length == 1) { return defaultEmptyOK }
      else { return (isFloat.arguments[1] == true) }
   }
   else
   {
    	return reFloat.test(s)
   }
}

function isSignedFloat (s)
{  if (isEmpty(s))
	{
   	if (isSignedFloat.arguments.length == 1) { return defaultEmptyOK }
   	else { return (isSignedFloat.arguments[1] == true) }
   }
   else
   {
       return reSignedFloat.test(s)
   }
}

function isAlpha (s)
{   var i

    if (isEmpty(s))
    {
       if (isAlpha.arguments.length == 1) { return defaultEmptyOK }
       else { return (isAlpha.arguments[1] == true) }
	 }
    else {
       return reAlpha.test(s)
    }
}

function isAlphanumeric (s)
{   var i

    if (isEmpty(s))
    {
       if (isAlphanumeric.arguments.length == 1) { return defaultEmptyOK }
       else { return (isAlphanumeric.arguments[1] == true) }
	 }
    else {
       return reAlphanumeric.test(s)
    }
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var dtCh2= "-";
var minYear=1900;
var maxYear=2100;

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	if (isEmpty(dtStr)) {
		if (isDate.arguments.length == 1 && defaultEmptyOK) { return true }
		if (isDate.arguments.length == 1 && !defaultEmptyOK) {
			alert("Date is required!")
			return false
		}
		if (isDate.arguments[1] == true) { return true }
		if (isDate.arguments[1] == false) {
			alert("Date is required!")
			return false
		}
	}
	dtStr = dtStr.replace(dtCh2,dtCh)
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYear.charAt(0)=="0" && strYear.length>1) strYear=strYear.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYear)
	if (isEmpty(strMonth) || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (isEmpty(strDay) || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (isEmpty(strYear) || strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
	return true
}

function isSSN (s) {
	 if (isEmpty(s))
       if (isSSN.arguments.length == 1) return defaultEmptyOK;
       else return (isSSN.arguments[1] == true);
    return (isInteger(s) && s.length == 9)
}

function isUSPhone (s) {
	if (isEmpty(s))
       if (isUSPhone.arguments.length == 1) return defaultEmptyOK;
       else return (isUSPhone.arguments[1] == true);
    return reUSPhone.test(s)
}

function isInternationalPhone (s) {
	if (isEmpty(s))
       if (isInternationalPhone.arguments.length == 1) return defaultEmptyOK;
       else return (isInternationalPhone.arguments[1] == true);
    return reIntlPhone.test(s)
}

function isEmail (s) {
    if (isEmpty(s))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    return reEmail.test(s)
}

function isURL(s) {
	 if (isEmpty(s))
       if (isURL.arguments.length == 1) return defaultEmptyOK;
       else return (isURL.arguments[1] == true);
    return (s.substr(0,7) == "http://") && (s.indexOf(" ") < 0)
}

function isCustNum(s) {
	 if (isEmpty(s))
       if (isCustNum.arguments.length == 1) return defaultEmptyOK;
       else return (isCustNum.arguments[1] == true);
    return (s.length == 6) && (isInteger(s))
}

function isSiteNum(s) {
	 if (isEmpty(s))
       if (isSiteNum.arguments.length == 1) return defaultEmptyOK;
       else return (isSiteNum.arguments[1] == true);
    return (s.length == 4) && (isInteger(s))
}

function isProdNum(s) {
	 if (isEmpty(s))
       if (isProdNum.arguments.length == 1) return defaultEmptyOK;
       else return (isProdNum.arguments[1] == true);
    return reProdNum.test(s)
}

function isUserID(s) {
	 if (isEmpty(s))
       if (isUserID.arguments.length == 1) return defaultEmptyOK;
       else return (isUserID.arguments[1] == true);
    return reUserID.test(s)
}

function isEmplID(s) {
	 if (isEmpty(s))
       if (isEmplID.arguments.length == 1) return defaultEmptyOK;
       else return (isEmplID.arguments[1] == true);
    return (s.length == 8) && (isInteger(s))
}

function isICC(s) {
	 if (isEmpty(s))
       if (isICC.arguments.length == 1) return defaultEmptyOK;
       else return (isICC.arguments[1] == true);
    return (s.length == 4) && (isInteger(s))
}


function isWFAccount(s) {
	 if (isEmpty(s))
       if (isWFAccount.arguments.length == 1) return defaultEmptyOK;
       else return (isWFAccount.arguments[1] == true);
    return (s.length == 6) && (isInteger(s))
}

function isValidCreditCard(s) {
var v = "0123456789";
var w = "";
for (var i=0; i < s.length; i++) {
	x = s.charAt(i);
if (v.indexOf(x,0) != -1)
	w += x;
}

var j = w.length / 2;
if (j < 6.5 || j > 8 || j == 7) return false;

var k = Math.floor(j);
var m = Math.ceil(j) - k;
var c = 0;
for (var i=0; i<k; i++) {
	a = w.charAt(i*2+m) * 2;
	c += a > 9 ? Math.floor(a/10 + a%10) : a;
}
for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
return (c%10 == 0);
}

function GetKeyCode(e) {
   // Check For Browser Type
   if (document.all){
      return e.keyCode
   }
   else {
      return e.which
   }
}

//Allow only Numbers
function keyPress_Integer(e)
{
   var intKeyCode
   intKeyCode = GetKeyCode(e)
	// Navigation Keys
   if(intKeyCode==0||intKeyCode==8||intKeyCode==13){
   	return true
   }

	// Allow only integers
   if((intKeyCode<48 || intKeyCode >58)) {
      return false
   }

   return true
}
