
/***********************************************
* Basic Ajax Routine- Copyright Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

//Basic Ajax Routine- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Last updated: Jan 15th, 06'

function createAjaxObj(){
  var httprequest=false
  if (window.XMLHttpRequest){ // if Mozilla, Safari etc
    httprequest=new XMLHttpRequest()
    if (httprequest.overrideMimeType)
    httprequest.overrideMimeType('text/xml')
  }
  else if (window.ActiveXObject){ // if IE
    try {
      httprequest=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
      try{
        httprequest=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e){}
    }
  }
  return httprequest
}

var ajaxpack=new Object()
ajaxpack.basedomain="http://"+window.location.hostname
ajaxpack.ajaxobj=createAjaxObj()
ajaxpack.filetype="txt"
ajaxpack.addrandomnumber=0 //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
  ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
  if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
  var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
  if (this.ajaxobj){
    this.filetype=filetype
    this.ajaxobj.onreadystatechange=callbackfunc
    this.ajaxobj.open('GET', url+"?"+parameters, true)
    this.ajaxobj.send(null)
  }
}

ajaxpack.postAjaxRequest=function(url, parameters, callbackfunc, filetype){
  ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
  if (this.ajaxobj){
    this.filetype=filetype
    this.ajaxobj.onreadystatechange = callbackfunc;
    this.ajaxobj.open('POST', url, true);
    this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this.ajaxobj.setRequestHeader("Content-length", parameters.length);
    this.ajaxobj.setRequestHeader("Connection", "close");
    this.ajaxobj.send(parameters);
  }
}


function MM_openBrWindow(theURL,winName,features)
{ //v2.0
  window.open(theURL,winName,features);
}

function removeAllOptions(select_box)
{
  var i;
  for(i=select_box.options.length-1;i>0;i--)
  {
    select_box.remove(i);
  }
}

function addOption(select_box,text,value,selected)
{
  var optn = document.createElement("OPTION");
  optn.text = text;
  optn.value = value;
  select_box.options.add(optn);
  if (selected) {
    optn.selected = true;
  }
}

var select_box;
var current_model;

function populate_select()
{
  var myajax=ajaxpack.ajaxobj;
  var selected;
  if (myajax.readyState == 4){ //if request of file completed
    if (myajax.status==200 || window.location.href.indexOf("http")==-1){
      models = myajax.responseText.split("\n");
      
      for (var i=0, len=models.length; i<len; ++i){
        if (models[i] == '')
        {
          continue;
        }

        if (current_model == models[i-1]) {
          selected = true;
        } else {
          selected = false;
        }

        addOption(select_box, models[i], models[i], selected);
      }

      if (select_box.options[0].text == "Loading...") {
        select_box.options[0].text = "Choose";
      }
    }
  }
}

function update_models(model_F)
{
  select_box = document.srcplug_basic_form.model_F;
  removeAllOptions(select_box);
  //    select_box.disabled=true;
  //    select_box.options.length=0;
  //    select_box.options[0] = new Option( 'Please wait...', '' );
  if (model_F) {
    current_model = model_F;
  } else {
    current_model = 1;
    select_box.options[0] = new Option( 'Loading...', '' );
  }
  ajaxpack.getAjaxRequest('/ajax-models-list.php', 'maker_F='+document.srcplug_basic_form.maker_F.value, populate_select, 'txt');
}

function update_manufacturers()
{
  select_box = document.srcplug_basic_form.maker_F;
  removeAllOptions(select_box);
  
  current_model = 1;
  select_box.options[0] = new Option( 'Loading...', '' );
  
  ajaxpack.getAjaxRequest('/ajax-manufacturers-list.php', '', populate_select, 'txt');
}

function validateForm(){

  if (document.srcplug_basic_form.maker_F.options[document.srcplug_basic_form.maker_F.selectedIndex].value == '')  {
    alert("Please select a Manufacturer");
  } else if (parseInt(document.srcplug_basic_form.to_price_F.options[document.srcplug_basic_form.to_price_F.selectedIndex].value) < parseInt(document.srcplug_basic_form.from_price_F.options[document.srcplug_basic_form.from_price_F.selectedIndex].value)) {
    alert("To Price (£"+document.srcplug_basic_form.to_price_F.options[document.srcplug_basic_form.to_price_F.selectedIndex].value+") cannot be less than From Price (£"+document.srcplug_basic_form.from_price_F.options[document.srcplug_basic_form.from_price_F.selectedIndex].value+")");
  } else if (parseInt(document.srcplug_basic_form.to_age_F.options[document.srcplug_basic_form.to_age_F.selectedIndex].value) < parseInt(document.srcplug_basic_form.from_age_F.options[document.srcplug_basic_form.from_age_F.selectedIndex].value)) {
    alert("To Age cannot be less than From Age");
  } else { 
   document.srcplug_basic_form.submit();  
  }  
}

