// JavaScript Document
miaData = new Date();
var anni = new Array;
for (i=0; i<3; i++) {
		anni[i] = miaData.getFullYear()+i;
}


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 anno = miaData.getFullYear();
var mioAnno;
for (var i=0; i< anni.length; i++) {
	if(anni[i] == anno) { 
		mioAnno = i;
	}
}
 
var mese = miaData.getMonth(); 
 

function setData (modulo) {
	data = readCalCookie();
	if(data != false) { 
		var miomese = data[0];
		var anno = data[1]; 
		var currency = data[2];
		var tipo = data[3];
	} else { 
		var miomese = mese;
		var anno = mioAnno; 
		var currency = 0;
		var tipo = 0;
	}
	var pm_2 = document.forms[modulo];
	pm_2.mese.selectedIndex = miomese; 
	pm_2.anno.selectedIndex = anno; 
	pm_2.currency.selectedIndex = currency;
	pm_2.type.selectedIndex = tipo;
}

 

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],selection[i]); 
					pm_2.country.options[pm_2.country.options.length] = cala;
					populateCities (selectedcountry);
					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) {
		if(selection[i+1]!= '') {
			var cale = new Option (selection[i+1],selection[i]); 
			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="";
		} else {
			var calg = new Option (allcities, "0");
			pm_2.city.options[pm_2.city.options.length] = calg;
		}
		for (var i=0; i<selection.length ; i+=3) {
			if (selection[i] == j) {
				for (var k = 0; k<selection[i+2].length; k+=3) {
		
					var calh = new Option (selection[i+2][k+1],selection[i+2][k+0]);
					pm_2.city.options[pm_2.city.options.length] = calh;
		
				}
			}
		}
		
	}
	
}

function setCalCookie(mese, anno,   currency, tipo) {
	mycookie = mese+"|"+anno+"|"+currency+"|"+tipo;
	document.cookie="dataAct="+mycookie;
	
}
function readCalCookie() {
	var allcookies = document.cookie;
	var pos = allcookies.indexOf("dataAct");
	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 validateSearchTour(f, lingua) {
	if(f.country.value == "0" ) {
		switch (lingua) {
			case "fr":
			alert ("Vous n'avez pas sélectionné le pays ");
			break;
			case "it":
			alert ("Non hai selezionato un paese");
			break;
			case "de":
			alert ("Sie muessen vorher Land waehlen");
			break;
			default:
			alert ("You must select a country before searching");
			break;
		}
		
		return false;
	} else {
		if( validateDateTour(f, lingua)) {
				if(f.city.value != "0"){
					f.action = 'act_step01_city.php';
				}
				return true;
		} else {
				return false;
		}
	}
}
function validateDateTour (f, lingua) { 
	miomese = f.mese.selectedIndex;
	mioanno = f.anno.selectedIndex; 
	miecurr = f.currency.selectedIndex; 
	miotipo = f.type.selectedIndex;
	mesecheck =    parseInt(f.mese.options[f.mese.selectedIndex].value) - 1;
	annocheck = parseInt( f.anno.options[f.anno.selectedIndex].value) ;  
	error = false;  
	date = new Date(); 
	annoadesso = date.getFullYear();
	if(annoadesso > annocheck) {
		error = true;
	} else if (annoadesso == annocheck) {
		meseadesso = date.getMonth(); 
		if (mesecheck < meseadesso) {
			error = true;
		}  
	}	
	
	if(error == true) {
		switch (lingua) {
			case "fr":
			alert ("Vous avez choisi une date incorrecte");
			break;
			case "it":
			alert ("La data che hai scelto non e' valida");
			break;
			case "de":
			alert ("Das Datum ist nicht gultig");
			break;
			default:
			alert ("You have selected an invalid date");
			break;
		}
		
		return false;
	} else {
		setCalCookie(miomese, mioanno,  miecurr, miotipo);
		return true;
	}
}