﻿///<reference path="~/scripts/jquery-1.3.2-vsdoc.js" />
///<reference path="~/scripts/jquery-ui-1.7.2.custom.js" />
///<reference path="~/scripts/jquery.validate.min.js" />

var ActiveCRMData = null;

function ActiveCRMSubmit(isNew) {

    var data = GetActiveCRMData();
    
    if (!$("form").valid()) {
        return;
    }

    var json = JSON2.stringify({ values: data });
    var url = '/PortalModules/ActiveCRMModule/Services/ActiveCRMService.svc/';
    if (isNew) {
        url = url + "CreateSubscriber";
    }
    else {
        url = url + "UpdateSubscriber";
    }
    
    $.ajax({
        url: url,
        data: json,
        type: "POST",
        processData: true,
        contentType: "application/json",
        timeout: 10000,
        dataType: "json",
        success: onSucceeded,
        error: handleError
    });
}

function onSucceeded(result) {
    var obj = JSON2.parse(result.d);
    $("#ActiveCRM_SuccessDialog").dialog("open");
}

function handleError(e) {
    $("#ActiveCRM_FailedDialog").dialog("open");
}

function GetCheckBoxListValue(id) {
    var total = 0;
    $("" + id + " input:checked").each(function() {
        total += parseInt($(this).val());
    });
    return total;
}

$(function() {
    $("#ActiveCRM_SuccessDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 200,
        modal: true,
        buttons: { "OK": function() {
            $(this).dialog("close");
            window.navigate("/");
        }
        }
    });

    $("#ActiveCRM_FailedDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 200,
        modal: true,
        buttons: { "OK": function() {
            $(this).dialog("close");
        }
        }
    });

    $("form").validate();
});



