
// JavaScript Document

  // url_encode version 1.0  
    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;  
    }  

    // 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 {  
                strDecode += str.charAt(n);  
            }  
        }  

        return strDecode;  
    } 

function XmlHttpRequest(){
      //verifica se o browser tem suporte a ajax
	  try {
	  
         	ajax = new ActiveXObject("Microsoft.XMLHTTP");
			
      } catch(e) {
	  
         try {
		 
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
          } catch(ex) {
		  
            	try {
               		ajax = new XMLHttpRequest();
            	 } catch(exc) {
               		alert("Esse browser não tem recursos para uso do Ajax");
               		ajax = null;
           		 }
           }
      }
}

function carregar(id,act){
XmlHttpRequest();
var campo = new Array();
	campo = dados_form();

var campos = "act="+act+"&pagina="+campo[1]+"&id="+id;

	if(ajax){

	     ajax.open("POST", "action.php", true);
		 ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 //ajax.setRequestHeader("Accept-Charset","UTF-8");	  			  
		 		ajax.onreadystatechange = function() {
            		if(ajax.readyState == 4 ) {
			   				if(ajax.responseXML) { 
			      				retorno_action(ajax.responseXML,campo,act,id);
			   				}
            		}
				}
        	}
        	

	  var param = campos;
	  ajax.send(param);
                 alert(param);
}

function retorno_action(obj,campo,act,id){

	var dataArray = obj.getElementsByTagName("reg");
	var len = dataArray.length;
	var item;
	var total = campo.length;
	var camp = new Array(total);


		  for(i = 0; i < len; i++) {			
	        //percorre o arquivo XML paara extrair os dados
            	item = dataArray[i];

						camp[0] =  item.getElementsByTagName("id")[0].firstChild.nodeValue;

						for(s = 3; s < total; s++){
								
								if(item.getElementsByTagName(campo[2].substr(0,3)+"_"+campo[s])[0].firstChild.nodeValue == "null"){
									camp[s-2] = " ";
								}else{
									camp[s-2] =  item.getElementsByTagName(campo[2].substr(0,3)+"_"+campo[s])[0].firstChild.nodeValue;
								}
								

							if(len == 1 && act !== "paginar"){

								
								if(campo[2] == "noticias" && s == 3){
									var todas = document.getElementById("todas_noticias");
		 							todas.className = "Noticias";
									todas.innerHTML = '<h3><div id="not_titulo"></div></h3><p class="data"><strong><div id="not_data"></div></strong></p><p><div id="not_descricao"></div></p>';
									document.getElementById("noticias").style.display = "inline";
								}
								
								if(document.getElementById(campo[2].substr(0,3)+"_"+campo[s])){
									document.getElementById(campo[2].substr(0,3)+"_"+campo[s]).innerHTML = (campo[2].substr(0,3)+"_"+campo[s] == 'not_data' || campo[2].substr(0,3)+"_"+campo[s] == 'ori_data')?formato_data(camp[s-2],campo[2]):(campo[s] == 'data')?camp[s-2].substr(8,2)+"/"+camp[s-2].substr(5,2)+"/"+camp[s-2].substr(0,4):camp[s-2];
								}
								
							}else{
								if(i == 0){
									document.getElementById("noticias").style.display = "none";
									if(document.getElementById(campo[2].substr(0,3)+"_"+campo[s])){
										document.getElementById(campo[2].substr(0,3)+"_"+campo[s]).innerHTML = "";
									}
								}
							}
						}
						if(len > 1 || act == "paginar"){
							if(i == 0){
								if(campo[2] == "noticias") var html = '<ul id="ulNoticias">';
							}
							
							if(campo[2] == "noticias") html += '<li><a href="?id='+ camp[0] +'">'+ camp[2] +' <br /> <strong>'+ camp[1] +'</strong></a></li>';
						}
		  }
		  


		 if(len > 1 || act == "paginar"){ 

		 	if(campo[2] == "noticias"){
		 		var todas = document.getElementById("todas_noticias");
		 		todas.className = "TodasNoticias";
		 		html += '</ul><div id="paginacao">';
				
				var resultados_total = item.getElementsByTagName("total")[0].firstChild.nodeValue;

				if(resultados_total%10 == 0){
					var paginas = resultados_total / 10;
				}else{
					var paginas = parseInt(resultados_total / 10) + 1;
				}
				
				html += '<ul><li><strong>Mais not&#237;cias:</strong></li>';
				var pagina = id;
				for(p=1;p <= paginas; p++){	
					if (p == ((pagina == 0)?1:(pagina == 1)?1:pagina)){
						html += "<li>"+ p +"</li>";
					}else{
						html += "<li><a href='#' onClick='carregar("+ p +",\"paginar\")'>"+ p +"</a></li>";
					}
				}
				html += "</ul></div>";
				
				todas.innerHTML = html;
				
			}
			
		 }
}


function formato_data(data,tipo){
					var dia = data.substr(8,2);
					var mes = data.substr(5,2);
					var ano = data.substr(0,4);

					switch(mes){
					  	case '01':
							mes = "Janeiro";
							break;
						case '02':
							mes = "Fevereiro";
							break;
						case '03':
							mes = "Mar&#231o";
							break;
						case '04': 
							mes = "Abril";
							break;
						case '05': 
							mes = "Maio";
							break;
						case '06': 
							mes = "Junho";
							break;
						case '07': 
							mes = "Julho";
							break;
						case '08': 
							mes = "Agosto";
							break;
						case '09': 
							mes = "Setembro";
							break;
						case '10': 
							mes = "Outubro";
							break;
						case '11': 
							mes = "Novembro";
							break;
						case '12': 
							mes = "Dezembro";
					}

					if(tipo == 'noticias'){
						return dia  + " de " + mes + " de " + ano;
					}else{
						return mes + "/" + ano;
					}
}