
//Returns true when combo has a valid option. Returns false when combo is empty and displays a message.
function CheckCbo(campo, msg) {
    var Inx = campo.selectedIndex;
    if (Inx != -1) {
	   var sVal = campo.options[Inx].text;
	   if (sVal == "") {	
		   alert (msg);
		   return false;
	   }	
	   else
	     return true;
	 }
	 else
	 {
	    alert (msg);
	    return false;    	
	 }
}
function SetCboIndex (Ctrl, Inx) {
	for (var i = 0; i < Ctrl.length; i++)
	{
		if ( Ctrl.options [i].value == Inx )
		{
		return Ctrl.options [i].index;
		}
	}
	return 0;
}
function CheckRadios(campo, msg) {
	for(i=0;i<(campo.length);i++) {
		if(campo[i].checked==true) 
			return(false);
	}
	alert(msg);
	return(true);
}
function CheckNumber (campo, msg) {
    valor = parseInt(campo.value);
	if (isNaN(valor))
		campo.value = "";
	else   
		campo.value = valor;
		
	if (campo.value=="") {
		alert(msg);
		return(true);
	}	
	else
		return(false);	
}

//Returns false when textbox has not data, and true, when has data.
function CheckEmpty (campo, msg) {
    if (campo.value.length > 0)
    {
	   campo.value = trim (campo.value);
	   if (campo.value == "") {	
	     alert(msg);
		 return false;
	   }
	   else
		  return true
    }
    else {
       alert (msg);
       return false;
    }
}
/*****************************************************************************************/				
function trim(arg) {
	var trimvalue = "";
	var func = "both";
	var i;
	var arglen;
	var pos;
	var arg;
	var lastpos;

	arglen = arg.length;
	if (arglen < 1) return trimvalue;
	if (func == "left" || func== "both") {
		i = 0;
		pos = -1;
		while (i < arglen) {
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
				pos = i;
				break;
			}
			i++;
		}
	}
	if (func == "right" || func== "both") {
		var lastpos = -1;
		i = arglen;
		while (i >= 0) {
			if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
				lastpos = i;
				break;
			}
			i--;
		}
	}
	if (func == "left") {
		trimvalue = arg.substring(pos,arglen-1);
	}
	if (func == "right") {
		trimvalue = arg.substring(0,lastpos+1);
	}
	if (func == "both") {
		trimvalue = arg.substring(pos,lastpos + 1);
	}
	return trimvalue;
}

// Limpia el combo especificado por CtrlName

function ClearCombo (CtrlName) {
   var Ctrl = document.getElementById (CtrlName);
		      
   for (var i = Ctrl.options.length -1; i >= 0; i--) {
      Ctrl.options [i] = null;
   }
		      
   Ctrl.selectedIndex = -1;
}

// Llena el combo especificado por CtrlName. Y pasa un array para llenarlo.

function FillCombo (CtrlName, Arr) {
   ClearCombo (CtrlName);
		      
   var Ctrl = document.getElementById (CtrlName);
		      
   for (var i = 0; i < Arr.length; i += 2) {
      Ctrl.options [Ctrl.length] = new Option (Arr [i + 1], Arr [i]);
   }
}
//Sólo Número
function jsNumer_only()
	{
	var key=window.event.keyCode;
	if (key < 48 || key > 58)
	{
		window.event.keyCode = null;
	}
}

function Save()
{
	if(Validate()) {
		document.forms[0].btnDescargar.click();
	}
}

function Validate()
{
	var doc = document.forms[0];
	
	var divComp = document.getElementById("CompanyAddress");
	
	if(!CheckEmpty(doc.txtCorreoElectronico,"El correo electrónico es un dato requerido")) {
		doc.txtCorreoElectronico.focus();
		return(false);
	}	
	
	if(!CheckEmpty(doc.txtNombre,"El nombre es un dato requerido")) {
		doc.txtNombre.focus();
		return(false);
	}		
	
	if(!CheckEmpty(doc.txtAPaterno,"El apellido paterno es un dato requerido")) {
		doc.txtAPaterno.focus();
		return(false);
	}	
		
	if(!CheckEmpty(doc.txtEmpresa,"La empresa es un dato requerido")) {
		doc.txtEmpresa.focus();
		return(false);
	}	
		
	if(!CheckEmpty(doc.txtPuesto,"El puesto es un dato requerido")) {
		doc.txtPuesto.focus();
		return(false);
	}		
	
	if(!CheckEmpty(doc.txtTelefono,"El teléfono es un dato requerido")) {
		doc.txtTelefono.focus();
		return(false);
	}			
	
	if (divComp.style.display == '')
	{
		if(!CheckEmpty(doc.TxtCalle,"Introduzca la calle.")) {
			doc.TxtCalle.focus();
			return(false);
		}
		
		if(!CheckEmpty(doc.TxtNbr,"Introduzca el numero exterior de la empresa.")) {
			doc.TxtNbr.focus();
			return(false);
		}
		
		if(!CheckEmpty(doc.txtColonia,"Introduzca la colonia.")) {
			doc.txtColonia.focus();
			return(false);
		}


		if(!CheckEmpty(doc.TxtCP,"Introduzca el código postal.")) {
			doc.TxtCP.focus();
			return(false);
		}

		if(CheckNumber(doc.TxtCP,"Por favor introduzca solo números en código postal.")){
			doc.TxtCP.focus();
			return(false);	
		}
		
		if(!CheckEmpty(doc.TxtCiudad,"Introduzca la Ciudad.")) {
			doc.TxtCiudad.focus();
			return(false);
		}	

		if(!CheckEmpty(doc.TxtDeleg,"Introduzca la delegación.")) {
			doc.TxtDeleg.focus();
			return(false);
		}		
		
			
		if(!CheckCbo(doc.CmbEntidad,"Seleccione la entidad federativa.")) {
			doc.CmbEntidad.focus();
			return(false);
		}		
		
		if(!CheckCbo(doc.CmbPais,"Seleccione el pais.")) {
			doc.CmbPais.focus();
			return(false);
		}
	}
	
	return (true);
}

