function EsVacio(formulario, campo){
	var i=1;
	var blancos ='';
	
	while (i<=document.forms[formulario].elements[campo].value.length)
        {
           blancos = blancos + ' ';
           i++;
        }   	

        return (document.forms[formulario].elements[campo].value == blancos);
}        
        
function validar_correo(correo){

    //	return /^\w([\w_\-\.\+]*\w)?@\w[\w_\-\.]*\w\.\w{2,}$/.test(email);

    // compruebo que no esté vacía la cadena exaustivamente
    if (correo == "" || correo == null || correo == "null" || correo == undefined || correo == "undefined") {
        return false;
    // compruebo si hay una arroba y sólo una
    } else if (correo.indexOf("@") == -1 || correo.indexOf("@") != correo.lastIndexOf("@")) {
        return false;
    } else {
        // dividimos la dirección en usuario y dominio
        var partes = correo.split("@");
        var usuario = partes[0];
        var dominio = partes[1];

        // compruebo usuario
        if (usuario.length<1) {
            return false;
        } else {
            // compruebo si hay algún carácter raro
            var caracteresProhibidos = new Array("º", "ª", "!", "#", "$", "%", "&", "¬", "/", "(", ")", "=", "?", "¿", "¡", ",", ";", ":", "[", "]", "{", "}", "á", "é", "í", "ó", "ú", "Á", "É", "Í", "Ó", "Ú");
            var numCaracteresProhibidos = 0;
            for (var k = 0; caracteresProhibidos[k]; k++) {
                if (usuario.indexOf(caracteresProhibidos[k]) != -1) {
                    numCaracteresProhibidos++;
                }
            }

            if (numCaracteresProhibidos>0) {
                return false;
            // compruebo dominio
            } else if (dominio.indexOf(".") == -1 || dominio.length<1) {
                return false;
            } else {
                var partes_dominio = dominio.split(".");
                var extension = partes_dominio[partes_dominio.length-1];
                // compruebo que el dominio tenga como mínimo 3 caracteres antes del punto
                if (dominio.length-extension.length<4) {
                    return false;
                } else if (extension.length<2 || extension.length>4) {
                    return false;
                } else {
                    return true;
                }
            }
        }
    }
}

function Bisiesto(pistrFecha){
	anno  = pistrFecha.substring(4,8);
	mes1   = pistrFecha.substring(2,4);
	dia   = pistrFecha.substring(0,2);	
	resto = anno % 4;
	if ((mes1 == 1) || (mes1 == 3) || (mes1 == 5) || (mes1 == 7) ||(mes1 == 8) || (mes1 == 10) || (mes1 == 12))
	{
		maxdia = 31;
	}
    else
	{
		if (mes1 == 2)
		{
			if (resto == 0)
			{
				maxdia = 29;
			}
			else
			{
				maxdia = 28 ;
			}
		}
		else
		{
			if ((mes1 == 4) || (mes1 == 6) || (mes1 == 9) || (mes1 == 11))
			{
				maxdia = 30;
			}
			else
			{
				return(false);
			}
		}   
	}
	if ((dia > maxdia) || (dia < 1))
		return(false);
	if ((anno > 2500) || (anno < 1800))
		return(false);
	return (true);
}

function numerico(pInString,piDecimal){
	var RefStringE = "1234567890";
	var RefStringD = "1234567890.";

	if (pInString.length==0)
		return false;
	if (piDecimal == 0)
		RefString = RefStringE ;
	else
		RefString = RefStringD;
	Punto = 0 ;
	for (Count=0; Count < pInString.length; Count++)
	{
		TempChar= pInString.substring (Count, Count+1);
		if (RefString.indexOf (TempChar, 0)==-1)
		{
			return false;
		}
		if (TempChar == ".")
		{
			Punto = Punto + 1;
		} 
	}
	if (Punto > 1)
		return false;
	return true;
}

function ValidaFormatoFecha(pistrFecha){
	var datFecha=pistrFecha;
	var strDia=datFecha.substring(0,datFecha.indexOf("/",0));
	var strMes=datFecha.substring(datFecha.indexOf("/",0)+1,datFecha.indexOf("/",datFecha.indexOf("/",0)+1));
	var strAnno=datFecha.substring(datFecha.indexOf("/",datFecha.indexOf("/",0)+1)+1,datFecha.length);
	/*if ((strDia.length == 1) || (strMes.length == 1) || (strAnno.length == 2))*/
	if ((strDia.length < 1) || (strDia.length > 2) || (strMes.length < 1) || (strMes.length > 2) || (strAnno.length < 4))
		{
			return (false);
		}
	else
		return (true);
	}
	
function ValidarFecha(pistrFechaA){
	var strNuevaFecha;
	
	if ((pistrFechaA.substring(1,2) == "/") || (pistrFechaA.substring(1,2) == "-"))
		{
		iDia = "0" + pistrFechaA.substring(0,1);
		strNuevaFecha = iDia + pistrFechaA.substring(1,pistrFechaA.length);
		pistrFechaA = strNuevaFecha;
		}
	if ((pistrFechaA.substring(4,5) == "/") || (pistrFechaA.substring(4,5) == "-"))
		{
		iMes = "0" + pistrFechaA.substring(3,4);
		strNuevaFecha = pistrFechaA.substring(0,3) + iMes + pistrFechaA.substring(4,pistrFechaA.length);
		pistrFechaA = strNuevaFecha;
		}
	if (pistrFechaA.length == 8) 
		{
		iFecha = parseFloat(pistrFechaA.substring(6,8))
		if (iFecha > 20)			
			{
			strNuevaFechaA = pistrFechaA.substring(0,6) + "19" + pistrFechaA.substring(6,8)
			}
		else
			{
			strNuevaFechaA = pistrFechaA.substring(0,6) + "20" + pistrFechaA.substring(6,8)
			}
		pistrFechaA = strNuevaFechaA
		}
	strNuevaFechaA = pistrFechaA.substring(0,2) + pistrFechaA.substring(3,5) + pistrFechaA.substring(6,10);
	strSlash       = pistrFechaA.substring(2,3) + pistrFechaA.substring(5,6);	
	if (!(strNuevaFechaA.length == 0))
		{
		if (!(numerico(strNuevaFechaA,0)))
			{
			return(false) ;
			}
		else
			{
			if ((strSlash != "//") && (strSlash != "--"))
				{
				return(false);	
				}
			else
				{
				if (!(Bisiesto(strNuevaFechaA)))
					{
					return(false);
					}	
				}
			}
		}

	if ((pistrFechaA.length != 8) && (pistrFechaA.length != 10) && (pistrFechaA.length !=0))
		{
		return(false);
		}
	return(true);
	alert ()
}

function FechasCorrectas(fechaDesde, fechaHasta){
	var pos, diad, mesd, anod;
	var pos2, diah, mesh, anoh;
	var valor = true;

	pos = fechaDesde.indexOf("/");
	diad = fechaDesde.substring(0,pos)*1;
	pos2 = fechaDesde.indexOf("/", pos + 1);
	mesd = fechaDesde.substring(pos+1,pos2)*1;
	pos = fechaDesde.indexOf("/", pos2 + 1);
	anod = fechaDesde.substring(pos2 + 1,10)*1;

	pos = fechaHasta.indexOf("/");
	diah = fechaHasta.substring(0,pos)*1;
	pos2 = fechaHasta.indexOf("/", pos + 1);
	mesh = fechaHasta.substring(pos+1,pos2)*1;
	pos = fechaHasta.indexOf("/", pos2 + 1);
	anoh = fechaHasta.substring(pos2 + 1,10)*1;
	
	if(anod > anoh){
		valor = false;
	}
	else
		if(anod == anoh){
			if(mesd > mesh){
				valor = false;
			}
			else
				if(mesd == mesh){
					if(diad > diah){
						valor = false;
					}
				}
			
		}
	
	
	return valor;
}

function soloNumeros(obj,e) {
	var tecla;
	var caracter;
	var tmp;

	if (window.event)
		tecla = window.event.keyCode;
	else if (e)
		tecla = e.which;
	else
		return true;

	caracter = String.fromCharCode(tecla);

	if (tecla == 13) {
		obj.blur();
	}else {
		if ((tecla==null) || (tecla==0) || (tecla==8) || (tecla==9) || (tecla==27) ) {
			return true;
		}else {
			if(caracter == ","){
				if(String(obj.value).length==0 || String(obj.value).indexOf(",") > -1){
					return false;
				}else{
					return true;
				}
			}else{
				if ( ("0123456789-").indexOf(caracter) > -1){
					return true;
				}else{
					return false;
				}
			}
		}
	}
}


