var gynIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;    // true if we're on ie

var garMonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var garMonthsLong = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

	function gfnCheckValueIsSet(ptxValue) {
		return (ptxValue.replace(/ /gi,"") != "");
	}

	function gfnCheckValueIsSetSelect(pobControl) {
		pvrValue = pobControl.options[pobControl.selectedIndex].value;
		return ((pvrValue != "") && (pvrValue != "0"));
	}

	function gfnCheckValueIsSetSelectMulti(pobControl) {
		vobOptions = pobControl.options;
		
		for (vinIndex = 0; vinIndex < vobOptions.length; vinIndex++) {
			if (vobOptions[vinIndex].selected) {
				return true;
			}
		}
		return false;
	}

	function gfnResetSelectMulti(pobControl) {
		vobOptions = pobControl.options;
		
		for (vinIndex = 0; vinIndex < vobOptions.length; vinIndex++) {
			vobOptions[vinIndex].selected = false;
		}
	}

	function gfnSetAllSelectMulti(pobControl) {
		vobOptions = pobControl.options;
		
		for (vinIndex = 0; vinIndex < vobOptions.length; vinIndex++) {
			vobOptions[vinIndex].selected = true;
		}
	}

	function gfnGetValuesSelectMulti(pobControl) {
		vobOptions = pobControl.options;
		vtxSelected = '';		
		for (vinIndex = 0; vinIndex < vobOptions.length; vinIndex++) {
			if (vobOptions[vinIndex].selected) {
				vtxSelected = vtxSelected + ',' + vobOptions[vinIndex].value;
			}
		}
		vtxSelected = vtxSelected.substr(1);
		return vtxSelected;
	}

	function gfnCheckValueSelectMulti(pobControl,vvrValue) {
		//returns true if the option with value vvrValue (not text, but actual value of this option!) is set, false otherwise
		vobOptions = pobControl.options;
		for (vinIndex = 0; vinIndex < vobOptions.length; vinIndex++) {
			if (vobOptions[vinIndex].selected && (vobOptions[vinIndex].value == vvrValue)) {
				return true;
			}
		}
		return false;
	}

	function gfnSetSelect(pobControl,ptxNewValue) {
		varOptions = pobControl.options;
		for (vinIndex = 0; vinIndex < varOptions.length; vinIndex++) {
			vtxThisValue = varOptions[vinIndex].value;
			if (vtxThisValue == ptxNewValue) {
				pobControl.selectedIndex = vinIndex;
				return
			}
		}			
	}

	function gfnFormatDate(pobDate) {
		//formats a date in a hard-coded format of yyyy mmm d  hh:mm:ss
		vinYear = pobDate.getFullYear();
		vinMonth = pobDate.getMonth();
		vinDay = pobDate.getDate();
		
		vinHour = pobDate.getHours();
		vtxHour = vinHour.toString();
		if (vtxHour.length == 1) {
			vtxHour = "0" + vtxHour;
		}
		
		vinMinute = pobDate.getMinutes();
		vtxMinute = vinMinute.toString();
		if (vtxMinute.length == 1) {
			vtxMinute = "0" + vtxMinute;
		}
		
		vinSecond = pobDate.getSeconds();
		vtxSecond = vinSecond.toString();
		if (vtxSecond.length == 1) {
			vtxSecond = "0" + vtxSecond;
		}
		
		vtxMonth = garMonths[vinMonth];
		vtxString = vinYear + " " + vtxMonth + " " + vinDay + "  " + vtxHour + ":" + vtxMinute + ":" + vtxSecond;
		return vtxString;
	}
	
	function gfnCheckFileExt(ptxString,ptxAllowedExt) {
		if (ptxAllowedExt == "") {
			//no restrictions
			return true;
		} else {
			vtxExt = ptxString.substr(ptxString.lastIndexOf(".") + 1);
			varAllowedExt = ptxAllowedExt.split(",");
			for (vinIndex = 0; vinIndex < varAllowedExt.length; vinIndex++) {
				vtxThisExt = varAllowedExt[vinIndex];
				if (vtxThisExt == vtxExt) {
					return true;
				}
			}
			return false;
		}
	}
	
	function gfnSetDirty(pobControl,ptxDirtyFlag) {
		pobForm = pobControl.form;
		vtxDirtyFlagControl = "fob" + ptxDirtyFlag;
		pobForm[vtxDirtyFlagControl].value = 1;
		return true;
	}
	
	function gfnTrimAllSpaces(ptxValue) {
		return ptxValue.replace(/ /gi,"");
	}

	function gfnTrimLeadingTrailingSpaces(ptxValue) {
		ptxValue = ptxValue.replace(/^[ ]+/i,"");
		return ptxValue.replace(/[ ]+$/i,"");
	}

	function gfnTrimLeadingTrailingSpacesAndCR(ptxValue) {
		ptxValue = ptxValue.replace(/^[ \n\r]+/i,"");
		return ptxValue.replace(/[ \n\r]+$/i,"");
	}

	function gfnAddYearToDate(ptxValue) {
		if (ptxValue.match(/^(\d{1,2}|[A-Z]{3})( |\/)\d{1,2}/i)) {
			//insert year first
			vobToday = new Date();
			vinYear = vobToday.getFullYear();
			return vinYear + " " + ptxValue;
		} else {
			return ptxValue;
		}
	}

	function gfnCheckDateTimeFormat(ptxValue,pynTimeAllowed,pynTimeRequired) {
		//valid date formats are yyyy mm dd, yyyy/mm/dd, yyyy mmm dd
		//time can either be omitted entirely, expressed as hh:mm, or hh:mm:ss
		if (pynTimeAllowed) {
			if (ptxValue.match(/^\d{4}( |\/)(\d{1,2}|[A-Z]{3})( |\/)\d{1,2} {1,2}\d{1,2}:\d{2}(|:\d{2})/i)) {
				return true;
			}
			if (pynTimeRequired) {
				return false;
			}
		}
		if (ptxValue.match(/^\d{4}( |\/)(\d{1,2}|[A-Z]{3})( |\/)\d{1,2}/i)) {
			return true;
		}
		return false;
	}

	function gfnCheckDateTimeValidity(ptxValue) {
		// makes sure date is actually valid
		if (varMatches = ptxValue.match(/\d{4} \d{1,2} /)) {
			//if in form "yyyy mm dd  hh:mm:ss", convert to "yyyy/mm/dd  hh:mm:ss" instead
			vtxSearch = varMatches[0];
			vtxReplaceWith = vtxSearch.substr(0,4) + "/" + gfnTrimLeadingTrailingSpaces(vtxSearch.substr(5)) + "/";
			vobRegExp = new RegExp(vtxSearch,"i");
			ptxValue = ptxValue.replace(vobRegExp,vtxReplaceWith);
		}
		return (!isNaN(Date.parse(ptxValue)));
	}

	function gfnValidateDate(ptxString,ptxFieldname) {
		if (ptxString != '') {
			if (gfnCheckDateTimeFormat(ptxString,false,false)) {
				if (!gfnCheckDateTimeValidity(ptxString)) {
					if (ptxFieldname != '')	{
						alert("\"" + ptxString + " is not a valid date (wrong number of days in month, maybe?)");
					}
					return false;
				}							
			} else {
				if (ptxFieldname != '')	{
					alert(ptxFieldname + " must be in the form YYYY MMM DD, eg: \"2005 Apr 1\".\n\nFormats YYYY/MM/DD (eg: \"2005/04/01\") and YYYY MM DD are also supported.");
				}
				return false;
			}
		}
		return true;
	}
						

	function gfnValidateEmail(ptxEmail) {

		vobRegExp = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/i
		if (ptxEmail.match(vobRegExp))
		{
			$vynReturn = false;
		} else {
			vobRegExp2 = /^.+\@(\[?)[a-zA-Z0-9\.\-]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/i
			if (ptxEmail.match(vobRegExp2))
			{
				vynReturn = true;
			}
			else
			{
				vynReturn = false;
			}
		}
		return vynReturn;
	}


	function gfnValidateURL(ptxString) {

		vobRegExp = /^(http|https)\:\/\/[a-zA-Z0-9\._]+(\.[a-zA-Z0-9\-\._]+)*(\.[a-zA-Z0-9\.]{2,3})((\/)|(\/[a-zA-Z0-9\-\.\/\\\#_]+(\?([a-zA-Z0-9\-\.\,\'\/\\\+=&%\$#_]+))?))?$/i

		if (ptxString.match(vobRegExp))
		{
			vynReturn = true;
		}
		else
		{
			vynReturn = false;
		}
		return vynReturn;
	}


	//note: not tested yet!
	function gfnValidateNumeric(ptxString) {

		vobRegExp = /^[0-9]*$/i
		if (ptxString.match(vobRegExp))
		{
			vynReturn = true;
		}
		else
		{
			vynReturn = false;
		}
		return vynReturn;
	}

	function gfnValidatePhone(ptxString) {

		vobRegExp = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/i
		if (ptxString.match(vobRegExp))
		{
			vynReturn = true;
		}
		else
		{
			vynReturn = false;
		}
		return vynReturn;
	}

	function gfnValidatePostalCode(ptxString) {

		vobRegExp = /^[A-Z]{1}[0-9]{1}[A-Z]{1} [0-9]{1}[A-Z]{1}[0-9]{1}$/
		if (ptxString.match(vobRegExp))
		{
			vynReturn = true;
		}
		else
		{
			vynReturn = false;
		}
		return vynReturn;
	}

	function gfnGetValueRadioButtons(pobControl) {
		/*
		not sure if this is necessary or not...
		if (pobControl.length == 0) {
			//single radio button... does not act as an array
			vtxValue = pobControl.value;
			vynChecked = pobControl.checked;
			if (vynChecked) {
				return vtxValue;
			}
		} else {
		*/
			for (vinIndex = 0; vinIndex < pobControl.length; vinIndex++) {
				vtxValue = pobControl[vinIndex].value;
				vynChecked = pobControl[vinIndex].checked;
				if (vynChecked) {
					return vtxValue;
				}
			}
		//}
		return "";
	}

	function gfnSetValueRadioButtons(pobControl,ptxValue) {
		for (vinIndex = 0; vinIndex < pobControl.length; vinIndex++) {
			vtxValue = pobControl[vinIndex].value;
			if (vtxValue == ptxValue) {
				pobControl[vinIndex].checked = true;
			}
		}
	}

	function gfnResetRadioButtons(pobControl) {
		for (vinIndex = 0; vinIndex < pobControl.length; vinIndex++) {
			pobControl[vinIndex].checked = false;
		}
		return true;
	}

	function gfnGetValueBitwiseCheckBoxes(pobForm,vtxControlName) {
		vinReturn = 0;
		vinLength = eval(pobForm[vtxControlName + "RowCount"].value);
		for (vinIndex = 0; vinIndex < vinLength; vinIndex++) {
			vobControl = pobForm[vtxControlName + '[' + vinIndex + ']'];
			if (vobControl.checked) {
				vinReturn += eval(vobControl.value);
			}
		}
		return vinReturn;
	}

	function gfnResetCheckBoxes(pobForm,vtxControlName) {
		vinLength = eval(pobForm[vtxControlName + "RowCount"].value);
		for (vinIndex = 0; vinIndex < vinLength; vinIndex++) {
			pobForm[vtxControlName + '[' + vinIndex + ']'].checked = false;
		}
		return true;
	}

	function gfnToggleVisibility(ptxDivID) {
		if (document.all[ptxDivID].style.display == "none") {
			document.all[ptxDivID].style.display = "block";
		} else {
			document.all[ptxDivID].style.display = "none";
		}
		return false;
	}

	function gfnCheckPasswordValidity(ptxValue,pinMin,pinMax) {
		ptxValue = gfnTrimLeadingTrailingSpaces(ptxValue);
		if (ptxValue.length < pinMin) {
			return false;
		}
		if (ptxValue.length > pinMax) {
			return false;
		}
		vynNum = false;
		vynUpper = false;
		vynLower = false;
		for (vinIndex = 0; vinIndex < ptxValue.length; vinIndex++)	{
			vinCharCode = ptxValue.charCodeAt(vinIndex);
			if ((vinCharCode >= 48) && (vinCharCode <= 57)) {
				vynNum = true;
			} else if ((vinCharCode >= 65) && (vinCharCode <= 90)) {
				vynUpper = true;
			} else if ((vinCharCode >= 97) && (vinCharCode <= 122)) {
				vynLower = true;
			} else {
				//non alphanumeric character
				return false;
			}
		}
		return (vynNum && vynUpper && vynLower);
	}

	function gfnPrinterFriendly(ptxURL){
		vobWindow = window.open(ptxURL,'printerFriendly','height=430,width=750,menubar,resizable,scrollbars,status,toolbar');
		vobWindow.focus();
	}

	function gfnActionAfterAddChanged(pobControl) {
		//
		vobForm = pobControl.form;
		if (eval(gfnGetValueRadioButtons(vobForm['fobActionAfterAdd']))) {
			//if clicking "Add another Item",  make the "default to last item?" box checked off
			vobForm['fobDefaultToLast'].checked = true;
		}
		return true;
	}

	function gfnSortControls(pobForm,ptxSortControl,ptxSortControlLast,ptxSortControlRowCount,pinRowNum) {
		vobSortControl = pobForm[ptxSortControl + '[' + pinRowNum + ']'];
		vinValueNew = eval(vobSortControl.options[vobSortControl.selectedIndex].value);
		vinValueOrig = eval(pobForm[ptxSortControlLast + '[' + pinRowNum + ']'].value);
		vinNumRows = eval(pobForm[ptxSortControlRowCount].value);
		for (vinIndex = 0; vinIndex < vinNumRows; vinIndex++) {
			if (vinIndex != eval(pinRowNum)) {
				//alert(vinIndex + " compare to " + pinRowNum);
				vobSortControl = pobForm[ptxSortControl + '[' + vinIndex + ']'];
				vinValue = eval(vobSortControl.options[vobSortControl.selectedIndex].value);
				//alert(vinValueOrig + " changed to " + vinValueNew);
				if (vinValueNew < vinValueOrig)	{
					if ((vinValue >= vinValueNew) && (vinValue < vinValueOrig)) {
						//increment by one
						vobSortControl.selectedIndex = vobSortControl.selectedIndex + 1;					
					}
				} else { //vinValueNew > vinValueOrig
					if ((vinValue > vinValueOrig) && (vinValue <= vinValueNew)) {
						//decrement by one
						vobSortControl.selectedIndex = vobSortControl.selectedIndex - 1;					
					}
				}
			}
		}
		//set the 'last' values
		for (vinIndex = 0; vinIndex < vinNumRows; vinIndex++) {
			vobSortControl = pobForm[ptxSortControl + '[' + vinIndex + ']'];
			vtxValue = vobSortControl.options[vobSortControl.selectedIndex].value;
			pobForm[ptxSortControlLast + '[' + vinIndex + ']'].value = vtxValue;			
		}
	}

	//following two functions derived from Apple sample code at http://developer.apple.com/internet/webcontent/examples/utility.txt

	function gfnGetStyleObject(pobObjectId) {
		// cross-browser function to get an object's style object given its id
		if(document.getElementById && document.getElementById(pobObjectId)) {
			// W3C DOM
			return document.getElementById(pobObjectId).style;
		} else if (document.all && document.all(pobObjectId)) {
			// MSIE 4 DOM
			return document.all(pobObjectId).style;
		} else if (document.layers && document.layers[pobObjectId]) {
			// NN 4 DOM.. note: this won't find nested layers
			return document.layers[pobObjectId];
		} else {
			return false;
		}
	}

	function gfnGetStyleObjectLink(pobObjectId) {
		// cross-browser function to get an object's style object given its id
		if(document.getElementById && document.getElementById(pobObjectId)) {
			// W3C DOM
			return document.getElementById(pobObjectId).style;
		} else if (document.links && document.links(pobObjectId)) {
			// MSIE 4 DOM
			return document.links(pobObjectId).style;
		} else if (document.layers && document.layers[pobObjectId]) {
			// NN 4 DOM.. note: this won't find nested layers
			return document.layers[pobObjectId];
		} else {
			return false;
		}
	}

	function gfnChangeObjectVisibility(pobObjectId, ptxNewVisibility) {
		// get a reference to the cross-browser style object and make sure the object exists
		var pobStyleObject = gfnGetStyleObject(pobObjectId);
		if(pobStyleObject) {
			pobStyleObject.visibility = ptxNewVisibility;
			return true;
		} else {
			// we couldn't find the object, so we can't change its visibility
			return false;
		}
	} 

//functions from Reva

//function to do mouseover
function imgOver(imgName){
document[imgName].src=eval("mo_" + imgName + ".src");
}
       
//function to do deactivate
function imgOut(imgName){
document[imgName].src=eval("nr_" + imgName + ".src");
}


//function to do mouseover
function imgOver2(imgName,type){
document[imgName].src=eval("mo_" + type + ".src");
}
       
//function to do deactivate
function imgOut2(imgName,type){
document[imgName].src=eval("nr_" + type + ".src");
}

//application-specific functions, but shared between different pages
function afnGoSource(ptxURL,ptxWinName){
	vobWindow = window.open(ptxURL,ptxWinName,'height=430,width=750,menubar,resizable,scrollbars,status,toolbar,location');
	vobWindow.focus();
}

function afnGoNew(ptxURL,ptxWinName){
	vobWindow = window.open(ptxURL,ptxWinName,'height=430,width=750,menubar,resizable,scrollbars,status,toolbar,location');
	vobWindow.focus();
}

function afnMenuMouseovers(pobObject,ptxObjectID,pynShow,pobEvent,ptxLevelOneImage) {
	if (pynShow) {
		//    alert(eventObj);
		//hideAllMenus();
		if (gfnChangeObjectVisibility(ptxObjectID, 'visible')) {
			pobEvent.cancelBubble = true;
			vynReturn = true;
		} else {
			vynReturn = false;
		}
	} else {
		gfnChangeObjectVisibility(ptxObjectID, 'hidden');
		vynReturn = true;
	}
	if (ptxLevelOneImage != "") {
		//also do the image change mouseovers for the Level One menu item
		if (pynShow) {
			eval("document['" + ptxLevelOneImage + "'].src=mo_" + ptxLevelOneImage + ".src");
		} else {
			eval("document['" + ptxLevelOneImage + "'].src=nr_" + ptxLevelOneImage + ".src");
		}
	}
	return vynReturn;
}

function afnClearDocTags(pobForm,ptxControlName) {

	vinNumTagFamilies = eval(pobForm[ptxControlName + 'FamilyCount'].value);
	for (vinFamilyIndex = 0; vinFamilyIndex < vinNumTagFamilies; vinFamilyIndex++) {
		vinNumTagsThisFamily = eval(pobForm[ptxControlName + 'RowCount[' + vinFamilyIndex + ']'].value);
		for (vinRowIndex = 0; vinRowIndex < vinNumTagsThisFamily; vinRowIndex++) {
			pobForm[ptxControlName + '[' + vinFamilyIndex + '][' + vinRowIndex + ']'].checked = false;
		}
	}
}

function afnOpenCMSCheckout(ptxPathToWWWRoot,pinDocID) {
	vobWindow = window.open(ptxPathToWWWRoot + 'CMSCheckout.php?pinDocument=' + pinDocID,'checkout','height=300,width=700,menubar,resizable,scrollbars,status');
	vobWindow.focus();
}
