// SEARCHBOX LIBRARY - COPYRIGHT 2007-2009 DIGITAL TRIP LTD - ALL RIGHTS RESERVED
// NO PORTION OF THIS LIBRARY MAY BE COPIED WITHOUT PRIOR WRITTEN CONSENT
var searchCal;
function initSearchBox()
{
    new Ajax.Autocompleter('destination','divAutoSuggest','/Assets/Handlers/Ajax/AutoSuggestV4.ashx',{paramName:'q',minChars:2,frequency:0.5,callback:autoSuggestSearchBox,afterUpdateElement:autoSuggestUpdDest,indicator:'destinationSearching'});
    updateSearchCalendars();
    switchSearch(defaultSearchType);
    for(var r=1;r<=$('rms').selectedIndex+1;r++){
        var cc=$('chd'+r).selectedIndex;
        if(cc==0){
            objHide('chdRow'+r);
        }else{
            objShow('chdRow'+r);
            for(var i=1;i<=6;i++){
                if(i<=cc) objShow('liCA'+r+i);
                else objHide('liCA'+r+i);
            }
        }
    }
}
function getSearchType(){if(document.forms[0].st.length){for(i=0;i<document.forms[0].st.length;i++){if(document.forms[0].st[i].checked){return parseInt(document.forms[0].st[i].value,10);}}}else{return parseInt($('searchType').value,10);}}
function autoSuggestSearchBox(id,qry){$('destinationid').value='';return qry+'&t='+getSearchType();}
function autoSuggestUpdDest(text,li){$('destinationid').value=li.id;}
function updateSearchCalendars(){
    var dateFrom=$('fr');var dateTo=$('to');var dur=0;
    if(isDate(dateFrom.value,'d/M/y') && isDate(dateTo.value,'d/M/y')){
        dur=Math.round((parseDate(dateTo.value,1)-parseDate(dateFrom.value,1))/(24*60*60*1000));
        if(dur<0){alert('Please select a return date after your departure date!');dateTo.value=dateFrom.value;}
        $('frTxt').innerHTML=formatDate(parseDate(dateFrom.value,1),'E, dd NNN yyyy');
        $('toTxt').innerHTML=formatDate(parseDate(dateTo.value,1),'E, dd NNN yyyy');
    }
    $('duration').innerHTML=(dur>0?dur:'0');
}
function clickCalendar(fId){searchCal.setTarget(fId);}
function updateSearchVilla(txt,val,name){$('destinationid').value=val;$('destination').value=txt;$('accname').value=name;}
function submitTravelSearch(fid){
    var errMsg='';
    var srchTyp=getSearchType();
	if(srchTyp==4){
        if($('carPickup').selectedIndex==0) errMsg+='\n- Enter a destination';
    }else if(srchTyp==5){
        if($('destinations').selectedIndex==0) errMsg+='\n- Enter a destination';
    }else if(srchTyp==6){
        if($('txresort').selectedIndex==0) errMsg+='\n- Enter enter your destination';
    }else{
        if($('destination').value=='' || $('destination').value=='Please enter your destination here...') errMsg+='\n- Enter a destination';
    }
    if($('fr').value=='' || !isDate($('fr').value,'d/M/y')) errMsg+='\n- Enter a valid departure date';
    if(srchTyp==2){
        if(!$('typ1').checked && !$('typ2').checked && !$('typ3').checked) errMsg+='\n- Select which type of properties you would like to search';
    }
    if(srchTyp==2 || srchTyp==3 || srchTyp==4){
        if($('duration').innerHTML=='0') errMsg+='\n - Select a duration greater than 0 days';
    }
    if(errMsg==''){
        if((srchTyp==1||srchTyp==2||srchTyp==3) && $('destinationid').value==''){
            // Check destination
            new Ajax.Request('/Assets/Handlers/Ajax/AutoSuggestV4.ashx',{method:'get',parameters:'ajax=1&t='+srchTyp+'&q='+escape($('destination').value),onSuccess:function(transport){
                var json=transport.responseText.evalJSON();
                if(json.matched){
                    if(json.res.length==1){
                        $('destinationid').value=json.res[0].id; document.forms[0].action='/Assets/Handlers/SearchHandler.ashx';document.forms[0].submit();
                    }else{
                        var em='';
                        var h='<p class="pSelect">We found <strong>'+json.res.length+'</strong> destinations that match your search.</p>';
                        h+='<div id="matchedDestinations" style="padding:20px;overflow:auto;height:220px;">';
                        h+='<ul>';
                        for(var i=0;i<json.res.length;i++){
                            if(json.res[i].rank==1) em=json.res[i].id;
                            h+='<li class="box4"><a href="#" onclick="$(\'destinationid\').value=\''+json.res[i].id+'\';document.forms[0].action=\'/Assets/Handlers/SearchHandler.ashx\';document.forms[0].submit();">'+json.res[i].name+(json.res[i].code=='' || srchTyp==3?'':' ('+json.res[i].code+')')+', '+json.res[i].ctry+'</a></li>';
                        }
                        h+='</ul>';
                        h+='</div>';
                        if(em==''){
                            destHelperDraw(h);
                        }else{
                            $('destinationid').value=em; document.forms[0].action='/Assets/Handlers/SearchHandler.ashx';document.forms[0].submit();
                        }
                    }
                }else{
                    var h='<p class="pSelect">Select a country:&nbsp;&nbsp;<select id="destinationSelect" onchange="destHelperPopulate('+srchTyp+',this[this.selectedIndex].value);"><option value="">- Select a country -</option>';
                    for(var i=0;i<json.res.length;i++){
                        h+='<option value="'+json.res[i].id+'">'+json.res[i].name+'</option>';
                    }
                    h+='</select></p><div id="divDestHelperOpts" style="height:220px;overflow:auto;padding:20px">';
                    h+='<div id="divDestNoResults">';
                    h+='<p>We are unable to find the destination you entered.</p>';
                    h+='<p>Please select a country from above to see a list of alphabetical destinations.</p>';
                    h+='</div></div>';
                    destHelperDraw(h);
                }
            }});
            return false;
        }else{
            fid.value='searching';document.forms[0].action='/Assets/Handlers/SearchHandler.ashx';return true;
        }
    }else{
        fid.value='search';alert('Please correct the following:'+errMsg);return false;
    }
}
function destHelperPopulate(t,id){
    if(id!=''){
        objHide('divDestNoResults');
        new Ajax.Request('/Assets/Handlers/Ajax/AutoSuggestV4.ashx',{method:'get',parameters:'ajax=1&t=7&q=xxx&ctry='+escape(id),onSuccess:function(transport){
            var json=transport.responseText.evalJSON();
            var select_destination = document.getElementById('destinationSelect').selectedIndex;
            var selected_destination = document.getElementById('destinationSelect').options[select_destination].text;

            var h='<span>Please select an airport in '+selected_destination+'</span>';
            h+='<ul id="ulDestHelper">';
            for(var i=0;i<json.res.length;i++){
                
                h+='<li class="box4"><a href="#" onclick="$(\'destinationid\').value=\''+json.res[i].code+'\';document.forms[0].action=\'/Assets/Handlers/SearchHandler.ashx\';document.forms[0].submit();">'+json.res[i].name+(json.res[i].code==''?'':' ('+json.res[i].code+')')+'</a></li>';
            }
            h+='</ul>';
            var objHelperOpts=document.getElementById('divDestHelperOpts');
            objHelperOpts.innerHTML=h;
        }});
    }
}
function destHelperDraw(h){
    h='<span class="fR"><a href="#" onclick="objHide(\'divDestHelper\');objShow(\'ulTSB\')"><a href="#" onclick="objHide(\'divDestHelper\');objShow(\'ulTSB\')">Close</a>&nbsp;&nbsp;<img src="/assets/images/icons/cross.gif" alt="Close" title="Close" style="padding:4px 8px 0px 0px;" /></a></span>'+h;
    var objHelper=document.getElementById('divDestHelper'); objHelper.innerHTML=h; objShow('divDestHelper'); objHide('ulTSB');
}
function changeDepartPoint(fId,selId){
    if(selId==1 && fId[fId.selectedIndex].value==''){objShow('devDepartFromOther');objHide('divDepartFromUk');$('origin').focus();}
    else{
        objShow('divDepartFromUk');objHide('devDepartFromOther');
        var origin1=$('origin1');var origin2=$('origin2');
        if(selId==1)buildDepartPoints(origin1,origin2);
        if(selId==2)buildDepartPoints(origin2,origin3);
    }
}
function buildDepartPoints(boxFr,boxTo){
    if(boxFr[boxFr.selectedIndex].value!=''){
        boxTo.options.length=0; addDDLOption(boxTo,'- Optional -','');
        for(var i=0;i<boxFr.options.length;i++)
        { if(boxFr.selectedIndex!=i && boxFr.options[i].value!='' && boxFr.options[i].value!='0') addDDLOption(boxTo,boxFr.options[i].text,boxFr.options[i].value); }
        boxTo.disabled=false;
    }
    boxFr.disabled=false;
}
function changeRooms(fId){
    if(fId.selectedIndex>=1){objShow('paxRow2');}else{objHide('paxRow2');objHide('chdRow2');}
    if(fId.selectedIndex>=2){objShow('paxRow3');}else{objHide('paxRow3');objHide('chdRow3');}
}
function changeCarCountry(fId){
    var cc=fId[fId.selectedIndex].value;var cp=$('carPickup');
    cp.options.length=0;addDDLOption(cp,'- Select pickup point -','');
    if(cc!=''){
        for(var i=0;i<carPickups.length;i++){
            if(carPickups[i][0]==cc){
                for(var j=1;j<carPickups[i].length;j=j+2){
                    addDDLOption(cp,carPickups[i][j+1],carPickups[i][j]);
                }
            }
        }
    }
}
function changeTxCountry(fId){
    var cc=fId[fId.selectedIndex].value;var cp=$('txdestination');
    cp.options.length=0;addDDLOption(cp,'- Select destination -','');
    if(cc!=''){
        for(var i=0;i<txDests.length;i++){
            if(txDests[i][0]==cc){
                for(var j=1;j<txDests[i].length;j=j+3){
                    addDDLOption(cp,txDests[i][j+1],txDests[i][j]);
                }
            }
        }
    }
}
function changeTxDest(fId){
    var cc=fId[fId.selectedIndex].value;var cp=$('txresort');
    cp.options.length=0;addDDLOption(cp,'- Select resort -','');
    if(cc!=''){
        for(var i=0;i<txDests.length;i++){
            for(var y=1;y<txDests[i].length;y=y+3){
                if(txDests[i][y]==cc){
                    for(var j=0;j<txDests[i][y+2].length;j=j+2){
                        addDDLOption(cp,txDests[i][y+2][j+1],txDests[i][y+2][j]);
                    }
                }
            }
        }
    }

}
function checkChildAges(id){
    var ok=true;
    var cnt=parseInt($('chd'+id).value);
    if(cnt>0){
        for(var i=1;i<=cnt;i++){
            if($('ca'+id+i).selectedIndex==0) ok=false;
        }
    }
    return ok;
}
function showChildAges(rid,c){
    if(c.selectedIndex==0){
        objHide('chdRow'+rid);
    }else{
        objShow('chdRow'+rid);
        for(var i=1;i<=6;i++){
            if(i<=c.selectedIndex) objShow('liCA'+rid+i);
            else objHide('liCA'+rid+i);
        }
    }
}
function switchSearch(s){
    defaultSearchType=s; //Stops slow loading of page switching page back to default
    if($('st'+s)){
        $('st'+s).checked=true;
        $$('li.on').each(function(f){f.removeClassName('on');f.addClassName('off');});
        $('srchTab'+s).addClassName('on');
    }
    objHide('paxRow2');objHide('paxRow3');  
    if(queryString('rms')!=null){$('rms').selectedIndex=parseInt(queryString('rms'),10)-1;changeRooms($('rms'));}else{$('rms').selectedIndex=0;}
    var sbItemCount=14;var sbItemArr;
    switch(s){
    case 2:
        sbItemArr=[3,5,8,10]; break; //AO
    case 3:
        sbItemArr=[1,3,5,8,9]; break; //F+H
    case 4:
        sbItemArr=[2,6,11]; objHide('chdRow1'); objHide('chdRow2'); objHide('chdRow3'); $('txtSbPickup').innerHTML='Pickup'; $('txtSbDropoff').innerHTML='Dropoff'; break; //CO
    case 5:
        sbItemArr=[1,4,5,8,9]; break; //PH
    case 6:
        sbItemArr=[6,8,13,14]; $('txtSbPickup').innerHTML='Arrive'; $('txtSbDropoff').innerHTML='Return'; break; //TX
    default:
        sbItemArr=[1,3,7,8,9,12]; break; //FO
    }
    for(var i=1;i<=sbItemCount;i++){
        var fnd=false;
        for(var j=0;j<sbItemArr.length;j++){
            if(sbItemArr[j]==i){fnd=true;break;}
        }
        if(fnd){objShow('tsbLi'+i);}else{objHide('tsbLi'+i);}
    }
}
function addDDLOption(fId,txt,val){var opt=document.createElement('option');opt.text=txt;opt.value=val;try{fId.add(opt,null);}catch(ex){fId.add(opt);}}
SafeAddOnload(initSearchBox);
