function test() {
	alert(document.referrer);
}

function switchTab(id,id2) {
	$('#' + id).show();
	$('#' + id2).hide();
	$('#tab-' + id).addClass('selected');
	$('#tab-' + id2).removeClass('selected');
	$.post('/ajax/setsession', {
		gender: id
	});
}

function toggleSublist(id) {
	$('#sublist_' + id).toggle();
}

function login() {
	var form = $('#form-login').serialize();
	$.post('/ajax/login', {
		post: form
	}, function (data) {
		if (data == 'true') {
			location.reload();
		} else {
			alert(data);
		}
	});	
}

function password() {
	var form = $('#form-password').serialize();
	$.post('/ajax/password', {
		post: form
	}, function (data) {
		if (data == 'true') {
			alert('Ett mail med mer information om hur\ndu byter lösenord har skickats till dig!');
		} else {
			alert(data);
		}
	});	
}

function contact() {
	$('#error').hide();
	$('#submit').hide();
	$('#loading').show();
	var form = $('#form').serialize();
	$.post('/ajax/contact', {
		post: form
	}, function (data) {
		$('#loading').hide();
		$('#submit').show();
		if (data == 'true') {
			$('#success').fadeIn();
			$('#form').hide();
		} else {
			$('#error').fadeIn();
			$('#error').html(data);
		}
	});
}

function user(id) {
	$('#error').hide();
	$('#success').hide();
	$('#maintabs li').removeClass('selected');
	$('#tab_' + id).addClass('selected');
	$('#profile').hide();
	$('#favorites').hide();
	$('#points').hide();
	$('#premiums').hide();
	$('#ref').hide();
	$('#' + id).fadeIn();
		
}

function logout() {
	$.get('/ajax/logout');
	location.reload();
}

function signup() {
	$('#error').hide();
	$('#submit').hide();
	$('#loading').show();
	var form = $('#form').serialize();
	$.post('/ajax/signup', {
		post: form
	}, function (data) {
		$('#loading').hide();
		$('#submit').show();
		if (data == 'true') {
			$('#signupform').hide();
			$('#success').fadeIn();
		} else {
			$('#error').fadeIn();
			$('#error').html(data);
		}
	});
}

function userUpdateInformation() {
	$('#error').hide();
	$('#success').hide();
	$('#submit-information').hide();
	$('#loading-information').show();
	var form = $('#form-information').serialize();
	$.post('/ajax/userUpdateInformation', {
		post: form
	}, function (data) {
		$('#loading-information').hide();
		$('#submit-information').show();
		if (data == 'true') {
			$('#success').fadeIn();
		} else {
			$('#success').hide();
			$('#error').fadeIn();
			$('#error').html(data);
		}
	});
}

function userUpdatePassword() {
	$('#error').hide();
	$('#success').hide();
	$('#submit-password').hide();
	$('#loading-password').show();
	var form = $('#form-password').serialize();
	$.post('/ajax/userUpdatePassword', {
		post: form
	}, function (data) {
		$('#loading-password').hide();
		$('#submit-password').show();
		if (data == 'true') {
			$('#success').fadeIn();
		} else {
			$('#success').hide();
			$('#error').fadeIn();
			$('#error').html(data);
		}
	});
}

function userUpdateNewsletter() {
	$('#error').hide();
	$('#success').hide();
	$('#submit-newsletter').hide();
	$('#loading-newsletter').show();
	var form = $('#form-newsletter').serialize();
	$.post('/ajax/userUpdateNewsletter', {
		post: form
	}, function (data) {
		$('#success').show();
		$('#submit-newsletter').show();
		$('#loading-newsletter').hide();	
	});
}

function saveFav(id) {
	$.post('/ajax/savefav', {
		productID: id
	}, function (data) {
		$('#product-fav').html('En av mina favoriter!');
	});
}

function removeFav(id) {
	$.post('/ajax/removefav', {
		favoriteID: id
	}, function (data) {
		$('#fav-' + id).fadeOut();
	});
}

function removeFromCart(id) {
	var items = $('#items').html();
	items = parseInt(items);
	var num 		= $('#num-' + id).html();
	var amount		= $('#amount-' + id).html();
	var price		= $('#price-' + id).html();
	var numtotal 	= $('#numtotal').html();
	var amounttotal	= $('#amounttotal').html();
	var pricetotal	= $('#pricetotal').html();
	num 		= parseInt(num);
	amount 		= parseInt(amount);
	price 		= parseInt(price);
	numtotal 	= parseInt(numtotal);
	amounttotal	= parseInt(amounttotal);
	pricetotal 	= parseInt(pricetotal);	
	$('#num-' + id).html(num - 1);
	$('#amount-' + id).html(amount - 50);
	$('#price-' + id).html(price - 1000);
	$('#items').html(items - 1);
	$('#numtotal').html(items - 1);
	$('#amounttotal').html(amounttotal - 50);
	$('#pricetotal').html(pricetotal - 1000);
	
	if (num == 1) {
		$('#cart-' + id).fadeOut();
	}
	if (items == 1) {
		$('#cart').fadeOut();
	}
}

function addToCart(title, id) {
	var items 		= $('#items').html();
	var numtotal 	= $('#numtotal').html();
	var amounttotal	= $('#amounttotal').html();
	var pricetotal	= $('#pricetotal').html();
	
	items 		= parseInt(items);
	numtotal 	= parseInt(numtotal);
	amounttotal	= parseInt(amounttotal);
	pricetotal 	= parseInt(pricetotal);
	
	$('#items').html(items + 1);
	$('#numtotal').html(items + 1);
	$('#amounttotal').html(amounttotal + 50);
	$('#pricetotal').html(pricetotal + 1000);
	
	if ($('#cart-' + id).length == 0) {
		$('#cart').fadeIn();
		$('#shoppingbag tbody').append('<tr id="cart-' + id + '"><td>' + title + '</td><td><span id="num-' + id + '">1</span> st</td><td><span id="amount-' + id + '">50</span> kr</td><td><span id="price-' + id + '">1000</span>p</td><td><a href="#" onclick=removeFromCart("' + id + '")>Ta bort</a></td>');
	} else {
		$('#cart').fadeIn();
		var num 	= $('#num-' + id).html();
		var amount	= $('#amount-' + id).html();
		var price	= $('#price-' + id).html();
		
		num 	= parseInt(num);
		amount 	= parseInt(amount);
		price 	= parseInt(price);
		
		$('#cart-' + id).show();
		$('#num-' + id).html(num + 1);
		$('#amount-' + id).html(amount + 50);
		$('#price-' + id).html(price + 1000);
	}
}

function order() {
	var items = $('#items').html();
	if (items == 0) {
		alert('Du har inte valt något presentkort!');
	} else {
		$.post('/ajax/order', {
			items: items
		}, function (data) {
			if (data == 'true') {
				alert('Klart! Presentkortet kommer hem på\nposten inom ett par dagar!');
			} else {
				alert('Du har inte tillräckligt med poäng\nför att beställa ' + items + ' st presentkort.');
			}
		});
	}
}

function outfit_vote(id) {
	$.post('/ajax/outfit_vote', {
		outfitID: id
	}, function (data) {
		if (data == 'true') {
			alert('Tack för din röst!');	
		} else {
			alert(data);
		}
	});
}
