// JavaScript Document
var regiones=new Array();
popUpWin_si=0;
	function popUpWindow_si(URLStr, left, top, width, height)
		{
		 	var width="500", height="350";
		  	var left = (screen.width/2) - width/2;
		  	var top = (screen.height/2) - height/2;
			if(popUpWin_si)
				{
					if(!popUpWin_si.closed) popUpWin_si.close();
				}
		 	popUpWin_si = open(URLStr, 'Mensaje', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
		}
	function inserta_datos(accion)
		{
			var capa;
			if (accion=='manual')
				{
				capa=document.getElementById("forma_cliente");
				capa.style.visibility="visible";
				capa.style.display="block";
				capa=document.getElementById("logueo_solicitud");
				capa.style.visibility="hidden";
				capa.style.display="none";
				//document.getElementById("formulario_solicita").action="/principal/registrar_contacto.php";
				}
			if (accion=='registro')
				{
				capa=document.getElementById("logueo_solicitud");
				capa.style.visibility="visible";
				capa.style.display="block";
				capa=document.getElementById("forma_cliente");
				capa.style.visibility="hidden";
				capa.style.display="none";
				//document.getElementById("formulario_solicita").action="#formulario_solicita";
				}
		}

function selecciona_pais()
	{
	obj_r=document.getElementById("ti2");	
	obj_div_sp=document.getElementById("cont_region");	
	if (obj_div_sp && obj_r && obj_r.checked)
		{
		obj_div_sp.style.display='block';
		}
	obj_r=document.getElementById("ti1");	
	if (obj_div_sp && obj_r && obj_r.checked)
		{
		obj_div_sp.style.display='none';
		}
//	inicia_pp_c();
	}


var regiones=new Array();
function sel_region(region)
	{
	if (regiones[region.id])
		{
		for (id_pais in regiones[region.id])
			{
			obj_pais=document.getElementById('p_'+regiones[region.id][id_pais]);
			if (obj_pais)
				{
				obj_pais.checked=region.checked;
				}
			}
		}
	}
function rev_region(id_region)
	{
	var cont_sel=0;
	for (id_pais in regiones[id_region])
		{
		obj_pais=document.getElementById('p_'+regiones[id_region][id_pais]);
		if (obj_pais && obj_pais.checked)
			{
			cont_sel++;
			}
		}
	obj_region=document.getElementById(id_region);
	if (obj_region)
		{
		if (cont_sel>0)
			obj_region.checked=1;
		else
			obj_region.checked=0;
		}
	}
	
/*Rodrigo*/

function validaFormulario(forma)
{		
	var mensaje='';
	
	if (document.getElementById("logueo_solicitud").style.visibility=="visible")
		{
		if (forma.usuarioqn.value=="" || forma.passwordqn.value=="")
			{
			mensaje+='Debe ingresar su nombre de usuario y contraseña o ingresar sus datos de forma manual.\n';
			forma.usuarioqn.focus();
			}
		}	
	else
		{
		if(!validaEmails(forma.email.value)) {
			mensaje+='El email no es válido.\n';
			forma.email.select();
		}		
		if(!validaVacio(forma.apellidoP.value)) {
			mensaje+='Los apellidos son requeridos.\n';
			forma.apellidoP.select();
		}
		if(!validaVacio(forma.nombre.value)) {
			mensaje+='El nombre es requerido.\n';
			forma.nombre.select();
		}
		if(!validaVacio(forma.empresa.value)) {
			mensaje+='La empresa es requerida.\n';
			forma.empresa.select();
		}
		if(!validaVacio(forma.telefono.value)) {
			mensaje+='El teléfono es requerido.\n';
			forma.telefono.select();
			if(!validaNumero(forma.telefono.value)) {
				mensaje+='El teléfono debe ser un número.\n';
				forma.telefono.select();
			}	
		}
		if(!validaNumero(forma.clave.value)) {
			mensaje+='La clave debe ser un número.\n';
			forma.clave.select();
		}
		if(!validaVacio(forma.clave.value)) {
			mensaje+='La clave debe ser un número.\n';
			forma.clave.select();
		}
		if(!validaVacio(forma.puesto.value)) {
			mensaje+='El puesto es requerido.\n';
			forma.puesto.select();
		}
		if(!validaVacio(forma.ciudad.value)) {
			mensaje+='La Ciudad es requerida.\n';
			forma.ciudad.select();
		}
		if(!validaVacio(forma.estado.value)) {
			mensaje+='El Estado es requerido.\n';
			forma.estado.select();
		}
		if(!validaVacio(forma.pais.value)) {
			mensaje+='El País / Clave del país es requerido.\n';
			forma.pais.focus();
		}		
		}
			
		
			
		
		if(!validaVacio(forma.producto.value)) {
			mensaje+='El producto es requerido.\n';
			forma.producto.select();
		}
		
		if(!validaVacio(forma.consumo.value) || !validaNumero(forma.consumo.value)) {
				mensaje+='El consumo es requerido y debe ser un número.\n';
				forma.consumo.select();
		}
		if(!validaVacio(forma.tipoUnidad.value)) {
				mensaje+='La unidad de consumo es requerida.\n';
				forma.tipoUnidad.focus();
		}
		if(!validaVacio(forma.frecuencia.value)) {
				mensaje+='La frecuencia de consumo es requerida.\n';
				forma.frecuencia.focus();
		}		
		
		
		fecha1 = new Date (anio_php,mes_php,dia_php); 
		fecha2 = new Date (forma.anio.value,forma.mes.value-1,forma.dia.value); 
		
		if(fecha1 > fecha2 ) {
			mensaje+='La fecha debe ser igual o mayor al día de hoy.\n';
			forma.dia.focus();
		}
		
		var msj="¿Deseas que te ayudemos a llenar este formulario\nla próxima vez que lo necesites? \n (Elige 'Cancelar' si estas en una computadora pública)";
//		alert(forma.cookieForm.value+'+'+mensaje+'+');
		if(mensaje=='')
			{	
			if (forma.cookieForm)
				{
				if( forma.cookieForm.value=='on' )
					if( !confirm(msj) )
						forma.cookieForm.value = 'off';
				}
			forma.submi.disabled = true;
			/*pop_res=popupPP(forma.action,600,400,"Solicitud");
			alert(pop_res);
			forma.target=divMoverPO;
			return false;*/
			return true;
			}
		else {
			alert(mensaje);
			return false;
		}
}

/********************************************
* VALIDAR EMAIL		(inicio)
********************************************/
function validaEmails(emails){
salida=false;
var email_Arr=emails.split(", ");
//alert  (email_Arr.length);

for (correo in email_Arr)  
 {
 salida=validaEmail(email_Arr[correo])
 }
 return(salida);
}
function validaEmail(emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
		}
	}
	if (user.match(userPat)==null) {
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
			}
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return false;
	}
	if (len<2) {
		return false;
	}
		return true;
}
/********************************************
* VALIDAR EMAIL		(fin)
********************************************/

/********************************************
* VALIDA ESPACIOS EN BLANCO	(inicio)
********************************************/
function validaVacio(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { 
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") {
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) {  
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   }
   //return retValue; 
   if (retValue=="") {
   		return false;
   } else {
   		return true;
   }
} 
/********************************************
* VALIDA ESPACIOS EN BLANCO	(fin)
********************************************/

/********************************************
* VALIDAR NUMEROS	(inicio)
********************************************/
function validaNumero(field) {
	var valid = "0123456789";
	var ok = "yes";
	var temp;
	for (var i=0; i<field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		return false;
	} else {
		return true;
	}
}
/********************************************
* VALIDAR NUMEROS	(fin)
********************************************/