var returnDate = '';
var currentlayer = '';

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
 
 

function showExtraText(layerid) {
	if(currentlayer!= '') {
		a = MM_findObj(currentlayer);	
		if(a!= null) {
			a.style.display="none";
		}
		
	}	
	a = MM_findObj(layerid);	
	if(a!= null) {
		a.style.display="block";
	}
	currentlayer = layerid;
	return false;
}

function showCal(returnFields) {
	returnDate = returnFields;
	var a = MM_findObj('calendar');
	if(a!== null) {
		a.style.display="block";
		a.style.position="absolute";
		a.style.zIndex = "2";
		a.style.left= (tempX -280) + "px";
		if(IE) {
			a.style.top= (tempY + 40) + "px";
		} else {
			a.style.top= (tempY + 10) + "px";
			
		}
	}
	return false;
}

function showWait() {
	var a = MM_findObj('wait');
	if(a!== null) {
		a.style.display="block";
		a.style.position="absolute";
		a.style.zIndex = "2";
		leftpos = window.screen.width/2 - 150;
		toppos = window.screen.height/2 - 100;
		//alert (leftpos + " top: " + toppos);
		a.style.left= (leftpos  ) + "px";
		a.style.top= (toppos ) + "px";
		
	} 
}

//============================================================
//Capturing The Mouse Position in IE4-6 & NS4-6
//(C) 2000 www.CodeLifter.com
//Free for all users, but leave in this  header

var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}   

  return true
}


myDate = new Date(); // prende la data di oggi

var giorniMesi = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // array coi giorni dei mesi
var nomeMesi = new Array ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); 
meseCorrente = new Date(myDate.getFullYear(), myDate.getMonth(),1); // setta la data al primo del mese corrente
var questoMese = false; // verifica se si sta scegliendo il mese corrente
var primaDataUtile; // se questoMese  true,  la prima data utile per una prenotazione

function chiudiCalendario() {
	var a = MM_findObj("calendar");
	 if (a!= null) {
	 	a.style.display = "none";
	 }
	 return false;
}
function setDataCalendario (anno, mese, giorno, dataAzione){ 
	
	if(dataAzione == 'doa') {
		for(i=0; i<document.resrvForm.doa_yy.options.length; i++) {
			if (document.resrvForm.doa_yy.options[i].value == anno) {
				document.resrvForm.doa_yy.options.selectedIndex = i;
			}
		} 
		document.resrvForm.doa_mm.options.selectedIndex = mese;
		document.resrvForm.doa_dd.options.selectedIndex = giorno;
		
	} else {
		for(i=0; i<document.resrvForm.dod_yy.options.length; i++) {
			if (document.resrvForm.dod_yy.options[i].value == anno) {
				document.resrvForm.dod_yy.options.selectedIndex = i;
			}
		} 
		document.resrvForm.dod_mm.options.selectedIndex = mese;
		document.resrvForm.dod_dd.options.selectedIndex = giorno;
		
	}
	 
	 var a = MM_findObj("calendar");
	 if (a!= null) {
	 	a.style.display = "none";
	 }
	
	 
}

function setExtraData(mycurrency, myrooms, myadults, mychildren, myfacility, myfacility1){
		document.resrvForm.currency.options.selectedIndex = mycurrency;
		document.resrvForm.rooms.options.selectedIndex = myrooms;
		document.resrvForm.adults.options.selectedIndex = myadults;
		document.resrvForm.children.options.selectedIndex = mychildren;
		if(document.resrvForm.facilities && document.resrvForm.facilities1) {
			for(i=0; i<document.resrvForm.facilities.options.length; i++) {
				if (document.resrvForm.facilities.options[i].value == myfacility) {
					document.resrvForm.facilities.options.selectedIndex = i;
				}
			} 
			for(i=0; i<document.resrvForm.facilities1.options.length; i++) {
				if (document.resrvForm.facilities1.options[i].value == myfacility1) {
					document.resrvForm.facilities1.options.selectedIndex = i;
				}
			} 
		}
		
		 
		 
		
}

function aggiorna (giorno) {
	var pattern = /[\d]+/;
	if (pattern.test(giorno)) {
		if (questoMese == false) {
			setDataCalendario(meseCorrente.getFullYear(), meseCorrente.getMonth(), giorno -1, returnDate);
			 
		} else if (giorno >= primaDataUtile) {
			setDataCalendario(meseCorrente.getFullYear(), meseCorrente.getMonth(), giorno -1, returnDate);
			  
		}
		if(returnDate == 'doa') {
			fixDates('dod'); 
		 } else {
			 fixDates('doa'); 
		 }
	}
}
function check_bisestile (anno) {
	if(anno % 4 == 0) {
		return true;
	} else {
		return false;
	}
}

function riempiCalendario (data) {
	a= window.document.forms['calendario']; // prende un riferimento al form

	var counter = 1;
	var giorniTotaliMese = giorniMesi[data.getMonth()];
	
	var z = data.getFullYear();
	var bisestile = check_bisestile (z) ;
	
	if (data.getMonth() == myDate.getMonth() && data.getFullYear() == myDate.getFullYear()) {
		
		questoMese = true;
		primaDataUtile = parseInt(myDate.getDate()) + 1;
	}	 else {
		questoMese = false;
		primaDataUtile = "";
	}
	 
	if (data.getMonth() == 1 && bisestile == true) {
		giorniTotaliMese = 29;
	}


	a.mese.value = nomeMesi[data.getMonth()] + " " + data.getFullYear();
	for (i=0 ; i< 42; i++) {
		if (i==0 || i%7==0) {
			a["b" + i].style.color="black";
			a["b" + i].style.backgroundColor="white";
			a["b" + i].style.cursor="pointer";
		} else {
			a["b" + i].style.color="#991f00";
			a["b" + i].style.backgroundColor="#E2E2E2";
			a["b" + i].style.cursor="pointer";
		}
		if (i >= data.getDay() && counter <= giorniTotaliMese) {
			
				a["b" + i].value=counter;
				if(questoMese == true && counter < primaDataUtile) {
					a["b" + i].style.backgroundColor="#f4efec";
					a["b" + i].style.color="#CCCCCC";
					a["b" + i].style.cursor="default";
				} 
			
			
			counter++;
		} else {
			a["b" + i].value=" ";
			
		}
	}
}

function avantiMese () {
	var a = meseCorrente.getMonth()+1;
	if (meseCorrente.getTime()<myDate.getTime()+63072000000) {
		meseCorrente.setMonth(a);
		riempiCalendario(meseCorrente);
	} else {
		//alert ("");
	}
}
function indietroMese () {
	var a = meseCorrente.getMonth()-1;
	if (meseCorrente.getTime()>=myDate.getTime()) {
		meseCorrente.setMonth(a);
		riempiCalendario(meseCorrente);
	} 
}
function check_search_form(city, f) { 
	error = false;
	countrycityerror = false;
	arrivalerror = false;
	departureerror = false;
	
	giornocheck = parseInt(f.doa_dd.options[f.doa_dd.selectedIndex].value) ;
	mesecheck =    parseInt(f.doa_mm.options[f.doa_mm.selectedIndex].value) ;
	annocheck = parseInt( f.doa_yy.options[f.doa_yy.selectedIndex].value) ;
	
	arrivalerror = check_search_date(giornocheck, mesecheck, annocheck);

	giornocheck = parseInt(f.dod_dd.options[f.dod_dd.selectedIndex].value) ;
	mesecheck =    parseInt(f.dod_mm.options[f.dod_mm.selectedIndex].value) ;
	annocheck = parseInt( f.dod_yy.options[f.dod_yy.selectedIndex].value) ;
	
	departureerror = check_search_date(giornocheck, mesecheck, annocheck);
	
	if(arrivalerror==true || departureerror == true) {
		error = true;
	}
		
	if(city == true) { // solo per le ricerche con stato/citta'
		if(f.country.options[f.country.selectedIndex].value==0) {
			error = true;	
			countrycityerror = true;
		}
		if(f.city.options[f.city.selectedIndex].value==0) {
			error = true;	
			countrycityerror = true;
		}
	} 
	if(error == true) {
		ermsg = "Please check the following fields:\r\n";
		if(countrycityerror == true) {
			ermsg += "You must select a country and a city before searching.\r\n";
		}
		if(arrivalerror == true) {
			ermsg += "The arrival date is invalid.\r\n";
		}
		if(departureerror == true) {
			ermsg += "The departure date is invalid.\r\n";
		}
		
		alert(ermsg);
		return false;
	} else {
		miogiornoa = f.doa_dd.selectedIndex;
		miomesea = f.doa_mm.selectedIndex;
		mioannoa = f.doa_yy.selectedIndex;
		miogiornod = f.dod_dd.selectedIndex;
		miomesed = f.dod_mm.selectedIndex;
		mioannod = f.dod_yy.selectedIndex;
		currency = f.currency.selectedIndex;
		rooms = f.rooms.selectedIndex;
		adults = f.adults.selectedIndex;
		children = f.children.selectedIndex; 
		if(f.facilities&& f.facilities1) {
			facilities = f.facilities.options[f.facilities.selectedIndex].value ;
			facilities1 = f.facilities1.options[f.facilities1.selectedIndex].value ;
		} else {
			facilities = 0;
			facilities1 = 0;
		}
		
		setCalCookie(miogiornoa, miomesea, mioannoa, miogiornod, miomesed, mioannod, currency, rooms, adults, children, facilities, facilities1 );
		showWait();
		return true;	
	}
	
}

function setCalCookie(giornoa, mesea, annoa, giornod, mesed, annod, currency) {
	mycookie = giornoa+"|"+mesea+"|"+annoa+"|"+giornod+"|"+mesed+"|"+annod+"|"+currency+"|"+ rooms+"|"+adults+"|"+children+"|"+facilities+"|"+facilities1 ;
	document.cookie="dataCal="+mycookie;
	
}
function readCalCookie() {
	var allcookies = document.cookie;
	var pos = allcookies.indexOf("dataCal");
	if(pos!= -1) {
		var start = pos +8;
		var end = allcookies.indexOf(";", start);
		if(end == -1) end = allcookies.length;
		var value = allcookies.substring(start,end);
		var data = value.split('|');
		return data;
	} else {
		return false;
	}
}

function check_search_date(giornocheck, mesecheck, annocheck){
	//alert ("checking: " + giornocheck + " " + mesecheck+ " " +  annocheck);
	giornimesi = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	leapyear = 2008;
	error = false;
	if(mesecheck == 2 && annocheck == leapyear) {
		if(giornocheck>29) {
			
			error =true;
		}
	} else {
		if(giornocheck>giornimesi[mesecheck-1]) {
			
			error = true;
		}
	}
	if(error == false) {
		date = new Date(); 
		annoadesso = date.getFullYear();
		if(annoadesso > annocheck) {
			
			error = true;
		} else if (annoadesso == annocheck) {
			meseadesso = date.getMonth();
			giornoadesso = date.getDate();
			if (mesecheck < meseadesso) {
				error = true;
			} else if((mesecheck == meseadesso) && (giornocheck< (giornoadesso + 1))) {
				error = true;
			}
		}
		
	}
	return error;
	
}


function ApriPopup(URL,nome,proprieta) {
  finestra = window.open(URL,nome,proprieta);
  finestra.focus();
  return false;
}

function inizializzaData() {
	var data = readCalCookie();
	if (data != false) {
		myyear = data['2'];
		mymonth = data['1'];
		myday = data['0'];
		myyeard = data['5'];
		mymonthd = data['4'];
		mydayd = data['3'];
		mycurrency = data['6'];
		myrooms = data['7'];
		myadults = data['8'];
		mychildren = data['9'];
		myfacility = data['10'];
		myfacility1 = data['11'];
		
		setDataCalendario (myyear, mymonth, myday, 'doa');
		setDataCalendario (myyeard, mymonthd, mydayd, 'dod');
		setExtraData(mycurrency, myrooms, myadults, mychildren, myfacility, myfacility1);
	} else {
		myyear = myDate.getFullYear();
		mymonth = myDate.getMonth();
		myday = myDate.getDate();
		
		setDataCalendario (myyear, mymonth, myday, 'doa');
		aggiornaDataPartenza(myyear, mymonth, myday);
	}
}

function checkDateErrate() {
	if (document.resrvForm.dod_yy.options.selectedIndex < document.resrvForm.doa_yy.options.selectedIndex) {
		return false;
	} else if (document.resrvForm.dod_yy.options.selectedIndex == document.resrvForm.doa_yy.options.selectedIndex) {
		if (document.resrvForm.dod_mm.options.selectedIndex < document.resrvForm.doa_mm.options.selectedIndex) {
			return false;
		} else if (document.resrvForm.dod_mm.options.selectedIndex == document.resrvForm.doa_mm.options.selectedIndex) {
			if(document.resrvForm.dod_dd.options.selectedIndex <= document.resrvForm.doa_dd.options.selectedIndex) {
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function aggiornaDataPartenza(anno, mese, giorno) {
	var check = checkDateErrate();
	if(check == false) {
		var myday = giorno + 1;
		var maxdays = (giorniMesi[mese]-1);
		if (mese == 1) {
			var bisestile = check_bisestile(anno);
			if (bisestile == true) {
					maxdays = 28;
			}
		} 
		if (myday > maxdays) {
			myday = 0;
			var mymonth = mese +1;
			if(mymonth>11) {
				mymonth = 0;
				var myyear = parseInt(anno) +1;
			} else {
				var myyear = anno;
			}
		} else {
			var mymonth = mese;
			var myyear = anno;
		}
		setDataCalendario (myyear, mymonth, myday, 'dod');
	}
	
}

function aggiornaDataArrivo(anno, mese, giorno) {
	var check = checkDateErrate();
	if(check == false) {
		var myday = giorno - 1;
		if(myday < 0) {
			if (mese == 0) {
				var myyear = parseInt(anno)  -1;
				var mymonth = 11;
				myday = 30;
			} else {
				var mymonth = parseInt(mese)-1;
				myday = (giorniMesi[mymonth]-1);
				var myyear = anno;
			}
		} else {
			var mymonth = mese;
			var myyear = anno;
		}
		
		setDataCalendario (myyear, mymonth, myday, 'doa');
	}
	
}

function fixDates(checkdate) {
	var check = checkDateErrate();
	if(check == false) {
		if(checkdate == 'dod') {
			aggiornaDataPartenza (document.resrvForm.doa_yy.value, document.resrvForm.doa_mm.options.selectedIndex, document.resrvForm.doa_dd.options.selectedIndex);
		} else {
			aggiornaDataArrivo (document.resrvForm.dod_yy.value, document.resrvForm.dod_mm.options.selectedIndex, document.resrvForm.dod_dd.options.selectedIndex);
		}
	}
}

function populateCountries() {
	var pm_2 = document.forms["resrvForm"];
	if (selectedcountry!= "") {
			pm_2.country.options.length=0;
			for (var i=0; i<selection.length ; i+=3) {
				if (selection[i] == selectedcountry) {
					var cala = new Option (selection[i+1],i+2); 
					pm_2.country.options[pm_2.country.options.length] = cala;
					populateCities (i+2);
					var cala = new Option ("--- ", "0");
					pm_2.country.options[pm_2.country.options.length] = cala;
				}
			}
			if(pm_2.country.options.length==0) {
				var cala = new Option ("select a country", "0");
				pm_2.country.options[pm_2.country.options.length] = cala;
			}
			
	}
	for (i=0; i<selection.length ; i+=3) {
		var cale = new Option (selection[i+1],i+2); 
		pm_2.country.options[pm_2.country.options.length] = cale;
	
	} 
}



function populateCities(j) {
	var pm_2 = document.forms["resrvForm"];
	
	if (j!="0") {
		
		pm_2.city.options.length = 0;
		if (selectedcity != "") {
			var calf = new Option (city_name, selectedcity);
			pm_2.city.options[pm_2.city.options.length] = calf;
			var calg = new Option ("--- ", "0");
			pm_2.city.options[pm_2.city.options.length] = calg;
			selectedcity="";
			
		}
		for (var k = 0; k<selection[j].length; k+=3) {
			
			var calh = new Option (selection[j][k+1],selection[j][k+0]);
			pm_2.city.options[pm_2.city.options.length] = calh;
			
		}
	}
	
} 

function closeDiv(divname){
	mydiv = MM_findObj(divname);
	if (mydiv != null){
		mydiv.style.display="none";
	}
	return false;
}



