﻿
/* 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();
    CallPageMethod("AddCat", "Categories.aspx", Success, Failure, "Name", name, "Level", level, "CatID", selCat);
}

function EditCat(catID, catName) {
    ShowDialog('#AddCategory_1', 300, 400, 'Edit Category');
    $("#AddCategory_1 .Text_CatName").val(catName);
    $("#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();
    CallPageMethod("EditCat", "Categories.aspx", Success, Failure, "Name", name, "CatID", selCat);
}

function RemoveCat(selCat) {
    if (confirm("You sure you want to remove this category?")) {
        CallPageMethod("RemoveCat", "Categories.aspx", Success, Failure, "CatID", selCat);
    }
}

function AddProduct() {
    //$.fck.config = { path: '/include/fckeditor/' };
    
    var productID = $("#Text_ProductID").val();
    var title = $("#Text_ProductTitle").val();
    var subTitle = $("#Text_SubTitle").val();

    var oEditor = FCKeditorAPI.GetInstance('Text_Description');
    var description = oEditor.GetXHTML();
	description = description.replace(/"/g,"\'");

    oEditor = FCKeditorAPI.GetInstance('Text_Features');
    var features = oEditor.GetXHTML();
	features = features.replace(/"/g,"\'");
	
    oEditor = FCKeditorAPI.GetInstance('Text_QuickLook');
    var quickLook = oEditor.GetXHTML();
    quickLook = quickLook.replace(/"/g,"\'");
    
    var relatedProducts = $("#Text_Related").val();
    var HCPCS = $("#Text_HCPCS").val();
    var HMESA = $("#Text_HMESA").val();
    
    var catID = $("#Text_CatID").val();
    CallPageMethod("AddProduct", "Categories.aspx", ProductAddSuccess, Failure, "ProductID", productID, "Title", title, "Subtitle", subTitle, "Description", description, "Features", features, "QuickLook", quickLook, "RelatedProducts", relatedProducts, "HCPCS", HCPCS, "HMESA", HMESA, "CatID", catID);
}

function ProductAddSuccess(response) {

    if (response.d.indexOf("error") == -1) {
        window.location.reload();
    }
    else {
        alert(response.d);
    }
}

function LoadProduct(productID) {
    CallPageMethod("GetProductData", "Categories.aspx", SuccessLoadProduct, Failure, "ProductID", productID);
}

function SuccessLoadProduct(response) {
     var responseArr = response.d.split("|");
    // array should have 8 values
    $("#Text_ProductTitle").val(responseArr[0]);
    $("#Text_SubTitle").val(responseArr[1]);
    $("#Text_Description").val(responseArr[2]);
    $("#Text_Features").val(responseArr[3]);
    $("#Text_QuickLook").val(responseArr[4]);
    $("#Text_Related").val(responseArr[5]);
    $("#Text_HCPCS").val(responseArr[6]);
    $("#Text_HMESA").val(responseArr[7]);
}

function RemoveProduct(selProdID) {
    if (confirm("You sure you want to remove this product?")) {
        CallPageMethod("RemoveProduct", "Categories.aspx", Success, Failure, "ProductID", selProdID);
    }
}

function ShowAddProduct() {
    $("#Text_ProductID").val("");
    $("#Text_ProductTitle").val("");
    $("#Text_SubTitle").val("");
    $("#Text_Description").val("");
    $("#Text_Features").val("");
    $("#Text_QuickLook").val("");
    $("#Text_Related").val("");
    $("#Text_HCPCS").val("");
    $("#Text_HMESA").val("");
    $("#Text_ProductID").removeAttr("disabled");
    $("#Button_AddProduct").val("Add Product");
	$('textarea .fck').fck({path: '/include/fckeditor/', height: 135, toolbar: 'Basic'});
    ShowDialog('#AddProduct', 550, 650, 'Add Product');
}

function EditProduct(selHeader) {
    $("#Text_ProductID").val(selHeader);
    $("#Text_ProductID").attr("disabled", "1");
    $("#Button_AddProduct").val("Modify Product");
    LoadProduct(selHeader);
    $('textarea .fck').fck({path: '/include/fckeditor/', height: 135, toolbar: 'Basic'});
    ShowDialog('#AddProduct', 550, 650, 'Edit Product');
}