/* ----------------------------------------------------
REQUISICAO ASSINCRONA
-------------------------------------------------------
Exemplo: ajax("minhaPagina.jsp","funcaoQualquer");
function funcaoQualquer(){ 
alert(req.status);
alert(req.readyState);
alert(req.responseText);
}
pagina = pagina a ser acessada
funcao = funcao a ser executada quando a requisicao tiver seu estado alterado
req = objeto que trarÃ¡ a resposta da requisicao

----------------------------------------------------------*/

// Objeto requisicao para requisicoes assincronas

function req(){
    this.readyState = -1;
    this.status = -1;
}


function ajax(pag,func){
    var ajaxProto = new SuperAjax();
    ajaxProto.pagina = pag;
    ajaxProto.funcao = func;
    ajaxProto.executar();
    req = ajaxProto.req;
    this.req = ajaxProto.req;
    return ajaxProto;
}

function SuperAjax(){
    this.req = null; 
    this.pagina = null;
    this.funcao = null;
    
    this.executar = function(){
        if (window.XMLHttpRequest){
            try{
                this.req = new XMLHttpRequest();
            }catch (e){
                alert('erro 1 ' + e);
            }
        }else if (window.ActiveXObject) {
            try {
                this.req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e){
                try {
                    this.req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert('erro 2' + e);
                }
            }
        }
        this.req.onreadystatechange = eval(this.funcao);
        this.req.open("POST",this.pagina, true);
        this.req.send("");
    }
}

function carregar(pagina){
ajax(pagina,"carregarConteudo")
}

function carregarConteudo(){
    if(document.getElementById("conteudo")!=null){
    	document.getElementById("conteudo").innerHTML = req.responseText;
    }else{
if (window.addEventListener)
    window.addEventListener("load", carregarConteudo, false) //invoke function
if (window.attachEvent)
    window.attachEvent("onload", carregarConteudo) //invoke function 
    }
}


function mudarFoto(foto){
    document.getElementById('image').src = foto;
}


function enviarFormulario(form, pagina, paginaDeSucesso){
    this.req = null; 
    
    if (window.XMLHttpRequest){
        try{
            this.req = new XMLHttpRequest();
        }catch (e){
            alert('erro 1 ' + e);
        }
    }else if (window.ActiveXObject) {
        try {
            this.req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e){
            try {
                this.req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert('erro 2' + e);
            }
        }
    }


    var parameters = new Array(form.elements.length);
    for(var i = 0 ; i < form.elements.length ; i++)
	parameters[i] = form.elements[i].name + "=" + form.elements[i].value;

    this.req.onreadystatechange = function(){
	    if(req.readyState == 4){
     		   if(req.status == 200){
		     	       carregar(paginaDeSucesso);
     	   	   }
	    }
	}
    this.req.open("GET",pagina + "?" + parameters.join("&"), true);
    this.req.send(parameters);
}
