/**
 *
 * First created between November and December 2003
 *
 *
 *
 * 1994-2003 Digitalis Informatica. All rights reserved.
 *
 *
 *
 * Distribuicao e Gestao de Informatica, Lda.
 *
 * Estrada de Paco de Arcos num.9 - Piso -1
 *
 * 2780-666 Paco de Arcos
 *
 * Telefone: (351) 21 4408990
 *
 * Fax: (351) 21 4408999
 *
 * http://www.digitalis.pt
 *
 *
 *
 *
 *
 * @author Daniel Alexandre Campelo <dcampelo@digitalis.pt>
 *
 * Created : 27/May/2005
 *
 *
 *
 *
 *
 * Class that generats a form dinamicly and submits the request.
 *
 *
 *
 * NOTE : This JS file is automaticly added to the page by the dif:document tag.
 *
 *
 *
 *
 *
 * Contructors
 *
 * DIFForm(<baseURL>) -> Constructor(1), receives the base URL.
 *
 * The base URL may contain the query string of the request, in that case the
 * query must be processed and taken in account.
 *
 *
 *
 * Methods
 *
 * addParameter(<key>, <value>) -> adds a new parameter to be send on the
 * request
 *
 * addQuery(<query>) -> adds a set of parameters to be sent in the request.
 *
 * The format of the 'String' must be '?<name>=<value>&<name>=<value>&'
 *
 * submit() -> Submits the form. Responsable for generating theform dinamicly.
 *
 * setMethod(<methodName>) -> Sets the method to be used for the form.
 *
 * <String> stripURLOfQuery(<String>) -> Removes the query string from a URL.
 *
 */

var VALUE_INDEX = 1;

var KEY_INDEX = 0;

/**
 *
 * Contructor.
 *
 * Checks if the baseURL defines a query. If so, adds the parameters to the form
 * request and strips it out from the URL
 *
 */

function DIFForm(baseURL) {

    // Attributes

    this.baseURL = baseURL;

    this.parameters = new Array();

    this.method = 'POST';

    // Methods

    this.addParameter = addDIFFormParameter;

    this.addQuery = addDIFFormQuery;

    this.setMethod = setDIFFormMethod;

    this.submit = submitDIFForm;

    this.debug = debugDIFForm;

    this.removeQueryFromURL = stripURLOfQuery;

    // Checks if the baseURL has a query defined

    this.addQuery(this.baseURL);

    this.baseURL = this.removeQueryFromURL(this.baseURL);

}

/**
 *
 * Adds a parameter to the parameter list
 *
 */

function addDIFFormParameter(key, value) {

    this.parameters[this.parameters.length] = [ key, value ];

}

/**
 *
 * Adds the parameters from a query string into the parameter list.
 *
 */

function addDIFFormQuery(query) {

    var QUERY_SPLITER = "&|\\?";

    var NAME_VALUE_SPLIT = "=";

    var regExpQuery = new RegExp(QUERY_SPLITER);

    var u = null;

    // Splits the query string

    var paramMatchs = query.split(regExpQuery);

    // Checks if there was any match

    if (paramMatchs != null) {

        var regExpAttrib = new RegExp(NAME_VALUE_SPLIT, "g");

        // Runs over all matchs (over the query)

        for ( var i = 0; i < paramMatchs.length; i++) {

            try {

                // Gets the key and the value

                var attribMatchs = paramMatchs[i].split(regExpAttrib);

                // Adds the parameter to the list

                var value = attribMatchs[1];

                if (value == u || value == 'undefined') {

                    value = '';

                }

                this.addParameter(attribMatchs[0], value);

            } catch (err) {

                // Traps any error ocurred due to the regular expression

            }

        }

    }

    // Get each of the parameters and add it to the list

}

/**
 *
 * Set the submit method.
 *
 * Only accepts "POST" and "GET"
 *
 */

function setDIFFormMethod(method) {

    var methAux = method.toLowerCase();

    if (methAux == 'post' || methAux == 'get') {

        this.method = method;

    }

}

/**
 *
 * Generats the form and submits it
 *
 */

function submitDIFForm() {
    // Write the form element
    var myform = document.createElement("form");
    myform.name = 'difformform';
    myform.id = 'difformform';
    myform.method = this.method;
    myform.action = this.baseURL;

    var i = 0;

    // write all parameters
    for (; i < this.parameters.length; i++) {
        var myInput = document.createElement("input");
        myInput.type = "hidden";
        myInput.name = this.parameters[i][KEY_INDEX];
        myInput.value = this.parameters[i][VALUE_INDEX];
        myform.appendChild(myInput);
    }

    document.body.appendChild(myform);
    // getting and submiting the form
    myform.submit();
}

function debugDIFForm() {

    // Write the form element

    var str = "<form name='difformform' id='difformform' method='" + this.method + "' action='" + this.baseURL + "'>";

    var i = 0;

    // write all parameters

    for (; i < this.parameters.length; i++) {

        str = str + "<input type='hidden' name='" + this.parameters[i][KEY_INDEX] + "' value='"
                + this.parameters[i][VALUE_INDEX] + "'/>";

    }

    // write end form

    str = str + "</form>";

    alert(str);

}

function stripURLOfQuery(url) {

    var regExp = new RegExp(".[^\\?]*");

    // Splits the query string

    return regExp.exec(url)[0];

}
