var busy = false;

// For MouseOver/Out on Add To Trip links, when loggedout
var titleAttr; var altAttr;

// Show AddToTrip overlays after user signs in via "Add To My Trip" link
function addToTripOnReload(id){
	// check to make sure they are still logged in
	if (typeof (tpVars) == 'undefined') {
		return;
	} else if (typeof (tpVars['userid']) == 'undefined') {
		return;
	} else {
		var theLink = $(".addToTrip[rel^="+id+"]");
		addToTrip(theLink);
	}
}
//addToTripOnReload(49802)

// Is Add To My Trip already engaged?
var addToTripOpen = 0;

$(document).ready(function() {
    
	
	// Prep any "Remove My Trip" buttons in sidebar
    removeIten();

    // Trip Planner: Add To My Trip
    //=============================
    $(".trip").each(function() {
        var trip = $(this).html();
        //console.info(trip)
    });

    
    $(".addToTrip").each(function() {

        // Add loggedout status to all Add To Trip links
        if (typeof (tpVars) == 'undefined') {
            $(this).addClass("loggedout");
        } else if (typeof (tpVars['userid']) == 'undefined') {
            $(this).addClass("loggedout");
        }
		
		// If the user is logged out...
		if ($(this).attr('class').indexOf("loggedout") != -1) {
			// Show tooltip and allow for easy signin
			$(this)
				.attr('title','Must be logged in to add to your trip.<br />Click to sign in.')
				.tooltip({
					track: true,
					delay: 0,
					showURL: false,
					showBody: " - ",
					fade: 250
				})
				.mouseover(function(){
					// Hide Title/Alt text
					titleAttr = $('img', this).attr('title');
					altAttr = $('img', this).attr('alt');
					$('img', this).attr('title','').attr('alt','');
				})
				.mouseout(function(){
					// Restore Title/Alt text
					$('img', this).attr('title',titleAttr);
					$('img', this).attr('alt',altAttr);
					titleAttr = altAttr = '';
				})
				.click(function(e){
					e.preventDefault();
					$("#signinBox").addClass("fixedPos");
					var val = $(this).attr('rel').substring(0,$(this).attr('rel').indexOf('|'));
					$("#addToTripID").attr("value",val);
					$.positionDiv("#signinBox");
					$("#signin").click();
				}
			);
		}


        // If the user is NOT logged out...
        if ($(this).attr('class').indexOf("loggedout") == -1) {
            $(this).click(function(e) {
                e.preventDefault();
                addToTrip($(this)); // See function down below
            });
        }
    });
	
	

    $(".addNewTrip").each(function() {
        $(this).click(function(e) {
            e.preventDefault();


            // Open Dialog box to get user's custom name for new trip
            if (addToTripOpen == 0) {
                // Create Dialog Box
                var tpDIV = $("<div/>").attr("id", "tpDialog").attr("class", "overlayA");
                $("body").append(tpDIV);
                $("#tpDialog").addClass("addToTrip");

                //Create Close button for DIV
                /*$("#tpDialog").append("<span id='overlayClose'><a href='#' title='Close'></a></span>");
                $("#overlayClose a").click(function(e) {
                e.preventDefault();
                $("#tpDialog").fadeOut("normal", function() {
                $("#tpDialog").remove();
                addToTripOpen = 0;
                });
                });*/

                // Populate Dialog Box
                var contents = ' \
					<div class="ol_top"></div> \
						<div class="ol_mid"> \
							<h2>Add New Itinerary</h2> \
							<fieldset> \
								<label for="tripName" style="font-size:1.2em;">Name your new trip itinerary: &nbsp;&nbsp;</label> \
								<input type="text" id="tripName" maxlength="16" /> \
							</fieldset> \
							<p><br /> \
								<a href="#" class="addNewTrip"><img src="images/overlay_button_addNewItinerary.gif" width="111" height="33" border="0" alt="Add New Itinerary" title="Add New Itinerary" /></a> \
								<a href="#" class="tpCancelButton"><img src="images/overlay_button_cancel.gif" width="97" height="33" border="0" alt="Cancel" title="Cancel" /></a> \
							</p> \
						</div> \
						<div class="ol_bot"></div>'
                $("#tpDialog").append(contents);


                // Handle Cancel Button
                $("#tpDialog .tpCancelButton").click(function(e) {
                    e.preventDefault();
                    if ($.browser.msie) {
                        $("#tpDialog").remove();
                        addToTripOpen = 0;
                    } else {
                        $("#tpDialog").fadeOut("normal", function() {
                            $("#tpDialog").remove();
                            addToTripOpen = 0;
                        });
                    }
                });


                // Position Dialog Box
                $.positionDiv("#tpDialog")



                $(".addNewTrip").click(function(e) {
                    e.preventDefault();

                    if ($("#tripName").val() != "") {

                        // Show spinner
                        $("#tripView").fadeOut("fast", function() {
                            $("#tripView").html('<img src="images/sidebarSpinner.gif" alt="" width="16" height="16" border="0" id="tpAddButtonLoader" />');
                        });
                        $("#tripView").fadeIn("normal");

                        busy = true;

                        $.addNewTrip($("#tripName").val());
                        $("#tpDialog").fadeOut("fast", function() {
                            $("#tpDialog").remove();
                        });
                    }
                });


                // Show Dialog Box
                $("#tpDialog").fadeIn(function() {
                    $("#tripName").focus();
                });


            }





        });
    });


    $.addNewTrip = function(name) {
        // SOAP Setup
        var soapEnv =
				'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
				  <soap:Body>\
					<AddItinerary xmlns="http://traveltex.com/">\
					  <userID>' + tpVars['userid'] + '</userID>\
					  <name>' + name + '</name>\
					  <view>' + tpVars['view'] + '</view>\
					</AddItinerary>\
				  </soap:Body>\
				</soap:Envelope>'

        // AJAX Call
        $.ajax({
            url: SLAddItinerary,
            type: "POST",
            data: soapEnv,
            dataType: "xml",

            error: function(event, XMLHttpRequest, ajaxOptions, thrownError) {
                //alert(XMLHttpRequest.status);
            },

            complete: RedrawItinerary,

            contentType: "text/xml; charset=\"utf-8\""
        });
    }






    $(".openPrintTrip").each(function() {
        $(this).click(function(e) {
            e.preventDefault();
            window.open("/print-itinerary/" + tpVars['tripid'] + "?id=" + tpVars['userid']);
        });
    });

    // Assign events to view toggle buttons
    AssignToggleEvents();
    // Assign change event to TripList
    AssignTripListChangeEvent($(".tripList"));
    // Attempting to fix disappearing sidebar itinerary
    if (tpVars['tripid']!=null) RefreshSidebar();

});   //End jQuery function onload *************************




function addToTrip(link){
		
	// Check if this is a link that clones an ID
	// Was used for Howdy Neighbor but then was not used
	var classes = link.attr("class").split(" ");
	var cloneID = false;
	var isLodging = false;
	for(var c=0; c<classes.length; c++){
		if(classes[c] == "cloneID") cloneID = true;
		if(classes[c] == "lodging") isLodging = true;
	}
	
	var _tid = link.attr("rel").split('|')[0];
	var _name = link.attr("rel").split('|')[1];
	
	
	if (addToTripOpen == 0) {
		// Create Dialog Box
		var tpDIV = $("<div/>").attr("id", "tpDialog").attr("class", "overlayA");
		$("body").append(tpDIV);
		$("#tpDialog").addClass("addToTrip");

		//Create Close button for DIV
		/*$("#tpDialog").append("<span id='overlayClose'><a href='#' title='Close'></a></span>");
		$("#overlayClose a").click(function(e) {
		e.preventDefault();
		$("#tpDialog").fadeOut("normal", function() {
		$("#tpDialog").remove();
		addToTripOpen = 0;
		});
		});*/

		// Populate Dialog Box
		// First if for normal Add to Trip link, Second for cloneID link
		if (cloneID == false) {
			var contents = ' \
				<div class="ol_top"></div> \
				<div class="ol_mid"> \
					<h2>Add To My Trip</h2> \
					<h3>' + link.attr("rel").split('|')[1] + '</h3> \
					<p>to <span class="tripName">' + tpVars['tripname'] + '</span></p> \
					<fieldset style="background:#f5f5f5; padding:15px 0;"> \
						<input type="hidden" value="' + link.attr("rel") + '" id="eventID" /> \
						<label for="tripDay" class="hidden">On what day?</label> \
						<select id="tripDay"> \
					 \
						</select><br /> \
						<span class="errorTxt" style="text-align:center; font-size:1.1em; color:#f00;"></span> \
					</fieldset> \
					<p><br /> \
					<a href="#" class="tpAddButton"><img src="images/overlay_button_addToTrip.gif" width="97" height="33" border="0" alt="Add to Trip" title="Add to Trip" /></a> \
					<a href="#" class="tpCancelButton"><img src="images/overlay_button_cancel.gif" width="97" height="33" border="0" alt="Cancel" title="Cancel" /></a> \
					</p> \
				</div> \
				<div class="ol_bot"></div>'
		} else {
			var contents = ' \
				<div class="ol_top"></div> \
				<div class="ol_mid"> \
					<h2>Add To My Trip</h2> \
					<h3>' + link.attr("rel").split('|')[1] + '</h3> \
					<p><br /> \
					<a href="#" class="tpAddButton"><img src="images/overlay_button_addToTrip.gif" width="97" height="33" border="0" alt="Add to Trip" title="Add to Trip" /></a> \
					<a href="#" class="tpCancelButton"><img src="images/overlay_button_cancel.gif" width="97" height="33" border="0" alt="Cancel" title="Cancel" /></a> \
					</p> \
				</div> \
				<div class="ol_bot"></div>'
		}
		$("#tpDialog").append(contents);

		// Handle Cancel Button
		$("#tpDialog .tpCancelButton").click(function(e) {
			e.preventDefault();
			if ($.browser.msie) {
				$("#tpDialog").remove();
				addToTripOpen = 0;
			} else {
				$("#tpDialog").fadeOut("normal", function() {
					$("#tpDialog").remove();
					addToTripOpen = 0;
				});
			}
		});

		// Populate Days Dropdown
		if (cloneID == false) {
			var newoptions = "<option value=''>Select A Day</option>";
			for (var i = 1; i <= tpVars["days"]; i++) {
				newoptions += '<option value="' + i + '">' + 'Day ' + i + '</option>';
			}
			newoptions += '<option value="' + (tpVars["days"] == null ? 1 : tpVars["days"] + 1).toString() + '">Add To New Day...</option>'
			$("#tripDay").html(newoptions);
		}

		// Position Dialog Box
		$.positionDiv("#tpDialog")

		// Handle Submit button trigger
		$(".tpAddButton").click(function(e) {
			e.preventDefault();

			// If there is not a day selected, don't process
			if (cloneID == false && $("#tripDay").val() == "") {
				$(".errorTxt").html("<br />Please select a day.");
				return;
			}

			// If there was an error, clear it now...
			$("p.tpError").remove();

			busy = true;

			// Show AJAX Loader animation
			$(".tpAddButton, .tpCancelButton").hide();
			$($(".tpAddButton").parent()).append('<img src="images/tripPlanner_ajax-loader.gif" alt="" width="16" height="16" border="0" id="tpAddButtonLoader" />');
			$("#tpAddButtonLoader").show();

			// SOAP Setup
			if (cloneID == true) {
			    var postLoc = SLCloneItinerary;
			    var soapEnv = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
								   <soap:Body>\
										<CloneItinerary xmlns="http://traveltex.com/">\
											<itineraryID>' + _tid + '</itineraryID>\
											<userID>' + tpVars['userid'] + '</userID>\
											<name>' + _name + '</name>\
											<view>' + tpVars['view'] + '</view>\
										</CloneItinerary>\
								   </soap:Body>\
							   </soap:Envelope>'
			} else if (isLodging == true) {
				var postLoc = SLAddLodgingToItinerary;
				var soapEnv = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
								   <soap:Body>\
									   <AddLodgingToItinerary xmlns="http://traveltex.com/">\
										   <itineraryID>' + tpVars['tripid'] + '</itineraryID>\
										   <userID>' + tpVars['userid'] + '</userID>\
										   <day>' + $("#tripDay").val() + '</day>\
										   <view>' + tpVars['view'] + '</view>\
										   <lodgingid>' + _tid + '</lodgingid>\
									   </AddLodgingToItinerary>\
								   </soap:Body>\
							   </soap:Envelope>'
			    
			} else {
				var postLoc = SLAddToItinerary;
				var soapEnv = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
								   <soap:Body>\
									   <AddToItinerary xmlns="http://traveltex.com/">\
										   <itineraryID>' + tpVars['tripid'] + '</itineraryID>\
										   <userID>' + tpVars['userid'] + '</userID>\
										   <itemID>' + _tid + '</itemID>\
										   <day>' + $("#tripDay").val() + '</day>\
										   <view>' + tpVars['view'] + '</view>\
									   </AddToItinerary>\
								   </soap:Body>\
							   </soap:Envelope>'
			}
			// Adjust max days
			if (parseInt($("#tripDay").val()) > parseInt(tpVars["days"])) tpVars["days"] = parseInt($("#tripDay").val());
			// AJAX Call
			$.ajax({
				url: postLoc,
				type: "POST",
				data: soapEnv,
				dataType: "xml",

				error: function(event, XMLHttpRequest, ajaxOptions, thrownError) {
					$(".tpAddButton").parent().after("<p class='tpError'>We're sorry. There was a problem adding this to your trip. Please try again.</p>");
					$("#tpAddButtonLoader").remove();
					$(".tpAddButton, .tpCancelButton").show();
				},

				complete: function(XMLHttpRequest, status) {
					// Replace everything except for the close button
					//var tempHTML = $("#tpDialog").html().substring(0, $("#tpDialog").html().indexOf("</span>") + 7)
					//$("#tpDialog").html(tempHTML + "<h2>Successfully Added!</h2><p><a href='#' class='tpCloseButton'>Done</a></p>")
					$("#tpDialog .ol_mid").html("<h2>Successfully Added!</h2><p><a href='#' class='tpCloseButton'>Done</a></p>")
					// Reset close button
					$("#overlayClose a, .tpCloseButton").click(function(e) {
						e.preventDefault();
						$("#tpDialog").fadeOut("normal", function() {
							$("#tpDialog").remove();
							addToTripOpen = 0;
						});
					});
					// Remove Dialog Box
					var timer = setTimeout(function() {
						$("#tpDialog").fadeOut("slow", function() {
							$("#tpDialog").remove();
							addToTripOpen = 0;
						});
					}, 3000)
					RedrawItinerary(XMLHttpRequest, status);
				},

				contentType: "text/xml; charset=\"utf-8\""
			});
		});

		// Show Dialog Box
		if ($.browser.msie) {
			$("#tpDialog").css("display", "block");
		} else {
			$("#tpDialog").fadeIn();
		}

		addToTripOpen = 1;
	} //End: if(addToTripOpen==0)
}
// End of addToTrip()










// Handle Toggle View Anchor Event Assignments
//======================================
function AssignToggleEvents() {
    $(".toggleView").each(function() {
        $(this).click(function(e) {
            e.preventDefault();
            if (!busy) {
                // Show spinner
                $("#tripView").fadeOut("fast", function() {
                    $("#tripView").html('<img src="images/sidebarSpinner.gif" alt="" width="16" height="16" border="0" id="tpAddButtonLoader" />');
                });
                $("#tripView").fadeIn("normal");

                busy = true;

                tpVars['view'] = $(this).attr("class").split(" ")[1];

                // SOAP Setup
                var soapEnv =
                    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
                      <soap:Body>\
                        <GetItinerary xmlns="http://traveltex.com/">\
                          <itineraryID>' + tpVars['tripid'] + '</itineraryID>\
                          <view>' + tpVars['view'] + '</view>\
                        </GetItinerary>\
                      </soap:Body>\
                    </soap:Envelope>'

                // AJAX Call
                $.ajax({
                    url: SLGetItinerary,
                    type: "POST",
                    data: soapEnv,
                    dataType: "xml",

                    error: function(event, XMLHttpRequest, ajaxOptions, thrownError) {
                        //alert(XMLHttpRequest.status);
                    },

                    complete: RedrawItinerary,

                    contentType: "text/xml; charset=\"utf-8\""
                });
                // Remove click events while current click is being processed
                //            $(".toggleView").each(function() {
                //                $(this).click(null);
                //            });
            }
        });
    });
}
// Handle TripList Event Assignments
//======================================
function AssignTripListChangeEvent(jqObj) {
    jqObj.change(function(e) {
        e.preventDefault();
        $("#tripView").fadeOut("fast", function() {
            $("#tripView").html('<img src="images/sidebarSpinner.gif" alt="" width="16" height="16" border="0" id="tpAddButtonLoader" />');
        });
        $("#tripView").fadeIn("normal");

        busy = true;

        tpVars['tripid'] = $(this).children('option[selected="true"]').val();
        tpVars['tripname'] = $(this).children('option[selected="true"]').text();
        tpVars['days'] = parseInt($(this).children('option[selected="true"]').attr('class'));

        // SOAP Setup
        var soapEnv =
                '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
                  <soap:Body>\
                    <SetItinerary xmlns="http://traveltex.com/">\
                      <itineraryID>' + $(this).val() + '</itineraryID>\
                      <userID>' + tpVars['userid'] + '</userID>\
                      <view>' + tpVars['view'] + '</view>\
                    </SetItinerary>\
                  </soap:Body>\
                </soap:Envelope>'

        // AJAX Call
        $.ajax({
            url: SLSetItinerary,
            type: "POST",
            data: soapEnv,
            dataType: "xml",

            error: function(XMLHttpRequest, ajaxOptions, thrownError, event) {
                //for (var i in XMLHttpRequest) alert(i + ": " + XMLHttpRequest[i]);
                //                alert(XMLHttpRequest);
                //                alert(ajaxOptions);
                //                alert(thrownError);
                //                alert(event);
            },

            complete: RedrawItinerary,

            contentType: "text/xml; charset=\"utf-8\""
        });
    });
}

// Handle Remove link on Itenerary Items
//======================================
var toRemove;
function removeIten(){
    $(".tpRemove").each(function() {
        $(this).click(function(e) {
            e.preventDefault();
            var itenID = $(this).parent().attr("class").split(" ")[0];//$(this).attr("rel");
			
			
			
            // If there was an error, clear it now...
            $(".tpRemoveError").remove();
			
			 // Create Dialog Box
			var tpDIV = $("<div/>").attr("id", "tpDialog").attr("class", "overlayA");
			$("body").append(tpDIV);
			$("#tpDialog").addClass("addToTrip");

			//Create Close button for DIV
			/*$("#tpDialog").append("<span id='overlayClose'><a href='#' title='Close'></a></span>");
			$("#overlayClose a").click(function(e) {
				e.preventDefault();
				$("#tpDialog").fadeOut("normal", function() {
					$("#tpDialog").remove();
					addToTripOpen = 0;
				});
			});*/
			
			// Populate Dialog Box
			var contents = ' \
				<div class="ol_top"></div> \
				<div class="ol_mid"> \
					<h2>Are you sure you want to remove this item?</h2> \
					<p style="font-size:1.4em; padding-bottom:.8em"><a href="#" class="tpRemoveYes" rel="'+itenID+'">Yes, I want to remove this.</a></p> \
					<p><a href="#" class="tpRemoveNo">No, I changed my mind.</a></p> \
				</div> \
				<div class="ol_bot"></div>'
			
			// Handle the links in dialog box
			$(".tpRemoveYes").live("click",function(e){
				e.preventDefault();
				$("#tpDialog").remove();
				removeIten2($(this).attr("rel"));
			});
			$(".tpRemoveNo").live("click",function(e){
				e.preventDefault();
				$("#tpDialog").remove();
			});
			
			
			$("#tpDialog").append(contents);
			
			 // Position Dialog Box
			$.positionDiv("#tpDialog")
			
			// Show Dialog Box
			$("#tpDialog").fadeIn();
			
			
        });
    });
}

function removeIten2(itenID){
	
	// Show AJAX Loader animation
	$("#tripView").fadeOut("fast", function() {
		$("#tripView").html('<img src="images/sidebarSpinner.gif" alt="" width="16" height="16" border="0" id="tpAddButtonLoader" />');
	});
	$("#tripView").fadeIn("normal");
	
	busy = true;
	
	var soapEnv = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
						<soap:Body>\
							<DeleteFromItinerary xmlns="http://traveltex.com/">\
								<itineraryID>' + $(".tripList").val() + '</itineraryID>\
								<userID>' + tpVars['userid'] + '</userID>\
								<itemID>' + itenID + '</itemID>\
								<view>' + tpVars['view'] + '</view>\
							</DeleteFromItinerary>\
						</soap:Body>\
					</soap:Envelope>'

	// AJAX Call
	$.ajax({
		url: SLDeleteFromItinerary,
		type: "POST",
		data: soapEnv,
		dataType: "xml",

		error: function(event, XMLHttpRequest, ajaxOptions, thrownError) {
			$("#tpRemoveLoader").parent().remove();
			$("a.tpRemove").show();
			$("." + toRemove + " .tpRemove").after("<p class='tpRemoveError'>We're sorry. There was a problem removing this from your trip. Please try again.</p>");
			toRemove = "";
		},

		complete: RedrawItinerary,
		contentType: "text/xml; charset=\"utf-8\""
	});	
}


// Handle Redraw Itenerary 
//======================================
function RedrawItinerary(XMLHttpRequest, status) {
    // alert(status);
    // alert(XMLHttpRequest.status);
    // Pull content from various Web Service responses
    var html = $(XMLHttpRequest.responseXML).find("\
                AddItineraryResponse,\
                AddToItineraryResponse,\
                AddLodgingToItineraryResponse,\
                ChangeItineraryItemDayResponse,\
                CloneItineraryResponse,\
                DeleteFromItineraryResponse,\
                DeleteItineraryResponse,\
                GetItineraryResponse,\
                GetItineraryListResponse,\
                GetUserNameResponse,\
                MoveItineraryItemResponse,\
                RenameItineraryResponse,\
                SetItineraryResponse\
               ").text();
    var numDays = parseInt($(html).attr("class").split(" ")[3]);
    if (XMLHttpRequest.responseText.indexOf("AddItineraryResponse")>-1 ||
        XMLHttpRequest.responseText.indexOf("DeleteItineraryResponse") > -1 ||
        XMLHttpRequest.responseText.indexOf("CloneItineraryResponse") > -1 ||
        XMLHttpRequest.responseText.indexOf("RenameItineraryResponse") > -1) {
        UpdateItineraryList();
    }
    $(".bottomLinks").each(function() {
        var h = '<li class="city' + (tpVars["view"] != "city" ? "Off" : "") + '"><a href="#" title="View by City" class="toggleView city"><span>View by City</span></a></li>\
                 <li class="day' + (tpVars["view"] != "day" ? "Off" : "") + '"><a href="#" title="View by Day" class="toggleView day"><span>View by Day</span></a></li>'
        $(this).html(h);
        AssignToggleEvents();
    });
    // Hide current list, load the new one, and then show new list...
    $("#tripView").fadeOut("fast", function() {
        $("#tripView").html(html);
        tpVars['days'] = numDays;

        // Re initialize the Remove buttons in the list
        removeIten();
    });
    $("#tripView").fadeIn("normal");
    busy = false;
}
// Handle Update Triplist
//======================================
function UpdateItineraryList() {
    $("#TripListContainer").fadeOut("fast", function() {
        $("#TripListContainer").html('<img src="images/sidebarSpinner.gif" alt="" width="16" height="16" border="0" id="tpAddButtonLoader" />');
    });
    $("#TripListContainer").fadeIn("normal");

    // SOAP Setup
    var soapEnv =
                    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
                      <soap:Body>\
                        <GetItineraryList xmlns="http://traveltex.com/">\
                          <userID>' + tpVars['userid'] + '</userID>\
                          <view>' + tpVars['view'] + '</view>\
                        </GetItineraryList>\
                      </soap:Body>\
                    </soap:Envelope>'

    // AJAX Call
    $.ajax({
        url: SLGetItineraryList,
        type: "POST",
        data: soapEnv,
        dataType: "xml",

        error: function(XMLHttpRequest, status, errorThrown) {
            //alert(XMLHttpRequest.status);
        },

        complete: function(XMLHttpRequest, status) {
            var html = $(XMLHttpRequest.responseXML).find("\
                                DeleteItineraryResponse,\
                                GetItineraryListResponse,\
                                RenameItineraryResponse\
                               ").text();
            $("#TripListContainer").fadeOut("fast", function() {
                var tList = $("<select id='tripList' class='tripList'>" + html + "</select>").appendTo($("#TripListContainer").empty());
                AssignTripListChangeEvent(tList);
                tpVars['tripid'] = tList.children('option[selected="true"]').val();
                tpVars['tripname'] = tList.children('option[selected="true"]').text();
                RefreshTripList();
            });
            $("#TripListContainer").fadeIn("normal");
        },

        contentType: "text/xml; charset=\"utf-8\""
    });

}

// Handle Sidebar Refresh
// for trip planner close and other events
//======================================
function RefreshSidebar() {
    $("#tripView").fadeOut("fast", function() {
        $("#tripView").html('<img src="images/sidebarSpinner.gif" alt="" width="16" height="16" border="0" id="tpAddButtonLoader" />');
    });
    $("#tripView").fadeIn("normal");

    busy = true;

    UpdateItineraryList();
}
// Handle TripList Refresh
// for trip planner close and other events
//======================================
function RefreshTripList() {
    // SOAP Setup
    var soapEnv =
                    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
                      <soap:Body>\
                        <GetItinerary xmlns="http://traveltex.com/">\
                          <itineraryID>' + tpVars['tripid'] + '</itineraryID>\
                          <view>' + tpVars['view'] + '</view>\
                        </GetItinerary>\
                      </soap:Body>\
                    </soap:Envelope>'

    // AJAX Call
    $.ajax({
        url: SLGetItinerary,
        type: "POST",
        data: soapEnv,
        dataType: "xml",

        error: function(event, XMLHttpRequest, ajaxOptions, thrownError) {
            //            alert(XMLHttpRequest.status);
        },

        complete: RedrawItinerary,

        contentType: "text/xml; charset=\"utf-8\""
    });
}


