/********************************************************
* former DHTMLapi.js functions                          *
*  Custom API for cross-platform object positioning     *
********************************************************/
var isCSS, isW3C, isIE4, isNN4, isIE6CSS;
var fudgeFactor = {top:-1, left:-1};	// global 'corrector' for IE, but doesn't hurt others

// fInitBrowserFlags - Initialize upon load
function fInitBrowserFlags() {
	var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
	if (!InternetExplorer) {
		alert("Please use Internet Explorer version 5.0 or higher.");
		top.close();
		return;
	}
	
	if (document.images) {
		isCSS = (document.body && document.body.style) ? true : false;
		isW3C = (isCSS && document.getElementById) ? true : false;
		isIE4 = (isCSS && document.all) ? true : false;
		isNN4 = (document.layers) ? true : false;
		isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
	}
	if (!isW3C && !isIE4 && !isIE6CSS) {
		alert("This browser does not support DHTML. Please upgrade to the latest version of Internet Explorer.");
	}
	document.onkeydown = mykeyhandler;
}
// Convert object name string or object ref into valid element reference.
function fGetRawObject(pObj) { 
	var theObj;
	if (typeof pObj == "string") {
		if (isW3C) {
			theObj = document.getElementById(pObj);
		} else if (isIE4) {
			theObj = document.all(pObj);
		} 
	} else {
		// pass through object reference
		theObj = pObj;
	}
	return theObj;
}
// Convert object name string or object ref into valid style reference
function fGetObject(pObj) {
	var theObj = fGetRawObject(pObj);
	if (theObj && isCSS) {
		theObj = theObj.style;
	}
	return theObj;
}
// Position object at x,y pixel coordinate
function fShiftTo(pObj, pX, pY) {
	var theObj = fGetObject(pObj);
	if (theObj) {
		if (isCSS) {
			// equalize incorrect numeric value type
			var units = (typeof theObj.left == "string") ? "px" : 0;
			theObj.left = pX + units;
			theObj.top = pY + units;
		}
	}
}
// Move object by x and/or y pixels
function fShiftBy(pObj, pDeltaX, pDeltaY) {
	alert("fShiftBy() not coded yet. (DHTML Ref pg 91)");
}
// Set z-order of object
function fSetZIndex(pObj, pZOrder) {
	alert("fSetZIndex() not coded yet. (DHTML Ref pg 91)");
}
// Set background color of object
function fSetBGColor(pObj, pColor) {
	var theObj = fGetObject(pObj);
	if (isCSS) {
		theObj.backgroundColor = pColor;
	}
}
// Set visibility of object to visible
function fShow(pObj) {
	var theObj = fGetObject(pObj);
	if (theObj) {
		theObj.visibility = "visible";
	} else { alert("object not found: " + pObj); }
}
// Set visibility of object to hidden
function fHide(pObj) {
	var theObj = fGetObject(pObj);
	if (theObj) {
		theObj.visibility = "hidden";
	}
}
// Get x coordinate of positionable object
function fGetObjectLeft(pObj) {
	var elem = fGetRawObject(pObj);
	var result = 0;
	if (document.defaultView) {
		var style = document.defaultView;
		var cssDecl = style.getComputedStyle(elem, "");
		result = cssDecl.getPropertyValue("left");
	} else if (elem.currentStyle) {
		result = elem.currentStyle.left;
	} else if (elem.style) {
		result = elem.style.left;
	}
	return parseInt(result,10);
}
// Get y coordinate of positionable object 
function fGetObjectTop(pObj) {
	var elem = fGetRawObject(pObj);
	var result = 0;
	if (document.defaultView) {
		var style = document.defaultView;
		var cssDecl = style.getComputedStyle(elem, "");
		result = cssDecl.getPropertyValue("top");
	} else if (elem.currentStyle) {
		result = elem.currentStyle.top;
	} else if (elem.style) {
		result = elem.style.top;
	}
	return parseInt(result,10);
}
// Get rendered width of element
function fGetObjectWidth(pObj) {
	var elem = fGetRawObject(pObj);
	var result = 0;
	if (elem.offsetWidth) {
		if (elem.scrollWidth && (elem.offsetWidth != elem.scrollWidth)) {
			result = elem.scrollWidth;
		} else {
			result = elem.offsetWidth;
		}
	} else if (elem.clip && elem.clip.width) {
		result = elem.clip.width;
	} else if (elem.style && elem.style.pixelWidth) {
		result = elem.style.pixelWidth;
	}
	return parseInt(result,10);
}
// Get rendered height of element
function fGetObjectHeight(pObj) {
	var elem = fGetRawObject(pObj);
	var result = 0;
	if (elem.offsetHeight) {
		result = elem.offsetHeight;
	} else if (elem.clip && elem.clip.height) {
		result = elem.clip.height;
	} else if (elem.style && elem.style.pixelHeight) {
		result = elem.style.pixelHeight;
	}
	return parseInt(result,10);
}
// Returns available content width space in browser window
function fGetInsideWindowWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (isIE6CSS) {
		// measure the html element's clientWidth
		return document.body.parentElement.clientWidth;
	} else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
	return 0;
}
// Returns available content height space in browser window
function fGetInsideWindowHeight() {
	if (window.innerHeight) {
		return window.innerHeight;
	} else if (isIE6CSS) {
		// measure the html element's clientHeight
		return document.body.parentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
	return 0;
}
// Returns how far the current page is scrolled down
function fGetScrollTop() {
	if (isIE4) {
		return document.body.scrollTop;
	}
	if (isW3C) {
		if (isIE6CSS) {
			return document.body.parentNode.scrollTop;
		} else {
			return window.scrollY;
		}
	}	
	return 0;
}
// Center a positionable element in the current window/frame, and show it.
function fCenterIt(pLayerName) {
	var obj = fGetRawObject(pLayerName);		// 'obj' is the positionable object
	
	// Setting fudgefactor every time; (**DHTML Ref. p 94 does only once when page loads**)
	if (fudgeFactor.top == -1) {
		if ((typeof obj.offsetTop == "number") && obj.offsetTop > 0) {
			fudgeFactor.top = obj.offsetTop;
			fudgeFactor.left = obj.offsetLeft;
		} else {
			fudgeFactor.top = 0;
			fudgeFactor.left = 0;
		}
		if (obj.offsetWidth && obj.scrollWidth) {
			if (obj.offsetWidth != obj.scrollWidth) {
				obj.style.width = obj.scrollWidth;
			}
		}
	}
	var x = Math.round((fGetInsideWindowWidth()/2) - (fGetObjectWidth(obj)/2));
	var y = Math.round((fGetInsideWindowHeight()/2) - (fGetObjectHeight(obj)/2));
	fShiftTo(obj, x - fudgeFactor.left, y - fudgeFactor.top + fGetScrollTop());
	fShow(obj);
}
function fAdjustIFrameSize(pID) {
	var myIframe = fGetRawObject(pID);
	if (myIframe) {
		if (myIframe.contentDocument && myIframe.contentDocument.body.offsetHeight) {
			// W3C DOM syntax for NN 6
			myIframe.style.height = myIframe.contentDocument.body.offsetHeight;
			myIframe.style.width = myIframe.contentDocument.body.offsetWidth;
		} else if (myIframe.Document && myIframe.Document.body.scrollHeight) {
			// IE DOM sytax
			myIframe.style.height = myIframe.Document.body.scrollHeight;
			myIframe.style.width = myIframe.Document.body.scrollWidth;
			//myIframe.style.height = myIframe.Document.body.clientHeight;
			//myIframe.style.width = myIframe.Document.body.clientWidth;
			//alert("new width,height="+myIframe.style.width+","+myIframe.style.height);
		}
	}
}
function fGetIFrameDocument(pID) {
	var vIframeDoc;
	var myIframe = fGetRawObject(pID);
	if (myIframe) {
		if (myIframe.contentDocument) {
			// W3C DOM syntax for NN 6
			vIframeDoc = myIframe.contentDocument;
		} else if (myIframe.Document) {
			// IE DOM syntax
			vIframeDoc = myIframe.Document;
		}
	}
	return vIframeDoc;
}

function fChangeText(pId, pNewTextStr) {
	var vObj = fGetRawObject(pId);
	if (vObj == null || typeof vObj == "undefined") {
		alert("object not found");
		return;
	}
	if (isIE4 && !isW3C) {
		vObj.innerHTML = pNewTextStr;
	}
	if (isW3C) {
		var vNewNode = document.createTextNode(pNewTextStr);	
		var vOldNode = vObj.firstChild;
		var vRemovedNode = vObj.replaceChild(vNewNode, vOldNode);
	}
}

function fChangeInnerHTML(pId, pNewStr) {
// This function differs from fChangeText in that pNewStr can contain html tags.
// Note that innerHTML is supported in IE and Netscape but not by the W3C DOM.
// (It would be a bit of a hassle to use createTextNode/replaceChild to create text containing html).
	var vObj = fGetRawObject(pId);
	if (!vObj) {
		alert("Object " + pId + " not found");
		return;
	}
	if (vObj.innerHTML) {
		vObj.innerHTML = (pNewStr != "") ? pNewStr : "&nbsp;";
	} else {
		alert("innerHTML property not supported by object " + pId);
	}
}

function mykeyhandler() {
    if (window.event && window.event.keyCode == 8 && 
		window.event.srcElement.tagName != "INPUT" && window.event.srcElement.tagName != "TEXTAREA") {
        // try to cancel the backspace
        window.event.cancelBubble = true;
        window.event.returnValue = false;
        return false;
    }
}


/************
* common.js *
*************/
function fGetArgs(pWindow) {
	if (pWindow == null) {
		vWin = window;
	} else {
		vWin = pWindow;
	}
	var args = new Object();
	var query = vWin.location.search.substring(1);	// Get query string
	var pairs = query.split("&");				// Break at &
	args["argcount"] = pairs.length;
	for (var i=0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');		// Look for "name=value"
		if (pos == -1) continue;				// If not found, skip.
		var argname = pairs[i].substring(0,pos);	// Extract the value.
		var value = pairs[i].substring(pos+1);	// 
		args[argname] = unescape(value);		// Store as a property.
	}
	return args;	// Return the object.
}

function fFormatTime(pTime) {
	if (pTime == null || pTime.length != 6) {
		return pTime;
	}
	var vHour = parseInt(pTime.substring(0,2),10);
	var vMin = parseInt(pTime.substring(2,4),10);
	var vAMPM = (vHour < 12) ? " AM" : " PM";
	if (vHour > 12) vHour -= 12;
	if (vHour == 0) vHour = 12;
	var vTimeStr = (vMin <= 0) ? (vHour + vAMPM) : (vHour + ":" + pTime.substr(2,2) + vAMPM);
	return vTimeStr;
}

// Returns 0 if pDate2 = pDate1; 1 if pDate2 > pDate1; -1 if pDate2 < pDate1
// pDate1,pDate2 must be m/d/yy or m/d/yyyy strings.
function fCompareMMDDYY(pDate1, pDate2) {
	var vDate1 = pDate1.split("/");
	var vDate2 = pDate2.split("/");
	var vDate1Year = parseInt(vDate1[2],10);
	var vDate2Year = parseInt(vDate2[2],10);
	if (vDate1Year < 100) {
		// add assumed century: 00 thru 69 = 2000 thru 2069; 70 thru 99 = 1970 thru 1999
		vDate1[2] = (vDate1Year >= 0 && vDate1Year < 70) ? vDate1Year+2000 : vDate1Year+1900;
	}
	if (vDate2Year < 100) {
		// add assumed century: 00 thru 69 = 2000 thru 2069; 70 thru 99 = 1970 thru 1999
		vDate2[2] = (vDate2Year >= 0 && vDate2Year < 70) ? vDate2Year+2000 : vDate2Year+1900;
	}
	for (var i=0; i < 2; i++) {
		vDate1[i] = parseInt(vDate1[i],10);
		vDate2[i] = parseInt(vDate2[i],10);
	}
	if (vDate2[2] < vDate1[2]) {
		return -1;
	}
	if (vDate2[2] > vDate1[2]) {
		return 1;
	}
	// ... year is same
	if (vDate2[0] < vDate1[0]) {
		return -1;
	}
	if (vDate2[0] > vDate1[0]) {
		return 1;
	}
	// ... year and month are equal
	if (vDate2[1] < vDate1[1]) {
		return -1;
	} else if (vDate2[1] > vDate1[1]) {
		return 1;
	} else {
		return 0;
	}
}

function fClearSelectOptions(pSelectObj) {
	// Clear old options
	for (var i=pSelectObj.options.length - 1; i >= 0; i--) {
		if (pSelectObj.remove) {
			pSelectObj.remove(i);	// IE 5+, NN6+
		} else {
			pSelectObj.options.remove(i); // IE 4
		}
	}	
}

function fDeleteOption(pSelectObj,pIndex) {
	if (pSelectObj.remove) {
		pSelectObj.remove(pIndex);	// IE 5+, NN6+
	} else {
		pSelectObj.options.remove(pIndex);// IE 4
	}
}

function fAddOption(object,text,value,sel) {
    var defaultSelected = false;
    var selected = sel;
	var newObj = new Option(text, value, defaultSelected, selected);
	if (isW3C) {
		if (isIE4) {				
			object.add(newObj); // IE5+
		} else {
			object.add(newObj,null); // NN6+
		}
	} else {
		object.options.add(newObj);	// IE4
	}
    //object.options[object.length-1].selected = false;
}

function fCompareArrayStr(a, b) {
	if (a[1]>b[1])return 1;
	else return -1;
}

function hrefDummy() {}


function fGetArrayData(pHiddenFieldValue, pLeaveQuotes, pDelim) {
	var vDelim = (pDelim) ? pDelim : String.fromCharCode(9);	// default is tab delimiter
	var vTempStr = new String(pHiddenFieldValue);
	if (!pLeaveQuotes || typeof pLeaveQuotes == "undefined") {
		vTempStr = vTempStr.replace(/\'/g, "&#39;");
		vTempStr = vTempStr.replace(/\"/g, "&quot;");
	}
	var vReturnVal = (vTempStr != "") ? vTempStr.split(vDelim) : new Array();
	return vReturnVal;
}


function fSetRadioSelection(pRadio, pValue) {
	for (var i=0; i < pRadio.length; i++) {
		if (pRadio[i].value == pValue) {
			pRadio[i].checked = true;
			return;
		}
	}
}

function fGetRadioSelection(pRadio) {
	for (var i=0; i < pRadio.length; i++) {
		if (pRadio[i].checked) {
			return pRadio[i].value;
		}
	}
	return "";
}

function fSetMenuSelection(pMenu, pValue) {
	//for (var i=0; i < pMenu.options.length; i++) {
	for (var i=0; i < pMenu.length; i++) {
		if (pMenu.options[i].value == pValue) {
			pMenu.options[i].selected = true;
			pMenu.selectedIndex = i;
			return;
		}
	}
}

function fGetMenuValue(pMenu) {
	return ((pMenu.selectedIndex < 0) ? "" : pMenu.options[pMenu.selectedIndex].value);
}

// Uses parseInt function to get return a base-10 number. Returns 0 if NaN.
function fParseInt(pStr) {
	var vNum;
	vNum = parseInt(pStr,10);
	if (isNaN(vNum)) {
		vNum = 0;
	}
	return vNum;
}


// Replace html single and double quote character entities with escaped quotes
function fTranslateQuotes(pValue) {
	var vStr = pValue;
	vStr = vStr.replace(/&#39;/g,"\'");
	vStr = vStr.replace(/&quot;/g,"\"");
	return vStr;
}

// strip of leading and trailing blanks and any tabs then return value
function fStripBlanks(pInputField) {
	vVal = new String(pInputField.value);
	vVal = vVal.replace(/^\s+/g,"");	// delete leading blanks
	vVal = vVal.replace(/\s+$/g,"");	// delete trailing blanks
	
	vVal = vVal.replace(/\t+/g," ");	// replace any tabs with single blank
	vVal = vVal.replace(/\v+/g," ");	// replace any vertical tabs with single blank
	pInputField.value = vVal;
	return vVal;
}

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}
function IsValidDate(pYear, pMonth, pDay) {
	var vDaysInMonth =     new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var vDaysInMonthLeap = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if (!LeapYear(pYear) && pDay > vDaysInMonth[pMonth-1]) {
		return ("Invalid date. There are only " + vDaysInMonth[pMonth-1] + " days in the selected month.");
	}
	if (LeapYear(pYear) && pDay > vDaysInMonthLeap[pMonth-1]) {
		return ("Invalid date. There are only " + vDaysInMonthLeap[pMonth-1] + " days in the selected month.");
	}
	return "";
}

function fSetWaitCursor(pDoc) {
	fChangeCursor(pDoc, "wait");
}
function fClearWaitCursor(pDoc) {
	fChangeCursor(pDoc, "auto");
}
function fChangeCursor(pDoc, pCursorName) {
	for (var i in pDoc.all) {
		if (pDoc.all[i].style) {
			pDoc.all[i].style.cursor = pCursorName;
		}
	}
}
function fIsWaitCursor(pDoc) {
	for (var i in pDoc.all) {
		if (pDoc.all[i].style) {
			if (pDoc.all[i].style.cursor == "wait") return true;
		}
	}
	return false;
} 

// Opens window to specified URL and width and height (if available screen size is less than 
// specified width/height window size is set to available size). The window will have scrollbars,
// status bar and be resizable. To set window to avail width/height use 0 or -1 for width and height.
function fWindowOpen(pURL, pName, pWidth, pHeight, pScroll, pOtherOptions) {
//alert(screen.availWidth + " x " + screen.availHeight);
	if (pURL.toLowerCase().indexOf("coming soon") == 0) {
		alert("Coming soon.");
		return;
	}
	var vScrWidth = screen.availWidth - 10;		// -10 for browser frame
	var vScrHeight = screen.availHeight - 50;	// - 50 for title bar and status bar
	if (pHeight > 0 && pHeight < vScrHeight) vScrHeight = pHeight;
	if (pWidth > 0 && pWidth < vScrWidth) vScrWidth = pWidth;
	var vScroll = (typeof pScroll == "undefined" || pScroll == true) ? "scrollbars,resizable," : "";
	var vWindowDecor = vScroll + "top=0,left=0,status,width="+vScrWidth+",height="+vScrHeight;
	if (pOtherOptions) vWindowDecor += pOtherOptions;	// pOther options must include leading comma (ex. ",menubar")
	var vWindow = window.open(pURL, pName, vWindowDecor);
	try {
		vWindow.focus();
	} catch (er) {
	}
	return vWindow;
}
