//----------------------------------------------------------------------------------
//---VALIDACION DE CAMPOS
//----------------------------------------------------------------------------------
	var nav4 = window.Event ? true : false;
	
	var tablaMes=new Array('01','02','03','04','05','06','07','08','09','10','11','12');
	var tablaCiudad=new Array('Medellín','etc');
	var tablaPais=new Array('Estados Unidos','Colombia');
	var tablaTipoDocumento=new Array('CC','CE','TI');


	function acceptNum(evt){
			// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
			var key = nav4 ? evt.which : evt.keyCode;
			return (key <= 13 || (key >= 48 && key <= 57));
	}
	function acceptDecimales(evt){
		var key = nav4 ? evt.which : evt.keyCode;
		return (key <= 13 || (key >= 48 && key <= 57) || key == 46 );
	}
	function acceptLetras(evt){
		var key = nav4 ? evt.which : evt.keyCode;
		return (key <= 13 || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key == 32) ||
				(key == 225) || (key == 193) || (key == 211) || (key == 243) || (key == 205) ||
				(key == 237) || (key == 233) || (key == 201) || (key == 250) || (key == 218)  );
	}
	function acceptAlfaNumerico(evt){
		var key = nav4 ? evt.which : evt.keyCode;
		return (key <= 13 || (key >= 48 && key <= 57) || key == 45 || key == 95 ||
                        (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key == 32) ||
			(key == 225) || (key == 193) || (key == 211) || (key == 243) || (key == 205) ||
			(key == 237) || (key == 233) || (key == 201) || (key == 250) || (key == 218)  );
	}
	function aceptaFecha(evt){
		var key = nav4 ? evt.which : evt.keyCode;
		return (key <= 13 || (key >= 48 && key <= 57) || key == 45 );
	}
	function  validateNumeric( strValue )
	{
		var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
                //check for numeric characters
		return objRegExp.test(strValue);
	}
	function refuseReturn(evt){
		// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
		var key = nav4 ? evt.which : evt.keyCode;
		return (key != 13);
	}
/* ---------------------------------------------------------------------*/
/* --------------PARA LA VALIDACION DE UN CONTROL-----------------------*/
/* ---------------------------------------------------------------------*/
	//--funcion para validar si un String es alfaNumerico
	function validarAlfaNumerico(str,campo)
	{
		if (str!="") {
			if(validateAlfaNumerico(str)){
				alert("El texto del campo "+ campo +" solo permite letras y Números.");
				return false;
			}
		}
		return true;
	}
	//--funcion para validar si un String es de letras del alfabeto
	function validarLetras(str,campo)
	{
		if (str!="") {
			if(validateAlfabetico(str)){
				alert("El texto del campo "+ campo +" solo permite letras.");
				return false;
			}
		}
		return true;
	}
	function validarDecimal(control){
			var cifraDecimal = new Number(2);
			var valor = new String(control.value);
			partes=valor.split('.');
			if (valor!="") {

                                      // alert( "valor:" +c);
                                       //alert( "tamaño:" + partes.length);
				if(partes.length>2 || !validateNumeric(valor)){
								alert("Número decimal inválido. Solo se aceptan dos cifras decimales y como separador el punto '.'");
					control.focus();
	                                return false;
							}
				else if(partes.length==2){
					var dec= new String(partes[1]) ;
					if(dec.length>cifraDecimal){
						alert("Solo se aceptan dos cifras decimales");
						control.focus();
	                                        return false;
					}
					//convierte a  un numero decimal valido (ej: .5 a 0.5)
					var c = new  Number(valor);
     			                control.value=c;
				}
			}
			return true;
	}
	//--funcion para validar una direccion
	function validarDireccion(objControl,campo)
	{
		str = objControl.value;
		if (str!="") {
			if(validateDireccion(str)){
				alert("El texto del campo "+ campo +" solo permite letras, números y los simbolos #-. ");
				objControl.focus();
				return false;
			}
		}
		return true;
	}
	function validarCorreo(objControl,campo)
	{
		str = objControl.value;
		if (str!="") {
			if(!validateEmail(str)){
				alert("El texto del campo "+ campo +" no es valido");
				objControl.focus();
	                        return false;
			}
		}
		return true;
	}
	function validarFecha(objControl,campo)
	{
                str = objControl.value;
                if (str!="") {
                    mensaje = "La "+ campo +" no es valida. Debe ser año-mes-dia(ejemplo:2005-01-20)";
                    partes = str.split('-');
                    if (partes.length==3) {
                         if(isFecha(partes[0],partes[1],partes[2])){
      			        return true;
      		        }
                    }
    		    alert(mensaje);
                    objControl.focus();
                    return false;
                }

	}
	/* -----------------------------------------------------------------------------------*/
	//--valida si el string es alfabetico
	function validateAlfabetico( strValue ) {
		var objRegExp = new RegExp('[^A-Za-z_.ñÑáÁéÉíÍóÓúÚüÜ ]');//sólo estos caracteres
		return objRegExp.test(strValue);
	}
	//--valida si el string es alfaNumerico
	function validateAlfaNumerico( strValue ) {
		var objRegExp = new RegExp('[^A-Za-z0-9_.ñÑáÁéÉíÍóÓúÚüÜ ]');//sólo estos caracteres
		return objRegExp.test(strValue);
	}

	//--valida si el string es una direccion
	function validateDireccion( strValue ) {
		var objRegExp = new RegExp('[^A-Za-z0-9_.#\\-ñÑáÁéÉíÍóÓúÚüÜ ]');//sólo estos caracteres
		return objRegExp.test(strValue);
	}
	//--valida si el string es alfanumerico
	function esAlfaNumerico(valor) {
		//[A-Za-z0-9_]<=>/\w/
		//var objRegExp = new RegExp('/\W/');//busca caracteres no válidos
		var objRegExp = /\W/; //busca caracteres no válidos
		return !objRegExp.test(valor);
	}

	function validateEmail(strValue) {
		var emailFilter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		return emailFilter.test(strValue)
	}
	//---------------------------------------------------------------------------
	//----validar fecha----------------------------------------------------
		var a, mes, dia, anyo, febrero;
		function anyoBisiesto(anyo)
		{
			if (anyo < 100)
				var fin = anyo + 1900;
			else
				var fin = anyo ;

			if (fin % 4 != 0)
				return false;
			else
			{
				if (fin % 100 == 0)
				{
					if (fin % 400 == 0)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
				else
				{
					return true;
				}
			}
		}

		/**
		* funcion principal de validacion de la fecha
		* argumento fecha > cadena de texto de la fecha introducida por el usuario
		*/
		function isFecha(anyo,mes,dia)
		{

		if( (isNaN(dia)==true) || (isNaN(mes)==true) || (isNaN(anyo)==true) )
		{
			alert("LA fecha introducida debe estar formada sólo por números");
			return false;
			}
			if(anyoBisiesto(anyo))
				febrero=29;
			else
				febrero=28;

			if ((mes<1) || (mes>12))
			{
				//alert("El mes introducido no es valido");
				return false;
			}

			if ((mes==2) && ((dia<1) || (dia>febrero)))
			{
				//alert("El dia introducido para la fecha de nacimiento no es valido");
				return false;
			}

			if (((mes==1) || (mes==3) || (mes==5) || (mes==7) || (mes==8) || (mes==10) || (mes==12)) && ((dia<1) || (dia>31)))
			{
				//alert("El dia introducido para la Fecha de Nacimiento no es valido");
				return false;
			}

			if (((mes==4) || (mes==6) || (mes==9) || (mes==11)) && ((dia<1) || (dia>30)))
			{
				//alert("El dia introducido para la Fecha de Nacimiento no es valido");
				return false;
			}

			if ((anyo<1900) || (anyo>2010))
			{
				//alert("El año introducido no es valido. Por favor, introduzca un año entre 1900 y 2010");
				return false;
			}
			else{
				return true;
			}
		}
    //---------------------------------------------------------------------------------------------
    function cotizar(form) {
           if(validarDecimal(form.libras)==false){
                return ;
           }else if(validarDecimal(form.Ancho)==false){
                return ;
           }
           /*if(validarDecimal(form.Largo)==false){
                return ;
           }if(validarDecimal(form.Alto)==false){
                return ;
           }*/
           else if(validarDecimal(form.valormercancia)==false){
                return ;
           }else {
                   form.submit();
           }
    }

/*---------------------------------
- Validaciones Cotización On Line
---------------------------------*/

var msg = "";

function addMsg(mensaje){
	msg = msg + mensaje +"\n";
}

function showMsg(){
	if(msg != ""){
		alert(msg);
		msg="";
		return false;
	}
	return true;	
}

function checkNum(pcampo, min,mensaje) {
      if (isNaN(pcampo.value)) {
         addMsg(mensaje);
         return false
         }
      var num = 0 + pcampo.value;
      if (num <= min ) {
         addMsg(mensaje);
         return false;
         }
      return true;
    }


function checkNumOpt(pcampo, min,mensaje){
	if(pcampo.value != ""){
	   return checkNum(pcampo, min,mensaje);
	}
	return true;
}

function CheckForm() {		

	if (document.frmLiqFletes.cboDe.selectedIndex== 0) {
	      addMsg("Origen:                   		Debe seleccionar una ciudad");
	}		   

	if (document.frmLiqFletes.cboPara.selectedIndex== 0 ) {
	      addMsg("Destino:                  		Debe seleccionar una ciudad");
	}		   

	checkNum(document.frmLiqFletes.txtUnidades1,0,"Número de Unidades Item 1:	Debe ingresar un número entero")
	checkNum(document.frmLiqFletes.txtPeso1,0,"Peso Real (kg) Item 1:    	Debe ingresar valor numérico")
	checkNumOpt(document.frmLiqFletes.txtAncho1,0,"Ancho (cm) Item 1:        	Debe ingresar valor numérico")
	checkNumOpt(document.frmLiqFletes.txtLargo1,0,"Largo (cm) Item 1:        	Debe ingresar valor numérico")
	checkNumOpt(document.frmLiqFletes.txtAlto1,0,"Alto (cm) Item 1:         	Debe ingresar valor numérico")


	if ((document.frmLiqFletes.txtPeso2.value != "") || (document.frmLiqFletes.txtUnidades2.value != "")) {
		checkNum(document.frmLiqFletes.txtUnidades2,0,"Número de Unidades Item 2:	Debe ingresar un número entero")
		checkNum(document.frmLiqFletes.txtPeso2,0,"Peso Real (kg) Item 2:    	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtAncho2,0,"Ancho (cm) Item 2:        	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtLargo2,0,"Largo (cm) Item 2:        	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtAlto2,0,"Alto (cm) Item 2:         	Debe ingresar valor numérico")
	}

	if ((document.frmLiqFletes.txtPeso3.value != "") || (document.frmLiqFletes.txtUnidades3.value != "")) {
		checkNum(document.frmLiqFletes.txtUnidades3,0,"Número de Unidades Item 3:	Debe ingresar un número entero")
		checkNum(document.frmLiqFletes.txtPeso3,0,"Peso Real (kg) Item 3:    	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtAncho3,0,"Ancho (cm) Item 3:        	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtLargo3,0,"Largo (cm) Item 3:        	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtAlto3,0,"Alto (cm) Item 3:         	Debe ingresar valor numérico")
	}


	if ((document.frmLiqFletes.txtPeso4.value != "") || (document.frmLiqFletes.txtUnidades4.value != "")) {
		checkNum(document.frmLiqFletes.txtUnidades4,0,"Número de Unidades Item 4:	Debe ingresar un número entero")
		checkNum(document.frmLiqFletes.txtPeso4,0,"Peso Real (kg) Item 4:    	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtAncho4,0,"Ancho (cm) Item 4:        	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtLargo4,0,"Largo (cm) Item 4:        	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtAlto4,0,"Alto (cm) Item 4:         	Debe ingresar valor numérico")
	} 

	if ((document.frmLiqFletes.txtPeso5.value != "") || (document.frmLiqFletes.txtUnidades5.value != "")) {
		checkNum(document.frmLiqFletes.txtUnidades5,0,"Número de Unidades Item 5:	Debe ingresar un número entero")
		checkNum(document.frmLiqFletes.txtPeso5,0,"Peso Real (kg) Item 5:    	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtAncho5,0,"Ancho (cm) Item 5:        	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtLargo5,0,"Largo (cm) Item 5:        	Debe ingresar valor numérico")
		checkNumOpt(document.frmLiqFletes.txtAlto5,0,"Alto (cm) Item 5:         	Debe ingresar valor numérico")
	}
	checkNum(document.frmLiqFletes.txtValorMercancia,0,"Valor Total de la Mercancía:	Debe ingresar valor numérico. No transportamos mercancía que no esté declarada (Art. 1010 del Código del Comercio) ")
	if(showMsg()){
	   	return true;
	}
	return false;
}
