var Offerte = new Array();
var Hotel = new Array();
var listAPT = ['Altopiano della Paganella','Garda Trentino','Madonna di Campiglio','Pinzolo','San Martino, Vanoi, Lagorai','Trento, Monte Bondone, Valle dei Laghi','Val di Fassa','Val di Fiemme','Val di Ledro','Val di Non','Val di Sole','Valsugana']
var listAPTHotel = ['Garda Trentino, Valle di Ledro','M. di Campiglio, Pinzolo e Val Rendena','Paganella, Andalo, Molveno e Dolomiti di Brenta','S. Martino di Castrozza, Valli del Primiero e Vanoi',"Trento, Monte Bondone e Valle dell'Adige",'Val di Fassa','Val di Fiemme','Val di Non','Val di Sole, Pejo e Rabbi','Valsugana, Lagorai, Vigolana e Valle dei Mocheni','Piné e Cembra']
var hotelInAPT = new Array();
var offerteInAPT = new Array();
var servizi = '';

Event.observe(window,'load',start);
function start(){
	//setto le date di default
	caricaDate();

	if ($('Offerte')){
		$('Offerte').style.display = 'none';
		getArrOfferte();
	}
	
	if ($('Hotel')){
		$('Hotel').style.display = 'none';
		getArrHotel();
	}
}

function randOrd(){
	return (Math.round(Math.random())-0.5);
}

function closeThePopup(myDiv)
{
	if($(myDiv)){
		$(myDiv).style.display = 'none';
	}
}

function showThePopup(myDiv)
{
	if($(myDiv)){
		var allPopups = document.getElementsByClassName('boxPop');
		for(i=0;i<allPopups.length;i++){
			allPopups[i].style.display = 'none';
		}
		$(myDiv).style.display = 'block';
	}
}

///////////////// OFFERTE
function getArrOfferte(){
	var myContainer = $('Offerte');
	var myChild = myContainer.down('div.Offerta');
	var i=0;
	while (myChild){
			Offerte[i] = new Array();
			Offerte[i][0] = myChild.id;										//id offerta
			Offerte[i][1] = myChild.down('div.TitoloOfferta').innerHTML;	//titolo
			Offerte[i][2] = myChild.down('div.ValleOfferta').innerHTML;		//valle
			i++;
			myChild = myChild.next();
	}
	ordOfferteForAPT();
}


function ordOfferteForAPT(){
	// Ottengo una matrice in cui ogni riga è popolata con tutte le offerte di 1 APT
	listAPT.sort(randOrd);
	for(i=0;i<listAPT.length;i++){
		offerteInAPT[i] = Offerte.select(function(Sogg) {return Sogg[2] == listAPT[i]});
		offerteInAPT[i].sort(randOrd);
	}

	//prendo numero colonne e numero righe dell'array hotelInAPT
	var colsLength = 0;
	var rowsLength = 0;
	rowsLength = offerteInAPT.length;
	for (j=0;j<offerteInAPT.length;j++){
			if (offerteInAPT[j].length>colsLength){
				colsLength = offerteInAPT[j].length;
			}
	}

	//scorro l'array per RIGA puntando ad ogni giro alla COLONNA successiva	
	for (a=0;a<colsLength;a++){
		for (b=0;b<rowsLength;b++){
			if(offerteInAPT[b][a]){
				offerteList(offerteInAPT[b][a][0]);
			}
		}	
	}
	$('Offerte').style.display = 'block';
}


function offerteList(myDiv){
	new Insertion.Bottom($('Offerte'),$(myDiv));
}
///////////////// END OFFERTE

///////////////// HOTELS
function getArrHotel(){
	var myContainer = $('Hotel');
	var myChild = myContainer.down('div.Hotel');
	var i=0;
	while (myChild){
			Hotel[i] = new Array();
			Hotel[i][0] = myChild.id;										//id hotel
			Hotel[i][1] = myChild.down('div.TitoloHotel').innerHTML;		//titolo
			Hotel[i][2] = myChild.down('div.ValleHotel').innerHTML;			//valle
			i++;
			myChild = myChild.next();
	}
	ordHotelForAPT();
}

function ordHotelForAPT(){
	// Ottengo una matrice in cui ogni riga è popolata con tutti gli hotel di 1 APT
	listAPTHotel.sort(randOrd);
	for(i=0;i<listAPTHotel.length;i++){
		hotelInAPT[i] = Hotel.select(function(Sogg) {return Sogg[2] == listAPTHotel[i]});
		hotelInAPT[i].sort(randOrd);
	}

	//prendo numero colonne e numero righe dell'array hotelInAPT
	var colsLength = 0;
	var rowsLength = 0;
	rowsLength = hotelInAPT.length;
	for (j=0;j<hotelInAPT.length;j++){
			if (hotelInAPT[j].length>colsLength){
				colsLength = hotelInAPT[j].length;
			}
	}

	//scorro l'array per RIGA puntando ad ogni giro alla COLONNA successiva	
	for (a=0;a<colsLength;a++){
		for (b=0;b<rowsLength;b++){
			if(hotelInAPT[b][a]){
				hotelList(hotelInAPT[b][a][0]);
			}
		}	
	}
	$('Hotel').style.display = 'block';
}

function hotelList(myDiv){
	new Insertion.Bottom($('Hotel'),$(myDiv));
}
///////////////// END HOTELS



function caricaDate()
{

	var data1 = document.getElementById("mRic").Data.value;
	var data2 = document.getElementById("mRic").DataAl.value;	
	var giorno;
	
	var my_date = new Date();
	my_date.setDate(my_date.getDate()+1);

	giorno = my_date.getDate();
	if(giorno < 10)
	{
		giorno = '0' + giorno;
	} 
	mese = (my_date.getMonth()+1);
	if(mese < 10)
	{
		mese = '0' + mese;
	}		
	document.getElementById("mRic").Data.value = giorno +'/'+ mese +'/'+ my_date.getFullYear(); 
	
	
	my_date.setDate(my_date.getDate()+7);
	giorno = my_date.getDate();
	if(giorno < 10)
	{
		giorno = '0' + giorno;
	} 
	mese = (my_date.getMonth()+1);
	if(mese < 10)
	{
		mese = '0' + mese;
	}	
	document.getElementById("mRic").DataAl.value = giorno +'/'+ mese +'/'+ my_date.getFullYear(); 		

}



///////////////////////////////////////////////////////////////
///////////////////////// FORM ////////////////////////////////
function fSubmitRic(){
	var objForm
	var StrParTypeStr
	var Url
	var check
	var idClubVal
	var giorniDiff
	objForm = document.mRic;
	
	check = confronta_data();
	if (check){
		StrParTypeStr = objForm.ParTypeStr[objForm.ParTypeStr.selectedIndex].value;
		if(StrParTypeStr == '0')
		{
			alert('Wählen Sie das Unterkunftstyp aus');
			objForm.ParTypeStr.focus();
		}
		else
		{
			var Arrivo= $('Data').value;
			if ((Arrivo) == ''){
				alert('Anreisedatum eingeben');
				return
			}
			var Partenza = $('DataAl').value;
			if ((Partenza) == ''){
				alert('Abreisedatum eingeben');
				return
			}
			var Localita = $('AreaTuristica').value;
			var Persone = $('Persone').value;
			if ((Persone) == ''){
				Persone = 2;
			}
			var Alloggio = $('n_alloggio').value;
			if(Alloggio == ''){
				Alloggio = 1;
			}
			
			giorniDiff = $('diff_Date').value
		    annoP = parseInt(Partenza.substr(6),10);
		    meseP = parseInt(Partenza.substr(3, 2),10);
		    giornoP = parseInt(Partenza.substr(0, 2),10);
		    annoA = parseInt(Arrivo.substr(6),10);
		    meseA = parseInt(Arrivo.substr(3, 2),10);
		    giornoA = parseInt(Arrivo.substr(0, 2),10);
		    document.location = 'http://www.tiscover.com/it/scout/stps/1449de,it,FECO/objectId,RGN27it,ref,0,ffs,2/result.html?on=RGN27it&curr=EUR&lm=false&s=0&ap=1&modulId=guide&c0=0&SPECIALHOTEL=0&HOTELCOOP=0&bd=&clk=http://www.visittrentino.it/it/Hotel_FestivalEconomia&r='+Localita+'&sd_D='+giornoA+'&sd_M='+(meseA-1)+'&sd_Y='+annoA+'&d='+giorniDiff+'&a0=2&r0=1&sr0='+StrParTypeStr+''+servizi+'&ac=&DESTLINK=search','Prenota','width=600,height=500 ,toolbar=yes, location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes0&dispInfo=2';
			//window.open('http://www.tiscover.it/it/scout/stps/85202sy,de,FECO/objectId,RGN27it,ref,0/result.html?on=RGN27it&curr=EUR&lm=false&s=0&ap=1&modulId=guide&c0=0&SPECIALHOTEL=0&HOTELCOOP=0&bd=0&dispInfo=2&clk=http%3A%2F%2Fwww.visittrentino.it&r='+Localita+'&sd_D='+giornoA+'&sd_M='+(meseA-1)+'&sd_Y='+annoA+'&d='+giorniDiff+'&a0=2&r0=1&sr0='+StrParTypeStr+''+servizi+'&ac=&DESTLINK=search','Prenota','width=600,height=500 ,toolbar=yes, location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
		}
	}
}

function controlloServizi(){

	var myServizi = document.getElementById("mRic").mf;
	servizi = '';

	for (i=0;i<myServizi.length;i++){
		if (myServizi[i].checked){
			servizi = servizi + '&mf=' + myServizi[i].value;
		}

	}

}


function controllo_data(stringa){
	var espressione = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
	if (!espressione.test(stringa))    
	{
		return false;    
	}
	else
	{
	    anno = parseInt(stringa.substr(6),10);
	    mese = parseInt(stringa.substr(3, 2),10);
	    giorno = parseInt(stringa.substr(0, 2),10);
	    var data=new Date(anno, mese-1, giorno);
	    if(data.getFullYear()==anno && data.getMonth()+1==mese && data.getDate()==giorno)
	    {
	    	return true;
	    }
	    else
	    {
	    	return false;
	    }
	}
}


function controllaData(strData){
	if(!controllo_data(strData)){
		alert("Datumsformat Tag/Monat/Jahr"); 
	}
}



function confronta_data(){
	var data1 = $F('Data');
	var data2 = $F('DataAl');
	
	var check = true;
	
	if(data1 != "" && data2 != "")
	{
	
		// controllo validità formato data
		if(controllo_data(data1) && controllo_data(data2)){
		//trasformo le date nel formato aaaammgg (es. 20081103)
			data1str = data1.substr(6)+data1.substr(3, 2)+data1.substr(0, 2);
			data2str = data2.substr(6)+data2.substr(3, 2)+data2.substr(0, 2);
	
			//controllo se la seconda data è successiva alla prima        
			if (data2str-data1str<0) {            
				alert("Das Ankunftsdatum muss vor dem Abreisedatum liegen"); 
				check = false;
				return check;       
			}
			else{			
				var one_day=1000*60*60*24;
				var data1Bis = new Date(data1.substr(6),data1.substr(3, 2)-1,data1.substr(0, 2));
				var data2Bis = new Date(data2.substr(6),data2.substr(3, 2)-1,data2.substr(0, 2));
				var timediff = Math.ceil((data2Bis.getTime()-data1Bis.getTime())/(one_day));	
				$('diff_Date').value = timediff;
			}
		}
		else{        
			alert("Datumsformat Tag/Monat/Jahr");    
			check = false;
		}
		
		return check;
	}else{
		return check;
	}
	

}
///////////////////// FINE FORM ///////////////////////////////
///////////////////////////////////////////////////////////////
