/*
'Client-side script for handling user entry on forms

' focusElemError
'	displays alert message
'	sets focus to elem
'	returns false

' setTextFocus()
'	set focus to first text field
'	<BODY onLoad="setTextFocus()">

' setSelect()
'	set selected index
'	<BODY onLoad="setSelect('FieldName','Value')">

' validateFields()
'	validate mandatory form fields
'	form name="thisForm"
'	fieldnames passed in arrField array
'		arrField = new Array (); 
'		arrField[0] = "txtFirstName";
'		arrField[1] = "txtLastName";
'			if (validateFields(arrField, arrName) == false){
'				thisPage.cancelEvent = true;
'			}
'	fieldNames passed in arrName array
'		fieldNames used in error message
' -- or --
'		return validateFields(arrField, arrName);

' hiLight/unHiLight
'	sets background of form element in focus
'	<input name="user" type="text" onfocus="hiLight(this)" onblur="unHiLight(this)" size=20>

' credit card validation scripts
'	validates and encrypts credit card information
'	<input type="text" name="CreditCardNumber" size="30" value="" onFocus="return doCCStuff(this)">

*/

function focusElemError(elem, msg) {
// focuses on element
	alert(msg);
	elem.select();
	elem.focus();
	return false;
}

function setTextFocus() {
var i=0;
var j=0;
var currForm;
	if (document.forms.length > 0) {
		for (j = 0; j < document.forms.length; j++) {
			currForm = document.forms[j];
			if (currForm.length > 0) {
				for (i = 0; i < currForm.length; i++) {
					if (currForm.elements[i].type == 'text') {
						break;
					}
				}
			}
			break;
		}
		if (i != currForm.length) {
			currForm.elements[i].focus();
		}
	}
}

function replaceString(fullS,oldS,newS) {
// Replaces oldS with newS in the string fullS   
	for (var i=0; i<fullS.length; i++) {
		if (fullS.substring(i,i+oldS.length) == oldS) {
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length)
		}
	}
	return fullS;
}

function setSelect(sField, sValue) {
	var currField;
	currField = eval("document.thisForm." + sField);
	for (i = 0; i < currField.options.length; i ++) {
		if (currField.options[i].value == sValue) {
			currField.options.selectedIndex = i;
			break;
		}
	}
}

function setSelectElem(elem, val) {
	for (i = 0; i < elem.options.length; i ++) {
		if (elem.options[i].value == val) {
			elem.options.selectedIndex = i;
			break;
		}
	}
}

function setCheckbox(sField, sValue) {
	var currField;
	currField = eval("document.thisForm." + sField);
	if (currField.value == sValue) {
		currField.checked = 1;
	}
}

var newcolor = "#ffffdf";
var defaultcolor = "#ffffff";
function validateFields(arrField, arrName) {
	var formField;
	var msg = "";
	var alertField;
	var strPhone;
	for (i = 0; i < arrField.length; i ++) {
		formField = eval("document.thisForm." + arrField[i]);

		if (formField.type != null) {
			formField.style.backgroundColor= defaultcolor;
		}
		// validate email address format
		if (arrField[i].indexOf("email") != -1 || arrField[i].indexOf("Email") != -1) {
			if (formField.value == "" || formField.value.indexOf("@") == -1 || formField.value.indexOf('..') != -1 || formField.value.indexOf(' ') != -1 || formField.value.indexOf('.') == -1) {
				//window.alert("Please enter a valid email address in " + arrName[i] + ".");
				if (msg.length == 0) {
					alertField = formField;
				}
				msg += arrName[i] + ": invalid email address. \n";
				formField.style.backgroundColor = newcolor;
				//formField.focus();
				//return false;
				//break;
			}		
		}
		// validate phone format
		if (arrField[i].indexOf("phone") != -1 || arrField[i].indexOf("Phone") != -1) {
			if (msg.length == 0) {
				alertField = formField;
			}
			// See if phone is valid 
			if (!(phoneCheck(formField.value))) {
				msg += arrName[i] + ": invalid phone number. \n";
				formField.style.backgroundColor = newcolor;
			}
		}
		// validate password length
		if (arrField[i].indexOf("password") != -1 || arrField[i].indexOf("Password") != -1) {
			if (formField.value == "" || formField.value.length < 4) {
				//window.alert("Please enter a password that is at least 4 characters.");
				if (msg.length == 0) {
					alertField = formField;
				}
				msg += arrName[i] + ": your password must be at least 4 characters. \n";
				formField.style.backgroundColor = newcolor;
				//formField.focus();
				//return false;
				//break;
			}
		}
		// text, textarea, password or file
		if ((formField.type == "text" || formField.type == "textarea" || formField.type == "password" || formField.type == "file" ) && formField.value == ""){
			//window.alert(arrName[i] + " is required.");
				if (msg.length == 0) {
					alertField = formField;
				}
				msg += arrName[i] + ": required. \n";
				formField.style.backgroundColor = newcolor;
			//formField.focus();
			//return false;
			//break;
						
		} 
		// radio buttons
		if (formField.type == "radio") {
			var radioValue = "";
			var radio = thisForm.elements;
			var radioName = formField;
			radio = radio[radioName.name];
			
			for (j = 0; j < radio.length; j ++) {
				radio[j].style.backgroundColor = defaultcolor;
				if (radio[j].checked == true) {
					radioValue = radio[j].value;
				}
			}
			
			if (radioValue == "") {
				//window.alert("An option choice is missing for " + arrName[i] + ".");
				//formField.focus();
				//return false;
				//break;
				if (msg.length == 0) {
					alertField = formField;
				}
				msg += arrName[i] + ": select an item. \n";
				for (j = 0; j < radio.length; j ++) {
					radio[j].style.backgroundColor = newcolor;
				}
			}
		} 
		// select	
		if (formField.type == "select" || formField.type == "select-one" || formField.type == "select-multiple") {
			if (formField.options[formField.selectedIndex].value == "" ) {
				//window.alert("An option choice is missing for " + arrName[i] + ".");
				//formField.focus();
				//return false;
				//break;
				if (msg.length == 0) {
					alertField = formField;
				}
				msg += arrName[i] + ": select an item. \n";
				formField.style.backgroundColor = newcolor;
		}
		}
	}
	if (msg.length > 0) {
		window.alert("Your form cannot be submitted. Please enter the correct information: \n\n" + msg);
		if (alertField.type != "select" && alertField.type != "select-one" && alertField.type != "select-multiple") {
			alertField.select();
		}
		alertField.focus();
		return false;
	}
	return true;
}

// can also use styles_form.htc where class determines behavior instead
// of calling these in the input element:
// 		<input name="username" id="username" type="text" onfocus="hiLight(this)" onblur="unHiLight(this)" ....
// HOWEVER, as of 3/13/2003, HTCs are not supported by Netscape!
function hiLight(fieldInput){
	if (fieldInput.type == "button" || fieldInput.type == "submit" || fieldInput.type == "reset") {
		fieldInput.style.background = "#d0d0d0";
		fieldInput.style.borderStyle = "inset";
		fieldInput.style.borderWidth = "2";
		fieldInput.style.borderColor = "#d0d0d0";
	} else {
		fieldInput.style.background = "#ffffdf";
	}
}

function unHiLight(fieldInput){
	if (fieldInput.type == "button" || fieldInput.type == "submit" || fieldInput.type == "reset") {
		fieldInput.style.background = "#c5c5c5";
		fieldInput.style.borderStyle = "outset";
		fieldInput.style.borderWidth = "2";
		fieldInput.style.borderColor = "#c5c5c5";
	} else {
		fieldInput.style.background = "#ffffff";
	}
}

// Author: Javafile.com - http://javafile.com, function isCreditCard(st) by Netcape - http://www.netscape.com
// Permission granted to SimplytheBest.net to feature script in its DHTML scripts and JavaScripts collection
var encrypt_it = true;
/* ==================================================================
  FUNCTION: isCreditCard(st)
  INPUT:  st - a string representing a credit card number
  RETURNS: true, if the credit card number passes the Luhn Mod-10 test
         false, otherwise
  ================================================================== */
function isCreditCard(st) {
 // Encoding only works on cards with less than 19 digits
 if (st.length > 19)
    return (false);
  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
}
function getCCNum(default_val) {
 msg = 'Please enter your credit card number here. '
  + 'It will be '
  + ((encrypt_it) ? "encrypted and then " : "")
  + 'put into the credit card field of the form '
  + 'after it is validated.';
 return prompt(msg,default_val);
}
// takes in a credit card number, adds one to each digit
// (9 becomes 0), and then returns the encrypted credit
// card number with an 'e' tacked on to the end to signal
// the number has been encrypted
function encrypt(val) {
 val = "" + val;
 var result = "";
 for (i=0;i<val.length;i++) {
  character = val.charAt(i);
  if ("0123456789".indexOf(character) != -1) {
   character = parseInt(character);
   character = (character+1)%10;
  }
  result += character;
 }
 if (result != "")
  result += "e";
 return result;
}
function unencrypt(val) {
 val = "" + val;
 for (n=0;n<9;n++)
  val = encrypt(val);
 return (val.substring(0,val.indexOf('e')));
}
function strip(val) {
 val = "" + val;
 if (!val)
  return "";
 var result = "";
 for (i=0;i<val.length;i++) {
  character = val.charAt(i);
  if ("0123456789".indexOf(character) != -1)
   result += character;
 }
 return result;
}
var last_entry = "";

function doCCStuff(form_element) {
 if (blur_reset) {
  last_entry = form_element.value;
  if (last_entry && last_entry.indexOf('e') != -1)
   last_entry = unencrypt(last_entry);
  entry = getCCNum(last_entry);
  stripped_entry = strip(entry);
  while (entry && (!isCreditCard(stripped_entry))) {
   alert('The credit card number you entered could not be '
    + 'validated. Please check the number and try again.');
   last_entry = entry;
   entry = getCCNum(last_entry);
   stripped_entry = strip(entry);
  }
  if (entry) {
   if (encrypt_it)
    form_element.value = encrypt(entry);
   else
    form_element.value = entry;
  }
  blur_form(form_element);
 }
 return false;
}
var blur_reset = true;
function blur_form(form_element) {
 form_element.blur();
 blur_reset = false;
 setTimeout("blur_reset=true",2000);
}

