// validate.js
// entivity v4 - 6/18/02
// revised 6-23-02 modified checkCOI function
// revised 8-12-02 added date functions

var whitespace = " \t\n\r";
var sFirst = "Please enter a First Name";
var sLast = "Please enter a Last Name";
var sName = "Please enter your Name";
var sTitle = "Please enter your Title";
var sStreet = "Please enter a Street Address";
var sCity = "Please enter a City";
var sState = "Please select a State";
var sCountry = "Please enter Country";
var sCompany = "Please enter a Company name";
var sLicense = "Please enter your license code";
var svendor = "Please enter the vendor where you made purchase"
var sdatapurchased = "Please enter the date of purchase"
var sapp = "Please enter your application";
var scoi = "Please enter your COI number";
var sCOI = "Please enter your license code (7-15 charachters)";
var sInteger = "Please enter a whole number between zero and 25";
var sPassword = "Please enter a valid Password (letters or numbers - Length 4-8)";
var sNAME_unsub = "Please enter your name";
var sEMAIL_unsub = "Please enter your email";
var sNAME_change = "Please enter your name";
var sEMAIL_old = "Please enter your old email";
var sEMAIL_new = "Please enter your new email";
var sPWnotEqual = "Passwords are not identical.  Please try again."
var EmptyisOK = true;
var defaultEmptyOK = false;
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
var digitsInUSPhoneNumber = 10;
var boolCatalog=false;
var ZIPCodeDelimiters = "-";
var phoneNumberDelimiters = "()- ";
var coiDelimiters = "- ";

var iEmail = "Please enter a valid email address (like name@company.com)";
var iZIPCode = "Please enter a 5 or 9 digit U.S. ZIP Code (like 94043 or 94043-1234)";
var iUSPhone = "Please enter a 10 digit U.S. phone number (like 415-555-1212). Please reenter it now.";

var iDay = "This field must be a day number between 1 and 31.  Please reenter it now."
var iMonth = "This field must be a month number between 1 and 12.  Please reenter it now."
var iYear = "This field must be a 2 or 4 digit year number.  Please reenter it now."
var iDatePrefix = "The Day, Month, and Year for "
var iDateSuffix = " do not form a valid date.  Please reenter them now."


// Check whether string s is empty
function isEmpty(s)
	{ return ((s == null) || (s.length == 0)) }

function checkString (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnInvalid (theField, s);
    else return true;
}
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;
}

// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnInvalid (theField, s)
{   theField.focus();
    alert(s);
    return false;
}

function checkCatalog (boolFlag)
{   
	if (boolFlag==true)
		return true;
	else
	{
		alert("You haven't ordered any materials.  Please try again");
		return false;
	}
}

function checkCombo (theField, s)
{   
    if (theField.value == 0)
		return warnInvalid (theField, s);
	else return true;        
}

function checkInteger (theField, s)

{   var str=theField.value;
	var i;
    if (isEmpty(str)) 
    {
       return true;
      }

    // 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 < str.length; i++)
    {   
        // Check that current character is number.
        var c = str.charAt(i);
        if (!isDigit(c)) return warnInvalid (theField, s);
    }
	if (str>25) return warnInvalid (theField, s);
    // All characters are numbers.
    boolCatalog=true;
    return true;
}

/*function checkZip (theField, str)
{
	if (isEmpty(str)) 
	{
		theField.focus();
		alert(sZip);
       return false;
    }
	if(isInteger(str) && 
            ((str.length == digitsInZIPCode1) ||
             (str.length == digitsInZIPCode2))==true)
		return true;
	else
	{
		theField.focus();
		alert(sZip);
       return false;
    }
}*/



/*function checkPhone (theField,str,emptyOK)
{   if (isEmpty(str)) 
       if (emptyOK == true) 
			return true;
       else
		{
		theField.focus();
		alert(sPhone);
       return false;
    }
    if ((isInteger(str) && str.length == digitsInUSPhoneNumber)==true)
		return true;
	else
		{
		theField.focus();
		if (emptyOK==false)
			alert(sPhone);
		else
			alert(sFax);
       return false;
    }
}*/

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // 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 < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}
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;
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

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;
}

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;
}

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 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)))
}

function reformatZIPCode (ZIPString)
{   if (ZIPString.length == 5) return ZIPString;
    else return (reformat (ZIPString, "", 5, "-", 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)
          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 checkCOI (theField, emptyOK,sMsg)
{   if (checkCOI.arguments.length == 1) emptyOK = defaultEmptyOK;
	if (isEmpty(sMsg)) sMsg=sCOI;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	if ((isWhitespace(theField.value)) || (theField.value.length < 7) || (theField.value.length > 15))
       return warnInvalid (theField, sMsg);
	var strippedCOI = stripCharsInBag(theField.value, coiDelimiters)
	theField.value=strippedCOI;
    if (!isAlphanumeric(theField.value))
       return warnInvalid (theField, sMsg);
    else return true;
}
function checkPassword2 (password1, password2)
{ 	if (password1.value == password2.value) return true;
    else return warnInvalid (password1, sPWnotEqual);
}
function checkPass (theField, emptyOK)
{   if (checkPass.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if ((isWhitespace(theField.value)) || (!isAlphanumeric(theField.value)) || (theField.value.length < 4) || (theField.value.length > 8))
       return warnInvalid (theField, sPassword);
    else return true;
}


function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    return true;
}
function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

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;
}

function checkMonth (theField, emptyOK)
{   if (checkMonth.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isMonth(theField.value, false)) 
       return warnInvalid (theField, iMonth);
    else return true;
}

function checkDay (theField, emptyOK)
{   if (checkDay.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isDay(theField.value, false)) 
       return warnInvalid (theField, iDay);
    else return true;
}

function checkDate (yearField, monthField, dayField, labelString, OKtoOmitDay)
{ 
    if (checkDate.arguments.length == 4) OKtoOmitDay = false;
    if (!isMonth(monthField.value)) return warnInvalid (monthField, iMonth);
    if ( (OKtoOmitDay == true) && isEmpty(dayField.value) ) return true;
    else if (!isDay(dayField.value)) 
       return warnInvalid (dayField, iDay);
    if (isDate (yearField.value, monthField.value, dayField.value))
       return true;
    if (!isYear(yearField.value)) return warnInvalid (yearField, iYear);
    alert (iDatePrefix + labelString + iDateSuffix)
    return false;
}

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 == 2) || (s.length == 4));
}

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}

function daysInFebruary (year)
{
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDate (year, month, day)
{   if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    if (!isInteger(s, false)) return false;

    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}


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 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 makeArray(n) {
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}



var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;