
// Browser Detection
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;

// Body onload utility (supports multiple onload functions)
var gSafeOnload = new Array();
function SafeAddOnload(f)
{
	if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
	{
		window.onload = SafeOnload;
		gSafeOnload[gSafeOnload.length] = f;
	}
	else if  (window.onload)
	{
		if (window.onload != SafeOnload)
		{
			gSafeOnload[0] = window.onload;
			window.onload = SafeOnload;
		}		
		gSafeOnload[gSafeOnload.length] = f;
	}
	else
		window.onload = f;
}
function RoundNumber(numberToRound, decimals) 
{	
	return Math.round(numberToRound*Math.pow(10,decimals))/Math.pow(10,decimals);	
}

function SafeOnload()
{
	for (var i=0;i<gSafeOnload.length;i++)
		gSafeOnload[i]();
}

	function safeAddEventListener(elementId,evnt, func)
{
	//Try IE way first
	var success=false;
	try
	{
		 document.getElementById(elementId).attachEvent("on" + evnt,func)	;
		 return true;	 
	}
	catch(ex1)
	{
		try
		{
		// IE approach failed so try the w3c one
			document.getElementById(elementId).addEventListener(evnt, func, false);
			return true;
		}
		catch(ex2)
		{
			return false		
		}
		
	}
}
function safeAddEventListenerToElement(element,evnt, func)
{
	//Try IE way first
	var success=false;
	try
	{
		 element.attachEvent("on" + evnt,func);
		 return true;	 
	}
	catch(ex1)
	{
		try
		{
		// IE approach failed so try the w3c one
			element.addEventListener(evnt, func, false);
			return true;
		}
		catch(ex2)
		{
			return false;	
		}
		
	}
}

function safeRemoveEventListener(elementId,evnt, func)
{
	//Try IE way first
	var success=false;
	try
	{
		 document.getElementById(elementId).detachEvent("on" + evnt,func);	 
		 return true;
	}
	catch(ex1)
	{
		try
		{
		// IE approach failed so try the w3c one
			document.getElementById(elementId).removeEventListener(evnt, func, false);
			return true;
		}
		catch(ex2)
		{
			return false;		
		}
	}
}

function safeRemoveEventListenerFromElement(element,evnt, func)
{
	//Try IE way first
	var success=false;
	try
	{
		 element.detachEvent("on" + evnt,func);		 
		 return true;
	}
	catch(ex1)
	{
		try
		{
		// IE approach failed so try the w3c one
			element.removeEventListener(evnt, func, false);
			return true;
		}
		catch(ex2)
		{
			return false;		
		}
	}
}

function ShowValidationLayer(controlId, layerHeight, layerWidth)
{
	var contentId=controlId + "InnerDiv";
	var bottomId=controlId + "InnerDivBottom";
	var iframeId=controlId + "iframe";
	document.getElementById(contentId).style.color="red";
	document.getElementById(contentId).style.display="block";
	document.getElementById(contentId).style.overflow="auto";
	document.getElementById(contentId).style.borderTop="red thin solid";
	document.getElementById(contentId).style.borderTopWidth="1px";
	document.getElementById(contentId).style.borderLeft="red thin solid";
	document.getElementById(contentId).style.borderLeftWidth="1px";
	document.getElementById(contentId).style.borderRight="red thin solid";
	document.getElementById(contentId).style.borderRightWidth="1px";
	document.getElementById(contentId).style.textAlign="left";
	document.getElementById(contentId).style.left= "0px";
	document.getElementById(contentId).style.height=layerHeight;
	document.getElementById(contentId).style.width=layerWidth;
	document.getElementById(contentId).style.zIndex=250;
	document.getElementById(contentId).style.top=(layerHeight *(-1))-14;
	document.getElementById(contentId).style.backgroundColor="white";
	document.getElementById(bottomId).style.background="url(images/validationbottom.gif) no-repeat bottom left";
	document.getElementById(bottomId).style.top="-14px";
	document.getElementById(bottomId).style.left= "0px";
	document.getElementById(bottomId).style.height="14px";
	document.getElementById(bottomId).style.width=layerWidth;
	document.getElementById(bottomId).style.zIndex=250;
	document.getElementById(contentId).style.visibility="visible";
	document.getElementById(bottomId).style.visibility="visible";
	document.getElementById(iframeId).style.display ='block';
	document.getElementById(iframeId).style.width =layerWidth;
	document.getElementById(iframeId).style.height =layerHeight+14;
	document.getElementById(iframeId).style.top=(layerHeight *(-1))-14;
	document.getElementById(iframeId).style.left= "0px";
	document.getElementById(iframeId).style.zIndex=249;
	document.getElementById(iframeId).style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
	document.getElementById(controlId + "OuterDiv").style.visibility="visible";
	document.getElementById(controlId + "OuterDiv").style.zIndex=250;
	return true;
}
function HideValidationLayer(controlId)
{
	var contentId=controlId + "InnerDiv";
	var bottomId=controlId + "InnerDivBottom";
	document.getElementById(contentId).style.visibility="hidden";
	document.getElementById(bottomId).style.visibility="hidden";
	document.getElementById(controlId + "OuterDiv").style.visibility="hidden";
	document.getElementById(controlId + "iframe").style.display ='none';
	return true;
}


function Trim(orgString){
	if(orgString!=undefined && orgString.length>0)
	{
		return LTrim(RTrim(orgString));	
	}
	else
	{
		return '';
	}
}

function LTrim(orgString)
{
	if(orgString!=undefined && orgString.length>0)
	{
		 return orgString.replace(/^\s+/,'');
	}
	else
	{
		return '';
	} 
}

function RTrim(orgString)
{
	if(orgString!=undefined && orgString.length>0)
	{
		 return orgString.replace(/\s+$/,'');
	}
	else
	{
		return '';
	}   
} 
function updateCityCountyState(zipId, cityId, countyId, stateId, nextElementId)
{
	// Call server asynchronously if the zip value is supplied
	try
	{			
		if(document.getElementById(zipId).value!='')
			PAI.Framework.Web.AJAXFunctions.CompleteAddress(
			document.getElementById(zipId).value,
			cityId,countyId,stateId,nextElementId,updateCityCountyState_callback);	
			
	}
	catch(ex)
	{	
	}

}
function updateCityCountyState_callback(response)
{		
	// Receievs reply from server and updates controls if a match was found.
	try
	{
		if(response.value.Resolved)
		{		
			document.getElementById(response.value.CityId).value = response.value.City;
			document.getElementById(response.value.CountyId).value =response.value.County;			
			document.getElementById(response.value.StateId).value = response.value.State;								
			document.getElementById(response.value.NextElementId).focus();
			document.getElementById(response.value.NextElementId).select();
			
		} 
    }
    catch(ex)
    {
    }
}

// Gets the absolute X coordinate relative to the top-left corner of the client area in the browser.
// It will work in Mozilla and IE and take into consideration scrolling in divs as well as the window itself.
function findPosX(obj)
{	
	var curleft = 0;
	var startObj=obj;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	obj=startObj;
	while(obj.parentNode)
	{		
		if (obj.pageXOffset) // all except Explorer
		{		
			curleft -= obj.pageXOffset;				
		}
		else if (obj && obj.scrollLeft && obj.tagName!="INPUT")
			// Explorer 6 Strict
		{		
			curleft -= obj.scrollLeft;				
		}
		obj=obj.parentNode;
	}	
	
	if (self.pageYOffset) // all except Explorer
	{
		curleft+= self.pageXOffset;		
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	// Explorer 6 Strict
	{
		curleft+=document.documentElement.scrollLeft;	
	}
	else if (document.body) // all other Explorers
	{
		curleft+=document.body.scrollLeft;		
	}	
	
	return curleft;
}
// Gets the absolute Y coordinate relative to the top-left corner of the client area in the browser.
// It will work in Mozilla and IE and take into consideration scrolling in divs as well as the window itself.
function findPosY(obj)
{
	var curtop = 0;
	var startObj=obj;
	
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;			
		}				
	}	
	obj=startObj;
	while(obj.parentNode)
	{			
			if (obj.pageYOffset) // all except Explorer
			{		
				curtop -= obj.pageYOffset;				
			}
			else if (obj && obj.scrollTop && obj.tagName!="INPUT")
				// Explorer 6 Strict
			{		
				curtop -= obj.scrollTop;				
			}
			obj=obj.parentNode;
	}	
	
	if (self.pageYOffset) // all except Explorer
	{		
		curtop += self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	// Explorer 6 Strict
	{		
		curtop += document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{		
		curtop += document.body.scrollTop;
	}
	
	return curtop;
}

function ShowRedPopUpBox(controlId,innerDivId, outerDivId,innerDivBottomId, iframeId)
{
	document.getElementById(innerDivId).style.color="red";
	document.getElementById(innerDivId).style.display="block";
	document.getElementById(innerDivId).style.textAlign="left";
	document.getElementById(innerDivId).style.left= (document.getElementById(controlId).offsetLeft-20);
	document.getElementById(innerDivId).style.zIndex=251;	
	document.getElementById(innerDivId).style.top=((-1)*document.getElementById(innerDivId).offsetHeight);
	document.getElementById(innerDivBottomId).style.background="url(images/validationbottom.gif) no-repeat bottom left";
	document.getElementById(innerDivBottomId).style.width=document.getElementById(innerDivId).style.width;
	document.getElementById(innerDivBottomId).style.zIndex=251;
	document.getElementById(innerDivId).style.visibility="visible";
	document.getElementById(innerDivBottomId).style.visibility="visible";
	document.getElementById(outerDivId).style.visibility="visible";
	document.getElementById(outerDivId).style.zIndex=250;
	return true;
}

function HideRedPopUpBox(controlId,innerDivId, outerDivId,innerDivBottomId, iframeId)
{
	document.getElementById(innerDivId).style.visibility="hidden";
	document.getElementById(outerDivId).style.visibility="hidden";
	document.getElementById(innerDivBottomId).style.visibility="hidden";	
	document.getElementById(iframeId).style.display ='none';
	return true;
}

// Adds a content layer to the specified placeholder. The content layer will be equipped with the specified
// header and contentHTML. Returns the object referenc of the layer holding the contentbox.
function showContentBox(placeHolderDivId, header, contentElement)
{
	var placeHolderDiv=document.getElementById(placeHolderDivId);
	
	placeHolderDiv.innerHTML="";
	var br=document.createElement("br");
	placeHolderDiv.appendChild(br);
		
	var hiddenDetailDiv=document.createElement("div");
	placeHolderDiv.appendChild(hiddenDetailDiv);
	placeHolderDiv.style.visibility="visible";
		
	var table=document.createElement("table");
	hiddenDetailDiv.appendChild(table);	
	table.border="0";
	table.cellPadding="0";
	table.cellSpacing="0";
	
	var tbody=document.createElement("tbody");
	table.appendChild(tbody);	
	
	var tr1=document.createElement("tr");
	tbody.appendChild(tr1);
	
	var td11=document.createElement("td");
	tr1.appendChild(td11);
	td11.className="contentboxtopleft";
	
	var td12=document.createElement("td");
	tr1.appendChild(td12);	
	td12.className="contentboxheader";
	td12.innerHTML=header;
	td12.style.fontWeight='bold';
		
	var td13=document.createElement("td");
	tr1.appendChild(td13);
	td13.className="contentboxtopright";
		
	var tr2=document.createElement("tr");
	tbody.appendChild(tr2);
	
	var td21=document.createElement("td");
	tr2.appendChild(td21);	
	td21.className="contentboxmiddleleft";
	
	
	var td22=document.createElement("td");
	tr2.appendChild(td22);	
	td22.appendChild(contentElement);
	
	var td23=document.createElement("td");
	tr2.appendChild(td23);	
	td23.className="contentboxmiddleright";
	
	
	var tr3=document.createElement("tr");
	tbody.appendChild(tr3);
	
	var td31=document.createElement("td");
	tr3.appendChild(td31);	
	td31.className="contentboxbottomleft";
	
	var td32=document.createElement("td");
	tr3.appendChild(td32);	
	td32.className="contentboxbottom";

	
	var td33=document.createElement("td");
	tr3.appendChild(td33);
	td33.className="contentboxbottomright";
}

function formatCurrency(num) {
	if(num==null || num=='undefined')
		num = "0";
			
	num = stringCurrencyToNumber(num.toString());
	
	if(isNaN(num))
		num = "0";
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function stringCurrencyToNumber(str)
{
	var sign = 1;

	if (str.substring(0,1) == '(' || str.substring(0,1) == '-')
	    sign = -1;

	var num = Number(str.replace(/\$|\,|\(|\)|\-/g,''));
	num = num * sign;

	return  num;
}



function eventElement(event)
{
try
{
  if(navigator.product == "Gecko")
  {
    return event.currentTarget;
  }
  else
  {
    return event.srcElement;
  }
}
catch(ex)
{}

 
}

function toolTipState(controlId,innerHTML,left,width)
{
	this.controlId=controlId;
	this.innerHTML=innerHTML;
	this.left=left;
	this.width=width;
}

var pendingToolTip=null;

function ShowToolTip(controlId,innerHTML,left,width)
{
	if(pendingToolTip!=null)
	{
		if(pendingToolTip.controlId==controlId)
			return;// This ToolTip is already pending or showin so just return
		else
			HideToolTip(controlId);// Hide tooltip for this other element			
	}	
		
	// Store the values off and then see if the mouse is still over the control
	// in a second	
	pendingToolTip=new toolTipState(controlId,innerHTML,left,width);
	setTimeout('DisplayToolTip()',1000);	
}

function DisplayToolTip()
{
	if(pendingToolTip!=null)
	{
		var control=document.getElementById(pendingToolTip.controlId);
		
			
		var outerDiv=document.createElement("div");
		outerDiv.style.visibility="hidden";
		outerDiv.id=pendingToolTip.controlId+"_tooltipouterdiv";	
		document.body.insertBefore(outerDiv,document.body.childNodes[0]);			
		outerDiv.style.position="absolute";


		

		var tp=document.createElement("div");
		tp.style.visibility="hidden";
		tp.id=pendingToolTip.controlId+"_tooltip";	
		tp.className='DefaultText';
		tp.style.position="absolute";
		tp.style.display ='block';	
		tp.style.visible ='hidden';	
		outerDiv.appendChild(tp);
		tp.innerHTML=pendingToolTip.innerHTML;
		tp.style.zIndex=251;
		tp.style.backgroundColor="#ffffcc";
		tp.style.border="solid";
		tp.style.borderColor="black";
		tp.style.borderWidth="1px";
		tp.style.padding="2px";
			
		
		tp.style.left=findPosX(control)+2;//+pendingToolTip.left;
		tp.style.top=findPosY(control)+control.offsetHeight-2;
	
		
		tp.style.width=pendingToolTip.width+"px";
		outerDiv.style.visibility="visible";
		tp.style.visibility="visible";
		
		if(IE4plus)
		{		
			var iframe=document.createElement("iframe");
			iframe.style.visibility="hidden";
			iframe.style.position="absolute";
			iframe.src="javascript:false";
			iframe.id=pendingToolTip.controlId+"_tooltipiframe";
			outerDiv.appendChild(iframe);
			iframe.style.display ='block';	
			iframe.style.width =tp.offsetWidth-2;
			iframe.style.height =tp.offsetHeight+2;
			iframe.style.top=tp.style.top;
			iframe.style.left=tp.style.left;
			iframe.style.zIndex=249;	
			iframe.style.border="none";
			iframe.style.visibility="visible";
			//iframe.style.backgroundColor='red';
				
			iframe.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';		
		}
		return true;
		
	}
}
function HideToolTip(controlId)
{
	try
	{			
		var outerdiv;
		if(pendingToolTip!=null)
		{			
			try
			{	
				// Hide whatever is in the pedningToolTip		
				outerdiv=document.getElementById(pendingToolTip.controlId + "_tooltipouterdiv")
				outerdiv.parentNode.removeChild(outerdiv);	
			}
			catch(ex){}
			pendingToolTip=null;			
		}	
					
		outerdiv=document.getElementById(controlId + "_tooltipouterdiv")
		outerdiv.parentNode.removeChild(outerdiv);	
	
			
	}
	catch(ex)
	{
	}
}


function setCursor(newCursor)
{
	document.body.style.cursor = newCursor;
}




function Queue()
{
    	this.elements = this.reset;
    	this.reset = 	function()		{	this.elements = new Array();		};
    	this.push = 	function(obj)	{	this.elements.push(obj);		};
    	this.pop = 	function()		{	return this.elements.shift();		};
    	this.empty = 	function()		{	return !this.elements.length;		};
    	this.size = 	function()		{	return this.elements.length;		};
    	this.front = 	function()		{	return this.elements[0];		};
}







/**
 * Throughout, whitespace is defined as one of the characters
 *  "\t" TAB \u0009
 *  "\n" LF  \u000A
 *  "\r" CR  \u000D
 *  " "  SPC \u0020
 *
 * This does not use Javascript's "\s" because that includes non-breaking
 * spaces (and also some other characters).
 */


/**
 * Determine whether a node's text content is entirely whitespace.
 *
 * @param nod  A node implementing the |CharacterData| interface (i.e.,
 *             a |Text|, |Comment|, or |CDATASection| node
 * @return     True if all of the text content of |nod| is whitespace,
 *             otherwise false.
 */
function is_all_ws( nod )
{
  // Use ECMA-262 Edition 3 String and RegExp features
  return !(/[^\t\n\r ]/.test(nod.data));
}


/**
 * Determine if a node should be ignored by the iterator functions.
 *
 * @param nod  An object implementing the DOM1 |Node| interface.
 * @return     true if the node is:
 *                1) A |Text| node that is all whitespace
 *                2) A |Comment| node
 *             and otherwise false.
 */

function is_ignorable( nod )
{
  return ( nod.nodeType == 8) || // A comment node
         ( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws
}

/**
 * Version of |previousSibling| that skips nodes that are entirely
 * whitespace or comments.  (Normally |previousSibling| is a property
 * of all DOM nodes that gives the sibling node, the node that is
 * a child of the same parent, that occurs immediately before the
 * reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest previous sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function node_before( sib )
{
  while ((sib = sib.previousSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

/**
 * Version of |nextSibling| that skips nodes that are entirely
 * whitespace or comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest next sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function node_after( sib )
{
  while ((sib = sib.nextSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

/**
 * Version of |lastChild| that skips nodes that are entirely
 * whitespace or comments.  (Normally |lastChild| is a property
 * of all DOM nodes that gives the last of the nodes contained
 * directly in the reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The last child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function last_child( par )
{
  var res=par.lastChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.previousSibling;
  }
  return null;
}

/**
 * Version of |firstChild| that skips nodes that are entirely
 * whitespace and comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The first child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function first_child( par )
{
  var res=par.firstChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.nextSibling;
  }
  return null;
}

/**
 * Version of |data| that doesn't include whitespace at the beginning
 * and end and normalizes all whitespace to a single space.  (Normally
 * |data| is a property of text nodes that gives the text of the node.)
 *
 * @param txt  The text node whose data should be returned
 * @return     A string giving the contents of the text node with
 *             whitespace collapsed.
 */
function data_of( txt )
{
  var data = txt.data;
  // Use ECMA-262 Edition 3 String and RegExp features
  data = data.replace(/[\t\n\r ]+/g, " ");
  if (data.charAt(0) == " ")
    data = data.substring(1, data.length);
  if (data.charAt(data.length - 1) == " ")
    data = data.substring(0, data.length - 1);
  return data;
}

function getFirstChildNodeOfTagName(parentNode,tagName)
{
	cur=first_child(parentNode);
	while (cur)
	{
		if(cur.tagName==tagName)
			return cur;		
				
		cur = node_after(cur);
	}
	return null;
}

function showLoginHelp()
{
	var messageBox=document.getElementById("LoginHelp_messagebox");	
	if(messageBox!=null)
	{
		// We already have a box showing here so no need to proceeed.
		return;
	}
	
	var control=document.getElementById("LoginHelp");
	var content=document.createElement("div");
	content.style.width="400px";
	content.style.backgroundColor="white";
	content.className='DefaultText';
	header=document.createTextNode("If you have previously verified your e-mail address and provided an account question and answer, we can provide you with a temporary Password after supplying your User Name and Email Address.   To provide your User Name and Email Address, select the \"I have forgotten my password\" link.  If that information has not previously been provided, please call 1-800-236-7400 for assistance.");
	content.appendChild(header);
	
	displayInRedPopUp(control,content,"LoginHelp_messagebox");	
}

function showChangePasswordHelp()
{
	var messageBox=document.getElementById("ChangePasswordHelp_messagebox");	
	if(messageBox!=null)
	{
		// We already have a box showing here so no need to proceeed.
		return;
	}
	
	var control=document.getElementById("ChangePasswordHelp");
	var content=document.createElement("div");
	content.style.width="400px";
	content.style.backgroundColor="white";
	content.className='DefaultText';
	header=document.createTextNode("Please contact our Customer Care team at 1-800-236-7400. Our agents are available from 8am-5pm (Central Standard Time), Monday-Friday, except for market holidays.");
	content.appendChild(header);
	
	displayInRedPopUp(control,content,"ChangePasswordHelp_messagebox");	
}







