var GLB_FUNCTION = null;
function setError(nodo, e)
{
         var a1=document.getElementById(nodo.getAttribute("id")+"_error");

         if(a1!=null)
         {
             if(e==null)
             e="Campo no se ha validado";
            
            if(a1.className.indexOf("error")<0)
            {
              a1.setAttribute("class_old",a1.className);
              
               if(a1.className.indexOf("flota")==-1)
                  a1.className="opcional error";
                 
              
               else
               a1.className="flota crearfix error compacto";
              
               a1.innerHTML= a1.innerHTML+"<p class=\"error\" >"+e+"</p>" ;
                return true;
            } 
         }
}
function resetError(nodo)
{
        try{
         //nodo.parentNode.style.background = "white";
         
         var a1=document.getElementById(nodo.getAttribute("id")+"_error");
         if(a1!=null && a1.className.indexOf("error")>=0)
         {
        
         if(a1.getAttribute("class_old")==null || a1.getAttribute("class_old")=="")
    a1.removeAttribute("className");
   
   else
            a1.className= a1.getAttribute("class_old");
            if(a1.childNodes[a1.childNodes.length-1].className=="error")
            a1.removeChild(a1.childNodes[a1.childNodes.length-1]);
          
         }
         //var er = 'err_' + nodo.name;
         //document.getElementById(er).innerText = "";
         }
         catch (e)
         {
       
         }
}

function body_onload()
{

    GLB_FUNCTION = null;
//    if(document.all)
    document.getElementById("form1").onsubmit=validarFormulario;
//    else
//     document.getElementById("form1").addEventListener("submit",validarFormulario,false);
   
    
    try {
    
       body_onload1();
    }
    catch (e)
    {
       nada = null;// alert("ERROR AL EJECUTAR ONLOAD1");
    }   
        
        
}
if(document.all)
{
   
    document.body.onload =  body_onload;
    }
//  else
//  {
//  alert("firefox");
//    document.body.addEventListener("load", body_onload,true);
//    }

function enviar()
{
if(!document.all)
validarFormulario("");
}
function validarFormulario(f)
{   

    f=document.getElementById("form1");
    var sw = 0;
    var nodos = f.getElementsByTagName("input");
    for (var i = 0; i < nodos.length; i++)
    {
   
        if (nodos[i].disabled)
        {
           resetError(nodos[i]);
           continue;      
        }
        if (isRequerido(nodos[i]) == false)
         {
             sw = 1;
          
         }

       else if (isValida(nodos[i]) == false) 
         sw = 1;
   }   
   
   //textarea requerido
   
    var nodos = f.getElementsByTagName("textarea");
    for (var i = 0; i < nodos.length; i++)
    {
   
        if (isRequerido(nodos[i]) == false)
         {
             sw = 1;
          
         }

       else if (isValida(nodos[i]) == false) 
         sw = 1;
   }   
   
   
   
   
   
   //desplegables requieridos
    var nodos_listas = f.getElementsByTagName("select");
    for (var i = 0; i < nodos_listas.length; i++)
    {   
//        if (nodos_listas[i].disabled)
//        {
//           resetError(nodos_listas[i]);
//           continue;      
//        }
        if (isRequerido(nodos_listas[i]) == false)
         {
             sw = 1;
          
         }
       else if (isValida(nodos_listas[i]) == false) 
         sw = 1;
   }   
   
    if (sw == 0) 
    {
 
     // validaciones especificas multicampo
        try {
               sw = validaFormulario2();
            }
            catch (e)
            {
             nada = true;
            }
    }
    if (sw == 0) 
    {
   
          return true; 
          }
    else 
    {
        alert("Hay errores en el formulario. Revise el Formulario");
         return false;
    }
}


function isValida(nodo)
{
        var f = nodo.getAttribute("validacion");
        try
        {
        if (f!=null && f!="")
        {
           var funcion= nodo.getAttribute("validacion") + "('"+nodo.value +"')";
           if (eval(funcion) == false)
           {
               
                var er = 'err_' + nodo.name;
                setError(nodo,   nodo.getAttribute("error"));    
                return false;
           }
           else
           {
                resetError(nodo);
                return true;
           }
        }
        }
        catch(e)
        {
        alert(""+e.description);
        return false;
        }
}
function isRequerido(nodo)
{
    var f = nodo.getAttribute("requerido"); 
    if (f==null || f=="")
       return true; 
       //alert("re"+nodo.value)
    if (nodo.value == "" || nodo.value == "-1")
    {
               setError(nodo, "requerido");
               return false;
    }
    else
    {
                resetError(nodo);
               return true;
    } 
}

function ifnumero(texto)
{
	if (texto == "")
	   return true;
        var r = /^[0-9]*$/
        if (texto.match(r) == null)
                return false;
	return true;
}
function ifmoneda(texto)
{
	if (texto == "")
	   return true;
        var r = /^[0-9]{1,7},[0-9]{2}$/
        if (texto.match(r) == null)
                return false;
	return true;
}

function iffecha(texto)
{
   
       if (texto == "")
	   return true;
   if (texto.length != 10)
      return false;
   // formato de fecha dd-mm-aaaa
   var r = /^\d{2}\/\d{2}\/\d{4}/;

   if (texto.match(r) == null)
   {
    // alert('patron1');
      
      return false;
   }   
   var diasMes = new Array(31, 28, 31, 30, 31, 30,
   			   31, 31, 30, 31, 30, 31);


   var dd, mm, aaaa
   dd   = parseInt(texto.substring(0, 2),10);
   mm   = parseInt(texto.substring(3, 5),10);
   aaaa = parseInt(texto.substring(6, 10),10);
   //alert(dd + "-" + mm + '-' + aaaa);
   // mes invalido
   if (mm > 12 || mm == 0)
      return false;
   // año bisiesto
   if (aaaa % 4 == 0)
       diasMes[1] = 29;
   // dia mayor que valido para el mes
   if (dd > diasMes[mm - 1])
       return false;
       
   var tmp;
   tmp =  texto.substring(6,10) + "-"    
   tmp += texto.substring(3,5) + "-"    
   tmp += texto.substring(0,2)
      
   return true;        	   			   	   

}
function checkMail(x)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) 
	    return true;
	else 
	    return false;
}

function checkNIFNIE(x)
{
try {
var l = x.length;
var numero = parseInt(x.substring(0, l - 1))
var letra = x.substring(l - 1, l )
// calculo de la letra
var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
var tmp = numero % 23;
var tmpletra = letras.substring(tmp,tmp+1);
if (tmpletra == letra)
  return true;
else 
  return false;  
}
catch (e)
{
    alert(e.description);
    return false;
}  

}

function checkDate(x)
{
 var dias = [31,28,31,30,31,30,31,31,30,31,30,31];
 
 var filter = /^\d{2}\/\d{2}\/\d{4}/
 if (filter.test(x)) 
 {
        var dd = parseInt(x.substring(0, 2));
        var mm = parseInt(x.substring(3, 5));
        var yyyy = parseInt(x.substring(6, 10));
        if ( ((yyyy % 4 == 0) && (yyyy % 100 != 0)) || (yyyy % 400 == 0)) 
             dias[mm-1] = 29;
        if (mm > 12)         return false;
        if (dd > dias[mm-1]) return false;
        return true;     
  }
	else 
	    return false;
 
}

function checkCP(x)
{
     var filter = /^\d{5,5}/
     if (filter.test(x)) 
     {
        return true;
     }
     else   
        return false;
}
function checkPalabra(x)
{

     var filter = /^\w/
     if (filter.test(x)) 
     {

        return true;
     }
     else   
        return false;
}

function checkTelefono(x)
{
  var filter = /^\d{9,9}/
     if (filter.test(x)) 
     {
    
        return true;
     }
     else   
        return false;
}

function checkEntero4(x) {   return ifRegExp(x,    /^\d{4,4}/); }
function checkEntero2(x) {   return ifRegExp(x,    /^\d{2,2}/); }
function checkEntero10(x) {   return ifRegExp(x,   /^\d{10,10}/); }
function checkEntero12(x) {   return ifRegExp(x,   /^\d{12,12}/); }
function ifRegExp(x, e)
{
      if (e.test(x)) 
     {
        return true;
     }
     else   
        return false;
}



function  dc(i) {
     var pesos=[6,3,7,9,10,5,8,4,2,1];
     var contador = 0;
     var s = 0 ;
     var d;
     while (i != 0)
     {
       
        d = i % 10;
        i = Math.floor(i / 10);
        s += d * pesos[contador];
        contador++;
     }
     var resultado= (11-(s % 11));
     if (resultado==10)
        resultado=1;
     else if (resultado==11)
        resultado=0;
     return resultado;
 } 
function DigitosControl(entidad, sucursal, cuenta) 
{
    var d = dc(entidad * 10000 + sucursal);
    d = d * 10;
    d += dc(cuenta);
    return d;
 }
 
function checkCCC(entidad, agencia, dc, cuenta)
{
    var dc1 = DigitosControl(entidad, agencia, cuenta);
    if (dc == dc1)
    return true;
    else
    return false;
    
}

function checkNASS(x)
{
    if (checkEntero12(x) == false)
       return false;
    var aa = parseInt(x.substring(0, 10),10);
    var cc = parseInt(x.substring(10, 13),10);
    var v = aa % 97;
   
   // if (v == cc) return true;
   // else   return false;
    
    return true;   
}
function setTrueFalse(valor, id)
{
    for (var i = 0; i < id.length; i++)
    {
   if (valor == "SI")
        document.getElementById(id[i]).disabled = false;
    else
        document.getElementById(id[i]).disabled = true;
    }
}
function nueva()
{
	return true;
}
