/*
request_source = onnavigate("request_source", function(data){
		
	data = url_decode(data);
	var url = data.split("|")[1];
	var resposta = data.split("|")[0];
	var msg = data.split("|")[2];
	ajaxn(url_encode(url),resposta,msg);
});
*/

function ajax(url,resposta,msg){
		
	if (msg == null) {msg = '';}
	
	var lista = url.split("?");
		
	var url 		= lista[0];
				
	var parametros	= "";
		
	var i=0;
				
	// Vamos tratar o caracter ?
	if (lista.length > 2) {
		
		for (i = 1; i <= lista.length-1; i++) {
			if (i > 1) { parametros = parametros + '_ES_'; }
			parametros = parametros + lista[i]; 
			
		}
	} else {
		parametros	= lista[1];
	}
	
	// Vamos tratar o caracter % (ATENÇÃO O TRATAMENTO E DIFERENTE DO CARACTER ?)
	lista = parametros.split("%");
	parametros = '';
	
	if (lista.length > 2) {
		
		for (i = 0; i <= lista.length-1; i++) {
			if (i > 1) { parametros = parametros + '_PP_'; }
			parametros = parametros + lista[i];
			
		}
	} else {
		parametros	= lista[0];
	}
	
	parametros = url_encode(parametros);
	
	// tratamento para que o navegador possa atuliar sempre a pagina
	ext = rand(1000);
	
	request_source(resposta+'|'+url+'?'+parametros+'|'+msg+'|'+ext);
	
}

function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

function ajaxn(url,resposta,msg){			
		
		url = url_decode(url);
						
		var lista = url.split("?");
		
		var url 		= lista[0];		
				
		var parametros	= "";
		
		var i=0;
		
		// Ajuste no get passado para tratamento da ?
		if (lista.length > 2) {
			
			for (i = 1; i <= lista.length; i++) {			
				
				parametros = parametros + url_decode(lista[i]);			
				
			}
		} else {
			parametros	= url_decode(lista[1]);
		}
		
		// Ajuste no campo valuePage para correção da aspas imples e o & comercial
		if (parametros.split("valuePage").length > 1) {
			var antes = parametros.split("valuePage")[0];
			var depois = parametros.split("valuePage")[1];
			var depois_str = depois.replace("&","%E6%",'g');
			parametros = antes + 'valuePage' + depois_str.replace("'","%AS%",'g'); 			
		} else {
			parametros = parametros.replace("_ES_","?",'g');
			//alert(parametros);
		}
		
		/*
		if (parametros.split("fieldLAYOUT_CSS").length > 1) {
			var antes = parametros.split("fieldLAYOUT_CSS")[0];
			var depois = parametros.split("fieldLAYOUT_CSS")[1];
			var depois_str = depois.replace("_PP_","%",'g');
			parametros = antes + 'fieldLAYOUT_CSS' + depois_str;			
		}
		*/
		
		try{
			eval("var "+resposta+" = new XMLHttpRequest();");

		}catch(ee){

	    try{

	        eval(resposta + " = new ActiveXObject(\"Msxml2.XMLHTTP\");");

	    }catch(e){

	        try{

	      	   eval(resposta + " = new ActiveXObject(\"Microsoft.XMLHTTP\");");

	        }catch(E){

	        //eval(resposta + " = false");

		document.getElementById(resposta).innerHTML='erro';

       			 }

  	  		}

	}
	
		if (msg=='') {
			document.getElementById(resposta).innerHTML=('<center><img style="padding:5px;" src="midia/image/loading.gif" ><br>Carregando... Aguarde!</center>');
		} else {
			document.getElementById(resposta).innerHTML=msg;
		}



		

		eval(resposta).onreadystatechange=function(){			

			if (eval(resposta).readyState == 4){

				//if (eval(resposta).status == 200){

					var html = eval(resposta).responseText;

					if (document.getElementById(resposta)!= null){
						
						//document.getElementById(resposta).innerHTML=url_decode(html);
						document.getElementById(resposta).innerHTML=html;
						
						tratarRetorno(msg);
						

					}

				//}

			}

		}
		
		eval(resposta).open("POST",url_encode(url),true);

		eval(resposta).setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=iso-8859-1;');			
		eval(resposta).send(url_encode(parametros));	
		
}


function tratarRetorno(msg){
	
	var lista = msg.split(":");

	if (lista[0] == 'PEDIDOGETTOTAL'){
		ajaxnull('index.php?control=gettotal&print=sim&ajax=sim&current=page_pedido_get.php&IDPEDIDO='+lista[1],'controlepedidototal','ajax-control.gif');
	}
}


function ajaxnull(url,resposta,msg){

		if (msg == null) {msg = '';}
	
		var parametros	= url.split("?")[1];
		var url 		= url.split("?")[0];			

		try{
			eval("var "+resposta+" = new XMLHttpRequest();");

		}catch(ee){

	    try{

	        eval(resposta + " = new ActiveXObject(\"Msxml2.XMLHTTP\");");

	    }catch(e){

	        try{

	      	   eval(resposta + " = new ActiveXObject(\"Microsoft.XMLHTTP\");");

	        }catch(E){

	        //eval(resposta + " = false");

		document.getElementById(resposta).innerHTML='erro';

       			 }

  	  		}

	}
			
		if (msg != ''){
			document.getElementById(resposta).innerHTML=('<img src="midia/image/'+msg+'" >');
		}
	
		eval(resposta).onreadystatechange=function(){			

			if (eval(resposta).readyState == 4){

				//if (eval(resposta).status == 200){

					var html = eval(resposta).responseText;

					if (document.getElementById(resposta)!= null){
						
						//document.getElementById(resposta).innerHTML=url_decode(html);
						document.getElementById(resposta).innerHTML=html;						


					}

				//}

			}

		}




		eval(resposta).open("POST",url_encode(url),true);		

		eval(resposta).setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=iso-8859-1;');

		eval(resposta).send(url_encode(parametros));	
		
}


function parentAjax(url,resposta,msg){

	

		var parametros	= url.split("?")[1];

		var url 		= url.split("?")[0];

				

		try{

			eval("var "+resposta+" = new XMLHttpRequest();");

		}catch(ee){

	    try{

	        eval(resposta + " = new ActiveXObject(\"Msxml2.XMLHTTP\");");

	    }catch(e){

	        try{

	      	   eval(resposta + " = new ActiveXObject(\"Microsoft.XMLHTTP\");");

	        }catch(E){

	        eval(resposta + " = false");

		parent.document.getElementById(resposta).innerHTML='erro';

	       			 }

	  	  		}

		}

		

		if (msg != null){

			parent.document.getElementById(resposta).innerHTML=('<div style="float:right;background:#CC4444;color:white;">'+msg+'</div>');

		}

		

		eval(resposta).onreadystatechange=function(){			

			if (eval(resposta).readyState == 4){

				//if (eval(resposta).status == 200){

					var html = eval(resposta).responseText;

					if (parent.document.getElementById(resposta)!= null){

						parent.document.getElementById(resposta).innerHTML=html;

					}

				//}

			}

		}


		// Ajuste para evitar que sejam acessados dados de outras páginas
		parametros = 'INDEXMODULO='+INDEXMODULO+'&'+parametros;

		eval(resposta).open("POST", url,true);		

		eval(resposta).setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8;');

		eval(resposta).setRequestHeader('Cache-Control','no-cache, no-store;');

		eval(resposta).send(encodeURI(parametros));	

}


var variavel_getdata;

function getdata(url){

		var parametros	= url.split("?")[1];
		var url 		= url.split("?")[0];			
		
		var resposta = "getxml";
		
		try{

			eval("var "+resposta+" = new XMLHttpRequest();");

		}catch(ee){

	    try{

	        eval(resposta + " = new ActiveXObject(\"Msxml2.XMLHTTP\");");

	    }catch(e){

	        try{

	      	   eval(resposta + " = new ActiveXObject(\"Microsoft.XMLHTTP\");");

	        }catch(E){

			        eval(resposta + " = false");

	       			 }

	  	  		}

		}
			
	
		eval(resposta).onreadystatechange=function(){			

			if (eval(resposta).readyState == 4){

					variavel_getdata = eval(resposta).responseText;

			}

		}

		// Ajuste para evitar que sejam acessados dados de outras páginas
		parametros = 'INDEXMODULO='+INDEXMODULO+'&'+parametros;

		
		eval(resposta).open("POST",url_encode(url),false);		

		eval(resposta).setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=iso-8859-1;');

		eval(resposta).send(url_encode(parametros));
		
		if (!variavel_getdata) {
			eval(resposta).send(url_encode(parametros));	
		}
		
		return variavel_getdata;
		
}

function borderOut(idTable,idLayer,borderColor,bgColor,mode,title){

	

	function td(style,id,title){

		if (title!=undefined){	

			return '<td '+style+' id="'+id+'" >'+title+'</td>';

		}else{

			return '<td '+style+' id="'+id+'"></td>';	

		}

	}

	

	var txt;

	var borderWidth = '1';

	

	var b1 = 'style="width:'+ borderWidth +'px;height:'+ borderWidth +'px;"';

	var n1 = 'style="width:'+ borderWidth +'px;height:'+ borderWidth +'px;background-color:'+borderColor+'"';

	var n2 = 'style="width:'+ borderWidth +'px;height:'+ borderWidth +'px;background-color:'+borderColor+';opacity:.70;filter:alpha(opacity=70)"';

	if (mode==1){

	var n3 = 'style="width:'+ borderWidth +'px;height:'+ borderWidth +'px;background-color:'+borderColor+';opacity:.20;filter:alpha(opacity=20);"';

	}else if (mode==2){

	var n3 = 'style="width:'+ borderWidth +'px;height:'+ borderWidth +'px;background-color:'+borderColor+';opacity:.90;filter:alpha(opacity=90);"';

	}

	

	

	var d1 = 'style="background-color:white;"';

	var d2 = 'style="background-color:#c0c0c0;"';

	

	

	onmouseover="this.src=\'img/CloseOn.gif\'"

	onmouseout="this.src=\'img/CloseOff.gif\'"

	

	txt='';

	txt+='<table style="position:absolute;" align="center" style="display:none;" id="'+ idTable +'" cellpadding="0" cellspacing="0">';

	txt+='<tr>'+td(b1)+td(b1)+td(b1)+td(b1)+td(n1)+td(n1)+td(n1)+td(b1)+td(b1)+td(b1)+td(b1)+'</tr>';

	txt+='<tr>'+td(b1)+td(b1)+td(n1)+td(n2)+td(n3)+td(d2)+td(n3)+td(n2)+td(n1)+td(b1)+td(b1)+'</tr>';

	txt+='<tr>'+td(b1)+td(n1)+td(n2)+td(d2)+td(d2)+td(d2)+td(d2)+td(d2)+td(n2)+td(n1)+td(b1)+'</tr>';

	txt+='<tr>'+td(b1)+td(n2)+td(d2)+td(d2)+td(d2)+td(d2)+td(d2)+td(d2)+td(d2)+td(n2)+td(b1)+'</tr>';

	txt+='<tr>'+td(n1)+td(n3)+td(d2)+td(d2)+td(d2)+td(d2)+td(d2)+td(d2)+td(d2)+td(n3)+td(n1)+'</tr>';

	

	txt+='<tr>'+td(n1)+td(d2)+td(d2)+td(d2)+td(d2)+'<td bgcolor="#c0c0c0" width="170" ><div style="width:150px;float:left;">'+ title +'</div><div style="height:18px;float:right;" ><img onclick="closeLayer(\''+idTable+'\');" onmouseover="'+onmouseover+'" onmouseout="'+onmouseout+'" src="img/closeoff.gif"></div></td>'+td(d2)+td(d2)+td(d2)+td(d2)+td(n1)+'</tr>';

	

	txt+='<tr>'+td(n1)+td(d1)+td(d1)+td(d1)+td(d1)+td(d1,idLayer)+td(d1)+td(d1)+td(d1)+td(d1)+td(n1)+'</tr>';

	

	txt+='<tr>'+td(n1)+td(n3)+td(d1)+td(d1)+td(d1)+td(d1)+td(d1)+td(d1)+td(d1)+td(n3)+td(n1)+'</tr>';

	txt+='<tr>'+td(b1)+td(n2)+td(d1)+td(d1)+td(d1)+td(d1)+td(d1)+td(d1)+td(d1)+td(n2)+td(b1)+'</tr>';

	txt+='<tr>'+td(b1)+td(n1)+td(n2)+td(d1)+td(d1)+td(d1)+td(d1)+td(d1)+td(n2)+td(n1)+td(b1)+'</tr>';

	txt+='<tr>'+td(b1)+td(b1)+td(n1)+td(n2)+td(n3)+td(d1)+td(n3)+td(n2)+td(n1)+td(b1)+td(b1)+'</tr>';

	txt+='<tr>'+td(b1)+td(b1)+td(b1)+td(b1)+td(n1)+td(n1)+td(n1)+td(b1)+td(b1)+td(b1)+td(b1)+'</tr>';

	txt+='</table>';	

	document.getElementById("resto").innerHTML=txt;



}	









function ajaxLayer(url,resposta,title,msg){
	

		borderOut('border','divLayer','#969696','#ffffff',2,title);

		var parametros	= url.split("?")[1];

		var url 		= url.split("?")[0];

		var divLayer = document.getElementById("divLayer");

		var border	 = document.getElementById("border");						

		var tela	 = document.getElementById("tela");	

		try{

			eval("var "+resposta+" = new XMLHttpRequest();");

		}catch(ee){

	    try{

	        eval(resposta + " = new ActiveXObject(\"Msxml2.XMLHTTP\");");

	    }catch(e){

	        try{

	      	   eval(resposta + " = new ActiveXObject(\"Microsoft.XMLHTTP\");");

	        }catch(E){

	        eval(resposta + " = false");

		document.getElementById(resposta).innerHTML='erro';

	       			 }

	  	  		}

		}



		tela.style.display='block';

		

			document.getElementById(resposta).innerHTML=('<center><img style="border:1px solid #c0c0c0;background-color:white;padding:5px;" src="loading.gif" ></center>');


		border.style.visibility='hidden';	

		eval(resposta).onreadystatechange=function(){			

			if (eval(resposta).readyState == 4){

				var html = eval(resposta).responseText;

					if (document.getElementById(resposta)!= null){

						document.getElementById(resposta).innerHTML=html;	

						border.style.display='block';									

						border.style.top='50%';

						border.style.left='50%';									

						border.style.marginTop='-'+(border.offsetHeight/2)+'px';												

						border.style.marginLeft='-'+(border.offsetWidth/2)+'px';						

						border.style.visibility='visible';	

					}

			}

		}

		eval(resposta).open("POST", url,true);		
		eval(resposta).setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=iso-8859-1;');
		eval(resposta).send(parametros);	

}


// url_encode version 1.0 
function url_encode(str) { 
	if (str == null) {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) {
	if (str == null) {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; 
} 

