var wHeight = window.screen.availHeight-35;
var wWidth = window.screen.availWidth-10;
var dom = document.getElementById && !document.all;
var AOPath = "/ao/";
/*
	Window opener script. For child popup windows.
*/
function openWindow( url, name, width, height, wWidth, wHeight )
{
	if(name=="AllRegsOnline" && dom)
	{
		window.open(AOPath + "browsererror.html");
		return;
	}
	// compute our centered left and top coordinates
	var left = ( wWidth / 2 ) - ( width / 2 );
	var top = ( wHeight / 2 ) - ( height / 2 );
	var winHdl = window.open( url, name, "status=0,resizable,scrollbars,height=" + height + ", width=" + width + ", top=" + top + ", left=" + left + "", true );
	if(winHdl != null)
		winHdl.focus();
}

function openWindowNoScroll( url, name, width, height, wWidth, wHeight )
{
	if( name == "AllRegsOnline" && dom )
	{
		url = AOPath + "browsererror.html";
	}
	// compute our centered left and top coordinates
	var left = ( wWidth / 2 ) - ( width / 2 );
	var top = ( wHeight /2 ) - ( height / 2 );
	var winHdl = window.open( url, name, "status=0, resizable, height=" + height + ", width=" + width + ", top=" + top + ", left=" + left + "", true );
	if( winHdl != null )
		winHdl.focus();
}
/* 
	Modal window popups. For opening modal popup windows.
*/
function openModal( url, width, height )
{
	if(!dom)
	{
		window.showModalDialog( url, "modal", "status=0;help=0;dialogHeight=" + height + "px; dialogWidth=" + width + "px;" );
	}
	else
	{
		var winHdl = window.open( url, "modal", "modal=yes;status=0;help=0;dialogHeight=" + height + "px; dialogWidth=" + width + "px;" );
		if(winHdl != null)
			winHdl.focus();
	}
}

function OpenAllRegsHome()
{
	if(window.opener && !window.opener.closed)
	{
		if( window.opener.location.href.indexOf( "/default.asp" ) < 0 )
			window.opener.location.href = "/default.asp";
		window.opener.focus();
	}
	else
		redirectOpener( "/default.asp" )
	
}

// Opens links in the window that spawned AllRegs. Alternately opens a new window when opener is null.
function redirectOpener( url )
{
	var oHandle = window.opener;
	// Determine whether our window.opener is NULL
	if( oHandle == null || oHandle.closed )
	{
		oHandle = window.open( url, "parent" );
		if(oHandle != null)
			oHandle.focus();
	}
	else
	{
		window.opener.location.href=url;
		window.opener.focus();
	}
}

var printWindow;
function openPrintWindow(pUrl, pName, pOptions, pReplace)
{
	printWindow = window.open(pUrl, pName, pOptions, pReplace);
	if(printWindow!=null)
	{
		window.attachEvent("onbeforeunload",ClosePrintWindow);
		printWindow.focus();
	}
}

function ClosePrintWindow()
{
	if(printWindow!=null && !printWindow.closed)
	{
		printWindow.close();
		window.detachEvent("onbeforeunload",ClosePrintWindow);
	}
	else if(printWindow!=null && printWindow.closed)
		window.detachEvent("onbeforeunload",ClosePrintWindow);
}

/*************** Progress Bar script. Needs Work! *************/ 

var mInterval;
var cont;
var maskElem;
var indElem;
var gradElem;


function getElem( elem ) {
	var div = dom ? document.getElementById( elem ) : document.all[ elem ];
	return div;
}

function startProgressBar() {
	window.loadingspan.style.zIndex = 1000;
	window.loadingspan.style.display="block";
}

function hideProgress()
{
	cont = getElem( "loadingspan" );
	cont.style.background = "#bfccd9";
	cont.style.display = "none";
	cont.style.zIndex = -1000;
}

function reset() {
	maskElem.style.left = "0px";
	maskElem.style.width = cont.offsetWidth + "px";
	indElem.style.zIndex  = 10;
	maskElem.style.display = "block";
	//indElem.innerHTML = "0%";
}

function doProgress() {
	curWidth = parseInt( maskElem.offsetWidth )-2;
	curLeft = parseInt( maskElem.offsetLeft )+2;

	if( curLeft >= 101 ) {
		clearInterval( mInterval );
		maskElem.style.display = "none";
		return;
	}


	maskElem.style.left = curLeft + "px";
	if( parseInt( maskElem.offsetWidth ) > 10 )
		maskElem.style.width = curWidth + "px";
	//indElem.innerHTML = Math.floor((curLeft / parseInt(gradElem.offsetWidth))*100) + "%";

}

/************************ Functions to display the appropriate fields for username/password entry ******************/

var pwField;
var dummyField;

function getFields()
{
	pwField = !dom ? document.all[ "Header1_login_txtPassword" ] : document.getElementById( "Header1_login_txtPassword" );
	dummyField = !dom ? document.all[ "Header1_login_dummyPassword" ] : document.getElementById( "Header1_login_dummyPassword" );
}

function focusPasswordField()
{
	getFields();
	pwField.style.display = "";
	pwField.focus();
	dummyField.style.display = "none";
}

function showDummyField()
{
	getFields();
	if( pwField.value == '' )
	{
		pwField.style.display = "none";
		dummyField.style.display = "";
	}
}

// Post Office Box Regex validation. We cannot accept them for addresses.
function valAddressInput( source, arguments )
{
	var regex = /(((P\.? *O\.? *)|(POST *OFFICE)) *BOX)/i;
	if( regex.test( arguments.Value ) ) 
		arguments.IsValid = false;
	else 
		arguments.IsValid = true;	
}