﻿// JScript File
var isSending = false;
var isDrawing = false;
var IsValidBrowser = (document.getElementById) ? true:false;
var TSURL = "http://" + window.location.host + "/International/TruckStopInfo.aspx";
var TRPURL ="http://" + window.location.host + "/International/GetMap.aspx";
var EMAILURL ="http://" + window.location.host + "/International/Email.aspx";
var sMode;
var df;
var startx = 0;
var starty = 0
var currpage=1;
var leftline;
var rightline;
var topline;
var bottomline;

function showAmenities(tsid) {
    var x = findPosX($("TS_" + tsid));
    var y = findPosY($("TS_" + tsid)); 
    p = $("PopUp")
    p.innerHTML = "<img src=\"Images/Loading.gif\" alt=\"Loading...\"/>";     
    p.style.left = x+"px";
    p.style.top = (y-10)+"px";
    p.style.visibility = "visible";
    sMode = "TS"; 
    SendPOSTRequest(TSURL, "TSID=" +tsid); 
    }
   
   function hidePopUp() {
        $("PopUp").style.visibility = "hidden";
} 

function flipTSRow() {
var cls = (document.forms[0].HideTS.checked) ? "TSShow":"TSHide";
var ncls = (document.forms[0].HideTS.checked) ? "TSHide":"TSShow";
var o =  getElementsByClass(cls,$('DirectionsTable'),'tr')

for(i=0;i<o.length;i++) {
    o[i].className = ncls;
}
}

function init() {
    df = document.forms[0];
    var o = $("MapPane");
    if(o!=null) {
   addEvent($("MapPane"),"mousedown",mapDrawStart);
   addEvent($("MapPane"),"mouseup",mapSend);
   addEvent($("MapPane"),"mousemove",mapDraw);
   }
   leftline = $("lleft");
   rightline = $("lright");
   topline = $("ltop");
   bottomline = $("lbottom");
}


function mapDraw(evt) {
if(isDrawing==true) {
//Have to do this to keep the ghost square from hosing stuff
topline.className = "linesOn";
bottomline.className = "linesOn";
leftline.className = "linesOn";
rightline.className = "linesOn";
    var ep = getMousePosition(evt);
    var xs=0;
    var xcs=0;
    var xw=0;
    var ys=0;
    var yh=0;
    var ycb=0;
    var evx = ep.x +3;
    var evy = ep.y +3; 
   				if (evx - startx <=0) {
					xs = evx;
					xcs = startx;
					xw = startx-evx;
				}
				else {
					xs = startx;
					xcs = evx;
					xw = evx - startx;
				}
				
				if (evy-starty <=0) {
					ys = evy;
					yh = starty - evy;
					ycb = starty;
				}
				else {
					ys = starty;
					yh = evy - starty;
					ycb = evy;
				}
				//TOP
				topline.style.left = xs+"px";
				topline.style.top = ys+"px";
				topline.style.width = xw+"px";
	
				//BOTTOM
				bottomline.style.left = xs+"px";
				bottomline.style.top = ycb+"px";
				bottomline.style.width = (xw + 2)+"px";

				//LEFT
				leftline.style.left = xs+"px";
				leftline.style.top = ys+"px";
				leftline.style.height = yh+"px";
				
				//RIGHT
				rightline.style.left = xcs+"px";
				rightline.style.top = ys+"px";
				rightline.style.height = yh+"px"; 
		}
}

function mapDrawStart(evt) {
if(df.ZoomType[0].checked == false) return; //ignore if click zoom is selected
isDrawing=true;
var ep= getMousePosition(evt);
startx=ep.x;
starty=ep.y;
}

function mapSend(evt) {
var mpx = findPosX($("MapPane"));
var mpy = findPosY($("MapPane"));
var ep = getMousePosition(evt);
var evx = ep.x;
var evy = ep.y

if(df.ZoomType[1].checked == true) {
    df.x1.value = (evx-mpx);
    df.y1.value = (evy-mpy); 
    df.zoom.value = "clickzoom";
}
else if(isDrawing == false) {
    return;
}
else {
    df.x1.value = (startx-mpx);
    df.y1.value = (starty-mpy);
    df.x2.value = (evx-mpx);
    df.y2.value = (evy-mpy); 
   df.zoom.value = "box"; 
   isDrawing=false;
}
    var ps = buildPostString() 
    sMode="ZOOM";
    $("MapPane").style.backgroundImage = "url(Images/Loading.gif)"; 
    SendPOSTRequest(TRPURL, ps)
   topline.className = "lines";
   bottomline.className = "lines";
   leftline.className = "lines";
   rightline.className = "lines"; 
   return;
}

function map(s) {
 df.MapType.value = "none";
 df.zoom.value="none";
 var ps = buildPostString() 
 sMode="ZOOM";
$("MapPane").style.backgroundImage = "url(Images/Loading.gif)"; 
 SendPOSTRequest(TRPURL, ps)
}

function pan(s) {
df.zoom.value=s;
 var ps = buildPostString()  
 sMode="ZOOM";
$("MapPane").style.backgroundImage = "url(Images/Loading.gif)"; 
 SendPOSTRequest(TRPURL, ps)
}

function zoomMap(s) 
{
df.zoom.value = "clickzoom" + s;
df.x1.value = "300";
df.y1.value = "200";
sMode="ZOOM";
var ps = buildPostString()  
$("MapPane").style.backgroundImage = "url(Images/Loading.gif)"; 
 SendPOSTRequest(TRPURL, ps)
}

function processZoom(txt) {
    if(txt=="ERROR") {
     alert("MapError");
    }

  var t = eval("(" + txt + ")");

  df.CenterLatitude.value = t.CenterLatitude;
  df.CenterLongitude.value = t.CenterLongitude;
  df.DistanceAcross.value = t.DistanceAcross;

  $("MapPane").style.backgroundImage = "url(" + t.ImageUrl + ")";
  //clear it
  df.x1.value="-1";
  df.y1.value="-1";
  df.x2.value="-1";
  df.y2.value="-1";
  df.MapType.value = "none";
  df.zoom.value = "none";
  //df.zoomParameter ="";

}

addLoadEvent(init);