function checkZip(make, model, year) 
{
	ZIPCODECHECK = window.document.getElementById("zipcode");
	var make  = "Honda";
	var model = "Accord";
	var year  = "2007";

	var url = "http://leads.arizonabay.com/v2/public/ping.cfm?Make="+make+"&Model="+model+"&Zipcode="+ZIPCODECHECK.value+"&Year="+year+"&Provider=JDPA"

	if(window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		request = new ActiveXObject("MSXML2.XMLHTTP");
	}

	sendRequest(url);
}

function sendRequest(url)
{
	request.onreadystatechange = onResponse;
	request.open("GET", url, true);
	request.send(null);
	
	var text = xmlhttp.responseText;
}

function checkReadyState(obj)
{
	if(obj.readyState == 0) { document.getElementById('copy').innerHTML = "Sending Request..."; }
	if(obj.readyState == 1) { document.getElementById('copy').innerHTML = "Loading Response..."; }
	if(obj.readyState == 2) { document.getElementById('copy').innerHTML = "Response Loaded..."; }
	if(obj.readyState == 3) { document.getElementById('copy').innerHTML = "Response Ready..."; }
	if(obj.readyState == 4)
	{
		if(obj.status == 200)
		{
			return true;
		}
		else if(obj.status == 404)
		{
			// Add a custom message or redirect the user to another page
			document.getElementById('copy').innerHTML = "File not found";
		}
		else
		{
			document.getElementById('copy').innerHTML = "There was a problem retrieving the XML.";
		}
	}
}

function onResponse() 
{
	if(checkReadyState(request))
	{
		//alert(request.responseXML);
		//alert(request.responseText);
		var response = request.responseText;
		if(response == "No Coverage Found") {
			window.document.getElementById("zipcode").value = "";
			window.document.getElementById("zipcode").focus();
			window.document.getElementById("zipCodeError").innerHTML = "Invalid zipcode, Please enter a valid zip code.";
		} else { 
				window.document.getElementById("zipCodeError").innerHTML = "";
				window.document.getElementById("submitBtn").innerHTML = "<input type='submit' name='submit' value='Get a Quote'>";
		}
	}
}

 function getModels(el)
  {
   var make = el.value;
   if(make === null || make == "") 
    make = el.getElementsByTagName("option")[el.selectedIndex].text;
      
   var modelBox = getEl("model_0");
   
   var xmlhttp;
	try	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  	} catch (e)
  	{
	  	try	{
		  	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  	} catch (E)
	  	{
		  	xmlhttp = false;
	  	}
 	}
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e)
		{
			xmlhttp = false;
		}
	}
   
   while(modelBox.childNodes.length > 0)
    modelBox.removeChild(modelBox.childNodes[0]);
   
   var loadingOption = document.createElement("option");
   loadingOption.appendChild(document.createTextNode ("Loading..."));
   modelBox.appendChild(loadingOption);
   
   var data = "AsyncRequest=ListModels&make=" + make;
   
   xmlhttp.open("POST", document.location, true);
   
   xmlhttp.onreadystatechange = function()
   {
    if(xmlhttp.readyState == 4)
    {
     modelBox.removeChild(modelBox.childNodes [0]);
     var dummyElement = document.createElement("option"); 
     dummyElement.appendChild(document.createTextNode("Select Model"));
     modelBox.appendChild(dummyElement);
     //alert( xmlhttp.responseText);
     var doc = xmlhttp.responseXML;
     var models = doc.getElementsByTagName("model");
     for(var i = 0; i < models.length; i++)
     {
      var mOption = document.createElement ("option");
      mOption.value = models[i].getAttribute("year") + " " + models[i].getAttribute("name"); 
      mOption.appendChild(document.createTextNode(models[i].getAttribute("year") + " " + models[i].getAttribute("name")));
      modelBox.appendChild(mOption);
     }
    }
   } 
   
   xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   xmlhttp.send(data);
  }
     
  function getTrims(el)
  {
	var carIndex = el.id;
   	carIndex = carIndex.substring(carIndex.length-1);

   var makeEl = getEl("make_"+carIndex);
   var make = makeEl.value;
   if(make === null || make == "") 
    make = makeEl.getElementsByTagName("option")[makeEl.selectedIndex ].text;
   
   var modelEl = getEl("model_"+carIndex);
   var model = modelEl.value;
   if(model == null || model == "") 
    model = modelEl.getElementsByTagName ("option")[modelEl.selectedIndex].text;
   
   var trimBox = getEl("trim_"+carIndex);
   var xmlhttp;
	try	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  	} catch (e)
  	{
	  	try	{
		  	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  	} catch (E)
	  	{
		  	xmlhttp = false;
	  	}
 	}
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e)
		{
			xmlhttp = false;
		}
	}
   
   while(trimBox.childNodes.length > 0)
    trimBox.removeChild(trimBox.childNodes[0]);
   
   var loadingOption = document.createElement("option");
   loadingOption.appendChild(document.createTextNode ("Loading..."));
   trimBox.appendChild(loadingOption);
   
   var data = "AsyncRequest=ListTrims&make=" + make + "&car=" + model;
   
   xmlhttp.open("POST", document.location, true);
   xmlhttp.onreadystatechange = function()
   {
    if(xmlhttp.readyState == 4)
    {
     var dummyElement = document.createElement("option");
     dummyElement.appendChild (document.createTextNode ("Select Trim"));
     trimBox.appendChild(dummyElement);
     trimBox.removeChild(trimBox.childNodes[0]);
     //alert(xmlhttp.responseText);
     var doc = xmlhttp.responseXML ;
     var trims = doc.getElementsByTagName("trim");
     for(var i = 0; i < trims.length; i++)
     {
      var tOption = document.createElement("option");
      tOption.appendChild( document.createTextNode (trims[i].getAttribute("bodytype") + "  / " + trims[i].getAttribute("trim")));
      tOption.value = trims[i].getAttribute("trim") + " | " + trims[i].getAttribute("doors") + " door " + trims[i].getAttribute("bodytype"); 
      trimBox.appendChild(tOption);
     }
    }
   }
   
   xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   xmlhttp.send(data);
  }

  function getEl(id) { 
  //alert("Getting element "+id);
  var el = window.document.getElementById(id);
   if (el === null) {
    alert("No object was found for the id ["+id+"]: " + typeof el); 
    return;
   } else {
    return el;
   }
  }
  
	function checkFields()
  	{
		var year = "";
		var make = "";
		var model = "";
		var email = "";
		var fname = "";
		var lname = "";
		var address = "";
		var city = "";
		var state = "";
		var zip = "";
		var phone = "";
		var within = "";
		var contact = "";
		var payment = "";
		
		try
	  	{
			year = window.document.form1.year.value;
			make = window.document.form1.make.value;
			model = window.document.form1.model.value;
			email = window.document.form1.Email.value;
			fname = window.document.form1.FName.value;
			lname = window.document.form1.LName.value;
			address = window.document.form1.Address.value;
			city = window.document.form1.City.value;
			state = window.document.form1.state.value;
			zip = window.document.form1.Zipcode.value;
			phone = window.document.form1.DPhone.value;
			within = window.document.form1.Within.value;
			contact = window.document.form1.BestTime.value;
			payment = window.document.form1.PaymentMethod.value;
			
			pmt_error = "Please choose a payment method.";
			smg22_error = "Please choose a contact time.";
			email_error = "Please enter a valid e-mail address.";
			fname_error = "Please enter your first name.";
			lname_error = "Please enter your last name.";
			address_error = "Please enter a valid address.";
			city_error = "Please enter a valid city.";
			state_error = "Please choose a state.";
			zipcode_error = "Please enter a valid zip code.";
			dphone_error = "Please enter a valid phone number.";
			within_error = "Please choose a purchase timeframe.";

			if(email == "")
			{
				getEl("email").style.display = "block";	
				getEl("email").innerHTML = email_error;
			}
			else
			{
				getEl("email").style.display = "none";	
			}

			if(fname == "")
			{
				getEl("fname").style.display = "block";	
				getEl("fname").innerHTML = fname_error;	
			}
			else
			{
				getEl("fname").style.display = "none";	
			}
			
			if(lname == "")
			{
				getEl("lname").style.display = "block";	
				getEl("lname").innerHTML = lname_error;	
			}
			else
			{
				getEl("lname").style.display = "none";	
			}
			
			if(address == "")
			{
				getEl("address").style.display = "block";	
				getEl("address").innerHTML = address_error;	
			}
			else
			{
				getEl("address").style.display = "none";	
			}
			
			if(city == "")
			{
				getEl("city").style.display = "block";	
				getEl("city").innerHTML = city_error;	
			}
			else
			{
				getEl("city").style.display = "none";	
			}
			
			if(state == "")
			{
				getEl("state").style.display = "block";	
				getEl("state").innerHTML = state_error;	
			}
			else
			{
				getEl("state").style.display = "none";	
			}
			
			if(zip == "")
			{
				getEl("zip").style.display = "block";	
				getEl("zip").innerHTML = zipcode_error;	
			}
			else
			{
				getEl("zip").style.display = "none";	
			}
			
			if(phone == "")
			{
				getEl("phone").style.display = "block";	
				getEl("phone").innerHTML = dphone_error;	
			}
			else
			{
				getEl("phone").style.display = "none";	
			}
			
			if(within == "")
			{
				getEl("within").style.display = "block";	
				getEl("within").innerHTML = within_error;	
			}
			else
			{
				getEl("within").style.display = "none";	
			}
			
			if(contact == "")
			{
				getEl("contact").style.display = "block";	
				getEl("contact").innerHTML = smg22_error;	
			}
			else
			{
				getEl("contact").style.display = "none";	
			}

			if(payment == "")
			{
				getEl("PaymentMethod").style.display = "block";	
				getEl("PaymentMethod").innerHTML = pmt_error;	
			}
			else
			{
				getEl("PaymentMethod").style.display = "none";	
			}

			if(year != "" && make != "" && model != "" && email != "" && fname != "" && lname != "" && address != "" && city != "" && state != "" && zip != "" && phone != "" && within != "" && contact != "" && payment != "")
			{
				getEl("submitted").style.display = "block";
			}
		} 
		catch(e) 
		{ 
			//alert(e); 
		}
  	}
