function getHTTPObject() {
  var xmlhttp;
 
  if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (!xmlhttp){
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
}
  return xmlhttp;
}

function esMail(mail)
{
	var Template = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; //Formato de direccion de correo electronico
	
		if (!Template.test(mail)) 
		{	
			return true;
		}		else
		{return false;}
	
}

function menu_desplegable_mostrar(name_id){
	if(name_id!="")
	{
		document.getElementById(name_id).style.display="block";
	}
}

function menu_desplegable_ocultar(name_id){
	if(name_id!="")
	{
		document.getElementById(name_id).style.display="none";
	}
}

function trim(cadena)
{
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}
var httpContacto = getHTTPObject(); 
function enviaContacto()
{
	
	if(trim(document.form_.nom.value)=="") { alert("Please, fill in your name"); return; }
	if(trim(document.form_.ape.value)=="") { alert("Please, fill in your last name"); return; }
	if(trim(document.form_.emp.value)=="") { alert("Please, fill in your Empresa"); return; }
	if(trim(document.form_.cargo.value)=="") { alert("Please, fill in your cargo"); return; }
	if(trim(document.form_.fono.value)=="") { alert("Please, fill in your telephone"); return; }
	if(esMail(document.form_.email.value)) { alert("Introduzca email correct"); return; }
	if(trim(document.form_.consulta.value)=="") { alert("Please, fill in your consulta"); return; }
	if(document.form_.chk_condiciones.checked!="1") { alert("Debe de aceptar las condiciones y terminos"); return; }
	
	var selec_act = document.form_.selec_act.value;
	//var selec_sector = document.form_.selec_sector.value;

	var nom = document.form_.nom.value;
	var ape = document.form_.ape.value;
	var emp = document.form_.emp.value;
	var cargo = document.form_.cargo.value;
	var direc = document.form_.direc.value;
	var cp = document.form_.cp.value;
	var local = document.form_.local.value;
	var pais = document.form_.pais.value;
	var fono = document.form_.fono.value;
	var fax = document.form_.fax.value;
	var email = document.form_.email.value;
	var consulta = document.form_.consulta.value;
 
 	var myrand=parseInt(Math.random()*99999999);
	var modurl= "send_contacto.php?e=1&myrand="+myrand+"&selec_act="+selec_act+"&nom="+nom+"&ape="+ape+"&emp="+emp+"&cargo="+cargo+"&direc="+direc+"&cp="+cp+"&local="+local+"&pais="+pais+"&fono="+fono+"&fax="+fax+"&email="+email+"&consulta="+consulta;
	var modur2= GetInfoStringCheck_01("chk_1[]");
	var modur3= GetInfoStringCheck_02("chk_2[]");
 	var urlok = modurl+modur2+modur3;
	//alert(urlok);
 	httpContacto.open("GET", urlok, true);
	httpContacto.onreadystatechange = rsp_enviaContacto;
	httpContacto.send(null);
}

function rsp_enviaContacto() 
{
	if (httpContacto.readyState == 4) 
	{
		if(httpContacto.status == 200) 
		{
			estado = httpContacto.responseText;
			alert(estado);  document.form_.reset();document.getElementById('ID_MSGE_PRESU').innerHTML=""; 
// 			if(estado=="1")
//			{ alert("Su mensaje ha sido enviado."); document.form_.reset();document.getElementById('ID_MSGE_PRESU').innerHTML=""; }
//			else
//			{ alert("Ha ocurrido un error al enviar."); document.getElementById('ID_MSGE_PRESU').innerHTML="";}
		}	
	} 
	else
	{
		document.getElementById('ID_MSGE_PRESU').innerHTML="sending...";
	}
}

function GetInfoStringCheck_01(chkinfo){
	var iCount =0;
	var sData="";
	
	iCount=form_.elements.namedItem(chkinfo,"").length;
	if(iCount>0)
	{
		for(i=0;i<iCount;i++)
		{    
			if (form_.elements.namedItem(chkinfo,"")(i).checked)
			{
				sData += "&chk_1"+i+"=" +  form_.elements.namedItem(chkinfo,"")(i).value;
			}
		}
	}         
	return sData;
}
function GetInfoStringCheck_02(chkinfo){
	var iCount =0;
	var sData="";
	
	iCount=form_.elements.namedItem(chkinfo,"").length;
	if(iCount>0)
	{
		for(i=0;i<iCount;i++)
		{    
			if (form_.elements.namedItem(chkinfo,"")(i).checked)
			{
				sData += "&chk_2"+i+"=" +  form_.elements.namedItem(chkinfo,"")(i).value;
			}
		}
	}         
	return sData;
}
var httpDistribuidor = getHTTPObject(); 
function enviaDistribuidor()
{
	
	if(trim(document.form_.ape.value)=="") { alert("Please, fill in your last name"); return; }
	if(trim(document.form_.nom.value)=="") { alert("Please, fill in your name"); return; }
	if(trim(document.form_.emp.value)=="") { alert("Please, fill in your Empresa"); return; }
	if(trim(document.form_.marca.value)=="") { alert("Please, fill in your marca"); return; }
	if(trim(document.form_.local.value)=="") { alert("Please, fill in your localidad"); return; }
	if(trim(document.form_.pais.value)=="") { alert("Please, fill in your pais"); return; }
	if(trim(document.form_.fono.value)=="") { alert("Please, fill in your telephone"); return; }
	if(esMail(document.form_.email.value)) { alert("Introduzca email correct"); return; }
	if(document.form_.chk_condiciones.checked!="1") { alert("Debe de aceptar las condiciones y terminos"); return; }

	var selec_act = document.form_.selec_act.value;
 
	var nom = document.form_.nom.value;
	var ape = document.form_.ape.value;
	var emp = document.form_.emp.value;
	var marca = document.form_.marca.value;
  	var local = document.form_.local.value;
	var pais = document.form_.pais.value;
	var fono = document.form_.fono.value;
 	var email = document.form_.email.value;
	var consulta = document.form_.consulta.value;
 
 	var myrand=parseInt(Math.random()*99999999);
	var modurl= "send_contactoDistribuidor.php?e=1&myrand="+myrand+"&selec_act="+selec_act+"&nom="+nom+"&ape="+ape+"&emp="+emp+"&marca="+marca+"&local="+local+"&pais="+pais+"&fono="+fono+"&email="+email+"&consulta="+consulta;
  	httpDistribuidor.open("GET", modurl, true);
	httpDistribuidor.onreadystatechange = rsp_enviaDistribuidor;
	httpDistribuidor.send(null);
}

 function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=400,height=250,left = 400,top = 307.5');");
 }

function rsp_enviaDistribuidor() 
{
	if (httpDistribuidor.readyState == 4) 
	{
		if(httpDistribuidor.status == 200) 
		{
			estado = httpDistribuidor.responseText;
			//alert(estado);
			alert(estado);  document.form_.reset();document.getElementById('ID_MSGE_PRESU').innerHTML=""; 
// 			if(estado=="1")
//			{ alert("Su mensaje ha sido enviado."); document.form_.reset();document.getElementById('ID_MSGE_PRESU').innerHTML=""; }
//			else
//			{ alert("Ha ocurrido un error al enviar."); document.getElementById('ID_MSGE_PRESU').innerHTML="";}
		}	
	} 
	else
	{
		document.getElementById('ID_MSGE_PRESU').innerHTML="sending...";
	}
}

function enviaTrabajos()
{
	if(trim(document.form_.nom.value)=="") { alert("Please, fill in your name"); return; }
	if(trim(document.form_.ape.value)=="") { alert("Please, fill in your last name"); return; }
	if(trim(document.form_.fono.value)=="") { alert("Please, fill in your telephone"); return; }
	if(trim(document.form_.city.value)=="") { alert("Please, fill in your city"); return; }
	if(trim(document.form_.cp.value)=="") { alert("Please, fill in your post code"); return; }
	if(esMail(document.form_.email.value)) { alert("Please email correct"); return; }
	if(document.form_.chk_condiciones.checked!="1") { alert("Debe de aceptar las condiciones y terminos"); return; }

	document.form_.method="post";
	document.form_.action="trabajos.php?i=1";
	document.form_.submit();
}
