function ajaxRequestInternalSync(url,postValues,returnFunction) {
    this.url = url;
    this.postValues = postValues;
    this.xmlhttp = false;

    this.create = function() {
        try {
            this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                this.xmlhttp = false;
            }
        }
        if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
            try {
                this.xmlhttp = new XMLHttpRequest();
            } catch (e) {
                this.xmlhttp = false;
            }
        }
        if (!this.xmlhttp && window.createRequest) {
            try {
                this.xmlhttp = window.createRequest();
            } catch (e) {
                this.xmlhttp=false;
            }
        }
    }

    this.send = function() {
        var sendData = '';
        sendData += this.postValues;
        var xh = this.xmlhttp;
        xh.open("POST",this.url,false);
        xh.onreadystatechange = function() {
            if (xh.readyState == 4) {
                if (true || xh.status == 200 || xh.status == 304)	{
                    returnFunction(xh.responseText, true);
                } else {
                    returnFunction(undefined, false);
                }
            }
        };
        xh.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
        xh.setRequestHeader("encoding","UTF-8");
        xh.setRequestHeader("Content-length", sendData.length );
        xh.send(sendData);
    }

    this.create();

    if (this.xmlhttp) {
        this.send();
    } else {
        alert('Este browser não suporta requisições por AJAX');
    }
}


function ajaxRequestInternal(url,postValues,returnFunction) {
    this.url = url;
    this.postValues = postValues;
    this.xmlhttp = false;

    this.create = function() {
        try {
            this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                this.xmlhttp = false;
            }
        }
        if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
            try {
                this.xmlhttp = new XMLHttpRequest();
            } catch (e) {
                this.xmlhttp = false;
            }
        }
        if (!this.xmlhttp && window.createRequest) {
            try {
                this.xmlhttp = window.createRequest();
            } catch (e) {
                this.xmlhttp=false;
            }
        }
    }

    this.send = function() {
        var sendData = '';
        sendData += this.postValues;
        var xh = this.xmlhttp;
        xh.open("POST",this.url,true);
        xh.onreadystatechange = function() {
            if (xh.readyState == 4) {
                if (true || xh.status == 200 || xh.status == 304)	{
                    returnFunction(xh.responseText, true);
                } else {
                    returnFunction(undefined, false);
                }
            }
        };
        xh.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
        xh.setRequestHeader("encoding","UTF-8");
        xh.setRequestHeader("Content-length", sendData.length );
        xh.send(sendData);
    }

    this.create();

    if (this.xmlhttp) {
        this.send();
    } else {
        alert('Este browser não suporta requisições por AJAX');
    }
}


function showConteudo(url) {
    //document.getElementById('loading').style.display = '';
    //document.getElementById('loadingModalDiv').style.display = '';

    ajaxRequestInternal(url,"postValues",function(retorno,sucesso) {
        if(sucesso) {
            document.getElementById("conteudo").innerHTML = retorno;
        }
    //document.getElementById('loading').style.display = 'none';
    //document.getElementById('loadingModalDiv').style.display = 'none';
    });
}


function expandir(id) {
    document.getElementById('e'+id).style.display = 'none';
    document.getElementById('r'+id).style.display = '';
    document.getElementById('m'+id).style.display = '';
}

function recolher(id) {
    document.getElementById('e'+id).style.display = '';
    document.getElementById('r'+id).style.display = 'none';
    document.getElementById('m'+id).style.display = 'none';
}


function imprimirContaReceber(id) {
    //var iframe = document.createElement("iframe");
    //iframe.src = "boleto/boleto" + new Date().getTime() +  ".pdf?id=" + id;
//    window.open("boleto/boleto" + new Date().getTime() +  ".pdf?id=" + id, "boleto") ;
    //iframe.style.display = "none";
    //document.body.appendChild(iframe);
    window.open("cobranca/index.jsp?id=" + id, "boleto"+ id +"" , "width=440,height=140");
  
}

function excluirContaReceber(id) {
    if(confirm("Deseja realmente excluir este documento?")) {
        ajaxRequestInternal("excluirContaReceber.jsp?id=" + id,"postValues",function(retorno,sucesso) {
            alert(retorno);
            if(sucesso) {
                showConteudo('financeiro.jsp');
            }
        });
    }
}



function restaurarContaReceber(id) {
    ajaxRequestInternal("restaurarContaReceber.jsp?id=" + id,"postValues",function(retorno,sucesso) {
        alert(retorno);
        if(sucesso) {
            showConteudo('financeiroExcluido.jsp');
        }
    });
}


function pesquisarLivros() {
    var dados = '';
    dados += '?titulo=' + escape(document.buscaBiblioteca.titulo.value);
    dados += '&tituloOpt=' + escape(document.buscaBiblioteca.tituloOpt.value);

    dados += '&autor=' + escape(document.buscaBiblioteca.autor.value);
    dados += '&autorOpt=' + escape(document.buscaBiblioteca.autorOpt.value);

    dados += '&assunto=' + escape(document.buscaBiblioteca.assunto.value);
    dados += '&assuntoOpt=' + escape(document.buscaBiblioteca.assuntoOpt.value);

    dados += '&indice=' + escape(document.buscaBiblioteca.indice.value);
    dados += '&indiceOpt=' + escape(document.buscaBiblioteca.indiceOpt.value);

    dados += '&tam=' + escape(document.buscaBiblioteca.tam.value);

    ajaxRequestInternal("bibliotecaResultado.jsp" + dados,"",function(retorno,sucesso) {
        if(sucesso) {
          document.getElementById('bibliotecaResultado').innerHTML = retorno;
        }
    });
}

function pesquisarLivrosPage(page) {
    var dados = '';
    dados += '?pg=' + page;
    
    ajaxRequestInternal("bibliotecaResultado.jsp" + dados,"",function(retorno,sucesso) {
        if(sucesso) {
          document.getElementById('bibliotecaResultado').innerHTML = retorno;
        }
    });
}


function executeActionNull(controllerVar, action, retFunction) {    
	var sendObj = new Object();	         
	sendObj.action = action;         
	sendObj.controller = controllerVar;                 
 
	var sendData = JSON.stringify(sendObj); 	         
	ajaxRequest("../../RequestServlet", sendData , retFunction);	         
}


function ajaxRequest(link,valores,funcao) {    
    ajaxRequestInternal(link,valores,function(retorno,sucesso) {	        
        var retObj;		
	if(sucesso) {
            try {
                retObj = parseToObject(retorno) ;
            } catch (e) {
                retObj = {sucess : false , message : 'ERRO: Problemas ao executar a operacao!'}; 
            }			
	} 	
	if(!retObj) retObj = {sucess : false , message : 'ERRO: Problemas ao executar a operacao!'}; 		        
        funcao(retObj);		
    
    }); 		
}
