// JavaScript Document
miaData = new Date();
var anni = new Array;
for (i=0; i<3; i++) {
		anni[i] = miaData.getFullYear()+i;
}
var giorni = new Array;
function populateYears(modulo) {
	var pm_2 = document.forms[modulo];
	for (i=0; i<3; i++) {
		var cald = new Option (anni[i],anni[i]+"",false, false); 
		pm_2.anno.options[pm_2.anno.options.length] = cald;
	}
}

function populateDays(modulo) {
	var pm_2 = document.forms[modulo];
	for (i=1; i<32; i++) {
		giorni[i] = i;
		var calc = new Option (giorni[i],giorni[i]+"",false, false); 
		pm_2.giorno.options[pm_2.giorno.options.length] = calc;
	}
}


var anno = miaData.getFullYear();
var mioAnno;
for (var i=0; i< anni.length; i++) {
	if(anni[i] == anno) {
		//alert (anno + " "  + anni[i]);
		mioAnno = i;
	}
}

var giorniMesi = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var mese = miaData.getMonth();
var giorno = miaData.getDate() + 2;
if (giorno > giorniMesi[mese]) {
	giorno -= giorniMesi[mese];
	mese++;
	if (mese >11) {
		mese = 0;
		mioAnno++;
	}
}

function setData (modulo) {
	data = readCalCookie();
	if(data != false) {
		var miogiorno = data[0];
		var miomese = data[1];
		var anno = data[2];
		var notti = data[3];
		var currency = data[4];
	} else {
		var anno = mioAnno;
		var miogiorno = giorno - 1; 
		var miomese = mese;
		var notti = 2;
		var currency = 0;
	}
	var pm_2 = document.forms[modulo];
	pm_2.mese.selectedIndex = miomese;
	pm_2.giorno.selectedIndex = miogiorno;
	pm_2.anno.selectedIndex = anno;
	pm_2.notti.selectedIndex = notti;
	pm_2.currency.selectedIndex = currency;
}

function setDataCalendario (anno, mese, giorno, modulo) {
	var pm_2 = document.forms[modulo];
	pm_2.mese.selectedIndex = mese;
	pm_2.giorno.selectedIndex = giorno;
	notti = pm_2.notti.selectedIndex;
	var annoCalendario;
	for (var i=0; i< anni.length; i++) {
		if(anni[i] == anno) {
			annoCalendario = i;
		}
	}
	setCalCookie(giorno, mese, annoCalendario, notti);
	pm_2.anno.selectedIndex = annoCalendario;
}

function populateCountries() {
	var pm_2 = document.forms["searchleft"];
	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["searchleft"];
	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 setCalCookie(giorno, mese, anno, notti, currency) {
	mycookie = giorno+"|"+mese+"|"+anno+"|"+notti+"|"+currency;
	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;
	}
}