//****************************************************************
//****************************************************************
//	COPYRIGHT 1998-99, Vertex Software
//****************************************************************
//****************************************************************

var gImageList = "";

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// PreloadImages
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function PreloadImages( imageDirectory, imageList ) {
	if (!imageList) imageList = gImageList;
	var imageName = imageList.split( "," );
	var index = 0;
	for (index=0; index<imageName.length; index++) {
		document.preloads = new Array();
		document.preloads[index] = new Image();
		document.preloads[index].src = imageDirectory + imageName[index] + "b.gif";
		}
	}


//****************************************************************
// SetSelectedImage
//****************************************************************
function SetSelectedImage(name) {
	if (!name) return; 
	// 02MAR2010 RFM - DEFECT #2911
	//var pictureObj = eval( "document." + name );
	var pictureObj = document.getElementsByName( name );
	pictureObj = (pictureObj.length > 0 ? pictureObj[0] : document.getElementById(name)); 
	if (pictureObj) {
		// HACK!!! Need to defer the execution for Netscape. Calling before the page is loaded results in teh image not being assigned.
		// 08APR2008 RFM - Created anonymous fucn to catch errors
		//window.setTimeout( "document." + name + ".src = document." + name + ".src.substring( 0, document." + name + ".src.length - 'c.gif'.length ) + 'c.gif'", 200 );
		window.setTimeout( function() {
			try {
				document[ name ].src = document[name].src.substring( 0, document[name].src.length - 'c.gif'.length ) + 'c.gif'
				}
			catch (error) {
				window.status = "SetSelectedImage: " + error.description;
				}
			}, 200 );
		}
	}


//===========================================================
// SetSelectedLink
//===========================================================
function SetSelectedLink( name ) {
	try {
		var link = document.getElementById( name );
		if (link) link.className += " Selected"; 
		}
	catch( error ) {
		window.status = ( "SetSelectedLink: " + error.description );
		}
	}



//****************************************************************
// rollover
//****************************************************************
function rollover(name) {
	try {
		var pictureObj = document.images[name];
		if (pictureObj) {
			var mouseOutSuffix = "a.gif";
			var mouseInSuffix = "b.gif";
			var sideImgSuffix = "c.gif";
			var openedImgSuffix = "d.gif";
			var defaultSideImg = "/img/default";
			var pictureSource = pictureObj.src || "";
			if (pictureSource.toLowerCase().indexOf( sideImgSuffix ) != -1 || pictureSource.toLowerCase().indexOf(openedImgSuffix) != -1) return;
			if (pictureSource.toLowerCase().indexOf( mouseOutSuffix ) != -1) {
				// ON MOUSEIN
				if (typeof pre != "undefined" && pre[name]) {
					pictureObj.src = pre[name].src;
					}
				else {
					pictureObj.src = pictureSource.substring( 0, pictureSource.length - mouseOutSuffix.length ) + mouseInSuffix;
					}
				if (document.centerimage){
					document.centerimage.src = "img/" + name + sideImgSuffix;
					}
				} 
			else {
				// ON MOUSEOUT
				pictureObj.src = pictureSource.substring( 0, pictureSource.length - mouseInSuffix.length ) + mouseOutSuffix;
				if (document.centerimage){
					document.centerimage.src = defaultSideImg + sideImgSuffix;
					}
				}
			}
		}
	catch (error) {
		window.status = ( "rollover: " + error.description );
		}
	}




//****************************************************************
//      CreateUniqueID
//****************************************************************
function CreateUniqueID( label ) {
        var todaydate = new Date();
        var ID = "";
        ID = ID + todaydate.getHours();
        ID = ID + todaydate.getMinutes();
        ID = ID + todaydate.getSeconds();
        // this function may be used in form elements
        if ( (label) && (label != "") ) {
                return label + "=" + escape(ID);
                }
        else {
                return escape(ID);
                }
        }




//===========================================================
// PopupWindow
//===========================================================
function PopupWindow( URL, name, height, width, resizeable, scrollbars, windowTop, windowLeft, menubars, toolbar, location) {
	try {
		resizeable = (resizeable ? ",resizable=yes" : ",resizable=no" );
		scrollbars = (scrollbars ? ",scrollbars=yes" : ",scrollbars=no" );
		menubars = (menubars ? ",menubars=yes" : ",menubars=no" );
		toolbar = (toolbar ? ",toolbar=yes" : ",toolbar=no" );
		location = (location ? ",location=yes" : ",location=no" );
		var windowprops = "height=" + height + ",width=" + width + resizeable + scrollbars + menubars + toolbar + location;	
		// only works on IE
		if (window.screenTop) {
			windowTop = (windowTop ? window.screenTop + windowTop :  window.screenTop + 10 );
			windowLeft = (windowLeft ?  window.screenLeft + windowLeft : window.screenLeft + 50 );
			windowprops = windowprops + ",top=" + windowTop + ",left=" + windowLeft;
			}
		var newWin = window.open(URL, name, windowprops);
		if (newWin) newWin.focus()
		}
	catch (error) {
		window.status = ( "PopupWindow: " + error.description );
		}
	}



//===========================================================
// CenterWindow
//===========================================================
function CenterWindow(width,height) {
	try {
		if (height > screen.availHeight) height = screen.availHeight - 100;
		if (width > screen.availWidth) width = screen.availWidth - 100;
		var top = (screen.availHeight - height)/2;
		var left = (screen.availWidth - width)/2;
		window.resizeTo( width, height );
		window.moveTo( left, top );
		}
	catch (error) {
		window.status = ( "CenterWindow: " + error.description  );
		}
	}


//===========================================================
// ResizeIFrameParent
// Call in content page of an Ifram to have enclosing ifram resize
// to accommodate all content.
// PARAMETERS:
// [direction]:string		If "h", resizes horizontally only. If "v", vertical only. Default is both.
// [oneLevelOnly]:boolean	If true, only resizes immediate parent IFrame - does NOT call parent's parent. Default is false;
//===========================================================
function ResizeIFrameParent( direction, oneLevelOnly, attempt ) {
	try {
		if (document.defaultView ) {
			document.parentWindow = document.defaultView;
			// 26MAY2010 RFM - DEFECT #3036 - fails on Firefox - catch error
			// document.parentWindow.window.document.readyState = "complete"
			try {
				document.parentWindow.window.document.readyState = "complete";
				}
			catch (error) {
				}
			}
		// 16APR2009 RFM - Hack added for Safari which does not reliably signal "loaded" - UNTESTED
		//if (!attempt) attempt = 0;
		//if (document.parentWindow && document.parentWindow.window.document.readyState == "loading" && attempt < 5) {
		//	if (!attempt) attempt = 1; 
		//	window.setTimeout( function() { ResizeIFrameParent(direction, oneLevelOnly, attempt+1), 1000 } );
		//	return;
		//	}
		// 23JUN2008 RFM - Added interactiev test because of issues with SiteTest TestPage loads.
		//if (document.parentWindow && (document.parentWindow.window.document.readyState == "complete" || document.parentWindow.window.document.readyState == "interactive")) {
		// 16APR2009 RFM - Added "loaded" for safari support
		//if (document.parentWindow && (document.parentWindow.window.document.readyState.match( /(complete|interactive)/) )) {
		if (document.parentWindow && (document.parentWindow.window.document.readyState.match( /(loaded|complete|interactive)/) )) {
			var parentFrame = document.parentWindow.window.frameElement;
			var kScrollBarWidth = 20;
			if (!parentFrame || document.body.scrollHeight == 0) return; 
			// 29MAY2010 RFM - Don't resize if running in Selenium
			if ( parentFrame.id && parentFrame.id.match(/selenium/gi)) return; 
			var frameHeight = document.body.scrollHeight;//Math.max(document.body.offsetHeight, document.body.scrollHeight);
			if (parentFrame.parentNode && !parentFrame.parentElement) parentFrame.parentElement = parentFrame.parentNode;
			// 12JAN2008 RFM - Causes frames to disappear on ARC
			//parentFrame.parentElement.style.height = frameHeight;
			// 12JAN2008 RFM - added "px" to satisfy Firefox
			if (!direction || direction.match(/v/gi)) parentFrame.style.height = frameHeight + "px";
			if (Math.abs( (parentFrame.style.pixelWidth ||parentFrame.clientWidth) - document.body.scrollWidth) > kScrollBarWidth) {
				// 27MAY2010 RFM - Added "px" for nonIE
				//var frameWidth = document.body.scrollWidth;
				var frameWidth = document.body.scrollWidth + "px";
				if (!direction || direction.match(/h/gi)) {
					parentFrame.parentElement.style.width = frameWidth;
					parentFrame.style.width = frameWidth;
					}
				}
			if (!oneLevelOnly && parentFrame.ownerDocument && typeof parentFrame.ownerDocument.parentWindow.ResizeIFrameParent == "function") {
				window.setTimeout( function() { parentFrame.ownerDocument.parentWindow.ResizeIFrameParent(direction) }, 10 );
				}
			}
		}
	catch( error ) {
		window.status = ( "ResizeIFrameParent: " + [error.description,document.location] );
		}	
	}


//===========================================================
// ClearDefaultText
//===========================================================
function ClearDefaultText( element ) {
	try {
		if (!element || element.cleared) return;
		element.cleared = 1;
		element.value = ""; 
		element.style.color = "";
		}
	catch (error) {
		window.status =( "ClearDefaultText: " + error.description  );
		}
	}



//===========================================================
// SetWindowOnLoad
//===========================================================
function SetWindowOnLoad( handlerFunction ) {
	try {
		var currentHandler = window.onload;
		if (currentHandler) {
			window.onload = function () {
				handlerFunction();
				currentHandler();
				}
			}
		else {
			window.onload = handlerFunction;
			}
		}
	catch (error) {
		window.status = ( "SetWindowOnLoad: " + error.description  );
		}
	}



//===========================================================
// DisablePageOnNavigate
// Provides feedback for user by disabling document on nav change
// and setting cursor to wait.
//===========================================================
function DisablePageOnNavigate( ) {
	try {
		if (document.body.onbeforeunload && document.body.onfocus) return;
		document.body.onbeforeunload  = function() { 
			document.body.style.cursor = "wait";
			// 10JAN2009 RFM - if run immediately, block clicks on links w/ submits!
			//document.body.disabled = true;		
			DisablePageOnNavigate.timerID = window.setTimeout(DisablePage,100);
			}
		document.body.onfocus = function() {
			try {
				if (DisablePageOnNavigate.timerID) {
					window.clearTimeout(  DisablePageOnNavigate.timerID );
					DisablePageOnNavigate.timerID = "";
					}
				EnablePage();
				// Need to re-enable any iframes that were disabled
				// 27MAR2008 RFM - Added since Firefox on mac does not define document.frames.
				if (!document.frames) return; 
				for (var item=0; item<document.frames.length; item++) {
					// 11APR2008 RFM - trap access denied errors
					try {
						var frameDocument = document.frames[item].document;
						if (!frameDocument || !frameDocument.body) continue;
						frameDocument.body.style.cursor = document.body.style.cursor;
						frameDocument.body.disabled = false;
						}
					catch (error) {
						// ignore AccessDenied errors due to cross-frame scripting
						}
					}
				}
			catch (error) {
				window.status = ( "document.body.onfocus: " + error.description  );
				}
			}
		}
	catch (error) {
		window.status = ( "DisablePageOnNavigate: " + error.description  );
		}
	}


//===========================================================
// DisablePage
// http://malsup.com/jquery/block/#options
//===========================================================
function DisablePage( ) {
	try {
		document.body.style.cursor = "wait";
		document.body.disabled = true;
		// 20APR2011 RFM - Added for support of non-IE browsers
		if (typeof jQuery != "undefined" && $.blockUI && !$.browser.msie) { 
			$.blockUI({ fadeIn: 0, message:"", overlayCSS: { opacity:0.05 } });
			}
		}
	catch (error) {
		alert( "DisablePage: " + error.description  );
		}
	}


//===========================================================
// EnablePage
//===========================================================
function EnablePage( ) {
	try {
		document.body.style.cursor = "default"; 
		document.body.disabled = false; 
		// 20APR2011 RFM - Added for support of non-IE browsers
		if (typeof jQuery != "undefined" && $.blockUI) $.unblockUI({ fadeOut: 0 });
		// 22MAR2011 RFM - Need to enable iframes as well
		if (!document.frames) return;
		for (var item=0; item<document.frames.length; item++) {
			// 11APR2008 RFM - trap access denied errors
			try {
				var frameDocument = document.frames[item].document;
				if (!frameDocument || !frameDocument.body) continue;
				frameDocument.body.style.cursor = "default";
				frameDocument.body.disabled = false;
				// 20APR2011 RFM - Added for support of non-IE browsers
				if (typeof jQuery != "undefined" && $.blockUI && !$.browser.msie) $(frameDocument.body).unblockUI({ fadeOut: 0 });
				}
			catch (error) {
				// ignore AccessDenied errors due to cross-frame scripting
				}
					}
		}
	catch (error) {
		alert( "EnablePage: " + error.description  );
		}
	}


//===========================================================
// Require
// Used in a file or a function to indicate another file which must
// be included in order for the file or function to be used. Write an
// error message to the page and displays a Javascript alert if the 
// required file has not been included.
// PARAMETERS:
//	includedFile:string		Path to included file.
//	procedure:string		Name of procedure in included file that is required.
// EXAMPLE:
//		Require( "/global/scripts/Files.asp", "ReadFile" );
//===========================================================
function Require( includeFile, procedure  ) {
	if ( eval( "typeof " + procedure ) == "undefined" ) {
		var message = "You must include " + includeFile + " in order to call " + procedure ;
		alert( message );
		return true;
		}
	return false;
	}




//===========================================================
// AttachBehaviors
// For each element with class behaviorName, calls the
// behavior's constructor for that element. Assumes that
// a class exists having behaviorName which takes an element
// as a constructor.
//===========================================================
function AttachBehaviors( behaviorName, manual ) {
	try {
		// 18FEB2009 RFM - added to allow for default call of AttachBehaviors
		// in behavior include to be disabled by calling ManuallyAttachBehaviors.
		// Need to pass manual parameter as true when calling on own
		if (AttachBehaviors.manual && !manual) return; 
		if (Require("/global/scripts/js/jsquery.js", "AttachBehaviors")) return; 
		$(function() {
			BindBehavior( behaviorName );
			} );
		}
	catch (error) {
		alert( "AttachBehaviors: " + error.description, false, true );
		}
	}


//===========================================================
// ManuallyAttachBehaviors
// Call this function BEFORE any behavior scripts are loaded
// to disable the include from automatically calling AttachBehaviors.
// This is require in certain cases when AttachBehaviors needs
// to be manually called after another initilization process
//===========================================================
function ManuallyAttachBehaviors( ) {
	AttachBehaviors.manual = true;
	}


//-----------------------------------------------------------
// BindBehavior
//-----------------------------------------------------------
function BindBehavior( behaviorName ) {
	try {
		var behaviorFunction = eval( "typeof " + behaviorName ) == "function" ? eval( behaviorName ) : "";
		var classNamePrefix = "js_";
		if (typeof behaviorFunction != "function") {
			alert( "Uknown behavior specified: " + behaviorName );
			return;
			} 
		// 30MAR2009 RFM - use to replace .htc with js behaviors
		var selectors = GetCssBehaviorSelector(behaviorName);
		selectors.push( "." + classNamePrefix + behaviorName );
		$( selectors.join(",") ).each( function() {
			// 02FEB2009 - only create one behavior for a given elementid!
			// 24FEB2009 RFM - Need to add random since times will not be unique in tight loop
			if (!this.id) this.id = "behaviorName" + new Date().getTime() + parseInt(Math.random()*100000)
			var elementID = this.id;
			if (!behaviorFunction.instances) behaviorFunction.instances = {};
			if (behaviorFunction.instances[this.id]) {
				return;
				}
			behaviorFunction.instances[this.id] = new behaviorFunction( this );
			} );
		}
	catch (error) {
		alert( "BindBehavior: " + error.description  );
		}
	}


//-----------------------------------------------------------
// GetCssBehaviorSelector
// Locates any IE behaviors that have been rewritten to use JS behaviors 
// And returns their selectors. Remove URL from behavior and replace 
// with name of JS behavior to use:
// EXAMPLE:
//		behavior: url("/global/scripts/HTCLibrary/rollover.htc");
//	=>  behavior: js_rollover;
//-----------------------------------------------------------
function GetCssBehaviorSelector( behaviorName ) {
	var selectors = [];
	try {
		for (var styleSheetIndex=0; styleSheetIndex<document.styleSheets.length; styleSheetIndex++) {
			var styleSheet = document.styleSheets[styleSheetIndex];
			var rules = [];
			// 13AR2009 RFM - Accessing a foregin css can result in a security vilation so dont' throw error
			try {
				rules = styleSheet.rules || styleSheet.cssRules || [];
				}
			catch (error) {
				rules = [];
				}
			for (var ruleIndex=0; ruleIndex<rules.length; ruleIndex++) {
				var rule = rules[ruleIndex];
				if (rule.style && rule.style.behavior && rule.style.behavior == "js_" + behaviorName) {
					selectors.push( rule.selectorText);
					}
				}
			}
		}
	catch (error) {
		alert( "GetCssBehaviorSelector: " + error.description  );
		}
	return selectors;
	}


//===========================================================
// KeyPressFilter
// Assign to onkeypress to ensure that only charcaters
// matching regEx are entered.
// EXAMPLE:
// onkeypress="KeyPressFilter(event,/^[0-9.]*$/,this)"
// allows only the numbers 0-9 and decimal.
//===========================================================
function KeyPressFilter( event, regEx, element ) {
	try {
		// Allow nav controls on Firefox and all control keycode
		// 14Jul2011  RFM - DEFECT #3516
		//if (event.charCode == 0 || event.keyCode < " ".charCodeAt(0)) return true;
		if (event.charCode == 0 || (event.keyCode > 0 && event.keyCode < " ".charCodeAt(0))) return true;
		if ( document.selection && document.selection.createRange().text == element.value) element.value = "";
		var value = element.value + String.fromCharCode( event.keyCode || event.charCode );
		var isMatch = value.match( regEx ) ? true : false;
		event.returnValue = isMatch;
		event.cancelBubble = !isMatch; 
		if (event.preventDefault && !isMatch) event.preventDefault();
		}
	catch (error) {
		alert( "KeyPressFilter: " + error.description  );
		}
	return event.returnValue;
	}
