var xmlHttp;
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 loadXMLString(txt) {
  try //Internet Explorer
    {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(txt);
      return(xmlDoc); 
    }
  catch(e)
    {
      try //Firefox, Mozilla, Opera, etc.
	{
	  parser=new DOMParser();
	  xmlDoc=parser.parseFromString(txt,"text/xml");
	  return(xmlDoc);
	}
      catch(e) {alert(e.message)}
    }
  return(null);
}

var argStr = new String();

function sendQuote() {
  xmlHttp = GetXmlHttpObject();
  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var str = getQuoteData();
  //alert(str);

  // check form type for moving or movstore, get the driving distance
  var formtype = document.getElementById("formtype").value;
  if( formtype == "moving" || formtype == "movstor" ) {
    argStr = str;
    addDistanceArg();
    return;
  }

  var url = "http://"+location.hostname+"/quote-units/quote-code/buildQuote.php";
  url = url + "?q=quoterequest" + str;
  url = url + "&sid=" + Math.random();
  //alert("sendThis: "+url);
  xmlHttp.onreadystatechange = quoteResults;
  xmlHttp.open("GET",url,true);
  //xmlHttp.open("GET",url,false); //waits for response before continuing
  xmlHttp.send(null);
  //alert("Your message is being processed...");
}

function quoteResults() { 
  if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) { 
    //alert(xmlHttp.responseText);
    if( (xmlHttp.responseText.search("noprice") != -1) || (xmlHttp.responseText.search("reached the maximum") != -1) ) {
      //document.getElementById("quoteresults").innerHTML = xmlHttp.responseText;
      //document.getElementById("quoteresults").style.visibility = "visible";
      document.getElementById("quotecontainer").innerHTML = xmlHttp.responseText;
      document.getElementById("quotespinnerbox").style.visibility = "hidden";
      document.getElementById("quotefillerbox").style.visibility = "hidden";
      document.getElementById("quoteresultsbox").style.visibility = "visible";
      document.getElementById("quotecontainer").style.visibility = "visible";
    } else {
      var newText = xmlHttp.responseText;
      var prefixEnd = newText.indexOf("<");
      var newHTML = newText.substring(prefixEnd);
      var prefix = newText.substring(0,prefixEnd);
      var info = prefix.split("?");
      document.getElementById("quoteID").value = info[0];
      document.getElementById("franName").value = info[1];
      document.getElementById("franPhone").value = info[2];
      //document.getElementById("quoteresults").style.color = "#000";
      document.getElementById("quotecontainer").innerHTML = newHTML;
      //document.getElementById("quotebox").style.top = "20px";
      //var promostring = document.getElementById("promoright").innerHTML;
      /*
      if( promostring.length > 0 ) {
	document.getElementById("tableleft").style.width = "350px";
      } else {
	document.getElementById("tableleft").style.width = "450px";
      }
      */
      window.scrollTo(0,0);
      document.getElementById("quotespinnerbox").style.visibility = "hidden";
      document.getElementById("quotefillerbox").style.visibility = "hidden";
      document.getElementById("quoteresultsbox").style.visibility = "visible";
      document.getElementById("quotecontainer").style.visibility = "visible";
      document.getElementById("headertextbox").innerHTML = '<p>Talk with a <font style="text-decoration: underline;">LOCAL</font> </p><p>Customer Care Representative</p><p>Call '+phoneNumber(document.getElementById("franPhone").value)+'</p>';

      // disable quote button for 2 more seconds
      var t = setTimeout("enableQuoteButton()",2000);

      //saveQuoteTable();
    }
  }
}

function enableQuoteButton() {
  document.getElementById("quotebutton").src = "http://"+location.hostname+"/quote-units/pics/get-a-quote-button.png";
  document.getElementById("quote-button").style.visibility = "visible";
}

function phoneNumber( phone ) {
  if( phone.length == 10 ) {
    return( phone.substr(0,3)+"-"+phone.substr(3,3)+"-"+phone.substr(4,4) );
  } else if( phone.length == 7 ) {
    return( phone.substr(0,3)+"-"+phone.substr(3,4) );
  } else {
    return( phone );
  }
}

function getQuoteData() {
  var data;

  data = "&formtype="+document.getElementById("formtype").value;
  
  zip1 = document.getElementById("initzip").value;
  data += "&zip1="+zip1;
  if( activeMenu < 3 ) {
    zip2 = document.getElementById("destzip").value;
    data += "&zip2="+zip2;
  } else {
    data += "&zip2=NULL";
  }
  deliverdate = document.getElementById("deliver_date").value;
  data += "&date="+deliverdate;
  months = document.getElementById("monthsNeeded").value;
  data += "&months="+months;
  unit12 = document.getElementById("twelves").selectedIndex;
  data += "&twelves="+unit12;
  unit16 = document.getElementById("sixteens").selectedIndex;
  data += "&sixteens="+unit16;
  if( document.getElementById("seniordiscount").checked ) {
    data += "&discount=S";
    discount = "S";
  } else if( document.getElementById("militarydiscount").checked ) {
    data += "&discount=M";
    discount = "M";
  } else if( document.getElementById("corpdiscount").checked ) {
    data += "&discount=C";
    discount = "C";
  } else {
    data += "&discount=N";
    discount = "N";
  }
  couponcode = document.getElementById("promodiscount").value;
  data += "&couponcode="+couponcode;

  if( document.getElementById("addcomments").value.length > 0 ) {
    if( document.getElementById("addcomments").value.search("Comments... Questions... Anything about your service or situation") == -1 ) {
      customercomment = document.getElementById("addcomments").value;
      data += "&comment="+customercomment;
    }
  }
  customername = document.getElementById("custname").value;
  data += "&name="+customername;
  customerphone = document.getElementById("custarea").value + document.getElementById("custexch").value + document.getElementById("custphone").value;
  data += "&phone="+customerphone;
  customeremail = document.getElementById("email").value;
  data += "&email="+customeremail;
  data += "&newsletter="+(document.getElementById("newsletter").checked?"true":"false");
  //if( activeMenu == 2 )
  if( activeMenu != 1 ) {
    data += "&location="+(document.getElementById("storagelocation").selectedIndex==1?"warehouse":"onsite");
    storage = document.getElementById("storagelocation").selectedIndex==1?"warehouse":"onsite";
  } else {
    data += "&location="+"onsite";
    storage = "onsite";
  }
  //if( activeMenu == 3 )
  //data += "&location="+(document.getElementById("mylocation").checked?"onsite":"warehouse");
  return(data);
}

// routines to get driving distance from mapquest.com
// first determine if this is a move and then whether it's an IFmove
// requires a chain of AJAX events to complete the task. each event handler
// analyzes its own results and decides whether to call the next chain event or
// the task has completed.
//
function address( Address, City, State, Zipcode ) {
  this.Address = Address;
  this.City = City;
  this.State = State;
  this.Zipcode = Zipcode;
}
//var addressInfo = new Array();
var IFmove = false;

function addDistanceArg() {
  // check here for multiple zips
  var zip1 = document.getElementById("initzip").value;
  var zip2 = document.getElementById("destzip").value;
  if( zip2.length > 0 ) {
    // this could be an IFmove, query the database for address(es) and move status
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp==null) {
      alert ("Browser does not support HTTP Request");
      return;
    }
    var url = "http://"+location.hostname+"/quote-units/quote-code/isIfMove.php";
    url = url + "?q=ifmoverequest&zip1=" + zip1 + "&zip2=" + zip2; 
    url = url + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = ifmoveResults;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
  }
}

function ifmoveResults() {
  if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) { 
    if( xmlHttp.responseText.search("ERROR") != -1 ) {
      // what to do on error?
      getCtoCdistance();
    } else {
      // parse the xml
      xmlDoc = loadXMLString(xmlHttp.responseText);
      var x = xmlDoc.getElementsByTagName("result");
      var y = x[0].childNodes[0].textContent;
      if( y == 'true' ) {
	// IF move, should have 2 warehouse addresses
	IFmove = true;
	//alert("Is IFmove");
	var origin = xmlDoc.getElementsByTagName("origin");
	//var address1 = z[0].childNodes[0].textContent;
	//var city1 = z[0].childNodes[1].textContent;
	//var state1 = z[0].childNodes[2].textContent;
	//var zip1 = z[0].childNodes[3].textContent;
	var addr1 = new address(origin[0].childNodes[0].textContent,origin[0].childNodes[1].textContent,origin[0].childNodes[2].textContent,origin[0].childNodes[3].textContent);

	var dest = xmlDoc.getElementsByTagName("destination");
	//var address2 = z[0].childNodes[0].textContent;
	//var city2 = z[0].childNodes[1].textContent;
	//var state2 = z[0].childNodes[2].textContent;
	//var zip2 = z[0].childNodes[3].textContent;
	var addr2 = new address(dest[0].childNodes[0].textContent,dest[0].childNodes[1].textContent,dest[0].childNodes[2].textContent,dest[0].childNodes[3].textContent);
	//addressInfo[0] = addr1;
	//addressInfo[1] = addr2;
	getIFdistance(addr1,addr2);
      } else {
	// Not an IF move, no addresses necessary, go get the CtoC distance
	IFmove = false;
	getCtoCdistance();
	//alert("NOT IFmove");
      }
    }
  }
}

function getIFdistance(addr1, addr2) {
  var from = addr1.Address+","+addr1.City+","+addr1.State+","+addr1.Zipcode;
  var to = addr2.Address+","+addr2.City+","+addr2.State+","+addr2.Zipcode;
  var url = 'http://mapquestapi.com/directions/v1/route?key=Fmjtd%7Cluu72l6rn1%2Cbn%3Do5-54z2q&ambiguities=ignore&from='+from+'&to='+to+'&callback=ifDistanceResult';
  MQA.jsonp(url);

}

function ifDistanceResult( response ) {
  if( response.info.statuscode == 0 ) {
    argStr += "&warehousedistance="+response.route.distance;
  }
  getCtoCdistance();
}

function getCtoCdistance() {
  zip1 = document.getElementById("initzip").value;
  zip2 = document.getElementById("destzip").value;

  var url = 'http://mapquestapi.com/directions/v1/route?key=Fmjtd%7Cluu72l6rn1%2Cbn%3Do5-54z2q&ambiguities=ignore&from='+zip1+'&to='+zip2+'&callback=ctocDistanceResult';
  MQA.jsonp(url);
}

function ctocDistanceResult( response ) {
  if( response.info.statuscode == 0 ) {
    argStr += "&distance="+response.route.distance;
  }
  sendQuoteRequest();
}

function sendQuoteRequest() {
  xmlHttp = GetXmlHttpObject();
  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var url = "http://"+location.hostname+"/quote-units/quote-code/buildQuote.php";
  url = url + "?q=quoterequest" + argStr;
  url = url + "&sid=" + Math.random();
  //alert("sendThis: "+url);
  xmlHttp.onreadystatechange = quoteResults;
  xmlHttp.open("GET",url,true);
  //xmlHttp.open("GET",url,false); //waits for response before continuing
  xmlHttp.send(null);
  //alert("Your message is being processed...");

}

 

