﻿var SearchWSURL = '/Services/Search.asmx';

function callWebService(webServiceUrl, webMethodName, data, delegateFunction) {
    var url = webServiceUrl + "/" + webMethodName;

    if (data == null || data.length == 0) {
        data = '{}';
    }

    var runningRequest = $.ajax({
        type: "POST",
        url: url,
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: delegateFunction,
        error: function(error) {
            alert("There was an error processing your request.\n"
                    + "[" + error.status + "]"
                    + " [" + error.statusText + "]");
        }
    });
    $(window).bind("beforeunload", function() {
        runningRequest.abort();
    });
}

function resetValues() {

    var radioButtons = $get('divRadioContainer').getElementsByTagName('input');

    for (var i = 0; i < radioButtons.length; i++)
        if (radioButtons[i].type == 'radio')
        if (radioButtons[i].value == '1')
        radioButtons[i].checked = true;
    else
        radioButtons[i].checked = false;

    $get('rightNavResults').style.display = 'none';
    $get('resultHeader').style.display = 'none';
}

function getValues() {
    var finalValues = new Array();
    var currentItem = 0;
    var radioButtons = $get('divRadioContainer').getElementsByTagName('input');

    //There are 5 Radio buttons per section, so there should always be a multiple of 5. So every 5, there is a new section.
    for (var i = 0; i < radioButtons.length; i += 5)
        for (var r = i; r < (i + 5); r++)
        if (radioButtons[r].checked) {
        finalValues[currentItem] = new Array();
        finalValues[currentItem][0] = (i / 5);
        finalValues[currentItem][1] = radioButtons[r].value;
        currentItem++;
    }

    return finalValues;
}

function getShips(data, caller) {
    caller.value = 'Please Wait...';
    callWebService(SearchWSURL, 'GetShipsByCriteria', "{ criteria: " + JSON.stringify(data) + "}", function(resultData) {
        resultData = resultData.d;
        var resultsContainer = document.getElementById('rightNavResults');
        var resultStructure = document.getElementById('resultTemplate');

        //Empty contents of result div
        while (resultsContainer.childNodes.length > 0)
            resultsContainer.removeChild(resultsContainer.lastChild);



        //Loop through all the Results
        var addMargin = true;
        for (var i = 0; i < resultData.length; i++) {
            var newNode = resultStructure.cloneNode(true);
            newNode.id = 'result_' + i.toString();
            newNode.style.display = 'block';

            for (var n = 0; n < newNode.childNodes.length; n++) {
                var childNode = newNode.childNodes[n];
                switch (childNode.id) {
                    case 'resultTemplate_image':
                        {
                            if (resultData[i].ImageUrl != '' && resultData[i].ImageUrl != null) {
                                childNode.src = resultData[i].ImageUrl + '?width=75&height=75';
                                childNode.alt = resultData[i].Name;
                                childNode.title = resultData[i].Name;
                            }

                            childNode.alt = resultData[i].Name;
                        }
                        break;
                    case 'resultTemplate_title':
                        {
                            childNode.innerHTML = '<a class="clearBoth bold" href="' + resultData[i].ShipUrl + '" title="' + resultData[i].Name + '">' + resultData[i].Name + '</a>' + ' ' + '<a href="' + resultData[i].CruiselineUrl + '" title="' + resultData[i].Cruiseline + '">' + '(' + resultData[i].Cruiseline + ')' + '</a>';
                        }
                        break;
                    case 'resultTemplate_description':
                        {
                            childNode.innerHTML = resultData[i].ShortDescription;
                        }
                        break;
                }
            }

            //Add a margin to each of the left most divs
            if (addMargin)
                newNode.style.marginRight = '15px';

            addMargin = !addMargin;

            resultsContainer.appendChild(newNode);
        }

        //Clear both sides
        var endDiv = $get('resultEnd').cloneNode(true);
        endDiv.style.display = 'block';
        resultsContainer.appendChild(endDiv);

        //Set header
        var resultsHeader = $get('resultHeader');

        if (resultData.length >= 2) {
            resultsHeader.style.display = 'block';
            resultsHeader.innerHTML = 'Here are a selection of cruise ships which match your choices:';
        }
        else if (resultData.length == 1) {
            resultsHeader.style.display = 'block';
            resultsHeader.innerHTML = 'Here is a cruise ship which matches your choices:';
        }
        else
            resultsHeader.style.display = 'none';

        //Show the Results Panel
        resultsContainer.style.display = 'block';
        caller.value = 'Search';
    });
}
