﻿//Save Form
$("fieldset.form .submit").live("click", function () {

    var parent;

    if ($(this).parents("fieldset.section").length > 0) {
        parent = $(this).parents("fieldset.section");
    }
    else {
        parent = $(this).parents("fieldset.form");
    }

    var order = 1;
    var formId = $(this).parents("fieldset.form").attr("class").replace("form type", "");

    var senderGroupId = "0";

    if ($(parent).attr("id") != undefined && $(parent).attr("id") != "") {
        senderGroupId = $(parent).attr("id").replace("group", "");
    }

    var request = "{\"Id\": " + formId + ",";

    var items = "[";

    $(parent).find("div").each(function () {

        var id = $(this).attr("id").toString();

        var regex = new RegExp("form([0-9]+)_item([0-9]+)_templateitem([0-9]+)_templatetype([0-9]+)")
        var m = regex.exec(id);

        if (m != null) {

            var itemId = m[2]
            var templateId = m[3]
            var templateType = m[4]

            var associatedId = "";

            var item = "{";

            //Set ID
            item += "\"Id\": " + itemId + ",";

            switch (templateId) {

                case "2":
                case "3":
                case "4":
                case "5":
                case "9":
                    item += "\"Required\": false,";
                    item += "\"Texts\": {\"Text\": \"" + $(this).find("span").html().replace(/"/gi, "\\\"") + "\"},";

                    if (templateId == "9") {
                        if ($(this).find("label").attr("for") != "") {

                            var associatedId = $('#' + $(this).find("label").attr("for")).parent().attr("id");

                            var mAssociated = regex.exec(associatedId);

                            if (mAssociated != null) {
                                item += "\"AssociatedItem\": { \"Id\":" + mAssociated[2] + ", \"Template\": " + mAssociated[3] + "},";
                            }

                        }
                    }

                    break;

                case "10":
                case "11":
                case "20":
                case "12":
                    item += "\"Required\": " + ($(this).find("input.red").length > 0) + ",";
                    if ($(this).find("input.red").length > 0) {
                        item += "\"Texts\": {\"Validation\": \"" + $(this).find("input.red").attr("value").toString().replace(/"/gi, "\\\"") + "\", \"ClientID\": \"" + $(this).find(".textbox").attr("id") + "\"},"
                    }
                    item += "\"Value\": \"" + $(this).find(".textbox").val() + "\",";
                    if ($(this).find(".textbox").attr("foremail") != undefined && $(this).find(".textbox").attr("foremail") != false) {
                        item += "\"UseForEmail\": true,";
                    }
                    break;

                case "13":
                case "14":
                case "15":
                case "16":
                case "21":

                    item += "\"Required\": " + ($(this).find("input.red").length > 0) + ",";
                    if ($(this).find("input.red").length > 0) {
                        item += "\"Texts\": {\"ClientID\": \"" + $(this).find("input").not(".mm").attr("id") + "\", \"Validation\": \"" + $(this).find("input.red").attr("value").replace(/"/gi, "\\\"") + "\""
                            + (templateId == "14" ? ", \"Text\": \"" + $(this).find("input").attr("name").replace(/"/gi, "\\\"") + "\",\"DefaultValue\": \"" + $(this).find("input").attr("marker").replace(/"/gi, "\\\"") + "\"" : "") + "},";
                    }
                    else {
                        item += "\"Texts\": {\"ClientID\": \"" + $(this).find("input").not(".mm").attr("id") + "\""
                        + (templateId == "14" ? ", \"Text\": \"" + $(this).find("input").attr("name").replace(/"/gi, "\\\"") + "\",\"DefaultValue\": \"" + $(this).find("input").attr("marker").replace(/"/gi, "\\\"") + "\"" : "") + "},";
                    }


                    if (templateId == "14" || templateId == "13") {
                        item += "\"Value\": \"" + $(this).find("input").attr("checked") + "\",";
                        if ($(this).find("input").attr("value") != undefined && $(this).find("input").attr("value") != "") {
                            item += "\"AssociatedItem\": { \"Value\": \"" + $('input[id="' + $(this).find('input, select, textarea').eq(0).attr("value") + '"]').val() +
                                    "\", \"Texts\": {\"ClientId\": \"" + $('input[id="' + $(this).find('input, select, textarea').eq(0).attr("value") + '"]').attr("id");
                            if ($('input[id="' + $(this).find('input, select, textarea').eq(0).attr("value") + '"]').next("input.red").attr("value") != undefined) {
                                item += "\", \"Validation\": \"" + $('input[id="' + $(this).find('input, select, textarea').eq(0).attr("value") + '"]').next("input.red").attr("value").replace(/"/gi, "\\\"");
                            }
                            item += "\"}, \"Required\": " + ($('input[id="' + $(this).find('input, select, textarea').eq(0).attr("value") + '"]').next("input.red").length > 0) + "},";
                        }
                    }

                    if (templateId == "15") {
                        item += "\"Value\": \"" + $(this).find(".calendar").val() + "\",";
                    }

                    if (templateId == "16") {
                        if ($(this).find(".hh").val() != "" && $(this).find(".mm").val() != "") {
                            item += "\"Value\": \"" + $(this).find(".hh").val() + ":" + $(this).find(".mm").val() + "\",";
                        }
                        else {
                            item += "\"Value\": \"\",";
                        }
                    }

                    if (templateId == "21") {
                        if ($(this).find("iframe").contents().find('input[type="hidden"]').length > 0) {
                            item += "\"Value\": \"" + $(this).find("iframe").contents().find('input[type="hidden"]').val() + "\",";
                        }
                        else {
                            item += "\"Value\": \"\",";
                        }

                    }

                    break;

                case "17":
                    item += "\"Required\": " + ($(this).find("input.red").length > 0) + ",";
                    item += "\"MaxLength\": " + $(this).find(".textbox").attr("maxlength") + ",";
                    if ($(this).find("input.red").length > 0) {
                        item += "\"Texts\": {\"ClientID\": \"" + $(this).find("input").not(".mm").attr("id") + "\", \"Validation\": \"" + $(this).find("input.red").attr("value") + "\"},";
                    }
                    else {
                        item += "\"Texts\": {\"ClientID\": \"" + $(this).find("input").not(".mm").attr("id") + "\", \"Validation\": \"\"},";
                    }


                    item += "\"Value\": \"" + $(this).find(".entercode").val() + "\",";

                    break;

                case "18":
                    item += "\"Required\": false,";
                    item += "\"Texts\": {\"Text\": \"" + $(this).find("input").val().replace(/"/gi, "\\\"") + "\"},";
                    break;

                default:
            }

            if ($('label[for="' + $(this).find('input, select, textarea').eq(0).attr("id") + '"]').length > 0) {
                item += "\"ItemLabel\": { \"Texts\": {\"Text\": \"" + $('label[for="' + $(this).find('input, select, textarea').eq(0).attr("id") + '"]').find("span").html().replace(/"/gi, "\\\"")
                                        + "\",\"DefaultValue\": \"" + $('label[for="' + $(this).find('input, select, textarea').eq(0).attr("id") + '"]').find("span").attr("marker").replace(/"/gi, "\\\"") + "\"}},";
            }

            //Set Type
            item += "\"Template\": " + templateId + ",";
            if ($(this).parents("fieldset").attr("id") == undefined || $(this).parents("fieldset").attr("id") == "") {
                item += "\"Group\": {\"Id\": " + "0" + "},";
            }
            else {
                item += "\"Group\": {\"Id\": " + $(this).parents("fieldset").attr("id").replace("group", "") + "},";
            }
            
            item += "\"DisplayOrder\": " + order;

            item += "},";

            items += item;
        }

        order++;

    })

    items = items.substr(0, items.length - 1) + "]";

    request += "\"Items\":" + items;
    request += "}";

    $.ajax({
        type: "POST",
        url: "/services.asmx/SubmitForm",
        data: "{\"form\": " + request + ", \"senderGroupId\": " + senderGroupId + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var json = $.parseJSON(msg.d);
            if (json.Successful) {
                if (json.RedirectOnSubmit) {
                    window.location = json.RedirectToUrl
                }
                else {
                    alert(json.Message);
                }
            }
            else {
                alert(json.Message);
            }

        },
        error: function (msg) {
            alert("error");
        }
    });


    return false;

})
    
