
function addBookmark() {
	if (window.sidebar) // firefox
		window.sidebar.addPanel(document.title, location.href, "");
	else if( document.all )
		window.external.AddFavorite(location.href, document.title)
}
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

/* CONSTANTS */
var NR_NIGHTS	= 30;	// max number of nights that can be reserved
var NR_PERSONS	= 10;	// max number of persons
var NR_BEDROOMS	= 10;	// max number of bedrooms ('any number' + 10)

function createSelector(el, nrOptions, idx) {
	var oOption, oText;
	
	if (el) {
		for (i = idx; i < nrOptions+1; i++) {
			oOption = document.createElement("OPTION");
			oText = document.createTextNode("" + i);
			oOption.value = i;
			el.appendChild(oOption);
			oOption.appendChild(oText);
		}
	}
}

function goToPage(nr, sort) {
	var oForm = document.forms['apartmentFilterForm'];
	oForm.pageNum.value = nr;
	oForm.sort.value = sort;
	oForm.submit();
}

function init() {
	//fixDateSelectors();
	var searchBox = document.getElementById("searchbox");
	var searchBoxHidden = document.getElementById("searchbox_hidden");
	var searchBox_reurl = document.getElementById("searchbox_reurl");
	var searchBox_reurl_hidden = document.getElementById("searchbox_reurl_hidden");
	var navigationBox = document.getElementById("navigationbox");
	var navigationBoxHidden = document.getElementById("navigationbox_hidden");
	var navigation_reurl = document.getElementById("navigation_reurl");
	var navigation_reurl_hidden = document.getElementById("navigation_reurl_hidden");
	var quickJump = document.getElementById("quickjump");
	var quickJumpHidden = document.getElementById("quickjump_hidden");
	var filterbox = document.getElementById("filterbox");
	var filterboxHidden = document.getElementById("filterbox_hidden");
	
	
	var text = document.getElementById("text");
	var textHidden = document.getElementById("text_hidden");
	var sitgesFeat = document.getElementById("sitgesfeature");
	var sitgesFeatHidden = document.getElementById("sitgesfeature_hidden");
	
	if (text && textHidden)
		text.innerHTML = textHidden.innerHTML;
			
	if (searchBox && searchBoxHidden) {
		searchBox.style.backgroundColor= "#F5F5F5";
		searchBox.style.backgroundImage= "url('../images/field_bg.gif')";
		searchBox.style.backgroundRepeat= "repeat-y";
		searchBox.innerHTML = searchBoxHidden.innerHTML;
		searchBoxHidden.innerHTML='';
		}
	if (searchBox_reurl && searchBox_reurl_hidden) {
		searchBox_reurl.style.backgroundColor= "#F5F5F5";
		searchBox_reurl.style.backgroundImage= "url('../../../images/field_bg.gif')";
		searchBox_reurl.style.backgroundRepeat= "repeat-y";
		searchBox_reurl.innerHTML = searchBox_reurl_hidden.innerHTML;
		searchBox_reurl_hidden.innerHTML='';
		}
	if (navigationBox && navigationBoxHidden) {
		navigationBox.style.backgroundColor= "#F5F5F5";
		navigationBox.style.backgroundImage= "url('../images/field_bg.gif')";
		navigationBox.style.backgroundRepeat= "repeat-y";
		navigationBox.innerHTML = navigationBoxHidden.innerHTML;
		}
	if (navigation_reurl && navigation_reurl_hidden) {
		navigation_reurl.style.backgroundColor= "#F5F5F5";
		navigation_reurl.style.backgroundImage= "url('../../../images/field_bg.gif')";
		navigation_reurl.style.backgroundRepeat= "repeat-y";
		navigation_reurl.innerHTML = navigation_reurl_hidden.innerHTML;
		}
	if (quickJump && quickJumpHidden)
		quickJump.innerHTML = quickJumpHidden.innerHTML;
		
	if (sitgesFeat && sitgesFeatHidden) {
		sitgesFeat.style.backgroundColor= "#F5F5F5";
		sitgesFeat.style.backgroundImage= "url('../images/field_bg.gif')";
		sitgesFeat.style.backgroundRepeat= "repeat-y";
		sitgesFeat.innerHTML = sitgesFeatHidden.innerHTML;
		}
	if (filterbox && filterboxHidden) {
		filterbox.style.backgroundColor= "#F5F5F5";
		filterbox.style.backgroundImage= "url('../images/field_bg.gif')";
		filterbox.style.backgroundRepeat= "repeat-y";
		filterbox.innerHTML = filterboxHidden.innerHTML;
		filterboxHidden.innerHTML= "";
		}
}

function fixDateSelectors() {
	if (!document.forms['searchform']) return;
	
	var date = new Date();
	var m = date.getMonth();
	var d = date.getDate();
	var y = date.getYear();
	var i;

	// limit the day selector's length according to the number of days in the current month
	// after that, increment month by one 'coz it goes [0..11]
	for (i = 0; i < document.forms.length; i++) {
		if (document.forms[i].name == "searchform" || document.forms[i].name == "calculateform")
			initDaySelector(document.forms[i], y, m);
	}
	m++;
	
	// convert to strings
	m += "";
	d += "";
	y += "";
	
	// init "today" as the default value in the selectors
	if (d.length == 1) d = "0" + d;
	if (m.length == 1) m = "0" + m;
	for (i = 0; i < document.forms.length; i++) {
		if (document.forms[i].name == "searchform" || document.forms[i].name == "calculateform") {
			document.forms[i].day.value = d;
			document.forms[i].month.value = m;
			document.forms[i].year.value = y;
		}
	}
}

function initDaySelector(aForm, year, month) {
	var days = 32 - new Date(year, month, 32).getDate();	// the number of days in the current month
	var formEl = aForm.day;
	var oldLength = formEl.length;

	formEl.length = days;
	
	// if the new selected month has more days than the previous one, 
	// we need to fill in the missing days in the selector
	if (oldLength < days) {
		var dx = days - oldLength;
		var value = formEl.options[oldLength-1].value;
		for (var i = 0; i < dx; i++) {
			formEl.options[oldLength].value = formEl.options[oldLength].text = ++value;
			oldLength++;
		}
	}
}

function initDaySelectorNew(aForm, year, month, day, mes) {
	var days = 32 - new Date(year, month, 32).getDate();	// the number of days in the current month
	var formEl = day;
	var oldLength = formEl.length;

	formEl.length = days;
	
	// if the new selected month has more days than the previous one, 
	// we need to fill in the missing days in the selector
	if (oldLength < days) {
		var dx = days - oldLength;
		var value = formEl.options[oldLength-1].value;
		for (var i = 0; i < dx; i++) {
			formEl.options[oldLength].value = formEl.options[oldLength].text = ++value;
			oldLength++;
		}
	}
	// Provocamos que cuando cambien el mes o año inicial, se cambie el mes o año final.
	if (mes!= '0') {
		aForm.monthfin.value= mes;
	}
	aForm.yearfin.value= year;
}

// Controlamos si la fecha que nos están introduciendo es menor que el día actual
function comprobarDia(yearSel, mesSel, diaSel, yearHoy, monthHoy, dayHoy, msg) {	
	var fechaHoy= yearHoy+monthHoy+dayHoy;
	var fechaSel= yearSel+mesSel+diaSel;
	
	if (fechaHoy>fechaSel) {
		alert (msg);
		return false;
	}
	return true;
}

// Controlamos si la fecha que nos están introduciendo es menor que el día actual
function comprobarDiaNew(yearSel, mesSel, diaSel, yearSelFin, mesSelFin, diaSelFin, yearHoy, monthHoy, dayHoy, msg) {	
	var fechaHoy= yearHoy+monthHoy+dayHoy;
	var fechaSel= yearSel+mesSel+diaSel;
	var fechaSelFin= yearSelFin+mesSelFin+diaSelFin;
	
	if (fechaHoy>fechaSel) {
		alert (msg);
		return false;
	}
	if (fechaSelFin <= fechaSel) {
		alert ('Departure date must be greater than the arrival.');
		return false;
	}
	// Asignamos el número de noches
	
	
	
   //Obtiene dia, mes y año
   var fecha1 = new fecha( diaSel+'/'+mesSel+'/'+yearSel )   
   var fecha2 = new fecha( diaSelFin+'/'+mesSelFin+'/'+yearSelFin)
   
   //Obtiene objetos Date
   var miFecha1 = new Date( fecha1.anio, fecha1.mes-1, fecha1.dia, 10 )	//Month is 0-11 in JavaScript
   var miFecha2 = new Date( fecha2.anio, fecha2.mes-1, fecha2.dia, 12)	//Month is 0-11 in JavaScript

   //Resta fechas y redondea
   var diferencia = miFecha2.getTime() - miFecha1.getTime()
   var dias = Math.floor(diferencia / (1000 * 60 * 60 * 24))
   var segundos = Math.floor(diferencia / 1000)
	
	
	document.searchform.elements['nr_nights'].value = (dias);
	return true;
}


// Controlamos si la fecha que nos están introduciendo es menor que el día actual
function comprobarDiaNew2(yearSel, mesSel, diaSel, yearSelFin, mesSelFin, diaSelFin, yearHoy, monthHoy, dayHoy, msg) {	
	var fechaHoy= yearHoy+monthHoy+dayHoy;
	var fechaSel= yearSel+mesSel+diaSel;
	var fechaSelFin= yearSelFin+mesSelFin+diaSelFin;
	
	if (fechaHoy>fechaSel) {
		alert (msg);
		return false;
	}
	if (fechaSelFin <= fechaSel) {
		alert ('Departure date must be greater than the arrival.');
		return false;
	}
	// Asignamos el número de noches
	
	
	
   //Obtiene dia, mes y año
   var fecha1 = new fecha( diaSel+'/'+mesSel+'/'+yearSel )   
   var fecha2 = new fecha( diaSelFin+'/'+mesSelFin+'/'+yearSelFin)
   
   //Obtiene objetos Date
   var miFecha1 = new Date( fecha1.anio, fecha1.mes-1, fecha1.dia, 10 )	//Month is 0-11 in JavaScript
   var miFecha2 = new Date( fecha2.anio, fecha2.mes-1, fecha2.dia, 12)	//Month is 0-11 in JavaScript

   //Resta fechas y redondea
   var diferencia = miFecha2.getTime() - miFecha1.getTime()
   var dias = Math.floor(diferencia / (1000 * 60 * 60 * 24))
   var segundos = Math.floor(diferencia / 1000)
	
	
	document.calculateform.elements['nr_nights_modif'].value = (dias);
	return true;
}

function openWin(url) {
	var newWin = window.open(url, "info", "height=400,width=400,status=no,scrollbars=yes,toolbar=no,menubar=no,location=no");
	newWin.focus();
}



function fecha( cadena ) {

   //Separador para la introduccion de las fechas
   var separador = "/"

   //Separa por dia, mes y año
   if ( cadena.indexOf( separador ) != -1 ) {
        var posi1 = 0
        var posi2 = cadena.indexOf( separador, posi1 + 1 )
        var posi3 = cadena.indexOf( separador, posi2 + 1 )
        this.dia = cadena.substring( posi1, posi2 )
        this.mes = cadena.substring( posi2 + 1, posi3 )
        this.anio = cadena.substring( posi3 + 1, cadena.length )
   } else {
        this.dia = 0
        this.mes = 0
        this.anio = 0   
   }
}

function validate(f) {

	ERROR = "";
	if (f.name.value == '' || f.name.value == ' ')
		ERROR += "Please insert first name\n"; 
	if(f.country.value == '')
		ERROR += "Please select a country\n";
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email.value)))
		ERROR += "Please enter a valid email\n";
	if(f.email.value != f.reemail.value) {
		ERROR += "Please confirm your email\n";
		f.reemail.value = "";
	}
	if (f.daytime_phone.value == '')
		ERROR += "Please insert your daytime phone\n"; 
	if(ERROR == "")
			return true;
	else{
		alert(ERROR);
		return false;
	}
}

function checkBookingForm(f)
{
	ERROR = "";
	if(f.firstname.value == '' ||f.firstname.value == ' ' )
		ERROR += "Please enter your first name<br/>";
	if(f.lastname.value == '' ||f.lastname.value == ' ' )
		ERROR += "Please enter your last name<br/>";
	if(f.country.value == '')
		ERROR += "Please select a country<br/>";
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email.value)))
		ERROR += "Please enter a valid email<br/>";
	if(f.email.value != f.reemail.value) {
		ERROR += "The two mails have to be the same<br/>";
		f.reemail.value = "";
	}
    if(f.daytime_phone.value == '' ||f.daytime_phone.value == ' ' )
		ERROR += "Please enter your day time phone<br/>";
	if(f.i_agree_to_the_terms_and_conditions.checked != true)
		ERROR += "You have to agree to the terms and conditions<br/>";
	
	if(f.address.value == '' || f.address.value == ' ' )
		ERROR += "Please enter your address<br/>";
	if(f.codpos.value == '' || f.codpos.value == ' ' )
		ERROR += "Please enter your zip<br/>";
	if(f.ciudad.value == '' || f.ciudad.value == ' ' )
		ERROR += "Please enter your city<br/>";
	
	if(ERROR == "")
	{
		return true;
	}
	else{
		document.getElementById("error").innerHTML=ERROR;
		return false;
	}
}

function checkBookingFormFR(f)
{
	ERROR = "";
	if(f.firstname.value == '' ||f.firstname.value == ' ' )
		ERROR += "Saisissez votre prénom<br/>";
	if(f.lastname.value == '' ||f.lastname.value == ' ' )
		ERROR += "Saisissez votre nom<br/>";
	if(f.country.value == '')
		ERROR += "Choisissez un pays<br/>";
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email.value)))
		ERROR += "Saisissez une adresse email valide<br/>";
	if(f.email.value != f.reemail.value) {
		ERROR += "Les deux mails doivent être identiques<br/>";
		f.reemail.value = "";
	}
    if(f.daytime_phone.value == '' ||f.daytime_phone.value == ' ' )
		ERROR += "Saisissez votre numéro de téléphone<br/>";
	if(f.i_agree_to_the_terms_and_conditions.checked != true)
		ERROR += "Vous devez lire et accepter les conditions<br/>";
	
	if(f.address.value == '' ||f.address.value == ' ' )
		ERROR += "S’il vous plaît, indiquez votre adresse<br/>";
	if(f.codpos.value == '' ||f.codpos.value == ' ' )
		ERROR += "S’il vous plaît, indiquez votre code postal<br/>";
	if(f.ciudad.value == '' ||f.ciudad.value == ' ' )
		ERROR += "S’il vous plaît, indiquez votre ville<br/>";
	
	if(ERROR == "")
	{
		return true;
	}
	else{
		document.getElementById("error").innerHTML=ERROR;
		return false;
	}
}

function checkBookingFormEs(f)
{
	ERROR = "";
	if(f.firstname.value == '' ||f.firstname.value == ' ' )
		ERROR += "Llena su nombre<br/>";
	if(f.lastname.value == '' ||f.lastname.value == ' ' )
		ERROR += "Llena su apellido<br/>";
	if(f.country.value == '')
		ERROR += "Elige su pa&igrave;s<br/>";
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email.value)))
		ERROR += "Favor de escoger una direccion email valida<br/>";
	if(f.email.value != f.reemail.value) {
		ERROR += "Los dos emails tienen que ser iguales<br/>";
		f.reemail.value = "";
	}
    if(f.daytime_phone.value == '' ||f.daytime_phone.value == ' ' )
		ERROR += "Llena su numero de telefono<br/>";
	if(f.i_agree_to_the_terms_and_conditions.checked != true)
		ERROR += "Tiene que leer et aceptar las condiciones<br/>";

	if(f.address.value == '' ||f.address.value == ' ' )
		ERROR += "Por favor, rellene su direccion<br/>";
	if(f.codpos.value == '' ||f.codpos.value == ' ' )
		ERROR += "Por favor, rellene su codigo postal<br/>";
	if(f.ciudad.value == '' ||f.ciudad.value == ' ' )
		ERROR += "Por favor, rellene su ciudad<br/>";
	
	if(ERROR == "")
	{
		return true;
	}
	else{
		document.getElementById("error").innerHTML=ERROR;
		return false;
	}
}

function goToApartmentFr(id, url) {
	var oForm = document.forms['searchform'];
	if (!oForm) return;
	
	oForm.elements['id'].value = id;
    if (url == "") url = "appartement";
	oForm.action = "appartement-madrid/"+id+"/"+url+".php";
	oForm.submit();
}

function goToApartmentEs(id, url) {
	var oForm = document.forms['searchform'];
	if (!oForm) return;
	
	oForm.elements['id'].value = id;
    if (url == "") url = "apartamento";
	oForm.action = "apartamentos-madrid/"+id+"/"+url+".php";
	oForm.submit();
}

function goToApartmentEn(id, url) {
	var oForm = document.forms['searchform'];
	if (!oForm) return;
	
	oForm.elements['id'].value = id;
    if (url == "") url = "apartment";
	oForm.action = "madrid-apartment/"+id+"/"+url+".php";
	oForm.submit();
}

function goToApartment(id) {
	var oForm = document.forms['searchform'];
	if (!oForm) return;
	
	oForm.elements['id'].value = id;
	oForm.action = "apartments_b.php";
	oForm.submit();
}

function displayPics()
{
	var photos = document.getElementById('galerie_mini') ;
	// On récupère l'élément ayant pour id galerie_mini
	var liens = photos.getElementsByTagName('a') ;
	// On récupère dans une variable tous les liens contenu dans galerie_mini
	var big_photo = document.getElementById('big_pict') ;
	// Ici c'est l'élément ayant pour id big_pict qui est récupéré, c'est notre photo en taille normale


	// Une boucle parcourant l'ensemble des liens contenu dans galerie_mini
	for (var i = 0 ; i < liens.length ; ++i) {
		// Au clique sur ces liens 
		liens[i].onmouseover = function() {
			big_photo.src = this.href; // On change l'attribut src de l'image en le remplaçant par la valeur du lien
			big_photo.alt = this.title; // On change son titre
			titre_photo.firstChild.nodeValue = this.title; // On change le texte de titre de la photo
			return false; // Et pour finir on inhibe l'action réelle du lien
		};
	}
}

function showdesc()
{
    document.getElementById("apartment_desc_text").style.display = "none"; 
    document.getElementById("apartment_desc_text_all").style.display = "block";     
}

function cambio_param(param1, param2, change_param3) {
	if (param1.checked) var checked= '1';
	else var checked= '0';
	
	if (param2.value != checked) {
		document.searchform.elements[change_param3.name].value = '1';
	} else 
		document.searchform.elements[change_param3.name].value = '0';
}