
// whitespace characters
var whitespace = " \t\n\r";
var defaultEmptyOK = false;

function Vacio(s)
{   return ((s == null) || (s.length == 0))
}


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) {
alert("La fecha no está en un formato válido.")
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
}
//Comprobamos los ficheros
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){
			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)){
		var strVariable = (fich.value.substr(fich.value.length-4,4));
		strVariable = strVariable.toLowerCase(); 
		var strVariable1 = (fich.value.substr(fich.value.length-5,5));
		strVariable1 = strVariable1.toLowerCase(); 
		switch (formato){
			case 1:
				if (strVariable!=".jpg" && strVariable!=".gif" && strVariable1!=".jpeg"){
					msg+="- El formato de " + nombre + " debe ser .jpg .gif o .jpeg\n";
				}
				break;
			case 2:
				if (strVariable!=".jpg" && strVariable1!=".jpeg"){
					msg+="- El formato de " + nombre + " debe ser .jpg o .jpeg\n";
				}
				break;
			case 3:
				if (strVariable!=".doc" && strVariable!=".pdf" && strVariable!=".txt"){
					msg+="- El formato de " + nombre + " debe ser .doc, .pdf o .txt \n";
				}
				break;
		}
	}
	return msg;
}

/****************************************************************/

// 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;
}

/****************************************************************/

// 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 LongMax(objField, nLength, strWarning)
{
	var strField = new String(objField.value);

	if (strField.length > nLength) {
		alert(strWarning);
		return false;
	} else
		return true;
}

function digito(valor){
   numero=parseInt(valor);
   if (!(numero>=0 && numero<=9)){
	alert("Introduzca un número entre 0 y 9");
	return(false);
   }
   return(true);
}
function Rango(valor,min,min){
   numero=parseInt(valor);
   if (!(numero>=min && numero<=min)){
	alert("Introduzca un número entre " + min+ " y " + max);
	return(false);
   }
   return(true);
}
function numero(valor){
   nume=parseInt(valor);
   if ((nume != valor)){
	alert("Introduzca un número");
	return(false);
   }
   return(true);
}

function Requerido(objField, FieldName)
{
	var strField = new String(objField.value);
	if (EnBlanco(strField)) {
		alert("Debe introducir " + FieldName);
		objField.focus();
		objField.select();
		return false;
	}

	return true;
}

function SonDigitos(cadena){
	var longitud = cadena.length
	var i
	var numero
	var ok=true
	i=0
	while ((i<longitud)&& (OK=true)) {
		numero=parseInt(cadena.charAt(i));		
		if (!(numero>=0 && numero<=9)){
			ok=false	
		}   
		i++;
	}
	return(ok)
}

function Decimal(valor){
//	valor = valor.replace(",",".");
//	num = parseFloat(valor)
//	if (num!=valor){
//		return(false)
//	} else {
//		return(true)
//	}
	var num
	num=parseFloat(valor);
	
	if (isNaN(num))
	{
		return false
	}
	else
	{
		return true
	}
}

function par(valor)
{
	if (numero(valor)){
		if ((valor%2) == 0)
			return true;
		else
			return false;
	}else{
		return false;
	}
}

function numeroPositivo(valor)
{
	if (numero(valor)){
		if (valor > 0)
			return true;
		else
			return false;
	}else{
		return false;
	}
}


