var req = null;
var xmlProcessingFunction = null;
var divName = null;
var fixOverflow = false;
var isSafari = navigator.userAgent.indexOf("Safari") > 0;

function Initialize(divToChange)
{
	try
	{
		divName = divToChange;
		req=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			req=null;
		}
	}

	if(!req&&typeof XMLHttpRequest!="undefined")
	{
		req=new XMLHttpRequest();
	}
	
}
function SendQueryToServerAndFixLayer(url, divToChange, fixLayer) {	
	fixOverflow = fixLayer;	
	_SendQueryToServer(url,divToChange);
}


function SendQueryToServer(url, divToChange)
{	
	fixOverflow = false;	
	_SendQueryToServer(url,divToChange);
	
}

function _SendQueryToServer(url, divToChange)
{	

	Initialize(divToChange);	

	if(req!=null)
	{

		req.onreadystatechange = Process;

		req.open("GET", url, true);
		
		//Safari 1.3 fix only
		if (IsSafari13()) {
			req.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
		}
		
        req.send(null);
        
	}
	
}

function Process()
{
	if (req.readyState == 4 && divName != null) 
        {
        // only if "OK"
			if (req.status == 200) 
			{
				if(req.responseText=="") {
					HideDiv(divName);
				}
				else
				{
					//Check to be sure that the login page hasn't been returned
					// and therefore the user has been timed out
					if (req.responseText.indexOf("<PositiveWareLoginPage") > 0) {
						alert("Your PositiveWare session has timed out, please log in again.");
						document.location.href="../index.aspx";
					}
					
					ShowDiv(divName);					
					
					WriteLayer(divName,null,req.responseText);
					
					//Fix where the div pops up so that it doesn't fly off-screen
					if (fixOverflow) {
						fixLayerWindowOverflow(divName);
					}
					
				}
				
				this.status = "";
			}
			else 
			{
				this.status = "AJAX: There was a problem retrieving data: "+ req.statusText;
			}
		}
}

function SendQueryToServerForXML(url, functionObj) {
	Initialize(null);	

	if(req!=null)
	{
	
		req.onreadystatechange = ProcessXML;
		xmlProcessingFunction = functionObj;
		
		//Hit a page that will query the DB and pull back event info
		req.open("GET", url, true);
		
		//Safari 1.3 fix only
		if (IsSafari13()) {
			req.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
		}
		
        req.send(null);
        
	}
}

function ProcessXML() {
	if (req.readyState == 4) 
    {    
	    // only if "OK"
		if (req.status == 200) 
		{
			if(req.responseText !="") {
				//Check to be sure that the login page hasn't been returned
				// and therefore the user has been timed out
				if (req.responseText.indexOf("<PositiveWareLoginPage") > 0) {
					alert("Your PositiveWare session has timed out, please log in again.");
					document.location.href="../index.aspx";
				}
				//Ensure that response XML gets loaded
				try {
					req.responseXML.loadXML(req.responseText);
				}
				catch (e){
				}
				
				var xmldoc = req.responseXML;
 				
 				if (xmlProcessingFunction) {
 					xmlProcessingFunction(xmldoc);
 				}
			}						
		}
	}
}

function ShowDiv(divid) 
{
   if (document.layers) {
	document.layers[divid].visibility="show";
   }	
   else { 	
	document.getElementById(divid).style.display = "inline";	
   }
}

function ShowLayer(layerid) {	
	if (getLayer(layerid)) {
		ShowDiv(layerid);		
	}	
}

/*
 * ShowLayerAsBlock - Generally, when we show a div, it's inline
 * Safari and Firefox hate this... so switch it to block
 */
function ShowLayerAsBlock(layerid) {	
	if (getLayer(layerid)) {
		ShowDiv(layerid);
		getLayer(layerid).style.display = "block";
	}	
}


function HideDiv(divid) 
{
   if (document.layers) {
	document.layers[divid].visibility="hide";
   }
   else {	
	document.getElementById(divid).style.display = "none";
   }
}

function HideLayer(layerid) {
	if (getLayer(layerid)) {
		HideDiv(layerid);
	}
}
function IsVisible(layerid) {
	var retValue = false;
	if (document.layers) {
		if (document.layers[layerid]) {
			retValue = document.layers[layerid].visibility != "hide";
		}
	}
	else {	
		if (document.getElementById(layerid) && document.getElementById(layerid).style) {
			retValue = document.getElementById(layerid).style.display != "none";
		}
	}	
	
	return retValue;
}

function ToggleLayer(layerid) {
	if (getLayer(layerid)) {
		if (getLayer(layerid).style.display == "none") {
			ShowLayer(layerid);
		}
		else {
			HideLayer(layerid);			
		}
	}
	
}

function getLayer(layerid) 
{
   if (document.layers) {
	return document.layers[layerid];
   }
   else {
	return document.getElementById(layerid);
   }
}

function WriteLayer(ID,parentID,sText) { 
	//Unsure of what browser uses this block
	if (document.layers) { 
	   
		var oLayer; 
	   
		if(parentID){ 
			oLayer = eval('document.' + parentID + '.document.' + ID + '.document'); 
		}
		else{ 
			oLayer = document.layers[ID].document; 
		} 
	   
		oLayer.open(); 
		oLayer.write(sText); 
		oLayer.close(); 
	} 
	//Firefox
	//Netscape
	else if (parseInt(navigator.appVersion)>=5&&navigator.appName=="Netscape") { 
		document.getElementById(ID).innerHTML = sText; 
	} 
	//IE
	else if (document.all) {
		document.getElementById(ID).innerHTML = sText; 
	}
} 

function FindPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}

	//Adjust for Safari
	if (isSafari) curleft += 7;
	
	return curleft;
}

function FindPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	
	//Adjust for Safari
	if (isSafari) curtop += 7;
	
	return curtop;
}
function GetWindowHeight() {
        var winH = 0;
        if (parseInt(navigator.appVersion)> 3) {
                if (navigator.appName=="Netscape") {
                        winH = window.innerHeight;
                }
                if (navigator.appName.indexOf("Microsoft")!=-1) {
                        winH = document.body.offsetHeight;
                }
        }

        return winH;
}
function GetWindowWidth() {
        var winW = 0;
        if (parseInt(navigator.appVersion)> 3) {
                if (navigator.appName=="Netscape") {
                        winW = window.innerWidth;
                }
                if (navigator.appName.indexOf("Microsoft")!=-1) {
                        winW = document.body.offsetWidth;
                }
        }

        return winW;
}
function getForm() {
	var theform;
		
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
		theform = document.Form1;
	}
	else {
		theform = document.forms["Form1"];
	}
	
	return theform;
}

function getFormElement(elementName) {
	var theForm = getForm();
	
	if (theForm) {	 	
		return theForm[elementName];
	}
	else {
		return null;
	}
}


function fixLayerWindowOverflow(layerName) {
	var layer = getLayer(layerName);
	
	if (layer) {
		//Does the layer stretch outside of the window's bounds?
		if (document.body.offsetWidth < (layer.offsetLeft + layer.offsetWidth)) {
			layer.style.left = document.body.offsetWidth - layer.offsetWidth - 25;
		}
		
		
		//Bottom		
		if ((document.body.clientHeight + document.body.scrollTop) < (layer.offsetTop + layer.offsetHeight)) {			
			layer.style.top = document.body.clientHeight - layer.offsetHeight - 25;			
			
			if (layer.onmouseover) {
				layer.onmouseover();
			}
		}
		
	}		
}


function fillScreenWithLayer(layerName) {
	var layer = getLayer(layerName);
	
	if (layer) {
		//Stretch the layer to fill the screen		
		layer.style.height=	document.body.clientHeight - layer.offsetTop;		
	}		
}

function IsSafari13 () {
	//Figure out whether this is a Safari 1.3 browser
	var userAgent = navigator.userAgent.toLowerCase();
	
	if (userAgent.indexOf("312.1")) {	
		return true;
	}
	else {	
		return false;	
	}
	
}

function ToggleDropDownLists(layer, layerVisible) {
   for (var j=0;j<document.forms.length;j++) {	
         for (var i=0;i<document.forms[j].elements.length;i++) {         
			if (document.forms[j].elements[i].type.substr(0,6) == 'select') {						
				//For now, hide 'em all
				//if (BehindLayer(layer, document.forms[j].elements[i])) {			
					document.forms[j].elements[i].style.visibility = (layerVisible) ? 'hidden' : 'visible';
				//}
			}
         }
   }
}

function GetTagPixels(StartTag, Direction) {
   var PixelAmt = (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
   while ((StartTag.tagName != 'BODY') && (StartTag.tagName != 'HTML')) {
      StartTag = StartTag.offsetParent;
      PixelAmt += (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
   }
   return PixelAmt;
}

function BehindLayer(layer, element) {	
    var layerTop = GetTagPixels(layer, 'TOP');    
    var layerLeft = GetTagPixels(layer, 'LEFT');
    var layerRight = layerLeft + layer.offsetWidth;
    var layerBottom = layerTop + layer.offsetHeight;
	var elementTop = GetTagPixels(element, 'TOP');
	var elementLeft = GetTagPixels(element, 'LEFT');
	var elementRight = elementLeft + element.offsetWidth;
	var elementBottom = elementTop + element.offsetHeight;
   return (((elementTop < layerBottom) && (elementBottom > layerTop)) && ((elementLeft < layerRight) && (elementRight > layerLeft)));
	
}

function SwitchDropDownSelectedValue(dropDownID, newSelectedValue) {
	if (document.getElementById(dropDownID)) {

		var dropDown = document.getElementById(dropDownID);

		for(var i=0; i < dropDown.options.length; i++) {
			if (dropDown.options[i].value == newSelectedValue) {
				dropDown.selectedIndex = i;
			}
		}
	
	}
}

function GetDropDownSelectedValue(dropDownID) {	

		var theForm = getForm();
		var dropDown = theForm[dropDownID];

	if (dropDown) {
		return dropDown.options[dropDown.selectedIndex].value;
	}
}


function SetRadioListSelectedValue(radioListID, newSelectedValue) {	

	var theForm = getForm();
	var radioList = theForm[radioListID];
	
	if (radioList) {

		for(var i=0; i < radioList.length; i++) {		
			if (radioList[i].value == newSelectedValue) {
				radioList[i].checked = true;
			}
			else {
				radioList[i].checked = false;
			}
		}
	}

}

function GetRadioListSelectedValue(radioListID) {	

		var theForm = getForm();
		var radioList = theForm[radioListID];

	if (radioList) {
		for(var i=0; i < radioList.length; i++) {		
			if (radioList[i].checked) {
				return radioList[i].value;
			}
		}
	}
}

function ResizeToBottomOfWindow(obj, marginBottom) {
	//Figure out the window height and the current position of obj	
	var winH = GetWindowHeight();	
	var objY = FindPosY(obj);
	
	if (winH && objY) {
		//Set the height of the object appropriately - use 5 px padding
		obj.style.height= (winH - objY - marginBottom - 5) + 'px';
	}	
}

function replaceParameter(URL, paramName, newParamValue) {
	URL = removeParameter(URL, paramName);
	URL = addParameter(URL, paramName, newParamValue);
	
	return URL;
}

function removeParameter(URL, paramName) {
        //Find any previous paramName and replace it with the current
        //setting
        if (URL.indexOf(paramName) > 0) {
                var startPos = URL.indexOf(paramName);
                var endPos = URL.indexOf("&",startPos);

                //It could be at the end of the url
                //If so, just use everyting up to right before paramName
                if (endPos < 0) {
                        URL = URL.substring(0,startPos - 1);
                }
                //Otherwise make up the new url with the pieces around paramName
                else {
                        URL = URL.substring(0,startPos) +  URL.substring(endPos + 1);
                }

        }

        return URL;
}

function addParameter(URL, paramName, paramValue) {
	//Simply adds a parameter to the URL
	if (URL.indexOf("?") > 0) {
		URL += "&";
	}
	else {
		URL += "?";	
	}
	
	URL += paramName + "=" + escape(paramValue);
	
	return URL;

}

function sendNote(name, lastLetter) {
	//This may or may not deter all screen-scraping 
	// spammers, but it should work most of the time
	var url = "mai";
	url += "lto:";
	url += name + lastLetter + "@ch";
	url += "eley.com";
	document.location.href=url;
}

function displayContact(fp2, lp2, lp1, fp1) {
	var url = fp1 + fp2 + "@" + lp1 + lp2;

	document.write('<a href="javascript:void(0);" onclick="openContact(');
	document.write("'"+fp2+"','"+lp2+"','"+lp1+"','"+fp1+"'")
	document.write(');">'+url+'</a>');
}

//OpenContact constructs the email address as follows:
//fp1 + fp2 + @ + lp1 + lp2
function openContact(fp2, lp2, lp1, fp1) {
	//This may or may not deter all screen-scraping 
	// spammers, but it should work most of the time
	var url = "mai";
	url += "lto:";
	url += fp1 + fp2 + "@" + lp1 + lp2;

	document.location.href=url;
}