window.onload = function() {
   var login = document.getElementById('login').value;
   if (login) {
      getDestinations();
   }
}

function getDestinations() {

   var ajaxRequest = openAjax();
   var ajaxDealers = openAjax();
   var currentDate = new Date();
   var newTime     = currentDate.getTime();
   var login       = document.getElementById('login').value;
   var ajaxDisplay = document.getElementById('destination');

   if(!ajaxRequest  ||  !ajaxDealers) { 
      return false; 
   }

   //Create a function that will implement main login destinations.

   ajaxRequest.onreadystatechange = function() {

      if(ajaxRequest.readyState == 4) {

         var ajaxDisplay = document.getElementById('destination');
         var optionData  = ajaxRequest.responseText.split('|'); 
         var indy        = 0;

         for (var i=0; i < optionData.length-1; i+=3) {
            var newOption = new Option(optionData[i+2], optionData[i], 0, optionData[i+1]);
            ajaxDisplay.options[indy] = newOption; indy++;
         }
      }
   }

   //Create a function that will implement salesforce login destinations.

   ajaxDealers.onreadystatechange = function() {

      if(ajaxDealers.readyState == 4) {
         var dealerDiv = document.getElementById('destination_dealer');
         dealerDiv.innerHTML = ajaxDealers.responseText;
      }
   }

   //Send data to server for implementation.

   if (ajaxDisplay) {

      //Remove current options.

      for (var i = ajaxDisplay.options.length; i > 0; i--) {
         ajaxDisplay.options[i-1] = null;
      }

      ajaxDisplay.options[0] = new Option('Loading...', 0);

      //Send ajaxrequest (main destinations).

      var ajaxStr = "x_destinations.php?login="+login+
                 "&time="+newTime;

      ajaxRequest.open("GET", ajaxStr, true);
      ajaxRequest.send(null);

      //Send ajaxrequest (salesforce destinations)

      var dealerStr = "x_sales_destinations.php?login="+login+
                      "&time="+newTime;

      ajaxDealers.open("GET", dealerStr, true);
      ajaxDealers.send(null);
   }
}


function openAjax() {

   var ajaxRequest;
	
   try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
   } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser does not support Ajax.");
            return false;
         }
      }
   }

   return ajaxRequest;
}
