///<reference path="https://ipp.tradingplaces.com/static/scripts/jquery-1.3.2-vsdoc.js" />

$(document).ready(function() {
    Date.prototype.toShortDateString = function() {
        return this.getMonth() + 1 + "/" + this.getDate() + "/" + this.getFullYear();
    }

    if (typeof Admin == "undefined") {
        window.Admin = new function() {
            this.toggleItemActivation = function(itemType, id) {
                var currentClass = $("#" + itemType.toString().toUpperCase() + "_" + id).attr("class");
                var functionUrl = "";
                var newClass = "";
                var newText = "";

                if (currentClass == "activate") {
                    functionUrl = "https://ipp.tradingplaces.com/Services/MasterDetailsWebService.svc/Activate" + itemType;
                    newClass = "deactivate";
                    newText = "Deactivate";
                }
                else if (currentClass == "deactivate") {
                    functionUrl = "https://ipp.tradingplaces.com/Services/MasterDetailsWebService.svc/Deactivate" + itemType;
                    newClass = "activate";
                    newText = "Activate";
                }

                $.ajax({
                    type: "POST",
                    url: functionUrl,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ itemId: id }),
                    success:
                    function(data) {
                        if (data.d == false) {
                            //error saving data to DB
                            alert("An error occurred while saving to the database!");
                            return;
                        }
                        $("#" + itemType.toString().toUpperCase() + "_" + id).removeClass(currentClass).addClass(newClass).text(newText);
                    },
                    error:
                    function(XMLHttpRequest, textStatus, errorThrown) {
                        alert("Error Occurred!");
                        alert(errorThrown);
                        alert("Status:" + textStatus);
                        alert("XMLHTTPRequest: " + XMLHttpRequest.toString());
                    }
                });
            }

            this.deleteItem = function(itemType, id) {
                var functionUrl = "https://ipp.tradingplaces.com/Services/MasterDetailsWebService.svc/Remove" + itemType;

                $.ajax({
                    type: "POST",
                    url: functionUrl,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ itemId: id }),
                    success:
                    function(data) {
                        if (data.d == false) {
                            alert(data.d);
                            //error saving data to DB
                            alert("An error occurred while saving to the database!");
                            return;
                        }
                        $("#" + itemType.toString().toUpperCase() + "_" + id).parents("tr:last").css("display", "none");
                    },
                    error:
                    function(XMLHttpRequest, textStatus, errorThrown) {
                        alert("Error Occurred!");
                        alert(errorThrown);
                        alert("Status:" + textStatus);
                        alert("XMLHTTPRequest: " + XMLHttpRequest.toString());
                    }
                });
            }

            this.showModify = function() {
                $("table[id*='tableBookedNights']").hide('normal');
                $("#divModifyBookedNights").show('normal');
            }

            this.hideModify = function() {
                $("#divModifyBookedNights").hide('normal');
                $("table[id*='tableBookedNights']").show('normal');
            }
        }
    }
});