// JavaScript Document

var newWindow = true;

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) { 
	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 populateCountries() {
	var pm_2 = document.forms["searchleft"];  
	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; 
		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 submitForm(f, lingua) {
	if(validateSearch(f,lingua)) {
		if(newWindow == true) {
			var site_ID = f.id_aff.value;
			var lan = f.lang.value;
			var giornocheck = parseInt(f.giorno.options[f.giorno.selectedIndex].value) ;
			var mesecheck =    parseInt(f.mese.options[f.mese.selectedIndex].value) ;
			var annocheck = parseInt( f.anno.options[f.anno.selectedIndex].value) ;
			var cityID =  parseInt( f.city.options[f.city.selectedIndex].value) ;
			var currencyID = parseInt(f.currency.options[f.currency.selectedIndex].value);
			var notti = parseInt(f.notti.options[f.notti.selectedIndex].value);
			ApriPopup('http://www.hostelspoint.com/res/'+site_ID+'/step01_city.php?giorno='+giornocheck+'&mese='+mesecheck+'&anno='+annocheck+'&city='+cityID+'&currency='+currencyID+'&notti='+notti+'&lang='+lan,'calendar','top=10,left=10,width=820,height=600,scrollbars'); 
			return false;
		} else {
			return true;
		}
	}
	return false;
	
}

function validateSearch(f, lingua) {
	if((f.country.value == "0" || f.city.value == "0") ) {
		switch (lingua) {
			case "fr":
			alert ("Vous n'avez pas sélectionné le pays et la ville");
			break;
			case "it":
			alert ("Non hai selezionato un paese e una città");
			break;
			case "de":
			alert ("Sie muessen vorher Land und Stadt waehlen");
			break;
			default:
			alert ("You must select a country and a city before searching");
			break;
		}
		
		return false;
	} else {
		return validateDate(f, lingua);
	}
}
function validateDate (f, lingua) {
	
	giornocheck = parseInt(f.giorno.options[f.giorno.selectedIndex].value) ;
	mesecheck =    parseInt(f.mese.options[f.mese.selectedIndex].value) - 1;
	annocheck = parseInt( f.anno.options[f.anno.selectedIndex].value) ;
	giornimesi = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	leapyear = 2012;
	error = false;
	if(mesecheck == 1 && annocheck == leapyear) {
		if(giornocheck>29) {error =true;}
	} else {
		if(giornocheck>giornimesi[mesecheck]) {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)) { 
				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 { 
		return true;
	}
}

function ApriPopup(URL,nome,proprieta) {
  finestra = window.open(URL,nome,proprieta);
  finestra.focus();
  return false;
}
  

