﻿var FormDataChanged = 'False';
var mainWin;
var EvalWin;
var HelpWin;
var ErrWin;
var PrintWin;
var DebugWin;
var varFilterChangeMessage = 'Selection filters have been changed.';
var CRLF = "\r\n";

function GetURLValue(curVal) {
	var URLstr = location.search;
	if (URLstr == '') return '';
	curVal += "=";
	if (URLstr.indexOf(curVal) == -1) return '';
	var curValLen = curVal.length + 1;
	URLstr = URLstr.substring(curValLen);
	if (URLstr.indexOf('&') == -1) return URLstr;
	return URLstr.substr(0,URLstr.indexOf('&'));
}

function CheckJumpURL() {
	if (document.getElementById('JumpURL') != null) {
		if (document.getElementById('JumpURL').value != '') {
			document.getElementById('data_form').action = document.getElementById('JumpURL').value;
			document.getElementById('JumpURL').value = '';
			document.getElementById('proc').value = '';
			document.getElementById('data_form').submit();
		}
	}
}

function TranItem(TranType,TranID,PasteID) {
	//TranType might be used later to perform a change to related paste drop-downs in UQI_0004
	document.getElementById(TranID).focus();
	document.getElementById(TranID).value = document.getElementById(PasteID)[document.getElementById(PasteID).selectedIndex].innerHTML;
	CCS_Activate_SaveUndo_State(TranID);
}

function SetCurrentForm() {
	if (document.getElementById('aiTabActive').value == "0") {
		if (document.getElementById('UQI_0008') != null) {
			document.getElementById('UQI_0008').className = 'IRT_tab_inactive';
		}
	}
	DisableOrEnableFields('disable',document.getElementById('ReportRole').value, document.getElementById('Session_acr').value, document.getElementById('i_InjuryProcessStateID').value);
}	

function SetFieldMode(curMode,curID) {
	if (document.getElementById(curID) != null) {
		if (curMode == 'enable') {
			document.getElementById(curID).disabled = false;
			return;
		}
		document.getElementById(curID).readOnly = true;
		document.getElementById(curID).className = 'disabled';
		if (document.getElementById(curID).tagName == 'SELECT') {
			document.getElementById(curID).disabled = 'disabled';
		}
		if (document.getElementById(curID).type == 'button' || document.getElementById(curID).type == 'checkbox' || document.getElementById(curID).type == 'radio') {
			document.getElementById(curID).disabled = 'disabled';
		}
	}
}
function ShowPopupHelp(rState) {
	ChangeItemVisibility('popupHelpDiv','hidden');
	var curVal = document.getElementById('popupHelp').value;
	// hide if already visible
	if (curVal != 'CONSOLE') {
		if (document.getElementById('popupHelpNav').innerHTML.indexOf('Hide') != -1) {
			HidePopupHelp(rState);
			return;
		}
	}
	var i_InjuryProcessStateID = document.getElementById('i_InjuryProcessStateID').value;
	//first check if user has requested all popups be hidden
	if (curVal != 'CONSOLE') {
		if (document.getElementById('popupHelpHide').value == '1' && rState=='popup') return;
	}
	//rState is used to determine if it was popped up or clicked
	//if the user clicked, we may need to remove the 'd' which was placed to indicate it is already shown
	if (curVal.substring(0,1) == 'd' && rState == 'clicked') {
		curVal = curVal.substring(1,curVal.length);
	}
	switch (curVal) {
		case "CONSOLE" :
			document.getElementById('popupHelpDiv').style.left = '40px';
			document.getElementById('popupHelpDiv').style.top = '578px';
			document.getElementById('popupHelpDiv').style.height = '20px';
			document.getElementById('popupHelpDiv').style.width = '340px';
			document.getElementById('popupHelpDiv').style.overflow = 'hidden';
			document.getElementById('popupHelpDiv').style.padding = '2px';
			DisplayShadow();
			ChangeItemVisibility('popupHelpDiv','visible');
			break;
		case "NEW" :
			document.getElementById('popupHelpDiv').style.left = '500px';
			document.getElementById('popupHelpDiv').style.top = '360px';
			document.getElementById('popupHelpDiv').style.height = '220px';
			document.getElementById('popupHelpDiv').style.width = '320px';
			DisplayShadow();
			ChangeItemVisibility('popupHelpDiv','visible');
			break;
		case "MODIFY1" :
			switch (document.getElementById('ReportRole').value) {
				case "3" :
					if (i_InjuryProcessStateID >= 4) {
						document.getElementById('popupHelpDiv').style.left = '755px';
						document.getElementById('popupHelpDiv').style.top = '240px';
						document.getElementById('popupHelpDiv').style.height = '190px';
						document.getElementById('popupHelpDiv').style.width = '200px';
					} else {
						document.getElementById('popupHelpDiv').style.left = '755px';
						document.getElementById('popupHelpDiv').style.top = '140px';
						document.getElementById('popupHelpDiv').style.height = '310px';
						document.getElementById('popupHelpDiv').style.width = '200px';
					}
					break;
				case "4" :
					if (i_InjuryProcessStateID >= 6) {
						document.getElementById('popupHelpDiv').style.left = '755px';
						document.getElementById('popupHelpDiv').style.top = '240px';
						document.getElementById('popupHelpDiv').style.height = '190px';
						document.getElementById('popupHelpDiv').style.width = '200px';
					} else {
						document.getElementById('popupHelpDiv').style.left = '755px';
						document.getElementById('popupHelpDiv').style.top = '140px';
						document.getElementById('popupHelpDiv').style.height = '290px';
						document.getElementById('popupHelpDiv').style.width = '200px';
					}
					break;
				default :
					if (i_InjuryProcessStateID >= 3) {
						document.getElementById('popupHelpDiv').style.left = '755px';
						document.getElementById('popupHelpDiv').style.top = '240px';
						document.getElementById('popupHelpDiv').style.height = '190px';
						document.getElementById('popupHelpDiv').style.width = '200px';
					} else {
						document.getElementById('popupHelpDiv').style.left = '285px';
						document.getElementById('popupHelpDiv').style.top = '340px';
						document.getElementById('popupHelpDiv').style.height = '190px';
						document.getElementById('popupHelpDiv').style.width = '200px';
					}
					break;
			}
			DisplayShadow();
			ChangeItemVisibility('popupHelpDiv','visible');
		break;
		case "MODIFY2" :
			document.getElementById('popupHelpDiv').style.left = '620px';
			document.getElementById('popupHelpDiv').style.top = '260px';
			document.getElementById('popupHelpDiv').style.height = '170px';
			document.getElementById('popupHelpDiv').style.width = '250px';
			DisplayShadow();
			ChangeItemVisibility('popupHelpDiv','visible');
			break;
		case "MODIFY3" :
			switch (document.getElementById('ReportRole').value) {
				case "3" :
					document.getElementById('popupHelpDiv').style.left = '440px';
					document.getElementById('popupHelpDiv').style.top = '525px';
					document.getElementById('popupHelpDiv').style.height = '130px';
					document.getElementById('popupHelpDiv').style.width = '530px';
					break;
				case "4" :
					document.getElementById('popupHelpDiv').style.left = '440px';
					document.getElementById('popupHelpDiv').style.top = '525px';
					document.getElementById('popupHelpDiv').style.height = '130px';
					document.getElementById('popupHelpDiv').style.width = '530px';
					break;
				default :
					document.getElementById('popupHelpDiv').style.left = '815px';
					document.getElementById('popupHelpDiv').style.top = '140px';
					document.getElementById('popupHelpDiv').style.height = '490px';
					document.getElementById('popupHelpDiv').style.width = '160px';
					break;
			}
			DisplayShadow();
			ChangeItemVisibility('popupHelpDiv','visible');
			break;
		case "MODIFY4" :
			switch (document.getElementById('ReportRole').value) {
				case "3" :
					document.getElementById('popupHelpDiv').style.left = '620px';
					document.getElementById('popupHelpDiv').style.top = '260px';
					document.getElementById('popupHelpDiv').style.height = '150px';
					document.getElementById('popupHelpDiv').style.width = '250px';
					break;
				case "4" :
					document.getElementById('popupHelpDiv').style.left = '620px';
					document.getElementById('popupHelpDiv').style.top = '260px';
					document.getElementById('popupHelpDiv').style.height = '150px';
					document.getElementById('popupHelpDiv').style.width = '250px';
					break;
				default :
					document.getElementById('popupHelpDiv').style.left = '620px';
					document.getElementById('popupHelpDiv').style.top = '260px';
					document.getElementById('popupHelpDiv').style.height = '240px';
					document.getElementById('popupHelpDiv').style.width = '250px';
					break;
			}
			DisplayShadow();
			ChangeItemVisibility('popupHelpDiv','visible');
			break;
		case "MODIFY5" :
			switch (document.getElementById('ReportRole').value) {
				case "3" :
					document.getElementById('popupHelpDiv').style.left = '620px';
					document.getElementById('popupHelpDiv').style.top = '260px';
					document.getElementById('popupHelpDiv').style.height = '150px';
					document.getElementById('popupHelpDiv').style.width = '250px';
					break;
				case "4" :
					document.getElementById('popupHelpDiv').style.left = '620px';
					document.getElementById('popupHelpDiv').style.top = '260px';
					document.getElementById('popupHelpDiv').style.height = '150px';
					document.getElementById('popupHelpDiv').style.width = '250px';
					break;
				default :
					document.getElementById('popupHelpDiv').style.left = '620px';
					document.getElementById('popupHelpDiv').style.top = '260px';
					document.getElementById('popupHelpDiv').style.height = '220px';
					document.getElementById('popupHelpDiv').style.width = '250px';
					break;
			}
			DisplayShadow();
			ChangeItemVisibility('popupHelpDiv','visible');
			break;
		case "MODIFY6" :
			switch (document.getElementById('ReportRole').value) {
				case "3" :
					if (i_InjuryProcessStateID >= 4) {
						document.getElementById('popupHelpDiv').style.left = '665px';
						document.getElementById('popupHelpDiv').style.top = '165px';
						document.getElementById('popupHelpDiv').style.height = '110px';
						document.getElementById('popupHelpDiv').style.width = '300px';
					} else {
						document.getElementById('popupHelpDiv').style.left = '360px';
						document.getElementById('popupHelpDiv').style.top = '485px';
						document.getElementById('popupHelpDiv').style.height = '160px';
						document.getElementById('popupHelpDiv').style.width = '610px';
					}
					break;
				case "4" :
					if (i_InjuryProcessStateID >= 6) {
						document.getElementById('popupHelpDiv').style.left = '665px';
						document.getElementById('popupHelpDiv').style.top = '165px';
						document.getElementById('popupHelpDiv').style.height = '110px';
						document.getElementById('popupHelpDiv').style.width = '300px';
					} else {
						document.getElementById('popupHelpDiv').style.left = '825px';
						document.getElementById('popupHelpDiv').style.top = '170px';
						document.getElementById('popupHelpDiv').style.height = '360px';
						document.getElementById('popupHelpDiv').style.width = '150px';
					}
					break;
				default :
					document.getElementById('popupHelpDiv').style.left = '825px';
					document.getElementById('popupHelpDiv').style.top = '170px';
					document.getElementById('popupHelpDiv').style.height = '360px';
					document.getElementById('popupHelpDiv').style.width = '150px';
					break;
			}
			DisplayShadow();
			ChangeItemVisibility('popupHelpDiv','visible');
			break;
		case "MODIFY7" :
			switch (document.getElementById('ReportRole').value) {
				case "3" :
					document.getElementById('popupHelpDiv').style.left = '580px';
					document.getElementById('popupHelpDiv').style.top = '210px';
					document.getElementById('popupHelpDiv').style.height = '210px';
					document.getElementById('popupHelpDiv').style.width = '250px';
					break;
				case "4" :
					if (i_InjuryProcessStateID >= 6) {
						document.getElementById('popupHelpDiv').style.left = '665px';
						document.getElementById('popupHelpDiv').style.top = '165px';
						document.getElementById('popupHelpDiv').style.height = '130px';
						document.getElementById('popupHelpDiv').style.width = '300px';
					} else {
						document.getElementById('popupHelpDiv').style.left = '580px';
						document.getElementById('popupHelpDiv').style.top = '210px';
						document.getElementById('popupHelpDiv').style.height = '240px';
						document.getElementById('popupHelpDiv').style.width = '250px';
					}
					break;
				default :
					document.getElementById('popupHelpDiv').style.left = '580px';
					document.getElementById('popupHelpDiv').style.top = '210px';
					document.getElementById('popupHelpDiv').style.height = '210px';
					document.getElementById('popupHelpDiv').style.width = '250px';
					break;
			}
			DisplayShadow();
			ChangeItemVisibility('popupHelpDiv','visible');
			break;
	}
	//Show GuidedTour link if popup help not visible
	if (document.getElementById('popupHelpNav') != null) {
		if (document.getElementById('popupHelpDiv').style.visibility == 'hidden') {
				document.getElementById('popupHelpNav').innerHTML = '&#9672;&nbsp;Show ' + document.getElementById('QuickHelpName').value;
		} else {
				document.getElementById('popupHelpNav').innerHTML = '&#9672;&nbsp;Hide ' + document.getElementById('QuickHelpName').value;
		}
	}
}

function DisplayShadow() {
	var leftPos = document.getElementById('popupHelpDiv').style.left;
	var topPos = document.getElementById('popupHelpDiv').style.top;
	var heightPos = document.getElementById('popupHelpDiv').style.height;
	var widthPos = document.getElementById('popupHelpDiv').style.width;
	leftPos = leftPos.substring(0,leftPos.length-2);
	leftPos = parseInt(leftPos);
	topPos = topPos.substring(0,topPos.length-2);
	topPos = parseInt(topPos);
	leftPos += 8;
	topPos += 8;
	document.getElementById('popupHelpDivShadow').style.left = leftPos.toString() + 'px';
	document.getElementById('popupHelpDivShadow').style.top = topPos.toString() + 'px';
	document.getElementById('popupHelpDivShadow').style.height = heightPos;
	document.getElementById('popupHelpDivShadow').style.width = widthPos;
	ChangeItemVisibility('popupHelpDivShadow','visible');
}
function HidePopupHelp(curState) {
	ChangeItemVisibility('popupHelpDivShadow','hidden');
	ChangeItemVisibility('popupHelpDiv','hidden');
	document.getElementById('popupHelpNav').innerHTML = '&#9672;&nbsp;Show ' + document.getElementById('QuickHelpName').value;
	if (curState == 'all') {
		document.getElementById('popupHelpHide').value = '1';
	}
}

function ShowLinkWindow(varURL) {
	if (varURL.substr(0,4) != 'http') {
		varURL = 'http://' + varURL;
	}
	var lWin = window.open(varURL,'lWin',"width=900,height=530,left=30,top=30,center=no,resizable=yes,status=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=yes");
	lWin.focus();
}
function ShowContactsWindow() {
	var varURL = 'http://www.uq.edu.au/contacts/';
	var cWin = window.open(varURL,'cWin',"width=850,height=530,left=30,top=30,center=no,resizable=yes,status=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=yes");
	cWin.focus();
}

function DemoUnderConstruction() {
	alert('This feature is under construction.');
}

function DisplayMessages(curID,curclassName) {
	if (document.getElementById(curID) != null) {
		if (document.getElementById(curID).value != '') {
			document.getElementById('message_area').className = curclassName;
			document.getElementById('message_area').innerHTML = '&nbsp;&nbsp;' + document.getElementById(curID).value;
		}
	}
}

function TabToPage(curItem) {
	if (CheckForSave() == false && CheckForNew() == false) {
		if (curItem.id == 'UQI_0008' && document.getElementById('aiTabActive').value == "0") {
			alert('The Additional Injury Details form is not relevant for the current Incident.');
			return;
		} else {
			document.getElementById('proctype').value = '';
			document.getElementById('data_form').action = curItem.id + '.asp';
			document.getElementById('data_form').submit();
		}
	}
}

function CheckForSave() {
	if (document.getElementById('cmdSave') != null) {
		if (document.getElementById('cmdSave').disabled == false) {
			alert('Please click Save to save your current changes first, or click Cancel to discard your changes.');
			return true;
		}
	}
	return false;
}

function CheckForNewReport() {
	if (document.getElementById('FormCode').value.substr(0,6) != 'UQI_00') {
		return false;
	}
	if (document.getElementById('EditMode').value == 'NEW' || document.getElementById('EditMode').value == 'NEWSUBMIT') {
		var answer = confirm ("You have not completed this report. Click OK to stop the report process now.");
		if (answer == 1) {
			return false;
		} else {
			return true;
		}
	}
	return false;
}


function CheckForNew() {
	if (document.getElementById('cid') != null) {
		if (document.getElementById('cid').value == 'NEW') {
			alert('You must fill out this form first and save this new item before modifying other form pages.');
			return true;
		}
	}
	return false;
}

function ConfirmDelete() {
	var answer = confirm ("Are you sure you want to delete this item?");
	if (answer == 1) {
		return true;
	} else {
		return false;
	}
}

function DeleteItem(dMode) {
	switch (dMode) {
		case "delete" : //delete
			if (ConfirmDelete() == true) {
				document.getElementById('proc').value = '1';
				document.getElementById('proctype').value = 'DELETE';
				document.getElementById('data_form').submit();
			}
			break;
		case "code" : //used in code
			alert('These items are used internally for coding purposes. They cannot be deleted.');
			break;
		case "pastDate" : //allow delete if > current month/year
			alert('This item date is already in effect in past or current periods. It cannot be deleted.');
			break;
		case "construct" : //under construction
			ShowUnderConstructionAlert();
			break;
	}
}

function roundNumber(rNum,rLength) {
	var newnumber;
	if (rNum > 8191 && rNum < 10485) {
		rNum = rNum-5000;
		return Math.round(rNum*Math.pow(10,rLength))/Math.pow(10,rLength);
		//newnumber = newnumber+5000;
	} else {
		return Math.round(rNum*Math.pow(10,rLength))/Math.pow(10,rLength);
	}
}


function ConvertDateToISO(DateStr) {
	/*
	This function takes an entered date and makes it conform to the specified date format
	This function gives the same results as the former VBscript function called CheckDate 
	*/

	var i;
	/* capture (day, month, year) into an array
	element 0 = day
	element 1 = month
	element 2 = year
	*/
	var DateElements = DateStr.split("/");

	for (i = 0; i <= 1; i++) {
		if (DateElements[i] == null) {
			DateElements[i] = "00";
		} else {
			if (DateElements[i].length == 1) {
				DateElements[i] = "0" + DateElements[i];
			}
		}
	}
	if (DateElements[2] == null) {
		DateElements[2] = "0000";
	} else {
		var YearLength = DateElements[2].length;
		if (YearLength == 2) {
			DateElements[2] = "20" + DateElements[2];
		}
		if (YearLength == 1) {
			DateElements[2] = "200" + DateElements[2];
		}
	}
	return DateElements[2] + "-" + DateElements[1] + "-" + DateElements[0];
}

function ConvertDateTimeToISO(DateTimeStr) {
	if (DateTimeStr != '') {
		var DateTimeElements = DateTimeStr.split(" ");
		return ConvertDateToISO(DateTimeElements[0]) + " " + DateTimeElements[1];
	} else {
		return '';
	}
}

function ShowUnderConstructionAlert() {
	alert('This feature is under construction.');
}

function SignIn_checkEnter(keyevent) {
	var characterCode = keyevent.keyCode //character code is contained in IE's keyCode property

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		ProcessFormData('signin');
		return true;
	}
}

function SetSortCriteria(valueName,curValue,preFix,listID) {
	if (CheckForSave() == false) {
		document.getElementById(valueName).value = curValue;
		document.getElementById('data_form').submit();
	} else {
		SelectListItem(listID,valueName,preFix,1);
	}
}

function SetSelectCriteria(valueName,curValue,preFix,listID) {
	if (CheckForSave() == false) {
		document.getElementById(valueName).value = curValue;
		document.getElementById('data_form').submit();
	} else {
		SelectListItem(listID,valueName,preFix,1);
	}
}

function SetTextToDefault(curID) {
	document.getElementById(curID).value = "";	
	document.getElementById(curID).name = "";	
}

function RemoveFormValues() {
	document.getElementById('button_pressed').value = '';
	document.getElementById('proctype').value = '';
	document.getElementById('proc').value = '';
}

function CancelChanges(cancelValue) {
	if (document.getElementById('cmdCancel').disabled == false) {
		if (cancelValue == '1') {
			WindowFocusParentCloseCurrent();
		} else {
			if (document.getElementById('i_IncidentID') != null) {
				if (document.getElementById('i_IncidentID').value == 'NEW') {
					//if cancelling new item, return to console
					document.getElementById('data_form').action = 'UQI_Console.asp';
					document.getElementById('data_form').submit();
				}
			}
			if (document.getElementById('i_injAreas') != null) {
				document.getElementById('i_injAreas').value = '';
			}
			RemoveFormValues();
			document.getElementById('data_form').submit();
		}
	}
}

function WindowFocusParentCloseCurrent() {
	try {
		window.opener.focus();
	}
	catch(err) {
		//do nothing
	}
	window.close();
}

function goBack() {
	if (document.getElementById('backURL') != null) {
		if (document.getElementById('backURL').value != '') {
			if (CheckForSave() == false) {
				document.getElementById('proc').value = '';
				document.getElementById('data_form').action = document.getElementById('backURL').value;
				document.getElementById('data_form').submit();
			}
		}
	}
}

function goHome() {
	if (CheckForSave() == false) {
		if (CheckForNewReport() == false) {
			document.getElementById('proc').value = '';
			document.getElementById('data_form').action = 'UQI_Console.asp';
			document.getElementById('data_form').submit();
		}
	}
}

function SetSignOutFlag() {
	if (CheckForSave() == false) {
		if (CheckForNewReport() == false) {
			if (document.getElementById('pSignOut') != null) {
				document.getElementById('pSignOut').value = '1';
				document.getElementById('data_form').submit();
			}
		}
	}
}

function jsCheckForSignOut() {
	if (document.getElementById('pSignOut') != null) {
		if (document.getElementById('pSignOut').value == '1') {
			try {
				opener.focus();
			}
			catch(err) {
				//do nothing
			}
			window.close();
			return true;
		}
	}
}

function Substitute_ID_for_Name(itemID) {
	document.getElementById(itemID).name = itemID;
}

function CCS_CheckDate(DateID,TextName) {
	var tDate;
	tDate = CCS_FormatDate(document.getElementById(DateID).value);
	if (CCS_ChkDate(CCS_IsDate(tDate)) == true) {
		document.getElementById(DateID).value = tDate;
		return true;
	} else {
		alert ("Please enter a valid " + TextName + " using the dd/mm/yyyy format.");
		document.getElementById(DateID).focus();
		return false;
	}
}

function CCS_CheckText(TextID, TextName, MaxLength) {
	if (document.getElementById(TextID).value.length <= MaxLength) {
		return true;
	} else {
		alert (TextName + " is limited to " + MaxLength + " characters. Please abbreviate your entry.");
		document.getElementById(TextID).focus();
		return false;
	}
}

function CCS_FindMatchingEmails(ID1, IDName1, ID2, IDName2) {
	if (document.getElementById(ID1).value != '' && document.getElementById(ID2).value != '') {
		if (document.getElementById(ID1).value == document.getElementById(ID2).value) {
			alert("The " + IDName1 + " and " + IDName2 + " cannot be the same.");
			document.getElementById(ID2).focus();
			return false;
		}
	}
	return true;
}

function CCS_CheckEmail(EmailID, TextName, MaxLength) {
	//assume valid
	var ValidEmail = 1;
	if (document.getElementById(EmailID).value.length > MaxLength) {
		ValidEmail = 0;
	}
	if (document.getElementById(EmailID).value.length < 6) {
		ValidEmail = 0;
	}
	if (document.getElementById(EmailID).value.indexOf(".") == -1) {
		ValidEmail = 0;
	}
	if (document.getElementById(EmailID).value.indexOf("@") == -1) {
		ValidEmail = 0;
	}	
	if (ValidEmail == 1) {
		return true;
	} else {
		alert ("Please enter a valid email address.");
		document.getElementById(EmailID).focus();
		return false;
	}
}

function CCS_CheckCurrency(CurrencyID,allowNeg) {
	var tCurrency;
	var valNumChars = "0123456789.";
	var i;

	//assume valid number
	var valNumber = true;

	// remove the dollar sign
	tCurrency = document.getElementById(CurrencyID).value.replace("$","");
	
	//add trailing zeros
	if (tCurrency.indexOf(".") == -1) {
		tCurrency += ".00";
	}
	if (tCurrency.lastIndexOf(".") == tCurrency.length) {
		tCurrency += "00";
	}
	if (tCurrency.lastIndexOf(".") == tCurrency.length -1) {
		tCurrency += "0";
	}
	if (tCurrency.indexOf(".") != tCurrency.lastIndexOf(".")) {
		//two decimals - invalid
		valNumber = false;
	} else {
		// search for non-numerics
		// and fail validation if one is found
		for (i = 0; i <= tCurrency.length; i++) {
			if (i == 0 && allowNeg == '1') {
				if (tCurrency.charAt(0) == '-') {
					i++; //this is okay; continue checking
				}
			}
			if (valNumChars.indexOf(tCurrency.charAt(i)) == -1) {
				valNumber = false;
			}
		}
	}
	if (valNumber == true) {
		document.getElementById(CurrencyID).value = tCurrency;
		return true;
	} else {
		alert ("Please enter a valid Rate.");
		document.getElementById(CurrencyID).focus();
		return false;
	}
}

function CCS_CheckNumeric(NumericID, MaxLength, TextName) {
	//allows numerics, including decimals, but no negative numbers
	var tNumeric = document.getElementById(NumericID).value;
	var valNumChars = "0123456789.";
	var i;

	//assume valid number
	var valNumber = true;

	if (tNumeric.length > MaxLength) {
		valNumber = false;
	} else {
		for (i = 0; i <= tNumeric.length; i++) {
			if (valNumChars.indexOf(tNumeric.charAt(i)) == -1) {
				valNumber = false;
			}
		}
	}

	if (valNumber == true) {
		return true;
	} else {
		alert ("Please enter a valid value for the " + TextName + ".");
		document.getElementById(NumericID).focus();
		return false;
	}
}

function CCS_CheckNumericWithNeg(NumericID, MaxLength, TextName) {
	//allows numerics, including decimals, but no negative numbers
	var tNumeric = document.getElementById(NumericID).value;
	var valNumChars = "0123456789.";
	var i;
	//assume valid number
	var valNumber = true;
	if (tNumeric.length > MaxLength) {
		valNumber = false;
	} else {
		var curLength = tNumeric.length;
		var testChar = 1;
		for (i = 0; i <= tNumeric.length; i++) {
			//allow negative
			if (i == 0) {
				if (tNumeric.substring(0,1) == '-') {
					testChar = 0;
				}
			}
			if (testChar == 1) {
				if (valNumChars.indexOf(tNumeric.charAt(i)) == -1) {
					valNumber = false;
				}
			}
		}
	}
	if (valNumber == true) {
		return true;
	} else {
		alert ("Please enter a valid value for the " + TextName + ".");
		document.getElementById(NumericID).focus();
		return false;
	}
}

function CCS_CheckNumericSilent(NumericID, MaxLength, TextName) {
	//allows numerics, including decimals, but no negative numbers
	var tNumeric = document.getElementById(NumericID).value;
	var valNumChars = "0123456789.";
	var i;

	//assume valid number
	var valNumber = true;

	if (tNumeric.length > MaxLength) {
		valNumber = false;
	} else {
		for (i = 0; i <= tNumeric.length; i++) {
			if (valNumChars.indexOf(tNumeric.charAt(i)) == -1) {
				valNumber = false;
			}
		}
	}

	if (valNumber == true) {
		return true;
	} else {
		//no message
		//alert ("Please enter a valid value for the " + TextName + ".");
		document.getElementById(NumericID).focus();
		return false;
	}
}

function CCS_CheckInteger(NumericID, MaxLength, MaxValue, TextName) {
	//allows positive integers only (no decimals, and no negative numbers)
	var tNumeric = document.getElementById(NumericID).value;
	var valNumChars = "0123456789";
	var i;

	//assume valid number
	var valNumber = true;

	if (tNumeric.length > MaxLength || tNumeric.length == 0) {
		valNumber = false;
	} else {
		for (i = 0; i <= tNumeric.length; i++) {
			if (valNumChars.indexOf(tNumeric.charAt(i)) == -1) {
				valNumber = false;
			}
		}
	}
	if (valNumber == true && MaxValue != 0) {
		if (tNumeric > MaxValue) {
			alert ("The value entered for " + TextName + " exceeds the maximum value allowed.");
			document.getElementById(NumericID).focus();
			return false;
		}
	}
	if (valNumber == true) {
		return true;
	} else {
		alert ("Please enter a valid value for the " + TextName + ".");
		document.getElementById(NumericID).focus();
		return false;
	}
}

function CCS_CheckIntegerSilent(NumericID, MaxLength, TextName) {
	//allows positive integers only (no decimals, and no negative numbers)
	var tNumeric = document.getElementById(NumericID).value;
	var valNumChars = "0123456789";
	var i;

	//assume valid number
	var valNumber = true;

	if (tNumeric.length > MaxLength || tNumeric.length == 0) {
		valNumber = false;
	} else {
		for (i = 0; i <= tNumeric.length; i++) {
			if (valNumChars.indexOf(tNumeric.charAt(i)) == -1) {
				valNumber = false;
			}
		}
	}

	if (valNumber == true) {
		return true;
	} else {
		//no message displays - this validation used to avoid parseInt errors
		return false;
	}
}

function CCS_FormatDate(DateStr) {
	/*
	This function takes an entered date and makes it conform to the specified date format
	This function gives the same results as the former VBscript function called CheckDate 
	*/

	var i;
	/* capture (day, month, year) into an array
	element 0 = day
	element 1 = month
	element 2 = year
	*/
	var DateElements = DateStr.split("/");

	for (i = 0; i <= 1; i++) {
		if (DateElements[i] == null) {
			DateElements[i] = "00";
		} else {
			if (DateElements[i].length == 1) {
				DateElements[i] = "0" + DateElements[i];
			}
		}
	}
	if (DateElements[2] == null) {
		DateElements[2] = "0000";
	/*
	} else {
		var YearLength = DateElements[2].length;
		if (YearLength == 2) {
			DateElements[2] = "20" + DateElements[2];
		}
		if (YearLength == 1) {
			DateElements[2] = "200" + DateElements[2];
		}
	*/
	}
	return DateElements[0] + "/" + DateElements[1] + "/" + DateElements[2];
}

function RemoveSpaces(curVar) {
	for (var i = 0; i < curVar.length; i++) {
		if (curVar.substr(i,1) == " ") {
			if (i == 0) {
				curVar = curVar.substr(1,curVar.length);
			} else {
				if (i == curVar.length - 1) {
					curVar = curVar.substr(0,curVar.length - 1);
				} else {
					curVar = curVar.substr(0,i-1) + curVar.substr(i+1,curVar.length);
				}
			}
		}
	}
	return curVar;
}

function MakeInvalidZero(curVar) {
	curVar = curVar.replace(" ","");
	if (curVar == "") {
		curVar = "0";
	} else {
		var valChars = "0123456789. ";
		for (var i = 0; i < curVar.length; i++) {
			if (valChars.indexOf(curVar.substr(i,1)) == -1) {
				curVar = "0";
			}
		}
	}
	return curVar;
}

function CCS_y2k(number) { return (number < 1000) ? number + 1900 : number; }

function CCS_ChkDate (day,month,year) {
	// checks if date passed is valid
	// will accept dates in following format:
	// isDate(dd,mm,ccyy), or
	// isDate(dd,mm) - which defaults to the current year, or
	// isDate(dd) - which defaults to the current month and year.
	// Note, if passed the month must be between 1 and 12, and the
	// year in ccyy format.

	var today = new Date();
	year = ((!year) ? CCS_y2k(today.getYear()):year);
	month = ((!month) ? today.getMonth():month-1);
	if (!day) return false
	var test = new Date(year,month,day);
	if ( (CCS_y2k(test.getYear()) == year) && (month == test.getMonth()) && (day == test.getDate()) ) {
		return true;
	} else {
		return false
	}
}

function CCS_IsDate (strDate) {
	var sa = new Array();
	sa = strDate.split("/");
	return CCS_ChkDate(sa[0],sa[1],sa[2]);
}

function CheckRequiredFields() {
	if (document.getElementById('RequiredFieldNames').value == '') return;
	var EditMode = document.getElementById('EditMode').value;
	if (EditMode != 'NEW' && EditMode != 'NEWSUBMIT') return;
	var TotalFieldsRequired = 0;
	var FieldStillRequired;
	var curOrFields;
	var FieldStillRequiredText = '<b>Instructions</b>: The following fields* must be filled out: ';
	var ClickNextText = '<b>Instructions</b>: Fill in any other available information, then click <b>Next</b> to continue.';
	ClickNextText += '&nbsp;<input class="button" id="cmdNext" type="button" value="Next" name="Next" title="Next" alt="Next" style="width: 45px; height: 31px; color:#000080; font-weight:bold; background-image: URL(\'images/Checkmark_2.gif\'); background-repeat: no-repeat" onclick="ProcessFormData(this.id);" />'
	var ClickSubmitText = 'Click <b>Submit to Supervisor</b> to complete the form.';
	ClickSubmitText += '&nbsp;<input class="button" id="cmdSubmittoSupervisor" type="button" value="Submit to Supervisor" name="Submit to Supervisor" title="Submit to Supervisor" alt="Submit to Supervisor" onclick="ChangeFlowStatus(\'SUBMIT_TO_SUPERVISOR\');" />'
	if (document.getElementById('RequiredFieldNames').value != 'ClickNextText') {
		var RequiredFieldNames = document.getElementById('RequiredFieldNames').value.split(",");
		var RequiredFieldIDs = document.getElementById('RequiredFieldIDs').value.split(",");
		var RequiredFieldTypes = document.getElementById('RequiredFieldTypes').value.split(",");
		for (var i=0;i < RequiredFieldNames.length; i++) {
			//check if the field has a value
			FieldStillRequired = 1;
			//check for the hidden fields, but allow injured areas from uqi_0006
			if (RequiredFieldIDs[i].indexOf("OR",0) == -1 && RequiredFieldIDs[i].indexOf("AND",0) == -1) {
				if (document.getElementById(RequiredFieldIDs[i]).style.visibility == 'hidden') {
					FieldStillRequired = 0;
				}
			}
			if (FieldStillRequired == 1) {
				switch (RequiredFieldTypes[i]) {
					case "text" :
						if (document.getElementById(RequiredFieldIDs[i]).value != "") {
							FieldStillRequired = 0;
						}
						break;
					case "select" :
						if (document.getElementById(RequiredFieldIDs[i])[document.getElementById(RequiredFieldIDs[i]).selectedIndex].innerHTML != "") {
							FieldStillRequired = 0;
						}
						break;
					case "textORselect" :
						curOrFields = RequiredFieldIDs[i].split("OR");
						if (document.getElementById(curOrFields[0]).value != "" || document.getElementById(curOrFields[1])[document.getElementById(curOrFields[1]).selectedIndex].innerHTML != "") {
							FieldStillRequired = 0;
						}
						break;
					case "yesno" :
						curOrFields = RequiredFieldIDs[i].split("AND");
						if (document.getElementById(curOrFields[0]).checked == "checked" || document.getElementById(curOrFields[1]).checked == "checked") {
							FieldStillRequired = 0;
						}
						break;
				}
			}
			if (FieldStillRequired == 1) {
				if (RequiredFieldNames[i].indexOf("OR",0) == -1) {
					FieldStillRequiredText += '[<b>' + RequiredFieldNames[i] + '</b>] ';
				} else {
					curOrFields = RequiredFieldNames[i].split("OR");
					FieldStillRequiredText += '[<b>' + curOrFields[0] + '</b>] or [<b>' + curOrFields[1] + '</b>]';
				}
				TotalFieldsRequired += 1;
			}
		}
	}
	if (TotalFieldsRequired > 0) {
		document.getElementById('RequiredFieldsMessage').innerHTML = FieldStillRequiredText + '<br />' + '<span class="smallnote">*marked with an asterisk</span>';
	} else {
		if (EditMode == 'NEW') {
			document.getElementById('RequiredFieldsMessage').innerHTML = ClickNextText;
		} else {
			document.getElementById('RequiredFieldsMessage').innerHTML = ClickSubmitText;
		}
	}
}

function CCS_Activate_SaveUndo_State(thisID) {
	CheckRequiredFields();
	var data_changed = 1 //assume true
	if (thisID != "" && thisID != "force_save") {
		// if the field is disabled, set data changed to 0
		if (document.getElementById(thisID).className.indexOf("disabled",0) != -1) {
			data_changed = 0;
		} else {
			// checkbox fields
			if (document.getElementById(thisID).type == "radio") {
				if (document.getElementById(thisID).checked == true && document.getElementById(thisID).value == "checked") {
					data_changed = 0;
				}
				if (document.getElementById(thisID).checked == false && document.getElementById(thisID).value != "checked") {
					data_changed = 0;
				}
			} else {
				if (escape(RTrim(document.getElementById(thisID).value)) == escape(RTrim(document.getElementById(thisID).name))) {
					//field still the same as initial value, so data has not changed
		 			data_changed = 0;
		 		}
			}
		}
	}
	// if data has changed, change save and undo button states
	if (data_changed == 1) {
		ActivateSaveUndoButtons();
		FormDataChanged = 'True';
	}
}

function ActivateSaveUndoButtons() {		
	if (document.getElementById('cmdSave') != null) {
		document.getElementById('cmdSave').disabled = false;
		document.getElementById('cmdSave').className = "SaveButtonHighlight";
	}
	if (document.getElementById('cmdCancel') != null) {
		document.getElementById('cmdCancel').disabled = false;
		document.getElementById('cmdCancel').className = "button";
	}
	SetDisabledButtonColors();
}

function SetDisabledButtonColors() {
	var iButtons = new Array();
	iButtons[0] = "cmdNew";
	iButtons[1] = "cmdViewModify";
	iButtons[2] = "cmdDelete";	
	iButtons[3] = "cmdSave";
	iButtons[4] = "cmdCancel";
	for (var i=0;i < iButtons.length; i++) {
		if (document.getElementById(iButtons[i]) != null) {
			if (document.getElementById(iButtons[i]).disabled == true) {
				document.getElementById(iButtons[i]).className = 'buttonDisabled';
			}
		}
	}
}

function EnableSaveCancel() {
	if (document.getElementById('cmdSave') != null) {
			document.getElementById('cmdSave').disabled = false;
			document.getElementById('cmdSave').className = "SaveButtonHighlight";
	}
	if (document.getElementById('cmdCancel') != null) {
			document.getElementById('cmdCancel').disabled = false;
	}
}

function DisableSaveCancel() {
	if (document.getElementById('cmdSave') != null) {
			document.getElementById('cmdSave').disabled = true;
			document.getElementById('cmdSave').className = "button";
	}
	if (document.getElementById('cmdCancel') != null) {
			document.getElementById('cmdCancel').disabled = true;
	}
}

function ShowFormEval(fid,ft) {
	// This function opens a window and displays evaluation information
	var FullURL = "FormsEval.asp?fid=" + fid + "&ft=" + ft;
	EvalWin = window.open(FullURL,'evalwin',"width=630,height=360,left=215,top=140,center=no,resizable=yes,status=no,scrollbars=no,toolbar=no,menubar=no,location=no");
	EvalWin.focus();
}
function ShowFormHelp(hFile) {
	// This function opens a window and displays help information
	var FullURL = "FormsHelp.asp?hlp=" + hFile;
	HelpWin = window.open(FullURL,'helpwin',"width=540,height=460,left=215,top=140,center=no,resizable=yes,status=no,scrollbars=no,toolbar=no,menubar=no,location=no");
	HelpWin.focus();
}

function ShowFormError() {
	// This function opens a window and displays error information
	var FullURL = "FormsErr.asp";
	ErrWin = window.open(FullURL,'errwin',"width=630,height=500,left=215,top=80,center=no,resizable=yes,status=no,scrollbars=yes,toolbar=no,menubar=no,location=no");
	ErrWin.focus();
}

function ShowFormPrint(urlStr) {
	// This function opens a window and displays error information
	var FullURL = "FormsPrint.asp" + urlStr;
	PrintWin = window.open(FullURL,'printwin',"width=810,height=600,left=15,top=40,center=no,resizable=yes,status=no,scrollbars=yes,toolbar=no,menubar=no,location=no");
	PrintWin.focus();
}

function ShowFormDebug() {
	// This function opens a window and displays evaluation information
	var FullURL = "FormsDebug.asp";
	DebugWin = window.open(FullURL,'debugwin',"width=630,height=500,left=215,top=80,center=no,resizable=yes,status=no,scrollbars=yes,toolbar=no,menubar=no,location=no");
	DebugWin.focus();
}

function ChangeItemVisibility(ItemID, ChangeMode) {
	if (document.getElementById(ItemID) != null) {
		document.getElementById(ItemID).style.visibility = ChangeMode;
	}
}

function testalert(message) {
	// emulates the alert function, but easier to find test alerts by searching for the function name
	alert(message);
}

function RTrim(str) {
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

function left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function CCS_Check_Popup() {
	var popupfailure = false;
	if (mainWin == null) {
		popupfailure = true;
	} else {
		if (mainWin.closed) {
			popupfailure = true;
		}
	}
	if (popupfailure == true) {
		alert("Pop-up windows appear to be disabled on your browser. This application will not function properly. Please click Help for more information.");
	}
}

function SelectListItem(listID,selID,preFix,selectFirst) {
	if (document.getElementById(listID) != null) {
		if (document.getElementById(listID).options.length != 0) {
			var isSelected = false;
			if (document.getElementById(selID).value != null) {
				for (var i = 0; i < document.getElementById(listID).options.length; i++) {
					if (preFix + document.getElementById(selID).value == document.getElementById(listID).options[i].id) {
						document.getElementById(listID).options[i].selected = true;
						isSelected = true;
					}
				}
			}
			if (selectFirst == true) {
				if (isSelected == false) {
					//select first item
					document.getElementById(listID).options[0].selected = true;
				}
			}
		}
	}
}

