
if (document.all && !document.getElementById) { document.getElementById = function(id) { return document.all[id] } }

function fnPrint() {
	var mac = (navigator.userAgent.indexOf("Mac") != -1); 	
	if (mac) { alert("To print this page press Command-P.") } else { self.print(); }
}

function fnCloseWindow() {
	window.open('','_parent','');
	window.close();
}

function fnMakePDF(strForm, intID) {
	var objWin = window.open(strForm+'.asp?pdf=1&id='+intID, 'makepdf', 'width=790,height=450,scrollbars=1,resizable=1');
	objWin.focus();
}

function fnIsAllIntegers(str) {
	var strIntegers = '1234567890';
	var charCurrent = '';
	for (i = 0; i < str.length; i++) {   
        charCurrent = str.charAt(i);
		// Return false if non-integer character is detected.
        if (strIntegers.indexOf(charCurrent) == -1) { return false; }
    }
	return true;
}

function fnValidatePhoneNumbers(bolRequired, num1, num2, num3) {
	// Get the total number of characers provided.
	var intTotal = 0;
	if (num1 != '') { intTotal += num1.length; }
	if (num2 != '') { intTotal += num2.length; }
	if (num3 != '') { intTotal += num3.length; }
	// Field is required and there aren't 10 numbers.
	if ((bolRequired) && (intTotal != 10)) { return 'missing'; }
	// Field is not required, but they tried to provide a value that doesn't have 10 numbers.
	if ((!bolRequired) && ((intTotal > 0) && (intTotal < 10))) { return 'missing'; }
	// The field is required or the field is not required and has ten characters.
	if ((bolRequired) || ((!bolRequired) && (intTotal == 10))) {
		// Prepare to determine if all characters in the phone number are integers.
		var strChars = num1 + num2 + num3;
		if (!fnIsAllIntegers(strChars)) { return 'invalid'; }
	}
}

function fourdigits(number)	{ return (number < 1000) ? number + 1900 : number; }
function fnWriteYear() {
	var now = new Date();
	document.write(fourdigits(now.getYear()));
}

function fnReplace(str, strSearch, strWith) {
    var strLength = str.length;
	var txtLength = strSearch.length;
    if ((strLength == 0) || (txtLength == 0)) { return str; }

    var i = str.indexOf(strSearch);
    if ((!i) && (strSearch != str.substring(0, txtLength))) { return str; }
    if (i == -1) { return str; }

    var newstr = str.substring(0, i) + strWith;

    if ((i + txtLength) < strLength) {
		newstr += fnReplace(str.substring(i + txtLength, strLength), strSearch, strWith);
	}

    return newstr;
}

function fnCheckLongMoney(objField) {
	var strValidChars = "0123456789.";
	var strChar;
	var blnValid = true, blnFormat = true;
	var strString = objField.value;
	for (i = 0; i < strString.length && blnValid == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) { blnValid = false; }
	}
	if (!blnValid) {
		// Invalid characters.
		alert("The value you entered, '" + strString + "' should contain only numbers [0 -9] and an optional decimal point.");
		objField.focus();
	}
	else {
		var arrString = strString.split('.');
		if (arrString.length > 2 || arrString.length == 0) { blnFormat = false; }
		else if (arrString.length == 2) {
			if (arrString[1].length > 4 || arrString[1].length == 0) { blnFormat = false; }
		}
		if (!blnFormat) {
			alert("The value you entered, '" + strString + "' should contain 4 or less decimal places.");
			objField.focus();
		}
	}
}


function fnCheckChars(objElem, objElemName) {
	var str 		= objElem.value;
	var strInvalid 	= "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
	for (var i = 0; i < str.length; i++) {
		if (strInvalid.indexOf(str.charAt(i)) != -1) {
			alert('The \'' + objElemName + '\' field contains invalid characters, which are not allowed.\n\nInvalid characters may include the following:\n\n' + strInvalid + '\n\nPlease remove any invalid characters and try again.');
			objElem.focus();
			return false;
		}
	}
	return true;
}

function fnFormatText(formatType) {
	var textRange = document.selection.createRange();
	var originalText = textRange.text;
	if (originalText != "") {
		switch (formatType) {
			case "italic":
				textRange.text = "<i>" + originalText + "</i>"
				break;
			case "bold":
				textRange.text = "<b>" + originalText + "</b>"
				break;
			case "underline":
				textRange.text = "<u>" + originalText + "</u>"
				break;
		}
	}
}

function fnCheckEmail(elem) {
	var emailStr 		= elem.value;
	/* The following variable tells the rest of the function whether or not to verify that the address ends in a two-letter country or well-known TLD. 1 means check it, 0 means don't. */
	var checkTLD 		= 1;
	/* The following is the list of known TLDs that an e-mail address must end with. */
	var knownDomsPat 	= /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	/* The following pattern is used to check if the entered e-mail address	fits the user@domain format. It also is used to separate the username from the domain. */
	var emailPat 		= /^(.+)@(.+)$/;
	/* The following string represents the pattern for matching all special characters. We don't want to allow special characters in the address. These characters include ( ) < > @ , ; : \ " . [ ] */
	var specialChars 	= "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	/* The following string represents the range of characters allowed in a username or domainname. It really states which chars aren't allowed.*/
	var validChars 		= "\[^\\s" + specialChars + "\]";
	/* The following pattern applies if the "user" is a quoted string (in which case, there are no rules about which characters are allowed and which aren't; anything goes). e.g. "jiminy cricket"@disney.com is a legal e-mail address. */
	var quotedUser 		= "(\"[^\"]*\")";
	/* The following pattern applies for domains that are IP addresses, rather than symbolic names. e.g. joe@[123.124.233.4] is a legal e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat 	= /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	/* The following string represents an atom (basically a series of non-special characters.) */
	var atom 			= validChars + '+';
	/* The following string represents one word in the typical username. For example, in john.doe@somewhere.com, john and doe are words. Basically, a word is either an atom or quoted string. */
	var word 			= "(" + atom + "|" + quotedUser + ")";
	// The following pattern describes the structure of the user
	var userPat 		= new RegExp("^" + word + "(\\." + word + ")*$");
	/* The following pattern describes the structure of a normal symbolic domain, as opposed to ipDomainPat, shown above. */
	var domainPat 		= new RegExp("^" + atom + "(\\." + atom +")*$");
	/* Finally, let's start trying to figure out if the supplied address is valid. */
	/* Begin with the coarse pattern to simply break up user@domain into different pieces that are easy to analyze. */
	var matchArray 		= emailStr.match(emailPat);
	if (matchArray == null) {
		/* Too many/few @'s or something; basically, this address doesn't even fit the general mould of a valid e-mail address. */
		alert("Email address seems incorrect (check @ and .'s)");
		elem.focus();
		return false;
	}
	var user 	= matchArray[1];
	var domain 	= matchArray[2];
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i = 0; i < user.length; i++) {
		if (user.charCodeAt(i) > 127) {
			alert("The username portion of the email address contains invalid characters.");
			elem.focus();
			return false;
		}
	}
	for (i = 0; i < domain.length; i++) {
		if (domain.charCodeAt(i) > 127) {
			alert("The domain name portion of the email address contains invalid characters.");
			elem.focus();
			return false;
		}
	}
	// See if "user" is valid 
	if (user.match(userPat) == null) {
		// user is not valid
		alert("The username portion of the email address doesn't seem to be valid.");
		elem.focus();
		return false;
	}
	/* if the e-mail address is at an IP address (as opposed to a symbolic host name) make sure the IP address is valid. */
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
		// this is an IP address
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i]>255) {
				alert("The destination IP address in the email address is invalid.");
				elem.focus();
				return false;
			}
		}
		return true;
	}
	// Domain is symbolic name.  Check if it's valid.
	var atomPat = new RegExp("^" + atom + "$");
	var domArr 	= domain.split(".");
	var len 	= domArr.length;
	for (i = 0; i < len; i++) {
		if (domArr[i].search(atomPat) == -1) {
			alert("The domain name portion of the email address does not seem to be valid.");
			elem.focus();
			return false;
		}
	}
	/* domain name seems valid, but now make sure that it ends in a known top-level domain (like com, edu, gov) or a two-letter word, representing country (uk, nl), and that there's a hostname preceding the domain or country. */
	if ((checkTLD) && (domArr[domArr.length-1].length != 2) && (domArr[domArr.length-1].search(knownDomsPat) == -1)) {
		alert("The email address must end in a well-known domain or two-letter " + "country.");
		elem.focus();
		return false;
	}
	// Make sure there's a host name preceding the domain.
	if (len < 2) {
		alert("The email address is missing its hostname portion.");
		elem.focus();
		return false;
	}
	// If we've gotten this far, everything's valid!
	return true;
}

function fnGetX(obj) {
	var intLeft = 0;
	if (obj.offsetParent) {
		while(1) {
			intLeft += obj.offsetLeft;
			if (!obj.offsetParent) { break; }
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) { intLeft += obj.x; }
	return intLeft;
}

function fnGetY(obj) {
	var intTop = 0;
	if (obj.offsetParent) {
		while(1) {
			intTop += obj.offsetTop;
			if (!obj.offsetParent) { break; }
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) { intTop += obj.y; }
	return intTop;
}

function fnIsInteger(strString) {
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) { return false };
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) { blnResult = false; }
	}
	return blnResult;
}

function fnIsFloat(strString, intPlaces) {
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) { return false };
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) { blnResult = false; }
	}
	if (blnResult) {
		var arrString = strString.split('.');
		if (arrString.length == 2) {
			if (arrString[1].length != intPlaces) { blnResult = false; }
		}
		else if (arrString.length > 2) { blnResult = false; }
	}
	return blnResult;
}

function fnIsTimeFormat(strString) {
	// Check to see it is not an empty string
	if (strString.length == 0) { return false; }
	var strChar;
	// Check for invalid charaters
	var strValidChars = "0123456789:";
	for (i = 0; i < strString.length; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) { return false; }
	}
	// Make sure there are only two colons (':') in the string
	var intCount = 0;
	for (i = 0; i < strString.length; i++) {
		strChar = strString.charAt(i);
		if (strChar == ':') { intCount++; }
	}
	if (intCount != 2) { return false; }
	// Make sure that the three values are of the proper length and are numbers
	strChar = strString.split(':');
	if ((strChar[0].length != 1) || (strChar[1].length != 2) || (strChar[2].length != 2)) { return false; }
	for (i = 0; i < strChar.length; i++) { if (!fnIsInteger(strChar[i])) { return false; } }
	return true;
}

function fnIsMoneyFormat(strString) {
	// Check to see it is not an empty string
	if (strString.length == 0) { return false; }
	var strChar;
	// Check for invalid charaters
	var strValidChars = "0123456789,.";
	for (i = 0; i < strString.length; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) { return false; }
	}
	// Make sure there is only one decimal ('.') in the string. Decimal is optional
	var intCount = 0;
	for (i = 0; i < strString.length; i++) {
		strChar = strString.charAt(i);
		if (strChar == '.') { intCount++; }
	}
	if (intCount > 1) { return false; }
	// Make sure that there are two numbers after the decimal, if provided.
	strChar = strString.split('.');
	if ((strChar.length > 1) && (strChar[1].length != 2)) { return false; }
	// Make sure that the part before and after decimal, if provided, are integers.
	// If a comma is in the first part of the amount entered, it is removed before testing.
	if (!fnIsInteger(strChar[0].replace(',',''))) 			{ return false; }
	if ((strChar.length > 1) && (!fnIsInteger(strChar[1]))) 	{ return false; }
	return true;
}

function IsDateFormat(strString) {
	// Check to see it is not an empty string
	if (strString.length == 0) { return false; }
	var strChar;
	// Check for invalid charaters
	var strValidChars = "0123456789/";
	for (i = 0; i < strString.length; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) { return false; }
	}
	// Make sure there are only two slashes ('/') in the string.
	var intCount = 0;
	for (i = 0; i < strString.length; i++) {
		strChar = strString.charAt(i);
		if (strChar == '/') { intCount++; }
	}
	if (intCount != 2) { return false; }
	// Make sure that there are one or two numbers for the month and day and four numbers for the year.
	strChar = strString.split('/');
	if ((strChar.length != 3) || (strChar[0].length == 0) || (strChar[0].length > 2) || (strChar[1].length == 0) || (strChar[1].length > 2) || (strChar[2].length != 4)) { return false; }
	// Make sure that the date parts are integers.
	if (!fnIsInteger(strChar[0].replace(',',''))) 			{ return false; }
	if ((!fnIsInteger(strChar[0])) || (!fnIsInteger(strChar[1])) || (!fnIsInteger(strChar[2]))) 	{ return false; }
	return true;
}

function fnAlertInteger(e) {
	alert("The value you entered, '" + e.value + "' should contain only numbers [ 0 through 9 ].");
	e.focus();
}

function fnAlertFloat(e, dec) {
	alert("The value you entered, '" + e.value + "' should contain only numbers [ 0 through 9 ], \nwith a single decimal point and " + dec + " decimal place\(s\).");
	e.focus();
}

function fnAlertTimeFormat(e) {
	alert("The value you entered, '" + e.value + "' must be formatted in this manner: h:mm:ss\n\nExample: 'Fifteen minutes and thirty seconds' should be entered as '0:15:30'.");
	e.focus();
}

function fnAlertMoneyFormat(e) {
	alert("The value you entered, '" + e.value + "' should contain only numbers [ 0 through 9 ], and comma(s), if necessary.\n\nYou may also use a decimal point provided there are only two numbers after it.\n\nAccepted Examples: '1,500' or '1500' or '1,500.00' or '1500.00'.");
	e.focus();
}

function fnAlertDateFormat(e) {
	alert("The value you entered, '" + e.value + "' should contain only numbers [ 0 through 9 ], and two slashes ['/'].\n\nDates must be formatted like this: mm/dd/yyyy\n\nAccepted Example: '01/15/2005'.");
	e.focus();
}
