﻿//This script handles Events when beginning and ending a partial postback

//** Important: the wiring of the InitializeRequest and EndRequest methods is done 
//** in the master pages: ~/Tripplanner.master and ~/Tripplanner/TripplannerHome.master, right after the scriptmanager tag.

var postbackelementid;
function InitializeRequest(sender, args)
{
    //The postbackelementid refers to the asp webcontrol which is clicked or triggered.
    postbackelementid = args.get_postBackElement().id;
    if (IsValidGoogleMapCategory(postbackelementid))
    {
	 	LoadVisibleItems();
    }

} 

function EndRequest(sender, args)
{
 //   alert(postbackelementid);

    //Stops the preloader of the Google maps.
    if (IsValidGoogleMapCategory(postbackelementid))
    {
        StopPreloader();
    }
    
    if (TripItemAdded(postbackelementid)){
        ShowModalMessage(GetAddedMessage());
        //updates the updatepanel in ~/_usercontrols/MyTrip/MyTripSummary.ascx usercontrol.
        //setTimeout("__doPostBack(SearchWebControlGetID('upNavRightMyTrip'), '');", 500);
        setTimeout("__doPostBack($('.sidebar-mytrip-pane').parent().attr('id'), '');", 500);
    }
    
    //A new function Alert New Suggestion should be created here.
    if(SuggestionCreated(postbackelementid)){
        ShowModalMessage(SuggestionSelected());
         setTimeout("__doPostBack(SearchWebControlGetID('upNavRightMyTrip'), '');", 500);
    }

}

/*** Functions related to ~/_usercontrols/GoogleMaps/GoogleMap.ascx ***/
function IsValidGoogleMapCategory(controlid)
{
    //the chk* are checkboxes located next to the google map.
    return ((controlid.indexOf("chkHotel") >= 0) || (controlid.indexOf("chkResto") >= 0) ||
            (controlid.indexOf("chkShop") >= 0) || (controlid.indexOf("chkDiscover") >= 0) ||
            (controlid.indexOf("chkParty") >= 0) || (controlid.indexOf("chkMustSee") >= 0))
}

function StopPreloader()
{
    if (($("#GMapPreloader") != null) && ($("#GMapPreloader").css("display") == "block"))
        $("#GMapPreloader").css("display", "none");
}

/*** Functions related to ~/_usercontrols/Discover/MostViewedTripItems.ascx ***/
function TripItemAdded(controlid)
{
    //Button Add To TripItem should be called 'btnAddTripItem' so the modalpopup can be triggered...
      return ((controlid.indexOf("btnAddTripItem") >= 0)|| (controlid.indexOf("cmdGetTripFromSuggestion") >= 0)) 
}
function SuggestionCreated(controlid){
    return  ((controlid.indexOf("cmdGetTripFromSuggestion") >= 0)) 
}

function GetAddedMessage()
{
    if ($.jqURL.strip().indexOf("/nl-") > 0) {
        return "Dit item is aan je trip toegevoegd.";
    }
    if ($.jqURL.strip().indexOf("/en-") > 0) {
        return "This item has been added<br />to your trip.";
    }
    if ($.jqURL.strip().indexOf("/fr-") > 0) {
        return "Ajouté à votre séjour!";
    }

}
function SuggestionSelected(){
    if ($.jqURL.strip().indexOf("/nl-") > 0 )
        return "Als je deze tripsuggestie toevoegt, creëer je automatisch een nieuwe trip.";
    if ($.jqURL.strip().indexOf("/en-") > 0)
        return "We warn you that by adding this trip suggestion a new trip will automatically be created";
    if ($.jqURL.strip().indexOf("/fr-") > 0)
        return "Nous vous avertissons qu' ajouter cette suggestion créera automatiquement un nouveau séjour.";
}