// Add event handler to body when window loads
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

addLoadEvent(function () {
	// More code to run on page load
	fixCallOuts();
	attachWindowOpeners();
});

// Validate string value
function isString(strValue) {
	return (typeof strValue == "string" && strValue != "" && isNaN(strValue));
}

// Validate numeric value
function isNumber(strValue) {
	return (!isNaN(strValue) && strValue != "");
}

// Validate email address
function isEmail(strValue) {
	var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
	return (strValue != "" && objRE.test(strValue));
}

// Validate select box selection
function isSelected(objField) {
	if ((objField.multiple && objField.selectedIndex == -1) || (!objField.multiple && objField.selectedIndex == 0)) {
		return false;
	} else {
		return true;
	}
}

// Validate various types
function validate(arrFields) {
	var isValid = true;
	
	for (var i = 0; i < arrFields.length; i++) {
		// arrTheField: [label, id, type, required]
		var arrTheField = arrFields[i].split("|");
		var strLabel = arrTheField[0];
		var objField = document.getElementById(arrTheField[1]);
		var strType = arrTheField[2];
		var req = arrTheField[3];
		
		// Check if field is required or not required and not empty
		if (req == "true" || (req == "false" && objField.value != "")) {
			switch (strType) {
				case "string":
					isValid = isString(objField.value.replace(/^\s*|\s*$/g, ''));
					break;
				case "number":
					isValid = isNumber(objField.value);
					break;
				case "email":
					isValid = isEmail(objField.value);
					break;
				case "select":
					isValid = isSelected(objField);
					break;
				case "any":
					isValid = (objField.value == "" ? false : true);
					break;
				default:
					isValid = true;
			}
		}
		
		if (!isValid) {
			// If field is invalid, alert user, stop validating
			var errMessage = "";
			errMessage = "The information you provided for \"" + strLabel + "\" is invalid.\n";
			errMessage += "Please correct this and submit again.";
			alert(errMessage);
			
			if (objField.nodeName.toLowerCase() != "select") {
				objField.select();
			}
			
			objField.focus();
			return false;
		}
	}
	
	return true;
}

// Adjust widths of callouts depending on size of image within
function fixCallOuts() {
	if (document.getElementById && document.getElementsByTagName) {
		var bin = document.getElementById("main");
		var arrBins = bin.getElementsByTagName("div");
		var classRE = /call[lr]/gi;
		
		// Set div width = image width
		for (var i = 0; i < arrBins.length; i++) {
			if (classRE.test(arrBins[i].className)) {
				var images = arrBins[i].getElementsByTagName("img");
				//alert(images.length);
				if (images.length >= 1) {
					arrBins[i].style.width = images[0].offsetWidth + "px";
				}
			}
		}
	}
}

// Make all extrnal links open in new windows
/*function attachWindowOpeners() {
	var arrLinks = document.getElementsByTagName("a");
	for (var i = 0; i < arrLinks.length; i++) {
		theHref = arrLinks[i].href;
		if (theHref.indexOf("ceonly") <= -1 && theHref.indexOf("javascript") <= -1) {
			var url = arrLinks[i].href;
			arrLinks[i].target = "_blank";
		}
	}
}*/