// JavaScript Document

var string;

function calcShopTotal() {

	string = "";
	numbers = "";
	teamname = "";
	sailname = "";
	totalprice = 0;
	totalquantity = 0;

	for(key in groups) {
		numbers = "";
		$(groups[key]).each(function(y) {
			$(sizes).each(function(z) {
				var quantity = $("#" + key + "_" + groups[key][y] + "_size_" + sizes[z]).val();
				var price = groupprices[key][y];
				if(quantity>0) {
					numbers += key + " colour " + groups[key][y] + ' size ' + sizes[z] + ': ' + quantity + ' items \n';
					totalquantity+=1*quantity;
					totalprice+=1*quantity*price;
				}
			});
		});
		if (numbers!="") {
			string += ''+ key + '\n';
			string += numbers + '\n';
		}
	}

	teamname = $("#teamname").val();
	sailname = $("#sailname").val();
	
	if ((teamname!="")|(sailname!="")) {
		string += 'Printed information on team outfit\n';
		totalprice+=totalquantity*printingPrice;
	}

	if (teamname!="") {
		string += 'Team name: '+ teamname + ' (font ' + $("input[@name=teamnamefont]:checked").val() + ')\n';
	}

	if (sailname!="") {
		string += 'Sail name: '+ sailname + ' (font ' + $("input[name=sailnamefont]:checked").val() + ')\n';
	}

	$("#shopdescription").attr("value",string); 
	$("#displayprice").text("€ "+Math.round(totalprice/100) + ",00"); 
	$("#shopprice").attr("value",totalprice);

}


$(document).ready(function(){

    $(".shopsizes input").focus(function () {
      $(this).addClass("fieldfocus");
    });
    $(".shopsizes input").blur(function () {
      $(this).removeClass("fieldfocus");
    });
	
    $("input.namefields").focus(function () {
      $(this).addClass("namefieldsfocus");
    });
    $("input.namefields").blur(function () {
      $(this).removeClass("namefieldsfocus");
    });

    $(".shopsizes input").keyup(calcShopTotal);
    $(".shopsizes input").blur(calcShopTotal);

    $("input#teamname").keyup(calcShopTotal);
    $("input#teamname").blur(calcShopTotal);

    $("input#sailname").keyup(calcShopTotal);
    $("input#sailname").blur(calcShopTotal);

	$(".choose a").click(function () {
      $(this).parent().addClass("hidden");
      $(this).parent().next().removeClass("hidden");
    });

	if ($("#displayprice").length) {
		calcShopTotal();
	}

});


