/* 
Purpose: 		CLient-side functions for MREGS
Author's Name:	Al Everett
  
Date Initially Written:	2/9/2007

History
Date		By				Modification
10/3/2007	Al Everett		Added email confirmation
							Removed extra submission confirmation
6/9/2009	Al Everett		"Zip" to "ZIP"
*/


//alert('JS file loaded');

var alertText='Your browser is outdated. Please download the latest version of Internet Explorer to properly view this site.';

if(navigator.appName == 'Netscape') {
	if(navigator.appVersion.substring(0,1) < 5) {
		alert(alertText);
	}
}

if(navigator.appName == 'Microsoft Internet Explorer') {
	if(navigator.appVersion.substring(0,1) < 4) {
		alert(alertText);
	}
}

bName = navigator.appName;
bVer = parseInt(navigator.appVersion);
if ((bName == "Netscape" && bVer >= 3) || (bName == "Microsoft Internet Explorer" && bVer >= 4)) {
	br = "n3"; 
} else {
	br = "n2";
}

//if (br== "n3") { 
	img1on = new Image();
	img1on.src = "images/home_on.gif";
	img2on = new Image();           
	img2on.src = "images/agenda_on.gif";  
	img3on = new Image();
	img3on.src = "images/logistics_on.gif";
	img4on = new Image();
	img4on.src = "images/contact_on.gif";
	img5on = new Image();
	img5on.src = "images/reg_on.gif";
	img6on = new Image();
	img6on.src = "images/regbutton_on.gif";
	img7on = new Image();
	img7on.src = "images/exhibitor_on.gif";
	img8on = new Image();
	img8on.src = "images/gen_info_on.gif";

	img1off = new Image();          
	img1off.src = "images/home_off.gif"; 
	img2off = new Image();          
	img2off.src = "images/agenda_off.gif"; 
	img3off = new Image();
	img3off.src = "images/logistics_off.gif";
	img4off = new Image();
	img4off.src = "images/contact_off.gif";
	img5off = new Image();
	img5off.src = "images/reg_off.gif";
	img6off = new Image();
	img6off.src = "images/regbutton_off.gif";
	img7off = new Image();
	img7off.src = "images/exhibitor_off.gif";
	img8off = new Image();
	img8off.src = "images/gen_info_off.gif";
//}

// Function to "activate" images.
function imgAct(imgName) {//alert('act');
	//if (br == "n3") {
		document[imgName].src = eval(imgName + "on.src");
	//}
}

// Function to "deactivate" images.
function imgInact(imgName) {//alert('inact');
	//if (br == "n3") {
		document[imgName].src = eval(imgName + "off.src");
	//}
}

// -----------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// -----------------------------------------

function trim(str) {
	return str.replace(/^\s+|\s+$/g, '')
}

// Registration form
function toggleDisplayOtherDegree(formObj,formObjToReveal) {
	var showObj = document.getElementById(formObjToReveal);
	if (formObj.options[formObj.selectedIndex].value=="Other") {
		showObj.disabled=false;
	} else {
		showObj.value="";
		showObj.disabled=true;
	}
}

function validateRegistrationForm(formObj) {
	var errorMessage = "";
	var selectedCountry = "";
	
	if (!fieldHasValue(formObj.FirstName)) {
		errorMessage=errorMessage + "You must enter a First Name.\n";
	}
	if (!fieldHasValue(formObj.LastName)) {
		errorMessage=errorMessage + "You must enter a Last Name.\n";
	}
	if (!fieldHasValue(formObj.Address)) {
		errorMessage=errorMessage + "You must enter an Address Line 1.\n";
	}
	if (!fieldHasValue(formObj.City)) {
		errorMessage=errorMessage + "You must enter a valid city.\n";
	}

	selectedCountry = formObj.Country.options[formObj.Country.selectedIndex].value;

	if (!fieldHasValue(formObj.State)) {
		if (selectedCountry == "United States" || selectedCountry == "Canada") {
			errorMessage=errorMessage + "You must enter a State/Province.\n";
		}
	}
	if (!fieldHasValue(formObj.ZIP)) {
		if (selectedCountry == "United States" || selectedCountry == "Canada") {
			errorMessage=errorMessage + "You must enter a valid ZIP/postal code.\n";
		}
	}
	if (!(fieldHasValue(formObj.Email) && validEmail(formObj.Email))) {
		errorMessage=errorMessage + "You must enter a valid Email Address.\n";
	} else if (formObj.Email.value != formObj.confirmEmail.value) {
		errorMessage=errorMessage + "Please confirm your Email Address.\n";
	}
	/* Doesn't quite work
	if (fieldHasValue(formObj.userURL) && !validURL(formObj.userURL)) {
		errorMessage=errorMessage + "The URL does not appear to be valid.\n";
	}
	*/
	if (!validSessionSelection(formObj.sessionRegistrationIDs)) {
		errorMessage=errorMessage + "You must select at least one session to attend.\n";
	}
	
	//If there are errors, alert the user and return failure
	if (errorMessage != "") {
		alert(errorMessage);
		return false;
	} else {
		return true;
	}
}

function fieldHasValue(formFieldObj) {
	return !trim(formFieldObj.value)=="";
}

function validEmail(emailField) {
	var theEmail = trim(emailField.value);  // value of field with whitespace trimmed off
	var emailRegExp = /^[^@]+@[^@.]+\.[^@]*\w\w$/
	
	return emailRegExp.test(theEmail);
}

function validURL(URLfield) {
	var theURL=trim(URLfield.value);
	var urlRegExp=/^(http|https):\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9]+$/
	
	return urlRegExp.test(theURL);
}

function validSessionSelection(chkBox) {
	if (chkBox.length) {
		// check to make sure at least one checkbox is checked
		for (var i=0;i < chkBox.length;i++) {
			if (chkBox[i].checked) {
				return true;
				break;
			}
		}
		// Made it through the loop, so there mustn't be anything checked
		return false;
	} else {
		// No array, so no checkboxes. No validation needed.
		return true;
	}
}
		