/// <reference path="~/Scripts/_references.js" />

Generic_Forms_SelectTab();

function Product_Create()
{

}

function Product_Edit(modelID)
{
    //comom  functions for edit
    var link = "[DATA]";
    Generic_List_LoadDataTable("ListSubProducts", 0, 2, "Product", "", "DeleteSubProduct", "ListSubProducts/" + modelID, false, true, false, null, false, false, link);
}

function Product_AddOn_Edit(modelID) {
    Generic_List_LoadDataTable("ListPriceDataTable", 0, 3, "Price", "EditAddOn", "Delete", "List/" + modelID, true, true, false, null, false, false);
}

function Product_SaaS_Edit(modelID) {

    Product_Edit(modelID);

    $('#HasDomain').on('ifChanged', function (event) {
        HasDomainChange();
    });

    $('#IsSubDomain').on('ifChanged', function (event) {
        IsSubDomainChange();
    });

    Generic_List_LoadDataTable("ListPriceDataTable", 0, 3, "Price", "EditSaaS", "Delete", "List/" + modelID, true, true, false, null, false, false);
    HasDomainChange();
}

function Product_License_Edit(modelID) {
    Product_Edit(modelID);
    Generic_List_LoadDataTable("ListPriceDataTable", 0, 3, "Price", "EditLicense", "Delete", "List/" + modelID, true, true, false, null, false, false);
}


function Product_Other_Edit(modelID) {
    Product_Edit(modelID);
    Generic_List_LoadDataTable("ListPriceDataTable", 0, 3, "Price", "EditOther", "Delete", "List/" + modelID, true, true, false, null, false, false);
}

function Product_Host_Edit(modelID, hostingServerId)
{
    Product_Edit(modelID);
    Generic_List_LoadDataTable("ListPriceDataTable", 0, 3, "Price", "EditHost", "Delete", "List/" + modelID, true, true, false, null, false, false);
    OnPanelChange(hostingServerId, modelID);
}

function Product_Domain_Edit(modelID, registrarId)
{
    Product_Edit(modelID);
    OnDomainOptClick();
    Generic_List_LoadDataTable("ListPriceDataTable", 0, 2, "Price", "EditDomain", "Delete", "ListDomain/" + modelID, true, true, false, null, false, false);
    OnRegistrarChange(registrarId, modelID);
}


function Product_List(productType)
{
    Generic_List_LoadDataTable("ListProductDataTable", 0, 3, "Product", "Edit", "Delete", "List/" + productType, true, true, false, null, true, true, null, null, true, "Copy");
}


function Product_Group_List()
{
    Generic_List_LoadDataTable("ListProductGroupDataTable", 0, 2, "ProductGroup", "Edit", "Delete", "List", true, true, false, null, true, true);
}

function AddSubProduct(modelId)
{
    var subproduct = $('#SubProduct').val();
    Generic_Forms_LoadDialog();

    $.post(ZB_Relative_Path + "ZBAdmin/Product/CreateSubProduct/" + modelId + "/" + subproduct)
           .success(function (data) {

               Generic_Forms_CloseLoadDialog();

               if (data.HasErrors  != true) {

                   Generic_List_RefreshDataGrid("ListSubProducts");

               } else {
                   Generic_Functions_ShowErrorDialog(Languages_BackOffice_Resource_Error, data.Description);
               }

           })
           .fail(function (data) {

               Generic_Forms_CloseLoadDialog();

               Generic_Functions_ShowErrorDialog(Languages_BackOffice_Resource_Error, JSON.stringify(data));

           });
}

//Hosting *************************

function OnPanelChange(hostingsID, regarding) {
   
    if (hostingsID != "" && hostingsID != null) {
        $("#ExecutionArea").show();
        $("#PanelConfig").show();
        $("#PanelConfig").html("<img src='" + ZB_Relative_Path + "images/ajax-loader.gif' alt='' />");
        $("#PanelConfig").load(ZB_Relative_Path+"ZBAdmin/Product/PanelForm/" + hostingsID + "/" + regarding);
    } else {
        $("#ExecutionArea").hide();
        $("#PanelConfig").hide();
    }
     
}

function OnDomainOptClick() {

    if ($('#IsDomainTransfer').is(':checked') == true || $('#IsDomainChoise').is(':checked') == true) {
        $("#RelatedProductArea").show();
    } else {
        $("#RelatedProductArea").hide();
    }
}

$('#IsDomainTransfer').on('ifChanged', function (event) {
    OnDomainOptClick();
});

$('#IsDomainChoise').on('ifChanged', function (event) {
    OnDomainOptClick();
});

//**********

//Domain *************


function OnRegistrarChange(registrarID, regarding) {
         
    if (registrarID != "") {
        $("#ExecutionArea").show();
        $("#RegistrarConfig").show();
        $("#RegistrarConfig").html("<img src='"+ZB_Relative_Path+"images/ajax-loader.gif' alt='' />");
        $("#RegistrarConfig").load(ZB_Relative_Path+"ZBAdmin/Product/RegistrarForm/" + registrarID + "/" + regarding);
    } else {
        $("#ExecutionArea").hide();
        $("#RegistrarConfig").hide();
    }

}

//SaaS **********************

function HasDomainChange() {
    if ($('#HasDomain').is(':checked') == true) {
        $("#dominio").show();
    } else {
        $("#dominio").hide();
    }
    IsSubDomainChange();
}

function IsSubDomainChange() {
    if ($('#IsSubDomain').is(':checked') == true) {
        $("#subdomain").show();
        $("#subsiteinitial").hide();
        $("#subsite").hide();
    } else {
        $("#subdomain").hide();
        $("#subsiteinitial").show();
        $("#subsite").show();
    }
}
