var confirmMessages = [];
function saveFormData(userId, formId, textBoxes, listBoxes, choiceGroups, _confirmMessage) {    
    if (_confirmMessage !== null) {
        confirmMessages["'" + String(formId) + "'"] = _confirmMessage;
    }    
    
    var params = [];
    for (i = 0; i < textBoxes.length; i++) {
        if ($("#" + textBoxes[i]).val()) {
            params.push(textBoxes[i] + "=" + $("#" + textBoxes[i]).val());
            $("#" + textBoxes[i]).val("");
        }
    }
    
    var tempArray = [];
    for (i = 0; i < choiceGroups.length; i++) {
        $(":checkbox, :radio").each(function() {
            if (this.name === choiceGroups[i] && this.checked) {                        
                tempArray.push(this.value);
                this.checked = false;
            }
        });
        
        if (tempArray.length > 0) {
            params.push(choiceGroups[i] + "=" + tempArray.join(", "));
            tempArray = [];
        }
    }
    
    for (i = 0; i < listBoxes.length; i++) {
        if ($("#" + listBoxes[i]).val()) {
            params.push(listBoxes[i] + "=" + $("#" + listBoxes[i]).val());
            $("#" + listBoxes[i]).val("");
        }
    }
    Teknomedia.EdgeII.Publications.Formbuilder.FormGen.SaveResponse(userId, formId, params, saveFormDataComplete);
}
function validateFormData(message, textBoxes, listBoxes, choiceGroups) {
    for (i = 0; i < textBoxes.length; i++) {                
        if (!$("#" + textBoxes[i]).val()) {
            if (message) { 
                alert(message);
            }
            return false;
        }
    }
    
    for (i = 0; i < choiceGroups.length; i++) {
        var anyChecked = false;
        $(":checkbox, :radio").each(function() {
            if (this.name === choiceGroups[i]) {
                anyChecked = anyChecked || this.checked;
            }
        });
        if (!anyChecked) {
            if (message) { 
                alert(message);
            }
            return false;
        }
    }
    
    for (i = 0; i < listBoxes.length; i++) {
        if (!$("#" + listBoxes[i]).val()) {
            if (message) { 
                alert(message);
            }
            return false;
        }
    }

    return true;
}
function saveFormDataComplete(formId) {    
    if (confirmMessages["'" + String(formId) + "'"]) {
        alert(confirmMessages["'" + String(formId) + "'"]);
    }
}

var formbuilder_articleFormTargets = [];
function formbuilder_getFormHtml(userId, templateId, targetElementId) {    
    if (userId > 0 && templateId > 0 && Teknomedia && Teknomedia.EdgeII.Publications.Formbuilder.FormGen) {
        formbuilder_articleFormTargets[templateId] = targetElementId;
        Teknomedia.EdgeII.Publications.Formbuilder.FormGen.GetHTML(userId, templateId, formbuilder_getFormHtmlComplete);
    }
}
function formbuilder_getFormHtmlComplete(inputArray) {
    if (inputArray.length > 0) {
        var templateId = inputArray[0];
        var html = inputArray[1];
        $("#" + formbuilder_articleFormTargets[templateId]).html(html);
    }
}