
var digits = "0123456789";
var defaultEmptyOK = false

// whitespace characters
var whitespace = " \t\n\r";

// U.S. ZIP codes have 5 or 9 digits.
// They are formatted as 12345 or 12345-6789.
var digitsInZIPCode1 = 5
var digitsInZIPCode2 = 9

// non-digit characters which are allowed in ZIP Codes
var ZIPCodeDelimiters = "-";

// U.S. phone numbers have 10 digits.
// They are formatted as 123 456 7890 or (123) 456-7890.
var digitsInUSPhoneNumber = 10;

// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";

// decimal point character differs by language and culture
var decimalPointDelimiter = "."

// non-digit characters which are allowed in money formating
var MoneyDelimiters = "$, ";

// i is an abbreviation for "invalid"

var iZIPCode = "This field must be a 5 or 9 digit U.S. ZIP Code (like 94043). Please reenter it now."
var iUSPhone = "This field must be a 10 digit U.S. phone number (like 415 555 1212). Please reenter it now."
var iEmail = "This field must be a valid email address (like foo@bar.com). Please reenter it now."
var iYear = "This field must be a 4 digit year number.  Please reenter it now."
var iMoney = "This field must be numeric. Please reenter it now."
var iNumeric = "This field must be numeric. Please reenter it now."
var iPassword = "The new password and confirm password must be the same. Please reenter it now."
var iDate = "This field must be a valid date. Please reenter it now."

// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isYear (s)
{   
if (isEmpty(s)) 
    if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    	return ((s.length == 4));
}

function isInteger (s)
{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    return true;
}

function isUSPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

function isEmpty(s)
{   
    return ((s == null) || (s.length == 0))
}
function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

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 SetPrevYr(theField, emptyOK)
{
    var pyr1 = 0;
    var pyr2 = 0;

    if (SetPrevYr.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isYear(theField.value, false)) 
       return warnInvalid (theField, iYear);
    else{
	pyr1 = theField.value - 2;
	pyr2 = theField.value - 1;
	document.frmprocess.pyr1.value = pyr1;
	document.frmprocess.pyr2.value = pyr2;
	return true;	
    }
}
function SetPrevYrs(theField, emptyOK)
{
    var pyr1 = 0;
    var pyr2 = 0;
    var cyr1 = 0;
    var cyr2 = 0;
    var cyr3 = 0;
    var cyr = 0;

    if (SetPrevYrs.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isYear(theField.value, false)) 
       return warnInvalid (theField, iYear);
    else{
   	cyr = theField.value;
	--cyr;
	pyr2 = cyr;
	--cyr;
	pyr1 = cyr;
	cyr1 = theField.value;
   	cyr = theField.value;
	++cyr;
	cyr2 = cyr;
	++cyr;
	cyr3 = cyr;
	document.frmprocess.pyr1.value = pyr1;
	document.frmprocess.pyr2.value = pyr2;
	document.frmprocess.cyr1.value = cyr1;
	document.frmprocess.cyr2.value = cyr2;
	document.frmprocess.cyr3.value = cyr3;

	return true;	
    }
}

function reformat (s)
{   var arg;
    var sPos = 0;
    var resultString = "";
    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


// Removes all characters which do NOT appear in string bag 
// from string s.

function stripCharsNotInBag (s, bag)
{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}


function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
   if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

// takes ZIPString, a string of 5 or 9 digits;
// if 9 digits, inserts separator hyphen

function reformatZIPCode (ZIPString)
{   if (ZIPString.length == 5) return ZIPString;
    else return (reformat (ZIPString, "", 5, "-", 4));
}


function isZIPCode (s)
{  if (isEmpty(s)) 
       if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == digitsInZIPCode1) ||
             (s.length == digitsInZIPCode2)))
}


// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Returns true if string s is empty or 
// whitespace characters only.

function checkEmail (theField, emptyOK)
{
   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, iEmail);
    else return true;
}

// takes USPhone, a string of 10 digits
// and reformats as (123) 456-789

function reformatUSPhone (USPhone)
{  return (reformat(USPhone, "(", 3, ") ", 3, "-", 4));}

function checkUSPhone (theField, emptyOK)
{   if (checkUSPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
       if (!isUSPhoneNumber(normalizedPhone, false)) 
          return warnInvalid (theField, iUSPhone);
       else 
       {  // if you don't want to reformat as (123) 456-789, comment next line out
          theField.value = reformatUSPhone(normalizedPhone);
          //theField.value = reformat(normalizedPhone, "(", 3, ") ", 3, "-", 4));
          return true;
       }
    }
}
function checkPsWrd(theField,emptyOK)
{   
	if (checkPsWrd.arguments.length == 1) emptyOK = defaultEmptyOK;
    
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {	
		if (theField.name == "txtNPassword"){
			if (isEmpty(document.maintfrm.txtCPassword.value)) return true;
			else
			{
				var confirmpswrd = document.maintfrm.txtCPassword.value;
			}
		}else{
			if (isEmpty(document.maintfrm.txtNPassword.value)) return true;
			else
			{
				var confirmpswrd = document.maintfrm.txtNPassword.value;
			}		
		}
      if (theField.value != confirmpswrd) {
         return warnInvalid (theField, iPassword);
      }else{ 
         return true;
      }
	}
}

function checkPassword(theField,emptyOK)
{   
	if (checkPassword.arguments.length == 1) emptyOK = defaultEmptyOK;
    
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {	
		if (theField.name == "txtNPassword"){
			if (isEmpty(document.frmpassword.txtCPassword.value)) return true;
			else
			{
				var confirmpswrd = document.frmpassword.txtCPassword.value;
			}
		}else{
			if (isEmpty(document.frmpassword.txtNPassword.value)) return true;
			else
			{
				var confirmpswrd = document.frmpassword.txtNPassword.value;
			}		
		}
      if (theField.value != confirmpswrd) {
         return warnInvalid (theField, iPassword);
      }else{ 
         return true;
      }
	}
}

function checkZIPCode (theField, emptyOK)
{   if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)
      if (!isZIPCode(normalizedZIP, false)) 
         return warnInvalid (theField, iZIPCode);
      else 
      {  // if you don't want to insert a hyphen, comment next line out
         theField.value = reformatZIPCode(normalizedZIP)
         return true;
      }
    }
}

function checkYear (theField, emptyOK)
{   if (checkYear.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isYear(theField.value, false)) 
       return warnInvalid (theField, iYear);
    else return true;
}

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, put focus in it, and return false.

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}

function isFloat (theField)
{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(theField)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (theField == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < theField.length; i++)
    {   
        // Check that current character is number.
        var c = theField.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function CheckDayBox (theField,emptyOK,theField2)
{   if (CheckDayBox.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {
		if (theField2.value == "Y"){
			theField2.value = 'N';
		}else{
			theField2.value = 'Y';
		}
	}
}
function checkMoney (theField, emptyOK)
{   if (checkMoney.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { var normalizedmoney = stripCharsInBag(theField.value, MoneyDelimiters)
      if (!isFloat(normalizedmoney)) 
         return warnInvalid (theField, iMoney);
      else 
      {  // if you don't want to format, comment next line out
         theField.value = formatMoney(normalizedmoney)
         return true;
      }
    }
}

function formatMoney(aValue)
{
  var tempstr = "";
  var holdvar, p;
  var theFloat = parseFloat(aValue);
  //var theMoney = (Math.round(theFloat * 100)) / 100;
  var theMoney = Math.round(theFloat);
  var c = (theMoney.toString(10).length % 3)
  holdvar = theMoney.toString(10);
  if (c > 0){
	tempstr = holdvar.substring(0,c);
	holdvar = holdvar.substring(c,holdvar.length)
  }
  for (var i = 0; i < holdvar.length; i++)
  {
		if (!i==0){
			i = p;}
		p = i + 3;
		if (p>holdvar.length){ break;}
		if (i==0 && c == 0){
			tempstr = tempstr +  holdvar.substring(i,p);
		}else{
			tempstr = tempstr + "," + holdvar.substring(i,p);
		}

  }	
  var theMoneyString = "$"+tempstr+".00";

  return theMoneyString;
}

function checkNumeric (theField,emptyOK)
{  
   if (checkNumeric.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  
       if (!isInteger(theField.value)) 
          return warnInvalid (theField, iNumeric);
       else return true;
    }
}

function checkDate(theField,emptyOK)
{
	if(checkDate.arguments.length==1) emptyOK=defaultEmptyOK;
	if ((emptyOK==true) && (isEmpty(theField.value))) return true;
	else
	{
		if(!isDate(theField.value))
		 return warnInvalid(theField, iDate);
		else return true;
	}
}
function isDate(datestr)
{
//	window.onerror=null // for all other strange errors
	var err=0
	a=datestr
	if (a.length != 10) err=1
	b = a.substring(0, 2)// month
	c = a.substring(2, 3)// '/'
	d = a.substring(3, 5)// day
	e = a.substring(5, 6)// '/'
	f = a.substring(6, 10)// year
		
	//basic error checking
	if (b<1 || b>12) err = 1
	if (c != '/') err = 1
	if (d<1 || d>31) err = 1
	if (e != '/') err = 1
	if (f<1900 || f>2099) err = 1
		
	//advanced error checking

	// months with 30 days
	if (b==4 || b==6 || b==9 || b==11){
		if (d==31) err=1
	}

	// february, leap year
	if (b==2){
		// feb
		var g=parseInt(f/4)
		if (isNaN(g)) {
			err=1
		}

		if (d>29) err=1
		if (d==29 && ((f/4)!=parseInt(f/4))) err=1
	}

	if (err==1){
		return false
	}
	else{
		return true
	}
}
