
var url = "/domaindata/CCLQuickSearch.svc/GetQuickSearchLists";
var searchResultsURL;
var ddlMonths, ddlPorts, ddlDestinations, ddlCruiseLengths, ddlStateResidence,chkPastGuest, chkSenior;
var ddlMonthsVal, ddlPortsVal, ddlCruiseLengthVal, ddlDestinationval, ddlStateResidenceVal;
var btnSearch;
var chkPastGuestVal, chkSeniorVal;

//KJL:Added 12/8/2009 to support setting values on quicksearch
var over55, res, pg;


function Search(request, callback, error) {

    $.ajax({ url: url, data: request, type: "GET", processData: true, timeout: 30000, dataType: "json", success: callback, error: error });

}

function buildQuery(event) {
    var strQuery = "";
    // for months we will send the description not the value,
    ddlMonthsVal = ddlMonths.val();
    var ddlMonthsValText = $("#ddlMonth option[value='" + ddlMonthsVal + "']").text();
    ddlPortsVal = ddlPorts.val();
    ddlCruiseLengthVal = ddlCruiseLengths.val();
    ddlDestinationval = ddlDestinations.val();
    
    

    if (ddlMonthsVal != "Any")
        strQuery = strQuery + "month=" + ddlMonthsValText + "&";
    if (ddlPortsVal != "Any")
        strQuery = strQuery + "port=" + ddlPortsVal + "&";
    if (ddlCruiseLengthVal != "Any")
        strQuery = strQuery + "dur=" + ddlCruiseLengthVal + "&";
    if (ddlDestinationval != "Any")
        strQuery = strQuery + "dest=" + ddlDestinationval + "&";
     



    return strQuery;





}

function SetRateCriteriaCookie() {
    var cookiestring = "";
    if(ddlStateResidenceVal !="Any")
        cookiestring = cookiestring + "StateCode="+ddlStateResidenceVal+"&"
    if(chkPastGuestVal == "Y")
        cookiestring = cookiestring + "PastGuest=Y&"
    if(chkSeniorVal == "Y")
        cookiestring = cookiestring + "Senior=Y"
    
    var COOKIE_NAME = 'CurrentRatesCriteria';
    var options = { path: '/', expires: 90 };
    if (cookiestring != "");
    $.cookie(COOKIE_NAME, cookiestring, options);
}

function GetRateCriteriaCookie() {
    var cookiestring = "";
    var COOKIE_NAME = 'CurrentRatesCriteria';
    cookiestring = $.cookie(COOKIE_NAME);
    try {
	    var cookies = cookiestring.split("&");
	    for (var i = 0; i < cookies.length; i++) {
	
	        //var over55, res, pg;
	        var cookie = cookies[i].split("=");
	        switch (cookie[0])
	        {
	            case "StateCode":
	                res = cookie[1];
	                break;
	            case "PastGuest":
	                pg = cookie[1];
	                break;
	            case "Senior":
	                over55 = cookie[1];
	                break;
	        }
	    }        
		}
		catch(e) { }
}

function SetUIFromCookie()
{
    GetRateCriteriaCookie();
    if (res != "Any" && res != "XX" && res != "") {
        ddlStateResidence.val(res);
        ddlStateResidenceVal = res;
        ddlStateResidence.value = res;
    }

    if (pg == "Y")
    {
        chkPastGuestVal = "Y";
        chkPastGuest.attr('checked', true);
    }
	else
	{
        chkPastGuestVal = "N";
        chkPastGuest.attr('checked', false);
	}

    if (over55 == "Y") {
        chkSeniorVal = "Y";
        chkSenior.attr('checked', true);
    }    
	else
	{
        chkSeniorVal = "N";
        chkSenior.attr('checked', false);
	}
}

function dropDownChanged(event) {

    var strQuery = buildQuery();
    Search(strQuery, Success, Failed);


}

function specialRatesChanged(){

ddlStateResidenceVal = ddlStateResidence.val();

if (chkPastGuest.is(':checked'))
        chkPastGuestVal = "Y";
    else
        chkPastGuestVal = "N";

    if (chkSenior.is(':checked'))
        chkSeniorVal = "Y";
    else
        chkSeniorVal = "N"; 

}

function Success(response) {
    ResetLists();
    BuildDropDowns(response.d);
    RestoreSelctions();

}

function Failed() {
    ResetLists();
}

function BuildDropDowns(d) {
    if (d.MonthYear != null) {
        for (i = 0; i < d.MonthYear.length; i++)
        { AddOptions(ddlMonths, d.MonthYear[i].code, d.MonthYear[i].description); }

    }
    if (d.DeparturePorts != null) {
        for (i = 0; i < d.DeparturePorts.length; i++)
        { AddOptions(ddlPorts, d.DeparturePorts[i].code, d.DeparturePorts[i].description); }

    }
    if (d.Destinations != null) {
        for (i = 0; i < d.Destinations.length; i++)
        { AddOptions(ddlDestinations, d.Destinations[i].code, d.Destinations[i].description); }

    }
    if (d.CruiseLength != null) {
        for (i = 0; i < d.CruiseLength.length; i++)
        { AddOptions(ddlCruiseLengths, d.CruiseLength[i].code, d.CruiseLength[i].description); }

    }




}

function AddOptions(target, value, option) {
    var strOption = "<option value=\"" + value + "\">" + option + "</option>"
    target.append(strOption);

}

function ResetLists() {
    ddlMonths.html("<option value=\"Any\">Any Month</option>");
    ddlPorts.html("<option value=\"Any\">Any Embarkation Port</option>");
    ddlDestinations.html("<option value=\"Any\">Any Destination</option>");
    ddlCruiseLengths.html("<option value=\"Any\">Any Cruise Length</option>");
}

function RestoreSelctions() {
    ddlMonths.val(ddlMonthsVal);
    ddlPorts.val(ddlPortsVal);
    ddlDestinations.val(ddlDestinationval);
    ddlCruiseLengths.val(ddlCruiseLengthVal);

}

function SearchClick() {

// initilize variables in case user just clicks Search
    var strQuery = "";
    ddlMonthsVal = ddlMonths.val();
    ddlPortsVal = ddlPorts.val();
    ddlCruiseLengthVal = ddlCruiseLengths.val();
    ddlDestinationval = ddlDestinations.val();
    specialRatesChanged();
        
        
    
        if (ddlMonthsVal != "Any")
            strQuery = strQuery + "dat=" + ddlMonthsVal + "&";
        if (ddlPortsVal != "Any")
            strQuery = strQuery + "embkCode=" + ddlPortsVal + "&";
        if (ddlCruiseLengthVal != "Any")
            strQuery = strQuery + "dur=" + ddlCruiseLengthVal + "&";
        if (ddlDestinationval != "Any")
            strQuery = strQuery + "dest=" + ddlDestinationval + "&";

        if (ddlStateResidenceVal != "Any")
            strQuery = strQuery + "StateCode=" + ddlStateResidenceVal + "&";
        if (chkPastGuestVal == "Y")
            strQuery = strQuery + "PastGuest=" + chkPastGuestVal + "&";
        if (chkSeniorVal == "Y")
            strQuery = strQuery + "Senior=" + chkSeniorVal + "&";

    SetRateCriteriaCookie();
    document.location.href = searchResultsURL+"?"+strQuery;
        


}

function SuccessItineraryPage(response) {
    ResetLists();
    BuildDropDowns(response.d);
    if (!firstLoad)
        RestoreSelctions();
    else {
         //ie6 timing hack with val()
        setTimeout(BindQueryStringValues, 500);
    }
    firstLoad = false;
}



function BindQueryStringValues() {
   
    ddlPorts.val($.query.GET("embkCode"));
    ddlDestinations.val($.query.GET("subRegionCode"));
    if($.query.GET("durDays") != null)
        ddlCruiseLengths.val(GetDurationValue(parseInt($.query.GET("durDays"))));
    if ($.query.GET("sailDate") != null)
        ddlMonths.val(GetMonthValue($.query.GET("sailDate")));
}

function GetDurationValue(durDays) {
    if (durDays > 1 && durDays <= 5)
        return "D1";
    if (durDays > 5 && durDays <= 9)
        return "D2";
    if (durDays >= 10)
        return "D3";
        
}

function GetMonthValue(monthVal) {
    var month = monthVal.substr(0, 2);
    if(month.indexOf("/") != -1)
        month = "0" + monthVal.substring(0, 1);
    else
        month = monthVal.substr(0, 2);
    var year = monthVal.substr(monthVal.lastIndexOf("/")+1, 4);
    return month + year;


}

