function ECOM__trim(__str) 
{
	return __str.replace(/^\s+|\s+$/g,"");
}

function ECOM__add_item(__codItem)
{
	document.getElementById('spn_clk_'+__codItem).style.display = 'none';
	document.getElementById('spn_qtd_'+__codItem).style.display = '';
}

function ECOM__comprar_item(__codItem, __tipoItem)
{
	__qtdItem = document.getElementById('qtd_'+__codItem).value;
	__params = 'acao=add&ids='+document.getElementById('tmpid').value+
	           '&tip='+__tipoItem+'&cod='+__codItem+'&qtd='+__qtdItem;
	__url = 'carrinho.ecom.php';
	postText(__url, __params, ECOM__Eval_ecomm);
	
	ECOM__cancel_add_item(__codItem);
}

function ECOM__cancel_add_item(__codItem)
{
	document.getElementById('spn_clk_'+__codItem).style.display = '';
	document.getElementById('spn_qtd_'+__codItem).style.display = 'none';	
}

function ECOM__mostrar_carrinho()
{	
	__params = 'acao=mostrar&ids='+document.getElementById('tmpid').value;
	
	if ( document.getElementById('xyz')!=null )
	{
		if ( document.getElementById('xyz').value!='' )
		{
			__params += '&xyz='+document.getElementById('xyz').value;
		}
	}
	
	__url = 'carrinho.ecom.php';
	effectTransDiv(__url, __params);
}

function ECOM__retorno_pgto()
{
	__params = 'acao=exibir_feedback';
	__url = 'carrinho.ecom.php';
	effectTransDiv(__url, __params);
}

function ECOM__apagar_item(__codItem, __tipoItem)
{
	__params = 'acao=sub&ids='+document.getElementById('tmpid').value+
	           '&tip='+__tipoItem+'&cod='+__codItem;
	__url = 'carrinho.ecom.php';
	postText(__url, __params, retDivFlutuante);
}

function ECOM__atualizar_item(__codItem, __tipoItem)
{
	__qtdItem = document.getElementById('qtd_item_'+__tipoItem+''+__codItem).value;
	
	__params = 'acao=upd&ids='+document.getElementById('tmpid').value+
			   '&tip='+__tipoItem+'&cod='+__codItem+'&qtd='+__qtdItem;
	__url = 'carrinho.ecom.php';
	postText(__url, __params, retDivFlutuante);		
}

function ECOM__finalizar_carrinho()
{
	__params = 'acao=fin&ids='+document.getElementById('tmpid').value;
	__url = 'carrinho.ecom.php';
	postText(__url, __params, retDivFlutuante);
}

function ECOM__mostrar_btn(__tip)
{
	if ( __tip=='CAD_SEM_ATIVACAO' )
	{
		//LOGAR
		document.getElementById('entrar_ecomm').style.display = '';
		document.getElementById('novocli_ecomm').style.display = '';
		//ATIVACAO
		document.getElementById('reenviar_ecomm').style.display = '';
		//CADASTRO		
		document.getElementById('cadastrar_ecomm').style.display = 'none';
		document.getElementById('retentar_ecomm').style.display = 'none';
		document.getElementById('tr_ecom_nom').style.display = 'none';
		document.getElementById('tr_ecom_cpf').style.display = 'none';
		document.getElementById('tr_ecom_tel').style.display = 'none';		
		document.getElementById('tr_ecom_sex').style.display = 'none';
		document.getElementById('spn_ecom_sen').style.display = 'none';		
	}
	else if ( __tip=='USR_INEXISTENTE' )
	{
		//LOGAR
		document.getElementById('entrar_ecomm').style.display = 'none';
		document.getElementById('novocli_ecomm').style.display = 'none';		
		//ATIVACAO
		document.getElementById('reenviar_ecomm').style.display = 'none';		
		//CADASTRO
		document.getElementById('cadastrar_ecomm').style.display = '';
		document.getElementById('retentar_ecomm').style.display = '';
		document.getElementById('tr_ecom_nom').style.display = '';
		document.getElementById('tr_ecom_cpf').style.display = '';
		document.getElementById('tr_ecom_tel').style.display = '';				
		document.getElementById('tr_ecom_sex').style.display = '';
		document.getElementById('spn_ecom_sen').style.display = '';		
	}
}

function ECOM__entrar_ecommerce(__tela)
{
	__ema = document.getElementById('ecom_email').value;
	__pwd = document.getElementById('ecom_senha').value;

	if ( ECOM__trim(__ema)=='' )
	{
		alert('Por favor preencha o campo E-MAIL');
		return false;		
	}
	else if ( ECOM__trim(__pwd)=='' )
	{
		alert('Por favor preencha o campo SENHA');
		return false;
	}

	__params = 'acao=validar_acesso&ids='+document.getElementById('tmpid').value+
			   '&email='+__ema+'&senha='+__pwd+'&tela='+__tela;
	__url = 'carrinho.ecom.php';
//	postTextFull(__url, __params, ret_entrar_ecomm, __tela);

	postText(__url, __params, ECOM__Eval_ecomm);	
	
	ECOM__processo('ini');
}

function ECOM__ret_entrar_ecomm(resp, __tela)
{
	if ( __tela=='transdiv' )
	{
		retDivFlutuante(resp);	
	}
	else if ( __tela=='tela' )
	{
		ECOM__Eval_ecomm(resp)	
	}
}

function ECOM__retentar_ecommerce()
{
	ECOM__finalizar_carrinho();
}

function ECOM__reenviar_ecommerce()
{
}

function ECOM__reenviar_ativacao(__codcli, __email)
{
	__params = 'acao=reenviar_ativacao'+
			   '&email='+__email+'&cliente='+__codcli;
	__url = 'carrinho.ecom.php';
	postText(__url, __params, ECOM__Eval_ecomm);
}

function ECOM__pagar_carrinho()
{		
	//TODO: IR PARA CIELO
	
	__params = 'acao=pagar&ids='+document.getElementById('tmpid').value;
	__url = 'ecom/pagamento.ecom.php';
	postText(__url, __params, retDivFlutuante);		
}

function ECOM__cadastrar_ecommerce()
{
	__nom = document.getElementById('ecom_nome');
	__cpf = document.getElementById('ecom_cpf');
	__tel = document.getElementById('ecom_tel');		
	__sex = ( document.getElementById('ecom_sexo_m').checked )? 'M' : 'F';
	__ema = document.getElementById('ecom_email');	
	__pwd = document.getElementById('ecom_senha');
	__rpw = document.getElementById('ecom_resenha');

	if ( ECOM__trim(__nom.value)=='' )
	{
		alert('Por favor preencha o campo NOME COMPLETO.');
		__nom.focus();
	}
	else if ( (__nom.value.split(" ").length)<2 )
	{
		alert('Por favor preencha corretamente o campo NOME COMPLETO.\nEx.: Maria da Silva');
		__nom.focus();
	}
	else if ( ECOM__trim(__ema.value)=='' )
	{
		alert('Por favor preencha o campo E-MAIL.');
		__ema.focus();
	}
	else if ( ECOM__trim(__cpf.value)=='' )
	{
		alert('Por favor preencha o campo CPF.');
		__cpf.focus();
	}	
	else if ( !(chk_cpf(__cpf, 'S')) )//CPF INVALIDO
	{
		alert('CPF Inválido!');
		__cpf.focus();
	}	
	else if ( ECOM__trim(__tel.value)=='' )
	{
		alert('Por favor preencha o campo TELEFONE.');
		__tel.focus();
	}	
	else if ( ECOM__trim(__pwd.value)=='' )
	{
		alert('Por favor preencha o campo SENHA.');
		__pwd.focus();
	}
	else if ( ECOM__trim(__rpw.value)=='' )
	{
		alert('Por favor preencha o campo REDIGITE A SENHA.');
		__rpw.focus();
	}
	else if ( __pwd.value!=__rpw.value )
	{
		alert('As senhas informadas divergem!');
		__pwd.focus();
	}
	else
	{
		__params = 'acao=cadastrar&ids='+document.getElementById('tmpid').value+
				   '&nome='+__nom.value+'&cpf='+__cpf.value+
				   '&tel='+__tel.value+'&sexo='+__sex.value+
				   '&email='+__ema.value+'&senha='+__pwd.value;
		__url = 'carrinho.ecom.php';
		
		postText(__url, __params, ECOM__Eval_ecomm);	
		ECOM__processo('ini');
	}
}

function ECOM__antesDePagar()
{
	__forma = document.getElementById('formapgto').value;
	
	if (  __forma=='vi' || __forma=='ve' || __forma=='ma' )
	{
		__params = 'acao=pagar&ids='+document.getElementById('tmpid').value+
				   '&ttl='+document.getElementById('ttl').value+
				   '&pgt='+__forma;
				   
	if ( document.getElementById('xyz')!=null )
	{
		if ( document.getElementById('xyz').value!='' )
		{
			__params += '&xyz='+document.getElementById('xyz').value;
		}
	}
				   
		__url = 'carrinho.ecom.php';

		postTextFull(__url, 'acao=carregar_pgto', ECOM__retCarregando, __url+'@'+__params);
	}
	else
	{
		alert('Por favor escolha uma forma de pagamento!');	
	}	
}

function ECOM__retCarregando(resp, params)
{
	retDivFlutuante(resp);
	
	arr = params.split('@');
	postText(arr[0], arr[1], Eval);	
}

function ECOM__repagar_ecomm()
{
		__params = 'acao=repagar&ids='+document.getElementById('tmpid').value;
		__url = 'carrinho.ecom.php';
		
		postText(__url, __params, retDivFlutuante);	
		ECOM__processo('ini');		
}

function ECOM__continuar_ecomm(__dst)
{
	document.location.href = ( __dst!='' )? __dst : '../index.php';
}

function ECOM__atualizar_ecomm()
{
	document.location.reload(true);
}


function ECOM__inicio(resp, destino)
{
	alert(resp)
	document.location.href = ( destino!='' )? destino : '../index.php';
}

function ECOM__sair_ecomm()
{
	if ( confirm('Tem certeza que deseja sair de seu usuário?') )	
	{
		__params = 'acao=sair&ids='+document.getElementById('tmpid').value;
		__url = 'carrinho.ecom.php';
		
		postText(__url, __params, ECOM__Eval_ecomm);				
	}	
}

function ECOM__logar_ecomm()
{
	__params = 'acao=logar&ids='+document.getElementById('tmpid').value;
	__url = 'carrinho.ecom.php';
	
	postText(__url, __params, retDivFlutuante);
}

function ECOM__Eval_ecomm(resp)
{	
	ECOM__processo('fim');
	eval(resp);
}

function ECOM__processo(__fase)
{
	if ( document.getElementById('td_processando') )
	{
		if ( __fase=='ini' )
		{
			document.getElementById('td_processando').style.display = '';
		}
		else if ( __fase=='fim' )
		{
			document.getElementById('td_processando').style.display = 'none';
		}
	}	
}

function ECOM__selpgto(__forma)
{
	document.getElementById('formapgto').value = __forma;
}

function ECOM__x()
{
	params = 'controle=xyz'+
			 '&xyz='+document.getElementById('xyz').value;
	postText('ecom/xyz.ecom.php', params, retX);
}

function retX(resp)
{
	eval(resp)
}

function ECOM__reenviar_email_compra(__codcli, __email)
{
	__params = 'acao=reenviar';
	__url = 'email_copia_carrinho.ecom.php';
	postText(__url, __params, ECOM__Eval_ecomm);
}

