//Developed by Vladimir Lebedko

var sMarkerIcon;
var sMarker;
var dMarkerIcon;
var dMarker;
var seletedLayout;
var seletedLimits;
var seletedLimitToDistanceType;
var seletedLimitValue;
var sIgnorFirst;
var dIgnorFirst;
var mTimeStart;
var mTimeRemaining;

sMarkerIcon = new GIcon();
sMarkerIcon.image = "sMarker.png";
sMarkerIcon.iconSize = new GSize(20, 34);
sMarkerIcon.shadow = "shadow.png"
sMarkerIcon.shadowSize = new GSize(37, 34);
sMarkerIcon.iconAnchor = new GPoint(9, 34);
sMarkerIcon.infoWindowAnchor = new GPoint(5, 1); 

dMarkerIcon = new GIcon();
dMarkerIcon.image = "dMarker.png";
dMarkerIcon.iconSize = new GSize(20, 34);
dMarkerIcon.shadow = "shadow.png"
dMarkerIcon.shadowSize = new GSize(37, 34);
dMarkerIcon.iconAnchor = new GPoint(9, 34);
dMarkerIcon.infoWindowAnchor = new GPoint(5, 1); 

function changeView(mName) {
 if(document.getElementById(mName).style.display!='none') {
   document.getElementById(mName).style.display='none'
  } else { 
     document.getElementById(mName).style.display='';
    }  
}

function changeLimits(mName) {
 document.getElementById("oShowOnlyTop").disabled=true;
 document.getElementById("oLimitToTime").disabled=true;
 document.getElementById("oLimitToDistance").disabled=true;
 document.getElementById("oLimitToDistanceTypeK").disabled=true;
 document.getElementById("oLimitToDistanceTypeM").disabled=true;
 switch (mName) {
  case "showOnlyTop": document.getElementById("oShowOnlyTop").disabled=false;
       break;
  case "limitToTime": document.getElementById("oLimitToTime").disabled=false;
       break;
  case "limitToDistance": document.getElementById("oLimitToDistance").disabled=false;
       document.getElementById("oLimitToDistanceTypeK").disabled=false;
       document.getElementById("oLimitToDistanceTypeM").disabled=false;
       break;
  default:
 }
}

function changeColumnLetter(mGroup, mName) {
 var vArray=new Array(false,false,false,false,false,false,false,false);
 var fValue=-1;

 vArray[document.getElementById(mGroup+"Name").selectedIndex]=true;
 vArray[document.getElementById(mGroup+"Street").selectedIndex]=true;
 vArray[document.getElementById(mGroup+"City").selectedIndex]=true;
 vArray[document.getElementById(mGroup+"Zip").selectedIndex]=true;
 vArray[document.getElementById(mGroup+"State").selectedIndex]=true;
 vArray[document.getElementById(mGroup+"Country").selectedIndex]=true;
 vArray[document.getElementById(mGroup+"Lat").selectedIndex]=true;
 vArray[document.getElementById(mGroup+"Long").selectedIndex]=true;
 
 for(i=0;i<vArray.length;i++){
  if(vArray[i]==false) fValue=i;
 }

 if(fValue==-1) return;
 
 if(mName!="Name"&&(document.getElementById(mGroup+"Name").selectedIndex==document.getElementById(mGroup+mName).selectedIndex)){
  document.getElementById(mGroup+"Name").selectedIndex=fValue;
 }
 if(mName!="Street"&&(document.getElementById(mGroup+"Street").selectedIndex==document.getElementById(mGroup+mName).selectedIndex)){
  document.getElementById(mGroup+"Street").selectedIndex=fValue;
 }
 if(mName!="City"&&(document.getElementById(mGroup+"City").selectedIndex==document.getElementById(mGroup+mName).selectedIndex)){
  document.getElementById(mGroup+"City").selectedIndex=fValue;
 }
 if(mName!="Zip"&&(document.getElementById(mGroup+"Zip").selectedIndex==document.getElementById(mGroup+mName).selectedIndex)){
  document.getElementById(mGroup+"Zip").selectedIndex=fValue;
 }
 if(mName!="State"&&(document.getElementById(mGroup+"State").selectedIndex==document.getElementById(mGroup+mName).selectedIndex)){
  document.getElementById(mGroup+"State").selectedIndex=fValue;
 }
 if(mName!="Country"&&(document.getElementById(mGroup+"Country").selectedIndex==document.getElementById(mGroup+mName).selectedIndex)){
  document.getElementById(mGroup+"Country").selectedIndex=fValue;
 }
 if(mName!="Lat"&&(document.getElementById(mGroup+"Lat").selectedIndex==document.getElementById(mGroup+mName).selectedIndex)){
  document.getElementById(mGroup+"Lat").selectedIndex=fValue;
 }
 if(mName!="Long"&&(document.getElementById(mGroup+"Long").selectedIndex==document.getElementById(mGroup+mName).selectedIndex)){
  document.getElementById(mGroup+"Long").selectedIndex=fValue;
 }
}

function changeStartingAddressFile() {
  var tS=document.getElementById("fStartingAddressFile").value;
  if(tS.length<4){
   return false;
  }
  if((tS.substring(tS.length-4,tS.length)).toUpperCase()!=".XLS"){
   alert("File type incorrect. Please select .XLS file.");
   return false;
  }
  document.getElementById("cAction").value="changeStartingAddressFile";
  document.mForm.submit();
  return true;
}

function changeDestinationAddressFile() {
  var tS=document.getElementById("fDestinationAddressFile").value;
  if(tS.length<4){
   return false;
  }
  if((tS.substring(tS.length-4,tS.length)).toUpperCase()!=".XLS"){
   alert("File type incorrect. Please select .XLS file.");
   return false;
  }
  document.getElementById("cAction").value="changeDestinationAddressFile";
  document.mForm.submit();
  return true;
}

function calculateDistances(){
 if(document.getElementById("sAddressCount").value==""){
  alert("Please specify Starting Address File for a start.");
  return;
 }
 if(document.getElementById("dAddressCount").value==""){
  alert("Please specify Destination Address File for a start.");
  return;
 }
 
 // Get checked "Output" options
 for(i=0;i<document.mForm.oLayout.length;i++){
  if(document.mForm.oLayout[i].checked) {
   seletedLayout = document.mForm.oLayout[i].value;
   break;
  }
 }
 for(i=0;i<document.mForm.oLimits.length;i++){
  if(document.mForm.oLimits[i].checked) {
   seletedLimits = document.mForm.oLimits[i].value;
   break;
  }
 }
 for(i=0;i<document.mForm.oLimitToDistanceType.length;i++){
  if(document.mForm.oLimitToDistanceType[i].checked) {
   seletedLimitToDistanceType = document.mForm.oLimitToDistanceType[i].value;
   break;
  }
 }
 
 // Check Limits value, if set 
 if(seletedLimits=="showOnlyTop"){
  if(isNaN(document.getElementById("oShowOnlyTop").value)){
   alert("Please enter a correct value for the \"Show only top\" field.");
   return;
  } else {seletedLimitValue=document.getElementById("oShowOnlyTop").value;}
 } 
 if(seletedLimits=="limitToTime"){
  if(isNaN(document.getElementById("oLimitToTime").value)){
   alert("Please enter a correct value for the \"Limit to time, hours\" field.");
   return;
  } else {seletedLimitValue=document.getElementById("oLimitToTime").value;}
 } 
 if(seletedLimits=="limitToDistance"){
  if(isNaN(document.getElementById("oLimitToDistance").value)){
   alert("Please enter a correct value for the \"Limit to distance\" field.");
   return;
  } else {seletedLimitValue=document.getElementById("oLimitToDistance").value;}
 } 
 
 // Check "Ignore first row"
 if(document.getElementById("fStartingIgnorFirst").checked){
  sIgnorFirst=1;
 } else {sIgnorFirst=0;}
 if(document.getElementById("fDestinationIgnorFirst").checked){
  dIgnorFirst=1;
 } else {dIgnorFirst=0;}
 
 // Chack Address/LatLong priority
 for(i=0;i<document.mForm.fPriority.length;i++){
  if(document.mForm.fPriority[i].checked) {
   seletedPriority = document.mForm.fPriority[i].value;
   break;
  }
 }
 
 document.getElementById("mOutput").style.display='none';
 document.getElementById("mFile").style.display='none';
 document.getElementById("mProgress").style.display='';
 document.getElementById("mResults").style.display='none';
 
 mTimeStart = new Date();
 mTimeStart = mTimeStart.getTime();
  
 getDistance(sIgnorFirst,dIgnorFirst);
}

function getDistance(sIndex, dIndex){
 var sCount=parseInt(document.getElementById("sAddressCount").value);
 var dCount=parseInt(document.getElementById("dAddressCount").value);
 var url;
 var request;

 request = GXmlHttp.create();
 url = "data.php?sAddressFile="+document.getElementById("sAddressFile").value+
       "&dAddressFile="+document.getElementById("dAddressFile").value+
       "&sIndex="+sIndex+"&dIndex="+dIndex+
       "&sName="+document.getElementById("fStartingName").selectedIndex+
       "&sStreet="+document.getElementById("fStartingStreet").selectedIndex+
       "&sCity="+document.getElementById("fStartingCity").selectedIndex+
       "&sZip="+document.getElementById("fStartingZip").selectedIndex+
       "&sState="+document.getElementById("fStartingState").selectedIndex+
       "&sCountry="+document.getElementById("fStartingCountry").selectedIndex+
       "&sLat="+document.getElementById("fStartingLat").selectedIndex+
       "&sLong="+document.getElementById("fStartingLong").selectedIndex+
       "&dName="+document.getElementById("fDestinationName").selectedIndex+
       "&dStreet="+document.getElementById("fDestinationStreet").selectedIndex+
       "&dCity="+document.getElementById("fDestinationCity").selectedIndex+
       "&dZip="+document.getElementById("fDestinationZip").selectedIndex+
       "&dState="+document.getElementById("fDestinationState").selectedIndex+
       "&dCountry="+document.getElementById("fDestinationCountry").selectedIndex+
       "&dLat="+document.getElementById("fDestinationLat").selectedIndex+
       "&dLong="+document.getElementById("fDestinationLong").selectedIndex;
       
 if((sIndex*dCount+dIndex+1)==(sCount*dCount)) { 
  url += "&layout="+seletedLayout+"&limits="+seletedLimits+
         "&limitToDistanceType="+seletedLimitToDistanceType+
         "&limitValue="+seletedLimitValue+
         "&priority="+seletedPriority+
         "&sIgnorFirst="+sIgnorFirst+
         "&dIgnorFirst="+dIgnorFirst+
         "&last=yes";
 }
 request.open("GET", url , true);
 
 request.onreadystatechange = function() {
  if (request.readyState == 4) { 
   var xmlDoc = request.responseText;
   var rows=xmlDoc.split("\n");
   var marker;
   var point;    
   var mTimeElapsed;
   
   if((rows.length<9)||((rows[8]!="NO")&&(rows[8].indexOf("temp")!=0))) { // If unexpected error occurred
    var errorMsg = "Unexpected Error Occurred:\n"+rows.join("\n"); 
    alert(errorMsg);
    return;
   }
   
   mTimeElapsed = new Date();
   mTimeElapsed = mTimeElapsed.getTime();
   mTimeElapsed = mTimeElapsed - mTimeStart;
   mTimeRemaining = mTimeElapsed*(sCount*dCount)/(sIndex*dCount+dIndex);
  
   document.getElementById("pStartingAddress").innerHTML=rows[0];
   document.getElementById("pDestinationAddress").innerHTML=rows[1];
   document.getElementById("mDistance").innerHTML="Est. Distance: "+rows[2];
   document.getElementById("mTravelTime").innerHTML="Est. Travel Time: "+rows[3];
   document.getElementById("timeElapsed").innerHTML=((Math.round(mTimeElapsed/6000))/10).toString()+" minutes";
   document.getElementById("timeRemaining").innerHTML=((Math.round(mTimeRemaining/6000))/10).toString()+" minutes";
   bar1.setValue((sIndex*dCount+dIndex+1)/(sCount*dCount)*100);
   
   if(sMarker){
    map.removeOverlay(sMarker); 
    delete sMarker;
   }
   if(dMarker){
    map.removeOverlay(dMarker); 
    delete dMarker;
   }
   if(rows[4]!="0" && rows[5]!="0"){ //Add Start Marker
    point=new GPoint(rows[4],rows[5]);
    sMarker = new GMarker(point, sMarkerIcon); 
    map.addOverlay(sMarker);  
   }
   if(rows[6]!="0" && rows[7]!="0"){ //Add Destination Marker
    point=new GPoint(rows[6],rows[7]);
    dMarker = new GMarker(point, dMarkerIcon); 
    map.addOverlay(dMarker);  
   }
   
   if(rows[8]!="NO"){ // Calculation has finished
    document.getElementById("mOutput").style.display='none';
    document.getElementById("mFile").style.display='';
    document.getElementById("mProgress").style.display='';
    document.getElementById("mResults").style.display='';
    document.getElementById("mResults").innerHTML="<a href=\""+rows[8]+
    "\">Calculation process has finished click here to download XLS file with results.</a>";
    
    // Reset "Progress" section
    document.getElementById("pStartingAddress").innerHTML="";
    document.getElementById("pDestinationAddress").innerHTML="";
    document.getElementById("mDistance").innerHTML="Est. Distance: ";
    document.getElementById("mTravelTime").innerHTML="Est. Travel Time: ";
    document.getElementById("timeElapsed").innerHTML="";
    document.getElementById("timeRemaining").innerHTML="";
    bar1.setValue(0);
   }
   
   if(dIndex<dCount){
    dIndex++;
    if(dIndex<dCount){
     getDistance(sIndex, dIndex);
    } else {
       if(sIndex<sCount){
        dIndex=dIgnorFirst;
        sIndex++;
        if(sIndex<sCount){
         getDistance(sIndex, dIndex);
        } else { return; }
       }
      }
   }
  }
 }
 request.send(null);
}
