// main init function
function initNav( parr_NavTypes )
{
	//loop through nav type array, initializing the cssjsmenu for each type
	for (var i = 0; i < parr_NavTypes.length; i++) {
		cssjsmenu( parr_NavTypes[i] );
	}
	
	// disable hoverjs if needed
	if (document.getElementById) {
		var kill = document.getElementById('hoverJS'); 
		kill.disabled = true;
	}
	
}

// common function for opening a new window.	
function fnc_OpenEditWindow( pvar_URL ) { 
	newWindow = window.open( pvar_URL ,'editWindow','width=900,height=600,scrollbars=yes,dependent=yes,resizable=yes');
	newWindow.focus();
}

// common function for opening a new window.     
function fnc_OpenWindow( pvar_URL, pvar_windowName, pvar_width, pvar_height ) {
 	newWindow = window.open( pvar_URL , pvar_windowName, 'width=' + pvar_width + ',height=' + pvar_height + ',scrollbars=yes,dependent=yes,resizable=yes');
	newWindow.focus();
}

// this function opens in new window, appending URL variables passed in via an array
function openWindowSendParam ( url, urlParamArray, width, height ) {

	//initialize URL parameters
	var urlParams 		= "?";
	var urlValueArray	= ""
	
	//Loop through form field array creating URL parameters
	for(i = 0; i < urlParamArray.length; i++) {
		
		// if this is not the first time through the loop, at an ampersand between the URL parameters
		if (i > 0)
			urlParams = urlParams + "&";
		
		// add parameter to URL params string
		urlParams = urlParams + urlParamArray[i][0] + '=';
		
		// if this parameter value contains a '|', then we need to get the value by element ID, else take the value from the array
		if ( urlParamArray[i][1].indexOf("|") > -1 ) {
		
			//split the parameter array value on the |
			urlValueArray = urlParamArray[i][1].split("|");
			
			// set the value to the second item in the array
			urlParams = urlParams +document.getElementById(urlValueArray[1]).value;
		}
		// else use the value from the array
		else {
			urlParams = urlParams + urlParamArray[i][1];
		}
	}	

	// call the open window function
	fnc_OpenWindow( url + urlParams, 'FEINWindow', width, height );
}

// disable form submit button only function (for forms without the cancel button)
function disableFormSubmitOnly(btnValue, form) {	
	tobj_FormDisplay = form;
	tobj_FormDisplay.btn_ClickedBtn.value = btnValue;
	tobj_FormDisplay.submit();
	tobj_FormDisplay.btn_Submit.disabled = true;
}

// disable form function
function disableForm(btnValue, form) {	
	tobj_FormDisplay = form;
	tobj_FormDisplay.btn_ClickedBtn.value = btnValue;
	tobj_FormDisplay.submit();
	tobj_FormDisplay.btn_Submit.disabled = true;
	tobj_FormDisplay.btn_Cancel.disabled = true;
}

// disable form submit button only function (for forms without the cancel button)
function disableFormPublishButtons(btnValue, form) {	
	tobj_FormDisplay = form;
	tobj_FormDisplay.btn_ClickedBtn.value = btnValue;
	tobj_FormDisplay.submit();
	tobj_FormDisplay.btn_Submit.disabled = true;
	tobj_FormDisplay.btn_SubmitForApproval.disabled = true;
	tobj_FormDisplay.btn_Approve.disabled = true;
	tobj_FormDisplay.btn_Cancel.disabled = true;	
}

// form button hover function
function buttonHov(loc,cls){
	if(loc.className)
		loc.className=cls;
}

// this functions shows and hides an element
function showHideSearch () {

	if ( document.getElementById('searchBox').style.display == '' ||  document.getElementById('searchBox').style.display == 'block') {
		Effect.BlindUp('searchBox');
		document.getElementById('searchState').value = 'closed';
		document.getElementById('showHideSearchLink').innerHTML = 'Search';
	}
	else {
		Effect.BlindDown('searchBox');
		document.getElementById('searchState').value = 'open';
		document.getElementById('showHideSearchLink').innerHTML = 'Hide Search';
	}
	
}

// this functions shows and hides an element
function showHideList (elemID) {
	
	if ( document.getElementById(elemID+"List").style.display == '' ||  document.getElementById(elemID+"List").style.display == 'inline') {
		document.getElementById(elemID+"List").style.display="none";
		document.getElementById(elemID+"Show").style.display="inline";
		document.getElementById(elemID+"Hide").style.display="none";
	}	
	else {
		document.getElementById(elemID+"List").style.display="inline";
		document.getElementById(elemID+"Show").style.display="none";
		document.getElementById(elemID+"Hide").style.display="inline";
	}

}

// this function hides tree element
function treeHide(elemID) {
	if (document.getElementById("ul"+elemID)) { 
		document.getElementById("ul"+elemID).style.display="none";
	}
		
	if (document.getElementById("hide"+elemID)) {	
		document.getElementById("hide"+elemID).style.display="none";
	}
		
	if (document.getElementById("show"+elemID)) {
		document.getElementById("show"+elemID).style.display="inline";
	}
}

// this function shows a tree element
function treeShow(elemID) {
	if (document.getElementById("ul"+elemID)) {
		document.getElementById("ul"+elemID).style.display="block";
	}
	
	if (document.getElementById("hide"+elemID)) {
		document.getElementById("hide"+elemID).style.display="inline";
	}
			
	if (document.getElementById("show"+elemID)) {
		document.getElementById("show"+elemID).style.display="none";
	}
}

// this function will expand or collapse all items based on the counter and a hide show very
function treeExpandCollapseAll() {
	
	// Loop through the number of counter elements to expand or collapse
	for ( var i = 1; i < treeElemArray.length; i++ ) {
	
		if ( expandCollapseAll == 'Expand' ) {
			treeShow(treeElemArray[i]);
			document.getElementById(treeExpandCollapseElemID).innerHTML = '<img src="' + imagePath + '/btn_CloseAll.gif" alt="close all" title="close all" border="0"//>';
			expandCollapseAll = 'Collapse';
		}
		else {
			treeHide(treeElemArray[i]);
			document.getElementById(treeExpandCollapseElemID).innerHTML = '<img src="' + imagePath + '/btn_ExpandAll.gif" alt="expand all" title="expand all" border="0"//>';
			expandCollapseAll = 'Expand';
		}
	}
}

// this function checks all checkboxes
function checkAllCheckboxes( field, elemID ) {
	
	//if this is a check all, then set all checkbox fields to true, the toggle the check flag, and replace the HTML
	if (checkflag == "false") {
		
		for (i = 0; i < field.length; i++) { field[i].checked = true; }		
		checkflag = "true";
		//document.getElementById(elemID).innerHTML = 'uncheck all';
		document.getElementById(elemID).innerHTML = '<img src="' + imagePath + '/btn_UncheckAll.gif" alt="uncheck all" title="uncheck all" border="0"//>';
		
	}
	//if this is a uncheck all, then set all checkbox fields to false, the toggle the check flag, and replace the HTML
	else {
		
		for (i = 0; i < field.length; i++) { field[i].checked = false; }
		checkflag = "false";
		//document.getElementById(elemID).innerHTML = 'check all';
		document.getElementById(elemID).innerHTML = '<img src="' + imagePath + '/btn_CheckAll.gif" alt="check all" title="check all" border="0"//>';
		
	 }
}

// this function loops through all check boxes in a form regardless of field name
function checkAllCheckboxesForm(form, elemID, imagePath) {

	// NOTE: the checkflag variable must be created on the calling page!!

	//if this is a uncheck all (i.e. the flag was previous checked)...
	if (checkflag == "false") {
		
		// the toggle the check flag
		checkflag = "true";

		// loop through the form
		for (var i = 0; i < form.elements.length; i++ ) {
        
			// if this is a checkbox, then check it
			if (form.elements[i].type == 'checkbox') {
	            form.elements[i].checked = true;
	        }
			
	    }// for (var i = 0; i < form.elements.length; i++ 
		
		// replace the HTML
		document.getElementById(elemID).innerHTML = '<img src="' + imagePath + '/btn_UncheckAll.gif" alt="check all" title="check all" border="0"//>';
		
	}
	//if this is a uncheck all (i.e. the flag was previously unchecked)...
	else {
		
		// the toggle the check flag
		checkflag = "false";
		
		// loop through the form
		for (var i = 0; i < form.elements.length; i++ ) {
        
			// if this is a uncheckbox, then check it
			if (form.elements[i].type == 'checkbox') {
	            form.elements[i].checked = false; 
	        }
			
	    }// for (var i = 0; i < form.elements.length; i++ 
		
		// replace the HTML
		document.getElementById(elemID).innerHTML = '<img src="' + imagePath + '/btn_CheckAll.gif" alt="uncheck all" title="uncheck all" border="0"//>';
		
	 }  //  (checkflag == "false")
}

// format currency
function formatCurrency(num) {

	// set the number to a string replacing anything that needs replacing
	num = num.toString().replace(/\$|\,/g,'');

	// if this is not a number, then set to 0
	if(isNaN(num))
		num = "0";

	// calcs
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();

	// if this is less than 10 cents, then...
	if(cents<10)
		cents = "0" + cents;

	// add commas
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));

	// return number formated
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

// function for getting a URL parameter
function getURLParameter( name ) {  
	
	// make sure this is a valid name
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	
	// use this regular expression to retrieve the value with a name past 10
	var regexS 	= "[\\?&]"+name+"=([^&#]*)";  
	var regex 	= new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	
	// if the result is null, then returned blank, else return the results
	if( results == null )    
		return "";  
	else    
		return results[1];
}

// this function gets a glossary term via an Ajax call
function ajaxRequestCall( url, pars, responseVar ) {
	
	$(responseVar).innerHTML = '<img src="/img/icon_loading.gif" title="loading data" alt="loading data" border="0" />&nbsp;&nbsp;Loading...';
	
	var myAjax = new Ajax.Updater(
								{success: responseVar},
								url,
								{	
									method: 	'get', 
									parameters: pars, 
									onFailure: 	reportError
								});				

}	

// this function gets a glossary term via an Ajax call
function ajaxRequestCallNoUpdate( url, pars, responseVar ) {
	
	//responseVar is ignored	
	var myAjax = new Ajax.Updater(
								{success: responseVar},
								url,
								{	
									method: 	'get', 
									parameters: pars, 
									onFailure: 	reportError
								});				

}

//function for submitting ajax form
function ajaxSubmitForm( btnValue, url, responseVar, thisForm ) {
	
	//$(responseVar).innerHTML = '<img src="/img/icon_loading.gif" title="loading data" alt="loading data" border="0" />&nbsp;&nbsp;Loading...';
//alert(thisForm.btn_Submit.value);
	// pass the button value and disable the form buttons
	thisForm.btn_ClickedBtn.value 	= btnValue;
	thisForm.btn_Submit.disabled 	= true;
	thisForm.btn_Cancel.disabled 	= true;	

	// init vars used for creating param list
	var theForm 	= thisForm;
	var lst_Params 	= "";	
	
	// loop through the form elements, creating strings for them
	for(i=0; i<theForm.elements.length; i++){
	
		// if the params list is not blank, then add an Ampersand
		if (lst_Params != "" )
			lst_Params = lst_Params + "&"
			
		// if this is a check box, make sure that you check to see if it's checked before sending a value
		if(theForm.elements[i].type == "checkbox"){
		
			// if it's checked, add it to the list, else leave it out (like normal checkbox behavior)
			if ( theForm.elements[i].checked ) {						
				lst_Params = lst_Params + theForm.elements[i].name + "=";
				lst_Params = lst_Params + theForm.elements[i].value;
			}
		
		}
		// else, add the current parameter to ths list
		else {
		lst_Params = lst_Params + theForm.elements[i].name + "=";
		lst_Params = lst_Params + theForm.elements[i].value;
		}
		
		/*
		var alertText = ""
		alertText += "Element Type: " + theForm.elements[i].type + "\n"
		
		
		if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button" || theForm.elements[i].type == "hidden" || theForm.elements[i].type == "submit"){
			alertText += "Element Name: " + theForm.elements[i].name + "\n"
			alertText += "Element Value: " + theForm.elements[i].value + "\n"
		}
		else if(theForm.elements[i].type == "checkbox"){
			alertText += "Element Name: " + theForm.elements[i].name + "\n"
			alertText += "Element Checked? " + theForm.elements[i].checked + "\n"
		}
		else if(theForm.elements[i].type == "select-one"){
			alertText += "Element Name: " + theForm.elements[i].name + "\n"
			alertText += "Selected Option's Text: " + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "\n"
			alertText += "Selected value: " + theForm.elements[i].value + "\n"
		} 
		
		alert(alertText);
		*/
	}
	
	//alert(lst_Params);
	new Ajax.Updater(
				 	{success: responseVar}, 
					url, 
				 	{
						asynchronous:	true, 
						method:			'post',
						parameters:		lst_Params, 
						onFailure: 		reportError
					});
	


	return false;
}

// this function returns the value of a form field from the window that opens this window
function getFormFieldValueFromOpener( formFieldID ) {
	return opener.document.getElementById(formFieldID).value;
}


// this function clears search fields.
function clearSearch( fieldList ) {
	for(i = 0; i < fieldList.length; i++) {
		document.getElementById(fieldList[i][0]).value = fieldList[i][1];
	}
}

// this function swaps images on a mouse out
function imgOut(imgName) {		
	if (document.images) {
		document.getElementById(imgName).src = eval(imgName + "_out.src");
	}
}

// this function swaps images on a mouse over
function imgOver(imgName) {
	if (document.images) {
		document.getElementById(imgName).src = eval(imgName + "_over.src");
	}
}

// this function swaps images on a mouse out
function imgSwap(imgElem, imgSwapName) {		
	if (document.images) {
		document.getElementById(imgElem).src = imgSwapName;
	}
}

// this functions shows and hides an element
function showHideID ( elemID ) {

	if ( document.getElementById(elemID).style.display == '' ||  document.getElementById(elemID).style.display == 'block') {
		document.getElementById(elemID).style.display="none";
	}
	else {
		document.getElementById(elemID).style.display="block";
	}	
	
}

// this function is a generic show hide check that compares two values to decide whether or not to show an element
function showHideCheck ( value, valueToCheckAgainst, element ) {
	if ( value == valueToCheckAgainst ) {
		document.getElementById(element).style.display = 'block';
	}
	else {
		document.getElementById(element).style.display = 'none';
	}
}

// this function is a generic item setting function.
//NOTE: The change to Element should be a hidden span with an ID that maps to the data inside the span
function onChangeElemValueUpdate ( element, changeToElement ) {
	document.getElementById(element).value = document.getElementById(changeToElement).innerHTML;
}

// this function closes the site message, and sends an ajax call to update a client variable.
function closeSiteWarningMessage ( ajaxURL, warningDiv ) {
	
	//if there was not a warning div passed in, then set it to a default
	if(!warningDiv) { 
		warningDiv = 'siteWarning';
	} 

	// hide the site warning
	document.getElementById(warningDiv).style.display = 'none';
	
	// set variables
	var pars = "1=1";
	
	// make the ajax call to set the client variable
	ajaxRequestCall( ajaxURL, pars, warningDiv );
	 
}

// this function counts the number of characters in a textarea and updates a status field
function textAreaCounter (textAreaElem, counterElem, maxLimit) {
	
	// if too long...trim it!
	if (document.getElementById(textAreaElem).value.length > maxLimit) {
		document.getElementById(textAreaElem).value = document.getElementById(textAreaElem).value.substring(0, maxLimit);
	}
	// otherwise, update 'characters left' counter
	else {
		document.getElementById(counterElem).innerHTML = maxLimit - document.getElementById(textAreaElem).value.length;
	}
	
}
// this function swaps out an input image based on the value
function swapInputBGImageFocus (elemID, valueCompare, color, url) {
	
	if (document.getElementById(elemID).value == valueCompare ) {
			document.getElementById(elemID).style.background = color; 
	}	
	else {
		document.getElementById(elemID).style.background = "url(" + url + ") " + color + " bottom right no-repeat;";
	}
}
// this function swaps out an input image based on the value
function swapInputBGImageBlur (elemID, valueCompare, color, url) {
	
	if (document.getElementById(elemID).value != valueCompare ) {
			document.getElementById(elemID).style.background = color; 
	}	
	else {
		document.getElementById(elemID).style.background = "url(" + url + ") " + color + " bottom right no-repeat;";
	}
}

// this functions shows and hides an element
function removeID ( elemID ) {

	// set the element to a local var
	var elem = document.getElementById(elemID)
	
	// remove the element from it's parent
	elem.parentNode.removeChild(elem);
	
}

//this function removes an array item from a list
function removeArrayItem (itemID, itemValue) {
		
	//set the local variables for items needed in logic
	var currArrAsString = document.getElementById(itemID).value;
	var currArr 		= currArrAsString.split(',');
	var curItem			= document.getElementById(itemValue).firstChild.innerHTML
		
	//find the position and set it
	var currItemIndex = findArrayIndex(currArr, curItem);
	
	//remove the item from the array and reset the value
	if (currItemIndex != -1) {
		currArr.splice(currItemIndex, 1);
		document.getElementById(itemID).value = currArr 
	}	
}

// Find the index of a value in an array (this is to support older browser that don't support arr.indexOf)
function findArrayIndex (currArr, value){
	
	//loop through array, looking for value
	for (var i = 0; i < currArr.length; i++) {
		if (currArr[i] == value) {
			return i;
		}
	}
	
	//if not found, then return -1 as a sign we didn't find it
	return -1;
}

// this function sends them along to the quote forms
function selectRedirect ( url ) {

	// if they've selected an action, then send them
	if ( url != '' ) {	
		window.location=url;
	}
}

//this function does nothing. it is used when certain click actions need a function to call
function nothing () {
}



