// preload images based on CSV string parameter

navImageArray = new Array();

function preLoad(navImageStr) {
	var comma = ",";
	navImages = navImageStr.split(comma);
	for (x=0; x < navImages.length; x++) {
		navImageArray[x] = new Image();
		navImageArray[x].src = navImages[x];
	}
}


// open window with glossary term lookup
function glossary(term) {
	window.open("/content/glossary.jsp?term=" + term, "glossaryWin","width=400,height=450,location=0,menubar=0,toolbar=0,scrollbars=yes");
	}

// open a URL in a new window of a given width & height
function popWin(theURL, popWidth, popHeight) {
	window.open(theURL, "messageWindow", "WIDTH=" + popWidth + ",HEIGHT=" + popHeight + ",toolbar=no,location=no,menubar=no,resizable=yes,titlebar=no");
	}


function isEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
}

function radioValue(field) {
	if (field.length) {
		for (i=0; i<field.length; i++) {
			if (field[i].checked) return field[i].value;
		}
		return '';
	} else {
		return field.value;
	}
}

function isCheckboxChecked(field){
	if (field.length){
		for (i=0; i<field.length; i++){
			if (field[i].checked)
				return field[i].value;
		}
		return '';
	}else{
		return '';
	}
}

//Match 5 or 5+4 digit zip code formats
function validateZip(zip) {
	if(zip.match(/(^\d{5}$)|(^\d{5}-{1}\d{4}$)/)) {return true;}
	else {return false;}
}

//Basic telephone regex - Match valid telephone characters: digits, whitespace, parentheses and hyphens (at least 10 valid characters)
function validatePhone(phone) {
	if(phone.match(/^[\d\s\(\)-]{10,}$/)) {return true;}
	else {return false;}
}

//Basic email address validation
//Requires any number greater than zero of mixed-case letters, numbers and periods, hyphens, underscores, and single quotes in mailbox name
//Requires any number greater than zero of mixed-case letters, numbers and periods, hyphens, and underscores in domain.
//Requires ampersand (@) between mailbox and domain.
function validateEmail(email) {
	if(email.match(/^([\x27]*[\w\.\-]*)+\@[\w\.\-]+$/)) {return true;}
	else {return false;}
}

function setValueByName(toSet, setTo){
	
	testing = true; // can leave 'on'. Only opens alert when error.
	
	if (testing == true ) {
		var msg = "Error in function SetValueById \n";
		var showMsg = false;
		
		if (!document.form1.elements[toSet]) {
			alert ( "toSet ("  + toSet +" ) has no properties (field may not exist)" );
		}
		if (!document.form1.elements[setTo]) {
			alert ( "setTo ("  + setTo +" ) has no properties (field may not exist)" );
		}
	}	
	document.form1.elements[toSet].value = ""; // initialize
	document.form1.elements[toSet].value = document.form1.elements[setTo].value;					
}

function errorAlert (error) {
	alert (error);
}
