// store func

function openChat() {
	var url = 'http://www.easychat.com.br/easy/iframe_w33.php?chat_id=1136&clie_id=1101&check_sum=3338';
	window.open (url, "MSChat", "location=0,status=1,width=400,height=550");
}

function changeCondition(e, id) {

	addLoading('condicoes');

	$('#condicoes').load('_ajax/loja_parcelamento.php?c='+id+'&t='+e.value);

}

function desejosIncluir(p) {

	addLoading('desejoIncluir');

	$('#desejoIncluir').load('_ajax/loja_favoritos.php?p='+p);

}

function storeFreteCalc(d, id) {
	addLoading('valorFrete');
	var tipoFrete = 'S';
	if (!document.getElementById('tipo_frete_sedex').checked) tipoFrete = 'P';
	
	$('#valorFrete').load('_ajax/loja_frete.php?cep='+d.value+'&t='+tipoFrete, function(data) {
		if (data == -1) {
			$('#valorFrete').html('R$ 0,0');
			showTr('trFreteParceiro');
		} else if (data == -2) {
			$('#valorFrete').html('R$ 0,0');
			showTr('trFreteParceiro');
		} else if (data == -3) {
			$('#valorFrete').html('R$ 0,0');
			showTr('trFreteGratis');
		} else
			hideTr('trFreteParceiro');

		setTimeout('storeTotalCalc()', 500);
	});
	
}
function storeCupomCalc(d) {

	addLoading('valorCupom');

	$('#valorCupom').load('_ajax/loja_cupom.php?id='+d.value, function() {

		storeTotalCalc();

	});

}

function storeTotalCalc(d) {

	addLoading('valorTotal');

	

	// prices

	var produtos = $('#valorProdutos').html().replace('R$ ','');

	var cupom = $('#valorCupom').html().replace('R$ ','');

	var frete = $('#valorFrete').html().replace('R$ ','');

	

	$('#valorTotal').load('_ajax/loja_total.php?p='+produtos+'&c='+cupom+'&f='+frete);

}

// ms main js functions

function addLoading(div) {

	if (div) {

		$('#' + div).html("<img src='_images/loader.gif' border='0'>");

	}

}

function delLoading(div) {

	if (div) {

		$('#' + div).html("");

		$('#' + div).hide();

	}

}

function showHide(div) {

	if ($("#" + div).css("display") == "none" || $("#" + div).css("display") == "") {

		$("#" + div).slideDown('slow');

	} else {

		$("#" + div).slideUp('slow');

	}

}



function showHideTr(div) {

	if ($("#" + div).css("display") == "none" || $("#" + div).css("display") == "") {

		showTr(div);

	} else {

		hideTr(div);

	}

}



function showTr(div) {

	if ($.browser.msie)

		document.getElementById(div).style.display = 'table-row';

//		$("#" + div).css("display", "table-row");

	else

		$("#" + div).css("display", "table-row");

		//$("#" + div).slideDown('slow');

}



function hideTr(div) {

	if ($.browser.msie)

		document.getElementById(div).style.display = 'none';

	else

		$("#" + div).css("display","none");

}



function txtBox(box,tipo) {

	if (box) {

		if (tipo == 'in') {

			if (box.value == box.title) {

				box.value = '';

			}

		} else {

			if (box.value == '') {

				box.value = box.title;

			}

		}

	}

}

Number.prototype.formatMoney = function(c, d, t){

    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "",

    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)

    + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");

};



function trocaImagemProduto(d) {
	$('#productImageBig').css('background-image', 'url('+d+')');
}

function validarIndique() {

	if ($('#nome').val() == '') {

		$('#nome').focus();

		alert('Preencha corretamente o seu nome.');

		return false;

	}

	for(var i=1; i <= 5; i++) {

		if ($('#email' + i).val() == '' || !emailCheck($('#email' + i).val())) {

			$('#email' + i).focus();

			alert('Preencha corretamente todos os e-mails.');

			return false;

		}

	}

	return true;

}

function emailCheck(str) {

	var at="@";

	var dot=".";

	var lat=str.indexOf(at);

	var lstr=str.length;

	var ldot=str.indexOf(dot);

	

	if (str.indexOf(at)==-1){

		return false;

	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

		return false;

	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

		return false;

	}

	if (str.indexOf(at,(lat+1))!=-1){

		return false;

	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

		return false;

	}

	if (str.indexOf(dot,(lat+2))==-1){

		return false;

	}

	if (str.indexOf(" ")!=-1){

		return false

	}	

	return true					

}

function rememberPassword() {
	addLoading('r_loading');
	var email = $('#rp_email').attr('value');
	$.get("cadastro_lembrarsenha_do.php?e="+email, function(data){
		if (data == '0') {
			alert('O e-mail informado não está cadastrado.');
		} else {
			alert('Uma nova senha foi gerada e enviada para o seu e-mail');
			tb_remove();
		}
	});
}

function blog_send() {
	var nome = $('#nome').val();
	var email = $('#email').val();
	var to_nome = $('#to_nome').val();
	var to_email = $('#to_email').val();
	var id = $('#c').val();
	
	if (nome.length == 0 || email.length == 0 || to_nome.length == 0 || to_email.length == 0) {
		alert('Preencha todos os dados');
		return false;
	}
	
	addLoading('r_loading');
	
	$.get("blog_enviar_do.php?e="+email+"&n="+nome+"&tn="+to_nome+"&te="+to_email+"&id=" + id, function(data){
		if (data == '0') {
			alert('Erro enviando indicação. Tente novamente em alguns instantes.');
		} else {
			alert('Indicação enviada com sucesso!');
			tb_remove();
		}
	});
}



function selectPerson() {

	if (document.getElementById('tipo_f').checked) {

		$('#cpfLabel').html('CPF');

		$('#trRazao').slideUp();
		$('#trIE').slideUp();

		//$('#trNasc').slideDown();

	} else {

		$('#cpfLabel').html('CNPJ');

		$('#trRazao').slideDown();
		$('#trIE').slideDown();

		//$('#trNasc').slideUp();

	}

}

function validateComment() {
	if ($('#f_nome').val() == '') {
		$('#f_nome').focus();
		alert('Preencha o nome');
		return false;
	} else if ($('#f_email').val() == '' || !isValidEmail($('#f_email').val())) {
		$('#f_email').focus();
		alert('Preencha o e-mail corretamente.');
		return false;
	} else if ($('#f_msg').val() == '') {
		$('#f_msg').focus();
		alert('Preencha a mensagem');
		return false;
	} else {
		addLoading('formLoading');
		return true;
	}
}

function isValidEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email))
		return false;
	else
		return true;
}

function showResponse(responseText, statusText) {
	delLoading('formLoading');
	alert(responseText); 
}