// ########################################################################################
// ########################################################################################
	var isIE	= document.all;
	var isIE6	= isIE && document.implementation;
	var isIE7	= isIE && window.XMLHttpRequest && window.ActiveXObject;
// ########################################################################################
// ########################################################################################

// JavaScript Document
// exibe o erro na página em um alert
function error(msg, url, line){
	//return true;
	alert(
		"Erro na página \n\n"	+
		"Error:	" + msg + "\n"	+
		"URL:	" + url + "\n"	+
		"Line:	" + line
	);
}
window.onerror = error;
// ########################################################################################
// ########################################################################################

function voltar(){
	history.back();
}
// ########################################################################################
// ########################################################################################

function nd(){
}

// ########################################################################################
// ########################################################################################
// ++++++++++++++++ TRIM +++++++++++++++++++++++++++++
String.prototype.trim = function() {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

// ########################################################################################
// ########################################################################################
// ++++++++++++++++ MÁSCARA NÚMERO +++++++++++++++++++++++++++++
function mascaraNumero(evtKeyPress) {
	var nTecla = 0;
	if (document.all) {
		nTecla = evtKeyPress.keyCode;
	} else {
		nTecla = evtKeyPress.which;
	}
	if ((nTecla> 47 && nTecla <58)
		|| nTecla == 8 || nTecla == 127
		|| nTecla == 0 || nTecla == 9  // 0 == Tab
		|| nTecla == 13) { // 13 == Enter
		return true;
	} else {
		return false;
	}
} // <input type="text"         onkeyPress="javascript:return mascaraNumero(event);"       />
// ########################################################################################
// ########################################################################################
//atacha o flash em uma div
//                   1obr 2obr   3obr    4opc	 5opc	6opc   7opc   	8opc
function attachFlash(url, width, height, target, wmode, scale, quality, vars){
	var content; identity = url.split("."); identity = identity[0]; 
	if(!target) { document.write("<div id=\"id_"+ identity +"\"></div>"); target = "id_"+ identity;}
	if(!quality)quality	= "best";
	target = document.getElementById(target);
	content =  "<object id=\"" + identity + "\" width=\"" + width + "\" height=\"" + height + "\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\">";
    content += "<param name=\"allowScriptAccess\" value=\"sameDomain\" />";
	content +=  "<param name=\"movie\" value=\"" + url + "?" + vars +"\">";
    content +=  "<param name=\"quality\" value=\"" + quality + "\">";
	content +=  "<param name=\"wmode\" value=\"" + wmode + "\" /> ";
	content +=  "<param name=\"bgcolor\" value=\"#F2B775\" /> ";
    content +=  "<embed name=\"" + identity + "\" src=\"" + url + "?" + vars +"\"  width=\"" + width + "\" height=\"" + height + "\" wmode=\"" + wmode + "\" scale=\"" + scale + "\" quality=\"" + quality + "\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\"></embed>";
	content +=  "</object>";
	target.innerHTML = content;
}
// ########################################################################################
// ########################################################################################
//manda uma variavel no flash
function talkFlash(fla, func, arguments){
	if(window[fla])		window.document[fla].SetVariable("_root.func", func);
	if(document[fla])	document[fla].SetVariable("_root.func", func);
	
	if(window[fla]) 	window.document[fla].SetVariable(arguments, arguments);
	if(document[fla])	document[fla].SetVariable(arguments, arguments);
	
	if(window[fla])		window.document[fla].SetVariable("_root.flag", arguments);
	if(document[fla])	document[fla].SetVariable("_root.flag", arguments);
}
/*
_root.watch("flag", function() { 
    eval(this.func)(); 
}); 
*/
//controla a visibilidade dos submenus
// ########################################################################################
// ########################################################################################
function submenu(target, action){
	var target = parent.window.document.getElementById(target);
	if(target){
		if(action == "show") target.style.visibility = "visible";
		else target.style.visibility = "hidden";
	}
}
// ########################################################################################
// ########################################################################################
//SHOW HIDDEN ELEMENT
function showHide(field, modeIni){
	if (field.nodeName == "#text") field = field.nextSibling;
	
	if (field.nodeType != 1) var target = document.getElementById(field);
	else target = field;
	if (target){
		if (!target.style.display) target.style.display = modeIni;
		else if (target.style.display == "none") target.style.display = "block";
		else									 target.style.display = "none";
	}
}
// tools encontra-se na home
// ########################################################################################
// ########################################################################################
function tools(change){
	background = document.getElementById("TOOLS_BTS");
	_destaque2	= document.getElementById("destaque2");
	_busca 		= document.getElementById("TOOLS_busca");
	_dicionario	= document.getElementById("TOOLS_dicionario");
	_calendario	= document.getElementById("TOOLS_calendario");
	_enquete	= document.getElementById("TOOLS_enquete");
	
	_busca.style.display = 'none';
	_dicionario.style.display = 'none';
	_calendario.style.display = 'none';
	_enquete.style.display = 'none';
	
	if(change == 'busca'){
		background.style.backgroundPosition = "0px 0px";
		_destaque2.style.display = "block";
		_busca.style.display = 'block';
	}
	if(change == 'dicionario'){
		background.style.backgroundPosition = "0px -25px";
		_destaque2.style.display = "block";
		_dicionario.style.display = 'block';
	}
	if(change == 'calendario'){
		loadPage('_calendario_escolha.php','TOOLS_calendario','',false,true, '', false);
		background.style.backgroundPosition = "0px -50px";
		_calendario.style.backgroundImage = "url(\"img/home_tools_exp.gif\")";
		_destaque2.style.display = "none";
		_calendario.style.height = "273px";
		_calendario.style.display = 'block';
	}
	if(change == 'enquete'){
		background.style.backgroundPosition = "0px -75px";
		_enquete.style.backgroundImage = "url(\"img/home_tools_exp.gif\")";
		_destaque2.style.display = "none";
		_enquete.style.height = "273px";
		_enquete.style.display = 'block';
	}
}
// ########################################################################################
// ########################################################################################
function submeterEstadoDistribuidorInt(){
	field = document.getElementById("estado");
	if (field.value.length > 0) {
		if(field != "") loadPage('dis_listagem_int.php','interna','estado='+field.value,true,true, '', true);
	}
}
// ########################################################################################
// ########################################################################################
function submeterEstadoDistribuidor(){
	field = document.getElementById("estado");
	if (field.value.length > 0) {
		if(field != "") loadPage('dis_listagem_int.php','interna','estado='+field.value,true,true, '', true);
	}
}
// ########################################################################################
// ########################################################################################
function submeterAreaAutorInt(){
	area = document.getElementById("area").value;
	nome = document.getElementById("nome").value;
	loadPage('aut_listagem_int.php','interna','nome='+nome+'&id='+area,true,true, '', false);
}
// ########################################################################################
// ########################################################################################
function submeterAreaAutor(){
	area = document.getElementById("area").value;
	nome = document.getElementById("nome").value;
	loadPage('aut_listagem_nomes.php','interna_nomes','nome='+nome+'&id='+area,true,true, '', false);
}
// ########################################################################################
// ########################################################################################
function showSubMenu(id,show) {
	tid = document.getElementById(id);
	if (show == 0){
		tid.style.display = 'none';
	} else if (show == 1) {
		tid.style.display = 'block';
	}
}
// ########################################################################################
// ########################################################################################
function certeza(form, id){
	if(confirm("Atenção!\n\nVocê tem certeza que quer excluir este registro?")){  
		loadPage(form+'_process.php','col_edicao','exclusao=true&id='+ id +'',true,true, '', false);
		loadPage(form+'_lista.php','col_lista','metodo=data_update',false,true, '', false);
	} 
	else {   
		
	}   
}
// ########################################################################################
// ########################################################################################
function changeDesc(bt, vars,id){

	bto 	= document.getElementById(bt);
	
	if(bto1 && b1)
		b1.style.backgroundPosition = "0px 0px";
	if(bto1 && b2)
		b2.style.backgroundPosition = "0px 0px";
	if(bto3 && b3)
		b3.style.backgroundPosition = "0px 0px";
	if(bto4 && b4)
		b4.style.backgroundPosition = "0px 0px";
	
	if(bto){
		loadPage('catalogo_desc_'+ bt +'.php','painelDesc',vars,false,true, '', false, '110');
		bto.style.backgroundPosition = "0px -50px";
	}
}
// ########################################################################################
// ########################################################################################
function geraboxMsn(Obj, array_Day){
	msnString	= "<div class='eventosDia'><div class='calendarioDesc'>";
	array_Co	= "array_Co_" + array_Day;
	array_Day 	= "array_Day_" + array_Day;
	//alert(this[array_Co]);
	
	if(this[array_Co]){
		msnString += "<div class='eventoComemorativos' title='Data Comemorativa'> <span>Datas Comemorativas:</span>";
		for (i=0; i< this[array_Co].length; i++){ 
			msnString += "<div class='eventoComemorativo' title='Data Comemorativa'> "+ this[array_Co][i][1] +"</div>";
		}
		msnString += "</div>";
	}

	if(this[array_Day]){
		msnString += "<div class='eventoMarcados' title='Eventos Marcaados'> <span>Eventos Marcados:</span>";
		for (i=0; i< this[array_Day].length; i++){
			//msnString += "<div class='eventoHora'>"+ this[array_Day][i][1] +"</div>";
			msnString += "<div class='eventoTitle'> "+ this[array_Day][i][2] +"</div>";
		}
		msnString += "</div>";
	}

  	msnString += "</div><div class='calendarioDescFim'></div></div>";
	criaBoxMSN(Obj, msnString);
}
// ########################################################################################
// ########################################################################################
function check_cad() {
	if (document.getElementById('usuario_cadastrado_n').checked) {
		loadPage('cadastro.php','PAGE','',true,true);
	} else if (document.getElementById('usuario_cadastrado_s').checked) {
		document.forms['loja_login'].submeter_login.click();
	}
}
// ########################################################################################
// ########################################################################################
function exibeAjudaFP(forma_pagamento) {
	var result;
	var fechar;
	//fechar = '<div style="margin:10px 0 0 0;color:#000;"><a style="cursor:pointer;" onclick="javascript:fecharAjudaFP();">[ fechar ajuda ]</a></div>';
	result = retornaAjudaFP(forma_pagamento);
	document.getElementById('saiba_'+forma_pagamento).innerHTML = result;
}
// ########################################################################################
// ########################################################################################
function fecharAjudaFP() {
	document.body.style.opacity = 1;
	document.getElementById('ajuda_forma_pagamento').style.display   = "none";
	document.getElementById('ajuda_forma_pagamento').style.innerHTML = "";
}
// ########################################################################################
// ########################################################################################
function retornaAjudaFP(forma_pagamento) {
		var xmlhttp;
		try{
			xmlhttp = new XMLHttpRequest();
		}catch(ee){
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
					xmlhttp = false;
				}
			}
		}
		try{
			xmlhttp.open("GET", "ajuda_forma_pagamento.php?forma_pagamento="+forma_pagamento, false);
			xmlhttp.send(null);
			return xmlhttp.responseText;
		}catch(ex){throw ex;}
}
// ########################################################################################
// ########################################################################################
	function atualizaValoresLoja (forma_entrega) {
		document.getElementById('fechar_pedido').style.display  = 'none';
		document.getElementById('status_calculo').style.display = 'block';
		document.getElementById('erro_correios').style.display 	= 'none';
		document.getElementById('valor_frete').value = 0;
		atualizaElemetosLoja(false,'','','','');
		loadPage('obter_valor_frete.php','retorna_valores_loja','forma_entrega='+forma_entrega,false,false,false, false);
	}
// ########################################################################################
// ########################################################################################
	function atualizaElemetosLoja(atualizado,frete,valor_total) {
		if (atualizado == true) {
			window.parent.document.getElementById('carr_valfrete').innerHTML 		= 'R$&nbsp;'+frete;
			window.parent.document.getElementById('carr_valtotal').innerHTML 		= 'R$&nbsp;'+valor_total;
			window.parent.document.getElementById('valor_frete').value 				= frete;
			window.parent.document.getElementById('fechar_pedido').style.display 	= 'block';
			window.parent.document.getElementById('status_calculo').style.display 	= 'none';
			window.parent.document.getElementById('erro_correios').style.display 	= 'none';
		} else {
			window.parent.document.getElementById('carr_valfrete').innerHTML 		= 'Aguarde...';
			window.parent.document.getElementById('carr_valtotal').innerHTML 		= 'Aguarde...';
			window.parent.document.getElementById('fechar_pedido').style.display 	= 'none';
			window.parent.document.getElementById('erro_correios').style.display 	= 'none';
			window.parent.document.getElementById('status_calculo').style.display 	= 'block';
			window.parent.document.getElementById('valor_frete').value 				= 0;
		}
	}
// ########################################################################################
// ########################################################################################
	function erroCorreios(erro, servico, destino){
		window.parent.document.getElementById('carr_valfrete').innerHTML 		= 'Indisponível';
		window.parent.document.getElementById('carr_valtotal').innerHTML 		= 'Indisponível';
		window.parent.document.getElementById('fechar_pedido').style.display 	= 'none';
		window.parent.document.getElementById('status_calculo').style.display 	= 'none';
		window.parent.document.getElementById('erro_correios').innerHTML 	    = " Não é possível utilizar o serviço " + servico + ' para o local de entrega (CEP:' + destino + ').<br>'  + erro + ". Utilize outro serviço para entrega. ";
		window.parent.document.getElementById('erro_correios').style.display 	= 'block';
	}
	
	// ++++++++++++++++ MÁSCARA NÚMERO +++++++++++++++++++++++++++++
	function mascaraNumero(evtKeyPress) {
		var nTecla = 0;
		if (document.all) {
			nTecla = evtKeyPress.keyCode;
		} else {
			nTecla = evtKeyPress.which;
		}
		if ((nTecla> 47 && nTecla <58)
			|| nTecla == 8 || nTecla == 127
			|| nTecla == 0 || nTecla == 9  // 0 == Tab
			|| nTecla == 13) { // 13 == Enter
			return true;
		} else {
			return false;
		}
	} // <input type="text"         onkeyPress="javascript:return mascaraNumero(event);"       />	
// ########################################################################################
// ########################################################################################
	function atualizaFotoEvento(fotoEvento,descricao) {
		document.getElementById('foto_evento').src 		= 'img/fotos/'+fotoEvento+'.jpg';
		document.getElementById('foto_evento').alt 		= descricao;
		document.getElementById('foto_evento').title 	= descricao;
	}
// ########################################################################################
// ########################################################################################
	function downloadFile(tipo,arquivo,novo_nome,ext,id){
		iframe_style = '';
		iframe_style = "width:0;height:0;color:#000;border:0;font-size:0;background-color:0;position:absolute;bottom:0;right:0;"
		iframedown = "<iframe src=\"download.php?tipo="+tipo+"&ext="+ext+"&novo_nome="+novo_nome+"&arquivo="+arquivo+"\" name=\"\" id=\"\" style=\""+iframe_style+"\">";
		document.getElementById(id).innerHTML = iframedown;
		setTimeout("document.getElementById('"+id+"').innerHTML = '';",1000);
	}
// ########################################################################################
// ########################################################################################
	function exibirPesquisa(){
		imagem	 = document.getElementById('img_pesquisa');
		elemento = document.getElementById('grupo_pesquisa');
		if (elemento.style.display == 'none') {
			elemento.style.display 	= 'block';
			imagem.src				= '../img/reprimir.jpg';
		} else {
			elemento.style.display 	= 'none';
			imagem.src				= '../img/expandir.jpg';
		}
	}
// ########################################################################################
// ########################################################################################
	function currencyFormat(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 == 8 || whichCode == 0 ) {
			return true;  // Enter
		}
		key = String.fromCharCode(whichCode);  // Get key value from key code
		if (strCheck.indexOf(key) == -1) {
			return false;  // Not a valid key
		}
		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 = '';
		}
		else if (len == 1) {
			fld.value = '0'+ decSep + '0' + aux;
		}
		else if (len == 2) {
			fld.value = '0'+ decSep + aux;
		}
		else 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 weightFormat(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 == 8 || whichCode == 0 ) {
			return true;  // Enter
		}
		key = String.fromCharCode(whichCode);  // Get key value from key code
		if (strCheck.indexOf(key) == -1) {
			return false;  // Not a valid key
		}
		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 = '';
		}
		else if (len == 1) {
			fld.value = '0'+ decSep + '00' + aux;
		}
		else if (len == 2) {
			fld.value = '0'+ decSep + '0' + aux;
		}
		else if (len == 3) {
			fld.value = '0'+ decSep + aux;
		}
		else if (len > 3) {
			aux2 = '';
			for (j = 0, i = len - 4; i >= 0; i--) {
				if (j == 4) {
					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 - 3, len);
		}
		return false;
	}
// ########################################################################################
// ########################################################################################
	function showIconBoleto(xParcelas){
		xParcelas = parseInt(xParcelas);
		if (xParcelas > 1) {
			document.getElementById('icon_pag_4').style.display = 'none';
		} else {
			document.getElementById('icon_pag_4').style.display = 'block';
		}
	}
// ########################################################################################
// ########################################################################################
	function captura_boleto(id_pedido, exibe){
		elemento = document.getElementById('captura_boleto');
		if(exibe) {
			elemento.style.display = 'block';
			loadPage('captura_boleto.php', 'captura_boleto', 'id_pedido='+id_pedido, false, false);
		} else {
			elemento.style.display = 'none';
			elemento.innerHTML = '';
		}
	}
// ########################################################################################
// ########################################################################################
function buscaUserAdm(){
	nome = document.getElementById("nome").value;
	loadPage('usu_lista.php','col_lista','metodo2=busca&nome='+nome,true,true, '', false);
}
// ########################################################################################
// ########################################################################################
function baixarBase(){
	document.getElementById('targetFrames').src = "usu_download.php";
}
// ########################################################################################
// ########################################################################################
// ########################################################################################
// ########################################################################################
function remover_cap(c){
	s = document.getElementById("suma");
	cc = document.getElementById("cria_"+c);
	d = document.getElementById("cap_"+c);
	//b = document.getElementById("btR_"+c);
	if(cc){
		//cc.style.display= 'none';
		//cc.innerHTML = "";
	}
	d.style.display= 'none';
	d.innerHTML = "";
	//s.removeChild(d);
}
// ########################################################################################
// ########################################################################################
function adicionar_cap(s, v){
	capNUM++;
	
	cria = document.getElementById("cria_"+s);
	c = document.getElementById("btC_"+s);
	c.style.display= 'none';
	
	ncont = "<h5 class=\"h2M\" id=\"btC_" + capNUM + "\" style=\" clear:both; text-align:right;  float:left; width:358px; margin-top:0px; margin-bottom:10px;\"> <a href=\"javascript:adicionar_cap('" + capNUM + "');\" >Adicionar Capítulo</a></h5>";
  ncont += "<div id=\"cria_" + capNUM + "\"></div>";
	ncont +="<div id=\"cap_" + capNUM + "\">"; 
	ncont +="<div>"; 
	ncont +="<label for=\"capitulo_" + capNUM + "\">Capítulo:</label>"; 
	ncont +="<input value=\" \" name=\"capitulo_" + capNUM + "\" type=\"text\" id=\"capitulo_" + capNUM + "\" maxlength=\"200\" >"; 
	ncont +="</div>"; 
	ncont +="<div>"; 
	ncont +="<label for=\"descricao_" + capNUM + "\">Descrição:</label>"; 
	ncont +="<textarea name=\"descricao_" + capNUM + "\" style=\" width:351px; height:30px;\" id=\"descricao_" + capNUM + "\"></textarea>"; 
	ncont +="</div>"; 
	ncont +="<h5 class=\"h2M\" id=\"add2\" style=\" clear:both; text-align:right;  float:left; width:358px; margin-top:0px; margin-bottom:5px;\"><a href=\"javascript:remover_cap('" + capNUM + "');\">Remover Capítulo</a></h5>"; 
	if(v != 'sim'){
		ncont += "<div id=\"cria_" + capNUM+1 + "\"></div>";
		ncont += "<h5 class=\"h2M\" id=\"btC_" + capNUM+1 + "\" style=\" clear:both; text-align:right;  float:left; width:358px; margin-top:0px; margin-bottom:10px;\"> <a href=\"javascript:adicionar_cap('" + capNUM+1 + "');\" >Adicionar Capítulo</a></h5>";
		capNUM++;
	}
	ncont +="</div>"; 
	//alert(ncont);
	cria.innerHTML = ncont;
	/**/
}
// ########################################################################################
// ########################################################################################
function sucess(id){
	html('col_erro1','<p class=\'sucesso\'>Imagem enviada com sucesso!</p>');
	//pag('col_list_images','portfolio_form_imagens_list.php', id);
	loadPage('livros_capitulo_form.php', 'col_edicao', 'id='+id+'&sucess=true', false, true, false, false);
}
function failed1(){
	show('bt_up');
	html('col_erro1','<p class=\'erro\'>Imagem enviada não é jpg ou vídeo flv!</p>');
}
function failed2(){
	show('bt_up');
	html('col_erro1','<p class=\'erro\'>Imagem não pode ser enviada!</p>');
}
function failed3(id){
	show('bt_up');
	html('col_erro1','<p class=\'erro\'>O Vídeo é muito grande! <br>Envie pelo ftp na pasta img/gallery/ com o nome mov_'+ id +'.flv </p>');
}
function failed4(id){
	show('bt_up');
	html('col_erro1','<p class=\'erro\'>A imagem é muito grande! <br>Envie pelo ftp em files/livros/'+ id +'/capitulo/ seu arquivo </p>');
}

// exibe um objeto
function html(obj, txt){
	obj = document.getElementById(obj);
	if(obj){
			obj.innerHTML = txt;
	}
}

// exibe um objeto
function show(obj){
	obj = document.getElementById(obj);
	if(obj){
			obj.style.display = 'block';
	}
}