/****
 *
 * ROWYN 5 AJAX Library
 *
 *
 */

var isset = function(v)
    {
        if(typeof(v)=='undefined')
            return false;
        else
            return true;
    };


var is_function = function(v)
    {
        if(typeof(v)=='function')
            return false;
        else
            return true;
    };


var $ = function(id)
    {
        return document.getElementById(id);
    };
    
    
var $n = function(nam)
	{
		return document.getElementsByName(nam);
	};

var ROWYN5_Form = {
    
    toArray: function (frm)
        {
            elements = new Array();
            fields = frm.elements;
            //alert(fields);

            for(var i=0; i < fields.length; i++)
            {                
                if (fields[i].type == 'checkbox' && fields[i].checked)
                {
                    elements.push(fields[i].name);
                    elements.push(fields[i].value);
                }
                else if (fields[i].type == 'checkbox' && (!fields[i].checked))
                {
                    elements.push(fields[i].name);
                    elements.push("0");
                }
                else if (fields[i].type == 'radio' && fields[i].checked)
                {
                    elements.push(fields[i].name);
                    elements.push(fields[i].value);
                }
                else if (fields[i].type == 'radio')
                {
                    // Nothing
                }
                else if (fields[i].type == 'submit')
                {
                    // Nothing
                }
                else if (fields[i].type == 'button')
                {
                    // Nothing
                }
                else
                {
                    elements.push(fields[i].name);
                    elements.push(fields[i].value);
                }

            }

            return elements;
        },
        
        toURL: function(frm)
            {
                url = new String();
                a = ROWYN5_Form.toArray(frm);
                
                for(i=0; i < (a.length / 2); i++)
                    url += a[2*i] + "=" + urlencode(a[(2*i)+1]) + "&";
                
                return url.substring(0, url.length - 1);
            },


        tokenDecoder: function(token)
            {
                
                var url = "";
                var dic = new String(this.getDico());

                for(i=0; i<(token.length/2); i++)
                {
                    ch = this.charValue( token.charAt(2*i) );
                    cl = this.charValue( token.charAt((2*i) + 1) );

                    url += String.fromCharCode((dic.length * ch)+cl);
                }

                return url;
            },
            
        charValue: function(c)
            {
                index = -1;
                dic = this.getDico();

                for(i=0; i<dic.length; i++)
                {
                    if(c == dic.charAt(i))
                    {
                        index = i;
                        break;
                    }

                }

                return index;
            },
            
        getDico: function()
        {
            return new String("Z123456789YBCDEFGHIJKLMNOPQRSTUV@#AW.-");
        }
}


ROWYN5_AJAX = {
    xhr: null,
    url: "",
    method: "GET",
    asynchronous: false,


    /*****************************************************************
     * ROWYN5.Request(url, parameters)
     *****************************************************************
     *
     * Parameters :
     *  method:
     *  onSuccess:
     *  Data:
     *  innerHtml:
     *
     * Example :
     * 
     * ROWYN5.Request("desk/save?",  {
     *                                  method: "POST",
     *                                  Data: ROWYN5_Form.toURL(myform),
     *                                  onSucces: function(xhr) { eval(myform).style.background = 'green'; },
     *                               }
     *               );
     *
     *****************************************************************/

    Request: function(l, p)
                    {
                        
                        // Récupération de la méthode
                        if(isset(p.method))
                        {
                            if(p.method == "POST")
                                this.method = "POST";
                            else
                                this.method = "GET";
                            
                        }

                        // Calcul des données et de l'URL en fonction de la methode
                        if(isset(p.Data))
                        {
                            if(this.method == "GET")
                            {
                                this.url = l + "?" + p.Data;
                                this.data = null;
                            }
                            else
                            {
                                this.url = l;
                                this.data = p.Data;
                            }
                        }

                        
                        //Création de l'objet XMLHTTPREquest
                        if(window.XMLHttpRequest)
                        {
                            xhr = new XMLHttpRequest();
                            
                            if (xhr.overrideMimeType)
                            {
                                xhr.overrideMimeType('text/xml');
                            }
                        }
                        else if(window.ActiveXObject)
                        {
                            xhr = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        else
                        {
                            window.status = "Votre navigateur ne supporte pas les objets XMLHTTPRequest...";
                            return;
                        }

                        
                        // Execution de la requete
                        try
                        {
                            xhr.open(this.method.toString().toLowerCase(), this.url, false);
                            xhr.send(this.data);
                        }
                        catch(e)
                        {
                            window.status = (e);
                        }
                        
                        // Execution du code utilisateur si succès
                        if((xhr.readyState == 4) /*&& (xhr.status == 200)*/)
                        {
                            // Execution de la methode onSuccess si définie
                            if(isset(p.onSuccess)/* && (is_function(p.onSuccess))*/)
                                p.onSuccess(xhr);

                            // Placement du retour (responseText) dans la zone d'id fournie
                            if(isset(p.innerHtml)/* && isset($(p.innerHtml).innerHTML)*/)
                                $(p.innerHtml).innerHTML = xhr.responseText;
                        }


                    }
}

function urlencode(str) {
    return escape(str.replace(/%/g, '%25').replace(/\+/g, '%2B')).replace(/%25/g, '%');
}
