cuenta=0;
// FX es el formulario a validar
function validar(fx){
	email_str=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;

	for (i = 0; i < fx.elements.length; i++) {
		if ((fx.elements[i].type == "text" ||
				fx.elements[i].type == "password" ||
				fx.elements[i].type == "textarea") &&
			 (fx.elements[i].type != "hidden" && fx.elements[i].type != "file") &&
			(fx.elements[i].name!= 'login_m' && 
				fx.elements[i].name!= 'pass_m' && 
				fx.elements[i].name!= 'confirm_m' && 
				fx.elements[i].name!= 'email' && 
				fx.elements[i].name!= 'id_funcionario' && 
				fx.elements[i].name!= 'telefono' && 
				fx.elements[i].name!= 'fax' &&
				fx.elements[i].name!= 'observaciones' &&
				fx.elements[i].name!= 'SEGUNDO_NOMBRE' &&
				fx.elements[i].name!= 'SEGUNDO_APELLIDO' &&
				fx.elements[i].name!= 'PARROQUIA' &&
				fx.elements[i].name!= 'COD_TELF_HAB' &&
				fx.elements[i].name!= 'TELF_HAB' &&
				fx.elements[i].name!= 'COD_TELF_MOVIL' &&
				fx.elements[i].name!= 'TELF_MOVIL' &&
				fx.elements[i].name!= 'TELF_MOVIL' &&
				fx.elements[i].name!= 'COD_TELF_TRAB' &&
				fx.elements[i].name!= 'TELF_TRAB' &&
				fx.elements[i].name!= 'COD_TELF_FAX' &&
				fx.elements[i].name!= 'FAX' &&
				fx.elements[i].name!= 'PISO_NUMERO' &&
				fx.elements[i].name!= 'APARTAMENTO' &&
				fx.elements[i].name!= 'CODIGO_POSTAL' &&
				fx.elements[i].name!= 'equipo_acc' &&
				fx.elements[i].name!= 'color2' &&
				fx.elements[i].name!= 'nom_comp' &&
				fx.elements[i].name!= 'fech_seg' &&
				fx.elements[i].name!= 'nom_reser' &&
				fx.elements[i].name!= 'ced_reser' &&
				fx.elements[i].name!= 'num_rif' &&
				fx.elements[i].name!= 'pla_ant' &&
				fx.elements[i].name!= 'dig_rif_res') &&
			(fx.elements[i].value == "" || 
				fx.elements[i].value == "null" || 
				fx.elements[i].value == "NULL" || 				
				fx.elements[i].value == "NaN" || 
				fx.elements[i].value.indexOf("\"", 0) > -1 || 
				fx.elements[i].value.indexOf("=", 0) > -1 || 
				fx.elements[i].value.indexOf("\'", 0) > -1)) {
					alert("Debe ingresar "+fx.elements[i].title);
				fx.elements[i].style.backgroundColor = "#F9E9B8";
			if(fx.elements[i].type != "hidden")
				fx.elements[i].focus();

			return false;
			break;
		}
		//fx.elements[i].style.backgroundColor = "transparent";
		if(fx.elements[i].name == "email" && fx.elements[i].value!=""){
			if(!email_str.test(fx.elements[i].value)) {
				alert("El formato del campo email no es valido");
				return false;
				break;
			}
		}
		if(fx.elements[i].value=="N999"){
			
			if(!email_str.test(fx.elements[i].value)) {
				alert("Seleccione "+fx.elements[i].title);
				fx.elements[i].style.backgroundColor = "#F9E9B8";
				return false;
				break;
			}
		}else if (fx.elements[i].length > 1){		
				fx.elements[i].style.backgroundColor = "#FFFFFF";
		}else if (fx.elements[i].length=="vacio")
		{
			
		}
				
		
		if(fx.elements[i].name == "FECHA_HOJA" && fx.elements[i].value==""){
			alert("Seleccione una fecha");
			return false;
			break;
		}
	}
	if (cuenta == 0){
		cuenta++;
		return true;
	}else{
		//alert("Por favor espera la respuesta de tu peticion!");
		return true;
	}
	
}

//onKeyPress="return(formato moneda(this,',','.',event))" 
function formato_moneda(fld, milSep, decSep, e) {
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true; 						// Enter
	if (whichCode == 8) return true; 							// Enter
	if (whichCode == 46) return true;						// Enter 
	if (whichCode == 0) return true; 							// Tab
    key = String.fromCharCode(whichCode); 				// Consigue el valor del codigo de tecla...
    if (strCheck.indexOf(key) == -1) return false; 	// no es una tecla valida
    len = fld.value.length;
    for(i = 0; i < len; i++)
     if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
    aux = '';
    for(; i < len; i++)
     if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) fld.value = '';
    if (len == 1) fld.value = '0'+ decSep + '0' + aux;
    if (len == 2) fld.value = '0'+ decSep + aux;
    if (len > 2) {
     aux2 = '';
     for (j = 0, i = len - 3; i >= 0; i--) {
      if (j == 3) {
       aux2 += milSep;
       j = 0;
      }
      aux2 += aux.charAt(i);
      j++;
     }
     fld.value = '';
     len2 = aux2.length;
     for (i = len2 - 1; i >= 0; i--)
      fld.value += aux2.charAt(i);
     fld.value += decSep + aux.substr(len - 2, len);
    }
    return false;
}

//onKeyPress="return(formato_campo(this,event,1))"
function formato_campo(fld,e,t) {

    var aux = aux2 = '';
	var i = j = 0;

	if(t==1)
    	var strCheck = '0123456789';
	if(t==2)
    	var strCheck = 'AaBbCcDdEeFfGgHhIiJjKkLlÑñNnMmOoPpQqRrSsTtUuVvWwXxYyZzáÁéÉíÍóÓúÚ ';
	if(t==3)
    	var strCheck = '0123456789-ext';
	if(t==4)
    	var strCheck = '0123456789,.';
	
    var whichCode = (window.Event) ? e.which : e.keyCode;
   // if (whichCode == 13) javascript:llamar();				// Enter
	if (whichCode == 8) return true; 							// Enter
	if (whichCode == 46) return true;						// Enter 
	if (whichCode == 0) return true; 							// Tab
    key = String.fromCharCode(whichCode); 				// Consigue el valor del codigo de tecla...
    if (strCheck.indexOf(key) == -1) return false; 	// no es una tecla valida
	fld.value += aux2.charAt(i);
}

//<a href="javascript:ventanaPopUp('pagina.html','ventana','600px','400px','yes');">link</a>
function ventanaPopUp (pagina,nom_ventana,ancho,alto,scroll_b){
	var opciones=("toolbar=no, "+
				  "location=no, "+
				  "directories=no, "+
				  "status=no, "+
				  "menubar=yes, "+
				  "scrollbars="+scroll_b+","+
				  "resizable=no,"+
				  "width="+ancho+","+
				  "height="+alto+"");
	var w=window.open(pagina,nom_ventana,opciones);
}

// Verifica que la longitud de caracteres sea igual al admitido en el input	text
function longitud_elb(fx){
	for (i = 0; i < fx.elements.length; i++) {
		if (fx.elements[i].type == "text"){	// si es input text
			if (fx.elements[i].maxLength != fx.elements[i].value.length){ // si la longitud no coincide con el maximo numero de caracteres que debe contenet el text
				if (fx.elements[i].name.indexOf("_exact")!=-1){ //el nombre del input tiene que tener indexado la cadena _exact
					alert(fx.elements[i].title+" debe contener solo "+fx.elements[i].maxLength+" caracteres");	
					fx.elements[i].style.backgroundColor = "#FFFFCC"; 		     												
					fx.elements[i].focus();
					return false;
					break;
				}
			}
			else // la longitud coincide 
			  return true;
			fx.elements[i].style.backgroundColor = "transparent";
		}					
	}	
};

//funcion para validar la sintaxis corecta de los correos electronicos
function validar_correo(txt){
	//expresion regular
	var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/
    if (!(b.test(txt.value))){
		alert("Direccion de correo no valida, Verifique");
		txt.value="";
		txt.focus();
	}
    //devuelve verdadero si validacion OK, y falso en caso contrario
    return b.test(txt)
}

//funcion para validar la longitud de un campo igual a ncaracteres
function chequea_longitud(form,ncaracteres) {
var ncaracteres; 
if (form.value.length != ncaracteres) {
	//form.focus();
	form.value="";
	alert("El campo debe contener "+ncaracteres+" caracteres obligatoriamente.");
	return false;
}
else return true;
}

//funcion para comprobar los campos de contraseña
function comprobar_contrase(form){
	if (document.regis.PASSWORD.value==form.value)
		return true;
	else{
		alert('Las contraseñas introducidas no coinciden verifique');  
		document.regis.PASSWORD.value="";
		form.value="";
		form.focus();
		return false;
	}
}

//funcion para comprobar que los campos de mail no sean iguales
function comprobar_mail(form){
	control= validar_correo(document.regis.EMAIL_ALTERNATIVO);
	//alert(control)
	if (control==false){
		if (document.regis.EMAIL.value!=form.value){
			return true;}
		else{
			alert('Los correos no pueden ser iguales...! Verifique.'); 
			form.value="";
			form.focus();
		return false;
		}
	}
}

//funcion para comprobar los campos de contraseña de cambio de contraseña
function comprobar_contrase2(form){
	if (document.login.pri_pass.value==form.value)
		return true;
	else{
		alert('Las contraseñas introducidas no coinciden verifique');  
		document.login.pri_pass.value="";
		form.value="";
		document.login.pri_pass.focus();
		return false;
	}
}


function marcar(){
		//Autor: David Concepcion
		//Funcion que recorre el formulario y le cambia el color de fondo de las cajas de texto que esten readOnly y con algun valor
		alert("marcando");
           var Formulario = document.getElementById("dataduplicado");
           var longitudFormulario = Formulario.elements.length;
		            	
             for (var i=0; i <=Formulario.elements.length-1;i++) {
             if (Formulario.elements[i].value != "" && Formulario.elements[i].type == "text" && Formulario.elements[i].readOnly == true){
			 	//alert(Formulario.elements[i]);
				Formulario.elements[i].style.background = "#F0F0F0";
				
			 }			
 			}
  }