var sombra_visivel = false;

//Função que controla o efeito de troca de estilo do menu principal esquerdo
function estiloMenu(obj, estilo){
	if(typeof document.getElementById(obj) != "undefined"){
		document.getElementById(obj).className = estilo;
	}
}

//Função utilizada para paginação
function definePagina(arquivo, pag, querystring){
	document.location.href = arquivo + "?npag=" + pag + "&" + querystring + "#resultados";
}

//Função que cria uma linha em branco nas tabelas
function quebra(colspan, altura, tipo, texto){
	switch(tipo){
		
		//Cria um espaço em branco entre as linhas
		case "vazio":
			document.write("<tr>");
			  document.write("<td colspan=\"" + colspan + "\" height=\"" + altura + "\" style=\"padding:0px;margin:0px;\"><img name=\"espaco\" width=\"1\" height=\"1\"></td>");
			document.write("</tr>");
		break;
		
		//Cria um espaço em branco seguido por um título entre as linhas
		case "titulo":
			document.write("<tr>");
			  document.write("<td colspan=\"" + colspan + "\" height=\"" + altura + "\" style=\"padding:0px;margin:0px;\"><img name=\"espaco\" width=\"1\" height=\"1\"></td>");
			document.write("</tr>");
			
			document.write("<tr>");
			  document.write("<td colspan=\"" + colspan + "\" class=\"titulo_divisao\">" + texto + "</td>");
			document.write("</tr>");
		break;
		
		//Cria um espaço em branco com uma linha horizontal entre as linhas
		case "separacao":
			document.write("<tr>");
			  document.write("<td colspan=\"" + colspan + "\" height=\"" + altura + "\" style=\"padding:0px;margin:0px;\"><img name=\"espaco\" width=\"1\" height=\"1\"></td>");
			document.write("</tr>");
			
			document.write("<tr>");
			  document.write("<td colspan=\"" + colspan + "\" bgcolor=\"#CCCCCC\" height=\"1\" style=\"padding:0px;margin:0px;\"><img name=\"espaco\" width=\"1\" height=\"1\"></td>");
			document.write("</tr>");
			
			document.write("<tr>");
			  document.write("<td colspan=\"" + colspan + "\" height=\"" + altura + "\" style=\"padding:0px;margin:0px;\"><img name=\"espaco\" width=\"1\" height=\"1\"></td>");
			document.write("</tr>");
		break;
	
	}
}

function expande_secao(aObjetos){
	if(typeof(aObjetos) == 'string'){
		 aObjetos = new Array(aObjetos);
	}
 
	for(var i=0;i<aObjetos.length;i++){
		with( document.getElementById(aObjetos[i]).style ){
		   overflow   = (overflow	== '' || overflow	== 'visible')	? 'hidden' : 'visible';
		   height     = (height		== '' || height		!= '1px')		? '1px'    : 'auto'; 
		   visibility = (visibility	== '' || visibility	== 'visible')	? 'hidden' : 'visible';
		}
	}
}

function carregando(){
	with( document.getElementById('carregando') ){
		var iTopo	= document.body.scrollTop;
		var iLimite = 100;
		
		style.visibility = 'visible';
		style.top		 = (iTopo < iLimite) ? String(iLimite) + 'px' : String(iTopo + 20) + 'px';
	}
}

function descarregando(){
	document.getElementById('carregando').style.visibility = 'hidden';
}

function viewCesta(estado){
	var altura;
	altura = (estado == 'hidden') ? '1px' : 'auto';
	document.getElementById('cesta').style.overflow = estado;
	document.getElementById('cesta').style.height = altura
	document.getElementById('cesta').style.visibility = estado;
}

function ExecutaPesquisa(texto){
	texto = texto.replace(/(\s+$)|(^\s+)/g, '');
	
	if (texto.length > 0){
		xajax_ExecutaPesquisa(texto);
	}else{
		descarregando();
	}
}


/**
* Atribui as configurações passadas a um determinado objeto.
* @param sId			{Element}	id do objeto em questão
* @param oAtributos		{Object}	atributos e valores que devem ser setados
* @return 				{}			nada
*/
function setAtributos(sId, oAtributos){
	for(var i=0;i<oAtributos.length;i++){
		with(oAtributos[i]){
			eval('xajax.$(sId).' + sAttr + ' = \'' + sValue + '\';');
		}
	}
}

/**
* Cria a transparência que divide parte principal e janela de diálogo.
* @param bCriar			{Boolean}	determina se o objeto será exibido ou não
* @param oContainer		{Object}	caso o navegador não seja IE, informe qual é objeto mestre da página (o tamanho da sombra se baseará nele)
* @return				{}			nada
* @see					#setAtributos
*/
function sombra(){
	var bCriar = (arguments[0]<=0) ? false : true;
	var sId	   = 'sombra';
	
	if(bCriar){
		if(sombra_visivel==false){ // nao colocar sombra sobre sombra
			var oContainer = (arguments[1] && arguments[1]!='') ? xajax.$(arguments[1]) : document.body;
			var iHeight = (navigator.userAgent.indexOf('MSIE') > -1) ? document.body.scrollHeight : new Number(oContainer.scrollHeight) + 'px';
			
			xajax.create('container', 'div', sId);
			this.setAtributos(sId, [{'sAttr' : 'style.height', 'sValue' : iHeight}]);
			sombra_visivel = true;
		}
	}else{
		xajax.remove(sId);
		sombra_visivel = false;
	}

	antiSelect(bCriar);
}

function enviarAmigo(){
	if(validaform.validate('frmAmigo')){
		xajax_emailAmigo("enviar", get_rotulos('frmAmigo'), xajax.getFormValues('frmAmigo'));
	}
	return false;
}

function get_rotulos(frm){
	var sRotulos   = new String();
	var sConcatena = new String();
	
	sRotulos = '<xjxquery><q>';
	
	with(document.forms[frm]){
		for(i=0;i<length;i++){
			sRotulos   += sConcatena + elements[i].name + '=' + encodeURIComponent(elements[i].title);
			sConcatena  = '&';
		}
	}
	sRotulos += '</q></xjxquery>';
	return sRotulos;
}

function faleConosco(){
	if(validaform.validate('frFaleConosco')){
		xajax_faleConosco(get_rotulos('frFaleConosco'), xajax.getFormValues('frFaleConosco'));
	}
	return false;
}

/**
* Altera a visibilidade de todos os SELECT da pagina
* @param acao		{Boolean}	TRUE = invisivel / FALSE = visivel
*/
function antiSelect(acao){
	acao = (acao == false) ? "visible" : "hidden";
	combos = document.getElementsByTagName('select');
	for(i = 0; i < combos.length; i++) combos[i].style.visibility = acao;
}

function copiarDados(){
	document.getElementById('f2_endereco').value 		= document.getElementById('f_endereco').value;
	document.getElementById('f2_numero').value 			= document.getElementById('f_numero').value;
	document.getElementById('f2_complemento').value 	= document.getElementById('f_complemento').value;
	document.getElementById('f2_bairro').value 			= document.getElementById('f_bairro').value;
	document.getElementById('f2_cidade').value 			= document.getElementById('f_cidade').value;
	document.getElementById('f2_referencia').value 		= document.getElementById('f_referencia').value;
	document.getElementById('f2_cep').value 			= document.getElementById('f_cep').value;
	document.getElementById('f2_telefone').value 		= document.getElementById('f_telefone').value;
	document.getElementById('f2_fax').value 			= document.getElementById('f_fax').value;
	document.getElementById('f2_estado').value 			= document.getElementById('f_estado').value;
}

function cadastroCliente(){
	with(document.frmCliente){
		validaform.criaComparacao(f_senha2, f_senha1.value, 'DIFERENTE');
		if(validaform.validate('frmCliente')){
			xajax_cadastroCliente(xajax.getFormValues('frmCliente'));
		}
	}
	return false;
}

function addItem(codtamcor, qtd){	
	if (codtamcor != ""){
		carregando();
		xajax_addItem(codtamcor, qtd);
	}else{
		alert("É necessário selecionar cor e tamanho do produto!");
	}
}

function alteraQtd(indice, codigo){
	var i = 0;
	carregando();
	while(document.getElementById('qtd_' + i)){
		document.getElementById('qtd_' + i).disabled = true;
		i++;
	}
	xajax_alteraQtd(codigo, document.getElementById('qtd_' + indice).value);
}

function habilitaQtd(){
	var i = 0;
	while(document.getElementById('qtd_' + i)){
		document.getElementById('qtd_' + i).disabled = false;
		i++;
	}
	descarregando();	
	//xajax_alteraQtd(codigo, document.getElementById('qtd_' + indice).value);
}


function fechaCesta(){
	viewCesta("hidden");
	sombra(0);
}

function excluirCestaItem(codtamcor){
	carregando();
	xajax_excluirCestaItem(codtamcor);
}

function login(acao, email, senha){
	var dados = new Array();
	dados['f_email'] = email;
	dados['f_senha'] = senha;
	
	xajax_login('logar', dados);
}

function novaJanela(pagina){
	WindowObjectReference = window.open(pagina, "Megabikers", "menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes,height=360,width=450");
}

function ajustaBotaoJS(objeto){
	var estoque = objeto.options[objeto.selectedIndex].getAttribute('mb:estoque') || 0;
	var botao = "";
	
	if (estoque > 0){
		botao = "<a href='#' onclick=\"addItem(document.getElementById('codtamcor').value);\"  class=comprar><img src='images/comprar.gif' align='absmiddle'>Comprar</a>"
	}else{
		botao = "<a href='#' onclick=\"addItem(document.getElementById('codtamcor').value);\"  class=comprar2><img src='images/comprar.gif' align='absmiddle'>Avisar disponibilidade</a><br>"
	}
	
	document.getElementById('botao_comprar').innerHTML = botao;
}

function finalizarCartao(){
	if(validaform.validate('frmCredito')){
		xajax_commit(document.getElementById("forma").value, xajax.getFormValues('frmCredito'));
	}else{
		descarregando();
	}
}

function finalizarCartaoMaster(){
	if(validaform.validate("frmCreditoMaster")){
		xajax_commit(document.getElementById("forma").value, xajax.getFormValues("frmCreditoMaster"));
	}else{
		descarregando();
	}
}



function finalizarBoleto(codforma){
	xajax_commit(codforma);
}

function dadospedidos(linha, codvenda){
	var i = 0;
	
	while(document.getElementById('dadospedido'+i)){
		document.getElementById('dadospedido'+i).innerHTML		= '';
		document.getElementById('dadospedido'+i).style.height	= "3px";
		document.getElementById('dadospedido'+i).style.overflow	= "hidden";
		i ++;
	}
	
	xajax_itensPedidosAntigos(linha, codvenda);
}

function ajuda(){
	var sDisplay = (arguments[0] && arguments[0] > 0) ? 'block' : 'none';
	xajax.$('janela-ajuda').style.display = sDisplay;
}