// JavaScript Document

/**********************************************************************/
/* Funciones genéricas auxiliares (validación de fechas, sobre todo) */ 
/*********************************************************************/
/*
Función que valida un e-mail para que sea de la forma cuenta@servidor.dominio
Toma una cadena y devuelve verdadero si cumple dicho formato, falso si no
*/
function validarEmail (email) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		return false;
	}
	return true;
}

/*
Función que determina si el valor pasado es una fecha en formato dd/mm/aaaa
*/
function esFechaCorta (str) {	
	Str = new String(str);
	// Longitud debe ser exactamente 10
	if (Str.length != 10) return false;
	var dia = Str.substr(0,2);
	var mes = Str.substr(3,2);
	var ano = Str.substr(6,4);
	if (isNaN(dia) || isNaN(mes) || isNaN(ano)) return false;
	if ((Str.substr(2,1) != "/") || (Str.substr(5,1) != "/")) return false;
	
	return isDate(mes + "/" + dia + "/" + ano);
}

/**************************************************************************************/
/*** 	DE AQUÍ PARA ABAJO, Funciones "prestadas" para la validación de fechas		***/
/**************************************************************************************/

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("El formato de fecha válido es: dd/mm/aaaa")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Introduzca un mes comprendido entre 01 y 12")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Día no válido")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Introduzca un año de 4 dígitos comprendido entre "+minYear+" y "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Introduzca una fecha válida")
		return false
	}
return true
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
 
/* Función que devuelve el atributo de 'display' (atributo CSS a su vez) para mostrar una <tr> */
function mostrarPwd () {
	if (navigator.appName && navigator.appName.indexOf('Microsoft')==-1) 
		return 'table-row';
	else
		return 'block';
}

/* Función para que no se pueda marcar el nombre de la provincia en el combo de comarcas */
function desmarcaProvincias (obj, provincias) {
	var opciones=obj.options;
	var hayprov=false;
	var ido=new String();
	
	for (i=0;i<opciones.length;i++) {
		ido=String(opciones[i].value);
		if (opciones[i].selected && (ido.indexOf("p")!=-1)) {
			opciones[i].selected=false;
			/*
			//Modificación by Rafa: que se marque la provincia que se selecciono en comarcas en provincias
			laprov=ido.substr(1,ido.length-1);
			for (j=0; j<provincias.options.length; j++) {
				if (provincias.options[j].value==laprov) {
					provincias.options[j].selected=true;
				}
			}
			*/
			hayprov=true;
		}
	}
/*	
	//Que no se pueda seleccionar una provincia si se ha seleccionado una comarca
	provincias.disabled=!hayprov;
	// Quitar las provincias que se hubieran podido seleccionar
	for (i=0;i<provincias.options.length;i++) {
		provincias.options[i].selected=false;
	}
*/
	return true;
}