// JavaScript Document
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
var auxmsg = '';
var arraux = new Array(6);
arraux[0]  = '';
arraux[1]  = '';    //linha para lista de itens (foto)
arraux[2]  = '';    //linha para lista de itens (pagamento)
arraux[3]  = '';    //linha para lista de itens (manual)
arraux[4]  = '';    //lista controle de menu e submenu
arraux[5]  = '';    //exclusao
arraux[6]  = '@1@'; //lista controle paginacao (Geral)
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function limparGlobais(){
	var tam = arraux.length;
	for(i=0; i<tam; i++){
		arraux[i] = '';	
	}
	arraux[6]  = '@1@';
	return true;
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
// url_encode version 1.0 
function url_encode(str) { 
	if(str == undefined){
		str = '';	
	}
	var hex_chars = "0123456789ABCDEF"; 
	var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
	var n, strCode, hex1, hex2, strEncode = ""; 
	
	for(n = 0; n < str.length; n++) { 
		if (noEncode.test(str.charAt(n))) { 
			strEncode += str.charAt(n); 
		} else { 
		strCode = str.charCodeAt(n); 
		hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
		hex2 = hex_chars.charAt(strCode % 16); 
		strEncode += "%" + (hex1 + hex2); 
		} 
	} 
	return strEncode; 
 } 
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 // url_decode version 1.0 
 function url_decode(str) { 
	  var n, strCode, strDecode = ""; 

	  for (n = 0; n < str.length; n++) { 
			if (str.charAt(n) == "%") { 
				 strCode = str.charAt(n + 1) + str.charAt(n + 2); 
				 strDecode += String.fromCharCode(parseInt(strCode, 16)); 
				 n += 2; 
			} else { 
				 strDecode += str.charAt(n); 
			} 
	  } 

	  return strDecode; 
 } 
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function posicionarLayer(id,w1,h1){
	var w = document.body.clientWidth;
	var h = document.body.clientHeight;
	var l = ((w-w1) / 2); 
	var t = ((h-h1) / 2);
	$("#"+id).css({left:l});
	$("#"+id).css({top:t});
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function gerLista(elem, op, idaux){
	var lst = arraux[idaux];
	expReg1 = eval('/@'+elem+'@/');
	aux = (lst.search(expReg1) == -1) ? true : false;
	if(op){ //Inserir
		if(aux){ //Não tem o elemento na lista (faz a inserção);
			arraux[idaux] = lst + '@' + elem + '@';
			return true;
		}else{ //Tem elemento (não faz a inserção);
			return false;
		}
	}else{ //Remover
		if(aux){ //Não tem elemento na lista (não faz a remoção);
			return false;
		}else{ //Tem elemento (faz a remoção);
			arraux[idaux] = lst.replace('@' + elem + '@','');
			return true; 
		}
	}
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function gerenciarArvoreMenu(indice, tipo, pag){
	var cam = $("#imgsubmenu"+indice).attr("cam");
	if(tipo == 'mod'){
		if($("#submenu"+indice+"").css("display") == 'block'){
			$("#submenu"+indice+"").css({display:"none"});
			$("#imgsubmenu"+indice+"").attr({src:cam+"seta_baixo.gif"});
		}else{
			$("#submenu"+indice+"").css({display:"block"});
			$("#imgsubmenu"+indice+"").attr({src:cam+"seta_cima.gif"});
		}
	}else{
		if($("#submenu"+indice+"").css("display") == 'block'){
			$("#submenu"+indice+"").css({display:"none"});
			$("#imgsubmenu"+indice+"").attr({src:cam+"mais.gif"});
		}else{
			$("#submenu"+indice+"").css({display:"block"});
			$("#imgsubmenu"+indice+"").attr({src:cam+"menos.gif"});
		}
	}
	//==============================
	var retorno = gerLista(indice, true, 4);
	if(retorno){
		strPost = "indice="+indice;
		ajax(pag, "submenucont"+indice, strPost, 0, true);	
	}
	limparGlobais();
	//==============================
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function ajax(pagina, elem, valor, opcao, carregar, tempo, pagina2){
	if(carregar){
		$("#"+elem).html('<font class="ftcarregando">Carregando...</font><img src="img/ampulheta.gif" width="16" height="16" align="absmiddle"/>');	
	}
	switch(opcao){
		case(0):
			el = elem;
		break;
		case(1):
			organizarAlert(pagina2,true);
			ajax(pagina, 'msg', valor, 0, false, '', '');
			setTimeout("ocultarLayer('alert','"+pagina2+"')",tempo);
		break;
	}
	if(opcao == 0){
		$.ajax({
			type: "POST",
			url : pagina,
			data: valor,
			success: function(retorno){
				if(el != ''){
					$("#"+el).html(retorno);	
				}
			}
		});
	}
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function organizarAlert(pag,op){
	document.body.scrollTop = 0;
	if(op){
		$("#conteudo").html('');	
		$("#alert").css({visibility:"visible"});
	}else{
		
	}
	$("#msg").html('Aguarde...');
	$("#alert_x").html("<img src='img/alert_fechar.gif' width='25' height='25' class='cursor' onClick='ocultarLayer(\"alert\",\""+pag+"\")'>");
	return true;
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function ocultarLayer(id,pag){
	if($("#"+id).css("visibility") == 'visible'){
		$("#"+id).css({visibility:"hidden"});
		if(pag != ''){
			ajax(pag, 'conteudo', '', 0, true, '','');
		}
	}
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function msgAlert(pag, msg){
	$("#msg").html(msg);
	$("#conteudo").html('');	
	$("#alert").css({visibility:"visible"});	
	setTimeout("ocultarLayer('alert','"+pag+"')",2500);
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function controlarAbas(id, quant){
	for(i=0; i<quant; i++){
		if(i == id){
			document.getElementById("aba_"+i).className = "abasel";
			$("#div_"+i+"").css({display:"block"});
		}else{
			document.getElementById("aba_"+i).className = "abades";
			$("#div_"+i+"").css({display:"none"});
		}
	}
	return true;
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function mudaCor(id,tipoclass){
	document.getElementById(id).className = tipoclass;
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function selecionarItemExcluir(id){
   var existe = gerLista(id, true, 5);
	if(!existe){
		gerLista(id, false, 5);
		$("#iex_"+id).attr({src:"img/excluir.gif"});		
	}else{
		$("#iex_"+id).attr({src:"img/excluir2.gif"});		
	}
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function checarIndExc(valor, id){
	if(arraux[5] == ''){
		$("#e"+id).html('&nbsp;Selecione ao menos um item para excluir');
		return false;
	}
	$("#e"+id).html('');
	return true;
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function controlePaginacao(numpag, totalpag, idcontrole, strPost, pag){
	var existe = gerLista(numpag, true, 6);
	if(existe){
		strPost = strPost + '&pag='+numpag;
		ajax(pag, 'div_pag_'+numpag, strPost, 0, true, '', '');
	}
	for(i = 1; i <=totalpag; i++){
		if(i == numpag){
			$("#div_pag_"+i).css({display:"block"});
		}else{
			$("#div_pag_"+i).css({display:"none"});
		}
	}		 
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function inserirLinha(comp, pag, quant, ext, op1, indice){
	if($("#lst"+comp+""+ext).attr("value") == undefined){
		arraux[indice] = '';	
	}else{
		arraux[indice] = $("#lst"+comp+""+ext).attr("value");
	}
	var qt  = (1 + stringToFloat($("#quant"+comp+""+ext).attr("value")));
	if(qt <= quant){
		$("#quant"+comp+""+ext).attr({value:qt});
		
		var ult = (1 + stringToFloat($("#ult"+comp+""+ext).attr("value")));
		$("#ult"+comp+""+ext).attr({value:ult});

		var td = document.createElement("td");
		td.id  = 'td'+ comp + '' + ult;
		var tr = document.createElement("tr");
		tr.id  = 'tr'+ comp + '' + ult;
		///td.setAttribute('colspan', '4');
		
		document.getElementById('tbody'+ comp + '' + ext).appendChild(tr);
		document.getElementById(tr.id).appendChild(td);
		
	
		var strPost = '';
		strPost = strPost + 'ult='+ult;	
		strPost = strPost + '&comp='+comp;	
		strPost = strPost + '&ext='+ext;	
		strPost = strPost + '&ind='+indice;	
		
		if(op1){
			listaLinhas(ult,indice);
		}
		ajax(pag, td.id, strPost, 0, true, '', '');
	}
	/**/
	$("#lst"+comp+""+ext).attr({value:arraux[indice]});
	/**/
	totalLinha(qt,'quant'+comp+""+ext);
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function listaLinhas(vl,indice){
   var existe = gerLista(vl, true, indice);
	if(!existe){
		gerLista(vl, false, indice);
	}
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function totalLinha(valor, id){
	if($("#"+id).attr("hab") == 'ok'){
		if(valor == '0'){
			$("#e"+id).html('Insira um arquivo');
			return false;	
		}else{
			$("#e"+id).html('');
			return true;
		}
	}else{
		return true;	
	}
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function removerLinha(pai, filho, ext, vl, op1, op2, indice, comp){
	if($("#op"+comp+""+ext).attr("value") == vl){
		$("#op"+comp+""+ext).attr({value:""});
	}
	if($("#lst"+comp+""+ext).attr("value") == undefined){
		arraux[indice] = '';	
	}else{
		arraux[indice] = $("#lst"+comp+""+ext).attr("value");
	}
	if(op1){
		var qt  = (stringToFloat($("#quant"+comp+""+ext).attr("value")) - 1);
		$("#quant"+comp+""+ext).attr({value:qt});
	}
	if(op2){
		listaLinhas(vl, indice);	
	}
	var epai = document.getElementById(pai);
	var efilho = document.getElementById(filho);
	epai.removeChild(efilho);
	/**/
	$("#lst"+comp+""+ext).attr({value:arraux[indice]});
	/**/
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function erroRadio(elem, id, idop){
	if(elem.checked == true){
		$("#"+idop).attr({value:elem.value});
		$("#"+id).html('');
	}
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function stringToFloat(x){
	if(x != ""){
		tmp = parseFloat(x.replace(",","."),2);
		return tmp;
	}else{
		return 0;
	}
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
//So para campos de arquivos com radio button.
function checarDestaque(form, opc){
	var doc = eval('document.'+form);
	var tam = doc.elements.length;
	var auxop = true;
	for(i=0; i<tam; i++){
		if((doc.elements[i].type == 'radio')&&(doc.elements[i].checked == true)){
			$("#"+opc).attr({value:doc.elements[i].value});
			auxop = false;
		}
	}
	if(auxop){
		$("#e"+opc).html("Selecione um arquivo para destaque");	
		return false;
	}
	return true;
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function checarOpcao(valor, id){
	if($("#"+id).attr("hab") == 'ok'){
		ext = id.split('_');
		if((valor == '')&&($("#quant_"+ext[1]).attr("value") != 0)){
			$("#e"+id).html("Selecione um arquivo para destaque");
			return false	
		}else{
			$("#e"+id).html("");		
			return true	
		}
	}else{
		return true;	
	}
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function doDate(pStr, pFmt){
	eval("reDate = reDate" + pFmt);
		if (reDate.test(pStr)) {
			return true;
		}else if (pStr != null && pStr != "") {
			return false
	}
} // doDate

var reDate1 = /^\d{1,2}\/\d{1,2}\/\d{1,4}$/;
var reDate2 = /^[0-3]?\d\/[01]?\d\/(\d{2}|\d{4})$/;
var reDate3 = /^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
var reDate4 = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
var reDate5 = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
var reDate  = reDate4;
var hora1   = /^([0-1]{1}[0-9]{1}|2[0-3]{1}):([0-5]{1}[0-9]{1})$/;
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function checarData(valor, id){
	if(valor.length > 10){
		var data = valor.split("/");
		var dia_dt = data[0];
		var mes_dt = data[1];
		var ano_dt = stringToFloat(data[2]);
		var parte1 = !(ano_dt % 4 == 0 && ano_dt % 100 != 0);
		var parte2 = !(ano_dt % 400 == 0);
		if((dia_dt == '29' ) && (mes_dt == '02') && parte1 && parte2){
			$("#e"+id).html("Data inválida");	
			return false;
		}
	}
	if(!doDate(valor, 4)){
		$("#e"+id).html("Data inválida");	
		return false;
	}
	$("#e"+id).html("");	
	return true;
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
function controlarThumbnail(id,img,e, op, larg, alt){
  thumb = document.getElementById(id);
  if(op){
	  if( thumb && thumb.style.visibility == 'hidden' )
	  {
			thumb.style.left       = e.pageX ? pageXOffset + e.clientX + 20 : document.body.scrollLeft + e.x + 20;
			thumb.style.top        = e.pageY ? pageYOffset + e.clientY : document.body.scrollTop  + e.y;
			thumb.style.visibility = 'visible';
			thumb.innerHTML        = '<img src="thumb.php?x='+ larg +'&y='+ alt +'&img='+ img +'">';
	  }
	}else{
		thumb.style.visibility = 'hidden';
		thumb.innerHTML        = '';
	}
}
//=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=





