var MAXPAX=8;
var MAXPAXVOLO=4;

function childAgeCombo(num,id)
{
    /*if(num > 0)
    {
        $(id).innerHTML='';
        for(var i=0; i < num; i++)
        {
            var copyAgeCombo = $('ageChild').innerHTML;                               
            var copyAgeCombo2=copyAgeCombo.replace('ageChild_XXX','ageChild_'+(i+1));
            $(id).innerHTML+=copyAgeCombo2.replace('ageChild_YYY','ageChild_'+(i+1));
            
            $(id).show();
        }
    }
    else
        $(id).innerHTML='';
        */

    for(var i=1; i<=2; i++)
      if($('ageChild'+i))
        $('ageChild'+i).hide();

    if(num>0)
    {        
        for(i=1; i<=num; i++)
            if($('ageChild'+i))
                $('ageChild'+i).show();
    }    
        
}



function childAgeComboImperatore(num,id)
{    
    var arrIdBox=Array('child_tripla','child_quadrupla');

    for(i=0;i<arrIdBox.length;i++)
    {
        if(arrIdBox[i]!=id && $(arrIdBox[i]))
            $(arrIdBox[i]).hide();
    }
        

    if(num>0)
    {
        $(id).show();
        //for(i=1; i<=num; i++)
            //if($('ageChild'+i))
                //$('ageChild'+i).show();
    }
    else    
        $(id).hide();
    
   //return true;
}//fine function childAgeComboImperatore(num,id)

function setComboAgeChildImperatore(offerta_id,index,tipo)
{
    var arrIdBox=Array('tripla','quadrupla');

    for(i=0;i<arrIdBox.length;i++)     
	if($('child_'+arrIdBox[i]))
        $('child_'+arrIdBox[i]).innerHTML='';
    

    if(index=='tripla' || index=='quadrupla')
    {
        voti = new Array();
        voti["tripla"] = 2;
        voti["quadrupla"] = 3;

        html='';
        html+="<div id='ageChild1' style='display: block;'>"+index+" con bambino <br /> ";
        for(i=1;i<voti[index];i++)
        {
            html+="Eta "+i+"  bambino: ";
            html+="<select name='ageChild_"+i+"' id='ageChild_"+i+"' onchange=\"calcolaPrezzoImperatore('"+offerta_id+"','"+tipo+"');\">";
            html+="<option value='0'>-Seleziona-</option>";
            for(a=2;a<=11;a++)
            {
                html+="<option value='"+a+"'>"+a+" anni</option>";
            }
            html+="</select><br />";
        }//fine for(i=1;i<=voti[index];i++)

        $('child_'+index).innerHTML=html;
        $('child_'+index).show();
    }
}//fine function setComboAgeChildImperatore(offerta_id,index,tipo)
 
function deselectRooms(id)
{
    var arrId=new Array('singola','doppia','tripla','quadrupla');
    for(var i=0;i<arrId.length; i++)
    {
        currentId=arrId[i];
        if($('room_'+currentId) && id!=currentId)
            {
                $('room_'+currentId).checked=false;
                $('nroom_'+currentId).options[0].selected=true;
                $('nroom_'+currentId).disabled=true;

                if($('child_'+currentId) && $('child_'+currentId).style.display!='none')
                    $('child_'+currentId).hide();                
            }
        if((currentId=='tripla' || currentId=='quadrupla') && $('child_'+currentId)) { $('child_'+currentId).innerHTML='';}
    }

    //if($('ageChild_1')) $('ageChild_1').options[0].selected=true;
    //if($('ageChild_2')) $('ageChild_2').options[0].selected=true;

    $('nroom_'+id).options[0].selected=true;
    $('nroom_'+id).disabled=false;
}//fine function deselectRooms(id)


var statoTg = true;
var stato365 = true;

function checkInsurance()
{
    if(stato365==true && $('assTG').checked==true && $('ass365').checked==false)
    {
        $('ass365').checked=false;
        $('assTG').checked=false;
        statoTg=false;
        stato365=false;
    }
    else
    if($('assTG').checked==true && statoTg==false)
    {
        $('ass365').checked=true;
        $('assTG').checked=true;
        statoTg=true;
        stato365=true;
    }
}



function calcolaPrezzo(id_offerta,tipo,pagina)
{
    var assTg=0;
    var ass365=0;

    if($('assTG'))
	var assTg = ($('assTG').checked == true) ? 1 : 0;
    if($('assTGCheck'))
            var assTg = $('assTGCheck').value==1 ? 1 : 0;
    if($('ass365'))
	var ass365 = ($('ass365').checked == true) ? 1 : 0;    
    if($('ass365Check'))
        var ass365 = $('ass365Check').value==1 ? 1 : 0;

    if($('offcode'))
        id_offerta=$('offcode').value;    

    tipo=tipo.toUpperCase();
    pagina = (pagina=='') ? 'dett' : pagina;
    if(tipo=='SOGG')
    {
        var indexPax = $('indexPax') ? $('indexPax').value : 1;
        var paxSogg = $('pax_sogg'+indexPax).value;
        var sistSogg = $('sist_sogg').value;
        $('pax_sogg').value=paxSogg;

        document.getElementById('offPrice').innerHTML='<img src=\"images/wait-anim.gif\" />';
        new Ajax.Updater('offPrice', 'make-price.php',
          {
            method: 'post',
            parameters:
            {
              id_offerta: id_offerta,
              paxSogg: paxSogg,
              indexPax: indexPax,
              assTg: assTg,
              ass365: ass365,
              sist_sogg: sistSogg,
              pagina: pagina,
              tipo: tipo
            }
          });          
     }
    else
    if(tipo=='PACC' || tipo=='CROC')
    {                
        var nDoppie = $('nroom_doppia') ? $('nroom_doppia').value : 1;
        var nTriple = $('nroom_tripla') ? $('nroom_tripla').value : 0;
        var chdAge1 = $('chdAge1') ? $('chdAge1').value : 0;
        var chdAge2 = $('chdAge2') ? $('chdAge2').value : 0;
        var ageChd1=$('ageChild_1') ? $('ageChild_1').value : 2;
        var ageChd2=$('ageChild_2') ? $('ageChild_2').value : 2;     
   
        /*var ageChildTriple='';
        for(var c=1; c<=nTriple; c++)            
          ageChildTriple+=parseInt($('ageChild_'+c).value)+'|';
          */

        document.getElementById('offPrice').innerHTML='<img src=\"images/wait-anim.gif\" />';
        new Ajax.Updater('offPrice', 'make-price.php',
          {
            method: 'post',
            parameters:
            {
              id_offerta: id_offerta,
              doppie: nDoppie,
              triple: nTriple,
              etaBambino1: ageChd1,
              etaBambino2: ageChd2,
              chdAge1: chdAge1,
              chdAge2: chdAge2,
              assTg: assTg,
              ass365: ass365,
              pagina: pagina,
              tipo: tipo
            }
          });          
     }
     else
     {
        var adt = $('adt').value;
        var chd = $('chd').value;
        var inf = $('inf').value;
        //var assTg = $('assTG').checked == true ? 1 : 0;
        //var ass365 = $('ass365').checked == true ? 1 : 0;

        document.getElementById('offPrice').innerHTML='<img src=\"images/wait-anim.gif\" />';
        new Ajax.Updater('offPrice', 'make-price.php',
          {
            method: 'post',
            parameters:
            {
              id_offerta: id_offerta,
              adt: adt,
              chd: chd,
              inf: inf,
              assTg: assTg,
              ass365: ass365,
              pagina: pagina,
              tipo: tipo
            }
          });
     }        
}//fine function calcolaPrezzo(id_offerta)



function calcolaPrezzoImperatore(id_offerta,tipo,pagina)
{
    var assTg=0;
    var ass365=0;

    if($('assTG'))
	var assTg = ($('assTG').checked == true) ? 1 : 0;
    if($('assTGCheck'))
            var assTg = $('assTGCheck').value==1 ? 1 : 0;
    if($('ass365'))
	var ass365 = ($('ass365').checked == true) ? 1 : 0;
    if($('ass365Check'))
        var ass365 = ($('ass365Check').checked == true) ? 1 : 0;

    if($('offcode'))
        id_offerta=$('offcode').value;    

    tipo=tipo.toUpperCase();
    pagina = (pagina=='') ? 'dett' : pagina;
    
    if(tipo=='PACC' || tipo=='CROC')
    {
        var nSingole = $('nroom_singola') ? $('nroom_singola').value : 0;
        var nDoppie = $('nroom_doppia') ? $('nroom_doppia').value : 1;
        var nTriple = $('nroom_tripla') ? $('nroom_tripla').value : 0;
        var nQuadruple = $('nroom_quadrupla') ? $('nroom_quadrupla').value : 0;
        var chdAge1 = $('chdAge1') ? $('chdAge1').value : 0;
        var chdAge2 = $('chdAge2') ? $('chdAge2').value : 0;
        var ageChd1=$('ageChild_1') ? $('ageChild_1').value : 2;
        //if(ageChd1 && nQuadruple>0) ageChd1=2;
        var ageChd2=$('ageChild_2') ? $('ageChild_2').value : 2;        
        var priceAdt=$('impPriceAdt') ? $('impPriceAdt').value : 2;

        var codiceSingola=nSingole>0 ? 'SG' : '';
        var codiceDoppia=nDoppie>0 ? 'DO' : '';
        var codiceTripla=nTriple>0 ? 'TR' : '';
        var codiceQuadrupla=nQuadruple>0 ? 'QU' : '';

        /*var ageChildTriple='';
        for(var c=1; c<=nTriple; c++)
          ageChildTriple+=parseInt($('ageChild_'+c).value)+'|';

        //adtPrice: priceAdt,
          */

        document.getElementById('offPrice').innerHTML='<img src=\"images/wait-anim.gif\" />';
        new Ajax.Updater('offPrice', 'make-price-imperatore.php',
          {
            method: 'post',
            parameters:
            {
              id_offerta: id_offerta,
              singole: nSingole,
              doppie: nDoppie,
              triple: nTriple,
              quadruple: nQuadruple,
              codiceSingola: codiceSingola,
              codiceDoppia: codiceDoppia,
              codiceTripla: codiceTripla,
              codiceQuadrupla: codiceQuadrupla,
              etaBambino1: ageChd1,
              etaBambino2: ageChd2,
              chdAge1: chdAge1,
              chdAge2: chdAge2,
              assTg: assTg,
              ass365: ass365,
              pagina: pagina,              
              tipo: tipo
            }
          });
     }     
}//fine function calcolaPrezzoImperatore(id_offerta)



function showComboRoomNumber(roomType)
{
    var idCombo= 'nroom_'+roomType;
    var idCheck = 'room_'+roomType;
    if($(idCheck).checked)
    {
        $(idCombo).disabled=false;
        //$(idCombo).options[1].selected=true;
    }
    else
    {
        $(idCombo).disabled=true;
        $(idCombo).options[0].selected=true;
        $('child_'+roomType).innerHTML = '';
    }
}


function changeImage(obj,ind)
{
    var srcSmall = obj.src;
    var srcBig = $('bigImg').src;

    $('bigImg').src = srcSmall.replace('small','huge');
    $('smallImg'+ind).src = srcBig.replace('huge','small');
}


function __getTotPax(tipo)
{    
    var totpax=0;
    if(tipo=='tras')
        totpax=parseInt($('adt').value)+parseInt($('chd').value)+parseInt($('inf').value);
    else
        if(tipo=='sogg')
            totpax=parseInt($('pax_sogg'));
        else
        {            
            //totpax=parseInt($('nroom_doppia').value)*2;
            if($('nroom_singola'))
                totpax+=parseInt($('nroom_singola').value)*1;
            if($('nroom_doppia'))
                totpax+=parseInt($('nroom_doppia').value)*2;
            if($('nroom_tripla'))
                totpax+=parseInt($('nroom_tripla').value)*3;
            if($('nroom_quadrupla'))
                totpax+=parseInt($('nroom_quadrupla').value)*4;
        }        

    return totpax;
}//fine function __getTotPax(tipo)

function checkDetailField(tipo)
{
    var totpax=0;
    if(tipo=='tras')
    {
        totpax=__getTotPax('tras');
        if(totpax==0)
        {
            alert('Selezionare almeno un passeggero')
            return false;
        }
    }
    else
    if(tipo=='sogg')
    {
        totpax=__getTotPax('sogg');
        if(totpax==0)
        {
            alert('Selezionare almeno un passeggero')
            return false;
        }
    }
    else
    {
        totpax=__getTotPax('pacc');
        if(totpax==0)
        {
            alert('Selezionare almeno una camera')
            return false;
        }
    }

    return true;
}


function checkTrasNumPax()
{
    var totNumPax=0;
    totNumPax=__getTotPax('tras');
    $('totPax').value=totNumPax;
    if(totNumPax > MAXPAXVOLO)
     {
        alert('Si e superato il numero massimo di passeggeri ('+MAXPAXVOLO+').');
        $('chd').options[0].selected=true;
        $('inf').options[0].selected=true;
     }
}


function checkNumPax()
{    
    var totNumPax=0;
    totNumPax=__getTotPax('pacc');
    $('totPax').value=totNumPax;    

    if(totNumPax > MAXPAX)
    {
        alert('Si e superato il numero massimo di passeggeri ('+MAXPAX+'). Scegliere un numero di camere inferiore');
        if($('room_doppia').checked==true)
            $('nroom_doppia').options[0].selected=true;
        if($('room_tripla').checked==true)
            $('nroom_tripla').options[0].selected=true;
    }//fine if(totNumPax > MAXPAX)
}//fine function checkNumPax()

function checkSoggNumPax()
{
    var totNumPax=0;
    totNumPax=__getTotPax('sogg');
    $('totPax').value=totNumPax;

    if(totNumPax > MAXPAX)
    {
        alert('Si e superato il numero massimo di passeggeri ('+MAXPAX+'). Scegliere un numero di passeggeri inferiore');
    }//fine if(totNumPax > MAXPAX)
}


function checkAgeChild(calendar)
{
    var minY=$('chdAge1').value>0 ? $('chdAge1').value : 0;
    var maxY=$('chdAge2').value>0 ? $('chdAge2').value : 10;
    
    if (calendar.dateClicked) {
      var id=calendar.params.inputField.id;
      var annoForm = calendar.date.getFullYear();
      var d = new Date();
      var annoCorrente= d.getFullYear();

      var eta=annoCorrente-annoForm;

      if(eta>=minY && eta<=maxY)
       {
            calendar.hide();
            return true;
       }
      else
        {
            calendar.hide();
            alert('l\'eta del bambino dev\'essere compresa tra '+minY+' e '+maxY);
            $(id).value='';
            return false;
        }
    }
}//fine function checkAgeChild(calendar)



//PAGINA ANAGRAFICA


function CheckPaxField(numPax)
{
    var arrCampi=new Array('namePax','surnamePax','agePax');
    var arrCampiTesto=new Array('il nome','il cognome','la data di nascita');

    var arrCampiReferente=new Array('namePax','surnamePax','agePax','addressPax','cityPax','provPax','capPax','codFiscPax','emailPax','mobilePax');
    var arrCampiTestoReferente=new Array('il nome','il cognome','la data di nascita','l\'indirizzo','la citta','la provincia','il cap','il codice fiscale','l\'email','il cellulare');
    for(var i=1; i<=numPax; i++)
    {
        if(i==1)
        {
            var arrFields=arrCampiReferente;
            var arrFieldsText=arrCampiTestoReferente;
        }
        else
        {
            var arrFields=arrCampi;
            var arrFieldsText=arrCampiTesto;
        }

        for(var c=0; c < arrFields.length; c++)
        {            
            if(arrFields[c] =='agePax' && $(arrFields[c]+''+i).value=='gg-mm-aaaa')
            {
                alert('Inserire una data di nascita corretta per il passeggero '+i);
                return false
            }

            if($(arrFields[c]+''+i).value=='')
            {
                alert('Inserire '+arrFieldsText[c]+' del passeggero '+i);
                return false
            }            
        }
    }//fine for(var i=0; i<numPax; i++)

    return true;
}//fine function CheckPaxField(numPax)

function ChekDataFormat(id)
{
    var validformat=/^\d{2}\-\d{2}\-\d{4}$/ //Basic check for format validity
    var returnval=false
    if (!validformat.test($(id).value))
    {
     $(id).style.className='default default_grey';
     $(id).value='gg-mm-aaaa';
     alert("Controllare il formato della data, esempio 03-03-2010");
    }
}

function setMaxPax(tipo,num)
{
    if(tipo=='tras' && num<MAXPAXVOLO)
        MAXPAXVOLO=num;
    else
       if(tipo!='tras' && num<MAXPAX)
            MAXPAX=num;
}

function obligatoryCheckbox(id,txtMsg)
{
    if($(id).checked!=true)
    {
        alert('L\'accettazione '+txtMsg+' e\' obbligatoria');
        $(id).checked=true;
    }

}



function getLocalities(dest)
{     
    document.getElementById('loc-ajax').innerHTML='<img src=\"../../images/wait-anim.gif\" />';
    new Ajax.Updater('loc-ajax', 'request-ajax.php',
      {
        method: 'post',
        parameters:
        {
          dest: dest,
          table: 'get-localities'
        }
      });
}


function getFotoLocalita(loc)
{    
    document.getElementById('foto-localita').innerHTML='<img src=\"../../images/wait-anim.gif\" />';
    new Ajax.Updater('foto-localita', 'request-ajax.php',
      {
        method: 'post',
        parameters:
        {
          dest: $('dest').value,
          loc: loc,
          table: 'get-foto-localita'
        }
      });
}

function delImage(path)
{
    dest=$('dest').value;
    loc=$('localita').value;    

    conferma=confirm('Sicuro di voler cancellare questa immagine?');
    if(conferma)
        location.href='edit-image.php?path='+path+'&send=del_image&dest='+dest+'&loc='+loc;    
}



function selectAipData(index)
{   
    var ad='datap_'+index;
    
    for(var i=0; i<$('numaipdata').value;i++)
    {
        $('datap_'+i).disabled=true;
        $('datap_'+i).options[0].selected=true;
    }


    $(ad).disabled=false;
    $(ad).options[1].selected=true;
}//fine function selectAipData(index)

function setDetailOffCode(offcode,index,tipo,to)
{
    if(offcode=='')
    {
        var ad='datap_'+index;
        $(ad).options[1].selected=true;
        offcode=$(ad).options[1].value;
    }
    
    $('offcode').value=offcode;
    $('offerta_id').value=offcode;

    if(tipo!='TRAS')
        GeneraSistemazione(offcode,to);
}//fine function setDetailOffCode(offcode,index)


function likeButtonFb()
{
    if($('offcode'))
        var offerta_id=$('offcode').value;
    var linkfb='http://www.lastminutetour.com/dettaglio_offerta.php?offerta_id='+offerta_id;    

    $('fb-button').innerHTML='';
    $('fb-button').innerHTML="<iframe src=\"http://www.facebook.com/widgets/like.php?href="+linkfb+"&font=tahoma&layout=button_count&locale=it_IT\" scrolling='no' frameborder='0' style='border:none; width:100px; height: 30px;' scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:100px; height: 30px; \" allowTransparency=\"true\"></iframe>";

}


function GeneraSistemazione(id_offerta,to)
{
    //if($('offcode')) id_offerta=$('offcode').value;

    //document.getElementById('boxSistemazione').innerHTML='<img src=\"images/wait-anim.gif\" />';
    var url='genera-sistemazione.php';
    if(to=='imperatore')
        url='genera-sistemazione-imperatore.php'

        new Ajax.Updater('boxSistemazione', url,
          {
            method: 'post',
            parameters:
            {
              id_offerta: id_offerta              
            }
          });               
}//fine function GeneraSistemazione(id_offerta)
