<!--
// ---------------------------------------------------------
// Author:			Massimo Mirabito Max (TRW/CISSS)
//					Jerrie Givens (EDS/CISSS)
// Organization:	HHS/CDC 
// Date:			6/30/1998
// Date Revised:	2/11/1999
//					6/24/1999
//					6/30/1999
//					9/13/1999
// Description:		JavaScript Client-side form validation code
// History:			6/30/1998	Coded the HTML page
//					2/11/1999	Per customer request added the 
//								abiltiy for users with international
//								order to skip the state field as a
//								required entry
//					6/24/1999	Per customer request added quantity 
//								validation per item
//					6/30/1999	Resolved browser compatability with IE and NS
//					9/13/1999	Per customer request modifed Min Max qunatities
// ---------------------------------------------------------

// ---------------------------------------------------------
// Global variables declaration
// ---------------------------------------------------------
month = 0;
day   = 0;
year  = 0;
DateInfo = new Object();
DateInfo.ErrorType = null;

// ---------------------------------------------------------
// Function declaration
// Function GetCookieInfo. Used to extract cookie information
// from a PC it will perform the following:
// 1) Split the cookie into tokens
// 2) form element equals cookie key
// 3) The value part of the cookie cannot meet this condition
// 4) If the form element is a dropdown list box select the item
// 5) Set the form element value to the cookie value
// 6) If a cookie existed set the "C_savecookie" to checked
// ---------------------------------------------------------
function GetCookieInfo()
{
var form;
var i, c, val, x;
var token, keyvalue, values;
CookieIsAvailable = false;

form = document.TBorderform;
token = document.cookie.split("|");

for (i = 0; i < token.length; i++) {
	keyvalue = token[i].split("=");
	for (c = 0; c < form.length; c++) {
		if (form.elements[c].name == keyvalue[0]) {													
			if ((keyvalue[1] != "") && (keyvalue[1] != null) && (keyvalue[1] != "null")) {	
				CookieIsAvailable = true;
				// used for dropdown list box
				if ((form.elements[c].type == "select-one")) {		
					x = parseInt(unescape(keyvalue[1]));
					if (x >= 0)	form.elements[c].options[x].selected = true;
					
				}
				else 
					form.elements[c].value = unescape(keyvalue[1]);
				break;
			}
		}
	}
}
if (CookieIsAvailable)
	form.C_savecookie.checked = true;

}

// ---------------------------------------------------------
// Function declaration
// Function SeCookieInfo. Make the cookie expire after on year
// ---------------------------------------------------------
function SetCookieInfo(form)
{
var s, expiration;
expiration = new Date();
expiration.setYear(expiration.getYear() + 1)

s =	form.T_last.name + "=" + escape(form.T_last.value) + "|" + 
	form.T_first.name + "=" + escape(form.T_first.value)  + "|" + 
	form.T_Add1.name + "=" + escape(form.T_Add1.value) + "|" + 
	form.T_Add2.name + "=" + escape(form.T_Add2.value) + "|" + 
	form.T_Add3.name + "=" + escape(form.T_Add3.value) + "|" + 
	form.T_City.name + "=" + escape(form.T_City.value) + "|" + 
	form.T_Zip.name + "=" + escape(form.T_Zip.value)  + "|" + 
	form.T_Country.name + "=" + escape(form.T_Country.value)  + "|" + 
	form.T_phone1.name	+ "=" + escape(form.T_phone1.value) + "|" + 
	form.T_phone2.name	+ "=" + escape(form.T_phone2.value) + "|" + 
	form.T_phone3.name	+ "=" + escape(form.T_phone3.value) + "|" + 
	form.T_email.name	+ "=" + escape(form.T_email.value) + "|" + 
	form.T_title.name	+ "=" + escape(form.T_title.value);

if (form.D_state.selectedIndex != 0) 	
	s += "|" + form.D_state.name + "=" + escape(form.D_state.selectedIndex);

if (form.D_Profession.selectedIndex != 0) 	
	s += "|" + form.D_Profession.name + "=" + escape(form.D_Profession.selectedIndex);

if (form.D_Employer.selectedIndex != 0) 	
	s += "|" + form.D_Employer.name + "=" + escape(form.D_Employer.selectedIndex);
	
// Currently NIP is the only one requring the users to select the web order source	
if (form.FormName == "NIPOrderForm") {		
	if (form.D_Source.selectedIndex != 0) 	
		s += "|" + form.D_Source.name + "=" + escape(form.D_Source.selectedIndex);
}

s += ";expires=" + expiration.toGMTString()

document.cookie = s;
}

// ---------------------------------------------------------
// Function declaration
// Function IsLeapYear determines if the year specified is 
// a leap year
// ---------------------------------------------------------
function IsLeapYear(year) 
{
if (((year % 4) == 0) && ((year % 100)!= 0) || ((year % 400)== 0)) return (true);
else return (false);
}

// ---------------------------------------------------------
// Function GetDaysInMonth determines how may day are in the 
// specified month
// ---------------------------------------------------------
function GetDaysInMonth(month)
{
var days;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)  days = 31;
else if (month == 4 || month == 6 || month == 9 || month == 11) days = 30;
else if (month == 2) {
	if (IsLeapYear(year)) days = 29;
   else days = 28;
}
return days
}
// ---------------------------------------------------------
// Function IsDate validates a date entered in a form field
// ---------------------------------------------------------
function IsDate(e, DTinfo)
{
var s = e.value;

DTinfo.ErrorType = null
// ---------------------------------------------------------
// Get Month, day and year by extracting it from the form element
// ---------------------------------------------------------
month = s.substring(0, s.indexOf("/"));
day   = s.substring(s.indexOf("/") + 1, s.lastIndexOf("/"));
year  = s.substring(s.lastIndexOf("/") + 1, s.length);

month = parseInt(month);
day = parseInt(day);
year = parseInt(year);

// ---------------------------------------------------------
// check the month number for validity
// ---------------------------------------------------------
if (month  > 12) {
	DTinfo.ErrorType = "month";
	return false;
}
// ---------------------------------------------------------
// check the month number for validity
// ---------------------------------------------------------
if (day > GetDaysInMonth(month)) 	{
	DTinfo.ErrorType = "day";
	return false;
}
return true;
}

// ---------------------------------------------------------
// Function IsFormatOK that the form element was entered in 
// the correct format using the formatstyle attribute.
// For example (###)###-####. # = any nymber @ = any charcter, 
// m = month, d = day, y = year
// ---------------------------------------------------------
function IsFormatOK(e)
{
var i, j;
var okNum = "0123456789";
var s, formatstyle, ch, f, chformat, bOK;

bOK = true
s = e.value;
f = e.formatstyle;
chformat = f.split(";");

for (j = 0; j < chformat.length; j++) {
	bOK = true
	for (i = 0; i < chformat[j].length; i++) {
		if (chformat[j].length != s.length) {
			bOK = false; 
			break;
		}
		ch = s.charAt(i);
		if ((chformat[j].charAt(i) == "#"))	{
			if (okNum.indexOf(ch, 0) == -1) {
				bOK = false; 
 				break;
			}
			else continue;
		}
		// Date format logic
		if ((chformat[j].charAt(i) == "m") || (chformat[j].charAt(i) == "d") || (chformat[j].charAt(i) == "y"))	{
			if (okNum.indexOf(ch, 0) == -1) {
				bOK = false; 
 				break;
			}
			else continue;
		}

		if (chformat[j].charAt(i) == "@") contiune;
		if (ch != chformat[j].charAt(i)){
			bOK = false; 
			break;
		}
	}
	if (bOK) break;
}
return bOK;
}

// ---------------------------------------------------------
// Function IsWithinRange validates that a numeric form 
// element is within rage using the min and max attributes
// ---------------------------------------------------------
function IsWithinRange(e)
{
if (IsBlank(e)) {
	e.value = "";
	return true;
}
var n = parseInt(e.value);
if (!(n >= e.min && n <= e.max)) return false;
return true;
}

// ---------------------------------------------------------
// Function IsNumeric validates that a numeric form element 
// is numeric
// ---------------------------------------------------------
function IsNumeric(e)
{
var ch, i, j;
var okNum = "0123456789";
var s = e.value;

for (i = 0; i < s.length; i++) {
   ch = s.charAt(i);
	for (j = 0; j < okNum.length; j++) {
		if (ch == okNum.charAt(j)) 
			break;
	}
	if (j == okNum.length) return false;
}
return true;
}

// ---------------------------------------------------------
// Function IsBlank validates that form element only has 
// white spaces or null
// ---------------------------------------------------------
function IsBlank(e)
{
var i, ch;
var s = e.value;
if (s == null) return true;
if (s == "") return true;

for (i = 0; i < s.length; i++) {
   ch = s.charAt(i);
   if((ch != '') && (ch != '\n') && (ch != '\t')) return false;
}
return true;
}

// ---------------------------------------------------------
// Function ValidateForm loops through all form elements 
// and validates the contenct, range, type and format
// ---------------------------------------------------------
function ValidateForm(form)
{

var e, s;
// ---------------------------------------------------------
// Validate the from input filed
// ---------------------------------------------------------
for (var i = 0; i < form.length; i++) {
	e = form.elements[i];
	s = e.name;

	if ((e.type == "text") || (e.type == "textarea")) {
		if (e.format && (e.value != "")) {
			if (!IsFormatOK(e)) {
				alert("Invalid format.\nPlease enter the " + e.displayname + " with the following format " + e.formatstyle);
				e.select()
				e.focus();
				return false;
			}
		}
		if (e.numeric){		// Text box requires a numeric value check for numeric and then check if ther is a range attribute
			if (!IsNumeric(e)) {
				alert("The Quatity entered for publication " + e.displayname + " must be a numeric value.");
				e.select();
				e.focus();
				return false;
			}
			if (e.range) {
				if (!IsWithinRange(e)) {
					alert(e.message);
					e.select();
					e.focus();
					return false;
				}
			}
		}
		if (e.limit != null) {
			if ((e.value).length > e.limit) {
				alert("You have exceed the maximum limit of " + e.limit + " charcters in " + e.displayname + " Please shorten the entry.");
				e.select();
				e.focus();
				return false;
			}
		}
		if (e.constraint) {
			if (eval(e.constrainteval)) {
				alert(e.constraintmessage);
				e.select();
				e.focus();
				return false;
			}
		}
		if (e.required) {
			if (e.requiredeval != null) {
				if (eval(e.requiredeval)) {
					alert(e.message);
					e.select();
					e.focus();
					return false;
				}
				else continue;
			}

			if (IsBlank(e)) {
				alert(e.message);
				e.select();
				e.focus();
				return false;
			}
		}
		if (e.date){		// Text box requires a date field
			if (!IsDate(e)) {
				alert("Invalid Date.\n'" + e.displayname + "' is an invalid.");
				e.select();
				e.focus();
				return false;
			}
		}
	}

	if ((e.type == "select-one") || (e.type == "select-multiple")) {		// Dropdown list box with single or multi select
		if ((e.required)) {													// Value is required
			if (e.requiredeval != null) {									// Vlaue now has to pass a required evaluation
				if (eval(e.requiredeval) && (e.selectedIndex == 0)) {
					alert("Required Entry.\n" + e.displayname + " is required. Please select a value from the dropdown list box.");
					e.focus();
					return false;
				}
			}
			else if (e.selectedIndex == 0){														// Is a required field but dous not need a required evaluation
				alert("Required Entry.\n" + e.displayname + " is required. Please select a value from the dropdown list box.");
				e.focus();
				return false;
			}
		}
	}
}	
return true;
}


// ---------------------------------------------------------
// Function ShowCountry displays in a new window a list of 
// countries
// ---------------------------------------------------------
function ShowCountries()
{
	open('countrylist.asp','CountryList','width=350,height=375');
}

// ---------------------------------------------------------
// Function SetItemMinMaxQuatity used to set MIN and MAX 
// attributes per lineitem for the quantity ordered
// ---------------------------------------------------------
function SetItemMinMaxQuatity(form, min, max)
{
	form.min = min;
	form.max = max;
	form.message = "Invalid Quantity.\nThe Quantity entered for publication '" + form.displayname + 
				   "' must be between '" + form.min + "' and '" + form.max + "'";
}

//-->
