









var JPF_FORM_SUBMIT_BEST      = 0x00; /* AJAX if possible */
var JPF_FORM_SUBMIT_NORMAL    = 0x01; /* standard way, using submit() method */
var JPF_FORM_SUBMIT_AJAX      = 0x02; /* always AJAX, failes otherwise */



function JpfFormSubmit(formElmOrId) {
    this.formElm = (typeof(formElmOrId) == "string")
            ? document.getElementById(formElmOrId) : formElmOrId;

    this.submitMode = JPF_FORM_SUBMIT_BEST;

    this.submitFunction = this.formElm.submit;
    this.callOnSubmit = true;
    this.formAction = this.formElm.action;
    this.formEncoding = this.getFormBestEncoding();
    this.formTarget = this.formElm.target;
}



JpfFormSubmit.prototype.setSubmitFunction = function(value) {
    this.submitFunction = value;
}



JpfFormSubmit.prototype.setCallOnSubmit = function(value) {
    this.callOnSubmit = value;
}



JpfFormSubmit.prototype.setAction = function(value) {
    this.formAction = value;
}



JpfFormSubmit.prototype.setTarget = function(value) {
    this.formTarget = value;
}



JpfFormSubmit.prototype.setSubmitMode = function(value) {
    this.submitMode = value;
}



JpfFormSubmit.prototype.submit = function() {
    var normalSubmit = (this.submitMode == JPF_FORM_SUBMIT_NORMAL);
    var canUseAjax = this.formElementsCanBeEncoded();

    if (this.submitMode == JPF_FORM_SUBMIT_AJAX) {
        if (!canUseAjax) {
            throw new "form cannot be submitted using AJAX technology";
        }
    }

    if (this.submitMode == JPF_FORM_SUBMIT_BEST) {
        normalSubmit = !canUseAjax;
    }

    if (this.callOnSubmit) {
        if (typeof(this.formElm.onsubmit) == "function") {
            if (!this.formElm.onsubmit()) {
                return true;
            }
        }
    }

    return normalSubmit ? this.submitFormUsingNormalWay()
            : this.submitFormUsingAjax();
}



JpfFormSubmit.prototype.submitFormUsingNormalWay = function() {
    this.formElm.action = this.formAction;
    this.formElm.encoding = this.formEncoding;
    this.formElm.target = this.formTarget;

    

    this.formElm.jpfFormSubmit = this.submitFunction;
    this.formElm.jpfFormSubmit();

    return true;
}



JpfFormSubmit.prototype.submitFormUsingAjax = function() {
    var postData = this.encodeFormData();

    if (postData == null) {
        throw new "form data cannot be encoded";
    }

    var httpRequest = new JpfHttpRequest(this.formAction, postData);
    var response = httpRequest.send();

    if (response == null) {
        return false;
    }

    if (this.formTarget.length > 0) {
        var wnd = window.frames[this.formTarget];
    } else {
        var wnd = window;
    }

    wnd.document.open();
    wnd.document.write(response);
    wnd.document.close();

    return true;
}



JpfFormSubmit.prototype.formElementsCanBeEncoded = function() {
    
    return !this.formContainsFiles();
}



JpfFormSubmit.prototype.formContainsFiles = function() {
    for (var i = 0; i < this.formElm.elements.length; i++) {
        if (this.formElm.elements[i].type == "file") {
            if (JpfUtils.trim(this.formElm.elements[i].value).length > 0) {
                return true;
            }
        }
    }

    return false;
}



JpfFormSubmit.prototype.encodeFormData = function() {
    var result = new String();

    for (var i = 0; i < this.formElm.elements.length; i++) {
        var name = this.formElm.elements[i].name;

        var isDisabled = typeof(this.formElm.elements[i].disabled) == "boolean"
                ? this.formElm.elements[i].disabled : true;

        if ((name.length == 0) || isDisabled) {
            continue;
        }

        var values = this.getFormElementValues(this.formElm.elements[i]);

        if (values == null) {
            return null;
        }

        while (values.length > 0) {
            result = result + ((result.length > 0) ? "&" : "")
                    + encodeURIComponent(name) + "="
                    + encodeURIComponent(values.pop());
        }
    }

    return result;
}



JpfFormSubmit.prototype.getFormElementValues = function(element) {
    
    if ((element.type == "hidden") || (element.type == "text")
            || (element.type == "password") || (element.type == "textarea")) {
        return [element.value];
    }

    
    if ((element.type == "checkbox") || (element.type == "radio")) {
        return !element.checked ? new Array() : [element.value];
    }

    
    if ((element.type == "select-one") || (element.type == "select-multiple")) {
        return this.getSelectFormElementValues(element);
    }

    
    if ((element.type == "submit") || (element.type == "button")) {
        return "";
    }

    
    if (element.type == "file") {
        if (JpfUtils.trim(element.value).length == 0) {
            return "";
        }
    }

    return null;
}



JpfFormSubmit.prototype.getSelectFormElementValues = function(element) {
    var result = new Array();

    if (element.type == "select-multiple") {
        for (var i = 0; i < element.options.length; i++) {
            if (element.options[i].selected) {
                result.push(element.options[i].value);
            }
        }
    } else {
        if (element.selectedIndex >= 0) {
            result.push(element.options[element.selectedIndex].value);
        }
    }

    return result;
}



JpfFormSubmit.prototype.getFormBestEncoding = function() {
    return this.formContainsFiles(this.formElm) ? "multipart/form-data"
            : "application/x-www-form-urlencoded";
}
