
function showShade(){

	document.getElementById('myStyle').style.display = 'block'; 
	
	resizeShade();

	//If tthis is an IE6 browser we need to hide select boxes.
	try {document.getElementById('ddlPage').style.visibility='hidden'} catch(e) {}
	try {document.getElementById('ddlOnPage').style.visibility='hidden'} catch(e) {}
	
}

function hideShade(){
	document.getElementById('myStyle').style.display = 'none'; 
	//If tthis is an IE6 browser we need to re-show select boxes.
	try {document.getElementById('ddlPage').style.visibility='visible'} catch(e) {}
	try {document.getElementById('ddlOnPage').style.visibility='visible'} catch(e) {}
}

function resizeShade(){ 
	if (document.getElementById('myStyle')) {
		if (document.getElementById('myStyle').style.display == 'block') {
			var pageWidth = f_clientWidth();
			var pageHeight = f_documentHeight(); //Using document height instead of client height so it covers the full page in Chrome & IE8. (The ClientHeigh + TopScroll method only works property in IE6)
			var topScroll = f_scrollTop();
			var leftScroll = f_scrollLeft();
			
			document.getElementById('myStyle').style.height = pageHeight+'px';
			document.getElementById('myStyle').style.width = pageWidth+'px';
			document.getElementById('myStyle').style.top = 0;//topScroll+'px';
			document.getElementById('myStyle').style.left = 0;//leftScroll+'px';
			
			//Center any overlib div popup.
			try {
				if (document.getElementById('overDiv')) {
					var windowWidth = f_clientWidth();
					var windowHeight = f_ViewPortHeight(); //NOTE: f_clientHeight doesn't seem to work properly in IE8 & Chrome.
					var windowScrollLeft = f_scrollLeft();
					var windowScrollTop = f_scrollTop();
					var divWidth = document.getElementById('overDiv').offsetWidth;
					var divHeight = document.getElementById('overDiv').offsetHeight;
					
					var newDivLeft = ((windowWidth / 2) - (divWidth / 2)) + windowScrollLeft;
					var newDivTop = ((windowHeight / 2) - (divHeight / 2)) + windowScrollTop;
					
					if (newDivLeft < 0) {
						newDivLeft = 0;
					}
					if (newDivTop < 0) {
						newDivTop = 0;
					}
					
					document.getElementById('overDiv').style.left=newDivLeft+'px';
					document.getElementById('overDiv').style.top=newDivTop+'px';
				}
			}
			catch(e) {
			}	
			
			//Center any Please Wait div popup.
			try {
				if (document.getElementById('divWait')) {
					var windowWidth = f_clientWidth();
					var windowHeight = f_ViewPortHeight(); //NOTE: f_clientHeight doesn't seem to work properly in IE8 & Chrome.
					var windowScrollLeft = f_scrollLeft();
					var windowScrollTop = f_scrollTop();
					var divWidth = document.getElementById('divWait').offsetWidth;
					var divHeight = document.getElementById('divWait').offsetHeight;
					
					var newDivLeft = ((windowWidth / 2) - (divWidth / 2)) + windowScrollLeft;
					var newDivTop = ((windowHeight / 2) - (divHeight / 2)) + windowScrollTop;
					
					if (newDivLeft < 0) {
						newDivLeft = 0;
					}
					if (newDivTop < 0) {
						newDivTop = 0;
					}
					
					document.getElementById('divWait').style.left=newDivLeft+'px';
					document.getElementById('divWait').style.top=newDivTop+'px';
				}
			}
			catch(e) {
			}	
			
		}
	}
}

function centerDiv(sDivID){
	//Center any Please Wait div popup.
	try {
		if (document.getElementById(sDivID)) {
			var windowWidth = f_clientWidth();
			var windowHeight = f_ViewPortHeight(); //NOTE: f_clientHeight doesn't seem to work properly in IE8 & Chrome.
			var windowScrollLeft = f_scrollLeft();
			var windowScrollTop = f_scrollTop();
			var divWidth = document.getElementById(sDivID).offsetWidth;
			var divHeight = document.getElementById(sDivID).offsetHeight;
			
			var newDivLeft = ((windowWidth / 2) - (divWidth / 2)) + windowScrollLeft;
			var newDivTop = ((windowHeight / 2) - (divHeight / 2)) + windowScrollTop;
			
			if (newDivLeft < 0) {
				newDivLeft = 0;
			}
			if (newDivTop < 0) {
				newDivTop = 0;
			}
			
			document.getElementById(sDivID).style.left=newDivLeft+'px';
			document.getElementById(sDivID).style.top=newDivTop+'px';
		}
	}
	catch(e) {
	}	
}

function closePopup(){
	hideShade();
	try{cClick();} catch(e){}
	try{if (document.getElementById('divWait')){document.body.removeChild(document.getElementById('divWait'));}} catch(e){}
}

function windowUnloaded(){
	//This is a empty function that must be here to force FireFox 2.x and 3.x to re-initialize the Javascript upon clicking the back button.
	//See https://developer.mozilla.org/en/Using_Firefox_1.5_caching for details.
}

function registerShadeFixes() {
	//Add the events to Resize or Reposition the shade if it is showing.
	if (window.addEventListener) {
		//DOM method for binding an event
		window.addEventListener("scroll", resizeShade, false);
		window.addEventListener("resize", resizeShade, false);
		window.addEventListener("unload", windowUnloaded, false); //Workaround for FireFox issue with back button not re-initializing JS.
	}
	else if (window.attachEvent) {
		//IE exclusive method for binding an event
		window.attachEvent("onscroll", resizeShade);
		window.attachEvent("onresize", resizeShade);
		window.attachEvent("onunload", windowUnloaded); //Workaround for FireFox issue with back button not re-initializing JS.
	}
	else if (document.getElementById) {
		//support older modern browsers
		window.onscroll=resizeShade;
		window.onresize=resizeShade;
		window.onunload=windowUnloaded; //Workaround for FireFox issue with back button not re-initializing JS.
	}
}

//Register The Shade Fixes on loading of this js file.
try{registerShadeFixes();}catch(e){}

function preloadCommonImages() {
	
	image_loading_static_gif = new Image();
	image_loading_static_gif.src = "images/loading_static.gif";
			
	image_pleasewaitplain_gif = new Image();
	image_pleasewaitplain_gif.src = "images/pleasewaitplain.gif";

	image_warning_gif = new Image();
	image_warning_gif.src = "images/warning.gif";

	image_nav_logo_gif = new Image();
	image_nav_logo_gif.src = "images/nav_logo.gif";

	image_shadow_grey_gif = new Image();
	image_shadow_grey_gif.src = "images/shadow_grey.gif";
	image_shadow_white_gif = new Image();
	image_shadow_white_gif.src = "images/shadow_white.gif";

	image_z_animated_gif = new Image();
	image_z_animated_gif.src = "images/z_animated.gif";
	
}

//Pre-Load the Common Images upon loading of this js file.
try{preloadCommonImages();}catch(e){}

//Since IE7 stops animations of Pre-Loaded gifs upon a form submit, we have a duplicate copy of the file and after 500ms we change the source from the pre-loaded to the non-pre-loaded.
var loadingAnimiationTimer;
function AnimatedLoading(){
	clearTimeout(loadingAnimiationTimer);
	if (document.getElementById('imgLoadingPleaseWait')){
		image_loading_gif = new Image();
		//Set onload function to swap our image source once we have the cached version of the new image available. Also the onload then needs to be cleared out otherwise every time the gif loops it causes that to fire.
		image_loading_gif.onload=function(){if (image_loading_gif){image_loading_gif.onload='';} document.getElementById('imgLoadingPleaseWait').src = 'images/loading.gif';}
		image_loading_gif.src = "images/loading.gif";
	}
}

function pleaseWait(){
	
	showShade();

	try {HideAllSelectElements();} catch(e) {}
	
	var content = '';
	content += '<table cellpadding="0" cellspacing="0">';
	content += '	<tr>';
	content += '		<td width="273" height="109" valign="top" style="BACKGROUND-IMAGE: url(images/pleasewaitplain.gif);">';
	content += '			<table cellpadding="0" cellspacing="0" style="LEFT: 195px; POSITION: relative; TOP: 30px">';
	content += '				<tr>';
	content += '					<td valign="center" align="left">';
	content += '						<img border=0 id="imgLoadingPleaseWait" src="images/loading_static.gif" style="display:block;">';
	content += '					</td>';
	content += '				</tr>';
	content += '			</table>';
	content += '		</td>';
	content += '	</tr>';
	content += '</table>';
	
	var divWait = document.createElement("div");
	divWait.id='divWait';
	divWait.innerHTML=content;
	divWait.style.zIndex=1000;
	divWait.style.position='absolute';
	divWait.style.height='109px';
	divWait.style.width='273px';
	divWait.style.top='20px';
	divWait.style.left='30px';
	
	document.body.appendChild(divWait);
	
	clearTimeout(loadingAnimiationTimer);
	loadingAnimiationTimer=setTimeout("AnimatedLoading()",500);
	
	//Center the DIV
	try {
		var windowWidth = f_clientWidth();
		var windowHeight = f_ViewPortHeight(); //NOTE: f_clientHeight doesn't seem to work properly in IE8 & Chrome.
		var windowScrollLeft = f_scrollLeft();
		var windowScrollTop = f_scrollTop();
		var divWidth = divWait.offsetWidth;
		var divHeight = divWait.offsetHeight;
		
		var newDivLeft = ((windowWidth / 2) - (divWidth / 2)) + windowScrollLeft;
		var newDivTop = ((windowHeight / 2) - (divHeight / 2)) + windowScrollTop;
		
		if (newDivLeft < 0) {
			newDivLeft = 0;
		}
		if (newDivTop < 0) {
			newDivTop = 0;
		}
		
		divWait.style.left=newDivLeft+'px';
		divWait.style.top=newDivTop+'px';
	}
	catch(e) {
	}
	
}

function HideAllSelectElements(){
	HideSelectElements(RegExp('\0'));
}

function HideSelectElements(regexPatOfDDLsToIgnore) {
	//example usage: HideSelectElements(RegExp('^'+sBaseClientID_IR,'i'));
	//Restore any currently hidden DDL's
	RestoreHiddenSelectElements();
	//Create a global variable to store the ID's of the elements we are hiding so they can be redisplayed.
	arr_HiddenSelects = new Array();
	var selectElements = document.getElementsByTagName('select');
	for(var i=0;i<=selectElements.length-1;i++){
		//Check to see if this is our DDL.
		if (regexPatOfDDLsToIgnore.test(selectElements[i].id)==false){
			//This is not one of our elements.
			var aar_ThisSelect = new Array(2);
			aar_ThisSelect[0]=selectElements[i];
			aar_ThisSelect[1]=selectElements[i].style.visibility;
			arr_HiddenSelects[arr_HiddenSelects.length]=aar_ThisSelect;
			selectElements[i].style.visibility='hidden';
		}
	}
	selectElements=null;
	
	//Hide elements that are in an IFRAME also.
	arr_HiddenSelects_Iframe = new Array();
	var myIFRAMES = document.getElementsByTagName('iframe');
	for(var iCurrIFRAME=0;iCurrIFRAME<=myIFRAMES.length-1;iCurrIFRAME++){
		//Load the document object from the IFRAME.
		var oDoc = (myIFRAMES[iCurrIFRAME].contentWindow || myIFRAMES[iCurrIFRAME].contentDocument);
		if (oDoc.document) {oDoc = oDoc.document}
		var selectElements = oDoc.getElementsByTagName('select');
		for(var i=0;i<=selectElements.length-1;i++){
			//Check to see if this is our DDL.
			if (regexPatOfDDLsToIgnore.test(selectElements[i].id)==false){
				//This is not one of our elements.
				var aar_ThisSelect = new Array(2);
				aar_ThisSelect[0]=selectElements[i];
				aar_ThisSelect[1]=selectElements[i].style.visibility;
				arr_HiddenSelects_Iframe[arr_HiddenSelects_Iframe.length]=aar_ThisSelect;
				selectElements[i].style.visibility='hidden';
			}
		}	
		selectElements=null;
	}
	myIFRAMES=null;
}

function RestoreHiddenSelectElements() {
	if (typeof(arr_HiddenSelects)!='undefined'){
		for(var i=0;i<=arr_HiddenSelects.length-1;i++){
			try{arr_HiddenSelects[i][0].style.visibility=arr_HiddenSelects[i][1];}catch(e){}
		}
		arr_HiddenSelects=undefined;
	}
	if (typeof(arr_HiddenSelects_Iframe)!='undefined'){
		for(var i=0;i<=arr_HiddenSelects_Iframe.length-1;i++){
			try{arr_HiddenSelects_Iframe[i][0].style.visibility=arr_HiddenSelects_Iframe[i][1];}catch(e){}
		}
		arr_HiddenSelects=undefined;
	}
}

function setFocus(elementID){
  if (event.keyCode==13){
		try{document.getElementById(elementID).focus();}
		catch(e){
		}
  }
}

function ShowHideAcctOptions()
{

	if (document.getElementById('rblAcctType_0').checked == true)
		{
		tblIncrementOptions.style.display= 'none';
		}
	if (document.getElementById('rblAcctType_1').checked == true)
		{
		tblIncrementOptions.style.display= 'Block';
		}
	
}

function switchText(object, baseText, eventName)
{
	if (eventName == "blur")
	{
		if (object.value == "")
		{
			object.value = baseText;
		}
	}
	else
	{
		if (object.value == baseText)
		{
			object.value = "";
		}
	}
}

//BEGINING OF PAGE HEIGHT / WIDTH FUNCTIONS
function f_ViewPortHeight(){
	// the standard compliant browsers (mozilla, netscape, opera, IE7, chrome)
	if (typeof window.innerHeight != 'undefined'){
		return window.innerHeight;
	}
	// IE6 in standards compliant mode with a valid doctype as the first line in the document.
	else if (typeof document.documentElement != 'undefined' 
						&& typeof document.documentElement.clientHeight != 'undefined' 
						&& document.documentElement.clientHeight != 0) {
							return document.documentElement.clientHeight;
	}
	// Older versions of IE
	else {
		return document.getElementsByTagName('body')[0].clientHeight;
	}
}
function f_ViewPortWidth(){
	// the standard compliant browsers (mozilla, netscape, opera, IE7, chrome)
	if (typeof window.innerWidth != 'undefined'){
		return window.innerWidth;
	}
	// IE6 in standards compliant mode with a valid doctype as the first line in the document.
	else if (typeof document.documentElement != 'undefined' 
						&& typeof document.documentElement.clientWidth != 'undefined' 
						&& document.documentElement.clientWidth != 0) {
							return document.documentElement.clientWidth;
	}
	// Older versions of IE
	else {
		return document.getElementsByTagName('body')[0].clientWidth;
	}
}
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function f_documentWidth() {
	if (document.body.scrollWidth) {
		return document.body.scrollWidth;
	}
	else {
		var w = document.documentElement.offsetWidth;
		if (window.scrollMaxX) {
			w += window.scrollMaxX;
		}
		return w;
	}
}
function f_documentHeight() {
	if (document.body.scrollHeight) {
		return document.body.scrollHeight;	
	}
	else {
		return document.documentElement.offsetHeight;
	}
}
//END OF PAGE HEIGHT / WIDTH FUNCTIONS

//START OF TABLE SORTING FUNCTIONS
function getSortedRowsByColumnFromTable(myTable,iColumnIndex){
	var t = myTable;
		
	//If the table doesn't have any areas then just return doing nothing.
	if (t.rows.length <= 1) return;

	sortfn = tr_sort_caseinsensitive;
	
	var newRows = new Array();
	for (k=0;k<t.tBodies.length;k++) {
		for (j=0;j<t.tBodies[k].rows.length;j++) { 
			newRows[j] = t.tBodies[k].rows[j];
		}
	}
	
	//Sort naturally first (ASC)
	SORT_COLUMN_INDEX = iColumnIndex; //Declared without the var makes it a global variable.
	newRows.sort(sortfn);
	
	return newRows;
}

function getSortedRowsByColumnFromTableBody_ALPHA(myTableBody,iColumnIndex){
	var t = myTableBody;
		
	//If the table doesn't have any areas then just return doing nothing.
	if (t.rows.length <= 1) return;

	sortfn = tr_sort_caseinsensitive;
	
	var newRows = new Array();
	for (j=0;j<t.rows.length;j++) { 
		newRows[j] = t.rows[j];
	}
	
	//Sort naturally first (ASC)
	SORT_COLUMN_INDEX = iColumnIndex; //Declared without the var makes it a global variable.
	newRows.sort(sortfn);
	
	return newRows;
}

function getSortedRowsByColumnFromTableBody_NUMERIC(myTableBody,iColumnIndex){
	var t = myTableBody;
		
	//If the table doesn't have any areas then just return doing nothing.
	if (t.rows.length <= 1) return;

	sortfn = tr_sort_numeric;
	
	var newRows = new Array();
	for (j=0;j<t.rows.length;j++) { 
		newRows[j] = t.rows[j];
	}
	
	//Sort naturally first (ASC)
	SORT_COLUMN_INDEX = iColumnIndex; //Declared without the var makes it a global variable.
	newRows.sort(sortfn);
	
	return newRows;
}

function getSortedRowsByColumnFromTableBody_DATE(myTableBody,iColumnIndex){
	var t = myTableBody;
		
	//If the table doesn't have any areas then just return doing nothing.
	if (t.rows.length <= 1) return;

	sortfn = tr_sort_date;
	
	var newRows = new Array();
	for (j=0;j<t.rows.length;j++) { 
		newRows[j] = t.rows[j];
	}
	
	//Sort naturally first (ASC)
	SORT_COLUMN_INDEX = iColumnIndex; //Declared without the var makes it a global variable.
	newRows.sort(sortfn);
	
	return newRows;
}
function tr_sort_caseinsensitive(rowA,rowB) {
	//Used to Sort the Values of a ROW, treated as Alpha's.
	//SORT_COLUMN_INDEX must be a globally declared and set variable this function can access.
	aa = td_getInnerText(rowA.cells[SORT_COLUMN_INDEX])
	bb = td_getInnerText(rowB.cells[SORT_COLUMN_INDEX])
	sortVal = sort_caseinsensitive(aa,bb);
	return sortVal;
}

function sort_caseinsensitive(a,b) {
	//Returns 0 if they are equal.
	//Returns 1 if a is greater than b
	//Returns -1 if a is less than b
	aa = a.toLowerCase();
	bb = b.toLowerCase();
	if (aa==bb) {
		return 0;
	}
	if (aa<bb) {
		return -1;
	}
	return 1;
}

function tr_sort_numeric(rowA,rowB) {
	//Used to Sort the Values of a ROW, treated as Numbers.
	//SORT_COLUMN_INDEX must be a globally declared and set variable this function can access.
	var aa = td_getInnerText(rowA.cells[SORT_COLUMN_INDEX]);
	aa = clean_num(aa);
	var bb = td_getInnerText(rowB.cells[SORT_COLUMN_INDEX]);
	bb = clean_num(bb);
	sortVal = sort_numeric(aa,bb);
	return sortVal;
}

function sort_numeric(a,b) {
	//Returns 0 if they are equal.
	//Returns positive int if a is greater than b
	//Returns negative int if a is less than b
	var a = parseFloat(a);
	var b = parseFloat(b);
	a = (isNaN(a) ? 0 : a);
	b = (isNaN(b) ? 0 : b);
	return a - b;
}

function tr_sort_date(rowA,rowB) {
	//Used to Sort the Values of a ROW, treated as Dates.
	//SORT_COLUMN_INDEX must be a globally declared and set variable this function can access.
	dt1 = makeDateSortable(td_getInnerText(rowA.cells[SORT_COLUMN_INDEX]));
	dt2 = makeDateSortable(td_getInnerText(rowB.cells[SORT_COLUMN_INDEX]));
	
	if (dt1==dt2) {
		return 0;
	}
	if (dt1<dt2) { 
		return -1;
	}
	return 1;
}

function makeDateSortable(date) {	
	// y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
	dt = "00000000";
	if (date.length == 11) {
		mtstr = date.substr(3,3);
		mtstr = mtstr.toLowerCase();
		switch(mtstr) {
			case "jan": var mt = "01"; break;
			case "feb": var mt = "02"; break;
			case "mar": var mt = "03"; break;
			case "apr": var mt = "04"; break;
			case "may": var mt = "05"; break;
			case "jun": var mt = "06"; break;
			case "jul": var mt = "07"; break;
			case "aug": var mt = "08"; break;
			case "sep": var mt = "09"; break;
			case "oct": var mt = "10"; break;
			case "nov": var mt = "11"; break;
			case "dec": var mt = "12"; break;
			// default: var mt = "00";
		}
		dt = date.substr(7,4)+mt+date.substr(0,2);
		return dt;
	} else if (date.length == 10) {
		if (europeandate == false) {
			dt = date.substr(6,4)+date.substr(0,2)+date.substr(3,2);
			return dt;
		} else {
			dt = date.substr(6,4)+date.substr(3,2)+date.substr(0,2);
			return dt;
		}
	} else if (date.length == 8) {
		yr = date.substr(6,2);
		if (parseInt(yr) < 50) { 
			yr = '20'+yr; 
		} else { 
			yr = '19'+yr; 
		}
		if (europeandate == true) {
			dt = yr+date.substr(3,2)+date.substr(0,2);
			return dt;
		} else {
			dt = yr+date.substr(0,2)+date.substr(3,2);
			return dt;
		}
	}
	return dt;
}

function clean_num(str) {
	str = str.replace(new RegExp(/[^-?0-9.]/g),"");
	return str;
}

function td_getInnerText(el) {
	//Used to get the TEXT values inside a TD tag.
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) return el.innerText;	//Not needed but it is faster
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += ts_getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}
//END OF TABLE SORTING FUNCTION

