$(document).ready(function(){

	$("#webshop-menu span").live("click", toggle_this);
	$(".toggle-prev").live("click", toggle_prev);
	$(".toggle_next").live("click", toggle_next);
	$(".webshop-product input").live("keypress", prevent_alphabetical);
	
	$("input#autofill").live("click", autofill);


});

function toggle_this(){
	var id = $(this).attr("id").replace("webshop-menu-", "");
	
	if($(this).hasClass("webshop-active") || id == 4){
		return;
	}
	
	current = $(".webshop-active").attr("id").replace("webshop-menu-", "");
	$("#webshop-menu span").removeClass("webshop-active");
	$("#webshop-menu span").eq(parseInt(id)-1).addClass("webshop-active");
	$("#webshop-"+current).removeClass("showme").addClass("hideme");
	$("#webshop-"+id).removeClass("hideme").addClass("showme");

	get_content(parseInt(id));
	
};
function autofill(){
	
	if($(this+':checked').size() > 0){
		$("#webshop-invoice input").each(function(i){
			$("#webshop-delivery input").eq(i).val($(this).val());
		});
	}
	else{
	
		$("#webshop-delivery input").each(function(){
			$(this).val('');
		});
	}
	
}
function toggle_next(){
	var id = $(this).attr("id").replace("toggle_", "");
	var checker = 0;
	if(id == 3){
		 $("#webshop-address input").each(function(i){
		 	if($(this).val().length == 0){
		 		$(this).addClass("invalid-input");
		 		checker++;
		 	}
		 	else{
		 		$(this).removeClass("invalid-input");
		 	}
		 });
		
		if(checker == 0){
			$("#webshop-menu").hide();
		}
	}
	
	if(checker == 0){
		$("#webshop-menu span").removeClass("webshop-active");
		$("#webshop-menu span").eq(id).addClass("webshop-active");
		$("#webshop-"+id).removeClass("showme").addClass("hideme");
		var next = parseInt(id)+1;
		$("#webshop-"+next).removeClass("hideme").addClass("showme");
		
		
		get_content(next);
	}
	
	/*	
	var page_height = $('#webshop-1').height();
	var new_height = page_height + 100;
	$('#webshop-content').css({height: new_height});
	*/
};

function toggle_prev(event){
	event.preventDefault();
	var id = $(this).attr("id").replace("previous_", "");
	if(id == 1){
		return;
	}
	$("#webshop-menu span").removeClass("webshop-active");
	$("#webshop-menu span").eq(parseInt(id)-2).addClass("webshop-active");
	$("#webshop-"+id).addClass("hideme").removeClass("showme");
	var prev = parseInt(id)-1;
	$("#webshop-"+prev).removeClass("hideme").addClass("showme");
	get_content(prev);

}

function get_content(id){

	switch(id){
		case 2: show_approval(); break;
		case 3: show_address(); break;
		case 4: show_final(); break;
	}
}

function show_approval(){

	$("ul#approval-content").empty();
	var total = 0;
	
	$(".webshop-product").each(function(){
	
		if($(this).find("input").val().length > 0){
			var name = $(this).find(".product-title").text();
			var article = $(this).find(".product-article").text();
			var price = $(this).find(".product-price").html();
			var amount = $(this).find("input").val();
			var sum = parseFloat(price)*parseInt(amount);
			total += sum;
			var content = '<span>'+article+'</span><span>'+name+'</span><span>'+price+'</span><span>'+amount+'</span><span>'+sum+' kr</span>';
			$('<li></li>').html(content).appendTo('ul#approval-content');
		}	
	});
	
	if(total == 0){
		$("ul#approval-head").hide();
		$("#grand_total").hide();
		$("<li>Ingen artikel vald ännu</li>").appendTo("ul#approval-content");
	}
	else{
		$("ul#approval-head").show();
		$("#grand_total").text(total + ' kr').show();
	}
	
	return;
};

function show_address(){
};

function show_final(){
	var data = {};
	var ids = '';
	var amount = '';
	$(".product-input input").each(function(){
		if($(this).val() > 0){
			ids += $(this).attr("name").replace("webid_", "")+",";
			amount += $(this).val()+",";
		}
	});
	
	ids = ids.substr(0,ids.length-1);
	amount = amount.substr(0, amount.length-1);
	
	
	//invoice addresses
	data.invoice_name = $("input[name=invoice_name]").val();
	data.invoice_street = $("input[name=invoice_street]").val();
	data.invoice_zip = $("input[name=invoice_zip]").val();
	data.invoice_city = $("input[name=invoice_city]").val();
	data.invoice_phone = $("input[name=invoice_phone]").val();
	data.invoice_email = $("input[name=invoice_email]").val();
	
	//delivery addresses
	data.delivery_name = $("input[name=delivery_name]").val();
	data.delivery_street = $("input[name=delivery_street]").val();
	data.delivery_zip = $("input[name=delivery_zip]").val();
	data.delivery_city = $("input[name=delivery_city]").val();
	data.delivery_phone = $("input[name=delivery_phone]").val();
	data.delivery_email = $("input[name=delivery_email]").val();
	
	data.personal_message = $("textarea[name=personal_message]").val();
	data.delivery_date = $("input[name=delivery_date]").val();
	data.ids = ids;
	data.amount = amount;
	
	$.ajax({
 		url: "?order=true",
 		type: "POST",
 		data: data,
  		success: function(data){
 
   		}
	});
};

function prevent_alphabetical(event){
	if(event.charCode && (event.charCode < 48 || event.charCode > 57)){
			event.preventDefault();
	}
};

