function getHTTPObject(){
    if(typeof(XMLHttpRequest)!='undefined'){ return new XMLHttpRequest(); }
    var axO=['Microsoft.XMLHTTP','Msxml2.XMLHTTP','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0'];
    for(var i=0; i<axO.length; i++){
        try{ return new ActiveXObject(axO[i]); }
        catch(e){ } 
    }
    return null;
} //var http;
function abreAjax(url,div,method){
    var http;    
    http = getHTTPObject();
    url = antiCacheRand(url);
    var sendvars = null;    
    http.onreadystatechange = function(){ conteudoPagina(http,div); };
    if(method == "GET"){
        method = 'GET';
        sendvars = null;
    }
    else{
        method = 'POST';
        sendvars = url;
    }
    http.open(method,url,true);
    http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=iso-8859-1");
    http.setRequestHeader("Cache-Control","no-store, no-cache, must-revalidate");
    http.setRequestHeader("Cache-Control","post-check=0, pre-check=0");
    http.setRequestHeader("Pragma","no-cache");
    http.send(sendvars);
}
function conteudoPagina(http,div){
    if(http.readyState == 1){ document.getElementById(div).innerHTML = "<span style='color:#000000;font:10px Verdana;'>Carregando ...</span>"; }
    if(http.readyState == 4){
        if(http.status == 200){
            var resultado = http.responseText;
            resultado = resultado.replace(/\+/g," ");
            resultado = unescape(resultado);
            extraiScript(resultado);
            document.getElementById(div).innerHTML = resultado;
        }
        else{ document.getElementById(div).innerHTML = 'ERRO!'+httpStatus(http.status); }
    }
}
function fnPadrao(id,url,div,method){
    try{ document.getElementById(id).style.display = ''; }
    catch(e){ /*ERRO*/ }
    setTimeout("abreAjax('"+url+"','"+div+"','"+method+"')",100);
}
function fnLimpa_Div(id){
    document.getElementById(id).innerHTML = "";
    document.getElementById(id).style.display = 'none';
}
function httpStatus(erro){
    switch(erro){
        case 0: return "Erro Desconhecido";
        case 400: return "400: Solicitação incompreensível"; break;
        case 403: return "403: Você Não tem permissão para acessar essa página"; break;
        case 404: return "404: Não foi encontrada a URL solicitada"; break;
        case 405: return "405: O servidor não suporta o método solicitado"; break;
        case 500: return "500: Erro desconhecido de natureza do servidor"; break;
        case 503: return "503: Capacidade máxima do servidor alcançada"; break;
        default: return "Erro: "+erro+". Mais informações em http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"; break;
    }
}
/*function extraiScript(texto){
    var inicio = 0;
    while(inicio!=-1){
        inicio = texto.indexOf('<script',inicio);
        if(inicio >= 0){
            inicio = texto.indexOf('>',inicio)+1;
            var fim = texto.indexOf('</script>',inicio);
            codigo = texto.substring(inicio,fim);
            novo = document.createElement("script")
            novo.text = codigo;
            document.body.appendChild(novo);
        }
    }
}*/
function extraiScript(texto){
    var inicio, pos_src, fim, codigo;
    var objScript = null;
    inicio = texto.indexOf('<script',0)
    while(inicio!=-1){
        var objScript = document.createElement("script");
        pos_src = texto.indexOf(' src',inicio)
        inicio = texto.indexOf('>',inicio)+1;
        if(pos_src < inicio && pos_src >=0){
            inicio = pos_src+4;
            fim = texto.indexOf('.',inicio)+4;
            codigo = texto.substring(inicio,fim);
            codigo = codigo.replace("=","").replace(" ","").replace("\"","").replace("\"","").replace("\'","").replace("\'","").replace(">","");
            objScript.src = codigo;
        }
		else{
            fim = texto.indexOf('</script>',inicio);
            codigo = texto.substring(inicio,fim);
            objScript.text = codigo;
        }
        document.body.appendChild(objScript);
        inicio = texto.indexOf('<script',fim);
        objScript = null;
    }
}
function antiCacheRand(url){
    var data = new Date();
    if(url.indexOf("?")>=0){ return url+"&"+encodeURI(Math.random()+"_"+data.getTime()); }
    else{ return url+"?"+encodeURI(Math.random()+"_"+data.getTime()); }
}
function addItem(obj,strText,strValue,blSel,intPos){
    var newOpt,i,ArTemp,selIndex; 
    selIndex = (blSel)?intPos:obj.selectedIndex; 
    newOpt = new Option(strText,strValue); 
    Len = obj.options.length+1 
    if(intPos > Len) return 
    obj.options.length = Len 
    if(intPos != Len){ 
        ArTemp = new Array(); 
        for(i=intPos;i<obj.options.length-1;i++) ArTemp[i] = Array(obj.options[i].text,obj.options[i].value); 
        for(i=intPos+1;i<Len;i++) obj.options[i] = new Option(ArTemp[i-1][0],ArTemp[i-1][1]); 
    }
    obj.options[intPos] = newOpt; 
    if(selIndex > intPos) obj.selectedIndex = selIndex+1; 
    else if(selIndex == intPos) obj.selectedIndex = intPos; 
}