﻿function updatePageView(countryId, targetControlId, ctrl) {
  manageFields(ctrl);
  getStates(countryId, targetControlId);
}

function getStates(countryId, targetControlId) {
  $("#" + targetControlId).attr("disabled", "");
  $("#" + targetControlId).html("");
  $("#" + targetControlId).append($('<option value="">Loading...</option>'));
  $.post(
            "/AjaxCommon/GetStates",
            { countryId: countryId },
            function (data) {
              $("#" + targetControlId).html("");
              var option = $("<option />");
              option.val("");
              option.html("Please Select State");
              option.selected = "selected";
              $("#" + targetControlId).append(option);
              for (var ind in data) {
                var option = $("<option />");
                option.val(data[ind].id);
                option.html(data[ind].name);
                $("#" + targetControlId).append(option);
              }
            },
            "json"
        );
}

function getSelectedStates(countryId, targetControlId, selectedId) {
  $("#" + targetControlId).attr("disabled", "");
  $("#" + targetControlId).html("");
  $("#" + targetControlId).append($('<option value="">Loading...</option>'));
  $.post(
            "/AjaxCommon/GetStates",
            { countryId: countryId },
            function (data) {
              $("#" + targetControlId).html("");
              var option = $("<option />");
              option.val("");
              option.html("Please Select State");
              $("#" + targetControlId).append(option);
              for (var ind in data) {
                var option = $("<option />");
                option.val(data[ind].id);
                option.html(data[ind].name);
                $("#" + targetControlId).append(option);
              }

              $("#" + targetControlId).val(selectedId);
            },
            "json"
        );
}

function getRegions(stateId, targetControlId) {
    $("#" + targetControlId).attr("disabled", "");
    $("#" + targetControlId).html("");
    $("#" + targetControlId).append($('<option value="">Loading...</option>'));
    $.post(
            "/AjaxCommon/GetRegions",
            { stateId: stateId },
            function(data) {
                $("#" + targetControlId).html("");
                var option = $("<option />");
                option.val("");
                option.html("Please Select Region");
                option.selected = "selected";
                $("#" + targetControlId).append(option);
                for (var ind in data) {
                    var option = $("<option />");
                    option.val(data[ind].id);
                    option.html(data[ind].name);
                    $("#" + targetControlId).append(option);
                }
            },
            "json"
        );
}

function getAreas(regionId, targetControlId) {
    $("#" + targetControlId).attr("disabled", "");
    $("#" + targetControlId).html("");
    $("#" + targetControlId).append($('<option value="">Loading...</option>'));
    $.post(
            "/AjaxCommon/GetAreas",
            { regionId: regionId },
            function(data) {
                $("#" + targetControlId).html("");
                var option = $("<option />");
                option.val("");
                option.html("Please Select Area");
                option.selected = "selected";
                $("#" + targetControlId).append(option);
                for (var ind in data) {
                    var option = $("<option />");
                    option.val(data[ind].id);
                    option.html(data[ind].name);
                    $("#" + targetControlId).append(option);
                }
            },
            "json"
        );
}

function getBusinessIndustries(categoryId, targetControlId) {
    $("#" + targetControlId).attr("disabled", "");
    $("#" + targetControlId).html("");
    $("#" + targetControlId).append($('<option value="">Loading...</option>'));
    $.post(
        "/AjaxCommon/GetIndustries",
        { id: categoryId },
        function(data) {
            $("#" + targetControlId).html("");
            var option = $("<option />");
            option.val("");
            option.html("Please Select Industry");
            option.selected = "selected";
            $("#" + targetControlId).append(option);
            for (var ind in data) {
                var option = $("<option />");
                option.val(data[ind].id);
                option.html(data[ind].name);
                $("#" + targetControlId).append(option);
            }
        },
        "json"
    );
}

function getTotalPrice(priceId1, priceId2, priceId3, priceId4, priceId5, priceId6, priceId7, priceId8, priceId9, totalCostElementId) {

  var priceIds = {
    priceId1: priceId1,
    priceId2: priceId2,
    priceId3: priceId3,
    priceId4: priceId4,
    priceId5: priceId5,
    priceId6: priceId6,
    priceId7: priceId7,
    priceId8: priceId8,
    priceId9: priceId9
  };

  $.post(
    "/AjaxCommon/GetPrice",
    { priceIds: priceIds },
    function (data) {
      totalCostElementId.html("<b>$" + data + " / month</b>");
    },
    "json"
  );
}

function getCategories(customerId, targetControlId) {
  $("#" + targetControlId).attr("disabled", "");
  $("#" + targetControlId).html("");
  $("#" + targetControlId).append($('<option value="">Loading...</option>'));
  $.post(
            "/AjaxCommon/GetCategories",
            { customerId: customerId },
            function (data) {
              $("#" + targetControlId).html("");
              var option = $("<option />");
              option.val("");
              option.html("Please Select Category");
              option.selected = "selected";
              $("#" + targetControlId).append(option);
              for (var ind in data) {
                var option = $("<option />");
                option.val(data[ind].id);
                option.html(data[ind].name);
                $("#" + targetControlId).append(option);
              }
            },
            "json"
        );
}

function getSelectedCategories(customerId, targetControlId, selectedId) {
  $("#" + targetControlId).attr("disabled", "");
  $("#" + targetControlId).html("");
  $("#" + targetControlId).append($('<option value="">Loading...</option>'));
  $.post(
            "/AjaxCommon/GetCategories",
            { customerId: customerId },
            function (data) {
              $("#" + targetControlId).html("");
              var option = $("<option />");
              option.val("");
              option.html("Please Select Category");
              option.selected = "selected";
              $("#" + targetControlId).append(option);
              for (var ind in data) {
                var option = $("<option />");
                option.val(data[ind].id);
                option.html(data[ind].name);
                $("#" + targetControlId).append(option);
              }

              $("#" + targetControlId).val(selectedId);
            },
            "json"
        );
}

