window.onload = resizeCols;
window.onresize = resizeCols;


/***********************************************************************
 * function resizeCols()                                               *
 * adjusts the heights of 'left_col', 'main_col', and 'right_col' divs * 
 * to equal to the max height of the three                             *
 ***********************************************************************/
function resizeCols() {
	/* make sure that the right column exists */
	if (!document.getElementById('right_content')) {
		/* make sure the left column exists */
		if(!document.getElementById('left_col')) {
			return;
		}
		document.getElementById('left_col').style.height = document.getElementById('main_col').offsetHeight;
		return;
	}
	
	//alert('resizeCols()!');
	
	var left_col_height = document.getElementById('left_content').offsetHeight;
	var main_col_height = document.getElementById('main_content').offsetHeight;
	var right_col_height = document.getElementById('right_content').offsetHeight;
	var minHeight = Math.round(screen.height * .55); // min column height
	var max_height = Math.max(left_col_height, main_col_height, right_col_height, minHeight) + 5;

	/* adjust column heights */
	document.getElementById('left_col').style.height = max_height + 'px';
	document.getElementById('main_col').style.height = max_height + 'px';
	document.getElementById('right_col').style.height = max_height + 'px';
	 
	/* needed for IE7 */
	document.getElementById('left_col').style.height = (max_height - (document.getElementById('left_col').offsetHeight - max_height)) + 'px';
	document.getElementById('main_col').style.height = (max_height - (document.getElementById('main_col').offsetHeight - max_height)) + 'px';
	document.getElementById('right_col').style.height = (max_height - (document.getElementById('right_col').offsetHeight - max_height)) + 'px';

	/* maintain minimum width for the middle column */
	/*
	var min_width = document.getElementById('left_col').offsetWidth * 4;	
	if (document.getElementById('mid_section').offsetWidth < min_width) {
		document.getElementById('mid_section').style.width = min_width + 'px';
	}
	else {
		document.getElementById('mid_section').style.width = (document.getElementById('header_r3').offsetWidth - 1) + 'px'
		document.getElementById('header').style.width = (document.getElementById('header_r3').offsetWidth - 1) + 'px'
		document.getElementById('footer').style.width = (document.getElementById('header_r3').offsetWidth - 1) + 'px';	
	}
	*/
}

/* TextResizeDetector code has been borrowed from http://www.alistapart.com/articles/fontresizing */
/* id of element to check for and insert test SPAN into for font size detection */
TextResizeDetector.TARGET_ELEMENT_ID = 'header';
/* function to call once TextResizeDetector was initialized */
TextResizeDetector.USER_INIT_FUNC = initTRD;
/* resizeCols will now be called every time the user changes the font */
function initTRD() {
	TextResizeDetector.addEventListener(resizeCols, null);
}

/******************************************************************************************************* 
 * function applySFHover()                                                                             *
 * IE 6 and earlier only supports the :hover pseudo class for the anchor element. Hence we need to add *
 * the 'sfhover' class to LI elements within the top_menu element when they are 'moused over' and      *
 * remove it, using a regular expression, when they are 'moused out'. IE 7 allows the :hover pseudo    *
 * class to be applied to any element.                                                                 *
 *******************************************************************************************************/  
function applySFHover() {
	var sfEls = document.getElementById("top_menu").getElementsByTagName("LI");
	for (var i = 0; i < sfEls.length; i++) {
		if (sfEls[i].className) {
			sfEls[i].onmouseover = function() {
				this.className += " sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
/* run the applySFHover function when the page loads */
if (window.attachEvent) window.attachEvent("onload", applySFHover);

/******************************************************************************************************* 
 * function selectTab()                                                                                *
 * Takes an integer, intTab, as am argument and sets the class of the 'tab_wrapper' element to         *
 * 'select_tab_' + intTab. This results in the appropriate tab being selected and the corresponding    *
 *  tabbed content displayed. This is accomplished through the use of CSS class inheritance.           *
 *******************************************************************************************************/ 
function selectTab(intTabNo) {
	 document.getElementById('tab_wrapper').className = 'select_tab_' + intTabNo;
	 document.getElementById('tab_wrapper').blur(); // remove focus from the link for Firefox
	 //Added by DM to resize the columns
	 resizeCols();
}

/******************************************************************************************************* 
 * function MM_jumpMenuGo()                                                                            *
 * Loads a new webpage into the browser                                                                *
 *******************************************************************************************************/ 
function MM_jumpMenuGo(objId,targ,restore){ //v9.0
	  var selObj = null;  with (document) { 
	  if (getElementById) selObj = getElementById(objId);
	  if (selObj) eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	  if (restore) selObj.selectedIndex=0; }
}

/*** Form Related Functions ***/

/******************************************************************************************
 * function getURLParam()                                                                 *
 * Takes a parameter name as an argument and retrieves its value from the URL.            *
 * Adapted from: http://www.netlobo.com/url_query_string_javascript.html                  *
 ******************************************************************************************/
function getURLParam(sParam) {  
	sParam = sParam.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var sRegEx = "[\\?&]" + sParam + "=([^&#]*)";  
	var oRegEx = new RegExp(sRegEx);  
	var arrResults = oRegEx.exec(window.location.search);  
	if (arrResults == null )    
		return "";  
	else    
		return decodeURI(arrResults[1]);
}

/******************************************************************************************
 * function displayMessage()                                                              *
 * Takes no arguments.                                                                    *
 * Checks the URL string and displays either a form processing error message or a         *
 * confirmation message.                                                                  *
 ******************************************************************************************/
function displayMessage() {
	var oDivMessageArea = document.getElementById("message_area");
	if (getURLParam('status') == 'fail') {
		var incorrectFields = getURLParam('incorrect_fields');
		var arrFields = incorrectFields.split(",");
		for (var i = 0; i < arrFields.length; i++) {
			oField = document.getElementById(arrFields[i] + '_li');
			if (oField) {
				oField.className = "incorrect";
				arrFields[i] = toUpperFirst(arrFields[i].replace(/_/g, " "));
			}
		}
		if (arrFields.length != 0) {
			oDivMessageArea.innerHTML = 
			"<span class=\"strong\">ERROR PROCESSING FORM:</span><br /><br />" +
			"The following field(s) haven't been filled out correctly:&nbsp;&nbsp;" + 
			"<span class=\"strong\">" + arrFields.join(", ") + "</span>";	
			oDivMessageArea.style.display = "block";
			window.document.location.href += "#start_of_form";
			// set form fields to their previous values
			setForm(document.getElementById("ois_form"));
		}
	} else if (getURLParam('status') == 'success') {
		oDivMessageArea.innerHTML = 
		"<span class=\"strong\">THE FORM HAS BEEN SUBMITTED SUCCESSFULLY.</span><br />" +
		"Thank you, " + getURLParam("contact_name") + ", for your request.<br />" +
		"An OIS staff member will be back in touch with you shortly. "; 
		oDivMessageArea.style.display = "block"
		window.document.location.href += "#start_of_form";	
	}
}

/******************************************************************************************
 * function setForm()                                                                     *
 * Restores previous form values by iterating over all form fields with IDs ending        *
 * in "_fld", retrieves their values from the URL and sets those fields to the            *
 * appropriate values.                                                                    *
 ******************************************************************************************/
function setForm(oForm) {
	for (var i = 0; i < oForm.elements.length; i++) {
		var sID = oForm.elements[i].id;
		if (sID.substr(sID.length - 4, 4) == "_fld") {
			oForm.elements[i].value = getURLParam(oForm.elements[i].name);
		}
	}
}

/*** General Functions ***/

/******************************************************************************************
 * function toUpperFirst()                                                                *
 * Takes a string as an argument and returns the same string after uppercasing the first  *
 * letter of every word in the string.                                                    *
 ******************************************************************************************/
function toUpperFirst(sText) {
	var arrWords = sText.split(" ");
	for (var i = 0; i < arrWords.length; i++) {
		arrWords[i] = arrWords[i].substring(0,1).toUpperCase() + arrWords[i].substring(1);
	}
	return arrWords.join(" ");
}

/******************************************************************************************
 * function encode()                                                                      *
 * Takes a string as argument and returns a comma delimited list of ASCII codes           *
 * corresponding to each character in the string.                                         *
 ******************************************************************************************/
function encode(sStr) {
	var sCodeStr = "";
	for (var i = 0; i < sStr.length; i++) {
		sCodeStr += sStr.charCodeAt(i) + ",";
	}
	return sCodeStr;
}

/******************************************************************************************
 * function decode()                                                                      *
 * Takes am array of ASCII codes as argument and returns a string constructed from those  *
 * charachters.                                                                           *
 ******************************************************************************************/
function decode(aArg) {
	var sStr = "";
	for (var i = 0; i < aArg.length; i++) {
		sStr += String.fromCharCode(aArg[i]);
	}
	return sStr;
}