function myAjax (combo_origem, combo_destino, fuseaction, callback2, callback, objectname, sessionname) {
    this.combo_origem = combo_origem;
    this.combo_destino = combo_destino;
    this.fuseaction = fuseaction;
    this.firstload = false;
    
    if (objectname == undefined) objectname = "objAjax";
    if (callback2 == undefined) callback2 = "";
    if (callback == undefined) callback = objectname+".process_request";
                           
    this.objectname = objectname;
    this.callback = callback;
    this.callback2 = callback2;
    this.method = "GET";
    this.sessionname = sessionname;
}

myAjax.prototype.method = function (method) {
    if (method == "GET" || method == "POST") {
        this.method = method;
    }
}

myAjax.prototype.start_request = function (firstLoad) {
    this.firstload = firstLoad;
                                 
    switch (this.method) {
        case "GET":
            if (firstLoad) {
                this.combo_origem.value = get_attribute(this.combo_origem, "inivalue");
                //alert(get_attribute(this.combo_origem, "inivalue"));
            }                          
                           
            for (i=0; i<this.combo_destino.length; i++) {
                this.combo_destino[i].disabled = true;
            }   

            if (this.combo_origem.value != "") {
                http(this.method, "index.php?fuseaction="+this.fuseaction+"&"+this.combo_origem.name+"="+this.combo_origem.value+"&sn="+this.sessionname, this.callback);
                //window.open("index.php?fuseaction="+this.fuseaction+"&"+this.combo_origem.name+"="+this.combo_origem.value+"&sn="+this.sessionname);
            } else {
                eval(this.callback+"();");
            }
            
            break;
            
        case "POST":
            http("POST", "index.php?fuseaction="+this.fuseaction, this.callback, this.combo_origem);        
            //window.open("index.php?fuseaction="+this.fuseaction, this.callback, this.combo_origem);        
            break;
    }
}

myAjax.prototype.process_request = function (result) {                       
    var k;
    var i;
                                    
    for (k=0; k<this.combo_destino.length; k++) {        
                                     
        switch (this.combo_destino[k].tagName.toLowerCase()) {
            case "select": 
                if (this.combo_destino[k].options.length>1) {
                    cant = this.combo_destino[k].options.length;
                    for (i=1; i<cant; i++) {
                        anOption = this.combo_destino[k].options[1];
                        anOption.parentNode.removeChild(anOption);
                    }
                }
                
                if (result) {
                    for (i=0; i<result.length; i++) {
                        add_option(this.combo_destino[k], result[i][0], result[i][1]);
                    }
                }

                if (this.firstload) {
                    this.combo_destino[k].value = get_attribute(this.combo_destino[k], "inivalue");
                    /*if (this.combo_destino[k].value != "") {
                        if (this.callback != undefined) {
                            eval(this.callback+"();");
                        }
                    }*/
                }
                break;
            
            case "input":
                this.combo_destino[k].value = "";
                if (result) {
                    this.combo_destino[k].value = result[k];
                }
                break;
        }
        
        this.combo_destino[k].disabled = false;
    }

    if (this.callback2 != "") {
        eval(this.callback2+"();");
    }
}

function isArray(testObject) {   
    return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number';
}

function add_option (combo, value, text) {
    var oOption = document.createElement("OPTION");
    combo.options.add(oOption);
    oOption.innerHTML = text;
    oOption.value = value;    
}

function set_attribute(obj, attribname, value) {
    for (i=0; i<obj.attributes.length; i++) {
        attrib = obj.attributes[i];
        
        if (attrib.name == attribname) {
            attrib.name = attribname;
            return;
        }            
    }
    
    var attrib = document.createAttribute(attribname);
    attrib.nodeValue = value
    obj.setAttributeNode( attrib);
}

function get_attribute(obj, attribname) {
    for (i=0; i<obj.attributes.length; i++) {
        attrib = obj.attributes[i];
        
        if (attrib.name == attribname) {
            return attrib.value;
        }
            
    }
}

function getCurrentDate() {
    var currentdate = new Date();
    return currentdate.getDate()+"/"+getCurrentMonth()+"/"+currentdate.getFullYear();
}
function getCurrentMonth() {
    var currentdate = new Date();
    var currentmonth = currentdate.getMonth()+1;
    
    if (currentmonth < 10) {
        return "0"+currentmonth;    
    } else {
        return currentmonth;
    }
}
