function createUrl(script, param) {
	var strQueryString = location.href.substr(0,location.href.lastIndexOf("/")).toLowerCase();
	alert(strQueryString);
	return strQueryString+"/scripts/osa/"+script+"?q="+param;
}

function createUrl2(cmd, param) {
	return "complete.php?t="+cmd+"&q="+param;
}

function getModels(makeid,element_name,select_name,mode){
 plik = createUrl2("pobierz_modele",makeid);
 e = document.getElementById(element_name);
 bsel = 'buda_select'+mode;
 bid = 'bid'+mode;
 xml = null;
 e.innerHTML = "";
 try{
  xml = new ActiveXObject("Microsoft.XMLHTTP"); // IE
 }catch(e){
  try {
   xml = new XMLHttpRequest(); // Mozilla/FireFox/Opera
  }catch(e){
   xml = null;
  }
 }
 if(xml != null){
  xml.onreadystatechange = function(){
   if(xml.readyState==4){
    e.innerHTML = "<select class=\"W170 lblue_brd\" name=\""+select_name+"\" onchange=\"getVersions(this[this.selectedIndex].value,'"+bsel+"','"+bid+"',"+mode+")\"><option value=\"0\">wybierz model</option>"+xml.responseText+"</select>";
   }
  }
  xml.open("GET", plik, true);
  xml.send(null);

 }
 if(mode==1){
  document.xform.bid1.selectedIndex = 0;
  document.xform.engineid1.selectedIndex = 0;
  document.xform.yearfrom1.selectedIndex = 0;
  document.xform.yearto1.selectedIndex = 0;
 }else if(mode==2){
  document.xform.bid2.selectedIndex = 0;
  document.xform.engineid2.selectedIndex = 0;
  document.xform.yearfrom2.selectedIndex = 0;
  document.xform.yearto2.selectedIndex = 0;
 }
 return false;
}

function getVersions(modelid,element_name,select_name,mode){
 plik = createUrl2("pobierz_wersje",modelid);
 element = document.getElementById(element_name);
 esel = 'engine_select'+mode;
 eid = 'engineid'+mode;
 xml = null;
 element.innerHTML="";
 try{
  xml = new ActiveXObject("Microsoft.XMLHTTP"); // IE
 }catch(e){
  try {
   xml = new XMLHttpRequest(); // Mozilla/FireFox/Opera
  }catch(e){
   xml = null;
  }
 }
 if(xml != null){
  xml.onreadystatechange = function(){
   if(xml.readyState==4){
    element.innerHTML = "<select class=\"W170 lblue_brd\" name=\""+select_name+"\" onchange=\"getEngines(this[this.selectedIndex].value,'"+esel+"','"+eid+"',"+mode+")\"><option value=\"0\">wybierz wersję nadwozia</option>"+xml.responseText+"</select>";
   }
  }
  xml.open("GET", plik, true);
  xml.send(null);
 }
 if(mode==1){
  document.xform.engineid1.selectedIndex = 0;
  document.xform.yearfrom1.selectedIndex = 0;
  document.xform.yearto1.selectedIndex = 0;
 }else if(mode==2){
  document.xform.engineid2.selectedIndex = 0;
  document.xform.yearfrom2.selectedIndex = 0;
  document.xform.yearto2.selectedIndex = 0;
 }
 return false;
}

function getEngines(budaid,element_name,select_name,mode){
 plik = createUrl2("pobierz_silniki",budaid);
 element = document.getElementById(element_name);
 ysel = "years_select"+mode;
 yid1 = "yearfrom"+mode;
 yid2 = "yearto"+mode;
 xml = null;
 element.innerHTML="";
 try{
  xml = new ActiveXObject("Microsoft.XMLHTTP"); // IE
 }catch(e){
  try {
   xml = new XMLHttpRequest(); // Mozilla/FireFox/Opera
  }catch(e){
   xml = null;
  }
 }
 if(xml != null){
  xml.onreadystatechange = function(){
   if(xml.readyState==4){
    element.innerHTML = "<select class=\"W170 lblue_brd\" name=\""+select_name+"\" onchange=\"pobierz_roczniki(document.xform.modelid"+mode+"[document.xform.modelid"+mode+".selectedIndex].value,document.xform.bid"+mode+"[document.xform.bid"+mode+".selectedIndex].value,this[this.selectedIndex].value,"+mode+")\"><option value=\"0\">wybierz silnik</option>"+xml.responseText+"</select>";
   }
  }
  xml.open("GET", plik, true);
  xml.send(null);

 }
 return false;
}

function getYears(modelid,bid,engineid,element_name,select1_name,select2_name,mode){
 plik = createUrl2("xx_pobierz_roczniki",bid)+"&q3="+modelid+"&q2="+engineid+"&q4="+mode;
 element = document.getElementById(element_name);
 xml = null;
 element.innerHTML="";
 try{
  xml = new ActiveXObject("Microsoft.XMLHTTP"); // IE
 }catch(e){
  try {
   xml = new XMLHttpRequest(); // Mozilla/FireFox/Opera
  }catch(e){
   xml = null;
  }
 }
 if(xml != null){
  xml.onreadystatechange = function(){
   if(xml.readyState==4){
    element.innerHTML = xml.responseText;
   }
  }
  xml.open("GET", plik, true);
  xml.send(null);

 }
 return false;
}

function pobierz_roczniki(modelid,bid,engineid, selectPostfix){
 plik = createUrl2("xx_pobierz_roczniki",bid)+"&q3="+modelid+"&q2="+engineid;
 selectFrom = document.getElementById("yearfrom"+selectPostfix);
 selectTo = document.getElementById("yearto"+selectPostfix);

 xml = null;
 selectFrom.innerHTML="";
 selectTo.innerHTML="";
 try{
  xml = new ActiveXObject("Microsoft.XMLHTTP"); // IE
 }catch(e){
  try {
   xml = new XMLHttpRequest(); // Mozilla/FireFox/Opera
  }catch(e){
   xml = null;
  }
 }
 if(xml != null){
  xml.onreadystatechange = function(){
   if(xml.readyState==4){
	pos = xml.responseText.indexOf('|');
    selectFrom.innerHTML = "<select name=\"yearfrom"+selectPostfix+"\" class=\"W80 lblue_brd\">"+xml.responseText.substring(0,pos)+"</select>";
    selectTo.innerHTML = "<select name=\"yearto"+selectPostfix+"\" class=\"W80 lblue_brd\">"+xml.responseText.substring(pos+1)+"</select>";
   }
  }
  xml.open("GET", plik, true);
  xml.send(null);

 }
 return false;
}

function IsValid(){
 if(!document.xform.modelid1.selectedIndex||!document.xform.modelid2.selectedIndex){
  alert("Proszę wybrać modele porównywanych samochodów");
  if(!document.xform.modelid1.selectedIndex) document.xform.modelid1.focus();
  if(!document.xform.modelid2.selectedIndex) document.xform.modelid2.focus();
  return false;
 }
 return true;
}

