// funziona solo con jQuery
var maxPax = 12;					// Se viene selezionata una compagnia prende il valore dal DB 
var maxVeh = 8;						// Se viene selezionata una compagnia prende il valore dal DB
var maxPet = 4;						// = $max_pet di db_config.php
var maxCat = 5;    					
var optBici =2;    					
var optMoto =4;
var etaAuto = 18;
var etaMoto = 16;
var etaBici = 12;
var maxTrailer=1;	
var maxT = maxVeh;					
var numTrailer=0;
var numVehicle=0;
var numPax=0;
var numPet=0;
var orig = 0;
var dest = 0;
var comp = 0;
var	ordinePD = 0;                        // 0:Partenza-Destinazione, 1:viceversa
var maxInterne=0;
var maxEsterne=0;
var numInterne=0;
var numEsterne=0;
var reload=0;
var biciSu=0;
var biciDietro=0;

// Inizializza le variabili globali coerentemente con db_config.php e ilDB
function setParameter(maxP, maxV, maxA, maxC, optB, optM, etaA, etaM, etaB){		
	maxPax=maxP;
	maxVeh=maxV;
	maxT=maxV;
	maxPet=maxA;
	maxCat=maxC;
	optBici=optB;
	optMoto=optM;
	etaAuto=etaA;
	etaMoto=etaM;
	etaBici=etaB;
}

function checkForm(searchDepartures){	
	// Controllo campi vuoti	
	var check=-1;
	myForm=document.chooseDateForm;
  	if(myForm.start_date.value=="") {getAlert(0,check); return;}
	if(myForm.end_date.value==""&&myForm.ar.value=="true") {getAlert(1,check); return;}
	if(myForm.partenza.value=="0"){
		if (ordinePD==0) getAlert(2,check); 
		else getAlert(3,check); 
		return;
	}
		if(myForm.destinazione.value=="0"){
		if (ordinePD==0) getAlert(3,check); 
		else getAlert(2,check); 
		return;
	}
	if(myForm.numpax.value=="0") {getAlert(4,check); return;}
	for(var i=1; i<=myForm.numveh.value; i++){
		var val=0;
		if(document.getElementById('tipoVeh'+i).value!=0){
			var arVeh = document.getElementById('tipoVeh'+i).value.split('-');
			val = arVeh[0];
		}
		if(val==0){ getAlert(5,i); return;}	
		else {
			if(val!=4){
				if(document.getElementById('associaP'+i).value==0) {getAlert(6,i); return;}
			}
			else{
				if(document.getElementById('bicicletta'+i).value==0&&document.getElementById('associaP'+i).value==0) {getAlert(7,i); return;}
				else if(document.getElementById('associaB'+i).value==0&&document.getElementById('bicicletta'+i).value!=0) {getAlert(8,i); return;}
			}
		}		
	}
	for(var i=0; i<myForm.numTrailer.value; i++){
		var val=0;
		var x=i+1;
		if(document.getElementById('tipoTra'+i).value!=0){
			var arTra = document.getElementById('tipoTra'+i).value.split('-');
			val = arTra[0];
		}
		if(val==0){ getAlert(9,x); return;}	
		else if(document.getElementById('associaT'+i).value==0) {getAlert(10,x); return;}		
	}
	for(var i=0; i<myForm.numPet.value; i++){
		var x=i+1;		
		if(document.getElementById('Pet'+i).value=="0"){ getAlert(11,x); return;}	
	}
	if (checkOpenDeck()==true) {getAlert(12,check); return;}	
	if(!contaAdulti()) {getAlert(13,check); return;}
	if(!isDate(document.getElementById('start_date').value)) {getAlert(31,check); return;}
	if(document.getElementById('ar').value=="true")
		if(!isDate(document.getElementById('end_date').value)) {getAlert(32,check); return;}		
	if(dateCompare(today(0), document.getElementById('start_date').value)==1) {getAlert(33,check); return;}	
	if(document.getElementById('ar').value=="true")
		if(dateCompare(document.getElementById('end_date').value, document.getElementById('start_date').value)!=1) {getAlert(34,check); return;}	
	// Preparazione campi aggiuntivi form
	if(ordinePD==0){
		myForm.porto_partenza.value=myForm.partenza[myForm.partenza.selectedIndex].text;
		myForm.porto_arrivo.value=myForm.destinazione[myForm.destinazione.selectedIndex].text;	
	}
	else{
		myForm.porto_arrivo.value=myForm.partenza[myForm.partenza.selectedIndex].text;
		myForm.porto_partenza.value=myForm.destinazione[myForm.destinazione.selectedIndex].text;	
		}
	myForm.id_comp.value=myForm.compagnia.value;
	setLunAltBici();
	// submit form
	myForm.end_date.style.display="none";
	myForm.action=searchDepartures;
	myForm.submit();
}

function resetForm(){	
	document.chooseDateForm.submit();
}

function contaAdulti(){
	var check = false;
	for (var j=1; j <= numPax; j++)
		if(document.getElementById('eta'+j).value>=12){check=true; break;}
	return check;	
}

function setLunAltBici(){
	for(var i=1; i <=numVehicle; i++){ 
		if(document.getElementById('catVehicle'+i).value=='4'){
			if(document.getElementById('bicicletta'+i).value=='1')
				document.getElementById('extraAlt'+document.getElementById('associaB'+i).value).value="0.50";
			if(document.getElementById('bicicletta'+i).value=='2')
				document.getElementById('extraLun'+document.getElementById('associaB'+i).value).value="0.50";
		}
	}	
}

function reloadData(sel_porto_def, sel_comp_def, listacomp, idarea_agenzia, dizlungh, dizpeso)
{
	reload=1;
	$('.date-pick').datePicker({clickInput:true})
	document.getElementById('start_date').value=document.getElementById('start_date_sex').value;	
	if(document.getElementById('ar_sex').value=='true')
	{ 	
		document.getElementById('ritorno').checked=true;	
		document.getElementById('ar').value='true';				
		enableData();	
		document.getElementById('end_date').value=document.getElementById('end_date_sex').value;						
	}
	else 
	{
		document.getElementById('ritorno').checked=false;	
		document.getElementById('ar').value='false';		
		enableData();	
	}
	var id_tratta=document.getElementById('id_tratta_sex').value;
	var id_tratta_ritorno=document.getElementById('id_tratta_ritorno_sex').value;
	//alert("ID_TRATTA="+id_tratta);alert("ID_TRATTA_RITORNO="+id_tratta_ritorno);
	comp=document.getElementById('compagnia').selectedIndex=getIndex(document.getElementById('compagnia_sex').value, 'compagnia');
	selOrig(document.getElementById('compagnia_sex').value,sel_porto_def, sel_comp_def, listacomp, idarea_agenzia);
	selNumPaxVeh(document.getElementById('compagnia_sex').value);
	orig=document.getElementById('partenza').value=document.getElementById('partenza_sex').value;
	document.getElementById('partenza').selectedIndex=getIndex(document.getElementById('partenza_sex').value, 'partenza');
	selDest(document.getElementById('partenza_sex').value, sel_porto_def, sel_comp_def, listacomp, idarea_agenzia);
	dest=document.getElementById('destinazione').value=document.getElementById('destinazione_sex').value;
	document.getElementById('destinazione').selectedIndex=getIndex(document.getElementById('destinazione_sex').value, 'destinazione');
	document.getElementById('compagnia').value=document.getElementById('compagnia_sex').value;
	document.getElementById('id_comp').value=document.getElementById('compagnia_sex').value;
	document.getElementById('porto_partenza').value=document.getElementById('porto_partenza_sex').value;
	document.getElementById('porto_arrivo').value=document.getElementById('porto_arrivo_sex').value;
	numPax = document.getElementById('numpax_sex').value;
	displayPax(numPax);
	fillPax(numPax);
	numVehicle=document.getElementById('numveh_sex').value;
	displayVeh(numVehicle);
	fillVeh(numVehicle, dizlungh, dizpeso);
	numPet=document.getElementById('numPet_sex').value;
	document.getElementById('numPet').selectedIndex=getIndex(document.getElementById('numPet_sex').value,'numPet');
	displayPet(numPet,maxPet);
	fillPet(numPet);	
	numTrailer=document.getElementById('numTrailer_sex').value;			
	displayTrailer(numTrailer);		
	document.getElementById('numTrailer').selectedIndex=numTrailer;	
	fillTrailer(numTrailer);	
	document.getElementById('id_tratta').value=id_tratta;
	document.getElementById('id_tratta_ritorno').value=id_tratta_ritorno;		
	if(document.getElementById('PD_sex').value=='1'){		
			switchPD();			
	}				
}


function fillPet(nP) {
	for(var y=0; y < nP; y++)		
		document.getElementById('Pet'+y).selectedIndex=getIndex(document.getElementById('Pet'+y+'_sex').value,'Pet'+y);
}

function fillTrailer(nT) {
	for(var y=0; y < nT; y++){		
		document.getElementById('tipoTra'+y).selectedIndex=getIndex(document.getElementById('tipoTra'+y+'_sex').value,'tipoTra'+y);
		displayDettTrailer(document.getElementById('tipoTra'+y+'_sex').value, y, 1);		
	}
	for(var x=1; x <=nT; x++) 
	{		
		selectedTrailer(x, document.getElementById('associaT'+x+'_sex').value, "Internal Error");
		document.getElementById('associaT'+x).selectedIndex=getIndex(document.getElementById('associaT'+x+'_sex').value,'associaT'+x);				
	}
}

function fillVeh(nV, dizlungh, dizpeso) {
	for(var y=1; y <= nV; y++){		
		document.getElementById('tipoVeh'+y).selectedIndex=getIndex(document.getElementById('tipoVeh'+y+'_sex').value,'tipoVeh'+y);
		displayDettV(document.getElementById('tipoVeh'+y+'_sex').value, y, 1, dizlungh, dizpeso);
		document.getElementById('bicicletta'+y).selectedIndex=getIndex(document.getElementById('bicicletta'+y+'_sex').value,'bicicletta'+y);
		displayBike(document.getElementById('bicicletta'+y).selectedIndex,y);						
	}			
	for(var x=1; x <=nV; x++) 
	{		
		associaPilota(x, document.getElementById('associaP'+x+'_sex').value);				
		document.getElementById('associaB'+x).selectedIndex=getIndex(document.getElementById('associaB'+x+'_sex').value,'associaB'+x);		
		if(isset(document.getElementById('gpl'+x+'_sex')))
		{
			document.getElementById('gpl'+x).disabled=false;
			document.getElementById('gpl'+x).checked=true;
		}
		if(isset(document.getElementById('sidecar'+x+'_sex')))
		{
			document.getElementById('sidecar'+x).disabled=false;
			document.getElementById('sidecar'+x).checked=true;
		}		
	}
}

function fillPax(nP){
	for(var i=1; i <= nP; i++)
	{
		document.getElementById('tipoSis'+i).value=document.getElementById('tipoSis'+i+'_sex').value;
		document.getElementById('tipoSis'+i).selectedIndex=getIndex(document.getElementById('tipoSis'+i+'_sex').value,'tipoSis'+i);
		document.getElementById('eta'+i).value=document.getElementById('eta'+i+'_sex').value;
		document.getElementById('eta'+i).selectedIndex=getIndex(document.getElementById('eta'+i+'_sex').value,'eta'+i);
		if(isset(document.getElementById('top'+i+'_sex')))
		{
			document.getElementById('top'+i).disabled=false;
			document.getElementById('top'+i).checked=true;
		}
		if(isset(document.getElementById('studente'+i+'_sex')))
		{
			document.getElementById('studente'+i).disabled=false;
			document.getElementById('studente'+i).checked=true;
		}
		if(isset(document.getElementById('disabili'+i+'_sex')))
		{
			document.getElementById('disabili'+i).disabled=false;
			document.getElementById('disabili'+i).checked=true;
		}			
	}
	document.getElementById('esterne').value=document.getElementById('esterne_sex').value;
	setEsterne(document.getElementById('esterne_sex').value)
	document.getElementById('interne').value=document.getElementById('interne_sex').value;
	setInterne(document.getElementById('interne_sex').value)
}
	
function getIndex(val, selectId){
	for(var index=0; index <document.getElementById(selectId).length; index++)
	{		
		if(document.getElementById(selectId)[index].value==val) 
		{	
			return index;
		}
	}
	return 0;
}

function checkOpenDeck(){
	var counter=0;
	var openDeck=0;
	for(var i=1; i <= numPax; i++){
		if(document.getElementById('tipoSis'+i).value=="7") openDeck++;		
	}
	for(var i=1; i <= numVehicle; i++){
		var code = document.getElementById('tipoVeh'+i).value.split('-');
		if (code[1]==9) counter++;
	}
	for(var i=0; i < numTrailer; i++){
		var code = document.getElementById('tipoTra'+i).value.split('-');
		if (code[1]==8) counter++;
	}
	if (counter<=0&&openDeck>0) return true;
	else return false;
}

function infoAlert(val,msg, value){
	if(value!=0&&document.chooseDateForm.destinazione.value==0&&val=="pax1"&&ordinePD==0) alert(msg);
	if(value!=0&&document.chooseDateForm.destinazione.value==0&&val=="pax2"&&ordinePD==1) alert(msg);
	if(value!=0&&document.chooseDateForm.numpax.value=="0"&&val=="veh") alert(msg);
}

function setEsterne(val){
	var app = numEsterne;
	var contaEst=0;
	for(var i=1; i <= numPax; i++)
		if(document.getElementById('tipoSis'+i).value==3) contaEst++;
	if (val < parseInt(contaEst/4+0.9)) val =app;
	numEsterne=val;
	document.getElementById('esterne').value=numEsterne;
}

function setInterne(val){  	
	var app = numInterne;
	var contaInt=0;
	for(var i=1; i <= numPax; i++)
		if(document.getElementById('tipoSis'+i).value==2) contaInt++;
	if (val < parseInt(contaInt/4+0.9)) val =app;
	numInterne=val;
	document.getElementById('interne').value=numInterne;
}

function setAutoBici(idx){			
	$("select[id='associaB"+idx+"']").empty();
	var options = '<option value="0" class="default" id="default">--</option>';	
	for(var i=1; i <= numVehicle; i++){
		var val = document.getElementById('tipoVeh'+i).value.split('-');		
		if(val[0]==1) options += '<option value="'+i+'" id="'+i+'">'+i+'</option>';	
		else options += '<option value="'+i+'" id="'+i+'" disabled="disabled">'+i+'</option>';	
	}
	$("select[id='associaB"+idx+"']").html(options);		
}
	
function setAutoTrailer(tipoT,idx){	
	var TT = tipoT.split('-');
	$("select[id='associaT"+idx+"']").empty();
	var options = '<option value="0" class="default" id="default">--</option>';	
	for(var i=1; i <= numVehicle; i++){
		var val = document.getElementById('tipoVeh'+i).value.split('-');
		if((val[0]==1||val[0]==6)&&TT[1]==4) options += '<option value="'+i+'" id="'+i+'">'+i+'</option>';			
		else if (val[0]==1) options += '<option value="'+i+'" id="'+i+'">'+i+'</option>';			
		else options += '<option value="'+i+'" id="'+i+'" disabled="disabled">'+i+'</option>';	
	}
	$("select[id='associaT"+idx+"']").html(options);		
}

function totVehicle(i){
	var tot = 0;
		for(var j=1; j <= maxVeh; j++) if (document.getElementById('catVehicle'+j).value==i) tot++;
	return tot;
}

function associaPilota(idx, val){	
	$("select[id='associaP"+idx+"']").empty();
	var options = '<option value="0" class="default" id="default">--</option>';	
	for(var i=1; i <= numPax; i++)
		if(val!=0){
			if (val==i) options += '<option value="'+i+'" id="'+i+'" SELECTED>'+i+'</option>';					
			else options += '<option value="'+i+'" id="'+i+'">'+i+'</option>';					
		}		
		else{		
			options += '<option value="'+i+'" id="'+i+'">'+i+'</option>';					
		}
	$("select[id='associaP"+idx+"']").html(options);		
}

function selectedPilota(val,sel,msg){	
	var count=0;
	for(var i=1; i<=numVehicle; i++)
		if(document.getElementById('associaP'+i).selectedIndex==val&&i!=sel) count++;
	if(count>0&&val!=0) {
		alert(msg);
		document.getElementById('associaP'+sel).selectedIndex=0;
		} 	

	else document.getElementById('associaP'+sel).selectedIndex=val;	
}

function checkEta(val,sel,msg1, msg2){	
		var check = 1;
		var optVeh = document.getElementById('tipoVeh'+sel).selectedIndex;
		var eta = document.getElementById('eta'+val).value;						
		if(eta>=etaAuto) check=0;
		else if(eta>=etaMoto&&(optVeh==optBici||optVeh==optMoto)) check =0;
		else if (eta>=etaBici&&optVeh==optBici) check=0;
		else check =1;
		if(check){
			alert(msg1+' '+sel+' '+msg2+' '+eta);
			document.getElementById('associaP'+sel).selectedIndex=0;
		}
		else document.getElementById('associaP'+sel).selectedIndex=val;
}

function selectedTrailer(val,sel,msg){
	var count=0;
	for(var i=0; i<numTrailer; i++)
		if(document.getElementById('associaT'+i).selectedIndex==val&&i!=sel) count++;
	if(count>0&&val!=0) {
		alert(msg);
		document.getElementById('associaT'+sel).selectedIndex=0;
		} 
	else document.getElementById('associaT'+sel).selectedIndex=val;	
}

function resetTrailer(valMax){
	for(var i=0; i<valMax; i++){
		document.getElementById('tipoTra'+i).selectedIndex=0;	
		displayDettTrailer('0-0',i, 0);
	}
}

function enableSelAssocia(val,i){		
    if(val!=2&&val!=0) document.getElementById('selAssocia'+i).style.display="inline";
	else document.getElementById('selAssocia'+i).style.display="none";
}

function contaCabine(){
	var maxInterne=0;
	var maxEsterne=0;
	var Int=0;
	var Est=0;
	for(var i=1; i <= numPax; i++){
		if(document.getElementById('tipoSis'+i).value==2) maxInterne++;
		if(document.getElementById('tipoSis'+i).value==3) maxEsterne++; 
		if(document.getElementById('tipoSis'+i).value!=4) {
			document.getElementById('top'+i).disabled=false;			
			}
		else {
			document.getElementById('top'+i).disabled=true;	
			document.getElementById('top'+i).checked=false;
		}
	}
	if(maxInterne==0) Int=0
	else Int= parseInt(maxInterne/4+0.9)
	if(maxEsterne==0) Est=0
	else Est= parseInt(maxEsterne/4+0.9)
	if(numInterne<Int) numInterne=Int
	if(numEsterne<Est) numEsterne=Est
	//esterne
	if(maxEsterne<numEsterne) numEsterne=maxEsterne;
	if(maxInterne<numInterne) numInterne=maxInterne;
	if( maxEsterne>0){
		$("select[id='esterne']").empty();
	    	var options = '<option value="0" class="default" id="default">--</option>';				
		for (var i = 1; i <= maxEsterne; i++) {      		  				
		      if(i==numEsterne) options += '<option value="' + i + '" id="' + i + '" selected="selected">' + i + '</option>';
			  else options += '<option value="' + i + '" id="' + i + '">' + i + '</option>';
    		}
		$("select[id='esterne']").html(options);					
	}
	else {
		numEsterne=0;
		$("select[id='esterne']").empty();
	   	var options = '<option value="0" class="default" id="default">--</option>';				
		$("select[id='esterne']").html(options);					
	}
	// interne
	if( maxInterne>0){
		$("select[id='interne']").empty();
	    	var options = '<option value="0" class="default" id="default">--</option>';				
		    for (var i = 1; i <= maxInterne; i++) {      		  
		      if(i==numInterne) options += '<option value="' + i + '" id="' + i + '" selected="selected">' + i + '</option>';
			  else options += '<option value="' + i + '" id="' + i + '">' + i + '</option>';
    		}
		$("select[id='interne']").html(options);				
	}
	else {
		numInterne=0;
		$("select[id='interne']").empty();
	   	var options = '<option value="0" class="default" id="default">--</option>';				
		$("select[id='interne']").html(options);			
	}
}
	
function enableSelect(){	
	var totAuto = totVehicle(1)+totVehicle(6);
	var app = Math.min(totAuto,(maxVeh-numVehicle));
	var check = 0;
	maxTrailer = Math.min(app,numVehicle);	
	if(maxTrailer==0) document.getElementById('infoTrailer').style.display="inline";
	else document.getElementById('infoTrailer').style.display="none";
	$.get("./select.php", { numTrailer: maxTrailer, requestItems: 'maxTrailer'}, 
		   function(trailer){   
			$("select[id='numTrailer']").empty();		
			options = '<option value="0">--</option>';
			for(var j=1; j<=trailer; j++) {
				if(numTrailer>=maxTrailer){
					if(maxTrailer==j) {
						options += '<option value="'+j+'" selected="selected">'+j+'</option>';				
						check = j;
					}					
					else options += '<option value="'+j+'">'+j+'</option>';
					}
				
				else{
					if(numTrailer==j) {						
						options += '<option value="'+j+'" selected="selected">'+j+'</option>';				
						check = j;
					}					
					else options += '<option value="'+j+'">'+j+'</option>';
				}				
			}					
			displayTrailer(check);									
			$("select[id='numTrailer']").html(options);		
			});	
	for(var i=1; i <= numVehicle; i++){
		if (totAuto>0){	
			document.getElementById('sopra'+i).disabled=false;
			document.getElementById('dietro'+i).disabled=false;
		}
		else {					
			document.getElementById('mano'+i).selected=true;
			document.getElementById('hide-bike'+i).style.display="none";
			document.getElementById('sopra'+i).disabled=true;
			document.getElementById('dietro'+i).disabled=true;
		}
	}	
}

// Select destinazione 
function selDest(partenza, defPorto, defComp, arcomp_agenzia, idarea_agenzia) {
//alert("A");
//alert(partenza);
//alert(partenza+" - "+defPorto+" - "+defComp+" - "+arcomp_agenzia+" - "+idarea_agenzia);
	  orig = partenza;
	  if(partenza==0) {dest=0; document.getElementById('destinazione').selectedIndex=0;}
	  if(partenza==0) document.getElementById('infoDes').style.display="inline";
	  else document.getElementById('infoDes').style.display="none";
	  var comp = document.getElementById('compagnia').value;
	  $.get("./select.php", { partenzaId: partenza, compagniaId:comp ,requestItems: 'destinazione', listacomp:arcomp_agenzia, zona_agenzia:idarea_agenzia}, 
	  function(dataDestinazione){	  
	    $("select[id='destinazione']").empty();
		if(orig==0||dest==0) displayPax(0);		
		var options = '<option class="default" value="0" id="default">'+defPorto+'</option>';	
	    var arrayDestinazione = dataDestinazione.split( '||');			
	    for (var i = 1; i < arrayDestinazione.length; i++) {		
	      var destinazione = arrayDestinazione[i].split( ',');
    	  if(destinazione[0]==dest){
			  options += '<option value="' + destinazione[0] + '" selected="selected">' + destinazione[1] + '</option>';		
		  }
		  else{ 
			  options += '<option value="' + destinazione[0] + '">' + destinazione[1] + '</option>';		  
		  }
	    }		
    	$("select[id='destinazione']").html(options);					
  	});
		//alert("select.php?partenzaId="+partenza+"&compagniaId="+comp+"&requestItems=destinazione&listacomp="+arcomp_agenzia+"&zona_agenzia="+idarea_agenzia);
	  $.get("./select.php", { partenzaId: orig, destinazioneId: dest, requestItems: 'compagnia', listacomp:arcomp_agenzia}, 
	  function(dataCompagnia){		  
        $("select[id='compagnia']").empty();
	    var options = '<option class="default" value="0" id="default">'+defComp+'</option>';	
		var arrayCompagnia = dataCompagnia.split( '||');			
	    for (var i = 1; i < arrayCompagnia.length; i++) {		
	      var compagnia = arrayCompagnia[i].split( ',');
		  if(compagnia[0]==comp) options += '<option value="' + compagnia[0] + '" selected="selected">' + compagnia[1] + '</option>';
		  else options += '<option value="' + compagnia[0] + '">' + compagnia[1] + '</option>';
	    }
    	$("select[id='compagnia']").html(options);					
  	});	  		

		if (reload<=0)
		{
		   if (ordinePD==0)
			 {
		      var par = document.getElementById('partenza').value;
			 		var des = document.getElementById('destinazione').value;
	     }
	  	 else
			 {
		      var des = document.getElementById('partenza').value;
		   		var par = document.getElementById('destinazione').value;
		   }
	  	 $.ajax({type: "GET", url: "select.php", data: "tratta="+par+"-"+des+"", success: function(response)
			 {
		      if (response!="0")
		   		   $('#id_tratta').attr('value', response);}
		   });
	     $.ajax({type: "GET", url: "select.php", data: "tratta="+des+"-"+par+"", success: function(response)
		   {
		      if (response!="0")
			    $('#id_tratta_ritorno').attr('value', response);}
		   });
    }

	//if (reload>0)
		//	 alert("PART="+partenza+" PORTO="+defPorto+" defComp"+defComp+" id_tratta"+document.getElementById('id_tratta').value);
}

// Select partenza 
function selOrig(compagnia, defPorto, defComp, arcomp_agenzia, idarea_agenzia)
{
	var check = 0;
	comp = compagnia;	
	dest = document.chooseDateForm.destinazione.value;
	$.get("./select.php", { compagniaId: compagnia, requestItems: 'partenza', listacomp:arcomp_agenzia}, 		  
	function(dataPartenza){
		$("select[id='partenza']").empty();
	    var options = '<option value="0" class="default" id="default">'+defPorto+'</option>';
    	var arrayPartenza = dataPartenza.split( '||');
	    for (var i = 1; i < arrayPartenza.length; i++) {      
		  var partenza = arrayPartenza[i].split( ',');	  
		  if (partenza[0]==orig) {
			  options += '<option value="' + partenza[0] + '" selected="selected">' + partenza[1] + '</option>';
			  check = 1;
		  }
	      else options += '<option value="' + partenza[0] + '">' + partenza[1] + '</option>';
    	}
	    $("select[id='partenza']").html(options);		
		if(!check) {
			check=0;
		}
		else {
			selDest(orig, defPorto, defComp, arcomp_agenzia, idarea_agenzia); 				 
			}
	 });
}

	
//RIEMPIE LA SELECT DEI PORTI DI PARTENZA IN BASE ALLA ZONA SCELTA
function selOrigByZone(compagnia, defPorto, defComp, arcomp_agenzia, idarea)
{
	 var check = 0;
	 dest = document.chooseDateForm.destinazione.value;
	 $.get("./select.php", {compagniaId:compagnia, requestItems:'zona', listacomp:arcomp_agenzia, zona_selezionata:idarea},
	 function(dataPartenza){
		$("select[id='partenza']").empty();
	    var options = '<option value="0" class="default" id="default">'+defPorto+'</option>';
    	var arrayPartenza = dataPartenza.split( '||');
	    for (var i = 1; i < arrayPartenza.length; i++)
			{      
		     var partenza = arrayPartenza[i].split( ',');	  
		     if (partenza[0]==orig)
			   {
			      options += '<option value="' + partenza[0] + '" selected="selected">' + partenza[1] + '</option>';
			      check = 1;
		     }
	       else
			      options += '<option value="' + partenza[0] + '">' + partenza[1] + '</option>';
      }
	    $("select[id='partenza']").html(options);
		  if (!check)
			   check=0;
		  else
			   selDest(orig, defPorto, defComp, arcomp_agenzia, idarea);
	 });
}


function setDest(destinazione, defPorto, defComp, listacomp, idarea_agenzia){
	dest=destinazione;
	if(orig==0||dest==0) displayPax(0);	
	selDest(orig, defPorto, defComp, listacomp, idarea_agenzia);
/*
	if(ordinePD==0){
		var par = document.getElementById('partenza').value;
		var des = document.getElementById('destinazione').value;
	}
	else {
		var des = document.getElementById('partenza').value;
		var par = document.getElementById('destinazione').value;
		}		
	$.ajax({type: "GET", url: "select.php", data: "tratta="+par+"-"+des+"", success: function(response){
		$('#id_tratta').attr('value', response);}
		});	
	$.ajax({type: "GET", url: "select.php", data: "tratta="+des+"-"+par+"", success: function(response){
			$('#id_tratta_ritorno').attr('value', response);}	
		});
*/
}

function selNumPaxVeh(compagnia){	
	$.get("./select.php", { compagniaId: compagnia, requestItems: 'numpax'}, 
	function(numMax){			
		$("select[id='numpax']").empty();
	    var options = '<option value="0" class="default" id="default">--</option>';				
	    for (var i = 1; i <= numMax; i++) {      		  
	      options += '<option value="' + i + '" id="' + i + '">' + i + '</option>';
    	}
		$("input[id='maxpax']").val(numMax);
    	$("select[id='numpax']").html(options);				
		displayPax(0);
		maxPax=numMax;
	 });	
	$.get("./select.php", { compagniaId: compagnia, requestItems: 'numveh'}, 
	function(numMax){		 
		$("select[id='numveh']").empty();
	    var options = '<option value="0" class="default" id="default">--</option>';				
	    for (var i = 1; i <= numMax; i++) {      		  
	      options += '<option value="' + i + '" id="' + i + '">' + i + '</option>';
    	}
		$("input[id='maxveh']").val(numMax);
    	$("select[id='numveh']").html(options);				
		maxVeh=numMax;
		document.getElementById('numveh').selectedIndex=numVehicle;
	 });	
}
	
function displayPax(val){
    if(val!=0)	numPax = val;
	numInterne=0;
	numEsterne=0;
	if(numPax==0) document.getElementById('cabineIE').style.display="none";
	else document.getElementById('cabineIE').style.display="inline";
	contaCabine()	
	for(var i=1; i <=numVehicle; i++) associaPilota(i, document.getElementById('associaP'+i).value);
	if (val==0) {
		if(numPax>maxPax){
			numPax=val;			
			document.getElementById('numpax').selectedIndex=0
		}
		else {document.getElementById('numpax').selectedIndex=numPax; return;}	
	}
	numPax = val;	
	var numMax = parseInt(document.getElementById('maxpax').value);	
	for	(var i=0; i<=maxPax; i++){		 
		 document.getElementById('hide-div-pax'+i).style.display="none";
    }
	var count = 0;
    for(i=0; i <= val; i++){
	     	document.getElementById('hide-div-pax'+i).style.display="block";
			count++;
	}
	for(var i=count; i<=numMax; i++){		 
		 document.getElementById('hide-div-pax'+i).style.display="none";
    }
	if(count==1){
				 document.getElementById('hide-div-pax0').style.display="none";
		}	
}

function displayPet(val,maxP){
	maxPet=maxP;
	if (val==0){
		for(var j=0; j<maxPet; j++)
			document.getElementById('PetType'+j).style.display="none";		
	}
	else{
		for(var i=0; i<val; i++)
			document.getElementById('PetType'+i).style.display="block";	
		for(var j=i; j<maxPet;j++)
			document.getElementById('PetType'+i).style.display="none";		
		}
}
	
function switchPD(){
	if(document.getElementById('hide-par1').style.display=="none"){
		document.getElementById('hide-switchA').style.display="inline";
 		document.getElementById('hide-switchB').style.display="none";
		document.getElementById('des2').style.display="none";
		document.getElementById('des1').style.display="inline";
		document.getElementById('hide-par1').style.display="inline";
		document.getElementById('hide-dest1').style.display="none";
		document.getElementById('hide-par2').style.display="none";
		document.getElementById('hide-dest2').style.display="inline";
		ordinePD = 0;
	}		
	else {
		document.getElementById('hide-switchA').style.display="none";
		document.getElementById('hide-switchB').style.display="inline";
		document.getElementById('des1').style.display="none";
		document.getElementById('des2').style.display="inline";		
		document.getElementById('hide-par1').style.display="none";
		document.getElementById('hide-dest1').style.display="inline";
		document.getElementById('hide-par2').style.display="inline";
		document.getElementById('hide-dest2').style.display="none";
		ordinePD = 1;
	}
	if(ordinePD==0){		
		var par = document.getElementById('partenza').value;
		var des = document.getElementById('destinazione').value;
		document.getElementById('PD').value='0';
	}
	else {
		var des = document.getElementById('partenza').value;
		var par = document.getElementById('destinazione').value;
		document.getElementById('PD').value='1';
	}			
	/*$.ajax({type: "GET", url: "select.php", data: "tratta="+par+"-"+des+"", success: function(response){
		$('#id_tratta').attr('value', response);}
	});	
	$.ajax({type: "GET", url: "select.php", data: "tratta="+des+"-"+par+"", success: function(response){
		$('#id_tratta_ritorno').attr('value', response);}	
	});	*/
	setTratta(par,des,0);
	setTratta(des,par,1);
}

var a_r=0;
var xmlHttp;
function setTratta(pars,dess,ar)
{	
	a_r=ar;
	xmlHttp=GetXmlHttpObject0();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="select.php?tratta="+pars+"-"+dess;
	xmlHttp.onreadystatechange=stateChanged1;		
	xmlHttp.open("GET",url,true);		
	xmlHttp.send(null);		
}	

// stringa[0]: esito, stringa[1]: funzione, stringa[2]...:risposta;
function stateChanged1(){
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   	{ 
	 	   var stringa=xmlHttp.responseText;
			 if (stringa!="0")
			 {
				  if (!a_r)
				  {
				     document.getElementById('id_tratta').value=stringa;
				  }
		      else
				 document.getElementById('id_tratta_ritorno').value=stringa;
			 }
    }
}

function displayBike(val,i){
	if(val==0) {
		document.getElementById('hide-bike'+i).style.display="none";
		document.getElementById('selAssocia'+i).style.display="inline";
		document.getElementById('associaBici'+i).style.display="inline";
	}
	else {
		document.getElementById('hide-bike'+i).style.display="inline";
		document.getElementById('selAssocia'+i).style.display="none";
		document.getElementById('associaBici'+i).style.display="none";
		setAutoBici(i);
	}
	
}
	
function disabledCheckB(i,val, msg){
	if(document.getElementById('associaB'+i)[val].disabled) {
		alert(msg);
		document.getElementById('associaB'+i).selectedIndex=0;
	}		
}

function disabledCheckT(i,val, msg){
	if(document.getElementById('associaT'+i)[val].disabled) {
		alert(msg);
		document.getElementById('associaT'+i).selectedIndex=0;
	}		
}

function displayStud(val, i){
	for(var x=1; x <=numVehicle; x++) associaPilota(x,0);
	if(val <26&&val>12) document.getElementById('studente'+i).disabled=false;
	else {
		document.getElementById('studente'+i).disabled=true;
		document.getElementById('studente'+i).checked=false;
		}
	}
	
function displayVeh(val){
	if(val==0)
	{		
		document.getElementById('numveh').selectedIndex=0; 
		for(var x=0; x <= maxVeh; x++)
			 document.getElementById('hide-div-veh'+x).style.display="none";
		displayTrailer(0);	 
		return;
	}
	numVehicle=val;	
	for(var i=1; i <=numVehicle; i++) associaPilota(i, document.getElementById('associaP'+i).value);
	if (val==0) {
		displayTrailer(0);
		document.getElementById('numveh').selectedIndex=0;		
	}
	var maxv= parseInt(numVehicle)+parseInt(numTrailer);
	if(maxv >= maxVeh) {
		document.getElementById('max_veh').style.display="inline";
		displayTrailer(0);
	}
	else document.getElementById('max_veh').style.display="none";			
	var numMax = parseInt(document.getElementById('maxveh').value);	
	for	(var i=0; i<=maxVeh; i++){		 
		 document.getElementById('hide-div-veh'+i).style.display="none";
	}
	var count = 0;
    for(var i=0; i <= val; i++){
	     	document.getElementById('hide-div-veh'+i).style.display="block";
			count++;
	}
	for	(var i=count; i<=numMax; i++){		 
		 document.getElementById('hide-div-veh'+i).style.display="none";
		 	if(count==1){
				 document.getElementById('hide-div-veh0').style.display="none";
		}
    }

	for (var i=1; i<=numVehicle;i++)
	{
	   var num_option=document.getElementById('tipoVeh'+i).options.length;
	   for (var j=0;j<num_option;j++)
	   {
		   if (document.getElementById('tipoVeh'+i).options[j].text=="Bus" && (document.getElementById('id_tratta').value=="1016" || document.getElementById('id_tratta').value=="1017"))
		      document.getElementById('tipoVeh'+i).options[j].text="Container";
		   else if (document.getElementById('tipoVeh'+i).options[j].text=="Bus" || document.getElementById('tipoVeh'+i).options[j].text=="Container")
		      document.getElementById('tipoVeh'+i).options[j].text="Bus";
	 	   //document.getElementById('modellovehicle_'+i).options[num_option].innerHTML = testo;
	   }
	}

}

function displayDettV(value, i, checkVeh, dizlungh, dizpeso){		
	associaPilota(i,0);
    var x = i-1;
  	var arrayValue=value.split( '-');
	var val = arrayValue[0];
    var veh = arrayValue[1];	
	if(val==1) {
		document.getElementById('gplCheck'+i).style.display="inline";
		document.getElementById('gpl'+i).checked=false;
		}
	else {
		document.getElementById('gplCheck'+i).style.display="none";
		document.getElementById('gpl'+i).checked=false;
		}
	if(val==3) {		
		document.getElementById('sidecar'+i).checked=false;
		}
	else {		
		document.getElementById('sidecar'+i).checked=false;
		}	
	document.getElementById('idVehicle'+i).value=veh;
	document.getElementById('catVehicle'+i).value=val;
	$.get("./select.php", { idVeicolo: veh, requestItems: 'Misure'},function(misure){
		var arrayMisure = misure.split( ',');		
		if(val=='1'||val=='6'||val=='5') {			
			document.getElementById('auto' + i).style.display="inline";
			document.getElementById('moto' + i).style.display="none";
			document.getElementById('bici' + i).style.display="none";			
			// lunghezza auto
			$("select[id='lunAutoMt"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">mt.</option>';				
			for(var j=parseInt(arrayMisure[0]); j<=parseInt(arrayMisure[3]); j++){				
				if(checkVeh==0){
					if(j==parseInt(arrayMisure[6])) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';					
					else options += '<option value="'+j+'">'+j+'</option>';
				}
				
				else{
					if(document.getElementById('lunAutoMt'+i+'_sex').value==j) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
					}								
			}
			$("select[id='lunAutoMt"+i+"']").html(options);	
			var lunCm = parseInt((arrayMisure[6] - parseInt(arrayMisure[6]))*100);																	 
			$("select[id='lunAutoCm"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">cm.</option>';							
			for(var j=0; j<10; j++){				
				if(checkVeh==0){
					if(j*10==lunCm)  options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';				
					else options += '<option value="'+j*10+'">'+j*10+'</option>';	
				}
				
				else{
					if(document.getElementById('lunAutoCm'+i+'_sex').value==j*10) options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}												
			}
			$("select[id='lunAutoCm"+i+"']").html(options);	
			// altezza auto
			$("select[id='altAutoMt"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">mt.</option>';				
			for(var j=parseInt(arrayMisure[2]); j<=parseInt(arrayMisure[5]); j++){				
				if(checkVeh==0){
					if(j==parseInt(arrayMisure[8])) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
				}				
				else{
					if(document.getElementById('altAutoMt'+i+'_sex').value==j) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
				}									
			}
			$("select[id='altAutoMt"+i+"']").html(options);				
			var altCm = parseInt((arrayMisure[8] - parseInt(arrayMisure[8]))*100);
			$("select[id='altAutoCm"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">cm.</option>';							
			for(var j=0; j<10; j++){	
				if(checkVeh==0){
					if(j*10==altCm) options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}				
				else{
					if(document.getElementById('altAutoCm'+i+'_sex').value==j*10) options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}				
			}
			$("select[id='altAutoCm"+i+"']").html(options);	
			// larghezza auto
			$("select[id='larAutoMt"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">mt.</option>';				
			for(var j=parseInt(arrayMisure[1]); j<=parseInt(arrayMisure[4]); j++){				
				if(checkVeh==0){
					if(j==parseInt(arrayMisure[7])) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
				}				
				else{
					if(document.getElementById('larAutoMt'+i+'_sex').value==j) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
				}										
			}
			$("select[id='larAutoMt"+i+"']").html(options);				
			var larCm = parseInt((arrayMisure[7] - parseInt(arrayMisure[7]))*100);
			$("select[id='larAutoCm"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">cm.</option>';							
			for(var j=0; j<10; j++){
				if(checkVeh==0){
					if(j*10==larCm) options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}				
				else{
					if(document.getElementById('larAutoCm'+i+'_sex').value==j*10) options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}				
			}
			$("select[id='larAutoCm"+i+"']").html(options);							
		}		
		else if(val=='2') {
			document.getElementById('auto' + i).style.display="none";
			document.getElementById('moto' + i).style.display="none";
			document.getElementById('bici' + i).style.display="none";			
		}
		else if(val=='3') {
			document.getElementById('auto' + i).style.display="none";
			document.getElementById('moto' + i).style.display="inline";
			document.getElementById('bici' + i).style.display="none";			
			// cc moto
			$("select[id='motoCc"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">c.c.</option>';				
			for(var j=parseInt(arrayMisure[1]); j<=parseInt(arrayMisure[4]); j++){				
				if(checkVeh==0){
					if(j==parseInt(arrayMisure[7])) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
				}				
				else{					
					if(document.getElementById('motoCc'+i+'_sex').value==j) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
				}
				if (j==100||j==125) j=j+24;
				else if(j>=300&&j<700) j=j+99;
				else j=j+49;
			}
			$("select[id='motoCc"+i+"']").html(options);	
			// lunghezza moto
			$("select[id='lunMotoMt"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">mt.</option>';				
			for(var j=parseInt(arrayMisure[0]); j<=parseInt(arrayMisure[3]); j++){				
			if(checkVeh==0){
					if(j==parseInt(arrayMisure[6])) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';					
					else options += '<option value="'+j+'">'+j+'</option>';
				}				
				else{
					if(document.getElementById('lunMotoMt'+i+'_sex').value==j) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
				}				
			}
			$("select[id='lunMotoMt"+i+"']").html(options);	
			var lunCm = parseInt((arrayMisure[6] - parseInt(arrayMisure[6]))*100);																	 
			$("select[id='lunMotoCm"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">cm.</option>';							
			for(var j=0; j<10; j++){				
				if(checkVeh==0){
					if(j*10==lunCm)  options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';				
					else options += '<option value="'+j*10+'">'+j*10+'</option>';	
				}				
				else{
					if(document.getElementById('lunMotoCm'+i+'_sex').value==j*10) options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}												
			}
			$("select[id='lunMotoCm"+i+"']").html(options);	
		}
		else if(val=='4') {
			document.getElementById('auto' + i).style.display="none";
			document.getElementById('moto' + i).style.display="none";
			document.getElementById('bici' + i).style.display="inline";	
		}
		else{
			document.getElementById('auto' + i).style.display="none";
			document.getElementById('moto' + i).style.display="none";
			document.getElementById('bici' + i).style.display="none";;		
		}				
	});	
	enableSelect();	
	enableSelAssocia(val,i);
	resetTrailer(maxTrailer);
	if(isset(document.getElementById('dimensione_veicolo_'+i))){
		if(veh==1||veh==10) document.getElementById('dimensione_veicolo_'+i).style.display="block";
		else document.getElementById('dimensione_veicolo_'+i).style.display="none";
	}
	
	if ((document.getElementById('id_tratta').value=="1016" || document.getElementById('id_tratta').value=="1017") && document.getElementById('tipoVeh'+i).value=="1-11")
	{
	    //alert(document.getElementById('tipoVeh'+i).value);
		//alert("Zammit");
		document.getElementById('dizlungh_'+i).innerHTML=dizpeso;
		document.getElementById('zamgroup_'+i).style.display='none';
    }
	else
	{
		document.getElementById('dizlungh_'+i).innerHTML=dizlungh;
		document.getElementById('zamgroup_'+i).style.display='inline';
    }
	
}

function displayTrailer(val){	
	numTrailer=val;	
	var maxv= parseInt(numVehicle)+parseInt(numTrailer);
	if(maxv >= maxVeh) {
		document.getElementById('max_veh').style.display="inline";
	}
	else {
		document.getElementById('max_veh').style.display="none";
	}
	if(val==0) 
	{
		document.getElementById('numTrailer').selectedIndex=0;					
		for	(var i=0; i<maxT; i++)		 			
			document.getElementById('hide-div-tra'+i).style.display="none"; 
			document.getElementById('main-div-tra').style.display="none"; 
		return;	
	}	
	else document.getElementById('main-div-tra').style.display="block"; 
	var count = 0;    
	for(var i=0; i < val; i++){			
   		 	document.getElementById('hide-div-tra'+i).style.display="block";
			count++;
	}
	var c = count
	for	(var i=c; i<maxT; i++){				
		document.getElementById('main-div-tra').style.display="block";
		document.getElementById('hide-div-tra'+i).style.display="none";		 		 		
	}	
}

function displayDettTrailer(value,i, checkTra){	
	var arrayValue=value.split( '-');
	if(arrayValue[0]!=0) document.getElementById('trailer' + i).style.display="inline";
	else document.getElementById('trailer' + i).style.display="none";	
    var veh = arrayValue[1];
	document.getElementById('idTrailer'+i).value=veh;	
	$.get("./select.php", { idVeicolo: veh, requestItems: 'Misure'},function(misure){
		var arrayMisure = misure.split( ',');		
		// lunghezza trailer
		$("select[id='lunTrailerMt"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">mt.</option>';							
			for(var j=parseInt(arrayMisure[0]); j<=parseInt(arrayMisure[3]); j++){				
				if(checkTra==0){
					if(j==parseInt(arrayMisure[6]))  options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
				}				
				else {
					if(document.getElementById('lunTrailerMt'+i+'_sex').value==j) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';	
					options += '<option value="'+j+'">'+j+'</option>';
				}
			}
			$("select[id='lunTrailerMt"+i+"']").html(options);	
			var lunCm = parseInt((arrayMisure[6] - parseInt(arrayMisure[6]))*100);
			$("select[id='lunTrailerCm"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">cm.</option>';							
			for(var j=0; j<10; j++){				
				if(checkTra==0){
					if(j*10==lunCm)  options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}
				else{
					if(document.getElementById('lunTrailerCm'+i+'_sex').value==j*10) options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';	
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}
			}
			$("select[id='lunTrailerCm"+i+"']").html(options);	
			// altezza auto
			$("select[id='altTrailerMt"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">mt.</option>';				
			for(var j=parseInt(arrayMisure[2]); j<=parseInt(arrayMisure[5]); j++){				
				if(checkTra==0){
					if(j==parseInt(arrayMisure[8])) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
				}				
				else {
					if(document.getElementById('altTrailerMt'+i+'_sex').value==j) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';	
					else options += '<option value="'+j+'">'+j+'</option>';					
				}
			}
			$("select[id='altTrailerMt"+i+"']").html(options);				
			var altCm = parseInt((arrayMisure[8] - parseInt(arrayMisure[8]))*100);
			$("select[id='altTrailerCm"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">cm.</option>';							
			for(var j=0; j<10; j++){				
				if(checkTra==0){
					if(j*10==altCm)  options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}
				else{
					if(document.getElementById('altTrailerCm'+i+'_sex').value==j*10) options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';	
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}				
			}
			$("select[id='altTrailerCm"+i+"']").html(options);	
			// larghezza auto
			$("select[id='larTrailerMt"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">mt.</option>';				
			for(var j=parseInt(arrayMisure[1]); j<=parseInt(arrayMisure[4]); j++){				
				if(checkTra==0){
					if(j==parseInt(arrayMisure[7])) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';
					else options += '<option value="'+j+'">'+j+'</option>';
				}
				else {
					if(document.getElementById('larTrailerMt'+i+'_sex').value==j) options += '<option value="'+j+'"  selected="selected">'+j+'</option>';	
					else options += '<option value="'+j+'">'+j+'</option>';
				}
			}
			$("select[id='larTrailerMt"+i+"']").html(options);			
			var larCm = parseInt((arrayMisure[7] - parseInt(arrayMisure[7]))*100);
			$("select[id='larTrailerCm"+i+"']").empty();
			var options = '<option value="default" class="default" id="default">cm.</option>';							
			for(var j=0; j<10; j++){				
				if(checkTra==0){
					if(j*10==larCm)  options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}
				else{
					if(document.getElementById('larTrailerCm'+i+'_sex').value==j*10) options += '<option value="'+j*10+'"  selected="selected">'+j*10+'</option>';	
					else options += '<option value="'+j*10+'">'+j*10+'</option>';
				}								
			}
			$("select[id='larTrailerCm"+i+"']").html(options);	
	});			
	setAutoTrailer(value,i);
}


function check(val, valAlert){
	if(val.checked) alert(valAlert);
	}

function checkDisabili(val, valAlert){
	if(val.checked) alert(valAlert);
	}
	
// Abilita/Disabilita Data ritorno
function enableData()
{	
	if(document.getElementById('ritorno').checked==true){
		isRitorno=true;		
    	document.getElementById('end_date').style.backgroundColor = "#FFF";
		document.getElementById('end_date').disabled=false;
		$('#end_date').dpSetDisabled(0);
		$('#end_date').datePicker('Enable'); 
		document.getElementById('ar').value="true";
		document.getElementById('infoCkAR').style.display="none";				
	}
	else{
		isRitorno=false;
		document.getElementById('end_date').style.backgroundColor = "#DFDFDF";
		document.getElementById('end_date').disabled=true;			
		$('#end_date').val('');
		$('#end_date').dpSetDisabled(1);
		$('#end_date').datePicker('Disable');  	
		document.getElementById('ar').value="false";
		document.getElementById('infoCkAR').style.display="inline";
	}	
}

function GetXmlHttpObject0()
		{
   	   var xmlHttp=null;
   		 try
   		 {
       		// Firefox, Opera 8.0+, Safari
      		xmlHttp=new XMLHttpRequest();
   		 }
   		 catch (e)
   		 {
       		//Internet Explorer
      		try
      		{
         	 	 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      		}
      		catch (e)
      		{
         	 	 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}
   		 }
   		 return xmlHttp;
		}

// Data: datepicker functions
	$(function()
            {															
				$('#start_date').bind(				
					'dpClosed',
					function(e, selectedDates)
					{
						var d = selectedDates[0];
						if (d) {
							d = new Date(d);							
							if(isset(document.getElementById('start_date_sex'))==0)
								$('#end_date').dpSetStartDate(d.addDays(0).asString());													
						}
					}
				);
			$('.date-pick').datePicker({clickInput:true});				
				$('#end_date').bind(
					'dpClosed',
					function(e, selectedDates)
					{
						var d = selectedDates[0];
						if (d) {
							d = new Date(d);							
							$('#start_date').dpSetEndDate(d.addDays(0).asString());
						}
					}
				);												 
            });
	
	$(function(){									
		if(reload==0){								
			$('#end_date').val('');	
			$('#end_date').dpSetDisabled(1);
			$('#end_date').datePicker('Disable'); 		
		}			
		else {							
			if(isset(document.getElementById('end_date_sex'))==0){					
				$('#end_date').val('');	
				$('#end_date').dpSetDisabled(1);
				$('#end_date').datePicker('Disable'); 		
			}
			else $('#start_date').dpSetEndDate(document.getElementById('end_date_sex').value);				
			$('#end_date').dpSetStartDate(document.getElementById('start_date_sex').value);							
		}
	});
	
$(function()
{
	$('.date-pick').datePicker({clickInput:true})
});

