function comprobarSiBisisesto(anio){
if ( ( anio % 100 != 0) && ((anio % 4 == 0) || (anio % 400 == 0))) {
    return true;
    }
else {
    return false;
    }
}


function ValidarFecha(fecha){
   
		if (fecha != undefined && fecha != "" ){
        var re=/^[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]$/

        if (!(re.test(fecha))){
        //alert(fecha);
            //alert("formato de fecha no válido (dd/mm/aaaa)");
            return false;
        }

        var dia  =  parseInt(fecha.substring(0,2),10);
        var mes  =  parseInt(fecha.substring(3,5),10);
        var anio =  parseInt(fecha.substring(6,10),10);

        switch(mes){
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                numDias=31; break;
            case 4: case 6: case 9: case 11:
                numDias=30; break;
            case 2:
                if (comprobarSiBisisesto(anio)){ numDias=29 }else{ numDias=28};break;
            default:
                //alert("Fecha introducida errónea1");
                return false;
        }

        if (dia>numDias || dia==0){
            //alert("Fecha introducida errónea2");
            return false;
        }
    //alert('fecha correcta');
        return true;
    }
}

function EsTelefono(id)
{
	if (!id.match(/^[0-9]{9,14}$/) )
		return false;
	else
	  return true;
}


function EsNumerico(str){
	//comprobar si tiene decimales
	var sep_decimal = str.lastIndexOf(',');
		
	if (sep_decimal != -1)
	{
		enteros   = str.substr(0,sep_decimal);
		decimales = str.substr(sep_decimal+1,str.length);

		//comprobamos que los decimales sean 1 ó 2 números
		if ( !decimales.match(/^[0-9]{1,2}$/) )
			return false;
	}
	else
	{
		// Si no tiene decimales
		enteros = str;
		decimales = '';
	}
	
	// Si los enteros son números y no tienen ningun punto
	if (enteros.match(/^[0-9]*$/) ){
		if (decimales!='' && enteros.length==0)		//si tiene decimales, pero no tiene enteros, no es correcto
			return false;
		else																			//si no tiene decimales, es que se han introducido un número entero.
			return true;
	}
	else
	{
		//si tienen puntos, hay que comprobar que estén bien puestos
		pos = enteros.length-4;
	
	  if (pos < 0)
		{
			if ( !enteros.match(/^[0-9]{1,3}$/) )
			 	return false;
			else
			  return true;
		}
		else
		{
		   //mientras la posicion sea mayor que 0, comprueba si tras el punto de miles, van 3 números
	    	while(pos>=0)
				{
			     temp = str.substr(pos,4);
			     if ( !temp.match(/^[\.]{1}[0-9]{3}$/) )
			     	return false;
			     pos = pos-4; 
		    }
 
		    //comprueba los caracteres anteriores al primer punto (.)
		    temp = str.substr(0,pos+4);
		    if ( !temp.match(/^[0-9]{1,3}/) )
			    return false;
					
				return true;	
  	}
  }
}


//Marca o desmarca todos los checkbox
function todos(nombre){
    marcaTodos  = 'todos'+nombre;
		nombre      = 'campos'+nombre+'[]';
		campos = document.getElementsByName(nombre);
		
		casillatodos = document.getElementById(marcaTodos);
		
		if( casillatodos.checked==false ){
			for (i = 0; i < campos.length; i++)
	    	campos[i].checked   = false;
		}
		
		if( casillatodos.checked==true ){
			for (i = 0; i < campos.length; i++)
	    	campos[i].checked   = true;
		}
}


function lTrim(sStr){
     while (sStr.charAt(0) == " ")
     sStr = sStr.substr(1, sStr.length - 1);
     return sStr;
 }

 function rTrim(sStr){
     while (sStr.charAt(sStr.length - 1) == " ")
     sStr = sStr.substr(0, sStr.length - 1);
     return sStr;
}

function allTrim(sStr){
    return rTrim(lTrim(sStr));
}



// Comprueba la validez de una cuenta de e-mail
	function isValidoEmail(email) {
		var at   = "@";
		var dot  = ".";
		var lat  = email.indexOf(at)
		var lstr = email.length
		var ldot = email.indexOf(dot)
		var lOk  = true;

		if ((email==null)||(email=="")){
		   lOk = false;
		} else if (email.indexOf(at)<=0 || email.indexOf(at)+1==lstr) {
		   lOk = false;
		} else if (email.indexOf(dot)<=0 || email.indexOf(dot)+1==lstr) {
		   lOk = false;
		} else if (email.indexOf(at,(lat+1))!=-1) {
		   lOk = false;
		} else if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot) {
		   lOk = false;
		} else if (email.indexOf(dot,(lat+2))==-1) {
		   lOk = false;
		} else if (email.indexOf(" ")!=-1) {
		   lOk = false;
		}
		return lOk;
	}

