/* /library/js/global.js -*/

// GLOBAL VARIABLES ---------------------------------------------------------------------------------------

var divMain = null;
var divHide = null;
var strFocusOnClose = "";
var blnMac = (navigator.userAgent.toLowerCase().indexOf("mac") != -1) ? true : false;
var blnNs4 = (document.layers) ? true : false;
var blnIe4 = (document.all && !document.getElementById) ? true : false;
var blnIe5 = (document.all && document.getElementById) ? true : false;
var blnNs6 = (!document.all && document.getElementById) ? true : false;

// BROWSER DEPENDENCE CORE FUNCTIONS -----------------------------------------------------------------------

function ftnGetObjectString(strObjectId) {
/*
	Purpose	:	Used to return a string that represents the path to an object in the DOM
				dependent on the browser being used.
	Effects	:	None
	Inputs	:	strObjectId - String - the id of an object
	Returns	:	String - the path to the object in the DOM
*/
	if(blnNs4) return "document." + strObjectId;
	if(blnIe4) return "document.all." + strObjectId;
	if(blnIe5 || blnNs6){
		if (blnMac && blnIe5)
			return "document.all['" + strObjectId + "']";
		else
			return "document.getElementById('" + strObjectId + "')";
	}
}

function ftnGetObject(strObjectId) {
/*
	Purpose	:	Used to return an object in the DOM.
	Effects	:	None
	Inputs	:	strObjectId - String - the id of an object
	Returns	:	Object - the object in the DOM
*/
	var strEval = ftnGetObjectString(strObjectId);
	return eval(strEval);
}

// VALIDATION FUNCTIONS -----------------------------------------------------------------------------

function ftnCheckNumeric(varValue)
{
/*
	Purpose	:	Checks that a value is numeric.
	Effects	:	None.
	Inputs	:	varValue - Variant - the value to check.
	Returns	:	Boolean - True if the value was numeric, False otherwise
	Comments:	None.
*/
	if(varValue.length < 1) {
		return false;
	} else if(isNaN(varValue)) {
		return false;
	} else {
		return true;
	}
}

function ftnCheckMandatory(strValue) {
	/*	
	 *	Purpose	:	Checks that a value exists, after stripping off leading and trailing white space.
	 *				Also checks for a single period, commonly used in an attempt to fool validation 
	 *				on our sites.
	 *	Effects	:	None.
	 *	Inputs	:	strValue - String - the value to check.
	 *	Returns	:	Boolean - True if the value contains more than white space, False otherwise
	 *	Comments:	None.
	 */
	
	// Strip leading and trailing white space
	strValue = strValue.replace(/^\s*(\b.*\b|)\s*$/, "$1");

	if (strValue == '' || strValue == '.') {
		return false;
	}
	return true;
}

function ftnCheckPassword(strValue) {
	/*	
	 *	Purpose	:	Checks that a value exists, after stripping off leading and trailing white space.
	 *				Also checks for a minimum length of 6 alphanumeric characters
	 *	Effects	:	None.
	 *	Inputs	:	strValue - String - the value to check.
	 *	Returns	:	Boolean - True if the value contains more than white space, False otherwise
	 *	Comments:	None.
	 */
	
	// Strip leading and trailing white space
	strValue = strValue.replace(/^\s*(\b.*\b|)\s*$/, "$1");

	if (strValue == '' || strValue == '.' || strValue.length < 6) {
		return false;
	}
	return true;
}


function ftnCheckDate(strDate)
{
/*
	Purpose	:	Checks the validity of a date string.
	Effects	:	None.
	Inputs	:	strDate - String - the date string to check in the format yyyy-mm-dd.
	Returns	:	Boolean - true if the date was valid, false otherwise
	Comments:	None.
*/

	var blnError = 0;
	var intYear, blnLeapYear;
	var aiDaysInMonth = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);

	if(strDate.length != 0) {
		if(strDate.length > 10){
			blnError = true;
		}
		else {
			if(strDate.substr(4,1) != "-" || strDate.substr(7,1) != "-") {
				blnError = true;
			} 
			else {
				if(!ftnCheckNumeric(strDate.substr(0,4)) || !ftnCheckNumeric(strDate.substr(5,2)) || !ftnCheckNumeric(strDate.substr(8,2))) {
					blnError = true;
				} 
				else {
					// all elements are numeric so check for leap year
					intYear = strDate.substr(0,4);
					blnLeapYear = ftnCheckLeapYear(intYear);
					if(blnLeapYear) {
						aiDaysInMonth[2] = 29;
					}
					// check month between 1 and 12
					if(parseInt(strDate.substr(5,2),10) < 1 || parseInt(strDate.substr(5,2),10) > 12) {
						blnError = true;
					} 
					else {
						// check days in month correct
						if(parseInt(strDate.substr(8,2),10) < 1 || strDate.substr(8,2) > aiDaysInMonth[parseInt(strDate.substr(5,2),10)]) {
							blnError = true;
						}
					}
				}
			}
		}
		return(!blnError);
	}
}

function ftnCheckDateBetween(dteDateToCheck, dteStartDate, dteEndDate)
{
/*
	Purpose	:	Checks that a date falls in a date range.
	Effects	:	None.
	Inputs	:	dteDateToCheck - Date - the date string to check.
				dteStartDate - Date - the start of the date range.
				dteEndDate - Date - the end of the date range.
	Returns	:	Boolean - True if the date falls in the range, False otherwise
	Comments:	Use ftnStringToDate() to convert an ISO formatted date string into a Date object.
*/
	// check valid dates has been given
	if(isNaN(Date.parse(dteDateToCheck))) { alert("ftnCheckDateBetween() - dteDateToCheck parameter is invalid."); return false; }
	if(isNaN(Date.parse(dteStartDate))) { alert("ftnCheckDateBetween() - dteStartDate parameter is invalid."); return false; }
	if(isNaN(Date.parse(dteEndDate))) { alert("ftnCheckDateBetween() - dteEndDate parameter is invalid."); return false; }
	
	// check date range is valid
	if(!ftnCheckDateRange(dteStartDate, dteEndDate)) { alert("ftnCheckDateBetween() - The specified date range is invalid."); return false; }
	
	// check date is in the range
	if(dteDateToCheck >= dteStartDate && dteDateToCheck <= dteEndDate) {
		return true;
	} else {
		return false;
	}
}

function ftnCheckDateRange(dteStartDate, dteEndDate)
{
/*
	Purpose	:	Checks that two dates specify a valid date range.
	Effects	:	None.
	Inputs	:	strStartDate - Date - the start of the date range.
				strEndDate - Date - the end of the date range.
	Returns	:	Boolean - True if the dates specify a valid range, False otherwise
	Comments:	Use ftnStringToDate() to convert an ISO formatted date string into a Date object.
*/
	if(isNaN(Date.parse(dteStartDate))) { alert("ftnCheckDateRange() - dteStartDate parameter is invalid."); return false; }
	if(isNaN(Date.parse(dteEndDate))) { alert("ftnCheckDateRange() - dteEndDate parameter is invalid."); return false; }
	
	if(dteStartDate <= dteEndDate) {
		return true;
	} else {
		return false;
	}
}

function ftnCheckLeapYear(intYear)
{
/*
	Purpose	:	Determines if the year is a leap year.
	Effects	:	None.
	Inputs	:	intYear - Integer - the year to check
	Returns	:	Boolean - true if intYear is a leap year, false otherwise
	Comments:	None.
*/
	return (((intYear % 4 == 0) && (intYear % 100 != 0)) || (intYear % 400 == 0)) ? 1 : 0;
}

function ftnCheckEmailAddress(strEMail)
{
/*
	Purpose	:	Checks to see if the string is a valid email address.
	Effects	:	None.
	Inputs	:	strEMail - String - the string to check.
	Returns	:	Boolean - true if the string was valid, false otherwise
	Comments:	None.
*/
	var strAt = "@";
	var strDot = ".";
	var intLastAt = strEMail.indexOf(strAt);
	var intLength = strEMail.length;
	
	if(strEMail.indexOf(strAt) == -1) {
		return false;
	}
	if(strEMail.indexOf(strAt) == -1 || strEMail.indexOf(strAt) == 0 || strEMail.indexOf(strAt) == intLength) {
		return false;
	}
	if(strEMail.indexOf(strDot) == -1 || strEMail.indexOf(strDot) == 0 || strEMail.indexOf(strDot) == intLength) {
		return false;
	}
	if(strEMail.indexOf(strAt,(intLastAt + 1)) != -1) {
		return false;
	}
	if(strEMail.substring(intLastAt - 1,intLastAt) == strDot || strEMail.substring(intLastAt + 1,intLastAt + 2) == strDot) {
		return false;
	}
	if(strEMail.indexOf(strDot,(intLastAt + 2)) == -1) {
		return false;
	}
	if(strEMail.indexOf(" ") != -1) {
		return false;
	}
 	return true;		
}

function ftnCheckOnlyReservedWords(strText)
{
/*
	Purpose	:	Checks to see if the string only contains SQL Server Full-text reserved words.
	Effects	:	None.
	Inputs	:	strText - String - the string to check.
	Returns	:	Boolean - true if the string did contain only reserved words, false otherwise
	Comments:	None.
*/
	var arrFT_RESERVED_WORDS = new Array("about", "1", "after", "2", "all", "also", "3", "an", "4", "and", "5", "another", "6", "any", "7", "are", "8", "as", "9", "at", "0", "be", "$", "because", "been", "before", "being", "between", "both", "but", "by", "came", "can", "come", "could", "did", "do", "each", "for", "from", "get", "got", "has", "had", "he", "have", "her", "here", "him", "himself", "his", "how", "if", "in", "into", "is", "it", "like", "make", "many", "me", "might", "more", "most", "much", "must", "my", "never", "now", "of", "on", "only", "or", "other", "our", "out", "over", "said", "same", "see", "should", "since", "some", "still", "such", "take", "than", "that", "the", "their", "them", "then", "there", "these", "they", "this", "those", "through", "to", "too", "under", "up", "very", "was", "way", "we", "well", "were", "what", "where", "which", "while", "who", "with", "would", "you", "your", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "'", "\"", "\"\"", "(", ")", "()");
	var arrWords = strText.split(" ");
	var intWordsLoop, intReservedWordsLoop;
	var strWord;
	var blnResult = null;
	
	// loop around the words specified
	for(intWordsLoop = 0; intWordsLoop < arrWords.length; intWordsLoop++) {
		// if the word is not a reserved word
		if(blnResult == false) { break; }

		strWord = arrWords[intWordsLoop];
		blnResult = false;
		
		// loop through the reserved words to see if strWord is present
		for(intReservedWordsLoop = 0; intReservedWordsLoop < arrFT_RESERVED_WORDS.length; intReservedWordsLoop++) {
			if(arrFT_RESERVED_WORDS[intReservedWordsLoop] == strWord) {
				blnResult = true;
				break;
			}
		}
	}
	return blnResult;
}

function ftnCheckMoney(strText)
{
/*
	Purpose	:	Checks to see if the string is a monetary value.
	Effects	:	None.
	Inputs	:	strText - String - the string to check.
	Returns	:	Boolean - true if the string if a monetary value, false otherwise
	Comments:	None.
*/
	if ((strText > 922337203685477.5807) || isNaN(strText) || (strText < 0.00)) {
		return false;
	} else {
		return true;
	}
}

// CONVERSION FUNCTIONS --------------------------------------------------------------------------------------

function ftnStringToDate(strString)
{
/*
	Purpose	:	Returns a variant of type Date from a string in ISO date format, i.e. yyyy-mm-dd
	Effects	:	None.
	Inputs	:	strString - String - the date string to convert.
	Returns	:	Variant of type Date or Null
	Comments:	WILL error if string is not in ISO date format. Use ftnCheckDate to validate 1st.
*/
	var intYear, intMonth, intDay;

	if(!ftnCheckDate(strString)) { alert("ftnStringToDate() - strString parameter is invalid."); return false; }

	if(strString.length <= 0) {
		return null;
	} else {
		intYear = parseInt(strString.substr(0,4),10);
		intMonth = parseInt(strString.substr(5,2),10);
		intDay = parseInt(strString.substr(8,2),10);
		return new Date(intYear, intMonth, intDay);
    }
}

function ftnDateToString(dteDate)
{
/*
	Purpose	:	Returns a string in ISO format of a date.
	Effects	:	None.
	Inputs	:	dteDate - Date - the date to convert.
	Returns	:	String - the date formatted as an ISO date.
	Comments:	None.
*/
	var strYear, strMonth, strDay;

	if(isNaN(Date.parse(dteDate))) { alert("ftnDateToString() - dteDate parameter is invalid."); return false; }

	strYear = dteDate.getFullYear().toString();
	strMonth = dteDate.getMonth().toString();
	strDay = dteDate.getDate().toString();
	
	if(strMonth.length == 1) { strMonth = "0" + strMonth; }
	if(strDay.length == 1) { strDay = "0" + strDay; }
	
	return strYear + "-" + strMonth + "-" + strDay;    
}


// IMAGE ROLL-OVER FUNCTIONS ------------------------------------------------------------------

function subPreloadImage(strImageVar, strImageSrc) {
/*
	Purpose	:	Used to an preload image.
	Effects	:	None
	Inputs	:	strImageVar - String - id of the image tag
				strImageSrc - String - URL of the image
	Returns	:	None
*/
	if (document.images) {
		eval(strImageVar + ' = new Image()');
		eval(strImageVar + '.src = "' + strImageSrc + '"');
	}
}

function subInitImgRollovers()
{
/*
	Purpose	:	Finds all image or "input type=image" tags with an "oversrc" attribute, preloads this image and 
				wires-up the onmouseover, onmouseout events.
	Effects	:	None.
	Inputs	:	None.
	Returns	:	None.
*/
	var objImages = document.images;
	var objInputTags = document.getElementsByTagName("INPUT");
	var objImage;
	var strOverSrc, strSrc, strOversrcVar, strSrcVar;
	var intCounter = 0;
	
	// loop through IMG tag collection
	for(i=0; i<objImages.length; i++) {
		objImage = objImages[i];
		strOverSrc = objImage.oversrc;
		if(strOverSrc != null) {
			if(strOverSrc != "undefined") {
				strSrc = objImage.src;
				// create new src and oversrc variable names
				strSrcVar = "src_" + intCounter;
				strOversrcVar = "oversrc_" + intCounter;
				intCounter++;
				subCreateRollover(objImage, strSrcVar, strSrc, strOversrcVar, strOverSrc);
			}
		}
	}
	
	// loop through INPUT TYPE=IMAGE collection
	for(i=0; i<objInputTags.length; i++) {
		objImage = objInputTags[i];
		if(objImage.type == "image") {
			strOverSrc = objImage.oversrc;
			if(strOverSrc != null) {
				if(strOverSrc != "undefined") {
					strSrc = objImage.src;
					// create new src and oversrc variable names
					strSrcVar = "src_" + intCounter;
					strOversrcVar = "oversrc_" + intCounter;
					intCounter++;
					subCreateRollover(objImage, strSrcVar, strSrc, strOversrcVar, strOverSrc);
				}
			}
		}
	}
}

function subCreateRollover(objImage, strSrcVar, strSrc, strOversrcVar, strOverSrc)
{
/*
	Purpose	:	Sets up the rollover on the specified tag.
	Effects	:	Creates new attributes on the specified tag.  Wires up the omouseover and onmouseout events.
	Inputs	:	objImage - Object - the IMG or INPUT TYPE=image tag.
				strSrcVar - String - the name of the variable to hold the preloaded original src image.
				strSrc - String - the URL of the original image.
				strOversrcVar - String - the name of the variable to hold the preloaded over src image.
				strOverSrc - String - the URL of the over image.
	Returns	:	None.
*/
	// store src and oversrc variable in an attribute on the image
	objImage.src_var = strSrcVar;
	objImage.oversrc_var = strOversrcVar;
	// preload images
	subPreloadImage(strSrcVar, strSrc);
	subPreloadImage(strOversrcVar, strOverSrc);
	// wire up onmouseover and onmouseout events
	objImage.onmouseover = subRollOver;
	objImage.onmouseout = subRollOut;
}

function subRollOver()
{
/*
	Purpose	:	onmouseover event handler for image rollovers.
	Effects	:	Changes the src attribute of the tag that fired the event.
	Inputs	:	None.
	Returns	:	None.
*/
	var objImageTag = window.event.srcElement;
	var strNewImageVar = objImageTag.oversrc_var;
	var objNewImage = eval(strNewImageVar);
	objImageTag.src = objNewImage.src;
}

function subRollOut()
{
/*
	Purpose	:	onmouseout event handler for image rollovers.
	Effects	:	Changes the src attribute of the tag that fired the event.
	Inputs	:	None.
	Returns	:	None.
*/
	var objImageTag = window.event.srcElement;
	var strNewImageVar = objImageTag.src_var;
	var objNewImage = eval(strNewImageVar);
	objImageTag.src = objNewImage.src;
}
// ---
function ftnShowHide(obj) {
	if (ftnGetObject(obj).style.display == 'none') {
		ftnGetObject(obj).style.display = 'block';
		ftnGetObject(obj).blur;
	} else {
		ftnGetObject(obj).style.display = 'none';
		ftnGetObject(obj).blur;
	}
}

function ftnSwitchImg(imgObj) {
	var c = ftnGetObject(imgObj).src.substring((ftnGetObject(imgObj).src.length - 5), ftnGetObject(imgObj).src.length);
	var i = ftnGetObject(imgObj).src.substring(0, (ftnGetObject(imgObj).src.length - 5))
	if (c == 'D.gif') {
		ftnGetObject(imgObj).src = i + "O.gif";
	} else {
		ftnGetObject(imgObj).src = i + "D.gif";
	}
	//alert(ftnGetObject(imgObj).src.substring((ftnGetObject(imgObj).src.length - 5), ftnGetObject(imgObj).src.length));
}

function ftnPrintPage() {
	window.print();
}

function ftnOpenWin(loc, name, attribs) {
	window.open(loc, name, attribs);
}

var maxLgth = 4000
function ftnTextAreaCount(obj) {
	ftnGetObject("chars_left").innerHTML = (maxLgth - obj.value.length);
	obj.blur();
	obj.focus();
}

function ftnConfirm(msg) {
	ftnConfirm = confirm(msg);
}

function ftnCheckValidPhoneNum(sText) {
   var ValidChars = "0123456789.-";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
         }
      } return IsNumber;
   
   }
//====================================================================