function gE(ID){
	return document.getElementById(ID);
}

function gEs(tag) {
	return document.getElementsByTagName(tag);
}

window.onload = loadFunctions;

function loadFunctions(){
	criaDivs();
}

// função para criar as imagens laterais
function criaDivs(){
	var tagBody = gEs('body').item(0);
	var geral = gE('geral');
	var esq = gE('esqgeral');
	var sizesPage = getPageSize();
	var area1 = (arrayPageSize[0] - 778) / 2;	
	area = Math.round(area1) + 'px';	
	
	if (arrayPageSize[0] > 800 && esq && geral){	
		geral.style.marginLeft = area;
		esq.style.display = 'block';		
		esq.style.width = area;
	}else{
		geral.style.marginLeft = '0px';
	}
}

function getPageSize(){
	
	var xScroll, yScroll, nav;
	nav = 0;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;		
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
		nav = 6;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,nav) 

}

//--------------------AJAX----------------------//
function openAjax() {
	var ajax;
	try {
		ajax = new XMLHttpRequest();
	} catch(ee) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				ajax = false;
			}
		}
	}
	return ajax;
}

function setLoading(opt, local){
	var refer = gE(local);
		
	if (opt == true) {						
		refer.innerHTML = "";
		refer.style.textAlign = "center";
		var img = document.createElement('img');			
		img.setAttribute('src','img/loader.gif');
		img.setAttribute('id','loading');			
		img.setAttribute('width','32');						
		if (!document.getElementById('loading')) {				
			refer.insertBefore(img, refer.firstChild);				
		}
	}else if (opt == false) {			
		refer.style.textAlign = "";
		var imgLoading = gE('loading');			
		if (imgLoading) {
			imgLoading.parentNode.removeChild(imgLoading);
		}
	}
}

function abrePaginaAjax(url, local){
	var conteudo = gE(local);
	var ajax = openAjax();
	ajax.open('GET', url, true);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 1){
			conteudo.innerHTML = '';
			setLoading(true, local);
		} // if->readyState->1
		if (ajax.readyState == 4){				
			if (ajax.status == 200){				
				setLoading(false, local);
				conteudo.style.textAlign = 'left';						
				conteudo.innerHTML = ajax.responseText;										
			}// id->ajax.status->200
			else{
				conteudo.innerHTML = "Erro ao carregar a dados";			
			}
		}// if->readyState->4
	}// ajax.readystatechange
	ajax.send(null);
	return false;		
}
//--------------------FIM AJAX-----------------------//

var Validacao = {
	ValidaItens: function(){
		var args = Validacao.ValidaItens.arguments; // coloca os parametros em uma variavel no qual se tornará um Array
		if (args.length > 0){ // verifica se há parametros atribuidos a função
			for (var x = 0; x < args.length; x++){
				var vItem = gE(args[x]);
				if (vItem.value == "Digite sua mensagem aqui." || vItem.value == "" || vItem.value == null){
					vItem.focus();
					vItem.style.border = 'solid 1px #E8B809';
					alert("Campo em branco");
					return(false);
				}else{
					vItem.style.border = '';
				}				
				//verifica se há algum campo de e-mail para chamar o validaEmail
				var vCampo = args[x].toLowerCase();		
				if (vCampo.indexOf('email') > 0){
					if (Validacao.ValidaEmail(vItem) == false)
						return(false);
				}				
			} // fim do for			
			return(true);
		}else{
			alert('Ocorreu um erro ao realizar operação');
			return(false);			
		}// else (args.length > 0){
	}, // fim do ValidaItens
	
	ValidaEmail: function(pCampo){
		var email = pCampo.value;
		var resp = email.search(/(\w[\w\.\+]+)@(.+)\.(\w+)$/)==0;
		
		if (resp == false){
			pCampo.focus();
			pCampo.style.border = 'solid 1px red';
			alert('E-mail inválido');
			return(false);
		}else{
			return(true);
		}
	}
};