/* Global JavaScript file to be included in all pages
 *------------------------------------------------------*/
 
 
var HUBRIS_jsvInputTextArray = new Array();
var HUBRIS_jsvWebsiteWidth = 1003;
var HUBRIS_jsvDropdownOuputArray = new Array();

/*
function HUBRIS_jsfTriggerDropdownOutput()
{
	//get every select element within the document
	var selectMenus = document.getElementsByTagName("select");
	
	//add all the IDs to the HUBRIS_jsvDropdownOuputArray, with the corresponding hide/show
	for( var i=0; i<selectMenus.length; i++ )
	{
		HUBRIS_jsvDropdownOuputArray[selectMenus[i].id] = selectMenus[i].id+"HideShow01,"+selectMenus[i].id+"HideShow02";
		var objReference = document.getElementById( new String( selectMenus[i].id ) );
		if( objReference != null )
		{			
			objReference.onchange = HUBRIS_jsfOutputDropdownText;
		}
	}
}
*/
/*
function HUBRIS_jsfOutputDropdownText()
{
	//giving you two elements e.g. labelIdBillMerchantHideShow01, labelIdBillMerchantHideShow02
	var hideShowArray = HUBRIS_jsvDropdownOuputArray[this.id].split(",");
	
	if((objReference = document.getElementById(this.id))==null)
	{
		return;
	}
	
	if(objReference.selectedIndex==0)
	{
		for(i=0;i<hideShowArray.length;i++)
		{
			if(document.getElementById(hideShowArray[i]))
			{
				document.getElementById(hideShowArray[i]).style.display = "none";
			}
		}
		return;
	}
	//effectively else
	for(i=0;i<hideShowArray.length;i++)
	{
		if(document.getElementById(hideShowArray[i]))
		{
			document.getElementById(hideShowArray[i]).style.display = "block";
		}
	}
	if(document.getElementById(this.id+"Output"))
	{
		document.getElementById(this.id+"Output").innerHTML = objReference.options[objReference.selectedIndex].innerHTML;
	}
}
*/
function HUBRIS_jsfTriggerInputFieldText(objId)
{
	if((objReference = document.getElementById(objId))==null)
	{
		return;
	}
	
	HUBRIS_jsvInputTextArray[objId] = objReference.value;
	
	objReference.onfocus = HUBRIS_jsfInputFieldClear;
	objReference.onblur =  HUBRIS_jsfInputFieldFill;
	// fixes NS_ERROR_XPC_JS_THREW_STRING bug
	// http://tinyurl.com/7ud3b
	objReference.setAttribute("autocomplete","off");
}

function HUBRIS_jsfInputFieldClear()
{
	if((objReference = document.getElementById(this.id))==null)
	{
		return;
	}
	if(objReference.getAttribute("value")==HUBRIS_jsvInputTextArray[objReference.id])
	{
		objReference.setAttribute("value","");
	}
}

function HUBRIS_jsfInputFieldFill()
{
	if((objReference = document.getElementById(this.id))==null)
	{
		return;
	}
	if(objReference.getAttribute("value")=="")
	{
		objReference.setAttribute("value",HUBRIS_jsvInputTextArray[objReference.id]);
	}
}

function HUBRIS_jsfTriggerShowHideBalance()
{
	if((objShowHideTable = document.getElementById("jsIdBalanceHideShow"))==null)
	{
		return;
	}
	if((objShowButton = document.getElementById("jsIdBalanceShowButton"))==null)
	{
		return;
	}
	if((objHideButton = document.getElementById("jsIdBalanceHideButton"))==null)
	{
		return;
	}
	
	objShowHideTable.style.display = "none";
	objShowButton.style.display = "block";
	objShowButton.onclick = HUBRIS_jsfShowHideBalance;
	objHideButton.onclick = HUBRIS_jsfShowHideBalance;
}

function HUBRIS_jsfShowHideBalance()
{
	if((objShowHideTable = document.getElementById("jsIdBalanceHideShow"))==null)
	{
		return;
	}
	if((objShowButton = document.getElementById("jsIdBalanceShowButton"))==null)
	{
		return;
	}
	if((objHideButton = document.getElementById("jsIdBalanceHideButton"))==null)
	{
		return;
	}
	
	if(objShowHideTable.style.display == "block")
	{
		objShowHideTable.style.display = "none";
		objShowButton.style.display = "block";
		objHideButton.style.display = "none";
	}
	else
	{
		objShowHideTable.style.display = "block";
		objShowButton.style.display = "none";
		objHideButton.style.display = "block";
	}
}

function HUBRIS_jsfTriggerShowHideOtherAccounts()
{
	if((objShowHideTable = document.getElementById("jsIdOtherAccountsHideShow"))==null)
	{
		return;
	}
	if((objShowButton = document.getElementById("jsIdOtherAccountsShowButton"))==null)
	{
		return;
	}
	if((objHideButton = document.getElementById("jsIdOtherAccountsHideButton"))==null)
	{
		return;
	}
	objShowHideTable.style.display = "none";
	objShowButton.style.display = "block";
	objShowButton.onclick = HUBRIS_jsfShowHideOtherAccounts;
	objHideButton.onclick = HUBRIS_jsfShowHideOtherAccounts;
}

function HUBRIS_jsfShowHideOtherAccounts()
{
	if((objShowHideTable = document.getElementById("jsIdOtherAccountsHideShow"))==null)
	{
		return;
	}
	if((objShowButton = document.getElementById("jsIdOtherAccountsShowButton"))==null)
	{
		return;
	}
	if((objHideButton = document.getElementById("jsIdOtherAccountsHideButton"))==null)
	{
		return;
	}
	
	if(objShowHideTable.style.display == "block")
	{
		objShowHideTable.style.display = "none";
		objShowButton.style.display = "block";
		objHideButton.style.display = "none";
	}
	else
	{
		objShowHideTable.style.display = "block";
		objShowButton.style.display = "none";
		objHideButton.style.display = "block";
	}
}

function HUBRIS_jsfTriggerCheckLogonStatus()
{
	var browser = new HUBRIS_jsfBrowserDetect();
	var defaultWidth = browser.isFF==1?HUBRIS_jsvWebsiteWidth+16:HUBRIS_jsvWebsiteWidth;
	var minimumPadding = 8;
	var jsvExtendLogon = 0;
	if((objLogonStatus = document.getElementById("jsIdLogonStatus"))==null)
	{
		return;
	}
	
	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		myWidth = window.innerWidth;
	}
	else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	}

	if((myWidth-defaultWidth)<0)
	{
		jsvExtendLogon = defaultWidth-myWidth+minimumPadding;
		objLogonStatus.style.paddingRight = jsvExtendLogon+"px";
	}
	else
	{
		objLogonStatus.style.paddingRight = minimumPadding+"px";
	}
}

function HUBRIS_jsfBrowserDetect()
{
	var ua = navigator.userAgent.toLowerCase(); 
	this.isIE501 = (ua.indexOf('msie 5.01') != -1);
	this.isFF = (ua.indexOf('firefox')!= -1);
}


function HUBRIS_jsfPopWin(e)
{
	if (!e) var e = window.event;
	var a = e.target ? e.target : e.srcElement;
	var newwin = window.open(a.href);
	return !newwin;                               
}

function HUBRIS_jsfTriggerPopLinks()
{
	var links = document.getElementsByTagName('a');
	for (var i = links.length; i != 0; i--)
	{
		var a = links[i-1];
		if (!a.href) continue;
		if (a.className && a.className.indexOf('extExternalLink') != -1) a.onclick = HUBRIS_jsfPopWin;
	}
}
 
function HUBRIS_jsfOnLoad()
{
	var browser = new HUBRIS_jsfBrowserDetect();
	if(browser.isIE501)
	{
		return;
	}
	HUBRIS_jsfTriggerShowHideBalance();
	HUBRIS_jsfTriggerShowHideOtherAccounts();
	HUBRIS_jsfTriggerInputFieldText("labelIdAmount");
	HUBRIS_jsfTriggerInputFieldText("jsIdStockCode");
	HUBRIS_jsfTriggerInputFieldText("labelBuyOrderStockCode");
	HUBRIS_jsfTriggerCheckLogonStatus();
	HUBRIS_jsfTriggerPopLinks();
	
	window.onresize = HUBRIS_jsfTriggerCheckLogonStatus;
}

if (window.addEventListener)
{
	window.addEventListener("load", HUBRIS_jsfOnLoad, false);
}
else
{
	if (window.attachEvent)
	{
		window.attachEvent("onload", HUBRIS_jsfOnLoad);
	}
	else
	{
	if (document.getElementById)
		{
			window.onload = HUBRIS_jsfOnLoad;
		}
	}
}

