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){
	if(correo != "") {
		emailad = correo;
		var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
		var check=/@[\w\-]+\./;
		var checkend=/\.[a-zA-Z]{2,4}$/;	
		if(((emailad.search(exclude) != -1)|| (emailad.search(check)) == -1)|| (emailad.search(checkend) == -1)) {
			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 refresca_imagen(valImageId) {
	var objImage = document.getElementById(valImageId);
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

function poner_ceros(valor){
	
	var longitud = valor.length;
	
	for (var i=0;i<9-longitud;i++){
		valor = "0" + valor;
	}
	
	return valor;
}

function valida_nif_nie(a){
	
	a = poner_ceros(a.toUpperCase());
	
	var temp      = a;
	var cadenadni = "TRWAGMYFPDXBNJZSQVHLCKE";
 
	if (temp!==''){
	    
		//si no tiene un formato valido devuelve error
		if ((!/^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$/.test(temp) && !/^[T]{1}[A-Z0-9]{8}$/.test(temp)) && !/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			return false;
		}
 
		//comprobacion de NIFs estandar
		if (/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			posicion = a.substring(8,0) % 23;
			letra = cadenadni.charAt(posicion);
			var letradni=temp.charAt(8);
			if (letra == letradni)
			{
			   	return true;
			}
			else
			{
				return false;
			}
		}
 
		//algoritmo para comprobacion de codigos tipo CIF
		suma = parseInt(a[2])+parseInt(a[4])+parseInt(a[6]);
		for (i = 1; i < 8; i += 2)
		{
			temp1 = 2 * parseInt(a[i]);
			temp1 += '';
			temp1 = temp1.substring(0,1);
			temp2 = 2 * parseInt(a[i]);
			temp2 += '';
			temp2 = temp2.substring(1,2);
			if (temp2 == '')
			{
				temp2 = '0';
			}
 
			suma += (parseInt(temp1) + parseInt(temp2));
		}
		suma += '';
		n = 10 - parseInt(suma.substring(suma.length-1, suma.length));
 
		//comprobacion de NIFs especiales (se calculan como CIFs)
		if (/^[KLM]{1}/.test(temp))
		{
			if (a.charAt(8) == String.fromCharCode(64 + n))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
 
		//comprobacion de NIEs
		//T
		if (/^[T]{1}/.test(temp))
		{
			if (a.charAt(8) == /^[T]{1}[A-Z0-9]{8}$/.test(temp))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
 
		//XYZ
		if (/^[XYZ]{1}/.test(temp))
		{
			pos = str_replace(['X', 'Y', 'Z'], ['0','1','2'], temp).substring(0, 8) % 23;
			if (a.charAt(8) == cadenadni.substring(pos, pos + 1))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
 
	return false;
} 

 function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
 
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
 
    return sa ? s : s[0];
} 

