	// validator.js
	
	// GLOBALES	
	var erroresValidacion = [];
	
	// valida el formato del email	
	function checkEmail (ctrl) {
		var error="";
		if (ctrl.value != "") {
		    var emailFilter = /^.+@.+\..{2,3}$/;
		    if (!(emailFilter.test(ctrl.value))) { 
		       error = "Please enter a valid e-mail address.";
		       setErrorMessage(ctrl, error);
		    }
		    else {
			//test email for illegal characters
			
		       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
		         if (ctrl.value.match(illegalChars)) {
		          error = "Please enter a valid e-mail address.";
		          setErrorMessage(ctrl, error);
		       }
		    }		    
		}
		return error;    
	}

//VERIFICA QUE LA 1ERA FECHA SEA MENOR A LA 2DA
function checkDate() {
	var error="";
	  var f1=document.forms[0].fechain.value.split("/");
	  var f2=document.forms[0].fechaout.value.split("/");	  	  
	 
	  if(f2[2]<f1[2]){
  		var malo=1;
		}else{
		
		if(f2[1]<f1[1]){
  		var malo=1;	
			}else{
		
	  	    if((f2[0]<=f1[0] & f2[1]==f1[1])){
	  		var malo=1;	        	
			}else{
			var malo=0;
				}
			}
		}
	  
	  if(malo=="1"){
		error="1";
      }
    return error;
}	
//FIN VERIFICA QUE LA 1ERA FECHA SEA MENOR A LA 2DA	
	
	
	// phone number - strip out delimiters and check for 12 digits
	function checkPhone (ctrl) {
		var error = "";
	
		if(ctrl.value != "") {
			var strValidChars = "0123456789.-() ";
		    var strChar;
			for (i = 0; i < ctrl.value.length; i++) {
		      	strChar = ctrl.value.charAt(i);
		      	if (strValidChars.indexOf(strChar) == -1) {
					error = "El número telefónico contiene caracteres no válidos.\nEl formato válido permite solo números, puntos(\".\"), guiones (\"-\") y paréntisis (\"()\").";
					setErrorMessage(ctrl, error);
					break;
		      	}
		   	}	    
		}
		return error;
	}	
	
	// password - between x-y chars, uppercase, lowercase, and numeral
	function checkPassword (ctrl, initialValue, finalValue)
	{
		var error = "";
	    var illegalChars = "/[\W_]/"; // allow only letters and numbers
	    if(ctrl.value != "") {
		    if ((ctrl.value.length < initialValue) || (ctrl.value.length > finalValue)) {
		       error = "La clave no posee un tama&ntilde;o v&aacute;lido.";
		    } else if (illegalChars.test(ctrl.value)) {
		      error = "La clave no contiene caracteres validos.";
		    } else if (!((ctrl.value.search(/(a-z)+/)) && (ctrl.value.search(/(A-Z)+/)) && (ctrl.value.search(/(0-9)+/)))) {
		       error = "Debe ingresar valores mas consistentes para la clave.";
		    }
		}
	    setErrorMessage(ctrl, error);
		return error;    
	}    
	
	
	// username - x-y chars, uc, lc, and underscore only.
	function checkUsername (ctrl, initialValue, finalValue)
	{
		var error = "";
		if (ctrl.value == "") {
		   error = "You didn't enter a username.\n";
		}
	
	    var illegalChars = "/\W/"; // allow letters, numbers, and underscores
	    if ((ctrl.value.length < initialValue) || (ctrl.value.length > finalValue)) {
	       error = "The username is the wrong length.";
	    }
	    else if (illegalChars.test(ctrl.value)) {
	    error = "The username contains illegal characters.";
	    } 
	    setErrorMessage(ctrl, error);
		return error;
	}       
	
	// non-empty textbox (required filed)
	function checkRequired(ctrl) {
		var error = "";
	  	if (ctrl.value.length == 0) {
  			error = "This field is required.";
			setErrorMessage(ctrl, error);
		} else {
			var val = false;			
			for(var i=0;i<ctrl.value.length;i++) {
		      	if (ctrl.value.charAt(i) != " ") {
		      		val = true;
		      		break;	
		      	}
		    }
		    if(!val) {
	  			error = "This field is required.";
				setErrorMessage(ctrl, error);
		    }
		}
		return error;	 
	}
	
	//Cheque que un valor sea mayor a un numero	
	function checkCantidad(ctrl, numero) {
		var error = "";
	  	
	  	if(ctrl.value != "") {
		  	if (ctrl.value <= numero) {
				error = "Este campo debe ser mayor a "+ numero+".";
				setErrorMessage(ctrl, error);
			}
		}			
		return error;	 
	}
	
	
	// was textbox altered
	function isDifferent(ctrl)
	{
		var error = ""; 
		if (ctrl.value != "Can\'t touch this!") {
		   error = "You altered the inviolate text area.";
		   setErrorMessage(ctrl, error);
		}
		
		return error;
	}
	
	// exactly one radio button is chosen
	function checkRadio(checkvalue)
	{
		var error = "";
	    if (!(checkvalue)) {
	       error = "Debe marcar una opci&oacute;n.";
	    }
		return error;
	}
	
	// valid selector from dropdown list
	function checkDropdown(ctrl)
	{
		var error = "";
	    if (ctrl.selectedIndex == 0) {
	    	error = "Debe seleccionar un elemento.";
	    	setErrorMessage(ctrl, error); 
	    }  
	    
		return error;
	}

//FIN VALIDA QUE SE HAYA MARCADO AL MENOS UN CHECKBOX
function checkCheckbox(ctrl,cuantos,campo)
	{
		//alert(ctrl.name)
		var error = "";
		var temp = "";				
		var selecciono= "";//Bandera
	for(j=0;j<cuantos;j++){	
	temp=document.getElementById(campo+j).checked;
		//alert(temp)
	    if (temp==true) {
			selecciono="1";
	    }  
	}
		if(selecciono!="1"){
	    	error = "Debe seleccionar un elemento.";
	    	setErrorMessage(ctrl, error); 
		}
		//alert(selecciono);
		return error;
	}
//FIN VALIDA QUE SE HAYA MARCADO AL MENOS UN CHECKBOX
	
	// valid a numeric input
	function checkNumeric(ctrl, allowNegative, allowDecimals) {
		var error = "";   
		var i = 0;
	    if(ctrl.value != "") {
		    var strValidChars = "0123456789.";
		    var strChar;
		    		   
			if(allowNegative) {
				strValidChars += "-";
				if (ctrl.value.substring(1, ctrl.value.length).indexOf("-") != -1) {
					alert(ctrl.value.substring(1, ctrl.value.length));
					error = "Please enter numeric values only.";
					setErrorMessage(ctrl, error);
					return error;
				}
			}
			
			if(allowDecimals) {
				var cantDot = 0;
				if(ctrl.value.charAt(0) == ".") {
	 				error = "Please enter numeric values only.";
					setErrorMessage(ctrl, error);
					return error;
				}				
				for (i = 0; i < ctrl.value.length; i++) {
		    	  	if (ctrl.value.charAt(i) == ".") { cantDot++; }
				}
				
				if(cantDot > 1) {
	 				error = "Please enter numeric values only.";
					setErrorMessage(ctrl, error);
					return error;
				}
			}
	   
			for (i = 0; i < ctrl.value.length; i++) {
		      	strChar = ctrl.value.charAt(i);
		      	if (strValidChars.indexOf(strChar) == -1) {
							
		 			error = "Please enter numeric values only.";
					setErrorMessage(ctrl, error);
		 	     	return error;
		      	}
		   	}		   	
		}
	    return error;
	}
	
	function checkCurrency(ctrl, allowNegative) {
		var error = "";
		var i = 0;
	    if(ctrl.value != "") {
		    var strValidChars = "0123456789.,";
		    var strChar;
			
			if(allowNegative) {
				if (ctrl.value.substring(1, ctrl.value.length).indexOf("-") != -1) {
					alert(ctrl.value.substring(1, ctrl.value.length));
					error = "Debe ingresar solo valores num&eacute;ricos.";
					setErrorMessage(ctrl, error);
					return error;
				}
			}
			
			var cantDot = 0;
			if(ctrl.value.charAt(0) == "." || ctrl.value.charAt(0) == ",") {
				error = "Debe ingresar solo valores num&eacute;ricos.";
				setErrorMessage(ctrl, error);
				return error;
			}				
			for (i = 0; i < ctrl.value.length; i++) {
				if (ctrl.value.charAt(i) == ",") { cantDot++; }
			}
			
			if(cantDot > 1) {
				error = "Debe ingresar solo valores num&eacute;ricos.";
				setErrorMessage(ctrl, error);
				return error;
			}
			for (i = 0; i < ctrl.value.length; i++) {
		      	strChar = ctrl.value.charAt(i);
		      	if (strValidChars.indexOf(strChar) == -1) {
		 			error = "Este campo debe ser mayor a 0."
					setErrorMessage(ctrl, error);
		 	     	return error;
		      	}
		   	}		   	
		}
	    return error;
	}
	
	
	
	// valid an interval value	
	function checkInterval (ctrl, initialValue, finalValue) {
		var error = "";
	
		if (ctrl.value != "") {
		    if ((ctrl.value < initialValue) || (ctrl.value > finalValue)) {
		       error = "El valor esta fuera del intervalo [" + initialValue + " - " + finalValue + "].";
		       setErrorMessage(ctrl, error);
		    }		    
		}
		return error;
	}
	
	// valida el tamaño de un texto	
	function checkLength (ctrl, initSize, finalSize) {
		var error = "";
	
		if (ctrl.value != "") {
			if(initSize == 0 && (ctrl.value > finalSize)) {
		        error = "La cantidad de d&iacute;gitos debe ser menor a " + finalValue + ".";
		        setErrorMessage(ctrl, error);
		    } else if(initSize == finalSize && (ctrl.value.length != finalSize)) {
		    	error = "Debe tener una longitud de " + initSize + " d&iacute;gito(s).";
  		    	setErrorMessage(ctrl, error);		        
		    } else if ((ctrl.value < initSize) || (ctrl.value > finalSize) && initSize != finalSize) {
			    error = "La cantidad de d&iacute;gitos debe estar comprendida entre los valores [" + initSize + " - " + finalSize + "].";
		        setErrorMessage(ctrl, error);
		    } else if (finalSize == 0 && (ctrl.value < initSize)) {
		        error = "La cantidad de d&iacute;gitos debe ser mayor a " + initValue + ".";
		        setErrorMessage(ctrl, error);
		    }
		}
		return error;
	}
	
	// valida el formato del RIF
	function checkRIF(ctrl)	{	
		var str = ctrl.value;
		var strChar;
		var strValidChars = "0123456789";
		var esValido = false;
		var error = "";
		if(ctrl.value != "") {
			if(ctrl.value.length == 10) {
				if(strValidChars.indexOf(str.charAt(0)) == -1) {
					for (var i=1;i<str.length;i++) {				
						strChar = str.charAt(i);
						if (strValidChars.indexOf(strChar) == -1) {
							error = "Debe ingresar un formato de RIF v&aacute;lido ej: J123456789.";
							setErrorMessage(ctrl, error); 
							break;
						}
					}
				} else {
					error = "Debe ingresar un formato RIF v&aacute;lido ej: J123456789.";
					setErrorMessage(ctrl, error); 
				}
			} else {
					error = "El RIF debe contener 10 caracteres.";
					setErrorMessage(ctrl, error);
			}
		}
		return error;
	}

	// valida de una lista multi selección o no el marcado de un registro.
	function checkList(ctrl, hasSelect) {
		var error = "";
		if(hasSelect) {
			if (ctrl.selectedIndex == 0 || ctrl.selectedIndex == -1) {
				error = "Debe seleccionar un elemento.";
				setErrorMessage(ctrl, error); 
			} 
		} else {
			if (ctrl.selectedIndex == -1) {
				error = "Debe seleccionar al menos un elemento.";
				setErrorMessage(ctrl, error); 
			} 		
		}	    
		return error;
	}

	
	// folder name validation
	function checkFile(ctrl) {
		var error = "";
		
		if(ctrl.value != "") {
			error = checkSpace(ctrl);
			if(error.length == 0) {	
				var iChars = "\\/:|<>?*";				
				for(var i = 0; i < ctrl.value.length; i++) {
					if (iChars.indexOf(ctrl.value.charAt(i)) != -1) {
						error = "\\ / : | < > ? * caracteres no permitidos.";
						setErrorMessage(ctrl, error);
						break;
					}
				}
			}		    
		}
		return error;    
	}

//VALIDAR FORMATO DE ARCHIVO
extArray = new Array(".doc", ".pdf", ".txt");
function checkFileFormat(ctrl) {
var error = "";
var file = "";
var ext = "";
var file =ctrl.value;
//alert(file);
allowSubmit = false;
	while (file.indexOf("\\") != -1){
	file = file.slice(file.indexOf("\\") + 1);
	ext = file.slice(file.lastIndexOf(".")).toLowerCase();
		for (var i = 0; i < extArray.length; i++) {
			if (extArray[i] == ext) { allowSubmit = true; break; }
		}
		
			if (allowSubmit!=true){
			error="Please send your file in the following formats: "
			+ (extArray.join(" ")) + "";
			setErrorMessage(ctrl, error);
			break;
			}
	}
return error;
}

//FIN VALIDAR FORMATO DE ARCHIVO

	/*Validador de fecha*/

	function checkDateFormat(ctrl) {
		var error = "";
		if (ctrl.value!= "") {
			error=validateDate(ctrl.value);
			if (error!=true) {						
			   setErrorMessage(ctrl, error);		   
			}
		}			
   		return error;
	}
	

	// espacios en blanco	
	function checkSpace(ctrl) {	
		var error="";

	    if(ctrl.value != "") {	  
			if(ctrl.value.indexOf(" ") > -1) {
				error = "Espacios en blanco no permitidos.";
				setErrorMessage(ctrl, error);
			}
		}
		return error;    
	}
	
	// almacena los errores en un arreglo		
	function setErrorMessage(ctrl, strError) {
		try	{
			var o = {
						nombreControl: document.getElementById("e" + ctrl.id).innerHTML,
						error : strError
				    };	
			erroresValidacion.push(o);			
		} catch(e) {
			alert("Error al crear mensaje: " + strError + "\nNo se encontró el elemento: e" + ctrl.id + "\nTipo de error: " + e.message);
		}
	}
