// BROWSER CHECKING
var browserVersion = parseInt(navigator.appVersion);
var browserName = navigator.appName;
var MSIE4 = ((document.all) && (browserVersion >= 4)) ? true : false;
var NN6 = ((document.getElementById) && (!document.all) && (!window.opera)) ? true : false;
var NN4 = false;
if ((browserName == 'Netscape') && (browserVersion == 4)) {
	NN4 = true;
}

var MENU_BORDER_COLOR				= '#CEDFF6';
var MENU_BACKGROUND_COLOR			= '#CEDFF6';
var MENU_TEXT_COLOR					= '#4B92D9';
var MENU_BORDER_MOUSEOVER_COLOR		= '#FFFFFF';
var MENU_BACKGROUND_MOUSEOVER_COLOR	= '#4B92D9';
var MENU_TEXT_MOUSEOVER_COLOR		= '#FFFFFF';

function lastUpdated() {
	var datUpdated = new Date(document.lastModified) ;
	var datMonth = datUpdated.getMonth() + 1 ;
	var datDate = datUpdated.getDate() ;
	var datYear = datUpdated.getYear() ;
	document.write(datMonth + "/" + datDate + "/" + datYear) ;
}

function changeBGColor(cursor, id) {
	var theTblObj;
	
	if (MSIE4) {
		theTblObj = eval('document.all.nav' + id);
	} else {
		theTblObj = document.getElementById("nav" +id);
	}

	if (cursor == 'on') {
		theTblObj.style.borderColor = MENU_BORDER_MOUSEOVER_COLOR;
		theTblObj.style.backgroundColor = MENU_BACKGROUND_MOUSEOVER_COLOR;
		theTblObj.style.color = MENU_TEXT_MOUSEOVER_COLOR;
	} else {
		theTblObj.style.borderColor = MENU_BORDER_COLOR;
		theTblObj.style.backgroundColor = MENU_BACKGROUND_COLOR;
		theTblObj.style.color = MENU_TEXT_COLOR;
	}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function goTo(url) {
	document.location.href = url;
}

/********************	START VALIDATION FUNCTIONS			********************/
var errors;
var firstErrorField;
var goFlag = true;

function LTrim(str){
	// trims off leading whitespace
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		s = s.substring(j, i);
	}
	return s;
}

function RTrim(str){ 
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;       // Get length of string
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
	}
	return s;
}

function Trim(str){
	return RTrim(LTrim(str));
}

function validatePassword(username, passwd1, passwd2) {
	var word;
	var psd1 = eval('document.forms[0].' + passwd1 + '.value');
	word = Trim(psd1.value);
	psd1.value = word;
	var psd2 = eval('document.forms[0].' + passwd2 + '.value');
	word = Trim(psd2.value);
	psd2.value = word;

	if (psd1 != psd2) {
		errors += 'Passwords do not match. Please re-type them and try again.\n'
		eval('document.forms[0].' + passwd1 + '.value = ""');
		eval('document.forms[0].' + passwd2 + '.value = ""');
		if(firstErrorField == "") {
			firstErrorField = passwd1;
		}
		goFlag = false;
	}
}

function validate(fieldname, id, label, minChars, maxChars, restrictedChars){
	var foundError = false;
	// trim entry and assign to working variable
	var tmp = eval('document.forms[0].' + fieldname);
	word = Trim(tmp.value);
	tmp.value = word;
	var theObj;

	// check minimum value length ...  append error message if necessary
	var tmpLen = eval('document.forms[0].' + fieldname + '.value.length');
	if (tmpLen < minChars){
		if(tmpLen == 0){
			errors += label + ' may not be blank.  \n';
		}else{
			errors += label + ' must be at least ' + minChars + ' characters \n';
		}
		// if this is the first error, flag the field for focus()
		if(firstErrorField == ""){
			firstErrorField = fieldname;
		}
		// set error flags
		goFlag = false;
		foundError = true;
	}
	
	// check maximum value length ...  append error message if necessary
	if (eval('document.forms[0].' + fieldname + '.value.length > ' + maxChars)){
		errors += label + ' must be no more than ' + maxChars + ' characters. \n';
		// if this is the first error, flag the field for focus()
		if(firstErrorField == "") {
			firstErrorField = fieldname;
		}	
		// set error flag
		goFlag = false;
		foundError = true;
	}
	
	// check for restricted characters
	var foundChars = "";
	for (var i = 0; i < restrictedChars.length; i++){
		// get a restricted character
		var z = restrictedChars.charAt(i);
		// search field for it...
		var tmp=eval('document.forms[0].'+fieldname+'.value');
		// if found, append foundChars var
		if(tmp.indexOf(z) != "-1"){
			if(z == " ")z = "spaces";
			foundChars += z + ' ';
		}
	}

	// if errors were found, append message and flag field for focus()
	if(foundChars.length != ""){
		errors += label + ' may not contain ' + foundChars + '  \n';
		if(firstErrorField == "") {firstErrorField = fieldname;}	
		goFlag = false;
		foundError = true;
	}

	highlightErrors(id, foundError);
}

function requiredChars(field, id, label){
	var foundError = false;
	// checks for proper email syntax)
	var str=eval('document.forms[0].'+field+'.value');	
	if (window.RegExp) { // error check using regular expressions
		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,20}|[0-9]{1,3})(\]?)$/; // valid email format
		if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid

		}else{// invalid characters or non-standard format
			errors +=  label + ' \"' + str + '\" does not appear to be valid.   \n';
			if(firstErrorField == "") {firstErrorField = field;}	
			goFlag = false;
			foundError = true;
		}
	} else {// trivial error check for non-regexp supporting browsers 
		if(str.indexOf("@") == -1){ 
			errors +=  label + ' \"' + str + '\" does not appear to be valid.   \n';
			if(firstErrorField == "") {firstErrorField = field;}	
			goFlag = false;
			foundError = true;
		}
	}

	highlightErrors(id, foundError);
}

function highlightErrors(id, foundError) {
	if (foundError) {
		if (MSIE4) {
			theObj = eval('document.all.' + id);
		} else {
			theObj = document.getElementById(id);
		}
		theObj.style.color = '#FF0000';
	} else {
		if (MSIE4) {
			theObj = eval('document.all.' + id);
		} else {
			theObj = document.getElementById(id);
		}
		theObj.style.color = '#000000';
	}
}

function validateInit(){ 
	//reset global variables
	errors = "Please correct the following: \n\n"; 
	firstErrorField=""; 
	goFlag= true; 
}

function display(){	
	// if errors are detected, display them and return user to the first field with errors
	if(!goFlag){
		alert(errors);
		eval('document.forms[0].' + firstErrorField + '.focus()'); 
		return false;
	}else{
		return true;
	}
}
/********************		END VALIDATION FUNCTIONS		********************/

/********************	START CREATE AND VALIDATING USER	********************/
function doEmailSubmit(type) {
	goFlag = false;
	validateEmail(type);

	if (goFlag) {
		document.forms[0].submit();
	}
}

function validateEmail(type) {
	validateInit();

	if (type == 'contact') {
		validate('firstName', 'fn', 'First Name', 1, 50, '\'\"\<>{}[]#$%^&;:/\\');
		validate('lastName', 'ln', 'Last Name', 1, 50, '\"\<>{}[]#$%^&;:/\\');
		validate('subject', 'subj', 'Subject', 1, 50, '');
		requiredChars('email', 'em', 'E-mail');
	}

	if (type == 'requestpwd') {
		validate('firstName', 'fn', 'First Name', 1, 50, '\'\"\<>{}[]#$%^&;:/\\');
		validate('lastName', 'ln', 'Last Name', 1, 50, '\'\"\<>{}[]#$%^&;:/\\');
		validate('organization', 'org', 'Organization', 1, 100, '');
		validate('phone', 'ph', 'Phone Number', 1, 25, '');
		requiredChars('email', 'em', 'E-mail');
	}

	display();
}
/********************	END CREATE AND VALIDATING USER		********************/