var isIE = navigator.appVersion.indexOf('MSIE') > 0;
var isMozilla = document.getElementById && !isIE;
var isNetscape4 = document.layers ? true : false;

   /**
   * Muda o conteudo da pagina
   *
   */
   function changeContent(id,titulo,width,height,frm){
	Element.hide('content');
	showContent(id,titulo,width,height,frm);
   }      	

   /**
   * Mostra o conteudo na pagina
   * 
   */
   function showContent(id,titulo,width,height,frm){
	var objMessages = $('content');	
	objMessages.innerHTML="";	
	
        //define o tamanho
    objMessages.style.width = width;
	objMessages.style.height = height;
	objMessages.style.position = 'absolute';

        // define a posição
	var vtop = ((document.body.clientHeight - parseInt(objMessages.offsetHeight)) / 2)  + document.body.scrollTop;
	var vleft = ((document.body.clientWidth - parseInt(objMessages.offsetWidth)) / 2);	

	objMessages.style.left = 320;
	objMessages.style.top = 20;
	
	//define o titulo
	var barMessages = document.createElement('div');
	barMessages.id = 'barMessages';	
	var spanTitle = document.createElement('span');
	spanTitle.id = 'spanTitle';
	spanTitle.appendChild(document.createTextNode(titulo));
	barMessages.appendChild(spanTitle);
	objMessages.appendChild(barMessages);

	
	//define o conteudo
	var contentMessages = document.createElement('div');
	contentMessages.id = id;
	contentMessages.style.display = '';
	
	objMessages.appendChild(contentMessages);

	// tamanhos
	var objMessagesWidth = parseInt(objMessages.style.width.replace('px', ''), 10);
	barMessages.style.width = (isIE ? objMessagesWidth : objMessagesWidth - 4 ) + 'px';
	spanTitle.style.width = (isIE ? width - 54 : width - 45 ) + 'px';
	$(id).style.height = (isIE ? height - 18 : height - 29) + 'px';
	$(id).style.width = (isIE ? objMessagesWidth : objMessagesWidth - 10) + 'px';

	//abre o conteudo via ajax
	openSector(id,frm);		
   }

/**
* Requisições em ajax
*/
function ajaxInnerRequest(url, params, target, appear) {
	showMouseWaiting(true,appear);
	var myAjax = new Ajax.Request(url, {
		method: 'get', 
		parameters: params,
		onSuccess: function(response) {
			showMouseWaiting(false);
			var text = response.responseText;
			text = text.replace(/\+/g," ");
			text = unescape(text);
			try {
				$(target).innerHTML = text;		
				if(appear){						
					new Effect.Appear('content');
				}
								
			} catch(e) {
				alert(e);
			}
		},
		onFailure: function(response) {
			showMouseWaiting(false,appear);
			var text = response.responseText;
			text = text.replace(/\+/g," ");
            text = unescape(text);
            try {
				eval(text);
				showMessages();
			} catch(e0) {
				try {
					alert('Houve uma falha ao recuperar os dados: ' + text);
				} catch (e) {
					alert(e);
				}
			}
		}
	});
}

/**
 * Abre o conteudo via ajax
 *
 */
function openSector(sector,frm){	
	if(frm){
	  ajaxInnerRequest('jsp/'+sector+'.jsp',frm,sector,true);		
	}else{
	  ajaxInnerRequest('jsp/'+sector+'.jsp','page='+sector,sector,true);	
	}
}


/** 
 * Altera o estado do cursor para 'aguarde' caso o valor flag 
 * for true, ou para 'normal' caso o valor da flag for false.
 *
 * @param flag true para cursor 'aguarde', false para cursor 'normal'.
 */
function showMouseWaiting(flag,appear) {
	if(appear && appear == "true") return;
	var bodyTags = document.getElementsByTagName('body');
	bodyTags[0].style.cursor = flag ? 'wait' : 'default';
	
	var selectTags = document.getElementsByTagName('select');
	for(var i=0; i<selectTags.length; i++) {
		selectTags[i].style.cursor = flag ? 'wait' : 'default';
	}
	
	var inputs = document.getElementsByTagName('input');
	for(var i=0; i<inputs.length; i++) {
		if (inputs[i].type == 'text' || inputs[i].type == 'password') {
			inputs[i].style.cursor = flag ? 'wait' : 'text';
		} else {
		inputs[i].style.cursor = flag ? 'wait' : 'default';
		}
	}
	if($('loading')){
		if(flag){
			Element.show('loading');
		}else{
			Element.hide('loading');
			Element.show('pagediv');
		}
	}
}

function hideLoading(){
try{
	if($('loading')){
		Element.hide('loading');
	}
}catch(e){}
finally{
	Element.show('pagediv');
}
}

function abreJanela(url,w,h){
	if(w==null || w=='') w='660';
  	if(h==null || h=='') h='420';
  	
   window.open(url,'4','width='+w+',height='+h+',toolbar=no,statusbar=no,scrollbars=yes,resizable=yes');
}


function validaEmail(campo) {
	if (!campo || (campo.value == "") || campo.value.length<=0) {
	 return false;
	} else {
		prim = campo.value.indexOf("@")
		if(
		  (campo.value.indexOf("..") > 0)
		||(campo.value.indexOf(")") > 0)
		||(campo.value.indexOf("(") > 0)
		||(campo.value.indexOf("]") > 0)
		||(campo.value.indexOf("[") > 0)
		||(campo.value.indexOf("/") > 0)
		||(campo.value.indexOf(".com.br.") > 0)
		||(campo.value.indexOf("@.") > 0)
		||(campo.value.indexOf(".@") > 0)
		||(campo.value.indexOf("hotmeil.com") > 0)
		||(campo.value.indexOf("zipmeil.com") > 0)
		||(campo.value.indexOf(" ") != -1)
		||(campo.value.indexOf(".") < 1)
		||(campo.value.indexOf("@",prim + 1) != -1)
		||(prim<2)
			) {
			return false;
		}
	}
		return true;
}

// verifica se um campo esta vazio ou não existe
function isEmpty(campo) {	
  return (campo==null || campo.value==null || campo.value=='');
}

function goTo(url){	
	window.location=url;
}

/*
* Define o campo como errado ou não
*/
function setError(campo,erro,mensagem){
	var sub = $(campo.name+'Error');	
	if(erro){			
		campo.className="hightlight";		
		if(sub)
			sub.innerHTML = sub.innerHTML+mensagem+"<br>";	
			
		return false;
	}else{
		campo.className = "";
		if(sub)
			sub.innerHTML = "";	
			
		return true;
	}
} 


function ajaxEvalRequest(url, params, afterCall) {
	showMouseWaiting(true);
	var myAjax = new Ajax.Request(url, {
		method: 'get', 
		parameters: params,
		onSuccess: function(response) {
			showMouseWaiting(false);
			var text = response.responseText;
			text = text.replace(/\+/g," ");
			text = unescape(text);
			try {
				goTo('index.jsp?pagina=obrigado');	
			} catch(e) {
				alert(e);
			}
		},
		onFailure: function(response) {
			showMouseWaiting(false);
			var text = response.responseText;
			text = text.replace(/\+/g," ");
            text = unescape(text);
            try {
				eval(text);				
			} catch(e0) {
				try {
					alert('Houve uma falha ao recuperar os dados: ' + text);
				} catch (e) {
					alert(e);
				}
			}
		}
	});
}


