﻿
/*
===================================================================================================
Manage Search
===================================================================================================
*/
function AqtSearch(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18){
    this.txtFrom = p1;
    this.txtTo = p2;
    this.hdFrom = p3;
    this.hdTo = p4;
    this.lblErrorFrom = p5;
    this.lblErrorTo = p6;
    this.hdInfo = p7;
    this.txtStartDate = p8;
    this.txtReturnDate = p9;
    this.lblErrorStartDate = p10;
    this.lblErrorReturnDate = p11;
    this.ckDirectOnly = p12;
    this.imgLoad1 = p13;
    this.imgLoad2 = p14;
    this.Updatepanel = p15;
    this.CurrentDate = p16;
    this.txtTextStartDate = p17;
    this.txtTextReturnDate = p18;
}

function search_validateSearch(){
    var error=false;
    if ($get(this.hdFrom).value == ""){
        search_ShowError(this.lblErrorFrom, pleaseSelectFromLocation);
        error = true;
    }
    
    if ($get(this.txtFrom).value == ""){
        search_ShowError(this.lblErrorFrom, pleaseSelectFromLocation);
        error = true;
    } 
    
    if ($get(this.txtTo).value == ""){
        search_ShowError(this.lblErrorTo, pleaseSelectToLocation);
            error = true;
    }
    
    if ($get(this.hdTo).value == ""){
        search_ShowError(this.lblErrorTo, pleaseSelectToLocation);
        error = true;
    }

    if ($get(this.txtStartDate).value == "" || $get(this.txtStartDate).value == noDepartureDateSet) {
        search_ShowError(this.lblErrorStartDate, pleaseSelectDate);
        error = true; 
    }

    //Direct Flight
    var DirectFlightOnly = 0;
    if ($get(this.ckDirectOnly).checked)
        DirectFlightOnly = 1;

    if (!error)
        this.BuildGetSearch($get(this.hdFrom).value,$get(this.hdTo).value,$get(this.txtStartDate).value,$get(this.txtReturnDate).value,DirectFlightOnly ,this.hdInfo);
    
    return !error;
}

function search_ShowError(pTag,pMsg){
    document.getElementById(pTag).innerHTML = pMsg;
}

function search_FromCheckEvent(){
    if ($get(this.hdFrom).value == "")
        if ($get(this.txtFrom).value != ""){
            Innovata.WebServices.OpenAdsAffiliates.set_defaultUserContext(this);
            Innovata.WebServices.OpenAdsAffiliates.GetCodeTakeAChance($get(this.txtFrom).value,this.FromCheckEventAsync);
    }
}

function search_FromCheckEventAsync(arg,cont,e){
    if (arg){
        $get(cont.hdFrom).value = arg.split('~')[1];
        $get(cont.txtFrom).value =  arg.split('~')[0];
        cont.ShowError(cont.lblErrorFrom,'');   
    }
    else{
        $get(cont.hdFrom).value = '';
        $get(cont.txtFrom).value = '';
        cont.ShowError(cont.lblErrorFrom, pleaseSelectValidFromLocation);
    }
    cont.CloseWaiting();
}

function search_ToCheckEvent(){
    if ($get(this.hdTo).value == "")
        if ($get(this.txtTo).value != ""){
            Innovata.WebServices.OpenAdsAffiliates.set_defaultUserContext(this);
            Innovata.WebServices.OpenAdsAffiliates.GetCodeTakeAChance($get(this.txtTo).value,this.ToCheckEventAsync);
    }
}

function search_ToCheckEventAsync(arg,cont,e){
    if (arg){
        $get(cont.hdTo).value = arg.split('~')[1];
        $get(cont.txtTo).value =  arg.split('~')[0];
        cont.ShowError(cont.lblErrorTo,'');   
    }
    else{
        $get(cont.hdTo).value = '';
        $get(cont.txtTo).value = '';
        cont.ShowError(cont.lblErrorTo, pleaseSelectValidToLocation);
    }
    cont.CloseWaiting();
}

function search_CloseWaiting(){
    document.getElementById(this.imgLoad1).style.visibility = "hidden";
    document.getElementById(this.imgLoad2).style.visibility = "hidden";
}


function search_BuildGetSearch(pFrom,pTo,pStartDate,pReturnDate,pDirect,pHF){
    pFrom.replace("|","");
    pTo.replace("|", "");
    
    pReturnDate = (pReturnDate == noReturnDateSet) ? "" : pReturnDate;
    document.getElementById(pHF).value = pFrom+"|"+pTo+"|"+pStartDate+"|"+pReturnDate+"|"+pDirect;
    document.getElementById('__EVENTTARGET').value = "NewAqtSearch";
    
    if (this.Updatepanel != '')
        __doPostBack(this.Updatepanel,"");
}

function search_ClearAll(){
    $get(this.txtFrom).value = "";
    $get(this.txtTo).value = "";
    $get(this.hdFrom).value = "";
    $get(this.hdTo).value = "";
    this.ShowError(this.lblErrorFrom,"");
    this.ShowError(this.lblErrorTo,"");
    $get(this.hdInfo).value = "";
    this.ShowError(this.lblErrorStartDate,"");
    this.ShowError(this.lblErrorReturnDate,"");
    $get(this.ckDirectOnly).checked = false;
    $get(this.imgLoad1).style.visibility = "hidden";
    $get(this.imgLoad2).style.visibility = "hidden";
    
    
    //Non Quick Search
    if (this.Updatepanel == "") {
        //ne devrait jamais passer ici
        var dt = $find("CalendarStart").get_todaysDate();
        $find("CalendarStart").set_selectedDate(dt);
        $find("CalendarReturn").set_selectedDate(null);
        $find("CalendarStart")._switchMonth();
        $find("CalendarReturn")._switchMonth();
    }
    else {
        //alert(this.CurrentDate);
        //$("#" + this.txtTextStartDate).dpResetSelected(null);
//        d = new Date();
//        d.setFullYear(2008,10,19);

        d = Date.fromString(this.CurrentDate);
        $("#" + this.txtTextStartDate).dpSetSelected(d.asString());
        
        $get(this.txtStartDate).value = this.CurrentDate;
        $get(this.txtTextStartDate).value = Date.abbrDayNames[d.getDay()] + ", " + d.getDate() + ' ' + Date.monthNames[d.getMonth()] + ' ' + d.getFullYear();
    }

    $("#" + this.txtTextReturnDate).dpResetSelected(null);
    $get(this.txtReturnDate).value = noReturnDateSet;
    $get(this.txtTextReturnDate).value = noReturnDateSet;
}


function search_ClearAll_V2(){
    $get(this.txtFrom).value = "";
    $get(this.txtTo).value = "";
    $get(this.hdFrom).value = "";
    $get(this.hdTo).value = "";
    this.ShowError(this.lblErrorFrom,"");
    this.ShowError(this.lblErrorTo,"");
    $get(this.hdInfo).value = "";
    this.ShowError(this.lblErrorStartDate,"");
    this.ShowError(this.lblErrorReturnDate,"");
    $get(this.ckDirectOnly).checked = false;
    $get(this.imgLoad1).style.visibility = "hidden";
    $get(this.imgLoad2).style.visibility = "hidden";
    
    $("#middlecolumn-home .searchform .date-pickerTo").dpResetSelected(null);
    $("#middlecolumn-home .searchform .date-pickerFrom").dpResetSelected(null);
    
    //$("#middlecolumn-home .searchform .date-pickerFrom table.jCalendar td.today").removeClass('today');
    $("#middlecolumn-home .searchform .date-pickerTo table.jCalendar td.today").removeClass('today');

    //d = Date.fromString(this.CurrentDate);

    //$("#middlecolumn-home .searchform .date-pickerFrom").dpSetSelected(d.asString(),true,true);

//    $get(this.txtStartDate).value = d.localeFormat("dd/MM/yyyy");
//    $get(this.txtTextStartDate).value = Date.abbrDayNames[d.getDay()] + ", " + d.getDate() + ' ' + Date.monthNames[d.getMonth()] + ' ' + d.getFullYear();
    //
    $get(this.txtTextStartDate).value = noReturnDateSet;
    $get(this.txtStartDate).value = noReturnDateSet;

    $get(this.txtTextReturnDate).value = noReturnDateSet;
    $get(this.txtReturnDate).value = noReturnDateSet;
}

function search_FiredEvent(x){
    //Non Quick Search
    /*if (this.Updatepanel == "") {
        if (x == 1) {
            if ($find("CalendarReturn").get_selectedDate() != null && $find("CalendarReturn").get_selectedDate() < $find("CalendarStart").get_selectedDate()) {
                $find("CalendarReturn").set_selectedDate($find("CalendarStart").get_selectedDate());
                $find("CalendarReturn")._switchMonth();
            }
            else {
                $find("CalendarReturn")._switchMonth($find("CalendarStart").get_selectedDate(), true);
            }
        }
    }*/
}

AqtSearch.prototype.ValidateSearch = search_validateSearch;
AqtSearch.prototype.ShowError = search_ShowError;
AqtSearch.prototype.FromCheckEvent = search_FromCheckEvent;
AqtSearch.prototype.FromCheckEventAsync = search_FromCheckEventAsync;
AqtSearch.prototype.ToCheckEvent = search_ToCheckEvent;
AqtSearch.prototype.ToCheckEventAsync = search_ToCheckEventAsync;
AqtSearch.prototype.CloseWaiting = search_CloseWaiting;
AqtSearch.prototype.BuildGetSearch = search_BuildGetSearch;
AqtSearch.prototype.ClearAll = search_ClearAll;
AqtSearch.prototype.ClearAll_V2 = search_ClearAll_V2;
AqtSearch.prototype.FiredEvent = search_FiredEvent;