var objPopup;

/*
' --------------------------------------------------------------------------------------------------------
©			Digital Industry - 2007
Autor:		Rafael Cronemberger de Assis
Cliente: 	Brother Store
------------------------------------------------------------------------------------------
*/

var isNS4 				= (navigator.appName=="Netscape")?1:0;
var isNN 				= (navigator.appName.indexOf("Netscape")!=-1);

window.defaultStatus 	= "Brother Store";

function posicionaDiv(){
	var nmCenter = (document.body.clientWidth - 770)/2;
}

function RedirectMenu(stLinkMenu, fgMethod){
	if(fgMethod == 0){
		document.location.href=stLinkMenu;
	}else{
		window.open(stLinkMenu);
	}
}

function displayFlash(largura, altura, arquivo){
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+largura+'" height="'+altura+'" id="objFlash" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" /><param name="wmode" value="transparent" />');
	document.write('<param name="movie" value="'+arquivo+'" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><embed src="'+arquivo+'" wmode="transparent" quality="best" width="'+largura+'" height="'+altura+'" name="div" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"/>');
	document.write('</object>');
}

function displayDhtmlFlash(largura, altura, arquivo, nmTime){
	window.setTimeout("closeDhtml();", nmTime);

	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+largura+'" height="'+altura+'" id="objDhtmlFlash" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" /><param name="wmode" value="transparent" />');
	document.write('<param name="movie" value="'+arquivo+'" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><embed src="'+arquivo+'" wmode="transparent" quality="best" width="'+largura+'" height="'+altura+'" name="div" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"/>');
	document.write('</object>');
}

function closeDhtml(){
	document.getElementById("objDhtmlFlash").style.display = "none";
}

function closeBanner(){
	document.getElementById("divBannerTop").style.display = "none";
}



function blockChars(){
	if(!isNS4){
		if(event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;
	}else{
		if(event.which < 45 || event.which > 57) returnfalse;
	}
}

function blockSpecialChars(){
	if(!isNS4){
		if ((event.keyCode > 32 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)) event.returnValue = false;
	}else{
		if((event.which > 32 && event.which < 48) || (event.which > 57 && event.which < 65) || (event.which > 90 && event.which < 97)) return false;
	}
}

function Field_OnFocus(objForm, stFieldValue){
	if(eval(objForm).value == stFieldValue){
		eval(objForm).value = "";
	}
}

function Field_OnBlur(objForm, stFieldValue){
	if(eval(objForm).value == ""){
		eval(objForm).value = stFieldValue;
	}
}

function displayMessage(objField, stMessage){
	if(objField != null){
		alert("Atenção para o(s) seguinte(s) erro(s) abaixo:\n\n"+stMessage);

		if(objField.disabled != true){
			objField.focus();
		}
	}
	return false;	
}

function validateSearch(objForm){
	if(objForm.stBusca.value == "" || objForm.stBusca.value == "Buscar"){
		return displayMessage(objForm.stBusca, "Favor preencher o campo Busca corretamente.");
	}
}

function abreTopico(stNomeTr, idTr){
	var nmResults;
	nmResults = document.getElementById("nmTotalItens").value;

	if(document.getElementById(stNomeTr+idTr).style.display == ""){
		document.getElementById(stNomeTr+idTr).style.display = "none"
	}else{
		for(i = 0; i < nmResults; i++){
			if(idTr != i){
				document.getElementById(stNomeTr+i).style.display = "none";
			}else{
				document.getElementById(stNomeTr+i).style.display = "";
			}
		}
	}
	i 			= 0
	nmResults	= 0
}

function validateRegexEmail(stEmail){
	if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(stEmail)){
		return false;
	}
} 

function validateLogin(objForm){
	if(validateRegexEmail(objForm.stEmail.value) == false){
		return displayMessage(objForm.stEmail, "Favor preencher o campo E-mail corretamente.");
	}else if(objForm.stSenha.value == ""){
		return displayMessage(objForm.stSenha, "Favor preencher o campo Senha corretamente.");
	}
}

function CalculaSubtotal(objForm, nmPrecoOpcional){
	var objSpan;
	var nmSubtotal
	var nmTotal

	objSpan 			= document.getElementById("spanSubtotal");
	nmSubtotal			= document.frmProdDet.nmPrecoPor.value;

	nmSubtotal 			= parseFloat(nmSubtotal.toString().replace(",",'.'));
	nmPrecoOpcional 	= parseFloat(nmPrecoOpcional.toString().replace(",",'.'));
	
	if(objForm.checked == true){
		nmTotal = nmSubtotal + nmPrecoOpcional
	}else{
		nmTotal = nmSubtotal - nmPrecoOpcional
	}

	document.frmProdDet.nmPrecoPor.value = nmTotal
	objSpan.innerHTML 	= formatCurrency(nmTotal)
}

function formatCurrency(nmValor){
	nmValor = nmValor.toString().replace(/\$|\,/g,'');
	if(isNaN(nmValor)) nmValor = "0";

	fgValor 	= (nmValor == (nmValor = Math.abs(nmValor)));
	nmValor 	= Math.floor(nmValor * 100 + 0.50000000001);
	nmCentavos 	= nmValor % 100;
	nmValor = Math.floor(nmValor/100).toString();
	if(nmCentavos < 10) nmCentavos = "0" + nmCentavos;
	
	for (var i = 0; i < Math.floor((nmValor.length-(1+i)) / 3); i++)
	nmValor 	= nmValor.substring(0,nmValor.length-(4*i+3))+'.'+

	nmValor.substring(nmValor.length-(4*i+3));
	return (((fgValor)?'':'-') + 'R$ ' + nmValor + ',' + nmCentavos);
}

function RecalculaQtdProduto(){
	document.frmComprar.action="?stAction=Update";
	document.frmComprar.submit();
}

function DesabilitaEvento(){
	alert("Atenção.\n\nNão é possível alterar a quantidade de Garantias Extendidas.")
	return false;
}

function focusLogin(){
	document.frmLogin.stEmail.value = "Favor preencher.";
	window.setTimeout("document.frmLogin.stEmail.value = '';", 1200);
	document.frmLogin.stEmail.focus();
}

function validatePagamento(objForm){
	var objIdFormaPagamento
	var nmFormaPagamento
	var fgErro

	fgErro = true;

	objFormaPagamento = objForm.stCodPagamento
	nmFormaPagamento	= objFormaPagamento.length
	
	if(!nmFormaPagamento){
		if(objFormaPagamento.checked == true){
			fgErro = false
		}
	}else{
		for(i = 0; i < nmFormaPagamento; i++){
			if(objFormaPagamento[i].checked == true){
				fgErro = false
			}
		}
	}

	if(fgErro == true){
		if(!nmFormaPagamento){
			return displayMessage(objFormaPagamento, "Favor selecionar alguma forma de pagamento.");
		}else{
			return displayMessage(objFormaPagamento[0], "Favor selecionar alguma forma de pagamento.");
		}
	}else{
		/* Disabled - Submit */
		document.getElementById("btnFinalizaCompra").disabled = true;
	}
}

function geraTransacaoBoleto(objForm, stParametros, stWindowName){
	if(!stWindowName)stWindowName = 'formTarget' + (new Date().getTime());
	objForm.target 	= stWindowName;
	objPopup 	= open('', stWindowName, stParametros);
	
	nmLargura 	= (screen.width - 695) / 2; 
	nmAltura 	= (screen.height - 550) / 2; 

	objPopup.moveTo(nmLargura, nmAltura);
	objPopup.focus();
	
	document.getElementById("btnGeraBoleto").disabled = true;

	//document.location.href("../finalizacao/finalizacao.asp");
}

function validaCnpj(stCnpj){
	
	var i;
	var c 	= stCnpj.substr(0,12);
	var dv 	= stCnpj.substr(12,2);
	var d1 	= 0;
		
	if (stCnpj != ""){
		for (i = 0; i < 12; i++){
			d1 += c.charAt(11-i)*(2+(i % 8));
		}
		if (d1 == 0){
			alert("O Cnpj informado está inválido.");
			return false;
		}
		d1 = 11 - (d1 % 11);
		if (d1 > 9) d1 = 0;
		if (dv.charAt(0) != d1){
			alert("O Cnpj informado está inválido.");
			return false;
		}
		d1 *= 2;
		for (i = 0; i < 12; i++){
			d1 += c.charAt(11-i)*(2+((i+1) % 8));
		}
		d1 = 11 - (d1 % 11);
		if (d1 > 9) d1 = 0;
		if (dv.charAt(1) != d1){
			alert("O Cnpj informado está inválido.");
			return false;
		}
		document.frmCadastro.nmAcao.value=1;
		document.frmCadastro.submit();
	}else{
		document.frmCadastro.nmAcao.value=1;
		document.frmCadastro.submit();
	}	
}
function validaCPF() {
	stCpf = document.frmCadastro.stCpf.value;
	stCpf = stCpf.replace(".", "");
	stCpf = stCpf.replace(".", "");
	stCpf = stCpf.replace("-", "");
	valor = true;
	erro = new String;
	if (stCpf.length < 11) erro += "Preencha corretamente o número do seu CPF! \n\n";
	var nonNumbers = /\D/;
	if (nonNumbers.test(stCpf)) erro += "Preencha corretamente o número do seu CPF! \n\n";
	if (stCpf == "00000000000" || stCpf == "11111111111" || stCpf == "22222222222" || stCpf == "33333333333" || stCpf == "44444444444" || stCpf == "55555555555" || stCpf == "66666666666" || stCpf == "77777777777" || stCpf == "88888888888" || stCpf == "99999999999"){
			erro += "Número de CPF inválido!"
	}
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++){
		a[i] = stCpf.charAt(i);
		if (i < 9) b += (a[i] *  --c);
	}
	if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	b = 0;
	c = 11;
	for (y=0; y<10; y++) b += (a[y] *  c--); 
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	if ((stCpf.charAt(9) != a[9]) || (stCpf.charAt(10) != a[10])){
		erro +="Número de CPF inválido!";
	}
	if (erro.length > 0){
		alert(erro);
	}
	else
	{
	    document.frmCadastro.nmAcao.value=1;
		document.frmCadastro.submit();
	}
}

function FormataFormaPagamento(stFormaPagamento){
	if(stFormaPagamento == "boleto_bbb"){
		document.frmFormaPgt.stFormaPagamento.value = "Boleto Bancário - Banco do Brasil";
	}else if(stFormaPagamento == "depos_bbb"){
		document.frmFormaPgt.stFormaPagamento.value = "Depósito Bancário - Banco do Brasil";
	}else if(stFormaPagamento == "depos_uni"){
		document.frmFormaPgt.stFormaPagamento.value = "Depósito Bancário - Unibanco";
	}else if(stFormaPagamento == "depos_itau"){
		document.frmFormaPgt.stFormaPagamento.value = "Depósito Bancário - Itaú";
	}else if(stFormaPagamento == "boleto_uni"){
		document.frmFormaPgt.stFormaPagamento.value = "Boleto Bancário - Unibanco";
	}else if(stFormaPagamento == "cartao_visa"){
		document.frmFormaPgt.stFormaPagamento.value = "Cartão de Crédito Visa";
	}else if(stFormaPagamento == "cartao_master"){
		document.frmFormaPgt.stFormaPagamento.value = "Cartão de Crédito MasterCard";
	}
}

function geraTransacaoVisa(stNumeroCartao, nmParcelas){
	window.name = "loja";

	if(document.frmFormaPgt.stNumeroCartao.value == ""){
		alert("Por favor, informe os 6 primeiros dígitos do seu cartão de crédito.");
		document.frmFormaPgt.stNumeroCartao.focus();
		return false;
	}else{
		if(navigator.appName.indexOf("Netscape") != -1){
		   objPopup = window.open("", "objPopup", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,screenX=0,screenY=0,left=0,top=0,width=765,height=440");
		}else{
		   objPopup = window.open("", "objPopup", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,screenX=0,screenY=0,left=0,top=0,width=765,height=440");
		}
		
		nmLargura 	= (screen.width - 765) / 2;
		nmAltura 	= (screen.height - 440) / 2;

		objPopup.location.href=("../Visa/PostVisa.asp?stNumeroCartao="+stNumeroCartao+"&nmParcelas="+nmParcelas)
		objPopup.moveTo(nmLargura, nmAltura);
		
		document.getElementById("btnTransacaoVisa").disabled = true;		
	}
}

function geraTransacaoMaster(nmParcelas){
	window.name = "loja";

	if(navigator.appName.indexOf("Netscape") != -1){
	   objPopup = window.open("", "objPopup", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,screenX=0,screenY=0,left=0,top=0,width=765,height=440");
	}else{
	   objPopup = window.open("", "objPopup", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,screenX=0,screenY=0,left=0,top=0,width=765,height=440");
	}
	
	nmLargura 	= (screen.width - 765) / 2;
	nmAltura 	= (screen.height - 440) / 2;

	objPopup.location.href=("../MasterCard/PostMasterCard.asp?nmParcelas="+nmParcelas)
	objPopup.moveTo(nmLargura, nmAltura);
	
	document.getElementById("btnTransacaoMaster").disabled = true;		
}

function fechaTransacaoMaster(){
	popupTransacaoCartao = window.open('', 'objPopup', 'width=0, height=0');
	popupTransacaoCartao.close();	
}

function fechaTransacaoVisa(){
	popupTransacaoCartao = window.open('', 'objPopup', 'width=0, height=0');
	popupTransacaoCartao.close();	
}

function validateEsqueci(objForm){
	if(validateRegexEmail(objForm.stEmailLembrete.value) == false){
		alert("Favor informar um e-mail válido.");
		objForm.stEmailLembrete.focus();
		return false;
	}
}

function preencheCampo(){
	var fgEntrega;
	
	fgEntrega = document.frmCadastro.fgEntrega.value;

	if(fgEntrega == "on"){
		stNomeEntrega				= eval(document.frmCadastro.stNomeEntrega);
		stNome						= document.frmCadastro.stNome.value;
		stNomeEntrega.value 		= stNome;
		stCepEntrega1				= eval(document.frmCadastro.stCepEntrega1);
		stCep1						= document.frmCadastro.stCep1.value;
		stCepEntrega1.value 	 	= stCep1;
		
		stCepEntrega2				= eval(document.frmCadastro.stCepEntrega2);
		stCep2						= document.frmCadastro.stCep2.value;
		stCepEntrega2.value 		= stCep2;
		
		stEnderecoEntrega			= eval(document.frmCadastro.stEnderecoEntrega);
		stEndereco					= document.frmCadastro.stEndereco.value;
		stEnderecoEntrega.value 	= stEndereco;
		
		stComplementoEntrega	   = eval(document.frmCadastro.stComplementoEntrega);
		stComplemento			   = document.frmCadastro.stComplemento.value;
		stComplementoEntrega.value = stComplemento;
		
		stBairroEntrega		 		= eval(document.frmCadastro.stBairroEntrega);
		stBairro			  		= document.frmCadastro.stBairro.value;
		stBairroEntrega.value 		= stBairro;
		
		stNumeroEntrega		  		= eval(document.frmCadastro.stNumeroEntrega);
		stNumero			  		= document.frmCadastro.stNumero.value;
		stNumeroEntrega.value 		= stNumero;
		
		idUfEntrega		  			= eval(document.frmCadastro.idUfEntrega);
		idUf			  			= document.frmCadastro.idUf.value;
		idUfEntrega.value 			= idUf;
		
		if(idUfEntrega.value != ""){
			idCidadeEntrega		  = eval(document.frmCadastro.idCidadeEntrega);
			idCidade			  = document.frmCadastro.idCidade.value;
			idCidadeEntrega.value = idCidade;
		}
	}
}

function Complete(obj, evt){
	if((!obj) || (!evt) || (aProduto.length == 0)){
		return;
	}

	if (obj.value.length == 0){
		return;
	}
	
	var elm = (obj.setSelectionRange) ? evt.which : evt.keyCode;
	
	if ((elm < 32) || (elm >= 33 && elm <= 46) || (elm >= 112 && elm <= 123)) {
		return;
	}
	
	var txt = obj.value.replace(/;/gi, ",");
	
	elm = txt.split(",");
	txt = elm.pop();
	txt = txt.replace(/^\s*/, "");
	
	if (txt.length == 0) {
		return;
	}
	
	if (obj.createTextRange){
		var rng = document.selection.createRange();
		if (rng.parentElement() == obj) {
			elm = rng.text;
			var ini = obj.value.lastIndexOf(elm);
		}
	}else if(obj.setSelectionRange){
		var ini = obj.selectionStart;
	}
	
	for (var i = 0; i < aProduto.length; i++){
		elm = aProduto[i].toString();
		if(elm.toLowerCase().indexOf(txt.toLowerCase()) == 0){
			obj.value += elm.substring(txt.length, elm.length);
			break;
		}
	}
	
	if(obj.createTextRange){
		rng = obj.createTextRange();
		rng.moveStart("character", ini);
		rng.moveEnd("character", obj.value.length);
		rng.select();
	}else if(obj.setSelectionRange){
		obj.setSelectionRange(ini, obj.value.length);
	}
}

function vopenw() {
	tbar='location=no,status=yes,resizable=yes,scrollbars=yes,width=560,height=535';
	sw =  window.open('https://www.certisign.com.br/seal/splashcerti.htm','CRSN_Splash',tbar);
	sw.focus();
}

function ChangeCountry(objForm){
	var stCountry
	stCountry = objForm.stCountry.value

	if(stCountry != ""){
		document.location.href=(stCountry)
	}
	return false;
}

function abrePublicidade(){
	objPopup = window.open("/publicidade.html", "objPopup", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=0,screenX=0,screenY=0,left=0,top=0,width=400,height=400");	
}