var theDropDown=null;

/*
url-loading object and a request queue built on top of it
*/

/* namespacing object */
var net=new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;


/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded';
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
    }catch (err){
      this.onerror.call(this);
    }
  }
}


net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.prototype.defaultError=function(){
  alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());
}

////////////////////////

function getVehicleCount(){
	var theForm = document.vehicleSearchForm
	theQueryString = "model=" + theForm.model.value;
	theQueryString += "&fuel=" + theForm.fuel.value;
	theQueryString += "&transmission=" + theForm.transmission.value;
	theQueryString += "&minPrice=" + theForm.minPrice.value;
	theQueryString += "&maxPrice=" + theForm.maxPrice.value;
	theQueryString += "&age=" + theForm.age.value;
	theQueryString += "&mileage=" + theForm.mileage.value;
	theQueryString += "&style=" + theForm.style.value;
	theQueryString += "&dealer=" + theForm.dealer.value;
	var loader1 = new net.ContentLoader('/used-car-search/returnVehicleCount.asp?' + theQueryString,displayVehicleCount,null, "GET", null);
	
}
function displayVehicleCount(){
	var xmlDoc = this.req.responseText;
	var theText = document.getElementById("numberOfResults");
	theText.innerHTML =  xmlDoc;
}

function tellAFriend(theForm){
	theParams = "yourName=" + escape(theForm.yourName.value);
	theParams += "&yourEmail=" + escape(theForm.yourEmail.value);
	theParams += "&friendsName=" + escape(theForm.friendsName.value);
	theParams += "&friendsEmail=" + escape(theForm.friendsEmail.value);
	theParams += "&message=" + escape(theForm.message.value);
	theParams += "&reg=" + escape(theForm.reg.value);
	var loader1 = new net.ContentLoader('tell-a-friend.asp',sayThankYou,null, "POST", theParams);
	return false;
}
function sayThankYou(){
	if(document.getElementById){
		document.getElementById("tafForm").style.display  = 'none';
		document.getElementById("tafThanks").style.display  = 'block';
	}
}

function vehicleEnquiry(theForm){
	theParams = "title=" + escape(theForm.title.value);
	theParams += "&firstName=" + escape(theForm.firstName.value);
	theParams += "&surname=" + escape(theForm.surname.value);
	theParams += "&email=" + escape(theForm.email.value);
	theParams += "&telephone=" + escape(theForm.telephone.value);
	theParams += "&mobile=" + escape(theForm.mobile.value);
	theParams += "&postcode=" + escape(theForm.postcode.value);
	theParams += "&reg=" + escape(theForm.reg.value);
	theParams += "&message=" + escape(theForm.message.value);
	theParams += "&dealer=" + theForm.dealer.value;
	var loader1 = new net.ContentLoader('vehicle-enquiry.asp',sayEnquiryThankYou,null, "POST", theParams);

	return false;
}
function sayEnquiryThankYou(){
	if(document.getElementById){
		document.getElementById("enquiryForm").style.display  = 'none';
		document.getElementById("enquiryThanks").style.display  = 'block';
	}
}
