function ajax(){
        try{
                xmlhttp = new XMLHttpRequest();
        }
        catch(ee){
                try{
                        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e){
                        try{
                                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch(E){
                                xmlhttp = false;
                        }
                }
        }
        return xmlhttp;
}

function setLocaleNull(selectForm)
{
        var qtdeformLocale = selectForm.length;
        if( qtdeformLocale > 0 )
        {
                while (qtdeformLocale != 0)
                {//exclui elementos antigos
                        selectForm.options[0] = null;
                        qtdeformLocale = selectForm.length;
                }
        }
}

// url_decode version 1.0
function url_decode(str) {
        var n, strCode, strDecode = "";

        for (n = 0; n < str.length; n++) {
                if (str.charAt(n) == "%") {
                        strCode = str.charAt(n + 1) + str.charAt(n + 2);
                        strDecode += String.fromCharCode(parseInt(strCode, 16));
                        n += 2;
                } else if (str.charAt(n) == "+") {
                        strCode = str.charAt(n);
                        strDecode += " ";
                } else {
                        strDecode += str.charAt(n);
                }
        }
        return strDecode;
}

function url_encode(str) {
        var hex_chars = "0123456789ABCDEF";
        var noEncode = /^([a-zA-Z0-9\_\-\.])$/;
        var n, strCode, hex1, hex2, strEncode = "";

        for(n = 0; n < str.length; n++){
                if (noEncode.test(str.charAt(n))){
                        strEncode += str.charAt(n);
                }
                else{
                        strCode = str.charCodeAt(n);
                        hex1 = hex_chars.charAt(Math.floor(strCode / 16));
                        hex2 = hex_chars.charAt(strCode % 16);
                        strEncode += "%" + (hex1 + hex2);
                }
        }
        return strEncode;
}

function changeOptions(id,select){
	var field = document.getElementById(select);
	setLocaleNull(field);
	var xmlhttp = ajax();
	xmlhttp.open("GET", "funcoesLocais.php?id="+id);

        xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState == 4) {
                        var conteudo = url_decode(xmlhttp.responseText);
			if( conteudo.length > 4 ){
				eval("document.getElementById('"+select+"div').style.display='';");
				eval("document.getElementById('"+select+"div2').style.display='';");
				var rows = conteudo.split(';');
				for( var i=0; i < rows.length; i++ ){
					cols = rows[i].split('::');
					field.options[i] = new Option(cols[1],cols[0],0,0);
				}
			}
			else{
				eval("document.getElementById('"+select+"div').style.display='none';");
				eval("document.getElementById('"+select+"div2').style.display='none';");
			}
                }
        }
        xmlhttp.send(null)

}

function viewCur(url){
	window.open(url,"pop","width=780,height=500,menubar=0,scrollbars=yes,toolbar=0,location=0,directories=0,resizable=0,top=10,left=10");
}

