function ajaxFunction()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        try
        {
          xmlHttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch(e)
        {
          alert("Your browser does not support AJAX!");
          return false;
        }
      }
    }
  }
  return xmlHttp;
}

function cdwCheck()
{
	if (document.getElementById("Country")[document.getElementById("Country").selectedIndex].value != 'IE')
	{
		document.getElementById("cdwHolder").style.visibility="hidden";
	}
	else
		document.getElementById("cdwHolder").style.visibility="visible";
}

function countriesDropDown(DefaultCountry,DefaultLocation)
{	
	
	countries = ajaxFunction();
    
	countries.onreadystatechange=function()
    	{
    		
		if (countries.readyState==4 || countries.readyState=="complete")
      		{
			var x1 =document.getElementById("Country");
      	
			//remove the current elements in dropdowns;
			for (i=x1.length;i>=0;i--)
			{
				x1.remove(i);
			}
            
			//prep the returned data and add it as new options to the dropdowns
			var options = countries.responseText.split("~");
			for (values in options)
			{
				optionVal=options[values].split("@");
				var y1=document.createElement('option');
				
			
				//cookie read Location
				var x = readCookie('quote');

				if (x)
				{
					x = unescape(x);
					var quoteArray = x.split('#');

					if (quoteArray[15]==optionVal[0])
					{
						y1.selected=true;
					}
					//set selected if matches current
				}
				else
				{
					if(optionVal[0]==DefaultCountry)
					{
						y1.selected=true;
					}
				}

				if (optionVal[0]=='N')
				{
					y1.disabled=true;
				}
				else
				{
					y1.value=optionVal[0];
				}

				y1.text=optionVal[1];

				try
				{
					x1.add(y1,null); 
				}
				catch (e)
				{
					x1.add(y1);
				}
			}
			
			locationsDropDown(DefaultLocation);
			if ((document.getElementById("ReturnCountry")) &&(document.getElementById("Country").selectedIndex != document.getElementById("ReturnCountry").selectedIndex))
				returnLocationsDropDown();
		}
	}
	var url="/ajax/countries_dropdown.php?sid="+Math.random();
	countries.open("GET",url,true);
	countries.send(null);    
	
	
}

function locationsDropDown(DefaultLocation)
{
	var DefaultCountry=document.getElementById("Country")[document.getElementById("Country").selectedIndex].value;
	if ((DefaultCountry.substr(0,2)=="US")||(DefaultCountry.substr(0,2)=="CA")||(DefaultCountry.substr(0,2)=="GB"))
	{
		if (document.getElementById("bf_mid"))
		{
			document.getElementById("bf_mid").style.height="315px";
			document.getElementById("qb_returncountry").innerHTML="<p>Return Country</p><select name=\"ReturnCountry\" id=\"ReturnCountry\" class=\"quoteFormField\" onchange=\"returnLocationsDropDown();\"><\/select>";
		}
		else
			document.getElementById("qb_returncountry").innerHTML="<p style=\"float: left; width: 190px; padding: 0; margin: 0; padding-bottom:10px;\">Return Country</p><select style=\"margin-bottom:10px;\" name=\"ReturnCountry\" id=\"ReturnCountry\" class=\"quoteFormField\" onchange=\"returnLocationsDropDown();\"><\/select>";
		
		for (i=0;i<document.getElementById("Country").length;i++)
		{
			//alert(document.getElementById("Country").options[i].text);
			var y1=document.createElement('option');
			y1.text=document.getElementById("Country").options[i].text;
			y1.value=document.getElementById("Country").options[i].value;
			
			var x = readCookie('quote');
			if (x)
			{
				x = unescape(x);
				var quoteArray = x.split('#');
				
				if (quoteArray[16])
				{
					if (quoteArray[16]==y1.value)
					{
						y1.selected=true;
					}
				}
				else if (y1.value == DefaultCountry)
				{
					y1.selected=true;
				}
			}
			else if (y1.value == DefaultCountry)
			{
				y1.selected=true;
			}
			
			try
        	{
				document.getElementById("ReturnCountry").add(y1,null);
			}
			catch (e)
			{
				document.getElementById("ReturnCountry").add(y1);
			}
		}
	}
	else
	{
		document.getElementById("qb_returncountry").innerHTML="";
		if (document.getElementById("bf_mid"))
			document.getElementById("bf_mid").style.height="285px";
	}
  locationsDD = ajaxFunction();
  locationsDD.onreadystatechange=function()
  {
    if (locationsDD.readyState==4 || locationsDD.readyState=="complete")
    {
      //dump out the data to the dropdowns
      var x1 =document.getElementById("PickUpLocationID");
      var x2 =document.getElementById("ReturnLocationID");
       
      //remove the current elements in dropdowns;
      for (i=x1.length;i>=0;i--)
      {
        x1.remove(i);
      }
      
      for (i=x2.length;i>=0;i--)
      {
	    x2.remove(i);
      }
            
      //prep the returned data and add it as new options to the dropdowns
      var options = locationsDD.responseText.split("~");
      for (values in options)
      {
        optionVal=options[values].split("@");
        var y1=document.createElement('option');
        var y2=document.createElement('option');
        
        //cookie read Location
        var x = readCookie('quote');

	if (x)
	{
		x = unescape(x);
		var quoteArray = x.split('#');
		if (quoteArray[11]==optionVal[0])
		{
			y1.selected=true;
		}
		if (quoteArray[12]==optionVal[0])
		{
			y2.selected=true;
		}
        //set selected if matches current
    	}
    	else
    	{
		if (DefaultLocation==optionVal[0])
		{			
			y1.selected=true;
			y2.selected=true;   
		}
    	
    	}
    	
        y1.value=optionVal[0];
        y2.value=optionVal[0];
        y1.text=optionVal[1];
        y2.text=optionVal[1];
        
        try
        {
          x1.add(y1,null);
          x2.add(y2,null); 
        }
        catch (e)
        {
          x1.add(y1);
          x2.add(y2);
        }
      }
   
	cdwCheck();
      if (((DefaultCountry.substr(0,2)=="US")||(DefaultCountry.substr(0,2)=="CA")||(DefaultCountry.substr(0,2)=="GB"))&&(document.getElementById("Country").selectedIndex != document.getElementById("ReturnCountry").selectedIndex))
	  {
		  returnLocationsDropDown("N");
	  }
    }
  }
  //alert(document.GetQuote.Country.options[document.GetQuote.Country.selectedIndex].value);
  var url="/ajax/locations_dropdown.php";
  url=url+"?Country="+document.GetQuote.Country.options[document.GetQuote.Country.selectedIndex].value;
  url=url+"&sid="+Math.random();
  locationsDD.open("GET",url,true);
  locationsDD.send(null);
  
  
}
function returnLocationsDropDown(DefaultLocation)
{
  locationsDD = ajaxFunction();
  locationsDD.onreadystatechange=function()
  {
    if (locationsDD.readyState==4 || locationsDD.readyState=="complete")
    {
      //dump out the data to the return dropdowns
      var x2 =document.getElementById("ReturnLocationID");
       
      //remove the current elements in dropdowns;
      for (i=x2.length;i>=0;i--)
      {
        x2.remove(i);
      }
            
      //prep the returned data and add it as new options to the dropdowns
      var options = locationsDD.responseText.split("~");
      for (values in options)
      {
        optionVal=options[values].split("@");
        var y2=document.createElement('option');
        
        //cookie read Location
        var x = readCookie('quote');

	if (x)
	{
		x = unescape(x);
		var quoteArray = x.split('#');
		if (quoteArray[12]==optionVal[0])
		{
			y2.selected=true;
		}
        //set selected if matches current
    	}
    	else
    	{
			if (DefaultLocation==optionVal[0])
			{			
				y2.selected=true;   
			}
    	
    	}

        y2.value=optionVal[0];
        y2.text=optionVal[1];
        
        try
        {
          x2.add(y2,null); 
        }
        catch (e)
        {
          x2.add(y2);
        }
      }      
    }
  }
  //alert(document.GetQuote.Country.options[document.GetQuote.Country.selectedIndex].value);
  if (document.getElementById("ReturnCountry"))
  {
  	var url="/ajax/locations_dropdown.php";
  	url=url+"?Country="+document.GetQuote.ReturnCountry.options[document.GetQuote.ReturnCountry.selectedIndex].value;
  	url=url+"&sid="+Math.random();
  	locationsDD.open("GET",url,true);
  	locationsDD.send(null);
  }
}
