/////// VALIDATE.JS
/*
***** val_integer(field)
	ENTRY TIME: Checks to be sure field is an integer.
	If not, displays error message and selects field.

***** val_range(field, minNbr, maxNbr)
	ENTRY TIME: Checks to be sure field is an integer and within a range.
	If not, displays error message and selects field.

***** val_email(field)
	ENTRY TIME: Very BASIC test to confirm that e-mail address looks right.
	If not, displays error message and selects field.

***** checkout_val_email(eTest)
	CHECKOUT TIME: For use at submission time. Validates e-mail address.
	Returns "ok" or "no".

***** val_phone(field)
	ENTRY TIME: Allows only characters used in phone numbers -- 0123456789()- .
	If invalid characters are present, displays error message and selects field.
	SEE CHECKPHONE.

***** checkPhone(myTemplate,theNumber,myLocation)
	ENTRY TIME: Validate that a phone number conforms to a template.
	Send the template, the value, and the location to return to if there's an error.
	If not, display error message and select field.

***** checkNumber(myTemplate,theNumber,myLocation,myNumType)
	ENTRY TIME: Validate that ANY number conforms to a template.
	Similar to previous, except also send a description of the field for use
	in the alert box.
	If number is incorrect, display error message and select field.

***** checkNumberCheckout(myTemplate,theNumber)
	CHECOUT TIME: Similar to previous.
	9=Any number  Other characters play themselves
	USAGE:
	if (checkNumberCheckout('FORMAT',document.forms[0].FIELD.value)>0) {
		missingInfo += "\n  -  MESSAGE.";
		nbrOfErrors++;
	}
	Returns 0 if field is empty or number is OK, 1 if number is no good.

***** email_ok(myEmail)
	CHECKOUT: Yet another e-mail test.
	Pass it the e-mail address value.
	Returns "approved" value:
   0 = OK
	+1 = None or too many @ signs
	+2 = No dot following the @ sign
	+4 = Not enough characters after the final dot
	+8 = Invalid characters present
	+16= No user (just a domain)

***** validate_2part_date(myCardMonth,myCardYear)
	CHECOUT: Determines wither a month-year date (2 fields) is valid.
	Returns "missingInfo" -- if blank, date was OK.

***** warnView()
	Displays standard warning before showing preview.

***** textCounter(field, countfield, maxlimit)
	Displays a running counter for data entry. Halts entry
	when maximum number of characters have been entered for field.
	INPUT FIELDS USAGE:
	Replace ALL CAPS names with appropriate values
	<input type="text" name="FIELDNAME" size="MAXIMUM"
	onFocus="textCounter(this.form.FIELDNAME,this.form.remLen,MAXIMUM);"
	onKeyDown="textCounter(this.form.FIELDNAME,this.form.remLen,MAXIMUM);"
	onKeyUp="textCounter(this.form.FIELDNAME,this.form.remLen,MAXIMUM);">
	DISPLAY FIELD USAGE
	<input readonly type=text name=remLen size=4
	maxlength=3 value="" tabindex="-1"> characters left</font>
	NOTE: readonly works only with MSIE and NETSCAPE 6
	NOTE: tabindex="-1" works only with MSIE and NETSCAPE 6

***** checkLanguage()
	Figures out what language the BROWSER is set to and returns
	that value to "document.forms[0].language_name.value".

***** find_special_characters()
	Examines all text field in document.forms[0] to determine
	whether they contain any characters above 127.
	Returns "yes" or "no".

***** upperMe(field) and lowerMe(field)
   Typical use: onBlur="lowerMe(this)"
   Returns lower (or upper) case to field.

***** validateURL(myURL, noHTTP)
	Typical use: if (validateURL(document.forms[0].website.value, false)==false) ...
	Returns true if URL is OK, false if it is not

***** isAllCaps(myText)
	Typical use: if (isAllCaps(document.forms[0].FIELD.value)==false) ...
	Returns true if field is all caps.

*/

/////// DOES A FIELD CONTAIN AN INTEGER?
function val_integer(field) {
var theseOK = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (theseOK.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Sorry! This field must contain numbers only -- \n no letters or punctuation.");
field.focus();
field.select();
   }
}

/////// INTEGER WITHIN A RANGE
function val_range(field, minNbr, maxNbr) {
var theseOK = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (theseOK.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Sorry! This field must contain numbers only -- \n no letters or punctuation.");
field.focus();
field.select();
   }
   if (field.value < minNbr || field.value> maxNbr) {
   alert("Sorry! This field must contain a value \n between " + minNbr
         +" and " + maxNbr + ".");
   field.focus();
   field.select();
   }
}

/////// VALIDATE AN E-MAIL FIELD
function val_email(field) {
var atCount = 0;
var dotCount = 0;
var atLoc = 0;
var dotLoc = 0;
var eTest = field.value;
// IF THERE IS NO ENTRY, CONTINUE
if (eTest.length> 0) {
// FIND LOCATION FOR AND NUMBER OF @ SIGNS
	for (var i=0; i<eTest.length; i++) {
   	 if (eTest.charAt(i) == "@"){
			atLoc = i;
			atCount++;
		 }
// FIND LOCATION FOR AND NUMBER OF DOTS
    	if (eTest.charAt(i) == "."){
			dotLoc = i;
			dotCount++;
		 }
	}
// IT'S ASSUMED OK IF WE HAVE 1 @ SIGN BEFORE 1 DOT
	if (atCount != 1 || dotCount < 1 || dotLoc < atLoc) {
   	alert("Please enter a valid e-mail address.");
   	field.focus();
   	field.select();
   	}
	}
}

/////// VALIDATE AN E-MAIL FIELD AT CHECOUT
function checkout_val_email(eTest) {
var atCount = 0;
var dotCount = 0;
var atLoc = 0;
var dotLoc = 0;
var thisAddress = "ok";
// IF THERE IS NO ENTRY, CONTINUE
if (eTest.length> 0) {
// FIND LOCATION FOR AND NUMBER OF @ SIGNS
	for (var i=0; i<eTest.length; i++) {
   	 if (eTest.charAt(i) == "@"){
			atLoc = i;
			atCount++;
		 }
// FIND LOCATION FOR AND NUMBER OF DOTS
    	if (eTest.charAt(i) == "."){
			dotLoc = i;
			dotCount++;
		 }
	}
// IT'S ASSUMED OK IF WE HAVE 1 @ SIGN BEFORE 1 DOT
	if (atCount != 1 || dotCount < 1 || dotLoc < atLoc) {
   	thisAddress = "no";
   	}
	}
	return(thisAddress);
}

/////// INCOMPLETE CHECK FOR PHONE NUMBERS.
function val_phone(field) {
var theseOK = "0123456789()- ."
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (theseOK.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Sorry! This field must contain only numbers, dashes, or parentheses.");
field.focus();
field.select();
   }
}

/////// ADDED 10/18/2000 6:21PM
/////// CHECK FORMAT OF A PHONE NUMBER
/////// 9=ANY NUMBER  OTHER CHARACTERS PLAY THEMSELVES
function checkPhone(myTemplate,theNumber,myLocation){
	myMsg = "";
	numberOK = "yes";
	myNumbers = "0123456789";
	// Allow blank entry to go through; if required, check elsewhere
	if (myTemplate.length != theNumber.length && theNumber.length > 0) {
		myMsg=myMsg + "\nWrong number of characters. The number should be\n" +
		myTemplate.length + " characters long, including formatting characters.";
	} else if (theNumber.length > 0) {
	// Check individual characters here
	// Loop through number and compare each character to template
		temLength = myTemplate.length;
		nbrLength = theNumber.length;
		for (var i=0; i<temLength; i++) {
			tempNbr = "" + theNumber.substring(i, i+1);
			tempTemplate = "" + myTemplate.substring(i, i+1);
			// Does the template have a non-number here?
			if (tempTemplate != "9") {
				// It's a formatting character
				if (tempNbr != tempTemplate) {numberOK = "no"};
			} else {
				// It should be a number, then
				if (myNumbers.indexOf(tempNbr) == "-1") {numberOK = "no"};
			}
		}
		if (numberOK != "yes") {
			myMsg = myMsg + "\nThe number must be in this format: " + myTemplate + "."
		}
	}

	if (myMsg != "") {
		myMsg = "The phone number's format is incorrect:" + myMsg;
		alert(myMsg);
		myLocation.focus();
		myLocation.select();
	}
}

/////// ADDED 2/14/2001 8:34AM
/////// CHECK FORMAT OF ANY NUMBER
/////// 9=ANY NUMBER  OTHER CHARACTERS PLAY THEMSELVES
function checkNumber(myTemplate,theNumber,myLocation,myNumType){
	myMsg = "";
	numberOK = "yes";
	myNumbers = "0123456789";
	// Allow blank entry to go through; if required, check elsewhere
	if (myTemplate.length != theNumber.length && theNumber.length > 0) {
		myMsg=myMsg + "\nWrong number of characters. The " + myNumType + " should be\n" +
		myTemplate.length + " characters long.";
	} else if (theNumber.length > 0) {
	// Check individual characters here
	// Loop through number and compare each character to template
		temLength = myTemplate.length;
		nbrLength = theNumber.length;
		for (var i=0; i<temLength; i++) {
			tempNbr = "" + theNumber.substring(i, i+1);
			tempTemplate = "" + myTemplate.substring(i, i+1);
			// Does the template have a non-number here?
			if (tempTemplate != "9") {
				// It's a formatting character
				if (tempNbr != tempTemplate) {numberOK = "no"};
			} else {
				// It should be a number, then
				if (myNumbers.indexOf(tempNbr) == "-1") {numberOK = "no"};
			}
		}
		if (numberOK != "yes") {
			myMsg = myMsg + "\nThe " + myNumType + " must be in this format: " + myTemplate + "."
		}
	}

	if (myMsg != "") {
		myMsg = "The " + myNumType + "'s format is incorrect:" + myMsg;
		alert(myMsg);
		myLocation.focus();
		myLocation.select();
	}
}

/////// ADDED 2/17/2001 1:30PM - CHECKOUT ROUTINE
/////// FUNCTION TO CHECK FORMAT OF A NUMBER
function checkNumberCheckout(myTemplate,theNumber){
	myMsg = "";
	numberOK = "0";
	myNumbers = "0123456789";
	// Allow blank entry to go through; if required, check elsewhere
	if (myTemplate.length != theNumber.length && theNumber.length > 0) {
		numberOK = "1";
	} else if (theNumber.length > 0) {
	// Check individual characters here
	// Loop through number and compare each character to template
		temLength = myTemplate.length;
		nbrLength = theNumber.length;
		for (var i=0; i<temLength; i++) {
			tempNbr = "" + theNumber.substring(i, i+1);
			tempTemplate = "" + myTemplate.substring(i, i+1);
			// Does the template have a non-number here?
			if (tempTemplate != "9") {
				// It's a formatting character
				if (tempNbr != tempTemplate) {numberOK = "1"};
			} else {
				// It should be a number, then
				if (myNumbers.indexOf(tempNbr) == "-1") {numberOK = "1"};
			}
		}
	}
return (numberOK);
}

//////// Added 2/19/2001 9:44AM - CHECKOUT for email
function email_ok(myEmail){
	approved = 0;
	atCount = 0;
	dotCount = 0;
	firstAT = "";
	lastAT = 0;
	lastDOT = 0;
	badCount = 0;
	badChar = " !#$%^&*()+={}[]|\\/<>";
	// IF NO ENTRY, SKIP TEST
	if (myEmail.length > 0) {
		// START THE LOOP
		for (var i=0; i<myEmail.length; i++) {
			// COUNT @ SIGNS, FIND LOCATION OF LAST ONE
   		if (myEmail.charAt(i) == "@"){
			 	// FIND LOCATION OF FIRST @ SIGN
			 	if (firstAT == "") firstAT = i;
				lastAT = i;
				atCount++;
		 	}
			// COUNT DOTS, FIND LOCATION OF LAST ONE
    		if (myEmail.charAt(i) == "."){
				lastDOT = i;
				dotCount++;
		 	}
		 	// ANY BAD CHARACTERS PRESENT?
		 	if (badChar.indexOf(myEmail.charAt(i)) != -1) {
		 		badCount++;
		 	}
		} // END LOOP
		// VALID OR NOT?
		if (atCount != 1) approved += 1;
		if (lastAT > lastDOT) approved += 2;
		if (myEmail.length < lastDOT + 2) approved += 4;
		if (badCount > 0) approved += 8;
		if (firstAT == 0) approved += 16;
	} // END LENGTH IF
	return(approved)
} // END FUNCTION

// VALIDATES A MONTH-YEAR DATE IN 2 PARTS 2/20/2001 9:42AM
function validate_2part_date(myCardMonth,myCardYear) {
  var today = new Date()
  myYear=today.getFullYear();
  myMonth=today.getMonth()+1;
  myMissingInfo="";
  if (myCardMonth < 0 || myCardMonth > 12) {
    // month is out of range
    myMissingInfo += "\n  -  Credit card month must be between 1 and 12.";
  } else
  {
  	 longYear = myCardYear.toString();
    if (longYear.length < 4) {
      // year is not 4 digits
      myMissingInfo += "\n  -  Credit card year must be 4 digits.";
    } else
    {
        if (myCardYear < myYear) {
  	     // expiration date is before this year
          myMissingInfo += "\n  -  This credit card seems to have expired.";
        } else
        {
  	       // expiraton year is valid; check month
  	       if (myCardYear == myYear && myCardMonth < myMonth) {
            myMissingInfo += "\n  -  This credit card seems to have expired.";
  	       }
  	     }
    }
  }
return (myMissingInfo);
}

// Warning about preview added 2/22/2001 11:28AM
function warnView() {
	warnMsg =
	"THE PREVIEW IS NOT INTENDED TO ACCURATELY DISPLAY:"
	+"\n   - The typeface or typeface size."
	+"\n   - Precise positioning of any element."
	+"\n   - Exact size or positioning of logos."
	+"\n   - Color."
	+"\n\nIN SOME CASES YOU MAY SEE:"
	+"\n   - Line breaks that will not exist in the printed piece."
	+"\n   - Colors that do not match the printed piece."
	+"\n   - Lowercase characters where you expect small caps."
	+"\n\nTHE PREVIEW IS INTENDED ONLY TO ALLOW YOU"
	+"\nTO CONFIRM THAT SPELLING AND LOGOS ARE CORRECT.";
	alert(warnMsg);
}

// CHARACTER COUNTER added 3/9/2001 8:13AM
// Original:  Ronnie T. Moore; Dynamic 'fix' by: Nannette Thacker
// Additional mods for positioning and tab index by Bill Blinn
function textCounter(field, countfield, maxlimit) {
	// if too long...trim it!
   if (field.value.length > maxlimit)
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else
	countfield.value = maxlimit - field.value.length;
}

// What language is the browser using? Added 3/14/2001 12:39PM
function checkLanguage() {
  if (navigator.appName == 'Netscape')
  var myLanguage = navigator.language;
  else
  var myLanguage = navigator.browserLanguage;
  document.forms[0].browser_language.value=myLanguage;
  if (myLanguage.indexOf('en') > -1) document.forms[0].language_name.value = 'English';
  else if (myLanguage.indexOf('nl') > -1) document.forms[0].language_name.value = 'Dutch';
  else if (myLanguage.indexOf('fr') > -1) document.forms[0].language_name.value = 'French';
  else if (myLanguage.indexOf('de') > -1) document.forms[0].language_name.value = 'German';
  else if (myLanguage.indexOf('ja') > -1) document.forms[0].language_name.value = 'Japanese';
  else if (myLanguage.indexOf('it') > -1) document.forms[0].language_name.value = 'Italian';
  else if (myLanguage.indexOf('pt') > -1) document.forms[0].language_name.value = 'Portuguese';
  else if (myLanguage.indexOf('es') > -1) document.forms[0].language_name.value = 'Spanish';
  else if (myLanguage.indexOf('sv') > -1) document.forms[0].language_name.value = 'Swedish';
  else if (myLanguage.indexOf('zh') > -1) document.forms[0].language_name.value = 'Chinese';
  else document.forms[0].language_name.value = 'Not known';
}

// Any special characters present? Added 5/3/2001 8:54AM
function find_special_characters() {
	myBigBlob = "";
	mySpecChar = "no";
	// CONCATENATE ALL TEXT FIELDS
	for (i=0; i<document.forms[0].elements.length; i++) {
		if (document.forms[0].elements[i].type == "text") myBigBlob += document.forms[0].elements[i].value;
	}
	// LOOK FOR ANY OCCURRENCE OF A SPECIAL CHARACTER
	// END WHEN YOU FIND *1* OR AT END OF STRING
	for (i=0; i<myBigBlob.length; i++) {
		test = 0 + myBigBlob.charCodeAt(i);
		if (test-0 > 127) mySpecChar = "yes";
		if (mySpecChar == "yes") break;
	}
	return(mySpecChar);
}

function lowerMe(field) {
	field.value = field.value.toLowerCase();
}
function upperMe(field) {
	field.value = field.value.toUpperCase();
}

function DEBUG_INPUTS() {
	var myList = "Variables in this form";
	var howMany = document.forms[0].elements.length;
	for (i=0; i<howMany; i++) {
	myList += '\n'+ document.forms[0].elements[i].type;
		// TEXT FIELDS
		if (document.forms[0].elements[i].type=="hidden") {
			myList += ' - '+document.forms[0].elements[i].name;
			myList += ': '+document.forms[0].elements[i].value;
		}
		if (document.forms[0].elements[i].type=="text") {
			myList += ' - '+document.forms[0].elements[i].name;
			myList += ': '+document.forms[0].elements[i].value;
		}
		// SELECTION LISTS
		if (document.forms[0].elements[i].type=="select-one") {
			myList += ' - '+document.forms[0].elements[i].name;
			myList += ': '+document.forms[0].elements[i].options[document.forms[0].elements[i].selectedIndex].value;
		}
		// CHECK BOXES
		if (document.forms[0].elements[i].type=="checkbox") {
			myList += ' - '+document.forms[0].elements[i].name;
			myList += ': '+document.forms[0].elements[i].checked;
		}
		// RADIO BUTTONS
		if (document.forms[0].elements[i].type=="radio") {
			myList += ' - '+document.forms[0].elements[i].name;
			myList += ': '+document.forms[0].elements[i].value;
			myList += '-'+document.forms[0].elements[i].checked;
		}
	}
	alert (myList);
}

function validateURL(myURL, noHTTP) {
	// Minimum length is 5 if we don't check for http
	// or 12 if we do.
	urlOK = true;
	minURL = 5;
	urlLength = 0;
	// Allow blank URL (if we need to ensure that a URL is present, do it elsewhere)
	urlLength = myURL.length;
	if (urlLength == 0) return (true);
	// Continue with other tests if something is there
	// Locate the first dot and the last dot (they may be the same)
	firstDot = 0;
	lastDot = 0;
	for (var i=0; i<myURL.length; i++) {
  		if (myURL.charAt(i) == "." && firstDot == 0)firstDot = i;
		if (myURL.charAt(i) == ".") lastDot = i;
		// Two sequential dots are not permitted
		if (lastDot-firstDot<2 && lastDot-firstDot>0) {
			urlOK = false;
			break;
		}
	}
	// String must begin with http://
	if (!noHTTP) {
		if (myURL.indexOf("http://") != 0) urlOK = false;
		minURL = 12;
	} else {
		if (myURL.indexOf("http://") == 0) urlOK = false;
	}
	// URL must be at least 12 characters: http://a.com
	if (urlLength < minURL) urlOK = false;
	// URL must contain at least one dot
	if (firstDot == 0) urlOK = false;
	// URL must contain at least 2 characters after the final dot
	if (urlLength - lastDot < 3) urlOK = false;
	// URL cannot contain any characters but a-z, 0-9, and hyphen.
	var theseOK = "0123456789-:/.ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for (var i=0; i<urlLength; i++) {
		temp = "" + myURL.substring(i, i+1).toUpperCase();
		if (theseOK.indexOf(temp) == "-1") {
			urlOK = false;
			break;
		}
	}
	if (!urlOK) {
		return (false);
	} else {
		return (true);
	}
}

function isAllCaps(myText) {
	var returnCaps = true;
	if (myText != myText.toUpperCase()) returnCaps = false;
	return (returnCaps);
}

function CheckEmail(CheckMe) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(CheckMe))
	{
		return (true)
	}
	return (false)
}
