//CONSTANTES
LETTERS = "áéíóúÁÉÍÓÚabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
LETRAS = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ";
LETRAS_AMADEUS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
DIGITS = "0123456789";
ZIP_VALID = DIGITS;
PHONE_VALID = DIGITS + "+- ()";
PASSPORT_VALID = "!\"#$%&'()*+,-./0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
PVP = DIGITS + ".,";
MAIL_VALID = LETTERS + DIGITS + ";._@-";
SELECTED = "Xx";
INVALID_FOR_TEXT = "!·\\\"'$%&=?¿|@#`+*^[]{}´¨_<>;";
INVALID_FOR_URL = "!·\\\"'$%()¿|@#`+*^[]{}´¨<>;";
INVALID_FOR_MAIL = "!·\\\"'$%()¿|#`+*^[]{}´¨<>;";
INVALID_NAME = "!·\\\"'$%()¿|#`+*^[]{}´¨<>:áéíóúÁÉÍÓÚäëïöüÄËÏÖÜâêîôûÂÊÎÔÛ";
INVALID_DATE = LETRAS+"!·\\\"'$%()¿|#`+*^[]{}´¨<>:áéíóúÁÉÍÓÚäëïöüÄËÏÖÜâêîôûÂÊÎÔÛ";
INVALID_TIME = "&/€!·\\\"'$%()¿|#`+*^[]{}´¨<>;áéíóúÁÉÍÓÚäëïöüÄËÏÖÜâêîôûÂÊÎÔÛ ";
INVALID_AMADEUS= "áéíóúÁÉÍÓÚàèìòùÀÈÌÒÙäëïöüÄËÏÖÜâêîôûÂÊÎÔÛ`";
INVALID_NONE="";
INVALID_NUMBER = ":+-()" + LETRAS + INVALID_FOR_TEXT+"ºª€¬¡ç*/";
DATE_VALID=DIGITS + "/";
CIF_CODE = "1";
NIF_CODE = "2";

function getNextDate(date,days){
	if(date.substring(0,1) == "0"){
		day = parseInt (date.substring (1, 2));
	}else{
		day = parseInt (date.substring (0, 2));
	}
	if(date.substring(3,4) == "0"){
		month = parseInt (date.substring (4, 5)) -1;
	}else{
		month = parseInt (date.substring (3, 5))-1;
	}
	year = parseInt (date.substring (6, 10));
	actualDate = new Date(year,month,day);
	millis = actualDate.getTime()+12*60*60*1000+days*24*60*60*1000;
	actualDate.setTime(millis);
	nday = new Number(actualDate.getDate());
	nmonth = new Number(actualDate.getMonth())+1;
	nyear = new Number(actualDate.getYear());
	if(nyear < 1000){
		nyear += 1900;
	}
	if(nday < 10){
		aux = "0"+nday.toString()+"/";
	}else{
		aux = nday.toString()+"/";
	}
	if(nmonth < 10){
		aux = aux + "0"+nmonth.toString()+"/";
	}else{
		aux = aux + nmonth.toString()+"/";
	}
	aux = aux+nyear.toString();
	return aux;
}

function getDays(date1,date2){
	if (!date2) { //Si no se manda el segundo parámetro tomamos el día actual
		actualDate1 = new Date();
		actualDate1 = new Date(actualDate1.getYear(), actualDate1.getMonth(), actualDate1.getDate());
		millis1 = actualDate1.getTime();
		date2 = date1;
	} else {
		if(date1.substring (0, 1) == "0"){
			day1 = parseInt (date1.substring (1, 2));
		}else{
			day1 = parseInt (date1.substring (0, 2));
		}
		if(date1.substring (3, 4) == "0"){
			month1 = parseInt (date1.substring (4, 5))-1;
		}else{
			month1 = parseInt (date1.substring (3, 5))-1;
		}
		year1 = parseInt (date1.substring (6, 10));
		actualDate1 = new Date(year1,month1,day1);
		millis1 = actualDate1.getTime();
	}
	if(date2.substring (0, 1) == "0"){
		day2 = parseInt (date2.substring (1, 2));
	}else{
		day2 = parseInt (date2.substring (0, 2));
	}
	if(date2.substring (3, 4) == "0"){
		month2 = parseInt (date2.substring (4, 5))-1;
	}else{
		month2 = parseInt (date2.substring (3, 5))-1;
	}
	year2 = parseInt (date2.substring (6, 10));
	actualDate2 = new Date(year2,month2,day2);
	millis2 = actualDate2.getTime();
	dif = millis2-millis1;
	days = dif/(24*60*60*1000);
	var resultado = ""+days;
	if(resultado.indexOf(".")!=-1){
		var aux = resultado.substring(resultado.indexOf(".")+1,resultado.indexOf(".")+2);
		if(parseInt(aux)>5){
			days = Math.ceil(days);
		}else{
			days = Math.floor(days);
		}
	}
	return days;
	//return Math.ceil(days);
}

function validateDate (str, desc, compulsory, canBeInTheFuture, mostrar, noFechaPasada)
{
	mensaje = "La fecha debe ser de la forma\ndd/mm/aaaa";

	if (compulsory && (!isFilled (str) || str=='dd/mm/aaaa'))
	{
		        if (mostrar)
			alert (desc + " es un campo obligatorio.");
			return false;
	}
	
	if (str.length > 0)
	{
		if (!validate (str, DATE_VALID))
		{
		        if (mostrar)
			alert (desc + " no es una fecha valida.\n"+mensaje);
			return false;
		}
		if((str.length < 6) || (str.length == 7) || (str.length == 9))
		{
			if (mostrar)
				alert (desc + " debe ser de la forma ddmmaa, ddmmaaaa, dd/mm/aa o dd/mm/aaaa.\n");
			return false;
		
		}
		if(str.length == 10){
			if(str.substring(2,3) == "/" && str.substring(5,6) == "/"){
				compactedDate = str.substring (0, 2) + str.substring (3, 5) + str.substring (6, 10);
			}else{
				if (mostrar) 
					alert (desc + " no es una fecha valida.\n"+mensaje);
				return false;
			}
		}else if(str.length == 8){
			if(str.substring(2,3) == "/" && str.substring(5,6) == "/"){
				compactedDate = str.substring (0, 2) + str.substring (3, 5) + "20"+ str.substring (6, 8);
			}else{
				if (str.substring(2,3) != "/" && str.substring(5,6) != "/"){
					compactedDate = str;
				}else {
					return false;
				}
			}
		}else if(str.length == 6){
			compactedDate = str.substring (0, 4) +"20"+ str.substring (4, 6);
		}
		if (!validate (compactedDate, DIGITS))
		{
		        if (mostrar)
			alert (desc + " no es una fecha valida.\n"+mensaje);
			return false;
		}
		if(compactedDate.substring (0, 1) == "0"){
			day = parseInt (compactedDate.substring (1, 2));
		}else{
			day = parseInt (compactedDate.substring (0, 2));
		}
		if(compactedDate.substring (2, 3) == "0"){
			month = parseInt (compactedDate.substring (3, 4));
		}else{
			month = parseInt (compactedDate.substring (2, 4));
		}
		year = parseInt (compactedDate.substring (4, 8));
		REGULAR_MONTHS = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		LEAP_MONTHS = new Array (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		curDate = new Date ();

		if((month < 1) || (month > 12)){
		        if (mostrar)
			alert("Mes no valido.\nDebe estar entre 1 y 12.");
			return false;
		}
		if (!canBeInTheFuture)
		{
			curYear = curDate.getYear();
			curMonth = curDate.getMonth () + 1;
			if (curYear < year || 
			    curYear == year && curMonth < month ||
			    curYear == year && curMonth == month  && curDate.getDate()<day)
			{
	        if (mostrar) alert (desc + " es posterior a la fecha actual.");
			return false;
			}
		}
		if(noFechaPasada){
			//Si es true (o existe) no hay que permitir que la fecha sea a pasado
			curYear = curDate.getYear();
			curMonth = curDate.getMonth () + 1;
			if ((curYear > year) ||
					(curYear == year && curMonth > month) ||
					(curYear == year && curMonth == month && curDate.getDate() > day)) {
				if (mostrar) alert (desc + " es anterior a la fecha actual.");
				return false;
			}		
		}

		if (isLeapYear (year))
		{
			if (day < 1 || day > LEAP_MONTHS[month-1])
			{
        		        if (mostrar)
				alert (desc + " no es una fecha válida. 1");
				return false;
			}
		}
		else
		{
			if (day < 1 || day > REGULAR_MONTHS[month-1])
			{
        		        if (mostrar)
				alert (desc + " no es una fecha válida. 2");
				return false;
			}
		}	
	}
	return true;
}

function parseDate(str){
 var compactedDate = str;
 if(str.length == 10){
  compactedDate = str;
 } else {
   var index = str.indexOf("/");
   if (index == -1) {
     if(str.length == 8) {
       if(str.substring(2,3) == "/"){
         compactedDate = str.substring (0, 6) + "20"+ str.substring (6, 8);
       } else {
         compactedDate = str.substring (0, 2)+"/"+str.substring (2, 4)+"/" + str.substring (4, 8);
       }
     } else if(str.length == 6){
       compactedDate = str.substring (0, 2)+"/" +str.substring (2, 4)+"/" +"20"+ str.substring (4, 6);
     }
   } else {	
     index = compactedDate.indexOf("/");
     var newFecha = compactedDate.substring(0,index)+"/";
     if (index == 1) {
       newFecha = "0"+newFecha;       
     }
     compactedDate = compactedDate.substring(index+1);
     index = compactedDate.indexOf("/");
     var saux = compactedDate.substring(0,index);
     compactedDate = compactedDate.substring(index+1);
     if (index == 1) {
       newFecha = newFecha+"0"+saux;       
     } else {
       newFecha = newFecha+saux;
     }
     if (compactedDate.length == 2) {
       compactedDate = newFecha + "/20" + compactedDate;
     } else {
       compactedDate = newFecha + "/" + compactedDate;
     }
   }
 }
 return compactedDate;
}

function isFilled (str)
{
	for (c = 0; c < str.length; c++)
	{
		if (str.charAt(c) != " ")
		{
			return true;
		}
	}
	return false;
}

function validate (str, valStr)
{
	for (c = 0; c < str.length; c++)
	{
		if (valStr.indexOf (str.charAt (c)) == -1)
		return false;
	}
	return true;
}

function isLeapYear (year)
{
	return ((((year%4)==0) && !((year%100)==0)) || (((year%4)==0) && ((year%400)==0)));
}

function pop(obj,ancho,alto,parametros){
if(!parametros)parametros='scrollbars = yes';
ventana=window.open(obj.href,obj.target,'width='+ancho+',height='+alto+','+parametros);
ventana.focus();}
