﻿$(document).ready(function() {

    var GUID = $("[id$='_hdnGUID']").val();

    updateItems(GUID);
    getItems(GUID);

    $("input.chkCompare").click(function() {

        var docID = $(this).attr("id").substring(11);

        if ($(this).is(":checked")) {

            $.ajax({
                type: "POST",
                url: "/SportDog/ProductComparer.asmx/AddComparerItem",
                data: "{DocumentID : " + docID + ", GUID: '" + GUID + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {

                    var responseText = "";

                    if (msg.hasOwnProperty("d"))
                    // Leave the .d behind and pass the rest of 
                    //  the JSON object forward.
                        responseText = msg.d;
                    else
                    // No .d; no transformation necessary.
                        responseText = msg;

                    if (responseText.length > 0) {
                        alert(responseText);
                    }
                    else {
                        updateItems(GUID)
                    }
                }
            });


        }
        else {

            $.ajax({
                type: "POST",
                url: "/SportDog/ProductComparer.asmx/DeleteComparerItem",
                data: "{DocumentID : " + docID + ", GUID: '" + GUID + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {

                    var responseText = "";

                    if (msg.hasOwnProperty("d"))
                    // Leave the .d behind and pass the rest of 
                    //  the JSON object forward.
                        responseText = msg.d;
                    else
                    // No .d; no transformation necessary.
                        responseText = msg;

                    if (responseText.length > 0) {
                        alert(responseText);
                    }
                    else {
                        updateItems(GUID)
                    }
                }
            });

        }
    });

    function updateItems(GUID) {
        $("#rightImages").load("/SportDog/ProductComparer.aspx", { 'GUID': GUID });
    }

    function getItems(GUID) {
        $.ajax({
            type: "POST",
            url: "/SportDog/ProductComparer.asmx/GetItems",
            data: "{GUID: '" + GUID + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {

                var responseText = "";

                if (msg.hasOwnProperty("d"))
                // Leave the .d behind and pass the rest of 
                //  the JSON object forward.
                    responseText = msg.d;
                else
                // No .d; no transformation necessary.
                    responseText = msg;

                eval("var x = " + responseText);

                for (i = 0; i < x.length; i++) {
                    $("input[name='" + x[i] + "']").attr('checked', true);
                }
            }
        });
    }

    $("#removeall").click(function() {

        removeall();

    });

    $("#removeallcs").click(function() {

        removeall()
        
        window.location.reload(true);

    });

    function removeall() {

        $.ajax({
            type: "POST",
            url: "/SportDog/ProductComparer.asmx/DeleteAllComparerItems",
            data: "{GUID: '" + GUID + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {

                var responseText = "";

                if (msg.hasOwnProperty("d"))
                // Leave the .d behind and pass the rest of 
                //  the JSON object forward.
                    responseText = msg.d;
                else
                // No .d; no transformation necessary.
                    responseText = msg;

                if (responseText.length > 0) {
                    alert(responseText);
                }
                else {

                    updateItems(GUID)

                    if ($("input.chkCompare")) {
                        $("input.chkCompare").attr('checked', false);
                    }

                    return true;

                }
            },
            error: function(xhr, ajaxOptions, thrownError) {

                //alert(xhr.responseText);
                //alert(thrownError);

                return false;

            }

        });

    }







});