function window_onload() {
//	if (o_tipo==0 && document.form1.IndexModelo.value > 0) {
			PopulateModelos();
//	}
}

function enumero(a) {
a=form1.preco.value;
a=a.replace(" ","");
a=a.replace(",",".");
return a;
}

function testatudo(forma, serie) {
	var ab="";
	var z=99
	for (var i = 0; i < forma.elements.length; i++) {
		if (serie.substring(i, i+1)=="1" && forma.elements[i].value == "") {
				ab=ab + forma.elements[i].name + ", \n";
				if (i<z) {z=i;}
		}
	}
	if (ab=="") {
		forma.Enviar.disabled=true;
		form1.sit6.disabled=false;
		return true;
	} else {
		alert ("deve preenceher o(s) campo(s) de \n" + ab + "\n para colocar o seu carro à venda.");
		forma.elements[z].focus();
		return false;
	}
}

function criaCookie(nome,valor,dias) {
	if (dias) {
		var date = new Date();
		date.setTime(date.getTime()+(dias*24*60*60*1000));
		var expira = "; expira="+date.toGMTString();
	} else { var expira = ""; }

	document.cookie = nome+"="+valor+expira+"; path=/";
}


function leCookie(nome) {
	var nome_igual = nome + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nome_igual) == 0) return c.substring(nome_igual.length,c.length);
	}
	return null;
}

function apagaCookie(nome) {
	createCookie(nome,"",-1);
}

function guardar() {

var a="marka|idmarca|" +   document.form1.idmarca.value + "||"
a+="marka_md|idmodelo|"+ document.form1.idmodelo.value + "||"
a+="marka_ai|ano_i|"+ document.form1.ano_i.value + "||"
a+="marka_af|ano_f|"+ document.form1.ano_f.value + "||"
a+="marka_ei|euro_ini|"+ document.form1.euro_ini.value + "||"
a+="marka_ef|euro_fin|"+ document.form1.euro_fin.value + "||"
a+="marka_cb|comb|"+ document.form1.comb.value + "||"
a+="marka_ct|categoria|"+ document.form1.categoria.value + "||"
a+="marka_cvi|cv_i|"+ document.form1.cv_i.value + "||"
a+="marka_cvf|cv_f|"+ document.form1.cv_f.value+ "||"
a+="marka_iex|iex|"+ document.form1.iex.value;
try {
a=a+ "|" +"marka_km|km|"+ document.form1.km.value;
 } catch (e) {}
alert(a);
		criaCookie ("marka_gd", a, 5, "/");
}

function limpar() {
var a="idmarca|a|0 idmodelo|a|0 ano_i|a|1 ano_f|a|0 euro_ini|b|0 euro_fin|b|400000 comb|a|0 categoria|a|0 cv_i|b|0 cv_f|b|999 iexm|a|0 iex|a|0 km_in|b|0 concelho|b| ";
var ca=a.split(' ');
try { document.getElementById('concelho').value="";} catch (e) {}
try { 
	for (var i=0; i < document.form1.elements.length; i++) {
		if (document.form1.elements[i].name.substring(0,3)=="ex_") {
			document.form1.elements[i].checked=false;
		}
	}
} catch (e) {}

	for(var i=0;i < ca.length;i++) {
			ca_ck=ca[i].split('|');
			b=document.getElementById(ca_ck[0]);
		switch(ca_ck[1]){
			case "a":
try {
				if (ca_ck[2]==1){b.selectedIndex=(b.length-1);} else {b.selectedIndex=0;} } catch (e) {}
				break;
			case "b":
				b.value=ca_ck[2];
				break;
			}
	}
Marca_onchange();
}

function restaurar() {

		b = leCookie ("marka_gd");
alert(document.cookie)
//alert(b)
		ca= b.split('||');
	for(var i=0;i < ca.length;i++) {
			ca_ck=ca[i].split('|');
			criaCookie(ca_ck[0],ca_ck[2],5);
try {
el=document.getElementById(ca_ck[1]);

				el.value=ca_ck[2];

 } catch (e) {}
	}
PopulateModelos(c[1]);
}

function Marca_onchange() {
//	document.form1.IndexModelo.value = 0;
//document.getElementById("txtHint").innerHTML= "<SELECT NAME='idversao' class=idversao><option value=0>Primeiro escolha marca e o modelo.</OPTION></SELECT><br><br>Pode escolher várias versões selecionando na caixa acima com a tecla <b>Ctrl</b> pressionada";
		document.getElementById("scond").style.visibility="visible";
		document.getElementById("lver").style.visibility="hidden";
	PopulateModelos();
}

function restor_marca (idmarca){

}


function Modelo_onchange() {
try {
restable("idv");
 } catch (e) {}
	a="&mk=" + document.form1.idmarca.options[document.form1.idmarca.selectedIndex].text + "&md=" + document.form1.idmodelo.options[document.form1.idmodelo.selectedIndex].text;
	//document.form1.IndexModelo.value = document.form1.idmodelo.selectedIndex;
	//document.form1.idmodelo.value = document.form2.idmodelo.value;

//alert (document.form1.idmodelo.value);
	if (document.form1.idmodelo.value>0){
		v_get ("idmarca=" + document.form1.idmarca.value + "&idmodelo="+document.form1.idmodelo.value + a, "", 5);
		document.getElementById("scond").style.visibility="hidden";
		document.getElementById("lver").style.visibility="visible";
	}
}

function Modelo1_onchange() {
try {
restable("idv");
 } catch (e) {}
	a="&mk=" + document.form1.idmarca.options[document.form1.idmarca.selectedIndex].text + "&md=" + document.form1.idmodelo.options[document.form1.idmodelo.selectedIndex].text;
	//document.form1.IndexModelo.value = document.form1.idmodelo.selectedIndex;
	//document.form1.idmodelo.value = document.form2.idmodelo.value;

//alert (document.form1.idmodelo.value);
	if (document.form1.idmodelo.value>0){
		v_get ("idmarca=" + document.form1.idmarca.value + "&idmodelo="+document.form1.idmodelo.value + a, "../", 17);
		document.getElementById("scond").style.visibility="hidden";
		document.getElementById("lver").style.visibility="visible";
		document.form1.filtra.focus();
		document.form1.filtra.select();
	}
}


function PopulateModelos(idmodel_o) {

	var index = document.form1.idmarca.selectedIndex;
	var marca = document.form1.idmarca.options[index].value;
	//var idmodelo=document.form1.IndexModelo.value;
	document.form1.idmodelo.options.length = 1;
	document.form1.idmodelo.options[0] = new Option("Modelo", "0");
	var indice=0;
	var aModelos = sModelos.split("||");
	var n = 1;
	for (var i = 0; i < aModelos.length; i++) {

		var a = aModelos[i].split("|");
		if (a[0] == marca) {
			document.form1.idmodelo.options[n] = new Option(a[2], a[1]);
			if (a[1]==idmodel_o) { 
				indice=n;
			}
			n = n + 1;
		}
	}
	document.form1.idmodelo.selectedIndex = indice;
//	alert (indice);
	if (n>=1) {
		Modelo_onchange();
	}
}


var xmlHttp
var sVersao=""

function v_get (str, niv, altura){ 

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Neste browser não funciona")
		return
	} 
	var url= niv + "conteudo/get_versao1.php";
//alert (document.form1.idversao.value);
	url=url + "?" + str //+ "&idversao=" + document.form1.idversao.value;
	url=url+"&altura=" + altura;
	url=url+"&sid="+Math.random();
	url=url+"&an=" + document.form1.an.value;

	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function stateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
//		if (xmlHttp.status == 200) {
try {
restable("idv");
 } catch (e) {}
var meutexto=xmlHttp.responseText;

	var root = meutexto.split("||");
	var n = 1;
	for (var i = 0; i < root.length -1; i++) {
			var celu = root[i].split("|");
            addrow("idv", celu);
	}
//	xd=document.getElementById("txtHint");
//xd.innerHTML=meutexto
//		sVersao=document.form1.constroi.value; 
//		PopulateMod(document.form1.ano.value);
	} 
} 

function restable(tablename) {
   var tbl = document.getElementById(tablename);
   var lastRow = tbl.rows.length;
	for (i = lastRow -1; i >= 0; i--) {
		tbl.deleteRow(i);
	}
}

function addrow(tablename, arr) {
   var tbl = document.getElementById(tablename);
   var lastRow = tbl.rows.length;
   var row = tbl.insertRow(lastRow);
				try {
	row.name = arr[0];
	row.style.cursor="pointer";
	row.style.backgroundColor="#fff";
	row.onmouseover=function(){this.style.backgroundColor="#cccccc";}
	row.onmouseout=function(){
				ab=document.getElementById('idv_ch_'+row.rowIndex);
				if (ab.checked) {this.style.backgroundColor="#3366cc";} else {this.style.backgroundColor="#ffffff";}
							}
	row.onclick=function(event){	
						ab=document.getElementById('idv_ch_'+row.rowIndex);

						ab.checked=!ab.checked;
						this.style.backgroundColor="#3366cc";
						}
} catch (e) {}

         var cell = row.insertCell(0);
		cell.innerHTML = "<input name=\"idversao[]\" id=\"idv_ch_"+row.rowIndex+"\" type=radio VALUE=\""+arr[0]+"\" onclick=\"this.checked=!this.checked;\" class=chkbx></input>";
		cell.className = "idv_bb";
         var cell = row.insertCell(1);
         cell.innerHTML = arr[1];
		 cell.className = "idv_bb";
         var cell = row.insertCell(2);
         cell.innerHTML = arr[2];
		 cell.className = "idv_bb";
}

function multiselect_selectall(name,val){
	var els=document.getElementsByTagName('input');
	var cor_bk=val?"#3366cc":"#ffffff";
		var j=0;
	for(var i=0;i<els.length;++i)if((els[i].name==name+'[]'||els[i].name==name)){
		els[i].checked=val;
		document.getElementById('idv').tBodies[0].rows[j].style.backgroundColor=cor_bk;
		j++;
	}
}


function filtra_este(valor){
if (valor.value=="") { multiselect_selectall('idversao',false); return;}
v_txt=valor.value.toUpperCase();
	var bob=document.getElementById('idv');


//for( var z = 0; z < bob.tBodies.length; z++ ) {
	for( var x = 0; x < bob.tBodies[0].rows.length; x++ ) {
var tex=bob.tBodies[0].rows[x].cells[1].innerHTML.toUpperCase();
	bob.tBodies[0].rows[x].style.backgroundColor="#ffffff"
	var ab=document.getElementById('idv_ch_'+x);

	if (tex.indexOf(v_txt)!=-1){
		bob.tBodies[0].rows[x].style.backgroundColor="#3366cc";
	ab.checked=true;
	} else {	ab.checked=false;}
		
	}
//}

}

function GetXmlHttpObject(){ 
	var objXMLHttp=null
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest()
         if (objXMLHttp.overrideMimeType) {
            objXMLHttp.overrideMimeType('text/xml');
         }
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")

         try {
            objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }

	}
	return objXMLHttp
}



