
var url = "/BookingEngine/QuickSearch/";
var searchResultsURL = "/BookingEngine/SailingSearch/Search2/";
var ddlMonthsHeader, ddlPortsHeader, ddlDestinationsHeader, ddlCruiseLengthsHeader, ddlStateResidenceHeader, chkPastGuestHeader, chkSeniorHeader;
var ddlMonthsValHeader, ddlPortsValHeader, ddlCruiseLengthValHeader, ddlDestinationvalHeader, ddlStateResidenceValHeader;
var btnSearchHeader;
var chkPastGuestValHeader, chkSeniorValHeader;


function SearchHeader(request, callback, error) {

    $.ajax({ url: url, data: request, type: "GET", processData: true, timeout: 30000, dataType: "json", success: callback, error: error });

}

function buildQueryHeader(event) {
    var strQuery = "";
    // for months we will send the description not the value,
    ddlMonthsValHeader = ddlMonthsHeader.val();
    //var ddlMonthsValText = $("#ddlMonthHeader-top option[value='" + ddlMonthsValHeader + "']").text();
    ddlPortsValHeader = ddlPortsHeader.val();
    ddlCruiseLengthValHeader = ddlCruiseLengthsHeader.val();
    ddlDestinationvalHeader = ddlDestinationsHeader.val();



    if (ddlMonthsValHeader != "Any")
        strQuery = strQuery + "month=" + ddlMonthsValHeader /*ddlMonthsValText*/ + "&";
    if (ddlPortsValHeader != "Any")
        strQuery = strQuery + "port=" + ddlPortsValHeader + "&";
    if (ddlCruiseLengthValHeader != "Any")
        strQuery = strQuery + "dur=" + ddlCruiseLengthValHeader + "&";
    if (ddlDestinationvalHeader != "Any")
        strQuery = strQuery + "dest=" + ddlDestinationvalHeader + "&";

/*
console.debug("Months %s  Ports %s  Length %s  Desti %s query %s", ddlMonthsValHeader, ddlPortsValHeader,
ddlCruiseLengthValHeader, ddlDestinationvalHeader, strQuery);
*/
    return strQuery;
}

function SetRateCriteriaCookieHeader() {
    var cookieArr = [];
    if (ddlStateResidenceValHeader != "Any")
        cookieArr.push("StateCode=" + ddlStateResidenceValHeader);
    if (chkPastGuestValHeader == "Y")
        cookieArr.push("PastGuest=Y")
    if (chkSeniorValHeader == "Y")
        cookieArr.push("Senior=Y")

    var COOKIE_NAME = 'CurrentRatesCriteria';
    var options = { path: '/', expires: 90 };
    $.cookie(COOKIE_NAME, cookieArr.join("&"), options);




}

function SetRateCriteriaCookiePastGuest() {
    var COOKIE_NAME = 'CurrentRatesCriteria';
    var cookiestring = $.cookie(COOKIE_NAME);
    if (cookiestring != null && cookiestring.indexOf("PastGuest=Y") == -1)
	{
		cookiestring = cookiestring + ((cookiestring.charAt(cookiestring.length-1) == "&") ? "" : "&") + "PastGuest=Y";
	}
    else
	{
		cookiestring = "PastGuest=Y"

	}
    var options = { path: '/', expires: 90 };
    if (cookiestring != "")
    $.cookie(COOKIE_NAME, cookiestring, options);
}

function SetRateCriteriaCookieSenior() {
    var COOKIE_NAME = 'CurrentRatesCriteria',
        cookiestring = $.cookie(COOKIE_NAME);
    if (cookiestring != null && cookiestring.indexOf("Senior=Y") == -1) {

			cookiestring = cookiestring + ((cookiestring.charAt(cookiestring.length-1) == "&") ? "" : "&") + "Senior=Y"
    }
    else {
        cookiestring = "Senior=Y"
    }
    var options = { path: '/', expires: 90 };
    if (cookiestring != "")
    $.cookie(COOKIE_NAME, cookiestring, options);

}

function RemoveRateCriteriaCookiePastGuest() 
{
    var COOKIE_NAME = 'CurrentRatesCriteria',
        cookiestring = $.cookie(COOKIE_NAME);
		console.debug("cookiestr %s", cookiestring);
    if (cookiestring != null && cookiestring.indexOf("PastGuest=Y") > -1)
	{
        cookiestring = cookiestring.replace("PastGuest=Y", "");
		
    }
    var options = { path: '/', expires: 90 };
    if (cookiestring != "")
    $.cookie(COOKIE_NAME, cookiestring, options);

}

function RemoveRateCriteriaCookieSenior() 
{
    var COOKIE_NAME = 'CurrentRatesCriteria',
        cookiestring = $.cookie(COOKIE_NAME);
    if (cookiestring != null && cookiestring.indexOf("Senior=Y") > -1) 
    {
		   cookiestring = cookiestring.replace("Senior=Y", "");
    }
	
    var options = { path: '/', expires: 90 };
    if (cookiestring != "")
    $.cookie(COOKIE_NAME, cookiestring, options);

}


function SetRateCriteriaCookieState(state) {

    var COOKIE_NAME = 'CurrentRatesCriteria',
        cookiestring = $.cookie(COOKIE_NAME);

	//console.debug("state %s", state);

    if (cookiestring != null) {
		console.log("cookie in set state");
        if (cookiestring.indexOf("StateCode=") == -1) {
            cookiestring = cookiestring +((cookiestring.charAt(cookiestring.length-1) == "&") ? "" : "&") + "StateCode=" + state;
			//console.debug("cookiestr %s", cookiestring);
		
		}
		else 
		{
			var stateCode= "StateCode=",
				stateCodeIndex = cookiestring.indexOf(stateCode),
				length = stateCodeIndex  + stateCode.length + 2,
				stateCodeVal = cookiestring.substring( stateCodeIndex, length);

			if(state == "")
			{
				cookiestring = cookiestring.replace(stateCodeVal, "");
			}
			else
			{
				cookiestring = cookiestring.replace(stateCodeVal, stateCode  + state);
			}
			//console.debug("has one already cookiestr %s", cookiestring);
		}
    }
    else {
        cookiestring = "StateCode=" + state;

    }
    var options = { path: '/', expires: 90 };
    if (cookiestring != "")
    $.cookie(COOKIE_NAME, cookiestring, options);

}

function GetRateCriteriaCookie() {
	var COOKIE_NAME = 'CurrentRatesCriteria',
        cookiestring = $.cookie(COOKIE_NAME),
		temp = [],
		cookieObj = {};
		
	if(cookiestring != null && cookiestring.length > 0 ){
	
		if(cookiestring.indexOf("&") > -1) {
			cookieArr = cookiestring.split("&");
			for(var i =0, len = cookieArr.length; i < len; i++){
				temp = cookieArr[i].split("=");
				cookieObj[temp[0]] = temp[1];
			}
		}
		else {
			cookieArr = cookiestring.split("=");
			cookieObj[cookieArr[0]] = cookieArr[1];
		}
	}
	
		
	
	return cookieObj;	
}


function dropDownChangedHeader(event) {

    var strQuery = buildQueryHeader();
    SearchHeader(strQuery, SuccessHeader, FailedHeader);


}

function specialRatesChangedHeader() {

    ddlStateResidenceValHeader = ddlStateResidenceHeader.val();
    chkPastGuestValHeader = (chkPastGuestHeader.attr("checked")) ? "Y" : "N";
    chkSeniorValHeader = (chkSeniorHeader.attr("checked")) ? "Y" : "N";
    

}

function SuccessHeader(response) {
    ResetListsHeader();
    BuildDropDownsHeader(response);
    RestoreSelctionsHeader();

}

function FailedHeader() {
   // alert("it faield");
    ResetListsHeader();
}

function BuildDropDownsHeader(d) {
    if (d.MonthYear != null) {
        for (i = 0; i < d.MonthYear.length; i++)
        { AddOptions(ddlMonthsHeader, d.MonthYear[i].Code, d.MonthYear[i].Description); }

    }
    if (d.DeparturePorts != null) {
        for (i = 0; i < d.DeparturePorts.length; i++)
        { AddOptions(ddlPortsHeader, d.DeparturePorts[i].Code, d.DeparturePorts[i].Description); }

    }
    if (d.Destinations != null) {
        for (i = 0; i < d.Destinations.length; i++)
        { AddOptions(ddlDestinationsHeader, d.Destinations[i].Code, d.Destinations[i].Description); }

    }
    if (d.CruiseLength != null) {
        for (i = 0; i < d.CruiseLength.length; i++)
        { AddOptions(ddlCruiseLengthsHeader, d.CruiseLength[i].Code, d.CruiseLength[i].Description); }

    }




}

function AddOptions(target, value, option) {
    var strOption = "<option value=\"" + value + "\">" + option + "</option>"
    target.append(strOption);

}

function ResetListsHeader() {
    ddlMonthsHeader.html("<option value=\"Any\">Any Month</option>");
    ddlPortsHeader.html("<option value=\"Any\">Any Embarkation Port</option>");
    ddlDestinationsHeader.html("<option value=\"Any\">Any Destination</option>");
    ddlCruiseLengthsHeader.html("<option value=\"Any\">Any Cruise Length</option>");
}

function RestoreSelctionsHeader() {
    ddlMonthsHeader.val(ddlMonthsValHeader);
    ddlPortsHeader.val(ddlPortsValHeader);
    ddlDestinationsHeader.val(ddlDestinationvalHeader);
    ddlCruiseLengthsHeader.val(ddlCruiseLengthValHeader);

}

function SearchClickHeader() {

    // initilize variables in case user just clicks Search
    var strQuery = new Array();
    ddlMonthsValHeader = ddlMonthsHeader.val();
    ddlPortsValHeader = ddlPortsHeader.val();
    ddlCruiseLengthValHeader = ddlCruiseLengthsHeader.val();
    ddlDestinationvalHeader = ddlDestinationsHeader.val();
    specialRatesChangedHeader();




    if (ddlMonthsValHeader != "Any")
        strQuery.push("dat=" + ddlMonthsValHeader);
    if (ddlPortsValHeader != "Any")
        strQuery.push("embkCode=" + ddlPortsValHeader);
    if (ddlCruiseLengthValHeader != "Any")
        strQuery.push("dur=" + ddlCruiseLengthValHeader);
    if (ddlDestinationvalHeader != "Any")
        strQuery.push("dest=" + ddlDestinationvalHeader);

    if (ddlStateResidenceValHeader != "Any" && ddlStateResidenceValHeader.length > 0)
        strQuery.push("StateCode=" + ddlStateResidenceValHeader);
    if (chkPastGuestValHeader == "Y")
        strQuery.push("PastGuest=" + chkPastGuestValHeader);
    if (chkSeniorValHeader == "Y")
        strQuery.push("Senior=" + chkSeniorValHeader);

    //alert( searchResultsURL + "?" + strQuery.join("&"));

    SetRateCriteriaCookieHeader();
    location.href = searchResultsURL + "?" + strQuery.join("&");



}



    
    

