function show_login() {
	if ($('#login_pop')) {
		$('#login_pop').load("/account/popup_login");
		$('#login_pop').show();
	}
}

function show_forgot_pass() {
	if ($('#login_pop')) {
		$('#login_pop').load("/account/popup_forgot_pass");
		$('#login_pop').show();
	}
}

function show_clasps() {
	if ($('#popup')) {
		$('#popup').load("/shop/popup_clasps");
		
		$('#popup').width('400px');
		$('#popup').height('200px');
		
		$('#popup').css('top','200px');
		$('#popup').css('margin-left','250px');
		
		$('#popup').show();
	}
}

function process_login() {
	$.post("/account/process_login", 
			{ email: $('#email').val(), password: $('#password').val() },
			function(data){
				if (data.msg == '1') {
					//$('#login_pop').show();
					//$("#errormsg").text('success');
					$('#login_pop').hide();
					$('#login_li').remove();
					$('#utilitynav').prepend('<li><a href="/account/home">My account</a></li><li><a href="/account/logout">Log Out</a></li>');
				} else {
					$("#errormsg").html('<p>' + data.msg + '</p>');
				}
			}, 
			"json");
}

function process_forgot_pass () {
	$.post("/account/process_forgot_pass", 
			{ email: $('#email').val() },
			function(data){
				if (data.msg == '1') {
					$('#email_field').hide();
					$('#submit_btn').hide();
					$('#msg').text('Your password has been emailed to you');
				} else {
					$('#msg').text('We did not recognize your email. Please try again');
				}
			}, 
			"json");
}

function add_to_cart() {	
	$.post("/shop/add_to_cart_ajax", 
		{ rcid: $('#rope_color_id').val(), 
			wsid: $('#wrist_size_id').val(), 
			cid: $('#clasp_product_id').val(), 
			pid: $('#pid').val(), 
			qty: $('#quantity').val() },
		function(data){
			if (data.msg_code == "1") {
				$("#msg").text(data.msg);
				$("#msg").append('<br /><a href="/shop/cart">View Cart</a>');
			} else if (data.msg_code == "0") {
				$("#msg").text(data.error);
			}
		}, 
		"json");
	$("#msg").show();
	get_cart_count();
}

function cart_remove(cart_id) {
	$.post("/shop/cart_remove_ajax", 
		{ cid: cart_id },
		function(data){
			if (data.msg_code == "1") {
				$("#cart_item_" + cart_id).css('backgroundColor','#ff0000');
				$("#cart_item_" + cart_id).fadeOut(500,function () { 
					$("#cart_item_" + cart_id).remove(); 
					update_cart_price(); 
				});
			}
		}, 
		"json");
	get_cart_count();
}

function cart_btn(proc) {
	$('#proc').val(proc);
	$('#cart_form').submit();
}

function nu_btn(proc) {
	$('#proc').val(proc);
	$('#nu_form').submit();
}

function co_btn(proc) {
	$('#proc').val(proc);
	$('#user_form').submit();
}

function new_ship_address() {
	$('#use_new_ship').attr('checked','checked');
	$('#new_ship_address').show();
}

function new_bill_address() {
	$('#use_new_bill').attr('checked','checked');
	$('#new_bill_address').show();
}

function update_cart_price() {
	var item_price = 0;
	var total = 0;
	$("td.item_total").each( function(i) {
		price = $('#' + this.id).text().replace('$','') * 1;
		total += price;
	});
	var dollar_amt = (total * 100);
    dollar_amt = Math.round(dollar_amt);
    dollar_amt = (dollar_amt / 100);
	$('#sub_total').text('$' + dollar_amt);
}

function get_cart_count() {	
	$.get("/shop/cart_items_cnt",
		function(data) {
			$('#cart_cnt').text('(' + data + ')');
		});
}