/*==============================================================================
* FILENAME site.js
* PURPOSE This file contains the code that are utility functions for the site.
* (c) Copyright Bentley Systems, Inc. 2002-2009
*=============================================================================*/
var agt = navigator.userAgent.toLowerCase();
var ns4 = (document.layers) ? true:false;
var ie4 = (document.all) ? true:false;
var ie5 = ((agt.indexOf('msie 5') > 0)) ? true:false;
var ie6 = ((agt.indexOf('msie 6') > 0)) ? true:false;
var ie7 = ((agt.indexOf('msie 7') > 0)) ? true:false;
var ie8 = ((agt.indexOf('msie 8') > 0)) ? true:false;
var ns6 = ((agt.indexOf('netscape6' )>0) && (document.getElementById)) ? true:false;
var ns7 = ((agt.indexOf('netscape/7')>0) && (document.getElementById)) ? true:false;
var ns8 = ((agt.indexOf('netscape/8')>0) && (document.getElementById)) ? true:false;
var ns9 = ((agt.indexOf('navigator/9')>0) && (document.getElementById)) ? true:false;
var fox = ((agt.indexOf('firefox/')>0)) ? true:false;
var fox2 = ((agt.indexOf('firefox/2')>0)) ? true:false;
var fox3 = ((agt.indexOf('firefox/3')>0)) ? true:false;
var chrome = ((agt.indexOf('chrome/')>0)) ? true:false;
var safari = ((agt.indexOf('safari/')>0)) ? true:false;

if (fox || ns8 || ns9 || safari || chrome) 
	ns7 = true;

if (ie7 || ie8)
    ie4 = true;

var YPrompt;

function swapImage(imgname, image)	{
	if (ns4) document.images[imgname].src = image;
	else if (ie4 || ns6 || ns7) document.images[imgname].src = image;
}

function showlayer(layerobj)
{
	if (ns4){
		if(document.layers[layerobj])
			document.layers[layerobj].visibility = "show";
	}
	else if (ie4){
		if(document.all[layerobj])
			document.all[layerobj].style.visibility = "visible";
	}
	else if (ns6 || ns7){
		if(document.getElementById(layerobj))
			document.getElementById(layerobj).style.visibility = "visible";
	}
}

function hidelayer(layerobj)
{
	if (ns4){
		if(document.layers[layerobj])
			document.layers[layerobj].visibility = "hide";
	}
	else if (ie4){
		if(document.all[layerobj])
			document.all[layerobj].style.visibility = "hidden";
	}
	else if (ns6 || ns7){
		if(document.getElementById(layerobj))
			document.getElementById(layerobj).style.visibility = "hidden";
	}
}

function movelayer(layerobj,top)
{
	if (ns4) document.layers[layerobj].top = top
	else if (ie4) document.all[layerobj].style.top = top
	else if (ns6 && ns7) document.getElementById(layerobj).style.top = top
}

function layerWrite(id,nestref,text)
{
	if (ns4) {
		var lyr = (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : document.layers[id].document
		lyr.open()
		lyr.write(text)
		lyr.close()
	}
	else if (ie4) document.all[id].innerHTML = text;
	else if (ns6 || ns7) document.getElementById(id).innerHTML = text;
}

function collapseLayer(layerobj)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		if(document.getElementById(layerobj)){
			var style2 = document.getElementById(layerobj).style;
			style2.display = "";
		}
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		if(document.all[layerobj]){
			var style2 = document.all[layerobj].style;
			style2.display = "";
		}
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		if(document.layers[layerobj]){
			var style2 = document.layers[layerobj].style;
			style2.display = "";		
		}
	}
}

function expandLayer(layerobj)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		if(document.getElementById(layerobj)){
			var style2 = document.getElementById(layerobj).style;
			style2.display = "block";
		}
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		if(document.all[layerobj]){
			var style2 = document.all[layerobj].style;
			style2.display = "block";
		}
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		if(document.layers[layerobj]){
			var style2 = document.layers[layerobj].style;
			style2.display = "block";
		}
	}
}

/**********************************************
Flag that sets the prompt to warn of overwrite

************************************************/
function onPrompt()
{
	var temp
	if(ns4) temp = document.VehicleLyr;
	else if (ie4 || ns6 || ns7) temp = document.VehicleForm;
	if (temp)
	{	
		YPrompt = true; 
		if (ie4 || ns6 || ns7) document.mainForm.YPrompt.value = true;
		else if (ns4) document.mainLyr.document.mainForm.YPrompt.value = true;
		return;
	}
}

function setStatus(msg){
    status = msg
    return true
}

function PermitTypeCompare(PTypeList, ptype)
{
	var typelist = new Array();

	typelist = PTypeList.split('##');
		
	for(j=0;j<typelist.length;j++){
		if(typelist[j] == ptype)
			return(true);
	}
	return(false);
}

function drawMapView(jreProtocol, urlName, serverName, serverPort, ctlServer, ctlDir, appletIE, ht)
{
	if(ie5 || ie6 || ie7 || ie8)
	{
        if(appletIE)
        {
		    document.writeln('<OBJECT'); 
		    document.writeln('	classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"  ');
		    document.writeln('	id="Slactvx1" ');
		    document.writeln('	width="100%" ');
		    document.writeln('	height="'+ht+'"  ');
		    document.writeln('	codebase="'+jreProtocol+'://java.sun.com/update/1.5.0/jinstall-1_5_0_11-windows-i586.cab#Version=1,5,0,11">');
		    document.writeln('	<PARAM NAME = CODE VALUE = "CWBJava.MapVwApp.MapVwApp.class">');
		    document.writeln('	<PARAM NAME = CODEBASE VALUE = "'+ctlServer+'/'+ctlDir+'" >');
		    document.writeln('	<PARAM NAME = ARCHIVE VALUE = "mapview.jar" >');
		    document.writeln('	<PARAM NAME="type" VALUE="application/x-java-applet;version=1.#">');
		    document.writeln('	<PARAM NAME="name" value="MapView">');
		    document.writeln('	<PARAM NAME="scriptable" VALUE="true">');
		    document.writeln('  <center>');
            document.writeln('<p><strong>The SUPERLOAD map requires Java 1.5.0_11 or higher, which your browser does not appear to have.</strong></p> ');
            document.writeln('    <p><a href="http://www.java.com/en/download/index.jsp">Get the latest Java Plug-in.</a></p> ');
            document.writeln('  </center>');
		    document.writeln('</OBJECT>');
	    }
	    else
	    {
		    document.write('     <object classid="clsid:7A16F968-8E79-11D4-AFC3-0060978DD938"');
		    document.write('      codebase="'+urlName+'://'+serverName+':'+serverPort+'/'+ctlDir+'/slactvx.cab#version=1,7,8,249"');
		    document.write('      id="Slactvx1" ');
		    document.write('      width="100%" ');
		    document.write('      height="'+ht+'"> ');
		    document.write('	   <br><br><br><br><br><p align="center" class="errmsg">Map control is not installed</p>');
		    document.write('	 <p class="basictext">Depending on how your browser is setup, you may need to click on the Information Bar (located below the URL textbox) in the browser or you may be prompted with a security dialog box to download and install the map control</p>');
		    document.write('	 <p class="basictext">If you cancelled the download and installation of the map control please click on the Back button on the web page and submit the application again.</p>');
		    document.write('     <p><span class="smalltext">');
		    document.write('            Select ');
		    document.write('            <a href="javascript:onClick=onClickReq();">Here</a> to view the complete list of system requirements for this site.</span>	 ');
		    document.write('</p><br><br><br><br><br></object>  ');
        }	    
	}
	else
	{
	    document.writeln('<object');
        document.writeln('  type="application/x-java-applet;version=1.5" ');
		document.writeln('	id="Slactvx1" ');
		document.writeln('	name="Slactvx1" ');
        document.writeln('  height="'+ht+'" ');
        document.writeln('  width="100%"> ');
		document.writeln('	<param name="code" value="CWBJava.MapVwApp.MapVwApp.class">');
        document.writeln('	<param NAME = CODEBASE VALUE = "'+urlName+'://'+serverName+':'+serverPort+'/'+ctlDir+'/" >');
		document.writeln('	<param name="archive" value="mapview.jar" >');
		document.writeln('	<param name="type" value="application/x-java-applet;version=1.5">');
		document.writeln('	<param name="name" value="MapView">');
		document.writeln('	<param name="mayscript" value="true">');        
		document.writeln('	<param name="scriptable" value="true">');        
		document.writeln('  <center>');
        document.writeln('<p><strong>The SUPERLOAD map requires Java 1.5.0_11 or higher, which your browser does not appear to have.</strong></p> ');
        document.writeln('    <p><a href="http://www.java.com/en/download/index.jsp">Get the latest Java Plug-in.</a></p> ');
        document.writeln('  </center>');
        document.writeln('</object>');
	}
}

function drawMapARPS(urlName, serverName, serverPort, ctlDir)
{
	if (ie4){
		document.write('     <object classid="clsid:B36AC2F9-197E-4102-8618-B72A26B4B7BF"');
		document.write('      codebase="'+urlName+'://'+serverName+':'+serverPort+'/'+ctlDir+'/rtgenctlhttpmo.cab#version=1,0,0,001"');
		document.write('      id="Slactvx1" ');
		document.write('      width="630" ');
		document.write('      height="450"> ');
		document.write('	   <br><br><br><br><br><p align="center" class="errmsg">Map control is not installed</p>');
		document.write('	 <p class="basictext">Depending on how your browser is setup, you may need to click on the Information Bar (located below the URL textbox) in the browser or you may be prompted with a security dialog box to download and install the map control</p>');
		document.write('	 <p class="basictext">If you cancelled the download and installation of the map control please click on the Back button on the web page and submit the application again.</p>');
		document.write('     <p><span class="smalltext">');
		document.write('            Select ');
		document.write('            <a href="javascript:onClick=onClickReq();">Here</a> to view the complete list of system requirements for this site.</span>	 ');
		document.write('</p><br><br><br><br><br></object>  ');
	}
	else{
	}
}

function drawTruckView(urlName, serverName, serverPort, ctlDir)
{
	if (ie4)
	{
		document.write('<object classid="clsid:6989A24E-9716-11D5-AFC3-0060978DD938"');
		document.write('codebase="'+urlName+'://'+serverName+':'+serverPort+'/'+ctlDir+'/trkview.cab#version=1,0,0,14"');
		document.write('id="TrkView"');
		document.write('align="baseline"');
		document.write('border="0"');
		document.write('width="632"');
		document.write('height="200">');
	    document.write('	   <br><br><br><br><br><p align="center" class="errmsg">Truck control is not installed</p>');
	    document.write('	 <p class="basictext">Depending on how your browser is setup, you may need to click on the Information Bar (located below the URL textbox) in the browser or you may be prompted with a security dialog box to download and install the truck control</p>');
	    document.write('	 <p class="basictext">If you cancelled the download and installation of the truck control please click on the Back button on the web page and submit the application again.</p>');
	    document.write('     <p><span class="smalltext">');
	    document.write('            Select ');
	    document.write('            <a href="javascript:onClick=onClickReq();">Here</a> to view the complete list of system requirements for this site.</span>	 ');
	    document.write('</p><br><br><br><br><br></object>  ');
	}
	else
	{
	    document.writeln('<object');
        document.writeln('  type="application/x-java-applet;version=1.5" ');
		document.writeln('	id="TrkView" ');
		document.writeln('	name="TrkView" ');
        document.writeln('  height="200" ');
        document.writeln('  width="632"> ');
		document.writeln('	<param name="code" value="CWBJava.TruckView.TrkVwApp.class">');
        document.writeln('	<param NAME = CODEBASE VALUE = "'+urlName+'://'+serverName+':'+serverPort+'/'+ctlDir+'/" >');
		document.writeln('	<param name="archive" value="TrkView.jar" >');
		document.writeln('	<param name="type" value="application/x-java-applet;version=1.5">');
		document.writeln('	<param name="name" value="TrkView">');
		document.writeln('	<param name="mayscript" value="true">');        
		document.writeln('	<param name="scriptable" value="true">');        
		document.writeln('  <center>');
        document.writeln('<p><strong>The SUPERLOAD truck applet requires Java 1.5.0_11 or higher, which your browser does not appear to have.</strong></p> ');
        document.writeln('    <p><a href="http://www.java.com/en/download/index.jsp">Get the latest Java Plug-in.</a></p> ');
        document.writeln('  </center>');
        document.writeln('</object>');
	}
}

function checkTrkControl()
{
	var trkcontrol;
	var trkplugin;
	var hasActiveX = false;
	try{
		if(window.ActiveXObject)
		{
			hasActiveX = true;
			trkcontrol = new ActiveXObject("TRKVIEW.TrkViewCtrl.1");
		}
		else
		{
			trkcontrol = document.getElementById('TrkView');
		}
	}
	catch(oError){
	}

/***
    try{
    	if(navigator.mimeTypes)
        {
        	trkplugin = navigator.mimeTypes['application/x-eskeractivex'].enabledPlugin; 
        } 
    }
    catch (oError){
    }	
**/
    
	if(!trkcontrol)
		return false;
	
	return true;
}

function getData(url,qString, cmd) {
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Cannot create an XMLHTTP instance');
		return false;
	}

    if (cmd == 'EndDate')
	    http_request.onreadystatechange =  function() { processEndDateXML(http_request); };
	else if (cmd == 'AddVehicles')
	    http_request.onreadystatechange =  function() { processAddVehiclesXML(http_request); };
	else if (cmd == 'getVoid')
	    http_request.onreadystatechange =  function() { processVoidInfoXML(http_request); };
	else
    	http_request.onreadystatechange =  function() { processXML(http_request); };
	    
	if (qString) { // POST
		http_request.open('POST', url, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  //For POST Method
        http_request.setRequestHeader("Content-length", qString.length);
		http_request.send(qString);
	}
	else {
		http_request.open('GET', url, true);
		http_request.send(null);
	}
}

function xml_encode(input)
{	
	if(input == undefined)	
	{
		alert("error in xml_encode: input undefined");	
		return;
	}
	
	var replace_with	=	'&amp;';
	// The 'g' in the first argument is used to tell the function 'replace' 
	// that all occurences (g = global)
	// of the character in between slashes have to be replaced.
	input 				= 	input.replace(/&/g,	replace_with);
	
	replace_with		=	'&lt;';
	input 				= 	input.replace(/</g,	replace_with);
	
	replace_with		=	'&gt;';
	input 				= 	input.replace(/>/g,	replace_with);
	
//	replace_with		=	'&apos;';
//	input 				= 	input.replace(/'/g,	replace_with);
		
	replace_with		=	'&quot;';
	input 				= 	input.replace(/"/g,	replace_with);
	
	return input;
}			

function getCookie(c_name)
{
	if (document.cookie.length>0) {
  		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
	    	c_start=c_start + c_name.length+1;
   			c_end=document.cookie.indexOf(";",c_start);
   			if (c_end==-1) 
   				c_end=document.cookie.length;
   			return unescape(document.cookie.substring(c_start,c_end));
   		} 
  	}
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function setCursor(cursorType)
{
	if(ie5 || ie6 || ie7 || ie8)
		document.styleSheets[0].rules[0].style.cursor = cursorType;
	else
		document.styleSheets[0].cssRules[0].style.cursor  = cursorType;
}

