// JavaScript Document

//if (document.all){}
//else document.captureEvents(Event.MOUSEMOVE);
document.onmousemove=mtrack;

function mtrack(e) {
	var x,y,x1,x2,y1,y2,dx=0,dy=0;
	WidthDiff=1200/530;
	FishEye=110;
	border=1;
	
	if (document.all) {
	   x=event.x;
	   y=event.y;
	   dx=window.document.body.scrollLeft;
	   dy=window.document.body.scrollTop;
	} else { 
	   x=e.pageX;
	   y=e.pageY;
	}
	
	x1=-FishEye+(x+dx)*WidthDiff;	//left
	y1=-FishEye+(y+dy)*WidthDiff;	//top
	x2=+FishEye+(x+dx)*WidthDiff;	//right
	y2=+FishEye+(y+dy)*WidthDiff;	//bottom
	
	document.images.bigone.style.left=(x+dx)*(1-WidthDiff);
	document.images.bigone.style.top=(y+dy)*(1-WidthDiff);
	document.images.bigone.style.clip="rect(" +y1 +"px," +x2 +"px," +y2 +"px," +x1 +"px)";
}

// External Code to be placed in tollCalc.js

var xmlHttp;

// include project-specific functions here

function pickStart(start) {
    // get list items selected

    document.getElementById("startTrip").value = start;
    filterEnds(start);
	
    SetFocus("startTrip");
	
	document.getElementById("endTrip").disabled = false;
	SetFocus("endTrip"); 
	
	UpdateMap(start);
}

function filterEnds(startPoint) {
//alert("filterEnds");
	xmlHttp=GetXmlHttpObject();

    if (xmlHttp==null) {
        alert ("Your browser does not support AJAX!");
        return;
    }
    
    // reset calculator results
    document.getElementById("calcResults").value="$0.00";
	ResetToggle(false);
    
    var url="include/filterEndPoints.php?startPoint=" + startPoint;
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=updateList;
    xmlHttp.send(null);
}

function updateList() {
	if (xmlHttp.readyState==4)  {
        var endList = document.getElementById("endTrip");

        // Clears the end points combo box contents.
        for (var count = endList.options.length - 1; count >= 0; count--) {
            endList.options[count] = null;
        }

        endList.selectedIndex = -1;

		var endNodes = xmlHttp.responseText.split(":");

        var itemValue;
        var itemText;
        var optionItem;

        optionItem = new Option("Please select an end point", "0", false, false);
        endList.options[endList.length] = optionItem;
 
        // Add possible end nodes list to the end points combo box.
        for (var count = 0; count < endNodes.length; count++) {
            itemText = (endNodes[count]);
            itemValue = (endNodes[++count]);

            optionItem = new Option(itemText, itemValue, false, false);
            endList.options[endList.length] = optionItem;
        }
    }
}

function UpdateMap(NodeID) {
	document.images.littleone.src = "include/create_map.php?node=" + NodeID + "&nodetype=X&mapsize=sm";
	document.images.bigone.src = "include/create_map.php?node=" + NodeID + "&nodetype=X&mapsize=lg";
	xmlHttp2=GetXmlHttpObject();	
	
	if (xmlHttp2==null) {
        alert ("Your browser does not support AJAX!");
        return;
    }


	document.getElementById("LoadingPleaseWait").style.visibility = "visible";
	xmlHttp2.onreadystatechange=PopImgMap;
	xmlHttp2.open("GET","include/EndCoords.php?startPoint=" + NodeID,true);
	xmlHttp2.send(null);

}

function PopImgMap() {
	var sResults;
	if(xmlHttp2.readyState==4) {
		sResults = xmlHttp2.responseText;
		document.getElementById("ImgMapCoords").innerHTML = sResults;
	}
//	document.getElementById("LoadingPleaseWait").style.visibility = "hidden";
}

function pickEnd(NodeID) {
	document.getElementById("endTrip").value = NodeID;
	calcToll();
}

function calcToll() {
    xmlHttp=GetXmlHttpObject();

    if (xmlHttp==null) {
        alert ("Your browser does not support AJAX!");
        return;
    }

    // get list items selected
    var startList = document.getElementById("startTrip");
    var startPoint = startList.options[startList.selectedIndex].value;

    var endList = document.getElementById("endTrip");
    var endPoint = endList.options[endList.selectedIndex].value;

    var bodyList = document.getElementById("body");
    var vehicleBody = bodyList.options[bodyList.selectedIndex].value;

    var classList = document.getElementById("class");
    var vehicleClass = classList.options[classList.selectedIndex].value;

    var paymentList = document.getElementById("payment");
    var paymentType = paymentList.options[paymentList.selectedIndex].value;

    // No work to be done if we have not selected a valid start/end point
    if (startPoint == "0" || endPoint == "0") {
        document.getElementById("calcResults").value="$0.00";
        return;
    }

    var url="include/calcToll.php";
    url=url+"?startPoint="+startPoint;
    url=url+"&endPoint="+endPoint;
    url=url+"&vehicleBody="+vehicleBody;
    url=url+"&vehicleClass="+vehicleClass;
    url=url+"&paymentType="+paymentType;
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=updateToll;
    xmlHttp.send(null);
}

function updateToll() {

    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        document.getElementById("calcResults").value=xmlHttp.responseText;
		//document.getElementById("calcResults").style.background="#FFFF00";
    }

}


//
// generic functions
//

function SetFocus(objectId) {
    document.getElementById(objectId).focus();
}


function GetXmlHttpObject() {
    var xmlHttp=null;

    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function ResetToggle(boolState) {
	if (boolState == false) {
		document.getElementById("btnReset").disabled=false;
		
	} else if (boolState == true) {
		window.location.href = unescape(window.location.pathname);

	}
	
}

/* Begin Tooltip Code */

var tooltip = false;
var tooltipMaxWidth = 500;
var tooltipMinWidth = 0;
var iframe = false;
var tooltip_is_msie = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('opera')==-1 && document.all)?true:false;

function showTooltip(e,tooltipTxt) {
	
	var bodyWidth = Math.max(document.body.clientWidth,document.documentElement.clientWidth) - 20;

	if(!tooltip){
		tooltip = document.createElement('DIV');
		tooltip.id = 'tooltip';
		
		document.body.appendChild(tooltip);
	
		if(tooltip_is_msie) {
			iframe = document.createElement('IFRAME');
			iframe.frameborder='5';
			iframe.style.backgroundColor='#FFFFFF';
			iframe.src = '#'; 	
			iframe.style.zIndex = 100;
			iframe.style.position = 'absolute';
			document.body.appendChild(iframe);
		} 

	}
	
	tooltip.style.display='block';
	if(tooltip_is_msie)iframe.style.display='block';
	
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
	var leftPos = e.clientX + 10;
	
	tooltip.style.width = null;	// Reset style width if it's set 
	tooltip.innerHTML = tooltipTxt;
	tooltip.style.left = leftPos + 'px';
	tooltip.style.top = e.clientY + 10 + st + 'px';

	if(tooltip.offsetWidth>tooltipMaxWidth) {	/* Exceeding max width of tooltip ? */
		tooltip.style.width = tooltipMaxWidth + 'px';
	}
	
	var tooltipWidth = tooltip.offsetWidth;		
	if(tooltipWidth<tooltipMinWidth)tooltipWidth = tooltipMinWidth;
	
	tooltip.style.width = tooltipWidth + 'px';

	if(tooltip_is_msie){
		iframe.style.left = tooltip.style.left;
		iframe.style.top = tooltip.style.top;
		iframe.style.width = tooltip.offsetWidth + 'px';
		iframe.style.height = tooltip.offsetHeight + 'px';
	}
}

function hideTooltip() {
	tooltip.style.display='none';
	if(tooltip_is_msie)iframe.style.display='none';		
}

/* End Tooltip Code */


