﻿function aweberSubmit(formId, opts) {
    if (formId != '') {
        var dist = $('#frmAweber');
        var src = $('#' + formId);
        updateValues(src);
        var myForm = dist.html(src.html());
        
        if (opts != null && opts != undefined) {
            if (opts['ajax'] == true) {
                myForm.ajaxSubmit();
                return false
            }
        }

        myForm.submit();
    }
}

function updateValues(form) {
    $(form).find('input').each(function() {
        this.setAttribute('value', this.value);
        if (this.checked)
            this.setAttribute('checked', 'checked');
        else
            this.removeAttribute('checked');
    });

    $(form).find('select').each(function() {
        var index = this.selectedIndex;
        var i = 0;
        $(this).children('option').each(function() {
            if (i++ != index)
                this.removeAttribute('selected');
            else
                this.setAttribute('selected', 'selected');
        });
    });

    $(form).find('textarea').each(function() {
        $(this).html($(this).val());
    });
}
