
// whitespace characters
var whitespace = " \t\n\r";
var defaultEmptyOK = false;

function Vacio(s)
{   return ((s == null) || (s.length == 0))
}

function esURL(s,campo)
/*	Devuelve true si la cadena tiene al menos un punto	
	Devuelve false en cualquier otro caso, y le quita el http si lo tiene */
{   
	if(Vacio(s))
		return true;
	var ok = false;
	if ( s.indexOf(".") != - 1)
	{
		pos1 = s.indexOf(".")
		if (pos1 > 0){
			ok=true;
			if(campo.value.substr(0,4)=="http")
				campo.value = campo.value.substr(7,campo.value.lenght);
		}
	}
	return ok;
}

function Fecha(dateStr)
{
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
return false;
}
month = matchArray[3]; // parse date into variables
day = matchArray[1];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
//alert("El mes debe estar entre 1 y 12.");
return false;
}
if (day < 1 || day > 31) {
//alert("El día debe estar entre 1 y 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
//alert("El mes "+month+" no tiene 31 dias!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
//alert("Febrero de " + year + " no tiene " + day + " dias!");
return false;
   }
}
return true;  // date is valid
}


/****************************************************************/

// Returns true if string s is empty or 
// whitespace characters only.

function EnBlanco(s)

{   var i;

    // Is s empty?
    if (Vacio(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function esFichero()
/*	Devuelve true si el Fichero tiene la extensión que se pasa como parámetro
	 Devuelve false en cualquier otro caso */
{
	var cadena,res,extensiones,i;
	
	extensiones = esFichero.arguments;
	cadena = extensiones[0];
	cadena = cadena.toUpperCase();
	res = false;
	for (i=1;i < extensiones.length; i++)
	{ 
		if (cadena.indexOf("."+ extensiones[i]) != -1 )
		{
			res = true;		
		}
	}
	return (res);
}

function FechaMayorque(datIni,datFin)
/*	Compara dos fechas
	datIni : Fecha Inicial
	datFin : Fecha Final
	Devuelve:
		True si datFin > datIni
		False en cualquier otro caso
*/
{
	var ok = true;
	var fechaini = str2Date(datIni);
	var fechafin = str2Date(datFin);
	
	if (fechafin<fechaini)
		ok=false;
	
	return ok;	
}
// Convierte una cadena(con Formato Fecha) a Fecha
function str2Date(strFecha)

/* Admite fechas en formato dd/mm/yy d/mm/yy dd/m/yy d/m/yy dd/mm/yyyy d/mm/yyyy dd/m/yyyy d/m/yyyy 
   dd-mm-yy d-mm-yy dd-m-yy d-m-yy dd-mm-yyyy d-mm-yyyy dd-m-yyyy d-m-yyyy 
*/
{
	var posdia,posmes,dia,mes,ano;
	
	if (strFecha.indexOf("/") != - 1)
	{
		posdia = strFecha.indexOf("/");
		dia =  strFecha.substring(0,posdia);
		posmes = strFecha.indexOf("/",posdia+1);
		mes	= strFecha.substring(posdia+1,posmes);
		ano = strFecha.substring(posmes+1);
		if (ano < 100)
		{
			ano = parseInt(ano,10) + 2000;
		}
		return new Date(ano,mes-1,dia);
	}
	
	if (strFecha.indexOf("-") != - 1)
	{
		posdia = strFecha.indexOf("-");
		dia =  strFecha.substring(0,posdia);
		posmes = strFecha.indexOf("-",posdia+1);
		mes	= strFecha.substring(posdia+1,posmes);
		ano = strFecha.substring(posmes+1);
		if (ano < 100)
		{
			ano = parseInt(ano,10) + 2000;
		}
		return new Date(ano,mes-1,dia);	
	}
	
	dia = strFecha.substring(0,2);
	mes = strFecha.substring(2,4);
	ano = strFecha.substring(4);
	if (ano < 100)
	{
		ano = parseInt(ano,10) + 2000;
	}
	return new Date(ano,mes-1,dia);
	
}
//convierte una fecha a formato ansi yyyymmdd
function AnsiDate(strFecha)

/* Admite fechas en formato dd/mm/yy d/mm/yy dd/m/yy d/m/yy dd/mm/yyyy d/mm/yyyy dd/m/yyyy d/m/yyyy 
   dd-mm-yy d-mm-yy dd-m-yy d-m-yy dd-mm-yyyy d-mm-yyyy dd-m-yyyy d-m-yyyy 
*/
{
	var posdia,posmes,dia,mes,ano;
	
	if (strFecha.indexOf("/") != - 1)
	{
		posdia = strFecha.indexOf("/");
		dia =  strFecha.substring(0,posdia);
		posmes = strFecha.indexOf("/",posdia+1);
		mes	= strFecha.substring(posdia+1,posmes);
		ano = strFecha.substring(posmes+1,strFecha.length);	
	}
	else{
		if (strFecha.indexOf("-") != - 1)
		{
			posdia = strFecha.indexOf("-");
			dia =  strFecha.substring(0,posdia);
			posmes = strFecha.indexOf("-",posdia+1);
			mes	= strFecha.substring(posdia+1,posmes);
			ano = strFecha.substring(posmes+1,strFecha.length);	
		}
		else{
			dia = strFecha.substring(0,2);
			mes = strFecha.substring(2,4);
			ano = strFecha.substring(4,strFecha.length);
		}
	}
	if(ano<100)
		ano = parseInt(ano)+2000;
	if(dia<10)
		dia = "0"+dia;
	if(mes<10)
		mes = "0"+mes;
	return (ano + mes + dia);
	
}

/****************************************************************/

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function Email(s)
{   if (Vacio(s)) 
       if (Email.arguments.length == 1) return defaultEmptyOK;
       else return (Email.arguments[1] == true);
   
    // is s whitespace?
    if (EnBlanco(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function NIF(strNif)
{
	var arrLetra = new Array('T','R','W','A','G','M','Y','F','P','D','X','B','N','J','Z','S','Q','V','H','L','C','K','E');

	if (strNif.length < 9) return false;
	
	
	var n = strNif.substring(0, 8);
	if (Number(n) == NaN) return false;
		 
	var l = strNif.substring(8, 9);
	var letra = l.charCodeAt(0);
	if (!(letra >= 65 && letra <= 90)) return false;
	var position =Number(n)%23;
	if (arrLetra[position] != l) return false;
	
	return true;
}

function LongMax(strField, nLength)
{
	if (strField.length > nLength) {
		return false;
	} else
		return true;
}

function LongMin(strField, nLength)
{
	if (strField.length < nLength) {
		return false;
	} else 
		return true;
}

function Rango(valor,min,min){
   var numero=parseInt(valor);
   if (!(numero>=min && numero<=min)){
		return(false);
   }
   return(true);
}

function numero(valor){
   var nume=parseInt(valor);
   if ((nume != valor)){
	return(false);
   }
   return(true);
}

function entero(valor){
   nume=parseInt(valor);
   if ((nume != valor)){
	return(false);
   }
   return(true);
}

function numeroreal(valor){
   valor = valor.replace(".","");
   valor = valor.replace(",",".");
   var nume = parseFloat(valor);
   if ((nume != valor)){
		return(false);
   }
   return(true);
}

function ErasePunto(objCampo){
   var strCadena = objCampo.value;
   objCampo.value = strCadena.replace(/\./g,"");
}

function EraseBlank(objCampo){
   var strCadena = objCampo.value;
   objCampo.value = LTrim(RTrim(strCadena));
}

function ConvToUpper(objCampo){
	objCampo.value = objCampo.value.toUpperCase();
}

function RTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var endpos = 0;
	
	if (EnBlanco(str)){
		return "";
	}
	else{
		for (i = str.length - 1; i >= 0 && endpos == 0; i = i - 1) {
			c = str.charAt(i);
			if (whitespace.indexOf(c) == -1)
				endpos = i;
		}

		return str.substring(0,endpos+1);
	}
}

function LTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var pos = 0;

	if (EnBlanco(str)){
		return "";
	}
	else{
		for (i = 0; i <= str.length - 1; i = i + 1) {
			c = str.charAt(i);
			if (whitespace.indexOf(c) == -1){
				pos = i;
				break;
			}
		}
		return str.substring(pos,str.length);
	}
}

function Trim(strTrim){
	var str = new String(strTrim);
	if(EnBlanco(str))	return "";
	else{
		strTrim = LTrim(strTrim);
		strTrim = RTrim(strTrim);
		return strTrim;
	}
}
//comprueba un fichero o una imagen obligatorio que se introduzca, y su formato
//obligatorio	1 si, 0 no
//opcion		1 alta, 2 modificacion
//num			el número de imagen que es
//nombre		el nombre del elemento
//formato		1 .gif o .jpg o .jpeg, 2 .jpg o .jpeg, 3 .doc,.pdf, .txt
//formu			el formulario
function Fichero(obligatorio,opcion,num,nombre,formato,formu){
	var fich = eval("formu.ImagenF"+num);
	var msg = "";
	if (obligatorio==1){
		var fich1 = eval("formu.Imagen"+num+"[2]");
		var fich2 = eval("formu.Imagen"+num+"[1]");
		var msg="";
		if ((opcion==1) || (!fich1)){
			if (EnBlanco(fich.value)){
				msg="- Debe introducir " + nombre + "\n";
			}
		}
		else{
			if(fich1.checked){
				msg="- No puede eliminar " + nombre + "\n";
			}
			else{
				if ((fich2.checked) && EnBlanco(fich.value)){
					msg="- Debe introducir " + nombre + "\n";
				}
			}
		}
	}
	else{
		//comprobamos que meta algo si le da a modificar
		if(opcion==2){
			var fich2 = eval("formu.Imagen"+num+"[1]");
			if(fich2!=undefined){
				if ((fich2.checked) && EnBlanco(fich.value)){
					msg="- Debe introducir " + nombre + "\n";
				}
			}	
		}
	}
	//ahora comprobamos el formato
	if(!EnBlanco(fich.value)){
		switch (formato){
			case 1:
				if(fich.value.substr(fich.value.length-4,4)!=".jpg" && fich.value.substr(fich.value.length-4,4)!=".gif" && fich.value.substr(fich.value.length-5,5)!=".jpeg"){
					msg+="- El formato de " + nombre + " debe ser .jpg, .gif o .jpeg\n";
				}
				break;
			case 2:
				if(fich.value.substr(fich.value.length-4,4)!=".jpg" && fich.value.substr(fich.value.length-5,5)!=".jpeg"){
					msg+="- El formato de " + nombre + " debe ser .jpg o .jpeg\n";
				}
				break;
			case 3:
				if(fich.value.substr(fich.value.length-4,4)!=".doc" && fich.value.substr(fich.value.length-4,4)!=".pdf" && fich.value.substr(fich.value.length-4,4)!=".txt"){
					msg+="- El formato de " + nombre + " debe ser .doc, .pdf o .txt \n";
				}
				break;
		}
	}
	return msg;
}
//comprueba el formato
/*
function FicheroNoObligatorio(formato,fichero)
{   
	if(Vacio(fichero))
		return true;
		
	switch (formato){
		case 1:
			if(fichero.substr(fichero.length-4,4)!=".jpg" && fichero.substr(fichero.length-4,4)!=".gif" && fichero.substr(fichero.length-5,5)!=".jpeg")
				return true;
			break;
		case 2:
			if(fichero.substr(fichero.length-4,4)!=".jpg" && fichero.substr(fichero.length-5,5)!=".jpeg")
				return true;
			break;
		case 3:
			if(fichero.substr(fichero.length-4,4)!=".doc" && fichero.substr(fichero.length-4,4)!=".pdf")
				return true;
			break;
	}

	return false;
}
*/
