// JavaScript Document
function caixaAlta(obj) {
    obj.value = obj.value.toUpperCase();
}

function divMinMax(id) {
	var imagem = gE("img_"+id);
	
	if (!gE(id).style.display) {
		gE(id).style.display = 'none';
		imagem.src = "../../cummon/images/mais.gif"
	}
	else {
		gE(id).style.display = '';
		imagem.src = "../../cummon/images/menos.gif"
	}
}

function centDiv(id) {
	var div = document.getElementById(id);

	var altTela = Math.floor(document.body.offsetHeight);
	var largTela =  Math.floor(document.body.offsetWidth);
	
	div.style.left = ((largTela/2)-(div.offsetWidth/2))+"px";
	
	var vScrollY = 0;
        
	if (document.all){
		if (!document.documentElement.scrollTop){
			vScrollY = document.body.scrollTop;
		}else{
			vScrollY = document.documentElement.scrollTop;
		}
	}else{
		vScrollY = window.pageYOffset;
	}
	if (window.innerHeight) {
		altura = window.innerHeight;
	} else if (window.document.body.clientHeight) {
		altura = window.document.body.clientHeight;
	} else {
		altura = Math.floor(document.body.offsetHeight);
	}
	div.style.top=((altura/2) + vScrollY - (parseInt(div.offsetHeight)/2))+"px";
}

//Mascara para os campos
//    * "#" - Numeros
//    * "A" - Letras UpperCase
//    * "a" - Letras LowerCase
//    * "Z" - Letras
//    * "*" - Qualquer Caracter
//    * "/", ".", "-", " ", ":" - Caracteres Fixos
//=============================================================================================================
function Mascara(objeto, evt, mask) {
 
var LetrasU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var LetrasL = 'abcdefghijklmnopqrstuvwxyz';
var Letras  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var Numeros = '0123456789';
var Fixos  = '().-:/ ';
var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";

evt = (evt) ? evt : (window.event) ? window.event : "";
var value = objeto.value;
if (evt) {
 var ntecla = (evt.which) ? evt.which : evt.keyCode;
 tecla = Charset.substr(ntecla - 32, 1);
 if (ntecla < 32) return true;

 var tamanho = value.length;
 if (tamanho >= mask.length) return false;

 var pos = mask.substr(tamanho,1);
 while (Fixos.indexOf(pos) != -1) {
  value += pos;
  tamanho = value.length;
  if (tamanho >= mask.length) return false;
  pos = mask.substr(tamanho,1);
 }

 switch (pos) {
   case '#' : if (Numeros.indexOf(tecla) == -1) return false; break;
   case 'A' : if (LetrasU.indexOf(tecla) == -1) return false; break;
   case 'a' : if (LetrasL.indexOf(tecla) == -1) return false; break;
   case 'Z' : if (Letras.indexOf(tecla) == -1) return false; break;
   case '*' : objeto.value = value; return true; break;
   default : return false; break;
 }
}
objeto.value = value;
return true;
}

//-------------------Funções MÁSCARA--------------------
function FormataReais(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13 || whichCode == 0 || whichCode == 8) return true;
	
	key = String.fromCharCode(whichCode);  // Valor para o código da Chave
	if (strCheck.indexOf(key) == -1) return false;  // Chave inválida
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) 
	break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt
	(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--) {
		if (j == 3) {
			aux2 += milSep;
			j = 0;
		}
		aux2 += aux.charAt(i);
		j++;
	}
	fld.value = '';
	len2 = aux2.length;
	for (i = len2 - 1; i >= 0; i--)
	fld.value += aux2.charAt(i);
	fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}

function findPos(obj) {
	var curleft = 0;
	var curtop = 0;
	
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = 0;
	winprops = 'status=false,resizable=yes,height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function Limpar(valor, validos) {
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += aux;
		}
	}
	return result;
}

//Checa se a palavra contem espaco e retorna true ou false, by Emanuel
function check_space(str) {
	for (i = 0; i < str.length; i++) {
		if (str.substring(i, (i+1)) == " ") {
			return false;
		}
	}
	return true;
}

//Checa se a palavra contem letra e retorna false se encontrar, by Emanuel
function is_numeric(input) {
	var chars = "abcdefghyjklmnopqrstuvwxyz";
	input = input.toLowerCase();
	for(i = 0; i < input.length; i++){
		if (chars.indexOf(input.charAt(i),0) != -1){
			return false;
		}
	}
	return true;
}

//////////////// JQuery functions //////////////
function closeMsg(div) {
	mensagem = $('#'+div);
	//clearTimeout(mensagem);
	setTimeout("mensagem.fadeOut('slow');", 5000);
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

function habilitaCampo(tipo) {
	return $(tipo).attr( { disabled: false } );
}

function validarChamado(titulo, msg, incidente, email, nome, texto, categoria) {

	var msgs = $('#'+msg);
	if ((!trim(titulo)) || (!incidente) || (!trim(email)) || (!trim(texto)) || (!categoria)) {
		msgs.addClass("warning").html("Os campos com (*) são necessários.").show();
		closeMsg(msg);
		return false;
	} else {
		return true;
	}
}
function mudaCateg(id, divObj, pagina, css, nome) {
	var div = $('#'+divObj);
	div.addClass(css).html("");
	$.post(pagina, nome+'='+id,
	function(retorno) {
		div.removeClass().html(retorno).show();
	});
}

// Marca e desmarca todas as checkbox
function checkAllBoxes(isto) {
	if ($(isto).attr("checked")==true) {
		$(":checkbox").attr("checked", true);
	}else {
		$(":checkbox").attr("checked", false);
	}
}

//controlaDetalhes e detailControl para tooltip
function controlaDetalhes(id, e, acao) {
	var pagX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	var pagY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	
	// Mostra a div
	if (acao == 0) {
		controle[id] = false;
		$('#divMiniDetalhes_'+id).hide();
	} else if (acao == 1) {
		controle[id] = true;
		detailControl(id, e, pagX, pagY, acao);
		
	}
}

function detailControl(id, e, pagX, pagY, acao) {
	var my_tooltip = $("#divMiniDetalhes_"+id);
	var border_top = $(window).scrollTop(); 
	var border_right = $(window).width();
	
	var left_pos;
	var top_pos;
	var offset = 20;

	pagX = (pagX) ? pagX : e.pageX; 
	pagY = (pagY) ? pagY : e.clientY;

	//my_tooltip.removeClass("load01").html(retorno).hide();

	if(border_right - (offset *2) >= my_tooltip.width() + pagX){
		left_pos = pagX +offset;
		} else{
		left_pos = border_right-my_tooltip.width()-offset;
		}
		
	if(border_top + (offset *2)>= pagY - my_tooltip.height()){
		top_pos = border_top +offset;
		} else{
		top_pos = pagY-my_tooltip.height()-offset;
		}
	my_tooltip.css({left:left_pos, top:top_pos}).show();
}
