﻿String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/, "");
}
sfHover = function () {
  updatetime();  
  if (document.getElementById("Menu")) {
    var sfEls = document.getElementById("Menu").getElementsByTagName("LI");
    for (var i = 0; i < sfEls.length; i++) {
      sfEls[i].onmouseover = function () {
        this.className += " sfhover";
      }
      sfEls[i].onmouseout = function () {
        this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
      }
    }
  }
}
function hideselectbox() {
	if (document.all) {
		objectx = document.getElementsByTagName("SELECT");
		for (var i = 0; i < objectx.length; i++) {
			objectx[i].style.visibility = 'hidden';
		}
	}
}
function showselectbox() {
	if (document.all) {
		objectx = document.getElementsByTagName("SELECT");
		for (var i = 0; i < objectx.length; i++) {
			objectx[i].style.visibility = 'visible';
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
function doDelete(securityid) {
	var yesdelete = confirm('Are you sure you want to delete?');
	if (yesdelete) {
		if ((securityid == 11) || (securityid == 16)) { return true; }
		else {
			var strReason;
			strReason = "";

			while (sReason == "") {
				sReason = window.prompt("Please enter a reason for deleting the record:", "");

				if (sReason == null)
				{ sReason = ""; }

				sReason = sReason.trim();

				if (sReason == "") {
					window.alert("You must enter a reason for deleting the record.");
				}
			}
			// --------------------------------------------------------------------------------------------------------------------

			if (!(strReason == "") && !(strReason == null)) {
				var objReason;
				objReason = findObj('txtreason');
				objReason.value = strReason;
				return true;
			}
			else { return false; }
		} 
	}
	else { return false; }
}

function doDeletePrompt(securityid, prompt) {
	var bDelete = confirm(prompt);

	if (bDelete) {
		if ((securityid == 11) || (securityid == 16))
		{ return true; }
		else {
			var sReason;
			sReason = "";

			while (sReason == "") {
				sReason = window.prompt("Please enter a reason for deleting the record:", "");

				if (sReason == null)
				{ sReason = ""; }

				sReason = sReason.trim();

				if (sReason == "") {
					window.alert("You must enter a reason for deleting the record.");
				}
			}

			if (!(sReason == "") && !(sReason == null)) {
				var objReason;
				objReason = findObj('txtreason');
				objReason.value = sReason;
				return true;
			}
			else
			{ return false; }
		}
	}
	else
	{ return false; }
}

function findObj(n, d) {
	var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++) x = findObj(n, d.layers[i].document);
	if (!x && document.getElementById) x = document.getElementById(n); return x;
}

function findControl(elementType, elementId) {
	var elArry = document.body.getElementsByTagName(elementType);
	if (elArry.length > 0) {
		for (x = 0; x < elArry.length; x++) {
			if (elArry[x].id != null) {
				if (elArry[x].id.length >= elementId.length) {
					var startCnt = (elArry[x].id.length - elementId.length)
					if (elArry[x].id.indexOf(elementId, startCnt) > -1) {
						return elArry[x];
					}
				}
			}
		}
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
function StayPut() {
  timerID = setTimeout("StayPut()", 60000);
  PageMethods.StayLoggedIn('', StayPutCompleted);  
}
function StayPutCompleted(result) {
  if (result != 1) { window.location.href = '/login.aspx'; }
}
function updatetime() {
	var now = new Date();
	var nowhour = now.getHours();
	var nowmin = now.getMinutes();
	var nowsec = now.getSeconds();
	if (document.layers) { } else {
		if (window.txtclock) {
			txtclock.innerText = now.getDate() + "/" + (now.getMonth() + 1) + "/" + now.getYear() + " " + +nowhour + ((nowmin < 10) ? ":0" : ":") + nowmin + ((nowsec < 10) ? ":0" : ":") + nowsec + ' ';
		}
		timerID = setTimeout("updatetime()", 1000);
	}
}

function setMoney(moneyfield) {
	if (moneyfield) {
		if (moneyfield.value) {
			var expr = moneyfield.value.replace(',', '');
			if (isNaN(parseFloat(expr))) { moneyfield.value = "0.00"; expr = 0; }
			expr = parseFloat(expr);
			var str = "" + Math.round(eval(expr) * Math.pow(10, 2));
			while (str.length <= 2) { str = "0" + str; }
			var decpoint = str.length - 2;
			moneyfield.value = str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
		} 
	}
}

function setDate(datefield) {
	if (datefield) {
		if (datefield.value) {
			if (datefield.value != '') { checkdate(datefield); } 
		} 
	}
}

function checkdate(objName) {
	if (objName.value.toLowerCase() == 't') { var now = new Date(); objName.value = now.getDate() + "/" + (now.getMonth() + 1) + "/" + now.getFullYear(); }
	var datefield = objName;
	if (chkdate(objName) == false) {
		datefield.select();
		alert("That date is invalid. Please try again.");
		datefield.focus();
		return false;
	}
	else {
		return true;
	}
}

function NumFormat(expr, decplaces) {
	if (isNaN(parseInt(expr))) return 0;
	var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces))
	while (str.length <= decplaces) { str = "0" + str }
	var decpoint = str.length - decplaces
	return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
}

function resetContextMenu(toState) {
	var contextMenu = null;
	if (navigator.userAgent.match(/Opera/ig)) { }
	else if (navigator.userAgent.match(/Safari/ig)) { }
	else if (navigator.appName.match(/^microsoft/i)) {
		if (toState == true) { document.oncontextmenu = function() { return true; }; }
		else { document.oncontextmenu = function() { return false; }; }
	} else if (navigator.appName.match(/^netscape/i)) {
		if (toState == true) {
			oncontextmenu = function() { return true };
		} else {
			oncontextmenu = function() { return false };
		} 
	}
}

function chkdate(objName) {
	var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-", " ", "/", ".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	if (strDate.length < 1) {
		return true;
	}
	if (strDate.length < 6) {
		err = 1;
		return false;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}
	if (booFound == false) {
		if (strDate.length > 5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	if (strYear.length == 2) {
		intYear = parseInt(strYear, 10)
		if (intYear <= 36) {
			strYear = '20' + strYear;
		}
		else {
			strYear = '19' + strYear;
		}
	}
	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0; i < 12; i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i + 1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intYear < 1753) {
		err = 10;
		return false;
	}
	if (intYear > 9999) {
		err = 11;
		return false;
	}
	if (intMonth > 12 || intMonth < 1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}
	if (strDatestyle == "US") {
		datefield.value = strMonthArray[intMonth - 1] + " " + intday + " " + strYear;
	}
	else {
		datefield.value = intday + " " + strMonthArray[intMonth - 1] + " " + strYear;
	}
	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}
