/*

This js file contains a series of functions that switch based on the browser

*/

//Used for the modal crossbrowser modal dialog
var objGlobalWindow = null;
var objGlobalReference = null;

function documentForms(strForm)
{
	return ((document.forms[strForm]) ? document.forms[strForm] : document.forms(strForm));
}

function bcGetElementsByName(strName, objDocument)
{
	var arrReturn = new Array();
	var arrTemp;
	var nCounter;
	
	if(objDocument == null) objDocument = document;
	
	arrTemp = bcGetElementsByTagName("*", objDocument);
	
	for(nCounter = 0; nCounter < arrTemp.length; nCounter++)
	{
		if(arrTemp[nCounter].name == strName)
		{
			arrReturn[arrReturn.length] = arrTemp[nCounter];
		}
	}
	
	return arrReturn;
}

function bcGetElementsByTagName(strTagName, objDocument)
{
	var arrReturn = null;
	
	if(objDocument == null) objDocument = document;
	
	strTagName = strTagName || '*';
	if(objDocument.getElementsByTagName)
	{
		arrReturn = objDocument.getElementsByTagName(strTagName);
	}
	else
	{
		if(strTagName == '*')
		{
			arrReturn = objDocument.all;
		}
		else
		{
			if (objDocument.all && objDocument.all.tags) arrReturn = objDocument.all.tags(strTagName);
		}
	}

	return arrReturn || new Array();
}

function bcGetElementById(strID)
{
	var objElement = null;
	if(typeof(strID) == 'string')
	{
		if(document.getElementById)
		{
			objElement = document.getElementById(strID);
		}
		else if(document.all)
		{
			objElement = document.all[strId];
		}
	}
	
	return objElement;
}

function bcAreAllDefined()
{
	var nCounter;
	
	for(nCounter = 0; nCounter < arguments.length; nCounter++)
	{
		if(typeof(arguments[nCounter]) == 'undefined') return false;
	}
	
	return true;
}

function bcGetInnerText(objElement)
{
	if(objElement.innerText)
	{
		return objElement.innerText;
	}
	else
	{
		return objElement.textContent;
	}
}

function bcCancelEvent(objEvent)
{
	if(objEvent.preventDefault)
	{
		objEvent.preventDefault();
	}
	else
	{
		objEvent.returnValue = false;
	}
}

function bcEvent(objEvent) // object prototype
{
	var objReturn = Object();
	
	if(typeof(objEvent) == 'undefined') objEvent = window.event;
	if(!objEvent) return;

	// Same on both
	objReturn.altKey = objEvent.altKey;
	objReturn.ctrlKey = objEvent.ctrlKey;
	objReturn.shiftKey = objEvent.shiftKey;
	objReturn.clientX = objEvent.clientX;
	objReturn.clientY = objEvent.clientY;
	objReturn.type = objEvent.type;
	objReturn.screenX = objEvent.screenX;
	objReturn.screenY = objEvent.screenY;
	objReturn.keyCode = objEvent.keyCode || objEvent.charCode || objEvent.which || 0;
	objReturn.target = objEvent.target || objEvent.srcElement;

	if (objEvent.relatedTarget)
	{
		objReturn.relatedTarget = objEvent.relatedTarget;
	}
	else if (objEvent.type == 'mouseover' && objEvent.fromElement)
	{
		objReturn.relatedTarget = objEvent.fromElement;
	}
	else if (objEvent.type == 'mouseout')
	{
		objReturn.relatedTarget = objEvent.toElement;
	}
	if(objEvent.metaKey) objReturn.metaKey = objEvent.metaKey;
	
	// 0 = none, 1 = left, 2 = right, 3 = middle
	objReturn.button = 0;
	if (objEvent.type.indexOf('click') != -1)
	{
		objReturn.button = 1;
	}
	else if (objEvent.type.indexOf('mouse') != -1)
	{
		if(objEvent.srcElement)
		{
			// objReturn is IE
			if(objEvent.button == 1 || objEvent.button == 3 || objEvent.button == 5)
			{
				objReturn.button = 1;
			}
			else if(objEvent.button == 2 || objEvent.button == 6)
			{
				objReturn.button = 2;
			}
			else if(objEvent.button == 4)
			{
				objReturn.button = 3;
			}
		}
		else
		{
			// Not IE
			if(objEvent.button == 0)
			{
				objReturn.button = 1;
			}
			else if(objEvent.button == 2)
			{
				objReturn.button = 2;
			}
			else if(objEvent.button == 1)
			{
				objReturn.button = 3;
			}
		}
	}
	
	return objReturn;
}

function windowScreenX()
{
	if(window.screenLeft) return window.screenLeft; else return window.screenX;
}

function windowScreenY()
{
	if(window.screenTop) return window.screenTop; else return window.screenY;
}

function addEvent(objElement, evType, fnHandler){	if (objElement.addEventListener)	{
		objElement.addEventListener(evType, fnHandler, false);
		return true;
	}	else if (objElement.attachEvent)	{
		var blnReturn = objElement.attachEvent("on" + evType, fnHandler);
		return blnReturn;	}	else	{
		return false;	}
}
function removeEvent(objElement, evType, fnHandler, blnCapture){	if (objElement.removeEventListener)	{
		objElement.removeEventListener(evType, fnHandler, blnCapture);
		return true;	}	else if (objElement.detachEvent)	{
		var blnReturn = objElement.detachEvent("on" + evType, fnHandler);
		return blnReturn;	}	else	{
		alert("Handler could not be removed");
	}
}

function getViewportHeight(){
	if (window.innerHeight	!=	window.undefined) return window.innerHeight;
	if (document.compatMode	==	'CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}
function getViewportWidth(){
	if (window.innerWidth != window.undefined) return window.innerWidth; 
	if (document.compatMode == 'CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return window.undefined; 
}
function getVScrollPos()
{	var scTop = 0;
	if( typeof( window.pageYOffset ) == 'number' )
	{
		//Netscape compliant
		scTop = window.pageYOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
	{
		//DOM compliant
		scTop = document.body.scrollTop;
	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
	{
		//IE6 standards compliant mode
		scTop = document.documentElement.scrollTop;
	}
	
	return scTop;
}

function getHScrollPos()
{
	var scLeft = 0;	if( typeof( window.pageYOffset ) == 'number' )
	{
		//Netscape compliant
		scLeft = window.pageXOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
	{
		//DOM compliant
		scLeft = document.body.scrollLeft;
	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
	{
		//IE6 standards compliant mode
		scLeft = document.documentElement.scrollLeft;
	}
	
	return scLeft;
}

function bcHideSelectBoxes(){	if(navigator.userAgent.indexOf("MSIE") != -1)
	{
		var arrSelects = bcGetElementsByTagName("SELECT");		for(var i = 0; i < arrSelects.length; i++)		{
			arrSelects[i].style.visibility = "hidden";		}	}
}

function bcShowSelectBoxes(){
	if(navigator.userAgent.indexOf("MSIE") != -1)
	{
		var arrSelects = bcGetElementsByTagName("SELECT");		for(var i = 0; i < arrSelects.length; i++)		{
			arrSelects[i].style.visibility = "visible";		}	}
}

function translateParameters(strParameters)
{
	var strOutput = strParameters;
	
	// Check to see if the parameter list is using the showModelessDialog format
	if((strOutput.indexOf(":") != -1) && (strOutput.indexOf(";") != -1))
	{
		strOutput = translateShowModalDialogParameters(strOutput);
	}

	var arrOutput = split(strOutput, ",");
	var nWidth = 0, nHeight = 0;
	for(nLocation = 0; nLocation < arrOutput.length; nLocation++)
	{
		if(arrOutput[nLocation].toString().indexOf("width=") != -1)
		{
			nWidth = Replace(Replace(arrOutput[nLocation], "width=", ""), "px", "");
		}
		if(arrOutput[nLocation].toString().indexOf("height=") != -1)
		{
			nHeight = Replace(Replace(arrOutput[nLocation], "height=", ""), "px", "");
		}
	}	

	// center will be handled by translating it to a left and top
	if((strParameters.toString().indexOf("center=yes") != -1) || (strParameters.toString().indexOf("center:yes") != -1))
	{
		var nScreenX = windowScreenX(), nScreenY = windowScreenY();
		var nInnerWidth = getViewportWidth(), nInnerHeight = getViewportHeight();
		strOutput += ",left=" + (nScreenX + (nInnerWidth / 2) - (nWidth / 2)) + "px";
		strOutput += ",top=" + (nScreenY + (nInnerHeight / 2) - (nHeight / 2)) + "px";
	}

	return strOutput;
}

function translateShowModalDialogParameters(strParameters)
{
	var nLocation, nCommaLocation;
	// First remove all spaces so that we make sure to get the correct replacements
	var strOutput = Replace(strParameters, " ", "");
	
	// Change all : to = and ; to , 
	strOutput = Replace(strOutput, ":", "=");
	strOutput = Replace(strOutput, ";", ",");

	// Strip end and beginning commas so we have a known to deal with	
	if(strOutput.substring(0, 1) == ",")
	{
		strOutput = strOutput.substring(1, strOutput.length);
	}
	if(strOutput.substring(strOutput.length - 1, strOutput.length) == ",")
	{
		strOutput = strOutput.substring(0, strOutput.length - 1);
	}

	// Change all yes and on to 1 and no and off to 0
	strOutput = Replace(strOutput, "1,", "yes,");
	strOutput = Replace(strOutput, "on,", "yes,");
	strOutput = Replace(strOutput, "0,", "no,");
	strOutput = Replace(strOutput, "off,", "no,");
	
	// Change the scroll to scrollbars and height width too
	strOutput = Replace(strOutput, "scroll=", "scrollbars=");
	strOutput = Replace(strOutput, "dialogHeight=", "height=");
	strOutput = Replace(strOutput, "dialogWidth=", "width=");
	
	// left and right are supported but different for each browser
	if(window.screenX)
	{
		strOutput = Replace(strOutput, "dialogLeft=", "screenX=");
		strOutput = Replace(strOutput, "dialogTop=", "screenY=");
	}
	else
	{
		// Turns out this is IE...
		strOutput = Replace(strOutput, "dialogLeft=", "left=");
		strOutput = Replace(strOutput, "dialogTop=", "top=");
	}

	var arrOutput = split(strOutput, ",");
	var nWidth = 0, nHeight = 0;
	for(nLocation = 0; nLocation < arrOutput.length; nLocation++)
	{
		if(arrOutput[nLocation].toString().indexOf("width=") != -1)
		{
			nWidth = Replace(Replace(arrOutput[nLocation], "width=", ""), "px", "");
		}
		if(arrOutput[nLocation].toString().indexOf("height=") != -1)
		{
			nHeight = Replace(Replace(arrOutput[nLocation], "height=", ""), "px", "");
		}
	}	

	var arrOutput = split(strOutput, ",");
	// center, dialogHide, help, edge and unadorned are not supported and will be removed
	for (nLocation = 0; nLocation < arrOutput.length; nLocation++)
	{
		if(arrOutput[nLocation].toString().indexOf("center") != -1)
		{
			arrOutput[nLocation] = "";
		}
		if(arrOutput[nLocation].toString().indexOf("dialogHide") != -1)
		{
			arrOutput[nLocation] = "";
		}
		if(arrOutput[nLocation].toString().indexOf("help") != -1)
		{
			arrOutput[nLocation] = "";
		}
		if(arrOutput[nLocation].toString().indexOf("edge") != -1)
		{
			arrOutput[nLocation] = "";
		}
		if(arrOutput[nLocation].toString().indexOf("unadorned") != -1)
		{
			arrOutput[nLocation] = "";
		}
	}
	strOutput = Replace(arrOutput.toString(), ",,", ",");
	if(strOutput.substring(0, 1) == ",")
	{
		strOutput = strOutput.substring(1, strOutput.length);
	}
	if(strOutput.substring(strOutput.length - 1, strOutput.length) == ",")
	{
		strOutput = strOutput.substring(0, strOutput.length - 1);
	}
	
	// showModelessDialog always turns off toolbars, locations, menubars and directories
	// Unless they are already turned on, turn them off.
	if(strParameters.indexOf('toolbar') != -1)
	{
		strOutput += ',toolbar=0';
	}
	if(strParameters.indexOf('location') != -1)
	{
		strOutput += ',location=0';
	}
	if(strParameters.indexOf('menubar') != -1)
	{
		strOutput += ',menubar=0';
	}
	if(strParameters.indexOf('directories') != -1)
	{
		strOutput += ',directories=0';
	}
		
	return strOutput;
}

function refocusWindow(objEvent)
{
	if(objGlobalWindow != null)
	{
		if(!objGlobalWindow.closed)
		{
			objGlobalWindow.focus();
		}
	}
}

function openCoverUp()
{
	var objDiv = document.createElement('div');
	objDiv.id = 'divCoverUp';
	objDiv.name = 'divCoverUp';
	objDiv.style.position = 'absolute';

	if(navigator.userAgent.toString().indexOf("MSIE") != -1)
	{	
		objDiv.style.backgroundImage = 'none';
		objDiv.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/images/overlay.png", sizingMethod="scale")';
	}
	else
	{
		objDiv.style.backgroundImage = 'url(/images/overlay.png)';
	}
	objDiv.style.visibility = 'visible';
	objDiv.style.left = '0px';
	objDiv.style.top = '0px';

	if(window.innerWidth)
	{
		objDiv.style.width = window.innerWidth;
		objDiv.style.height = window.innerHeight;
	}
	else
	{
		objDiv.style.width = window.document.body.clientWidth;
		objDiv.style.height = window.document.body.clientHeight;
	}

	addEvent(objDiv, "focus", refocusWindow);
	addEvent(objDiv, "click", refocusWindow);	

	try
	{
		objDiv.captureEvents(Event.FOCUS|Event.CLICK);
	}
	catch(e)
	{
	}

	bcHideSelectBoxes();
	
	document.body.appendChild(objDiv);
}

function closeCoverUp()
{
	var objDiv = bcGetElementById("divCoverUp");
	if(objDiv != null)
	{
		if(objGlobalWindow == null || objGlobalWindow.closed)
		{
			removeEvent(objDiv, "focus", refocusWindow, false)
			removeEvent(objDiv, "click", refocusWindow, false)
			document.body.removeChild(objDiv);
			bcShowSelectBoxes();
		}
		else
		{
			setTimeout("closeCoverUp();", 1);
		}
	}
}

function openDialog(strURL, objReference, strParameters)
{
	strParameters = translateParameters(strParameters);
	openCoverUp();		
	objGlobalReference = objReference;
	objGlobalWindow = window.open(strURL, "newWin", strParameters);
	objGlobalWindow.focus();
	setTimeout('closeCoverUp();', 1000);
}
