

var xmlhttp=false;

function espere(){
	return "<p class='filtroRojo'>Cargando...</p>";
}

function waitDetails(){
	return "<p class='filtroRojo'>Cargando...</p>";
}
function waitfoto(){
	return "<p class='filtroRojo'>Cargando...</p>";
}


function objetoAjax(){
xmlhttp=false;
try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (E) {
             xmlhttp = false;
      }
    }

    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
      xmlhttp = new XMLHttpRequest();
    }
    //alert(xmlhttp)
    return xmlhttp;
}


function callAjax(page){
		
      divResultado = document.getElementById('resultado');
      ajax=objetoAjax();
      ajax.open("GET",  page + ".php");
      ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('resultado').innerHTML = espere();
		}
         if (ajax.readyState==4) {
            divResultado.innerHTML = ajax.responseText
         }
      }
      ajax.send(null)
}

function getFiltro(params){
	reg_codigo 		= document.getElementById("p_region")[document.getElementById("p_region").selectedIndex].value;
	ciu_codigo		= document.getElementById("p_ciudad")[document.getElementById("p_ciudad").selectedIndex].value;
	cna_codigo		= document.getElementById("p_comuna")[document.getElementById("p_comuna").selectedIndex].value;
	
	
	tip_codigo 		= document.getElementById("p_tipo")[document.getElementById("p_tipo").selectedIndex].value;
	mar_codigo		= document.getElementById("selectMarca")[document.getElementById("selectMarca").selectedIndex].value;	
	mod_codigo		= document.getElementById("hijomodelo")[document.getElementById("hijomodelo").selectedIndex].value;
	com_codigo		= document.getElementById("p_combustible")[document.getElementById("p_combustible").selectedIndex].value;
	tra_codigo		= document.getElementById("p_transmision")[document.getElementById("p_transmision").selectedIndex].value;
	palabra			= document.getElementById("p_palabra").value;
	reg_codigo		= document.getElementById("p_region")[document.getElementById("p_region").selectedIndex].value;
	anodesde		= document.getElementById("p_anodesde")[document.getElementById("p_anodesde").selectedIndex].value;
	anohasta		= document.getElementById("p_anohasta")[document.getElementById("p_anohasta").selectedIndex].value;
	preciodesde		= document.getElementById("p_preciodesde")[document.getElementById("p_preciodesde").selectedIndex].value;
	preciohasta		= document.getElementById("p_preciohasta")[document.getElementById("p_preciohasta").selectedIndex].value;
	consumodesde		= document.getElementById("p_consumodesde")[document.getElementById("p_consumodesde").selectedIndex].value;
	consumohasta		= document.getElementById("p_consumohasta")[document.getElementById("p_consumohasta").selectedIndex].value;
	vendedor		= document.getElementById("p_tipovendedor")[document.getElementById("p_tipovendedor").selectedIndex].value;
	kmdesde		= document.getElementById("p_kmdesde")[document.getElementById("p_kmdesde").selectedIndex].value;
	kmhasta		= document.getElementById("p_kmhasta")[document.getElementById("p_kmhasta").selectedIndex].value;
		
	params = "?tip_codigo="+tip_codigo+"&mar_codigo="+mar_codigo+
	"&tip_codigo=" + tip_codigo +
	"&mar_codigo=" + mar_codigo+
	"&mod_codigo=" + mod_codigo+
	"&com_codigo=" + com_codigo+
	"&tra_codigo=" + tra_codigo+
	"&palabra=" + palabra+
	"&anodesde=" + anodesde+
	"&anohasta=" + anohasta+
	"&preciodesde=" + 	preciodesde+
	"&preciohasta=" + preciohasta+
	"&consumodesde=" + consumodesde+
	"&consumohasta=" + consumohasta+
	"&vendedor=" + vendedor+
	"&kmdesde=" + kmdesde+
	"&kmhasta=" + kmhasta +
	"&reg_codigo=" + reg_codigo +
	"&ciu_codigo=" + ciu_codigo +
	"&cna_codigo=" + cna_codigo;
	
	return params;
}

function callAjaxData(p){

	pageTracker._trackPageview("/getdata" );

	params = getFiltro('') + p;
	closeDetails();
	posicionTop();
	divResultado = document.getElementById('resultado');
	ajax=objetoAjax();
	ajax.open("GET",  "getdata.php"+params);
	ajax.onreadystatechange=function() {
	if(ajax.readyState!=4){
		document.getElementById('resultado').innerHTML = espere();
	}
	 if (ajax.readyState==4) {
		divResultado.innerHTML = ajax.responseText
	 }
	}
	ajax.send(null)
}

function callAjaxLoadCity(reg){
	ajax=objetoAjax();
	pagina = "getciudades.php?reg_codigo=" + reg;
	ajax.open("GET", pagina);	
    ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('ciudad').innerHTML = espere();
		}
		if (ajax.readyState==4) {				
			document.getElementById('ciudad').innerHTML = ajax.responseText;
		}	
    }	
    ajax.send(null);	
}

function callAjaxLoadCityUpdate(reg){
	ajax=objetoAjax();
	pagina = "getciudadesUPDATE.php?reg_codigo=" + reg;
	ajax.open("GET", pagina);	
    ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('newciudad').innerHTML = espere();
		}
		if (ajax.readyState==4) {				
			document.getElementById('newciudad').innerHTML = ajax.responseText;
		}	
    }	
    ajax.send(null);	
}

function callAjaxLoadCityNew(reg){
	ajax=objetoAjax();
	pagina = "getciudadesNEW.php?reg_codigo=" + reg;
	ajax.open("GET", pagina);	
    ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('newciudad').innerHTML = espere();
		}
		if (ajax.readyState==4) {				
			document.getElementById('newciudad').innerHTML = ajax.responseText;
		}	
    }	
    ajax.send(null);	
}

function callAjaxLoadComunas(ciu){
	ajax=objetoAjax();
	pagina = "getcomunas.php?ciu_codigo=" + ciu;
	ajax.open("GET", pagina);	
    ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('comuna').innerHTML = espere();
		}
		if (ajax.readyState==4) {				
			document.getElementById('comuna').innerHTML = ajax.responseText;
		}	
    }	
    ajax.send(null);	
}

function callAjaxLoadComunasNew(ciu){
	ajax=objetoAjax();
	pagina = "getcomunasNEW.php?ciu_codigo=" + ciu;
	ajax.open("GET", pagina);	
    ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('newcomuna').innerHTML = espere();
		}
		if (ajax.readyState==4) {				
			document.getElementById('newcomuna').innerHTML = ajax.responseText;
		}	
    }	
    ajax.send(null);	
}


function removeModelos() {	
		
	obj = document.getElementById('hijomodelo');
	if(obj.length>0){
		for(i=obj.length;i>=0;i--){
			obj.options[i]=null;
		}
		obj.options[0] = new Option("Todos","0");
	}
}

function removeComunas() {	
		
	obj = document.getElementById('p_comuna');
	if(obj.length>0){
		for(i=obj.length;i>=0;i--){
			obj.options[i]=null;
		}
		obj.options[0] = new Option("Todos","0");
	}
}

function removeComunasNew() {	
		
	obj = document.getElementById('new_comuna');
	if(obj.length>0){
		for(i=obj.length;i>=0;i--){
			obj.options[i]=null;
		}
		obj.options[0] = new Option("Todos","0");
	}
}

function removeModelosNew() {	
		
	obj = document.getElementById('newselectmodelo');
	if(obj.length>0){
		for(i=obj.length;i>=0;i--){
			obj.options[i]=null;
		}
		obj.options[0] = new Option("Todos","0");
	}
}

function callAjaxModelos(marca){
	ajax2=objetoAjax();
	pagina = "getmodelos.php?mar_codigo=" + marca;
	ajax2.open("GET", pagina);	
    ajax2.onreadystatechange=function() {
		if(ajax2.readyState!=4){
			document.getElementById('cmodelo').innerHTML = espere();
		}
		if (ajax2.readyState==4) {				
			document.getElementById('cmodelo').innerHTML = ajax2.responseText;
		}	
    }	
    ajax2.send(null);	
	
}

function callAjaxMarca(tipoveh){
	ajax2=objetoAjax();
	pagina = "getmarcas.php?tip_codigo=" + tipoveh ;
	//alert(pagina)
	ajax2.open("GET", pagina);	
    ajax2.onreadystatechange=function() {
		if(ajax2.readyState!=4){
			document.getElementById('cmarcas').innerHTML = espere();
		}
		if (ajax2.readyState==4) {				
			document.getElementById('cmarcas').innerHTML = ajax2.responseText;
		}	
    }
    ajax2.send(null);
}


function callAjaxMarcaNew(tipoveh){
	ajax2=objetoAjax();
	pagina = "getmarcasNEW.php?tip_codigo=" + tipoveh ;
	//alert(pagina)
	ajax2.open("GET", pagina);	
    ajax2.onreadystatechange=function() {
		if(ajax2.readyState!=4){
			document.getElementById('cmarcasnew').innerHTML = espere();
		}
		if (ajax2.readyState==4) {				
			document.getElementById('cmarcasnew').innerHTML = ajax2.responseText;
		}	
    }
    ajax2.send(null);
}


function callAjaxRemoveVersion(){
	obj = document.getElementById('hijoversion');
	if(obj.length>0){
		for(i=obj.length;i>=0;i--){
			obj.options[i]=null;
		}
		obj.options[0] = new Option("Todos","0");
	}
}

function removeElement(divNum) {
  var d = document.getElementById('resultado');
  var olddiv = document.getElementById(divNum);
  
	if(olddiv!=null){
		d.removeChild(olddiv);
	}
}


// NUEVO AVISO
function newAviso(){
	genericAjaxCaller("newaviso");
}


function callAjaxModelosNew(marca){
	ajax2=objetoAjax();
	pagina = "getmodelosNEW.php?mar_codigo=" + marca;
	ajax2.open("GET", pagina);	
    ajax2.onreadystatechange=function() {
		if (ajax2.readyState==4) {				
			document.getElementById('newmodelo').innerHTML = ajax2.responseText;
		}	
    }	
    ajax2.send(null);		
}
var iniciarDespliegue = false;
function posicionTop() {
  if (!iniciarDespliegue) {
	  barraTop();
	  iniciarDespliegue = true;
  }
}

function barraTop() {
  var posiciontop = document.getElementById('posiciontop');
  ajaxP2 = caller();
  ajaxP2.open("POST", "pub/publicidad.php", true);

  ajaxP2.onreadystatechange = function() {
    if (ajaxP2.readyState == 4 && ajaxP2.status == 200) {
      posiciontop.innerHTML = ajaxP2.responseText;
    }
  }
  ajaxP2.send(null);
  setTimeout(barraTop, 1000 * 30);
}

function newcallAjaxVersiones(marca, modelo){
	ajax2=objetoAjax();
	pagina = "getversionnew.php?mar_codigo=" + marca + "&mod_codigo="+modelo;
	//alert(pagina)
	ajax2.open("GET", pagina);	
    ajax2.onreadystatechange=function() {
		if (ajax2.readyState==4) {				
			document.getElementById('newversion').innerHTML = ajax2.responseText;
		}	
    }
    ajax2.send(null);
}




// NUEVO guardar
function saveAviso(){
	pageTracker._trackPageview("/new" );
	
	if(!validaFormNuevoAviso()){
		return false;
	}

	reg_codigo		= document.getElementById("new_region")[document.getElementById("new_region").selectedIndex].value;
	ciu_codigo		= document.getElementById("new_ciudad")[document.getElementById("new_ciudad").selectedIndex].value;
	cna_codigo		= document.getElementById("new_comuna")[document.getElementById("new_comuna").selectedIndex].value;
	
	usu_codigo 		= document.getElementById("new_rut").value;
	avi_patente 	= document.getElementById("new_patente").value;
	tip_codigo 		= document.getElementById("new_tipo")[document.getElementById("new_tipo").selectedIndex].value;
	mar_codigo		= document.getElementById("newselectMarca")[document.getElementById("newselectMarca").selectedIndex].value;	
	mod_codigo 		= document.getElementById("newselectmodelo")[document.getElementById("newselectmodelo").selectedIndex].value;
	com_codigo 		= document.getElementById("new_combustible")[document.getElementById("new_combustible").selectedIndex].value;
	tra_codigo 		= document.getElementById("new_transmision")[document.getElementById("new_transmision").selectedIndex].value;
	avi_puertas		= document.getElementById("new_puertas")[document.getElementById("new_puertas").selectedIndex].value;
	avi_ano			= document.getElementById("new_ano").value;
	avi_consumo		= document.getElementById("new_consumo").value;
	avi_kilometros	= document.getElementById("new_kilometros").value;
	avi_tipovendedor = document.getElementById("new_tipovendedor")[document.getElementById("new_tipovendedor").selectedIndex].value;
	avi_precio		= document.getElementById("new_precio").value;
	avi_moneda		= document.getElementById("new_moneda")[document.getElementById("new_moneda").selectedIndex].value;
	avi_potencia	= document.getElementById("new_potencia").value;
	avi_unicodu		= document.getElementById("new_unico")[document.getElementById("new_unico").selectedIndex].value;
	avi_nuevo 		= document.getElementById("new_nuevo")[document.getElementById("new_nuevo").selectedIndex].value;
	col_codigo		= document.getElementById("new_color")[document.getElementById("new_color").selectedIndex].value;
	avi_comentario	= document.getElementById("new_comentario").value;
	
	usu_nombre		= document.getElementById("new_nombre").value;
	usu_apellidos	= document.getElementById("new_apellidos").value;
	usu_email		= document.getElementById("new_email").value;
	usu_fonos		= document.getElementById("new_telefonos").value;
	
	
	
	//atr	= document.getElementById("atr").value;
	atributos="";
	veces = 0;
	arreglo = document.getElementsByTagName('input');
	for(i=0;i<arreglo.length;i++){
		if(arreglo[i].name="atr" && arreglo[i].checked){
			atributos = atributos + ((veces==0)? arreglo[i].value : "," + arreglo[i].value);
			veces++;
		}
	}
	
	
	parametros = "?usu_codigo="+ usu_codigo +
	"&avi_patente="+ avi_patente +
	"&tip_codigo="+ tip_codigo +
	"&mar_codigo="+ mar_codigo +
	"&mod_codigo="+ mod_codigo +
	//"&ver_codigo="+ ver_codigo +
	"&com_codigo="+ com_codigo +
	"&tra_codigo="+ tra_codigo +
	"&avi_puertas="+ avi_puertas +
	"&avi_ano="+ avi_ano +
	"&avi_consumo="+ avi_consumo +
	"&avi_kilometros="+ avi_kilometros +
	"&avi_tipovendedor="+ avi_tipovendedor +
	"&avi_precio="+ avi_precio +
	"&avi_moneda="+ avi_moneda +
	"&avi_potencia="+ avi_potencia +
	"&avi_unicodu="+ avi_unicodu +
	"&avi_nuevo="+ avi_nuevo +
	"&col_codigo="+ col_codigo +
	"&avi_comentario="+ avi_comentario +
	"&atr=" + atributos +
	"&usu_nombre=" + usu_nombre +
	"&usu_apellidos=" + usu_apellidos +
	"&usu_email=" + usu_email +
	"&usu_fonos=" +  usu_fonos + 
	"&reg_codigo=" + reg_codigo +
	"&ciu_codigo=" + ciu_codigo +
	"&cna_codigo=" + cna_codigo;
 
	ajax2=objetoAjax();	
	ajax2.open("GET", "saveaviso.php" + parametros);	
    ajax2.onreadystatechange=function() {
		if(ajax2.readyState!=4){
			document.getElementById('resultado').innerHTML = espere();
		}
		if (ajax2.readyState==4) {				
			document.getElementById('resultado').innerHTML = ajax2.responseText;
		}	
    }	
    ajax2.send(null);

}

function updateAviso(){
    genericAjaxCaller('updateaviso');
}
function updateinfo(){
    genericAjaxCaller('updateinfo');
}
function terminos(){
    genericAjaxCaller('terminos');
}


function genericAjaxCaller(page){
	 closeDetails();
	 try {cerrarEnviarAmigo(); } catch(e) { alert(e);}
	ajax=objetoAjax();	
	ajax.open("GET", page +".php");	
    ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('resultado').innerHTML = espere();
		}
		if (ajax.readyState==4) {				
			document.getElementById('resultado').innerHTML = ajax.responseText;
		}	
    }
    ajax.send(null);
}

function details(p,a){		  
 		pageTracker._trackPageview("/details" );
 		try {cerrarEnviarAmigo(); } catch(e) { alert(e);}
		
		if (document.getElementById("detalles") == null){
			self.location = "details.php?avi_patente="+p+"&avi_codigo="+a;
			return;
		}
		document.getElementById("detalles").style.display ='';
		posicionTop();
		ajax=objetoAjax();
		ajax.open("GET",  "details.php?avi_patente="+p+"&avi_codigo="+a+"&cerrar=0");
		ajax.onreadystatechange=function() {		
				
				if (ajax.readyState==4) {
				  document.getElementById('detalles').innerHTML = ajax.responseText
				}else{
					document.getElementById('detalles').innerHTML = waitDetails();
				}
			}
      ajax.send(null);
      
}

function closeDetails(){
	document.getElementById('detalles').innerHTML = "";
}

function detailsVisit(a,b){
	pageTracker._trackPageview("/lastvisit" );
	posicionTop();
	document.getElementById('resultado').innerHTML = "";
	details(a,b);
}

function displayfoto(p){		  
	pageTracker._trackPageview("/verfoto" );
	document.getElementById('displayfoto').innerHTML = "<img src=watermark.php?i="+p+" border=0 style='border:1px solid navy;'>";
}

function callAjaxGetMyData(){
		
	ajax=objetoAjax();
	rut		= document.getElementById("usu_codigo").value;
	pass	= document.getElementById("usu_password").value;
	params = "?usu_codigo="+rut+"&usu_password="+pass;
	
	ajax.open("GET",  "getmydata.php"+params);
	ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('resultado').innerHTML = waitDetails();
		}
		if (ajax.readyState==4) {
			document.getElementById('resultado').innerHTML = ajax.responseText
		}
	}
    ajax.send(null)
}

function saveMyData(){
	ajax=objetoAjax();
	
	un		= document.getElementById("update_usu_nombre").value;
	ua		= document.getElementById("update_usu_apellidos").value;
	ue		= document.getElementById("update_usu_email").value;
	uf		= document.getElementById("update_usu_fonos").value;
	user	= document.getElementById("update_usu_codigo").value;
	pass	= document.getElementById("update_usu_password").value;
	/*
	pass1	= document.getElementById("update_nuevapass1").value;
	pass2	= document.getElementById("update_nuevapass2").value;
	*/
	params = "?usu_codigo="+user+"&usu_password="+pass+"&usu_nombre="+un+"&usu_apellidos="+ua+"&usu_email="+ue+"&usu_fonos="+uf;
		//"&pass1="+pass1+"&pass2="+pass2;
	
	//alert(params)
	ajax.open("GET",  "savemydata.php"+params);
	ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('resultado').innerHTML = waitDetails();
		}
		if (ajax.readyState==4) {
			document.getElementById('resultado').innerHTML = ajax.responseText
		}
	}
    ajax.send(null)
}

function callAjaxFindAvisos(){
	pageTracker._trackPageview("/buscaravisospropios" );	
	ajax=objetoAjax();
	rut		= document.getElementById("usu_codigo").value;
	pass	= document.getElementById("usu_password").value;
	params = "?usu_codigo="+rut+"&usu_password="+pass;
	
	ajax.open("GET",  "findupdateaviso.php"+params);
	ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('resultado').innerHTML = waitDetails();
		}
		if (ajax.readyState==4) {
			document.getElementById('resultado').innerHTML = ajax.responseText
		}
	}
    ajax.send(null)
}

function callAjaxDeleteAviso(){
	if(isSelectedOne()){
		
		if(confirm("¿está seguro de eliminar el aviso?")){
			ajax=objetoAjax();
			
			rut		= document.getElementById("update_usu_codigo").value;
			pass	= document.getElementById("update_usu_password").value;
			avc	= document.getElementById("update_avi_codigo").value;
			
			params = "?usu_codigo="+rut+"&usu_password="+pass+"&avi_codigo="+avc;
			
			ajax.open("GET",  "deleteaviso.php"+params);
			ajax.onreadystatechange=function() {
				if(ajax.readyState!=4){
					document.getElementById('resultado').innerHTML = waitDetails();
				}
				if (ajax.readyState==4) {
					document.getElementById('resultado').innerHTML = ajax.responseText
				}
			}
			ajax.send(null)
		}
	}	
}

function callAjaxEditAviso(){

	if(isSelectedOne()){
		
		ajax=objetoAjax();
		rut		= document.getElementById("update_usu_codigo").value;
		pass	= document.getElementById("update_usu_password").value;
		avc	= document.getElementById("update_avi_codigo").value;
		
		params = "?usu_codigo="+rut+"&usu_password="+pass+"&avi_codigo="+avc;
		
		ajax.open("GET",  "getavisoforupdate.php"+params);
		ajax.onreadystatechange=function() {
			if(ajax.readyState!=4){
				document.getElementById('resultado').innerHTML = waitDetails();
			}
			if (ajax.readyState==4) {
				document.getElementById('resultado').innerHTML = ajax.responseText
			}
		}
		ajax.send(null)
	}
}


function callAjaxSaveEditAviso(){
	ajax=objetoAjax();
	rut		= document.getElementById("update_usu_codigo").value;
	pass	= document.getElementById("update_usu_password").value;
	avc	= document.getElementById("update_avi_codigo").value;
	
	params = "?usu_codigo="+rut+"&usu_password="+pass+"&avi_codigo="+avc;
	
	ajax.open("GET",  "getavisoforupdate.php"+params);
	ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('resultado').innerHTML = waitDetails();
		}
		if (ajax.readyState==4) {
			document.getElementById('resultado').innerHTML = ajax.responseText
		}
	}
    ajax.send(null)
}

function callAjaxSaveAvisoUpdated(){
	pageTracker._trackPageview("/actualizaraviso" );
	if(!validaFormEditaAviso()){
		return false;
	}
	
	usu_codigo 	= document.getElementById("hidden_usu_codigo").value;
	avi_codigo 		= document.getElementById("hidden_avi_codigo").value;
	usu_password	= document.getElementById("hidden_usu_password").value;	
	
	reg_codigo		= document.getElementById("new_region")[document.getElementById("new_region").selectedIndex].value;	
	ciu_codigo		= document.getElementById("new_ciudad")[document.getElementById("new_ciudad").selectedIndex].value;
	cna_codigo		= document.getElementById("new_comuna")[document.getElementById("new_comuna").selectedIndex].value;
	avi_patente 	= document.getElementById("new_patente").value;
	tip_codigo 		= document.getElementById("new_tipo")[document.getElementById("new_tipo").selectedIndex].value;
	mar_codigo		= document.getElementById("newselectMarca")[document.getElementById("newselectMarca").selectedIndex].value;	
	mod_codigo 		= document.getElementById("newselectmodelo")[document.getElementById("newselectmodelo").selectedIndex].value;
	com_codigo 		= document.getElementById("new_combustible")[document.getElementById("new_combustible").selectedIndex].value;
	tra_codigo 		= document.getElementById("new_transmision")[document.getElementById("new_transmision").selectedIndex].value;
	avi_puertas		= document.getElementById("new_puertas")[document.getElementById("new_puertas").selectedIndex].value;
	avi_ano			= document.getElementById("new_ano").value;
	avi_consumo		= document.getElementById("new_consumo").value;
	avi_kilometros	= document.getElementById("new_kilometros").value;
	avi_tipovendedor = document.getElementById("new_tipovendedor")[document.getElementById("new_tipovendedor").selectedIndex].value;
	avi_precio		= document.getElementById("new_precio").value;
	avi_moneda		= document.getElementById("new_moneda")[document.getElementById("new_moneda").selectedIndex].value;
	avi_potencia	= document.getElementById("new_potencia").value;
	avi_unicodu		= document.getElementById("new_unico")[document.getElementById("new_unico").selectedIndex].value;
	avi_nuevo 		= document.getElementById("new_nuevo")[document.getElementById("new_nuevo").selectedIndex].value;
	col_codigo		= document.getElementById("new_color")[document.getElementById("new_color").selectedIndex].value;
	avi_comentario	= document.getElementById("new_comentario").value;
	
	//atr	= document.getElementById("atr").value;
	atributos="";
	veces = 0;
	arreglo = document.getElementsByTagName('input');
	for(i=0;i<arreglo.length;i++){
		if(arreglo[i].name="atr" && arreglo[i].checked){
			atributos = atributos + ((veces==0)? arreglo[i].value : "," + arreglo[i].value);
			veces++;
		}
	}	
	
	parametros = "?usu_codigo="+ usu_codigo +
	"&usu_password="+ usu_password +
	"&avi_codigo="+ avi_codigo +
	"&avi_patente="+ avi_patente +
	"&tip_codigo="+ tip_codigo +
	"&mar_codigo="+ mar_codigo +
	"&mod_codigo="+ mod_codigo +
	//"&ver_codigo="+ ver_codigo +
	"&com_codigo="+ com_codigo +
	"&tra_codigo="+ tra_codigo +
	"&avi_puertas="+ avi_puertas +
	"&avi_ano="+ avi_ano +
	"&avi_consumo="+ avi_consumo +
	"&avi_kilometros="+ avi_kilometros +
	"&avi_tipovendedor="+ avi_tipovendedor +
	"&avi_precio="+ avi_precio +
	"&avi_moneda="+ avi_moneda +
	"&avi_potencia="+ avi_potencia +
	"&avi_unicodu="+ avi_unicodu +
	"&avi_nuevo="+ avi_nuevo +
	"&col_codigo="+ col_codigo +
	"&avi_comentario="+ avi_comentario +
	"&atr=" + atributos +	
	"&reg_codigo=" + reg_codigo +
	"&ciu_codigo=" + ciu_codigo +
	"&cna_codigo=" + cna_codigo;
	
	 
	ajax2=objetoAjax();	
	ajax2.open("GET", "saveupdateaviso.php" + parametros);	
    ajax2.onreadystatechange=function() {
		if(ajax2.readyState!=4){
			document.getElementById('resultado').innerHTML = espere();
		}
		if (ajax2.readyState==4) {				
			document.getElementById('resultado').innerHTML = ajax2.responseText;
		}	
    }	
    ajax2.send(null);
}


function validaFormNuevoAviso(){
		
	o = document.getElementById("new_region");	
	if(o.selectedIndex==0){
		alert('Seleccione una región.', 'Error en el registro de datos');	
		return false;		
	}	
	o = document.getElementById("new_ciudad");
	if(o.selectedIndex==0){
		alert('Seleccione una ciudad.');	
		return false;		
	}
	o = document.getElementById("new_comuna");
	if(o.selectedIndex==0){
		alert('Seleccione una comuna.');	
		return false;		
	}
	
	o = document.getElementById("new_tipo");	
	if(o.selectedIndex==0){
		alert('Seleccione el tipo de vehiculo.');	
		return false;		
	}	
	o = document.getElementById("new_marca");
	if (o == null)  o = document.getElementById("newselectMarca");
	if(o.selectedIndex==0){
		alert('Seleccione la marca del vehiculo.');	
		return false;		
	}
	o = document.getElementById("new_modelo");	
	if (o == null)  o = document.getElementById("newselectmodelo");
	if(o.selectedIndex==0){
		alert('Seleccione el modelo del vehiculo.');	
		return false;		
	}
	
	o = document.getElementById("new_color");	
	if(o.selectedIndex==0){
		alert('Seleccione el color del vehiculo.');	
		return false;		
	}
	
	
	o1 = document.getElementById("new_consumo");
	if( (o1.value!="") && (!isInteger(o1.value)) && (!isDecimal(o1.value))){
		alert('Ingreso un número válido en el consumo.');	
		o1.value="";
		o1.focus();
		return false;		
	}
	
	o1 = document.getElementById("new_kilometros");
	if( (o1.value!="") && (!isInteger(o1.value))){
		alert('Ingreso un número válido en el kilometraje.');	
		o1.focus();
		o1.value="";
		return false;		
	}
	o1 = document.getElementById("new_precio");
	if( (o1.value!="") && (!isInteger(o1.value))){
		alert('Ingrese el precio, sin puntos ni espacios.');	
		o1.focus();
		o1.value="";
		return false;		
	}
	o1 = document.getElementById("new_potencia");
	if( (o1.value!="") && (!isInteger(o1.value)) && (!isDecimal(o1.value))){
		alert('Ingrese un número válido en la colindrada, use punto(.)');	
		o1.focus();
		o1.value="";
		return false;		
	}
	o1 = document.getElementById("new_patente");
	if( o1.value=="" || !isAlphaLatin(o1.value)){
		alert('La patente es inválida. Es un campo abligatorio');	
		o1.value="";
		o1.focus();
		return false;		
	}
	
	o1 = document.getElementById("new_comentario");
	if( o1.value.length >300){
		alert('El largo máximo del comentario es de 300 caracteres.');			
		o1.focus();
		return false;		
	}
	
	try{
		o1 = document.getElementById("new_nombre");
		if( o1.value=="" || o1.length<3 ){
			alert('El nombre no es válido. Es un campo abligatorio');	
			o1.value="";
			o1.focus();
			return false;		
		}
		o1 = document.getElementById("new_apellidos");
		if( o1.value=="" || o1.length<3 ){
			alert('Los apellidos no son válidos. Es un campo abligatorio');	
			o1.value="";
			o1.focus();
			return false;		
		}
		o1 = document.getElementById("new_rut");
		if( o1.value!="" && o1.value.length>2){
			dva = o1.value.substring(o1.value.length-1);
			cuerpo = o1.value.substring(0,o1.value.length-1);
			
			if(dva!=dv(cuerpo)){		
				alert('El rut es inválido. Es un campo abligatorio');	
				o1.focus();
				return false;
			}
		}else{
			alert('El rut es inválido. Es un campo abligatorio');	
			o1.value="";
			o1.focus();
			return false;
		}	
			
		o1 = document.getElementById("new_email");
		if( !isEMailAddr(o1.value)){
			alert('Ingrese un mail válido. Es un campo abligatorio');	
			o1.focus();
			return false;		
		}	
		
		o1 = document.getElementById("new_telefonos");
		if( o1.value==""){
			alert('Ingrese un teléfono de contacto. Es necesario para sea contactado por un comprador');	
			o1.value="";
			o1.focus();
			return false;		
		}
	}catch(e){
		alert('Es necesario validar su usuario');
		return false;
	}
	return true;
}

function validaFormEditaAviso(){
		
		
	o1 = document.getElementById("new_consumo");
	if( (o1.value!="") && (!isInteger(o1.value)) && (!isDecimal(o1.value))){
		alert('Ingreso un número válido en el consumo.');	
		o1.value="";
		o1.focus();
		return false;		
	}
	
	o1 = document.getElementById("new_kilometros");
	if( (o1.value!="") && (!isInteger(o1.value))){
		alert('Ingrese un número válido en el kilometraje.');	
		o1.focus();
		o1.value="";
		return false;		
	}
	
	o1 = document.getElementById("new_precio");
	if( (o1.value!="") && (!isInteger(o1.value))){
		alert('Ingrese el precio, sin puntos ni espacios.');	
		o1.focus();
		o1.value="";
		return false;		
	}
	o1 = document.getElementById("new_potencia");
	if( (o1.value!="") && (!isInteger(o1.value)) && (!isDecimal(o1.value))){
		alert('Ingrese un número válido en la colindrada, use punto(.)');	
		o1.focus();
		o1.value="";
		return false;		
	}
	
	o1 = document.getElementById("new_comentario");
	if( o1.value.length >300){
		alert('El largo máximo del comentario es de 300 caracteres.');			
		o1.focus();
		return false;		
	}
	
	
	return true;
}

function dv(T){var M=0,S=1;for(;T;T=Math.floor(T/10))
	S=(S+T%10*(9-M++%6))%11;return S?S-1:'k';
}

function isSelectedOne(){
	
	veces = 0;
	arreglo = document.getElementsByTagName('input');
	for(i=0;i<arreglo.length;i++){
		if(arreglo[i].name="avi_codigo_radio" && arreglo[i].checked){			
			veces++;
		}
	}
	if(veces==0){
		alert("Seleccione un aviso.");
		return false;
	}
	return true;
}


function isInteger(s) {
	return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function isDecimal(number){// positive or negative decimal
	if(!number) return false;
	decimalRegExp = /^-?(0|[1-9]{1}\d{0,})(\.(\d{1}\d{0,}))?$/
	return decimalRegExp.test(number);
}

function isEMailAddr(string){
	emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/
	return emailRegExp.test(string);
}

function isDecimal(number){// positive or negative decimal
	if(!number) return false;
	decimalRegExp = /^-?(0|[1-9]{1}\d{0,})(\.(\d{1}\d{0,}))?$/
	return decimalRegExp.test(number);
}

function existePlaca(placa){
		ajax=objetoAjax();
		ajax.open("GET",  "existeplaca.php?avi_patente="+placa);
			ajax.onreadystatechange=function() {				
				if (ajax.readyState==4) {
					if(ajax.responseText=="1"){
						document.unobtrusive.filename[1].disabled=true;
						alert('La patente ingresada registra un aviso publicado');					
					}else{
						document.unobtrusive.filename[1].disabled=false;
					}					
				}
			}
			ajax.send(null)
	}	


function callAjaxSendMsj(emailto, avi_codigo){		
		pageTracker._trackPageview("/sendmail" );
		ajax=objetoAjax();
		i = 0;
		email_from		= document.msj.email_from.value; //document.getElementById("email_from").value;
		telefono_from	= document.getElementById("telefono_from").value;
		mensaje_from	= document.getElementById("mensaje_from").value;
		
		if(!isEMailAddr(email_from)){
			alert("Ingrese un email válido");		
			i = 1;	
		}else if(telefono_from.length<6){
			alert("Ingrese un telefono de contacto");
			i = 1;
		}else if (mensaje_from.length<6){
			alert("Ingrese un mensaje, no olvide incluir su nombre");
			i = 1;
		}
		if(i==0){		
			params = "?avi_codigo="+avi_codigo+"&emailto="+emailto+"&email_from="+email_from+"&telefono_from="+telefono_from+"&mensaje_from="+mensaje_from;
			
			ajax.open("GET",  "sendemailto.php"+params);
			ajax.onreadystatechange=function() {
				if(ajax.readyState!=4){
					document.getElementById('emailok').innerHTML = waitDetails();
				}
				if (ajax.readyState==4) {
					document.getElementById('emailok').innerHTML = ajax.responseText
				}
			}
			ajax.send(null)
		}
}

function nuevaClave(){
	pageTracker._trackPageview("/nuevaclave" );
	rut		= document.getElementById("usu_codigo").value;
	if(rut==""){
		alert('Ingrese su RUT para solicitar una nueva contraseña.');
	}else{
		ajax=objetoAjax();
		ajax.open("GET",  "nuevapass.php?usu_codigo="+rut);
			ajax.onreadystatechange=function() {
				
				if (ajax.readyState==4) {
					document.getElementById('nuevapass').innerHTML = ajax.responseText
				}
			}
			ajax.send(null)
	}
}


function getMyRegisteredUser(){
	rut		= document.getElementById("usu_codigo").value;
	pass		= document.getElementById("usu_password").value;
	if(rut=="" || pass==""){
		alert('Ingrese RUT y contraseña');
	}else{
		ajax=objetoAjax();
		ajax.open("GET",  "alreadyexists.php?usu_codigo="+rut+"&usu_password="+pass);
			ajax.onreadystatechange=function() {
				
				if (ajax.readyState==4) {
					document.getElementById('registrado').innerHTML = ajax.responseText
				}
			}
			ajax.send(null)
	}
}


function yaestoy(){
	
	htmlcode = '<table width="100%" border="0" cellpadding="0" cellspacing="0"  height="108">';
	htmlcode = htmlcode +'<tr>';		
	htmlcode = htmlcode +'	<td class="filtro" align="left">&nbsp;&nbsp;Rut <font color="red">(<b>*</b>)</font></td>';
	htmlcode = htmlcode +'	<td class="filtro" align="left"><input type="text" name="usu_codigo" id="usu_codigo" class="inputTextShort"  maxlength="9"> &nbsp;Sin puntos ni guion.</td>';
	htmlcode = htmlcode +'</tr>';
	htmlcode = htmlcode +'<tr>	';	
	htmlcode = htmlcode +'	<td class="filtro" align="left" height="15">&nbsp;&nbsp;Contrase&ntilde;a <font color="red">(<b>*</b>)</font></td>';
	htmlcode = htmlcode +'	<td class="filtro" align="left"><input type="password" name="usu_password" id="usu_password" class="inputTextShort"  maxlength="9"> &nbsp;</td>';
	htmlcode = htmlcode +'</tr>';
	htmlcode = htmlcode +'<tr>	';	
	htmlcode = htmlcode +'	<td class="filtro" align="left">&nbsp</td>';
	htmlcode = htmlcode +'	<td class="filtro" align="left"><input type="button" value="Validar" class="botonShort" onclick="javascript:getMyRegisteredUser()"> </td>';
	htmlcode = htmlcode +'</tr>';
	htmlcode = htmlcode +'<tr>	';	
	htmlcode = htmlcode +'	<td class="filtro" align="left">&nbsp</td>';
	htmlcode = htmlcode +'	<td class="filtro" align="left">&nbsp;<a href="javascript:noValidarUser()"><i>No estoy registrado</i></a></td>';
	htmlcode = htmlcode +'</tr>';
	htmlcode = htmlcode +'<tr>	';	
	htmlcode = htmlcode +'	<td class="filtro" align="left">&nbsp</td>';
	htmlcode = htmlcode +'	<td class="filtro" align="left">&nbsp;</td>';
	htmlcode = htmlcode +'</tr>';
	htmlcode = htmlcode +'</table>';
	document.getElementById('registrado').innerHTML = htmlcode;
	
}

function noValidarUser(){
	htmlcode = '<table width="100%" border="0" cellpadding="0" cellspacing="0" height="108">';
	htmlcode = htmlcode +'<tr>';
	htmlcode = htmlcode +'  <td class="filtro">&nbsp;&nbsp;Nombre <font color="red">(<b>*</b>)</font></td>';
	htmlcode = htmlcode +'  <td class="filtro"><input type="text" name="new_nombre" id="new_nombre" class="inputText" maxlength="40" /></td>';
	htmlcode = htmlcode +'</tr>';
	htmlcode = htmlcode +'<tr>';
	htmlcode = htmlcode +'  <td class="filtro">&nbsp;&nbsp;Apellidos <font color="red">(<b>*</b>)</font></td>';
	htmlcode = htmlcode +'  <td class="filtro"><input type="text" name="new_apellidos" id="new_apellidos" class="inputText"  maxlength="44" />';
	htmlcode = htmlcode +'&nbsp;&nbsp;</td>';
	htmlcode = htmlcode +'</tr>';
	htmlcode = htmlcode +'<tr>';
	htmlcode = htmlcode +'  <td class="filtro">&nbsp;&nbsp;Rut <font color="red">(<b>*</b>)</font></td>';
	htmlcode = htmlcode +'  <td class="filtro"><input type="text" name="new_rut" id="new_rut" class="inputTextShort"  maxlength="9" />';
	htmlcode = htmlcode +'  Sin puntos ni guion.</td>';
	htmlcode = htmlcode +'</tr>';
	htmlcode = htmlcode +'<tr>';
	htmlcode = htmlcode +'  <td class="filtro">&nbsp;&nbsp;E-mail <font color="red">(<b>*</b>)</font></td>';
	htmlcode = htmlcode +'  <td class="filtro"><input type="text" name="new_email" id="new_email" class="inputText" maxlength="44" /></td>';
	htmlcode = htmlcode +'</tr>';
	htmlcode = htmlcode +'<tr>';
	htmlcode = htmlcode +'  <td class="filtro">&nbsp;&nbsp;Telefonos <font color="red">(<b>*</b>)</font></td>';
	htmlcode = htmlcode +'  <td class="filtro"><input type="text" name="new_telefonos" id="new_telefonos" class="inputText" maxlength="44" /></td>';
	htmlcode = htmlcode +'</tr>';
	htmlcode = htmlcode +'</table>';
	document.getElementById('registrado').innerHTML = htmlcode;
	
}


function multas(){
	genericAjaxCaller("multasindex");	
}


function getAjaxMultas(){
	pageTracker._trackPageview("/multas" );
	ajax=objetoAjax();	
	pat	= document.getElementById("multa_patente").value;

	params = "?patente=" + pat;
	ajax.open("GET",  "getmultas.php"+params);
	ajax.onreadystatechange=function() {
		if(ajax.readyState!=4){
			document.getElementById('multa_layer').innerHTML = waitDetails();
		}
		if (ajax.readyState==4) {
			document.getElementById('multa_layer').innerHTML = ajax.responseText;
		}
	}
    ajax.send(null);
}