/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxcombo(url, containerid, label, handle, mostrar){

    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest()
    else if (window.ActiveXObject){ // if IE
        try {
            page_request = new ActiveXObject("Msxml2.XMLHTTP")
        } catch (e){
            try{
                page_request = new ActiveXObject("Microsoft.XMLHTTP")
            } catch (e){}
        }
    } else
        return false

    if(label)
        document.getElementById(label).innerHTML = "<img src='/images/home/loading.gif' border='0'>";

    if(handle)
        page_request.onreadystatechange = function(){
            eval(handle);
        }
    else {
        page_request.onreadystatechange=function(){
            loadcombo(page_request, containerid, label, mostrar)
        }
    }

    page_request.open("GET", url, true);
    page_request.setRequestHeader('encoding','ISO-8859-1');
    page_request.send(null);
	
	
}
/**
You should not provide an onreadystatechange handler 
for synchronous requests. If you do, versions of Firefox 
prior to version 3 call the handler anyway. Firefox 3 
blocks until the request is completed (as in the example above).
 Firefox 2 provides the same behavior as long as you don't 
 implement an onreadystatechange handler.
 
 Add Alessandro 07/10/2008
 */
function ajaxpage(url, containerid, handle, assync){

    // definindo sync entre as conexões. Definir assync = 1 para modo sincrono
    var stat = true;
    if(assync)
        stat = false;


    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest()
    else if (window.ActiveXObject){ // if IE
        try {
            page_request = new ActiveXObject("Msxml2.XMLHTTP")
        } catch (e){
            try{
                page_request = new ActiveXObject("Microsoft.XMLHTTP")
            } catch (e){}
        }
    } else
        return false

    if(handle)
        page_request.onreadystatechange = function(){
            eval(handle);
        }
    else {
        page_request.onreadystatechange=function(){
            loadpage(page_request, containerid)
        }
    }
	
    if (bustcachevar) //if bust caching of external page
        bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()

    page_request.open("GET", url+bustcacheparameter, stat)
    page_request.setRequestHeader('encoding','ISO-8859-1');
    page_request.send(null)
}

function ajaxpost(formname,containerid,handle){
	
    var strData="";
    var url = "";

    if(document.getElementById(formname)){
        url = document.getElementById(formname).action;
    } else
        url = formname;

    if(containerid=="")
        containerid = "divCorpo";

    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest()
    else if (window.ActiveXObject){ // if IE
        try {
            page_request = new ActiveXObject("Msxml2.XMLHTTP")
        } catch (e){
            try{
                page_request = new ActiveXObject("Microsoft.XMLHTTP")
            } catch (e){}
        }
    } else
        return false

    if(handle)
        page_request.onreadystatechange = handle;
    else {
        page_request.onreadystatechange=function(){
            loadpage(page_request, containerid)
        }
    }
	
    if (bustcachevar) //if bust caching of external page
        bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()

    if(document.getElementById(formname)){
        for( i=0; i < document.getElementById(formname).elements.length; i++ ){
            //			alert(document.getElementById(formname).elements[i].type);
            if( document.getElementById(formname).elements[i].type == "checkbox" || document.getElementById(formname).elements[i].type == "radio" ) {
                //				alert(document.getElementById(formname).elements[i].checked);
                if( document.getElementById(formname).elements[i].checked == true )
                    strData += document.getElementById(formname).elements[i].name + "="+ escape(document.getElementById(formname).elements[i].value) + "&";
                else if (document.getElementById(formname).elements[i].type == "checkbox" && document.getElementById(formname).elements[i].checked == false )
                    strData += document.getElementById(formname).elements[i].name + "= 0" + "&";
            } else
                strData += document.getElementById(formname).elements[i].name + "="+ escape(document.getElementById(formname).elements[i].value) + "&";
		
        }
    } else {
        strData = url;
    }
    
    //alert(url);


		
    page_request.open("POST", url, true)
    page_request.setRequestHeader('encoding','ISO-8859-1'); 
    page_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
    page_request.setRequestHeader('Content-length', strData.length ); 
    page_request.send(strData); //Envia dados ao documento da url.
	
	
}

function loadpage(page_request, containerid){

    document.getElementById(containerid).innerHTML="<img src='/images/home/loading.gif' border='0'>";

    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
        // coloca o valor no objeto requisitado
        texto=unescape(page_request.responseText.replace(/\+/g," "));
        //alert(texto);
        document.getElementById(containerid).innerHTML=texto;
        // executa scripts
        extraiScript(texto);
    }
		
//		document.getElementById(containerid).innerHTML=unescape(page_request.responseText.replace(/\+/g," "))
}

function loadcombo(page_request, containerid,label, mostrar){

    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
        // coloca o valor no objeto requisitado
        retorno=unescape(page_request.responseText.replace(/\+/g," "));
        //prompt("",retorno);
        var arrayRetorno = retorno.split("#;#");
        var cont = 0;
		
        var x=document.getElementById(containerid);
        x.options.length = 0;
	  	
        if(!mostrar){
            var opcao=document.createElement('option');
            opcao.value = "";
            opcao.text  = "Selecione";

            try	{
                x.add(opcao,null); // standards compliant
            } catch(ex)	{
                x.add(opcao); // IE only
            }
        }
        
        // 1. campo: Id da combo, normalmente um IDF
        // 2. campo: true or false pra definir se o item tem que ficar selecionado
        // 3. em diante: descrição do item
        // ex.: 1.#!#.true.#!#.Registro selecionado#;#
        //		2.#!#.false.#!#.Registro não selecionado#;#
        //alert(arrayRetorno.length);
        while(cont < arrayRetorno.length-1) {

            itens = arrayRetorno[cont].split(".#!#.");
            //alert(itens);
            opcao=document.createElement('option');
            opcao.value = itens[0];

            if(itens[1] == "true")
                opcao.selected = true;
	  		
            for(k=2; k < itens.length;k++){
                opcao.text = opcao.text + itens[k];
            }
			
            try	{
                x.add(opcao,null); // standards compliant
            } catch(ex) {
                x.add(opcao); // IE only
            }
            cont++;
        }

        if(label)
            document.getElementById(label).innerHTML = "";
			
    }
}


function extraiScript(texto){
    // inicializa o inicio ><
    var ini = 0;
    // loop enquanto achar um script
    while (ini!=-1){
        // procura uma tag de script
        ini = texto.indexOf('<script', ini);
        // se encontrar
        if (ini >=0){
            // define o inicio para depois do fechamento dessa tag
            ini = texto.indexOf('>', ini) + 1;
            // procura o final do script
            var fim = texto.indexOf('</script>', ini);
            // extrai apenas o script
            codigo = texto.substring(ini,fim);
            // executa o script
            //            alert(codigo);
            eval(codigo);
        }
    }
}

function ajaxupload(url,containerid,method,formname){
	
    //	alert("FORM: "+formname);

    var strData="";

    if(!method)
        method="POST";
	
    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest()
    else if (window.ActiveXObject){ // if IE
        try {
            page_request = new ActiveXObject("Msxml2.XMLHTTP")
        } catch (e){
            try{
                page_request = new ActiveXObject("Microsoft.XMLHTTP")
            } catch (e){}
        }
    } else
        return false

    page_request.onreadystatechange=function(){
        loadpage(page_request, containerid)
    }

    if (bustcachevar) //if bust caching of external page
        bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()

    for( i=0; i < document.getElementById(formname).elements.length; i++ ){
        //			alert(document.getElementById(formname).elements[i].type);
        if( document.getElementById(formname).elements[i].type == "checkbox" || document.getElementById(formname).elements[i].type == "radio" ) {
            //				alert(document.getElementById(formname).elements[i].checked);
            if( document.getElementById(formname).elements[i].checked == true )
                strData += document.getElementById(formname).elements[i].name + "="+ escape(document.getElementById(formname).elements[i].value) + "&";
            else if (document.getElementById(formname).elements[i].type == "checkbox" && document.getElementById(formname).elements[i].checked == false )
                strData += document.getElementById(formname).elements[i].name + "= 0" + "&";
        } else
            strData += document.getElementById(formname).elements[i].name + "="+ escape(document.getElementById(formname).elements[i].value) + "&";
		
    }
    alert(strData);

    page_request.open(method, url, true)
    page_request.setRequestHeader('encoding','ISO-8859-1'); 
    page_request.setRequestHeader('Content-type', 'multipart/form-data'); 
    page_request.setRequestHeader('Content-length', strData.length ); 
    page_request.send(strData); //Envia dados ao documento da url.
	
	
}

