// 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 () {
	// Code to run on page load
	//Hover.init(Hover.collections);
	Callouts.fix();
	DocLinks.init();
});


/*----------------------------------------------+
 | DocLinks - Add icon after links to documents |
 +----------------------------------------------*/
var DocLinks = {
	init : function() {
		// Find all links
		var links = document.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++) {
			var theLink = links[i];
			var address = theLink.href.toLowerCase();
			
			// Check if link points to files with common extensions
			var matches = address.match(/\.(doc|pdf|xls|ppt)/);
			
			if (matches) {
				// Using "match" always returns two results (not sure why)
				var ext = matches[0].substr(1, 3);
				
				// Create new image and insert it
				var newImg = document.createElement("img");
				newImg.alt = "(" + ext.toUpperCase() + ")";
				newImg.className = "icon";
				newImg.src = "/images/shared/icon-" + ext + ".gif";
				newImg.title = newImg.alt;
				
				if (theLink.getElementsByTagName("img").length <= 0)
					theLink.parentNode.insertBefore(newImg, theLink);
				
				// Make link open in new window/tab
				theLink.onclick = function () {
					window.open(this.href);
					return false;
				}
			}
		}
	}
};


/*------------------------------------------------------------------------+
 | Callouts - Adjust widths of callouts depending on size of image within |
 +------------------------------------------------------------------------*/
var Callouts = {
	fix : function() {
		// Check for functionality
		if (!document.getElementById || !document.getElementsByTagName) return false;
		
		var bin = document.getElementById("content");
		var arrBins = bin.getElementsByTagName("*");
		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");
				if (images.length >= 1) {
					arrBins[i].style.width = images[0].offsetWidth + "px";
				}
			}
		}
		
		return false;
	}
};


// Finlizes error message
function finalizeError(errMessage) {
	if (errMessage != "") {
		newErrMessage  = "_____________________________________________________________\n\n";
		newErrMessage += "The form was not submitted because of the following error(s).\n";
		newErrMessage += "Please correct resubmit.\n";
		newErrMessage += "_____________________________________________________________\n\n";
		newErrMessage += errMessage;
		alert(newErrMessage);
		return false;
	}
	
	return true;
}


// 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 numeric value
function isNumber(strValue) {
	return (!isNaN(strValue) && strValue != "");
}

// Validate url
function isURL(strValue) {
	return (strValue.indexOf("http://") == 0);
}


// Validate select box selection
function isSelected(objField) {
	if ((objField.multiple && objField.selectedIndex == -1) || (!objField.multiple && objField.selectedIndex == 0)) {
		return false;
	} else {
		return true;
	}
}


// Validate string value
function isString(strValue) {
	return (typeof strValue == "string" && strValue != "" && isNaN(strValue));
}


// Validate various types
function validate(arrFields) {
	var errMessage = "";
	
	for (var i = 0; i < arrFields.length; i++) {
		var isValid = true;
		// 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 "url":
					isValid = isURL(objField.value);
					break;
				case "any":
					isValid = (objField.value == "" ? false : true);
					break;
				default:
					isValid = true;
			}
		}
		
		// If field is invalid, add to error message
		if (!isValid) { errMessage += "- " + strLabel + " is missing or invalid.\n"; }
	}
	
	return errMessage;
}