var checks=["touroperator_AL","touroperator_FR","touroperator_KA","touroperator_VI","touroperator_VO","touroperator_BR"];
var iChk=[true,true,true,true,true,true];

function heyItsMe(){
	var who=(arguments.length>0)?arguments[0]:"destinaz";
	with(document.ricercaSoluzioni){
	
	var first_destOpt=4;// posizione nell'array "destinazioneOptions" della prima stringa di configurazione checkbox
	var first_tipVacOpt=2;// posizione nell'array "tipovacanzaOptions" dell primo valore di configurazione checkbox
	
	switch(who){
	case"touroperator_AL":case"touroperator_FR":case"touroperator_KA":
	case"touroperator_VI":case"touroperator_VO":case"touroperator_BR":
		var i,y,j;
		var dest=[],tourOper=[],tipi=[];
		// trovo i tour operators selezionati e li metto in un array
		for(y=0;y<checks.length;y++)if(elements[checks[y]].checked)tourOper[tourOper.length]=y;
		
		// trovo le destinazioni in base ai TO
		for(i=1;i<destinazioneOptions.length;i++){
			found=false;
			for(y=0;y<tourOper.length;y++){
				if(destinazioneOptions[i][first_destOpt+tourOper[y]].indexOf("T")!=-1){
					dest[dest.length]=destinazioneOptions[i];
					break;
				}
			}
		}
		popola_destinazione(dest);
		
/*
		// trovo i TO disponibili nelle Dest
		TO=[];
		for(i=0;i<checks.length;i++)TO[i]=false;
		for(i=0;i<dest.length;i++){
			for(y=0;y<checks.length;y++){
				TO[y]=( TO[y] || destinazioneOptions[i][first_destOpt+y].indexOf("T")!=-1 );
			}
		}
*/
		// trovo i TO disponibili nella Dest selezionata
		TO=[];
		for(i=0;i<checks.length;i++)TO[i]=false;
		var current=getItemFromText( destinaz.options[destinaz.options.selectedIndex].text, destinazioneOptions );
		for(y=0;y<checks.length;y++){
			TO[y]=( TO[y] || current[first_destOpt+y].indexOf("T")!=-1 );
		}
		// controllo che ci sia almeno un TO
		var found=false;
		for(i=0;i<TO.length;i++)found=(found||TO[i]);
		// seleziono i TO
		for(y=0;y<TO.length;y++){
			if(found){
				if(TO[y]){
					elements[checks[y]].disabled=0;
				}else{
					elements[checks[y]].checked=0;
					elements[checks[y]].disabled=1;
				}
			}else{
				elements[checks[y]].disabled=0;
			}
		}
		
		// se esiste il campo tipoVacanza,
		if(typeof tipovacanzaOptions!="undefined"){
			var arr=[];
			var quantiTipi=destinazioneOptions[0][first_destOpt].length;
			// inizializzo array tipi
			for(i=0;i<quantiTipi;i++)tipi[i]=false;
			
			
			// valorizzo array tipi
			for(i=1;i<tipovacanzaOptions.length;i++){
				for(y=0;y<tourOper.length;y++){
					tipi[i] = ( tipi[i] || tipovacanzaOptions[i][first_tipVacOpt+tourOper[y]] );
				}
			}
			
			
 			// creo array con i tipi vacanza usati
			for(i=0;i<tipi.length;i++){
				if(tipi[i]){
					arr[arr.length]=tipovacanzaOptions[i];
				}
			}
			// popolo la select
			popola_tipoVacanza(arr);
		}
/*
OLD:
		popola_destinazione(dest);
		// se esiste il campo tipoVacanza,
		if(typeof tipovacanzaOptions!="undefined"){
			var arr=[];
			var quantiTipi=destinazioneOptions[0][first_destOpt].length;
			// inizializzo array tipi
			for(i=0;i<quantiTipi;i++)tipi[i]=false;
			// trovo i tipi nella destinazione selezionata
			var current = getItemFromText( destinaz.options[destinaz.options.selectedIndex].text , destinazioneOptions );
			// valorizzo array tipi
			for(y=0;y<tourOper.length;y++){
				for(i=0;i<quantiTipi;i++){
					tipi[i] = ( tipi[i] || current[first_destOpt+tourOper[y]].charAt(i)=="T" );
				}
			}
//			alert(tipi)
 			// creo array con i tipi vacanza usati
			for(i=0;i<tipi.length;i++){
				if(tipi[i])arr[arr.length]=tipovacanzaOptions[i+1];
			}
			// popolo la select
			popola_tipoVacanza(arr);
		}
		
		// trovo i TO disponibili nelle Dest
		TO=[];
		for(i=0;i<checks.length;i++)TO[i]=false;
		for(i=0;i<dest.length;i++){
			for(y=0;y<checks.length;y++){
				TO[y]=( TO[y] || destinazioneOptions[i][first_destOpt+y].indexOf("T")!=-1 );
			}
		}
		// controllo che ci sia almeno un TO
		var found=false;
		for(i=0;i<TO.length;i++)found=(found||TO[i]);
		for(y=0;y<TO.length;y++){
			if(found){
				if(TO[y]){
					elements[checks[y]].disabled=0;
				}else{
					elements[checks[y]].checked=0;
					elements[checks[y]].disabled=1;
				}
			}else{
				elements[checks[y]].disabled=0;
			}
		}
*/		
	break;
	case"destinaz":
		var pos=destinaz.options.selectedIndex;
		if(pos>0){
			var i,j,y,TF,index;
			// trovo la voce dell'array destinazioneOptions corrispondente
			var current = getItemFromText( destinaz.options[pos].text , destinazioneOptions );
			if(tiporicerca!="weekend"){
				for(y=0;y<checks.length;y++){
					TF=(current[first_destOpt+y].indexOf("T")!=-1)?1:0;
					if(current[1]==""){
						// abilito tutti i checkbox
						elements[checks[y]].disabled=0;
						elements[checks[y]].checked=1;
					}else{
						// abilito e disabilito i checkbox
						elements[checks[y]].disabled=!TF;
					}
				}
			}
			
			// riscorro i check, se risultano disabilitati devono essere anche non checkati
			for(y=0;y<checks.length;y++)if(elements[checks[y]].disabled) elements[checks[y]].checked=0;
			
			// se esiste il campo tipoVacanza,
			if(typeof tipovacanzaOptions!="undefined"){
				var tipi=[],arr=[];
	 			// trovo i tipi vacanza usati
				// inizializzo array tipi
				for(i=0;i<current[first_destOpt].length;i++)tipi[i]=false;
				// valorizzo array tipi
				for(y=0;y<checks.length;y++){
					if(elements[checks[y]].checked){
						for(i=0;i<current[first_destOpt].length;i++){
							tipi[i] = ( tipi[i] || current[first_destOpt+y].charAt(i)=="T" );
						}
					}
				}
	 			// creo array con i tipi vacanza usati
				for(i=0;i<tipi.length;i++){
					if(tipi[i])arr[arr.length]=tipovacanzaOptions[i+1];
				}
				// popolo la select
				popola_tipoVacanza(arr);
			}
			
			// trovo tutte le dest che abbiano i TO
			var TO=[];
			for(y=0;y<checks.length;y++)if(elements[checks[y]].checked)TO[TO.length]=y;
			var dest=[];
			for(i=1;i<destinazioneOptions.length;i++){
				var found=false;
				for(y=0;y<TO.length;y++){
					if( destinazioneOptions[i][first_destOpt+TO[y]].indexOf("T")!=-1 ){
						found=true;
						break;
					}
				}
				if(found)dest[dest.length]=destinazioneOptions[i];
			}
			popola_destinazione(dest);
		}else{
			resetSearchTravelForm();
		}
	break;
	case"tipovacanza":
		// trovo la posizione corrente della select tipovacanza
		var pos = tipovacanza.options.selectedIndex;
		if(pos>0){
			// trovo la voce dell'array destinazioneOptions corrispondente
			var current = getItemFromText( destinaz.options[destinaz.options.selectedIndex].text , destinazioneOptions );
			// trovo lindice dell'array tipovacanzaOptions corrispondente
			var index = getIndexFromText( tipovacanza.options[tipovacanza.options.selectedIndex].text, tipovacanzaOptions ) - 1 ;
			if(tiporicerca!="weekend"){
				// abilito le agenzie che forniscono il tipo di vacanza nella destinazione selezionata e disabilito le altre
				
				for(y=0;y<checks.length;y++){	
					if(current[first_destOpt+y].charAt(index)=="T"){
						elements[checks[y]].disabled=0;
					}else{
						elements[checks[y]].checked=0;
						elements[checks[y]].disabled=1;
					}
				}
			}
			// creo un array con le opzioni che soddisfano i requisiti
			var arr=[];
			for(i=1;i<destinazioneOptions.length;i++){
				for(y=0;y<checks.length;y++){
					var index = getIndexFromText( tipovacanza.options[tipovacanza.options.selectedIndex].text , tipovacanzaOptions ) - 1 ; // -1 xkè c'è la 1a opzione
					if(elements[checks[y]].checked && destinazioneOptions[i][first_destOpt+y].charAt(index)=="T"){
						arr[arr.length]=destinazioneOptions[i];
						break;
					}
				}
			}
			// popolo la select
			popola_destinazione(arr);
			
			// trovo tutti i tipi delle dest trovate
/*			
			var TO=[];
			for(y=0;y<checks.length;y++)if(elements[checks[y]].checked)TO[TO.length]=y;
			var tipi=[];
			for(y=1;y<tipovacanzaOptions.length;y++)tipi[tipi.length]=false;
			for(i=0;i<arr.length;i++){
				for(y=0;y<TO.length;y++){
					for(j=0;j<tipi.length;j++){
						tipi[j] = ( tipi[j] || arr[i][first_destOpt+TO[y]].charAt(j)=="T" );
					}
				}
			}
			arr=[];
			for(i=0;i<tipi.length;i++){
				if(tipi[i])arr[arr.length]=tipovacanzaOptions[i+1];
			}
			popola_tipoVacanza(arr);
*/			
		}else{
			resetSearchTravelForm();
		}
	break;
	}}
}
function getItemFromText(val,arr){
	var i;
	for(i=0;i<arr.length;i++){
		if(LTrim(arr[i][0])==LTrim(val))return arr[i];
	}
	return null;
}

function getIndexFromText(val,arr){
	var i;
	for(i=0;i<arr.length;i++){
		if(LTrim(arr[i][0])==LTrim(val))return i;
	}
	return -1;
}

function resetSearchTravelForm(){
	with(document.ricercaSoluzioni){
		if(tiporicerca!="weekend"){
			// seleziono tutte le agenzie
			for(y=0;y<checks.length;y++){
				elements[checks[y]].disabled=0;
				elements[checks[y]].checked=1;
			}
		}
		if(tipovacanza){
			if(tipovacanza.type=="select-one"){
				popola_tipoVacanza();
				if(tipovacanza.options[0])tipovacanza.options.selectedIndex=0;
			}
		}
		popola_destinazione();
		if(destinaz.options[0])destinaz.options.selectedIndex=0;
	}
}

var checksWeekend=["touroperator_AL"];
function heyItsMeWeekend(){
	
var who=(arguments.length>0)?arguments[0]:"tipovacanza";
with(document.ricercaSoluzioni){
	var	pos=tipovacanza.options.selectedIndex;
	var i,first_destOpt=4;
	switch(who){
	case"touroperator_AL":
	case"tipovacanza":
		if(pos!=-1){
			// ricavo il tipovacanza selezionato
			var tipoformTipoVacanza = tipovacanzaOptions[pos][3];			
			// e il tema
			var temaTipoVacanza = tipovacanzaOptions[pos][4];			
			// svuoto le destinazioni...
			for(i=destinaz.options.length-1;i>=0;i--)destinaz.options[i]=null;
			// ...e le ricarico in base a quanto selezionato
			var arr=[];
			for(i=1;i<destinazioneOptions.length;i++){
				if( destinazioneOptions[i][3]==temaTipoVacanza && destinazioneOptions[i][10]==tipoformTipoVacanza ) {
					arr[arr.length]=destinazioneOptions[i];
				}
			}
			popola_destinazione(arr);
		}
	break;
	}
	
	}
}

function zeroFill(q,z){
	q=String(q);
	while(q.length<z)q="0"+q;
	return q;
}
function setSelect(f,q,v){
	var i;
	for(i=0;i<document.forms[f].elements[q].options.length;i++){
		if(document.forms[f].elements[q].options[i].value==v){
			document.forms[f].elements[q].options.selectedIndex=i;
			break;
		}
	}
}
function setFormDate(f,fd,fm,fY){
	now=new Date();
	d=zeroFill(now.getDate(),2);
	dp=zeroFill(now.getDate()+1,2);
	m=zeroFill(now.getMonth()+1,2);
	Y=now.getFullYear();
//	H=zeroFill(now.getHours(),2);
//	i=zeroFill(now.getMinutes(),2);
	setSelect(f,fd,d);
	setSelect(f,fm,m);
	setSelect(f,fY,Y);
}

function submitCheck() {
	
	// controlla che ci sia selezionato almeno un tour operator
	var touroperator_AL = document.ricercaSoluzioni.touroperator_AL.checked;
	var touroperator_FR = document.ricercaSoluzioni.touroperator_FR.checked;
        var touroperator_KA = document.ricercaSoluzioni.touroperator_KA.checked;
        var touroperator_VI = document.ricercaSoluzioni.touroperator_VI.checked;
        var touroperator_VO = document.ricercaSoluzioni.touroperator_VO.checked;
        var touroperator_BR = document.ricercaSoluzioni.touroperator_BR.checked;
        
        // metto in or tutti i tour
        var selezionato = ( touroperator_AL || touroperator_FR || touroperator_KA || touroperator_VI || touroperator_VO || touroperator_BR );
        // se ve n'e' almeno uno a true selezionato vale true
        if(!selezionato) {
        	alert('Selezionare almeno un tour operator');
        	return false;
        }
        
        // controllo data
        var gg = document.ricercaSoluzioni.dtPartDaGiorno.value;
        var mm = document.ricercaSoluzioni.dtPartDaMese.value;
        var yy = document.ricercaSoluzioni.dtPartDaAnno.value;
        var dataValida = verificaData(gg,mm,yy);
        if(!dataValida) {
        	alert("La data inserita e' formalmente scorretta");
        	return false;
        }
        
        dataValida = !dateExpired(gg,mm,yy)
	if(!dataValida) {
		alert("La data di partenza non puo' essere inferiore ad oggi");
		return false;
	}
	/*
	var tipoRic = document.ricercaSoluzioni.tiporicerca.value;
	if(tipoRic == "offerte") {
		dataValida = checkDateOfferta(gg,mm,yy)
		if(!dataValida) {
			alert("La data indicata non e' abbastanza recente per una vacanza in offerta.");
			return false;
		}
	} */
        
        // controllo che vi siano parametri di ricerca
        var parametroRicerca = document.ricercaSoluzioni.destinaz.options[document.ricercaSoluzioni.destinaz.options.selectedIndex].value;
        if(LTrim(parametroRicerca) == "") {
        	alert("La selezione e' troppo generica, scegliete una destinazione piu' specifica.");
        	return false;
        }
		if(typeof tipovacanzaOptions!="undefined"){
			var parametroRicerca = document.ricercaSoluzioni.tipovacanza.options[document.ricercaSoluzioni.tipovacanza.options.selectedIndex].value;
			if(LTrim(parametroRicerca) == "") {
				alert("Non hai selezionato il tipo vacanza.");
				return false;
			}
		}
		var parametroRicerca = false;
		for(var i=0;i<checks.length;i++){
			parametroRicerca = ( parametroRicerca || document.ricercaSoluzioni.elements[checks[i]].checked );
        	if(parametroRicerca)break;
		}
		if( !parametroRicerca ){
        	alert("Selezionare almeno un Tour Operator.");
        	return false;
		}
		
        // se arrivo qui ho fatto tutti i controlli che servivano
        // copio le descrizioni di destinazione, tipo vacanza e durata nei campi hidden preposti
        
        if(document.ricercaSoluzioni.destinaz.selectedIndex != -1) {        	
        	document.ricercaSoluzioni.destinazioneEstesa.value=document.ricercaSoluzioni.destinaz.options[document.ricercaSoluzioni.destinaz.selectedIndex].text;        
        }
	else {
		document.ricercaSoluzioni.destinazioneEstesa.value = "  ";
	}
        if(document.ricercaSoluzioni.tipovacanza && document.ricercaSoluzioni.tipoVacanzaEstesa) {
        	if(document.ricercaSoluzioni.tipovacanza.selectedIndex!=-1) {
        		document.ricercaSoluzioni.tipoVacanzaEstesa.value=document.ricercaSoluzioni.tipovacanza.options[document.ricercaSoluzioni.tipovacanza.selectedIndex].text;
        	}
        	else {
        		document.ricercaSoluzioni.tipoVacanzaEstesa.value = "  ";
        	}
        }
        document.ricercaSoluzioni.durataEstesa.value=document.ricercaSoluzioni.durata.options[document.ricercaSoluzioni.durata.selectedIndex].text;
        
        return true;
}

function y2k(number){return (number < 1000) ? number + 1900 : number; }
function verificaData(gg,mm,yyyy){
	today = new Date();
	gg*=1;
	mm*=1;
	yyyy*=1;
	yyyy = ((!yyyy) ? y2k(today.getFullYear()):yyyy)
	mm = ((!mm) ? today.getMonth():mm-1)
	if (!gg) return false
	testa = new Date(yyyy,mm,gg)
	if ((y2k(testa.getFullYear()) == yyyy) && (mm == testa.getMonth()) && (gg == testa.getDate()) ){
		return true
	}else{
		return false
	}
}

function dateExpired(g,m,a){
	m--;
	var t2=new Date(a,m,g);
	var t1=new Date();
	if(t2.getMonth()==t1.getMonth()&&t2.getDay()==t1.getDay()&&t2.getFullYear()==t1.getFullYear()){
		return false;
	}else{
		return (t1<t2)?false:true;
	}
}

function checkDateOfferta(g,m,a) {
	m--;
	var t1 = new Date(a,m,g);
	var today = new Date();
	var dif = t1*1 - today*1;
	dif = dif/1000/3600/24;
	if(dif>45)
		return false;
	else
		return true;
}

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function


