
dojo.require("dojo.xml.*");
dojo.require("dojo.io.*");
dojo.require("dojo.graphics.*");


var last_innerCall = null;

var altura_anterior=0;
var  hs  = new Array();

hs[0] = document.location.href;


function httpdo_get_history(url, target, innerCall) {

    hslen = hs.length;
    if ( hs.length == 1) { window.history.go(-5); }

    document.getElementById('history').value = url;
	document.getElementById('loader').style.display='';
	
	
	if(innerCall!="nofade" && last_innerCall!="nofade")	
		dojo.graphics.htmlEffects.fadeOut(document.getElementById(target),200);
	
	var req = new dojo.io.Request(url + "&innerCall=1");

	req.onload = function (e) {
		document.getElementById(target).innerHTML = e.data;
		document.getElementById('loader').style.display='none';
		if(innerCall!="nofade" && last_innerCall!="nofade")	
			dojo.graphics.htmlEffects.fadeIn(document.getElementById(target),200);	
		
		
		if(document.getElementById(target).style.display=='none')
			document.getElementById(target).style.display='';
			
		scroll_value = (document.all)?document.body.scrollTop:window.pageYOffset; 

		altura_actual = document.getElementById('content').offsetHeight;
		if((altura_anterior>0) && (innerCall==null || innerCall==undefined || innerCall=="nofade") && (document.all)) // controlar o scroll no IE
		{

			if(altura_anterior>altura_actual) // é necessário verificar se precisamos de scroll..
			{
				// heuristicas simples para verificar se é necessário fazer scroll
				if((altura_actual-scroll_value)>400)
					window.scroll(0, 0);
				if(scroll_value>400)
					window.scroll(0, 0);
				if(altura_actual<scroll_value)
					window.scroll(0, 0);
			}
		}
		altura_anterior = altura_actual;

		//alert("Body:" + document.getElementById('body_site').offsetHeight + "\nContent:" + document.getElementById('content').offsetHeight + "\nScroll:" + scroll_value);
		//alert("Body:" + document.getElementById('body_site').offsetHeight + "\nContent:" + document.getElementById('content').offsetHeight + "\nScroll:" + scroll_value);
		//document.getElementById('body_site').style.height=document.getElementById('content').offsetHeight;
		changeNavegacao();
		changeImagem();
		changeTitle()
		
		
		/*
		tentativa de executar scripts no resultado que é carregado
		scripts = document.getElementById(target).all.tags("script");
		for(i=0;i<scripts.length;i++)
			eval(scripts[i].innerHTML);*/
	}
	
	// handle back button
	req.addEventListener("backbutton", function (e) { 		

												

							document.title="Goweb";
							if(window.history.length!=0)
							{
                                hslen = hs.length;
                                ind = hslen-1;
                                rem = hs.splice (ind,1);
								httpdo_get_history(rem, 'page');
												}
										});
	
	last_innerCall = innerCall;
	dojo.io.bind(req);



}



function httpdo_get(url, target, innerCall) {
    
    if ( hs.length == null ) hslen = 0;
	else hslen = hs.length;
    hs[hslen] = document.getElementById('history').value;
    document.getElementById('history').value = url;

	
	document.getElementById('loader').style.display='';
	
	
	if(innerCall!="nofade" && last_innerCall!="nofade")	
		dojo.graphics.htmlEffects.fadeOut(document.getElementById(target),200);
	
	var req = new dojo.io.Request(url + "&innerCall=1");
    
	req.onload = function (e) {
		document.getElementById(target).innerHTML = e.data;
	//alert(e.data.substring(6000,8000));
		document.getElementById('loader').style.display='none';
		if(innerCall!="nofade" && last_innerCall!="nofade")	
			dojo.graphics.htmlEffects.fadeIn(document.getElementById(target),200);	
		
		
		if(document.getElementById(target).style.display=='none')
			document.getElementById(target).style.display='';
			
		scroll_value = (document.all)?document.body.scrollTop:window.pageYOffset; 

		altura_actual = document.getElementById('content').offsetHeight;
		if((altura_anterior>0) && (innerCall==null || innerCall==undefined || innerCall=="nofade") && (document.all)) // controlar o scroll no IE
		{

			if(altura_anterior>altura_actual) // é necessário verificar se precisamos de scroll..
			{
				// heuristicas simples para verificar se é necessário fazer scroll
				if((altura_actual-scroll_value)>400)
					window.scroll(0, 0);
				if(scroll_value>400)
					window.scroll(0, 0);
				if(altura_actual<scroll_value)
					window.scroll(0, 0);
			}
		}
		altura_anterior = altura_actual;
		changeNavegacao();
		changeImagem();
		changeTitle()
		
		
	}
	
	// handle back button
	req.addEventListener("backbutton", function (e) { 		

												

							document.title="Goweb";
							if(window.history.length!=0)
							{
													

                                hslen = hs.length;
                                ind = hslen-1;
                                rem = hs.splice (ind,1);
								httpdo_get_history(rem, 'page');
												}
										});
	
	last_innerCall = innerCall;
	dojo.io.bind(req);



}


/* funções gráficas... */

				function showImagemBackground()
				{
					dojo.graphics.htmlEffects.fadeIn(document.getElementById("imagem_background"),200);
				}
				
				function changeImagem()
				{
					if(document.getElementById("imagem_background_interna") && document.getElementById("imagem_background"))
					{
						if(document.getElementById("imagem_background_interna").innerHTML==document.getElementById("imagem_background").innerHTML) return;
						dojo.graphics.htmlEffects.fadeOut(document.getElementById("imagem_background"),200);	
						document.getElementById("imagem_background").innerHTML = document.getElementById("imagem_background_interna").innerHTML;
						setTimeout('showImagemBackground()', 500)
					}
				}
				
				function changeNavegacao()
				{
					if(document.getElementById("barra_navegacao_interna") && document.getElementById("barra_navegacao"))
					{
						document.getElementById("rodape").style.display='';
						document.getElementById("barra_navegacao").innerHTML = document.getElementById("barra_navegacao_interna").innerHTML;
					}
					
					if(!document.getElementById("barra_navegacao_interna") && document.getElementById("barra_navegacao"))
						document.getElementById("rodape").style.display='none';
				}
				
				function changeTitle()
				{
					if(document.getElementById("titulo_pagina"))
					{
						document.title = "Goweb - " + document.getElementById("titulo_pagina").innerHTML;
					}
				}
				
				
				function explodeDiv(start, end) {
					dojo.graphics.htmlEffects.explode(document.getElementById(start), document.getElementById(end), 250);
					document.getElementById(end).style.display = '';
				}			
				
				function implodeDiv(start, end) {
					dojo.graphics.htmlEffects.implode(document.getElementById(start), document.getElementById(end), 250);
				}		
				
				function fadeOut(id)
				{
					dojo.graphics.htmlEffects.fadeHide(document.getElementById(id),200);	
				}
				
/* fim de funções gráficas*/




/////////// FUNÇÕES DE POST

var htmlobj_comunicate; 
var comunicate;

function init_comunicate()
{
	try {
		comunicate=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			comunicate=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (oc) {
			comunicate=null;
		}
	}
	if(!comunicate && typeof XMLHttpRequest != "undefined")
		comunicate = new XMLHttpRequest();			
}


function httpdo_postform(form, target) {
	init_comunicate();
	comunicate.open('POST',form.action,true);
	comunicate.setRequestHeader('MessageType','CALL');
	comunicate.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
	document.getElementById('loader').style.display='';
	if(target!='')
	{
		htmlobj_comunicate = document.getElementById('page');
		comunicate.onreadystatechange=httpresponse;
	}
	comunicate.send(get_form_vars(form));	
	return false;
}

function httpresponse() {
	if (comunicate.readyState==4) {
		text = comunicate.responseText;
	    htmlobj_comunicate.innerHTML=text;	
		document.getElementById('loader').style.display='none';
	}
} 

function get_form_vars(fobj) 
{ 
	elements = fobj.elements;
	var pairs = new Array();
	
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].type=='checkbox')
			if (!elements[i].checked )
				continue;
			
		if ((name = elements[i].name) && (value = elements[i].value))
			pairs.push(name + "="+ encode64(value));
	}
	pairs.push("encoded64=1");
	return pairs.join("&");
}


var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
"abcdefghijklmnopqrstuvwxyz" + //all lowercase
"0123456789+/="; // all numbers plus +/=

//Heres the encode function
function encode64(inp)
{
	var out = ""; //This is the output
	var chr1, chr2, chr3 = ""; //These are the 3 bytes to be encoded
	var enc1, enc2, enc3, enc4 = ""; //These are the 4 encoded bytes
	var i = 0; //Position counter

	do { //Set up the loop here
		chr1 = inp.charCodeAt(i++); //Grab the first byte
		chr2 = inp.charCodeAt(i++); //Grab the second byte
		chr3 = inp.charCodeAt(i++); //Grab the third byte

		//Here is the actual base64 encode part.
		//There really is only one way to do it.
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}

		//Lets spit out the 4 encoded bytes
		out = out + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) +
		keyStr.charAt(enc4);

		// OK, now clean out the variables used.
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";

	} while (i < inp.length); //And finish off the loop

	//Now return the encoded values.
	return out;
}

//Heres the decode function
function decode64(inp)
{
	var out = ""; //This is the output
	var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
	var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
	var i = 0; //Position counter

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	var base64test = /[^A-Za-z0-9\+\/\=]/g;

	if (base64test.exec(inp)) { //Do some error checking
		alert("There were invalid base64 characters in the input text.\n" +
		"Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +
		"Expect errors in decoding.");
	}
	inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	do { //Here.s the decode loop.

		//Grab 4 bytes of encoded content.
		enc1 = keyStr.indexOf(inp.charAt(i++));
		enc2 = keyStr.indexOf(inp.charAt(i++));
		enc3 = keyStr.indexOf(inp.charAt(i++));
		enc4 = keyStr.indexOf(inp.charAt(i++));

		//Heres the decode part. There.s really only one way to do it.
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		//Start to output decoded content
		out = out + String.fromCharCode(chr1);

		if (enc3 != 64) {
			out = out + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			out = out + String.fromCharCode(chr3);
		}

		//now clean out the variables used
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";

	} while (i < inp.length); //finish off the loop

	//Now return the decoded values.
	return out;
}

