function ajaxObject()
{ 
    /* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
    lo que se puede copiar tal como esta aqui */
    var xmlhttp=false;
    try
    {
        // Creacion del objeto AJAX para navegadores no IE
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            // Creacion del objet AJAX para IE
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
            if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
        }
    }
    return xmlhttp; 
}

function obtenerValorSelect(id_select){
	selectActual = document.getElementById(id_select);
	return selectActual.options[selectActual.selectedIndex].value;
}

function setearValorSelect(id_select,valor){
	selectActual = document.getElementById(id_select);
	selectActual.options[selectActual.selectedIndex].value = valor;
}


function cargarDatosCountry(parametros_get){
	//Aca guardamos el id seleccionado en el combo de pais
	id_pais = obtenerValorSelect("selectPais");
	
	if (id_pais != 0){
		orderBy = obtenerValorSelect("order");
		
		sel = document.getElementById("comboTiposProducto");
		sel.options[0].selected = true;	
		
		tiposProducto = 0;
		ciudad = 0;
	
		buscarCiudades(id_pais, parametros_get);
	
		buscarDistribuidores(id_pais, parametros_get, orderBy, ciudad, tiposProducto);
		document.getElementById("comboTiposProducto").disabled = false;
		document.getElementById("order").disabled = false;

	} else {
		document.getElementById("city").options[0].selected = true;
		document.getElementById("city").disabled = true;
		document.getElementById("comboTiposProducto").disabled = true;
		document.getElementById("comboTiposProducto").options[0].selected = true;
		document.getElementById("order").disabled = true;
		document.getElementById("order").options[0].selected = true;
		document.getElementById("listadoDistribuidores").innerHTML = '';
	}
}

function cargarDatos(parametros_get){
	//Aca guardamos el id seleccionado en el combo de pais
	id_pais = obtenerValorSelect("selectPais");
	orderBy = obtenerValorSelect("order");
	try {
		ciudad = obtenerValorSelect("city");
	} catch (e) {
		ciudad = 0;
	}
	tiposProducto = obtenerValorSelect("comboTiposProducto");
	
	buscarDistribuidores(id_pais, parametros_get, orderBy, ciudad, tiposProducto);
}

function buscarCiudades(id_pais, parametros_get){
	// donde se mostrara el resultado
	divResultado = document.getElementById('resultado');
	
	// Se crea el objeto Ajax
    ajax = ajaxObject();    
    ajax.open('GET', 'ajax.php?' + parametros_get + '&id_pais=' + id_pais + '&cargar_ciudades=1', true);
   
    ajax.onreadystatechange = function() 
        {
            if (ajax.readyState==4)
            {
                divResultado.innerHTML = ajax.responseText;
            }
        }

   ajax.send(null);
}


function validarCertificado(parametros_get){
	// donde se mostrara el resultado
	divListado = document.getElementById('listadoDistribuidores');
	certificate = document.getElementById('certificado_a_validar').value;
	
	//borro el contenido del input de numero de certificado
	document.getElementById('certificado_a_validar').value='';
	
	// Se crea el objeto Ajax
    ajaxDistribuidor = ajaxObject();    
    ajaxDistribuidor.open('GET', 'ajax.php?' + parametros_get + '&certificate=' + certificate , true);
    
    ajaxDistribuidor.onreadystatechange = function() 
        {
            if (ajaxDistribuidor.readyState==4)
            {
                divListado.innerHTML = ajaxDistribuidor.responseText;
            }
        }
   ajaxDistribuidor.send(null);
}

function buscarDistribuidores(id_pais, parametros_get, orderBy, ciudad, tiposProducto){
	// donde se mostrara el resultado
	divListado = document.getElementById('listadoDistribuidores');
	
	// Se crea el objeto Ajax
    ajax2 = ajaxObject();    
    ajax2.open('GET', 'ajax.php?' + parametros_get + '&id_pais=' + id_pais + '&id_distribuidor=1&order=' + orderBy + '&city=' + ciudad + '&tipoProducto=' + tiposProducto, true);
    
    ajax2.onreadystatechange = function() 
        {
            if (ajax2.readyState==4)
            {
                divListado.innerHTML = ajax2.responseText;
            }
        }

   ajax2.send(null);
}


function comparar(obj){
alert("Please, check some products to compare.");	
if ( !obj.id_producto[0].checked && 
     !obj.id_producto[1].checked &&
     !obj.id_producto[2].checked)
    {
	alert("Please, check some products to compare.");	
     return false;
    }
return true 



}


function contact(){
		document.formulario.submit();	
}

function user_r(str){
		flat = false;
		if(document.formulario.first_name.value == "")
			flat = "first_name";
		if(document.formulario.last_name.value == "") 
			flat = "last_name";
		if(document.formulario.name.value == "") 
			flat = "name";	
		if(document.formulario.pass.value == "") 
			flat = "pass";			
		if(document.formulario.nac_1.value == "0")
			flat = "nac_1";			
		if(document.formulario.nac_2.value == "0")						
			flat = "nac_2";			
		if(document.formulario.nac_3.value == "0")							
			flat = "nac_3";			
		if(document.formulario.email.value == "")
			flat = "email";			

 

		if(flat == false)
		{			
			document.formulario.submit();	
		}else{
			alert(str);
			eval("document.formulario."+flat+".focus()");
		}

}

function dist_r(){
		flat = false;
		if(document.formulario.first_name.value == "")
			flat = "first_name";
		if(document.formulario.last_name.value == "") 
			flat = "last_name";
		if(document.formulario.name.value == "") 
			flat = "name";	
		if(document.formulario.pass.value == "") 
			flat = "pass";			
		if(document.formulario.nac_1.value == "0")
			flat = "nac_1";			
		if(document.formulario.nac_2.value == "0")						
			flat = "nac_2";			
		if(document.formulario.nac_3.value == "0")							
			flat = "nac_3";			
		if(document.formulario.email.value == "")
			flat = "email";			

 

		if(flat == false)
		{			
			document.formulario.submit();	
		}else{
			alert("Please, complete all the information.");
			eval("document.formulario."+flat+".focus()");
		}


}

function product_r(){
		flat = false;
		if(document.formulario.first_name.value == "")
			flat = "first_name";
		if(document.formulario.last_name.value == "") 
			flat = "last_name";
		if(document.formulario.name.value == "") 
			flat = "name";	
		if(document.formulario.nac_1.value == "0")
			flat = "nac_1";			
		if(document.formulario.nac_2.value == "0")						
			flat = "nac_2";			
		if(document.formulario.nac_3.value == "0")							
			flat = "nac_3";			
		if(document.formulario.email.value == "")
			flat = "email";			

 

		if(flat == false)
		{			
			document.formulario.submit();	
		}else{
			alert("Please, complete all the information.");
			eval("document.formulario."+flat+".focus()");
		}
}

function searchadv(){
	if (document.getElementById('search_adv').exact.value!="" || document.getElementById('search_adv').atleast.value!="" || document.getElementById('search_adv').without.value!="")
		document.getElementById('search_adv').submit();	
	else
		alert("Please, complete all the information.")
}
function forgot(){
	if(document.form_forgot.mail.value!="")
	{
		var s = document.form_forgot.mail.value;
		var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		if (filter.test(s))
		{
			document.form_forgot.submit();	
		}else{
			alert("Please, complete the mail correctly");
			mail.focus();		
		}
		
	}else{
		alert("Please, complete all the information.");
	}
		



}

function searchadv_(){
	document.getElementById('search_adv_').submit();	
}

function login(){
	document.form_login.submit();	
}

function Subrayar(id){
	document.getElementById(id).style.textDecoration = "underline";
}

function NoSubrayar(id){
	document.getElementById(id).style.textDecoration = "none";
}


ultimo_abierto = 0;
function abrirProd(cual){
  	var elem;
    if(ultimo_abierto!=0) {
		elem = document.getElementById('cat_'+ultimo_abierto);
		elem.style.visibility = 'hidden';
		elem.style.position = 'absolute';
	}
	elem = document.getElementById('cat_'+cual);
	elem.style.visibility = 'visible';
	elem.style.position = 'relative';
	ultimo_abierto = cual;
}



function mmenu(){

    cual = "smenu";
	if (document.body.clientWidth >= 770){
    pi = ((document.body.clientWidth - 770)/ 2) + 180;
	}else{
		pi = 180;
	}
    ac1 = cual + ".style.left =" + pi;
    ac2 = cual + ".style.visibility='visible'";
    eval(ac1);
    eval(ac2);
}

function iniciar() {
	sitio = document.form.sitio.value;	
	items = document.form.items_loading.value;
	if (sitio != "no"){ window.location = "iniciar.php?sitio=" + sitio + "&items=" + items; }
}

function masInfoProducto(id_prod,id_cat,id_tipo) {
  window.open('line.php?id_producto='+id_prod+'&id_categoria='+id_cat+'&tipo='+id_tipo+'&p_estado=2','_top','');
}

function externalLinks() {
	if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
	   var anchor = anchors[i];
	   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
       anchor.target = "_blank";
	 }
}
