/* AJAX FUNCTIONS by Caio Augusto Franchi */
//
function instanciaAjax() {
	//Função que cria e retorna uma Instância xmlHttp;
	var xmlHttp = null;
		try {
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			var arrProgs = ['MSXML2.XMLHTTP', 'Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0'];
			var isHabil = false;
			for (var i = 0; (i < arrProgs.length) && ( ! isHabil); i ++) {
				try {
					xmlHttp = new ActiveXObject(arrProgs[i]);
					isHabil = true;
				} catch (e) {}
			}
			if ( ! isHabil ) {
				alert('Esse browser não suporta recursos AJAX');
				return null;
			}
		}
	return xmlHttp;
	
}
//
function executaScript(pParametros,pValores,pLocalExecucao,pTempoExecucao,pMetodo,pLocalRetorno) {
	//Função que executa scripts em background
}
//
function executaJavaScript(pTexto){
	//Essa função faz com que todos os javascripts que estiverem dentro de uma página
	//executada via ajax funcionem
    var ini, pos_src, fim, codigo;
    var objScript = null;
    ini = pTexto.indexOf('<script', 0);
	//
	while (ini!=-1){
        var objScript = document.createElement("script");
        pos_src = pTexto.indexOf(' src', ini)
        ini = pTexto.indexOf('>', ini) + 1;
		//
        if (pos_src < ini && pos_src >=0) {
            ini = pos_src + 4;
            fim = pTexto.indexOf('.', ini)+4;
            codigo = pTexto.substring(ini,fim);
            codigo = codigo.replace("=","").replace(" ","").replace("\"","").replace("\"","").replace("\'","").replace("\'","").replace(">","");
            objScript.src = codigo;
        }
		else {
            fim = pTexto.indexOf('</script>', ini);
            codigo = pTexto.substring(ini,fim);
            objScript.text = codigo;
        }
        document.body.appendChild(objScript);
        ini = pTexto.indexOf('<script', fim);
        objScript = null;
    }
}
/*
//
function RedimensionaFade() {
	
	if(document.all) {
		
			valornovo =document.getElementById('divGeral').offsetHeight+(screen.availHeight-106);
			document.getElementById('divFade').style.height = valornovo+"px";
			
	}
	
}
//
function MostraCarregador() {
	RedimensionaFade();
	document.getElementById('divFade').style.display = "block";
	document.getElementById('divCarregador').style.display = "block";
	
}
//
function FechaCarregador() {
	
	document.getElementById('divFade').style.display = "none";
	document.getElementById('divCarregador').style.display = "none";
	
}
//
function Display(pObj) {
	
	var divDisp = document.getElementById(pObj).style.display;
		
		if (divDisp == "block") {
			document.getElementById(pObj).style.display = "none";
		} else {
			document.getElementById(pObj).style.display = "block";
		}
	
}
//

//
function Executa(pAction,pTempo) {
	
	var nmbTimeOut = setTimeout( 
	function(){
		clearTimeout(nmbTimeOut);
		MostraCarregador();
		//
		xmlHttp = InstanciaAjax();
		xmlHttp.open("POST", pAction , true);
		xmlHttp.setRequestHeader('Content-Type',"application/x-www-form-urlencoded;");
		xmlHttp.send("teste=teste");
		xmlHttp.onreadystatechange = function() {
			switch(xmlHttp.readyState){
				case 4:
					FechaCarregador();
					ExecutaScript(xmlHttp.responseText);
				break;
			}
		}
	},pTempo);
	
}
//
function ShowContent(pAction,pTarget,pTempo) {
	
	var nmbTimeOut = setTimeout( 
	function(){
		clearTimeout(nmbTimeOut);
		MostraCarregador();
		//
		xmlHttp = InstanciaAjax();
		xmlHttp.open("POST", pAction , true);
		xmlHttp.setRequestHeader('Content-Type',"application/x-www-form-urlencoded;");
		xmlHttp.send("teste=teste");
		xmlHttp.onreadystatechange = function() {
			switch(xmlHttp.readyState){
				case 1:
					//document.getElementById(pTarget).innerHTML="Carregando...";
				break;
				case 2:
					//document.getElementById(pTarget).innerHTML="Carregando...";
				break;
				case 3:
					//document.getElementById(pTarget).innerHTML="Carregando...";
				break;
				case 4:
					document.getElementById(pTarget).innerHTML = xmlHttp.responseText;
					FechaCarregador();
					ExecutaScript(xmlHttp.responseText);
				break;
			}
		}
	},pTempo);
	
}
//
function MostraAviso(pAviso,pCampo) {
	
	document.getElementById('divAviso').style.display = "block";
	document.getElementById('txtAviso').innerHTML = pAviso;
	if (pCampo != '') {
		//document.getElementById('txtLink').onclick = "function anonymous() { alert('sim'); }";
	}
	
}
//
function FechaAviso() {
	
	document.getElementById('divAviso').style.display = "none";
	
}
//
function AbrePop(pUrl,pLargura,pAltura,pNome){
	//Nem o bloqueador de Pops do windows, nem o do google pegam
    sealWin = window.open(pUrl, "win" ,'toolbar=no,status=yes,menubar=no,scrollbars=yes,left=150,top=100,width='+pLargura+',height='+pAltura);
    self.name = "mainWin";
	//
}
//
function Arredonda(pValor,pCasas){
	
   var valorFinal = Math.round( pValor * Math.pow( 10 , pCasas ) ) / Math.pow( 10 , pCasas );
   valorFinal = valorFinal.toFixed(pCasas); 
   return(valorFinal);

}
//
function TravaNumero(pTecla)
{
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers) // Nestcape
		var tecla = pTecla.which;
		if (tecla > 47 && tecla < 58 || tecla == 120) // numeros de 0 a 9
			return true;
		else
			{
				if (tecla != 8) // backspace
					event.keyCode = 0;
					//return false;
				else
					return true;
			}
}
//
function data(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);


if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;


else if ((("0123456789/").indexOf(keychar) > -1))
   return true;


else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}
//
function valor(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);


if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;


else if ((("0123456789,").indexOf(keychar) > -1))
   return true;


else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}
//
function documento(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);


if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;


else if ((("0123456789./-").indexOf(keychar) > -1))
   return true;


else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}
*/

/*function FechaLayer(){
 setTimeout("collapseMenu('logbox','none')", 900);
}*/

function setStyle(pDiv, pClass){
	var divTemp = document.getElementById(pDiv);
	//
	divTemp.className=pClass;
}	

function collapseMenu(whichEl, displayValue){
	var objeto = document.getElementById(whichEl);
	var iexplorer = (document.all)? true:false 
	
	if(iexplorer == false && displayValue == "block"){
		displayValue="table";
	}
	objeto.style.display = displayValue;	
}
	
function findPosX(obj){
	var curleft = 0;
	//
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}else if (obj.x){
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	//
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}else if (obj.y){
		curtop += obj.y;
	}
	return curtop;
}

function findW(obj){
	var curwidth = 0;
	//
	if (obj.offsetParent){
		curwidth = obj.offsetWidth;
	}
	else if (obj.width)
		curwidth += obj.width;
	return curwidth;
}

function findH(obj){
	var curheight = 0;
	//
	if (obj.offsetParent){
		curheight = obj.offsetHeight;
	}
	else if (obj.height)
		curheight += obj.height;
	return curheight;
}

function posiciona(obj, referencia, t, l){
	var newX = findPosX(referencia) + l;
	var newY = findPosY(referencia) + t;
	//
	obj.style.left = newX;
	obj.style.top = newY;
}

function posicionaLogbox(){
	var newX = findPosX(document.getElementById("btlogin"));
	var newY = findPosY(document.getElementById("btlogin"));
	//
	logbox.style.left = newX-90;
	logbox.style.top = newY-25;
	logbox.style.visibility = "visible";	
}

var tamH = 200;
var tamHi = 50;
var idTempo = 0;

function aumenta(){
	var objeto = document.getElementById("soBarra");
	var objetoH = findH(objeto);
	//
	if(objetoH >= tamH){
		clearInterval(idTempo);
	}else{
		clearInterval(idTempo);
		objeto.style.height = objetoH + 10;
		idTempo = setInterval(aumenta, 20);
	}
}
function diminui(){		
	var objeto = document.getElementById("soBarra");
	var objetoH = findH(objeto);
	//
	if(objetoH <= tamHi){
		clearInterval(idTempo);
	}else{
		clearInterval(idTempo);
		objeto.style.height = objetoH - 10;
		idTempo = setInterval(diminui, 20);
	}
}

atual = ""
function collapse( whichEl ){
		if (atual != ""){
			eval("document.all."+atual).style.display = (eval("document.all."+atual).style.display == "none")? "" : "none";
		}
		eval("document.all."+whichEl).style.display = (eval("document.all."+whichEl).style.display == "none")? "" : "none";
		atual = whichEl
}

function contaTexto(numero,texto,contador){
 var tamanho = texto.value.length;
 var tex = texto.value;
 if (tamanho>=(numero+1)){
  texto.value=tex.substring(0,(numero-1));
 }else{
	eval(contador).value = numero -texto.value.length;
 return true;
 }
}

function atualizaDados(){
	document.location.href="editar.asp"	
}

function atualizaMeta(fase,peso){
	var ajxAtualizaMeta = new ajxDestroy("POST");
	//alert(fase+' , '+ peso);
	ajxAtualizaMeta.mostraConteudo('atualizaFase,atualizaPeso',fase+','+peso,'../inc/atualizameta.asp','ajx',true,0);
	//alert("Atualizando. Peso:"+peso+" Fase:"+fase);
	//window.open("../inc/atualizameta.asp?atualizaPeso="+peso+"&atualizaFase="+fase)
	//window.location.reload(true)
}
	
	
function mostraInfo(cor,noFlashReload){	
	alturaDivInfo = findH(document.getElementById("divFlash"))+findPosY(document.getElementById("divFlash"));
	document.getElementById("divInfo").style.top=alturaDivInfo;
	if(document.all){
		document.getElementById("divInfo").style.display='block';
	}else{
		document.getElementById("divInfo").style.display='table';
	}
	document.getElementById("divInfoBox").innerHTML='';
	ajxFases.mostraConteudo("fase",""+cor+"","../inc/conceitofase.asp","divInfoBox",true,0);
	document.getElementById("divInfoBox").style.backgroundColor=arrCores[cor];
	if(noFlashReload){
		atualizaFlash(cor);
	}
}

function mostraPalavra(cor,profissional){	
	alturaDivInfo = findH(document.getElementById("divFlash"))+findPosY(document.getElementById("divFlash"));
	document.getElementById("divPalavra").style.top=alturaDivInfo+65;
	if(document.all){
		document.getElementById("divPalavra").style.display='block';
	}else{
		document.getElementById("divPalavra").style.display='table';
	}
	document.getElementById("divPalavraBox").innerHTML='';
	ajxFases.mostraConteudo("fase,profissional",""+cor+","+profissional+"","../inc/palavrafase.asp","divPalavraBox",true,0);
}

function fechaPalavra(cor,profissional){	
	document.getElementById("divPalavra").style.display='none';
	
}

function fechaDHTML(divid){
	document.getElementById(divid).style.display='none';
}

function decimalMask(e,caixa,casas,separador,decimais){
	var tamanhoMax = casas + decimais;
	var valorAtual = document.getElementById(caixa).value.replace(separador,'');
	var tamanhoAtual = valorAtual.length;
	var tecla; 
	
	if (!e) { 
		e = window.event; 
	}

	if (e.keyCode) { 
		tecla = e.keyCode; 
	}	else if(e.which) { 
		tecla = e.which;
	}

	if ((tecla >= 47 && tecla < 58) || (tecla >= 96 && tecla <= 105)) { 
		if (tamanhoAtual < tamanhoMax) {
			do {
				valorAtual = "0" + valorAtual;
				tamanhoAtual = valorAtual.length
			} while(tamanhoAtual < tamanhoMax);
		} else {
				valorAtual = valorAtual.slice(1,valorAtual.length)
		}
		document.getElementById(caixa).value = valorAtual.slice(0,casas)+separador+valorAtual.slice(casas,tamanhoMax-1);
		//event.keyCode = 0;
		return true;
	} else if (tecla == 8) {
		do {
			valorAtual = "0" + valorAtual;
			tamanhoAtual = valorAtual.length
		}while(tamanhoAtual < tamanhoMax);
		document.getElementById(caixa).value = valorAtual.slice(0,casas)+separador+valorAtual.slice(casas,tamanhoMax+1);
		//event.keyCode = 0;
		return true;
	} else if (tecla == 9) {
		return true;
	}
	return false;
}
