﻿
/* AJAX helper function to talk to ASP.net page method */
function CallPageMethod(methodName, loc, onSuccess, onFail) {
    var args = '';
    var l = arguments.length;
    if (l > 4) {
        for (var i = 4; i < l - 1; i += 2) {
            if (args.length != 0) args += ',';
            args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
        }
    }
    $.ajax({
        type: "POST",
        url: loc + "/" + methodName,
        data: "{" + args + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onSuccess,
        fail: onFail
    });
}

function Failure(response) {
    alert(response.d);
}

function Success(response) {

    if (response.d.indexOf("error") == -1) {
        window.location.reload();
    }
    else {
        alert(response.d);
    }
}

function ShowDialog(DivID, heightVar, widthVar, title) {
    $(DivID).show();
    $(DivID).dialog({ bgiframe: true, modal: true,
        height: heightVar, width: widthVar,
        autoOpen: false, title: title
    });
    $(DivID).dialog("open");
}

function AddCat(level) {

    var name = $("#AddCategory_" + level + " .Text_CatName").val();
    var selCat = $("#AddCategory_" + level + " .Text_SelCatID").val();
    var subtitle = $("#AddCategory_" + level + " .Text_CatSubtitle").val();
    var description = $("#AddCategory_" + level + " .Text_CatDescription").val();
    var type = $("#AddCategory_" + level + " .Text_Type").val();
    var link = $("#AddCategory_" + level + " .Text_LinkTo").val();

    CallPageMethod("AddCat", "Catalog.aspx", Success, Failure, "Name", name, "Level", level, "CatID", selCat, "Subtitle", subtitle, "Description", description, "Type", type, "LinkTo", link);
}

function EditCat(catID, catName, subtitle, description, link) {
    ShowDialog('#AddCategory_1', 320, 450, 'Edit Category');
    $("#AddCategory_1 .Text_CatName").val(catName);
    $("#AddCategory_1 .Text_CatSubtitle").val(subtitle);
    $("#AddCategory_1 .Text_CatDescription").val(description);
    $("#AddCategory_1 .Text_LinkTo").val(link);

    $("#AddCategory_1 .Text_SelCatID").val(catID);
    $("#AddCategory_1 .Button_AddCat").val("Edit Category");
    $("#AddCategory_1 .Button_AddCat").attr("OnClick", "EditCatSubmit();");
}

function EditCatSubmit() {
    var name = $("#AddCategory_1 .Text_CatName").val();
    var selCat = $("#AddCategory_1 .Text_SelCatID").val();
    var subtitle = $("#AddCategory_1 .Text_CatSubtitle").val();
    var description = $("#AddCategory_1 .Text_CatDescription").val();
    var link = $("#AddCategory_1 .Text_LinkTo").val();

    CallPageMethod("EditCat", "Catalog.aspx", Success, Failure, "Name", name, "CatID", selCat, "Subtitle", subtitle, "Description", description, "LinkTo", link);
}

function RemoveCat(selCat, type) {
    if (confirm("You sure you want to remove this category?")) {
        CallPageMethod("RemoveCat", "Catalog.aspx", Success, Failure, "CatID", selCat, "Type", type);
    }
}

function AddProduct() {
    //$.fck.config = { path: '/include/fckeditor/' };

    var productID = $("#Text_ItemNumber").val();
    var title = $("#Text_Title").val();
    var subTitle = $("#Text_SubTitle").val();
    var sort = $("#Text_Sort").val();
    var status = $("#Text_Status").val();
    var styleType = $("#Text_StyleType").val();
    var catID = $("#Text_CatID").val();
    var key4 = $("#Text_Keywords4").val();
    var key5 = $("#Text_Keywords5").val();

    var oEditor = $('#Text_Description');
    var description = "";

    if (oEditor) {
        description = oEditor.val().replace(/"/g, "\'");
    }
    else {
        description = $("#Text_Description").val();
    }

    oEditor = $('#Text_Keywords1');
    var key1 = "";

    if (oEditor) {
        key1 = oEditor.val().replace(/"/g, "\'");
    }
    else {
        key1 = $("#Text_Keywords1").val();
    }

    oEditor = $('#Text_Keywords2');
    var key2 = "";
    if (oEditor) {
        key2 = oEditor.val().replace(/"/g, "\'");
    }
    else {

        key2 = $("#Text_Keywords2").val();
    }

    oEditor = $('#Text_Keywords3');
    var key3 = "";
    if (oEditor) {
        key3 = oEditor.val().replace(/"/g, "\'");
    }
    else {
        key3 = $("#Text_Keywords3").val();
    }

    oEditor = $('#Text_Upsell');
    var upsell = "";
    if (oEditor) {
        upsell = oEditor.val().replace(/"/g, "\'");
    }
    else {
        upsell = $("#Text_Upsell").val();
    }

    CallPageMethod("AddProduct", "Catalog.aspx", ProductAddSuccess, Failure, "ProductID", productID, "Title", title, "Subtitle", subTitle, "Description", description, "Key1", key1, "Key2", key2, "Key3", key3, "Key4", key4, "Key5", key5, "Upsell", upsell, "StyleType", styleType, "CatID", catID, "Sort", sort);
}

function ProductAddSuccess(response) {

    if (response.d.indexOf("error") == -1) {
        window.location.reload();
    }
    else {
        alert(response.d);
    }
}

function LoadProduct(productID) {
    CallPageMethod("GetProductData", "Catalog.aspx", SuccessLoadProduct, Failure, "ProductID", productID);
}

function SuccessLoadProduct(response) {
    var responseArr = response.d.split("|");
    // array should have 8 values
    $("#Text_Title").val(responseArr[0]);
    $("#Text_SubTitle").val(responseArr[1]);
    $("#Text_Description").val(responseArr[2]);
    $("#Text_Keywords1").val(responseArr[3]);
    $("#Text_Keywords2").val(responseArr[4]);
    $("#Text_Keywords3").val(responseArr[5]);
    $("#Text_Keywords4").val(responseArr[6]);
    $("#Text_Keywords5").val(responseArr[7]);
    $("#Text_Upsell").val(responseArr[8]);
    $("#Text_StyleType").val(responseArr[9]);
    $("#Text_Sort").val(responseArr[10]);
    $("#Text_Status").val(responseArr[11]);
}

function RemoveProduct(selProdID) {
    if (confirm("You sure you want to remove?")) {
        CallPageMethod("RemoveProduct", "Catalog.aspx", Success, Failure, "ProductID", selProdID);
    }
}

function ShowAddProduct() {
    $("#Text_ItemNumber").val("");
    $("#Text_Title").val("");
    $("#Text_SubTitle").val("");
    $("#Text_Description").val("");
    $("#Text_Keywords1").val("");
    $("#Text_Keywords2").val("");
    $("#Text_Keywords3").val("");
    $("#Text_Keywords4").val("");
    $("#Text_Keywords5").val("");
    $("#Text_Upsell").val("");
    $("#Text_StyleType").val("");
    $("#Text_Sort").val("");
    $("#Text_ItemNumber").removeAttr("disabled");
    $("#Button_AddProduct").val("Add");
    $('textarea.fck').ckeditor();
    ShowDialog('#AddProduct', 600, 750, 'Add');
}

function EditProduct(selHeader) {
    $("#Text_ItemNumber").val(selHeader);
    $("#Text_ItemNumber").attr("disabled", "1");
    $("#Button_AddProduct").val("Modify");
    LoadProduct(selHeader);
    $('textarea.fck').ckeditor();
    ShowDialog('#AddProduct', 600, 750, 'Edit');
}

function showAddExistingContent() {
    $("#newContent").hide();
    $("#existingContent").show(200);
}

function showAddNewContent() {
    $("#newContent").show(200);
    $("#existingContent").hide();
}

function inPageAddContent(pageID, phID) {
    $("#Hidden_PageID").val(pageID);
    $("#Hidden_PHID").val(phID);
    ShowDialog('#contentAdd', 550, 800, 'Add Content To Page');
    $("#Text_Content").ckeditor();
}

function inPageEditContent(pageID, phID, contentID) {
    ShowDialog('#contentEdit', 550, 800, 'Edit Content');
    $("#Hidden_ContentID").val(contentID);
    $("a#editLinkDetails").attr("href", "/admin/core/Content.aspx?ContentID=" + contentID);
    $('#fckEdit').ckeditor();
    CallPageMethod("GetContent", "/admin/core/content.aspx", SuccessGetContent, Failure, "ContentID", contentID);
}

function SuccessGetContent(response) {
    var str = response.d
    $("#fckEdit").val(str);
}

function saveContent() {
    var contentID = $("#Hidden_ContentID").val();
    var content = "";

    content = $("#fckEdit").val().replace(/"/g, "\'");
    CallPageMethod("SaveContent", "/admin/core/content.aspx", Success, Failure, "ContentID", contentID, "Content", content);
}

function addExistingContent(selectListID) {
    var pageID = $("#Hidden_PageID").val();
    var phID = $("#Hidden_PHID").val();
    var contentID = $("#" + selectListID).val();
    CallPageMethod("AddExistingContent", "/admin/core/content.aspx", Success, Failure, "ContentID", contentID, "PageID", pageID, "PlaceHolderID", phID);
}

function addNewContent() {
    var pageID = $("#Hidden_PageID").val();
    var phID = $("#Hidden_PHID").val();
    var category = $("#Text_Category").val();
    var title = $("#Text_Title").val();
    var content = $("#Text_Content").val().replace(/"/g, "\'");

    CallPageMethod("AddNewContent", "/admin/core/content.aspx", Success, Failure, "Title", title, "Category", category, "Content", content, "PageID", pageID, "PlaceHolderID", phID);
}

function inPageRemovePlaceHolder(pageID, phID, contentID) {
    CallPageMethod("RemoveContent", "/admin/core/content.aspx", Success, Failure, "ContentID", contentID, "PageID", pageID, "PlaceHolderID", phID);
}

function inPageMove(pageID, phID, contentID, pageDetailsID, up) {
    CallPageMethod("MoveContent", "/admin/core/content.aspx", Success, Failure, "DetailsRowID", pageDetailsID, "PageID", pageID, "PlaceHolderID", phID, "Up", up);
}

function toggleCMSLinks() {
    $(".cmsZone").toggle();
}