//******** Click outsite to hidden popup ********
var atvOutsiteClick = false;
var PopupTarget = "";
function PopupOutsiteClick(e) {
    var target = (e && e.target) || (event && event.srcElement);
    if (!atvOutsiteClick) { atvOutsiteClick = true; return; }
    if (checkParent(target, PopupTarget)) {
        document.getElementById(PopupTarget).style.display = 'none';
        document.onclick = null;
        atvOutsiteClick = false;
    }
}
function checkParent(target, eID) {
    while (target.parentNode) {
        if (target == document.getElementById(eID)) { return false; }
        target = target.parentNode
    }
    return true
}
function jAlertMessage(titleStr, messageStr) {
    $("#AlertMessageBox").html("<p>" + messageStr + "</p>"); 
    $("#AlertMessageBox").dialog({
        modal: true, draggable: false, resizable: false, title: titleStr, 
        buttons: { Ok: function () { $(this).dialog("close"); } }
    });
}


//******** Main Search Form functions ********
function switchMainSearchBox(tabNum) {
    switch (tabNum) {
        case 1:
            if ($("#MainHotelTab div").css('display') == 'none') { $("#MainHotelTab div").show("slide", { direction: "down" }, 'fast'); }
            if ($("#MainFlightTab div").css('display') != 'none') { $("#MainFlightTab div").hide("slide", { direction: "down" }, 'fast'); }
            if ($("#MainPackageTab div").css('display') != 'none') { $("#MainPackageTab div").hide("slide", { direction: "down" }, 'fast'); }

            SetupHotelSearchForm();
            if ($("#Hotel_SearchForm").css('display') == 'none') { $('#Hotel_SearchForm').show(); }
            $('#Flight_SearchForm').css('display', 'none');
            $('#Package_SearchForm').css('display', 'none');
            break;
        case 2:
            if ($("#MainHotelTab div").css('display') != 'none') { $("#MainHotelTab div").hide("slide", { direction: "down" }, 'fast'); }
            if ($("#MainFlightTab div").css('display') == 'none') { $("#MainFlightTab div").show("slide", { direction: "down" }, 'fast'); }
            if ($("#MainPackageTab div").css('display') != 'none') { $("#MainPackageTab div").hide("slide", { direction: "down" }, 'fast'); }

            switchFlightTripMode();
            $('#Hotel_SearchForm').css('display', 'none');
            if ($("#Flight_SearchForm").css('display') == 'none') { $('#Flight_SearchForm').show(); }
            $('#Package_SearchForm').css('display', 'none');
            break;
        case 3:
            if ($("#MainHotelTab div").css('display') != 'none') { $("#MainHotelTab div").hide("slide", { direction: "down" }, 'fast'); }
            if ($("#MainFlightTab div").css('display') != 'none') { $("#MainFlightTab div").hide("slide", { direction: "down" }, 'fast'); }
            if ($("#MainPackageTab div").css('display') == 'none') { $("#MainPackageTab div").show("slide", { direction: "down" }, 'fast'); }

            SetupPackageSearchForm();
            $('#Flight_SearchForm').css('display', 'none');
            $('#Hotel_SearchForm').css('display', 'none');
            if ($("#Package_SearchForm").css('display') == 'none') { $('#Package_SearchForm').show(); }
            break;
    }
}


//******** Flight Search Form ********
var flightGatewayURL = "http://www.newshan.com/airfare/Gateway.aspx";
var flightWSURL = "http://www.newshan.com/SearchService.asmx";
var flightAutocomplete = {
    source: function (request, response) {
        $.ajax({
            type: "POST", url: flightWSURL + "/GetFlightCompletionList",
            contentType: "application/json; charset=utf-8", dataType: "json",
            data: "{'prefixText':'" + request.term + "', 'count':6}",
            success: function (data) {
                response($.map(data.d, function (item) { return { label: item.Display, value: item.Value, img: item.Icon} }))
            }
        })
    },
    minLength: 3
};
var flightAutocomplateItem = function (ul, item) {
    return $("<li></li>").data("item.autocomplete", item)
                .append("<a><img width=\"16\" height=\"11\" src=\"" + item.img + "\">&nbsp;" + item.label + "</a>").appendTo(ul);
};
function switchFlightTripMode() {
    switch ($('input[name=FlightTrip]:checked').val()) {
        case 'O':
            $('#Flight_R_Panel').css('display', ''); $('#Flight_M_Panel').css('display', 'none');
            SetupFlightRSearchForm();
            $('#Flight_R_TravelDate2').datepicker("disable");
            break;

        case 'M':
            $('#Flight_R_Panel').css('display', 'none'); $('#Flight_M_Panel').css('display', '');
            SetupFlightMSearchForm();
            break;

        default:
            $('#Flight_R_Panel').css('display', ''); $('#Flight_M_Panel').css('display', 'none');
            SetupFlightRSearchForm();
            $('#Flight_R_TravelDate2').datepicker("enable");
            break;
    }

}
function submitFlightSearchForm(ref) {
    var FlightTripMode = $('input[name=FlightTrip]:checked').val();
    var Parameters = flightGatewayURL + "?Trip=" + FlightTripMode;
    Parameters += "&ref=" + ref;

    if (FlightTripMode == 'M') {
        Parameters += "&Adult=" + $('#Flight_M_Adult').val();
        Parameters += "&Child=" + $('#Flight_M_Child').val();
        Parameters += "&Cabin=" + $('#Flight_M_Cabin').val();

        if ($('#Flight_M_To1').val().length < 3) {
            jAlertMessage("Travel Destination", "Please Enter your destination for Trip 1");
            return;
        } else {
            Parameters += "&From1=" + $('#Flight_M_From1').val();
            Parameters += "&To1=" + $('#Flight_M_To1').val();
        }

        if ($('#Flight_M_TravelDate1').val().length != 11) {
            jAlertMessage("Travel Date", "Please Enter your Travel Date for Trip 1");
            return;
        } else {
            Parameters += "&TravelDate1=" + $('#Flight_M_TravelDate1').val();
        }

        if ($('#Flight_M_From2').val() != "" || $('#Flight_M_To2').val() != "") {
            if ($('#Flight_M_From2').val().length < 3 || $('#Flight_M_To2').val().length < 3) {
                jAlertMessage("Travel Destination", "Please Enter your destination for Trip 2");
                return;
            } else {
                Parameters += "&From2=" + $('#Flight_M_From2').val();
                Parameters += "&To2=" + $('#Flight_M_To2').val();
            }

            if ($('#Flight_M_TravelDate2').val().length != 11) {
                jAlertMessage("Travel Date", "Please Enter your Travel Date for Trip 2");
                return;
            } else {
                Parameters += "&TravelDate2=" + $('#Flight_M_TravelDate2').val();
            }
        }

        if ($('#Flight_M_From3').val() != "" || $('#Flight_M_To3').val() != "") {
            if ($('#Flight_M_From3').val().length < 3 || $('#Flight_M_To3').val().length < 3) {
                jAlertMessage("Travel Destination", "Please Enter your destination for Trip 3");
                return;
            } else {
                Parameters += "&From3=" + $('#Flight_M_From3').val();
                Parameters += "&To3=" + $('#Flight_M_To3').val();
            }

            if ($('#Flight_M_TravelDate3').val().length != 11) {
                jAlertMessage("Travel Date", "Please Enter your Travel Date for Trip 3");
                return;
            } else {
                Parameters += "&TravelDate3=" + $('#Flight_M_TravelDate3').val();
            }
        }


    } else {
        Parameters += "&Adult=" + $('#Flight_R_Adult').val();
        Parameters += "&Child=" + $('#Flight_R_Child').val();
        Parameters += "&Cabin=" + $('#Flight_R_Cabin').val();
        Parameters += "&Airline=" + $('#Flight_R_Airline').val();

        if ($('#Flight_R_To').val().length < 3) {
            jAlertMessage("Travel Destination", "Please Enter your destination");
            return;
        } else {
            Parameters += "&From1=" + $('#Flight_R_From').val();
            Parameters += "&To1=" + $('#Flight_R_To').val();
        }

        if ($('#Flight_R_TravelDate1').val().length != 11) {
            jAlertMessage("Travel Date", "Please Enter your Travel Date");
            return;
        } else {
            Parameters += "&TravelDate1=" + $('#Flight_R_TravelDate1').val();
        }

        if (FlightTripMode != 'O') {
            if ($('#Flight_R_TravelDate2').val().length != 11) {
                jAlertMessage("Travel Date", "Please Enter your Travel Date");
                return;
            } else {
                Parameters += "&TravelDate2=" + $('#Flight_R_TravelDate2').val();
            }
        }
    }

    //alert(Parameters);
    window.location.assign(Parameters);
}


//** Setup flight return/oneway search form **
function SetupFlightRSearchForm() {

    $("#Flight_R_To").autocomplete(flightAutocomplete).data("autocomplete")._renderItem = flightAutocomplateItem;

    //** setup travel date picker
    var dates = $('#Flight_R_TravelDate1, #Flight_R_TravelDate2').datepicker({
        defaultDate: "+1w", numberOfMonths: 2,
        monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        showOn: 'both',
        buttonImage: 'http://www.newshan.com/img/calendar.gif', buttonImageOnly: true, buttonText: 'Choose Travel Date',
        minDate: '+5d', maxDate: '+12m', dateFormat: 'dd-M-yy',
        showAnim: '',
        onSelect: function (selectedDate) {
            var option = this.id == "Flight_R_TravelDate1" ? "minDate" : "maxDate",
					instance = $(this).data("datepicker"),
					date = $.datepicker.parseDate(
					instance.settings.dateFormat ||
					$.datepicker._defaults.dateFormat,
					selectedDate, instance.settings);
            if (this.id == "Flight_R_TravelDate1") { date.setDate(date.getDate() + 1); }
            else { date.setDate(date.getDate() - 1); }
            dates.not(this).datepicker("option", option, date);
        }
    });

    //** download airlines options
    if ($('#Flight_R_Airline option').length == 0) {
        $('#Flight_R_Airline option').each(function (i, option) { $(option).remove(); });
        $('#Flight_R_Airline').attr('disabled', true);
        $("#Flight_R_Airline").append("<option>Downloading...</option>");
        $.ajax({
            type: "POST", url: flightWSURL + "/GetAirlinesList", data: "",
            contentType: "application/json; charset=utf-8", dataType: "json",
            success: function (response) {
                var airlines = response.d;
                $('#Flight_R_Airline option').each(function (i, option) { $(option).remove(); });
                $('#Flight_R_Airline').attr('disabled', false);
                for (var i = 0; i < airlines.length; i++) {
                    $("#Flight_R_Airline").append("<option value='" + airlines[i].Value + "'>" + airlines[i].Display + "</option>");
                }
            }
        });
    }
}

//** Setup flight multi stop search form **
function SetupFlightMSearchForm() {

    $("#Flight_M_To1").autocomplete(flightAutocomplete).data("autocomplete")._renderItem = flightAutocomplateItem;
    $("#Flight_M_From2").autocomplete(flightAutocomplete).data("autocomplete")._renderItem = flightAutocomplateItem;
    $("#Flight_M_To2").autocomplete(flightAutocomplete).data("autocomplete")._renderItem = flightAutocomplateItem;
    $("#Flight_M_From3").autocomplete(flightAutocomplete).data("autocomplete")._renderItem = flightAutocomplateItem;
    $("#Flight_M_To3").autocomplete(flightAutocomplete).data("autocomplete")._renderItem = flightAutocomplateItem;

    //** setup travel date picker
    var dates = $("#Flight_M_TravelDate1, #Flight_M_TravelDate2, #Flight_M_TravelDate3").datepicker({
        defaultDate: "+1w", numberOfMonths: 2,
        monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        showOn: 'both',
        buttonImage: 'http://www.newshan.com/img/calendar.gif', buttonImageOnly: true, buttonText: 'Choose Travel Date',
        minDate: '+5d', maxDate: '+12m', dateFormat: 'dd-M-yy',
        showAnim: '',
        onSelect: function (selectedDate) {
            instance = $(this).data("datepicker");
            date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
            switch (this.id) {
                case "Flight_M_TravelDate1":
                    dates.not(this).datepicker("option", "minDate", date);
                    break;
                case "Flight_M_TravelDate2":
                    $('#Flight_M_TravelDate1').datepicker("option", "maxDate", date);
                    $('#Flight_M_TravelDate3').datepicker("option", "minDate", date);
                    break;
                case "Flight_M_TravelDate3":
                    $('#Flight_M_TravelDate2').datepicker("option", "maxDate", date);
                    //dates.not(this).datepicker("option", "maxDate", date);
                    break;
            }
        }
    });

}

//** Setup flight airport picker **
function showFlightAirportPicker(targetInput) {
    $('#Flight_AP_targetInput').val(targetInput);
    $('#Flight_AP').css('display', '');
    $('#Flight_AP').css('left', ($('#' + targetInput).position().left - 9) + "px");
    $('#Flight_AP').css('top', ($('#' + targetInput).position().top + 22) + "px");
    if ($('#Flight_AP_Country option').length == 0) { loadFlightAP_CountryList(); }
    PopupTarget = "Flight_AP";
    document.onclick = PopupOutsiteClick;
}
function hiddenFlightAirportPicker() {
    document.onclick = null;
    atvOutsiteClick = false;
    $('#Flight_AP').css('display', 'none');
}
function selectFlightAirportPicker() {
    $('#' + $('#Flight_AP_targetInput').val()).val($('#Flight_AP_Airport').val());
    hiddenFlightAirportPicker();
}
function selectFlightTopAirportPicker(AirportValue) {
    $('#' + $('#Flight_AP_targetInput').val()).val(AirportValue);
    hiddenFlightAirportPicker();
}
function loadFlightAP_CountryList() {
    $('#Flight_AP_Country option').each(function (i, option) { $(option).remove(); });
    $('#Flight_AP_Country').attr('disabled', true);
    $("#Flight_AP_Country").append("<option>Downloading...</option>");
    $.ajax({
        type: "POST", url: flightWSURL + "/GetFlightCountryList", data: "",
        contentType: "application/json; charset=utf-8", dataType: "json",
        success: function (response) {
            var countries = response.d;
            $('#Flight_AP_Country option').each(function (i, option) { $(option).remove(); });
            $('#Flight_AP_Country').attr('disabled', false);
            for (var i = 0; i < countries.length; i++) {
                $("#Flight_AP_Country").append("<option value='" + countries[i].Value + "'>" + countries[i].Display + "</option>");
            }
        }
    });
}
function loadFlightAP_AirportList() {
    $('#Flight_AP_Airport option').each(function (i, option) { $(option).remove(); });
    if ($('#Flight_AP_Country').val() == "") { return; }
    $('#Flight_AP_Airport').attr('disabled', true);
    $("#Flight_AP_Airport").append("<option>Downloading...</option>");
    $.ajax({
        type: "POST", url: flightWSURL + "/GetFlightAirportList",
        data: "{countrycode: '" + $('#Flight_AP_Country').val() + "'}",
        contentType: "application/json; charset=utf-8", dataType: "json",
        success: function (response) {
            var airports = response.d;
            $('#Flight_AP_Airport option').each(function (i, option) { $(option).remove(); });
            $('#Flight_AP_Airport').attr('disabled', false);
            for (var i = 0; i < airports.length; i++) {
                $("#Flight_AP_Airport").append("<option value='" + airports[i].Value + "'>" + airports[i].Display + "</option>");
            }
        }
    });
}



//******** Hotel Search Form ********
var hotelGatewayURL = "http://www.newshan.com/hotel/Gateway.aspx";
var hoteltWSURL = "http://www.newshan.com/SearchService.asmx";
var hotelAutocomplete = {
    minLength: 3,
    source: function (request, response) {
        $.ajax({
            type: "POST", url: hoteltWSURL + "/GetHotelCompletionList",
            contentType: "application/json; charset=utf-8", dataType: "json",
            data: JSON.stringify({'prefixText': request.term, 'count':6}),
            success: function (data) {
                response($.map(data.d, function (item) { return { label: item.Display, value: item.Value, code: item.Code, img: item.Icon} }))
            }
        })
    },
    select: function (event, ui) { $("#Hotel_CityValue").val(ui.item.code); }
};
var hotelAutocomplateItem = function (ul, item) {
    return $("<li></li>").data("item.autocomplete", item)
                .append("<a><img width=\"16\" height=\"11\" src=\"http://www.newshan.com/" + item.img + "\">&nbsp;" + item.label + "</a>").appendTo(ul);
};
function SetupHotelSearchForm() {
    $("#Hotel_City").autocomplete(hotelAutocomplete).data("autocomplete")._renderItem = hotelAutocomplateItem;
    $("#Hotel_HotelName").autocomplete(hotelNameAutocomplete).data("autocomplete")._renderItem = hotelNameAutocomplateItem;

    var dates = $('#Hotel_Date_CheckIn, #Hotel_Date_CheckOut').datepicker({
        defaultDate: "+1w", numberOfMonths: 2,
        monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        showOn: 'both',
        buttonImage: 'http://www.newshan.com/img/calendar.gif', buttonImageOnly: true, buttonText: 'Choose Travel Date',
        minDate: '+5d', maxDate: '+12m', dateFormat: 'dd-M-yy',
        showAnim: '',
        onSelect: function (selectedDate) {
            var option = this.id == "Hotel_Date_CheckIn" ? "minDate" : "maxDate",
					instance = $(this).data("datepicker"),
					date = $.datepicker.parseDate(
					instance.settings.dateFormat ||
					$.datepicker._defaults.dateFormat,
					selectedDate, instance.settings);
            if (this.id == "Hotel_Date_CheckIn") { date.setDate(date.getDate() + 1); }
            else { date.setDate(date.getDate() - 1); }
            dates.not(this).datepicker("option", option, date);
            refreshHotelSearchForm();
        }
    });

    refreshHotelSearchForm();
}
function refreshHotelSearchForm() {
    var dateDiff = $('#Hotel_Date_CheckIn').datepicker('getDate') - $('#Hotel_Date_CheckOut').datepicker('getDate');
    var Nights = Math.abs(Math.round(dateDiff / 86400000));
    $('#Hotel_Nights_Lable').html("( " + Nights + " nights )");
    $('#Hotel_Duration').val(Nights);

    switch ($('#Hotel_Rooms').val()) {
        case '1':
            $('#Hotel_R2_inputs').css('display', 'none');
            $('#Hotel_R3_inputs').css('display', 'none');
            $('#Hotel_R2_Child').val('0');
            $('#Hotel_R3_Child').val('0');
            break;
        case '2':
            $('#Hotel_R2_inputs').css('display', '');
            $('#Hotel_R3_inputs').css('display', 'none');
            $('#Hotel_R3_Child').val('0');
            break;
        case '3':
            $('#Hotel_R2_inputs').css('display', '');
            $('#Hotel_R3_inputs').css('display', '');
            break;
    }
    var C1_lable = false;
    var C2_lable = false;
    for (r = 1; r <= 3; r++) {
        if ($('#Hotel_R' + r + '_Child').val() == '0') {
            $('#Hotel_R' + r + '_C1_age').css('display', 'none');
            $('#Hotel_R' + r + '_C1_age').val('0');
            $('#Hotel_R' + r + '_C2_age').css('display', 'none');
            $('#Hotel_R' + r + '_C2_age').val('0');
        } else if ($('#Hotel_R' + r + '_Child').val() == '1') {
            $('#Hotel_R' + r + '_C1_age').css('display', '');
            C1_lable = true;
            $('#Hotel_R' + r + '_C2_age').css('display', 'none');
            $('#Hotel_R' + r + '_C2_age').val('0');
        } else {
            $('#Hotel_R' + r + '_C1_age').css('display', '');
            C1_lable = true;
            $('#Hotel_R' + r + '_C2_age').css('display', '');
            C2_lable = true;
        }
    }
    if (C1_lable) { $('#Hotel_C1age_Lable').css('display', ''); }
    else { $('#Hotel_C1age_Lable').css('display', 'none'); }
    if (C2_lable) { $('#Hotel_C2age_Lable').css('display', ''); }
    else { $('#Hotel_C2age_Lable').css('display', 'none'); }
}
function submitHotelSearchForm(ref) {
    var parameters = hotelGatewayURL + "?ref=" + ref + "&Gateway=NormalSearch";
	var roomCount = parseInt($('#Hotel_Rooms').val());
	var errormsg = "";
	var paxerror = 0;
	var hCity = $('#Hotel_CityValue').val();
	
    if (hCity == '' || hCity.indexOf("_") < 0) {
        errormsg = "Please enter your destination";
    }

	for (var r = 1; r <= roomCount; r++) {
		var pax = parseInt($("#Hotel_R" + r + "_Adult").val()) + parseInt($("#Hotel_R" + r + "_Child").val());
		if(pax > 4)
			paxerror = 1;
	}
	
	if (paxerror == 1)
	{
		errormsg += errormsg == "" ? "Max 4 persons in each room" : "<br>Max 4 persons in each room";
	}
	
	if (errormsg != "")
	{
		jAlertMessage("Error", errormsg);
		return;
	}
	
    parameters += "&Destination=" + $('#Hotel_CityValue').val();
    parameters += "&CheckInDate=" + $('#Hotel_Date_CheckIn').val();
    parameters += "&Duration=" + $('#Hotel_Duration').val();
    parameters += "&RoomCount=" + roomCount;

    for (var r = 1; r <= roomCount; r++) {
        parameters += "&Room" + r + "Adult=" + $('#Hotel_R' + r + '_Adult').val();
        var childCount = parseInt($('#Hotel_R' + r + '_Child').val());
        parameters += "&Room" + r + "Child=" + childCount;
        for (var c = 1; c <= childCount; c++) {
            parameters += "&ChildAge" + r + c + "=" + $('#Hotel_R' + r + '_C' + c + '_age').val();
        }
    }

    if ($('#Hotel_HotelName').val() != '') { parameters += "&HotelName=" + $('#Hotel_HotelName').val(); }

    //alert(parameters);
    window.location.assign(parameters);
}
var hotelNameAutocomplete = {
    minLength: 3,
    source: function (request, response) {
        $.ajax({
            type: "POST", url: hoteltWSURL + "/GetHotelNameCompletionList",
            contentType: "application/json; charset=utf-8", dataType: "json",
            data: JSON.stringify({ 'prefixText': request.term, 'cityId': $("#Hotel_CityValue").val(), 'count': 6 }),
            success: function (data) {
                response($.map(data.d, function (item) { return { label: item.Display, value: item.Value, code: item.Code} }))
            }
        })
    },
    search: function (event, ui) { if ($("#Hotel_CityValue").val() == "") { return false; } }
};
var hotelNameAutocomplateItem = function (ul, item) {

    var star = parseInt(item.code);
    var imgCode = "";
    for (i = 0; i < star; i++) {
        imgCode += "<img width=\"13\" height=\"12\" border=\"0\" src=\"http://www.newshan.com/Hotel/img/star.png\">";
    }
    return $("<li></li>").data("item.autocomplete", item)
                .append("<a>&nbsp;" + item.label + "&nbsp;" + imgCode + "</a>").appendTo(ul);
};

//** Setup hotel city picker **
function showHotelCityPicker() {
    $('#Hotel_CP').css('display', '');
    $('#Hotel_CP').css('left', ($('#Hotel_City').position().left - 8) + "px");
    $('#Hotel_CP').css('top', ($('#Hotel_City').position().top + 22)+ "px");
    if ($('#Hotel_CP_Country option').length == 0) { loadHotelCP_CountryList(); }
    PopupTarget = "Hotel_CP";
    document.onclick = PopupOutsiteClick;
}
function hiddenHotelCityPicker() {
    document.onclick = null;
    atvOutsiteClick = false;
    $('#Hotel_CP').css('display', 'none');
}
function selectHotelTopCityPicker(DisplayName, cValue) {
    $('#Hotel_City').val(DisplayName);
    $('#Hotel_CityValue').val(cValue);
    hiddenHotelCityPicker();
}
function selectHotelCityPicker() {
    var DisplayName = $('#Hotel_CP_City option:selected').text() + ', ' + $('#Hotel_CP_Country option:selected').text();
    $('#Hotel_City').val(DisplayName);
    $('#Hotel_CityValue').val($('#Hotel_CP_City').val());
    hiddenHotelCityPicker();
}
function loadHotelCP_CountryList() {
    $('#Hotel_CP_Country option').each(function (i, option) { $(option).remove(); });
    $('#Hotel_CP_Country').attr('disabled', true);
    $("#Hotel_CP_Country").append("<option>Downloading...</option>");
    $.ajax({
        type: "POST", url: hoteltWSURL + "/GetHotelCountryList", data: "",
        contentType: "application/json; charset=utf-8", dataType: "json",
        success: function (response) {
            var countries = response.d;
            $('#Hotel_CP_Country option').each(function (i, option) { $(option).remove(); });
            $('#Hotel_CP_Country').attr('disabled', false);
            for (var i = 0; i < countries.length; i++) {
                $("#Hotel_CP_Country").append("<option value='" + countries[i].Value + "'>" + countries[i].Display + "</option>");
            }
        }
    });
}
function loadHotelCP_CityList() {
    $('#Hotel_CP_City option').each(function (i, option) { $(option).remove(); });
    if ($('#Hotel_CP_Country').val() == "") { return; }
    $('#Hotel_CP_City').attr('disabled', true);
    $("#Hotel_CP_City").append("<option>Downloading...</option>");
    $.ajax({
        type: "POST", url: flightWSURL + "/GetHotelCityList",
        data: "{countrycode: '" + $('#Hotel_CP_Country').val() + "'}",
        contentType: "application/json; charset=utf-8", dataType: "json",
        success: function (response) {
            var airports = response.d;
            $('#Hotel_CP_City option').each(function (i, option) { $(option).remove(); });
            $('#Hotel_CP_City').attr('disabled', false);
            for (var i = 0; i < airports.length; i++) {
                $("#Hotel_CP_City").append("<option value='" + airports[i].Value + "'>" + airports[i].Display + "</option>");
            }
        }
    });
}

var PackageGatewayURL_Tour = "http://www.newshan.com/tour/PackageList.aspx";
var PackageGatewayURL_FNE = "http://www.newshan.com/tour/FreeEasyList.aspx";
var PackageWSURL = "SearchService.asmx";

function SetupPackageSearchForm() {
    if ($('#Package_Country option').length == 0) { loadPackage_CountryList(); }
    if ($('#Package_City option').length == 0) { loadPackage_CityList(); }

}

function loadPackage_CountryList() {
    $('#Package_Country option').each(function (i, option) { $(option).remove(); });
    $('#Package_Country').attr('disabled', true);
    $("#Package_Country").append("<option value=''>Downloading...</option>");
    $.ajax({
        type: "POST", url: PackageWSURL + "/GetPackageCountryList", data: "",
        contentType: "application/json; charset=utf-8", dataType: "json",
        success: function (response) {
            var countries = response.d;
            $('#Package_Country option').each(function (i, option) { $(option).remove(); });
            $('#Package_Country').attr('disabled', false);
            for (var i = 0; i < countries.length; i++) {
                $("#Package_Country").append("<option value='" + countries[i].Value + "'>" + countries[i].Display + "</option>");
            }
        }
    });
}

function loadPackage_CityList() {
    $('#Package_City option').each(function (i, option) { $(option).remove(); });
    $('#Package_City').attr('disabled', true);
    $("#Package_City").append("<option>Downloading...</option>");
    $.ajax({
        type: "POST", url: PackageWSURL + "/GetPackageCityList",
        data: "{countrycode: '" + $('#Package_Country').val() + "'}",
        contentType: "application/json; charset=utf-8", dataType: "json",
        success: function (response) {
            var airports = response.d;
            $('#Package_City option').each(function (i, option) { $(option).remove(); });
            $('#Package_City').attr('disabled', false);
            for (var i = 0; i < airports.length; i++) {
                $("#Package_City").append("<option value='" + airports[i].Value + "'>" + airports[i].Display + "</option>");
            }
        }
    });
}


//******** Package Search Form ********
function PackageKeywordSearch() {
    if ($('#Package_Keyword').val() != "") {
        var query = $('#Package_Keyword').val();
        var URL = "http://www.newshan.com/search.aspx?";
        top.location = URL + 'q=' + query;
    } else {
        jAlertMessage("Keywords", "Please Enter your keywords");
    }
}

function PackageSearch() {
    var url;
    if($('input[name=Package_Type]:checked').val() == 'F'){
        url = PackageGatewayURL_FNE;
    }else{
        url = PackageGatewayURL_Tour;
    }
    url += "?Country=" + $('#Package_Country').val();
    url += "&City=" + $('#Package_City').val();
    url += "&Budget=" + $('#Package_Budget').val();
    url += "&Duration=" + $('#Package_Duration').val();

    //alert(url);
    window.location.assign(url);
}


