<!--
var url         = "utils/get_models.lml?make=";
var manurl      = "utils/get_manu.lml";
var http        = getHTTPObject();

window.onload = initialize;

function initialize() {

  var makeSearch = getSessionValue("makeSearch");
  if (makeSearch == null) {
    loadManu();   
  } else {
     if (getSessionValue("makeSearch") != null) {
      loadManu();   
      loadModel();   
      if (getSessionValue("modelSearch") != null) {doSearch();}
     } else {
      loadManu(); //used in case a user refreshes the page before a manufacturer is selected 
     }    
  }
}
function handleManuHttpResponse() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
         var indexID = 0;
         var list = document.getElementById("manufacturer_F");
         var xmlDocument = http.responseXML;
         var items = xmlDocument.getElementsByTagName("manu");
         clearList(list);
         addElementToList(list, "-", "-----Choose-----" );         
         if (items.length > 0)
         { 
           for (var i=0; i<items.length; i++)
           {
             var node = items[i];
             var manuid   = node.getElementsByTagName("name")[0].firstChild.nodeValue;
             var manuname = node.getElementsByTagName("name")[0].firstChild.nodeValue;
             addElementToList(list, manuid, manuname);
             if (manuname == getSessionValue("makeSearch")){
               indexID = i+1;
             }             
           }
         } else {
            alert("No Manufacturers found"); // This should never happen
         }    
         document.getElementById("manufacturer_F").selectedIndex = indexID;
         document.getElementById("manufacturer_F").value = getSessionValue("makeSearch");
     }
    }
}

function handleModelHttpResponse() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
         var indexID = 0;
         var list = document.getElementById("model_F");
         var xmlDocument = http.responseXML;
         var items = xmlDocument.getElementsByTagName("model");
         clearList(list);
         addElementToList(list, "-", "-----Choose-----" );         
         if (items.length > 0)
         {         
           for (var i=0; i<items.length; i++)
           {  
             var node = items[i];
             var modelid   = node.getElementsByTagName("name")[0].firstChild.nodeValue;
             var modelname = node.getElementsByTagName("name")[0].firstChild.nodeValue;
             addElementToList(list, modelid, modelname);

             if (modelname == getSessionValue("modelSearch")){
               indexID = i+1;
             }             
           }
         } else {
           if (document.search_select.manufacturer_F.value != "-") { alert("No Models found for this make"); }// This should never happen
         }
         if (getSessionValue("modelSearch") != null) {
            document.getElementById("model_F").selectedIndex = indexID;
            document.getElementById("model_F").value = getSessionValue("modelSearch");
         }
     }
    }
}

function handleSearchHttpResponse() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
        document.getElementById("search_box").innerHTML = http.responseText;       
     }
    }
}

function handleDetailsHttpResponse(carID) {
  if (http.readyState == 4) {
      if (http.responseText.indexOf('invalid') == -1) {
        document.getElementById(carID).innerHTML = http.responseText;       
     }
    }
}

function addElementToList(list, value, label)
{
    var option = document.createElement("option");
    option.value = value;
    var labelNode = document.createTextNode(label);
    option.appendChild(labelNode);
    list.appendChild(option);
}

function clearList(list)
{     
  while (list.length > 0)
  {
    list.options[0] = null;
  }   
}

function loadManu() {           
    http.open("GET", manurl, false);    
    if (navigator.appVersion.indexOf("MSIE")>0){
      http.onreadystatechange = handleManuHttpResponse;
    } else {
      http.onload = handleManuHttpResponse;
    }    
    http.send(null);
    var list = document.getElementById("model_F");         
}


function loadModel() {    
    
    var makeValue = document.getElementById("manufacturer_F").value;
       
    http.open("GET",url + escape(makeValue),false);  
    if (navigator.appVersion.indexOf("MSIE")>0){
      http.onreadystatechange = handleModelHttpResponse;
    } else {
      http.onload = handleModelHttpResponse;
    }    
    http.send(null);
        
    setSessionValue("makeSearch", makeValue);        
}

function validate_email(field) {
	field=trim(field); 
	var emailFilter=/^.+@.+\..{2,3}$/;
	var illegalChars= /[\!\"\Â£\$\%\^\&\*\+\=\{\}\~\#\?\(\)\<\>\,\;\:\\\/\"\[\]\ ]/;
	
	if ((!(emailFilter.test(field))) || (field.match(illegalChars))) {
		return 0;
	} else {
		return 1;
	}
}

function trim (str) {
	str = this != window? this : str;
	return str.replace(/^\s+|\s+$/g,"");
}

function validate_phone(field){
	var telFilter=/^([0-9])+$/;
	if (!telFilter.test(field)) {return 0;} else {return 1;}
}

function submitThisForm() {
 	var valid = true;
	var msg = "";
   if (document.search_select.email_F.value == "" 
       || document.search_select.manufacturer_F.value == "-"
       || document.search_select.model_F.value == "-" )
   {
      msg = "Please enter your email, manufacturer and model to continue.";
        document.search_select.email_F.focus(); 
        valid = false;
   } else {
    if (!validate_email(document.search_select.email_F.value)) {
        msg = msg + "Please enter a valid email address.";
        document.search_select.email_F.focus(); 
        valid = false;
    }
    if (document.search_select.telephone_F.value !='') {
    	if((!validate_phone(document.search_select.telephone_F.value)) || (document.search_select.telephone_F.value.length < 11)){
    		msg = msg + "Please enter a valid UK landline or mobile number of a minimum 11 digits beginning with a zero (0)";
    		valid= false;
    		document.search_select.telephone_F.focus();
    	}
    }
   }
   if(!valid) alert(msg);
	 return valid;
}

-->
