function sustituirBR(valor){
		cr=String.fromCharCode(13)
		lf=String.fromCharCode(10)
		crre = new RegExp(cr,"g")
		lfre = new RegExp(lf,"g")
		valor=valor.replace(crre,"<br/>")
		valor=valor.replace(lfre,"")
		return(valor);
}
//Fecha de creacion: 2/10/2003
//Funcion que sustituye el comodin de la apostrofe por el apostrofe.
function RemplazarComodinApostrofe(f)
{
	for (count=0;count<=f.elements.length-1;count++)	
	{
		if (f.elements[count].name.substring(0,2) != "S_" ) {
			texto = f.elements[count].value
			f.elements[count].value = texto.replace(/#131#/g,"\'");
		}	
	}
}
////////////////////////////////////////////////////////////////////

function valida_datos(f,sub) {
	a=0;
	c=0;//Variable de control de erorres de las tablas calculadas...
	Msg="";
	if (sub==0) MsgRequerido="";
	var count;
	var tipo;
	var campo;

    //Si un V_Datos es distinto de cero, todos los subformularios requeridos sin datos deben tener V_Datos=1
    for (count=0;count<=f.elements.length-1;count++) {
        if ((f.elements[count].name.lastIndexOf("V_Datos") != "-1") && (f.elements[count].value!=0)) {
            SForm = f.elements[count].name.replace("V_Datos","");
//alert('SubFormulario: ' + SForm);
            //Bucle buscando subformularios de SForm
            for (countVD=0;countVD<=f.elements.length-1;countVD++) {
                if ((f.elements[countVD].name.lastIndexOf("V_SForm") != "-1") && (f.elements[countVD].name.lastIndexOf(SForm) != "-1")){
                    //Verifico si el subformulario es requerido y no tiene registros
                    if ((f.elements[countVD].Requerido == 1) && (f.elements["V_NRegistros"+f.elements[countVD].FContenido.replace("eg","lg")].value==0)) {
                        //Activamos el proceso de este subformulario
//alert("Activamos" + f.elements[countVD].name);
//alert("FContenido: " + f.elements[countVD].FContenido);
                        try {
                            f.elements["V_Datos"+f.elements[countVD].FContenido].value = 1;
                            f.elements["V_SUBMIT"+f.elements[countVD].FContenido].value = 1;
                            }
                        catch (error){}	
                        }
                    }
                }
            }
        }
    
    //Si un V_Datos es distinto de cero su formulario parent debe tener V_Datos=1
    for (count=0;count<=f.elements.length-1;count++) {
        if (f.elements[count].name.lastIndexOf("V_Datos") != "-1") {
            SForm = f.elements[count].name.replace("V_Datos","");
            if (SForm.lastIndexOf("root") == "-1") {
//alert(' ' + SForm + ' ' + f.elements["V_Datos"+SForm].value + ' ' + f.elements["V_SUBMIT"+SForm].value);
                if (f.elements["V_Datos"+SForm].value != 0) {
                    f.elements["V_Datos"+f.elements["V_AFContenido"+SForm].value].value = 1;
                    f.elements["V_SUBMIT"+f.elements["V_AFContenido"+SForm].value].value = 1;
                    }
                }
            }
        }
        
    for (countVD=0;countVD<=f.elements.length-1;countVD++) {                //Buscamos los V_Datos que tenemos
        if (f.elements[countVD].name.lastIndexOf("V_Datos") != "-1") {      //Verificamos si hay que procesarlo
            if (f.elements[countVD].value!=0) {
                SForm = f.elements[countVD].name.replace("V_Datos","");
                f.elements["V_SUBMIT"+SForm].value=1;                       //y ponemos su V_Submit a 1
                
                //Si ejecutamos el submit de un subformulario tambien debemos ejecutar el submit del formulario que lo contiene
                if (SForm.lastIndexOf("eg") > 1) {
                    SFormP = SForm.substring(1,SForm.lastIndexOf("eg"));
                    try {f.elements["V_SUBMIT"+SFormP].value=1;}                  //y ponemos su V_Submit a 1 al formulario superior
                    catch (error){}	
                    }
self.status = 'Verifica requeridos' + SForm;
//alert('Verifica requeridos' + SForm);
	            for (count=0;count<=f.elements.length-1;count++)
	            {
	                tipo = f.elements[count].name.substring(0,2);
	                campo = f.elements[count].name.substring(4,f.elements[count].name.length);
		            if ((f.elements[count].name.lastIndexOf(SForm) != "-1") && (tipo != "V_") && (f.elements[count].Requerido == 1)) {//Validamos requeridos solo en este formulario
self.status = self.status + ', ' + f.elements[count].TV;
		                if (tipo == "E_" || tipo == "T_" || tipo == "D_" || tipo == "A_" || tipo == "N_" || tipo == "B_" || tipo == "H_" || tipo == "G_" || tipo == "Y_") 
			                {
				                //if (f.elements[count].name.substring(1,4) == "_R_") {CheckSpecified(f.elements[count].value, f.elements[count].TV)}
				                if (f.elements[count].value, f.elements[count].Requerido == 1) {CheckSpecified(f.elements[count].value, f.elements[count].TV)}
			                }
		                //Valores requeridos C_(Check)
		                if (tipo == "C_" ) 
			                {
				                //if (f.elements[count].name.substring(1,4) == "_R_") {CheckSpecifiedC(f.elements["V_R_"+campo].checked, f.elements[count].TV)}
				                if (f.elements[count].value, f.elements[count].Requerido == 1) {CheckSpecifiedC(f.elements["V_R_"+campo].checked, f.elements[count].TV)}
			                }
		                //Valores requeridos C_(Check) Multiple check
		                if (tipo == "M_" || tipo == "W_") 
			                {
                					
				                //bucle para comprobar que el array del multiple check esta alguno relleno, obligatorio quiere decir que al menos uno esta seleccionado					
				                if (f.elements[count].value, f.elements[count].Requerido == 1) {
					                chequeado = false;
					                nelementos=1
					                if (f.elements["V_R_"+campo].length != undefined){nelementos=f.elements["V_R_"+campo].length}
					                if (nelementos==1)
						                {chequeado=chequeado || f.elements["V_R_"+campo].checked}
					                else
						                {for (cc=0;cc<=nelementos-1;cc++){																
							                chequeado=chequeado || f.elements["V_R_"+campo][cc].checked}
						                }																	
					                if (chequeado != true){
						                MsgRequerido = MsgRequerido + "\n" + f.elements[count].TV;
						                a = 1;
					                }
				                }					
			                }
		                //Ventana auxiliar
		                if (tipo == "X_") {
		                    //bucle para comprobar que el array del multiple check esta alguno relleno, obligatorio quiere decir que al menos uno esta seleccionado					
		                    if (f.elements[count].value, f.elements[count].Requerido == 1) {
			                    chequeado = false;
			                    try {if (f.elements["V_O_"+campo].length != undefined){chequeado = true}}
			                    catch (error){}
			                    try {if (f.elements["V_O_"+campo].value != ""){chequeado = true}}
			                    catch (error){}					
			                    if (chequeado != true){
				                    MsgRequerido = MsgRequerido + "\n" + f.elements[count].TV;
				                    a = 1;
			                        }
		                        }
                            }
		                //Valores requeridos L_(Lista texto) P_ (Lista numero)
		                if (tipo == "L_" || tipo == "P_" ) 
			                {
				                if (f.elements[count].value, f.elements[count].Requerido == 1) {CheckSpecified(f.elements[count].options[f.elements[count].selectedIndex].value, f.elements[count].TV)}
			                }
		                if (tipo == "S_" ) 
			                {
                					
				                //bucle para comprobar que el array del multiple slect esta alguno seleccionado, obligatorio quiere decir que al menos uno esta seleccionado					
				                if (f.elements[count].value, f.elements[count].Requerido == 1) {
					                seleccionado = false;
					                for (cc=0;cc<=f.elements[count].length-1;cc++){							
						                seleccionado=seleccionado || f.elements[count][cc].selected}
					                if (seleccionado != true){
						                MsgRequerido = MsgRequerido + "\n" + f.elements[count].TV;
						                a = 1;
					                }
				                }					
			                }
		                if (tipo == "F_" || tipo == "R_" || tipo == "Q_" || tipo == "Z_")
			                {
				                if (f.elements[count].value, f.elements[count].Requerido == 1) {CheckSpecified(f.elements["V_dia_"+campo].value, f.elements[count].TV)}
			                }
		                }
	                }
                }
            }
        }
	    
	    
	    //si todo se cumple	

	    if (a==0 && c==0) 
		    {
self.status = 'Verifica valores';
			    //Comprobación de valores por tipo
			    a=0;				
			    for (count=0;count<=f.elements.length-1;count++)	
			    {
self.status = 'Verifica valores: ' + f.elements[count].name;
			        tipo = f.elements[count].name.substring(0,2);
			        campo = f.elements[count].name.substring(4,f.elements[count].name.length);
			        //DNI
			        if (tipo == "D_") 
				        {
					        f.elements[count].value=nif(f.elements[count].value);
				        }
			        //Textarea (máximo de caracteres)
			        if (tipo == "A_") 
				        {
					        TextL(f.elements[count].value,f.elements[count].MAX,f.elements[count].MAXT)
				        }
			        //Textarea sustitución de BR(máximo de caracteres y BR)
			        if (tipo == "B_") 
				        {
					        f.elements[count].value=sustituirBR(f.elements[count].value)
					        TextL(f.elements[count].value,f.elements[count].MAX,f.elements[count].MAXT)
				        }
			        //E_ email correcto
			        if (tipo == "E_") 
				        {
					        Isemail(f.elements[count].value,f.elements[count].TE)							
				        }
			        //F_ (Fecha)				
			        if (tipo == "F_" || tipo == "R_" || tipo == "Q_" || tipo == "Z_" )
				        {
					        ob=0;
					        miny=1900;
					        maxy=2078;
					        if (f.elements[count].value, f.elements[count].Requerido == 1) {ob=1;}
					        if (f.elements[count].MINY != undefined){miny=f.elements[count].MINY;}
					        if (f.elements[count].MAXY != undefined){maxy=f.elements[count].MAXY;}
					        arr=CheckDate(f.elements["V_dia_"+campo].value,f.elements["V_mes_"+campo].value,f.elements["V_ano_"+campo].value, f.elements[count].TV + " incorrecta",ob,miny,maxy);
					        f.elements[count].value=arr[0];
					        f.elements["V_dia_"+campo].value=arr[1];
					        f.elements["V_mes_"+campo].value=arr[2];
					        f.elements["V_ano_"+campo].value=arr[3];
    						
				        }
			        //Numero
			        if (tipo == "N_" || tipo == "G_" || tipo == "K_") {
					        Isnum(f.elements[count].value, f.elements[count].MMT,f.elements[count].MINV,f.elements[count].MIN,f.elements[count].MAXV,f.elements[count].MAX);
				        }
			    }
self.status = 'Verifica ok';
			    if (a==0) {					//si todo se cumple
				    if(sub){return true;}
				    else{f.submit();}
			    }
			    else {						//Si falla alguna
				    try{f.V_SUBMIT.value="";}
				    catch(error){}
				    alert(Msg);	
				    if(sub){return false;}
				    }

		    }
	    else//Si falla alguna
		    {
self.status = 'fallo en verificación';
			    try{f.V_SUBMIT.value="";}
			    catch(error){}
			    if(c==1)
			    {
				    alert(MSG_C)
			    }
			    else
			    {
				    if (sub==0) alert("Los siguientes campos son requeridos:\n" + MsgRequerido);			
				    if(sub){return false;}	
			    }
		    }
        }
