// Desk/Store code

var PRODUCT_LINE = "product_line";
var TOTAL_PRICE_LINE = "total_price_line";

function setProductData(field, line_id) {
	var get_str = "";

	if($(field).hasClass("shopcode"))
		get_str += "shopcode=";
	else
		get_str += "barcode=";

	get_str += $(field).val();

       	$("#loader" + line_id).html("<img src=\"admin/admin_core/img/loader.gif\"/>");

        $.ajax({
                url: "getProductData.php?" + get_str,
                type: "GET",
                dataType: "json",
                success: function(data) {
	                $("#loader" + line_id).html("");

	                if(data["product_name"] == "" || data["brand_name"] == "")
        	                return;

	                // Set data

        	        var table = $("#product_list");
			var table_rows = table.attr('rows');

	                for(var i = 0;i < table_rows.length;i++) {
        	                var id = table_rows[i].id;

				if(id == null)
					continue;

                	        if(id == (PRODUCT_LINE + line_id)) {
					$("#price" + line_id).val(data["price"]);

					$("#brand_name" + line_id).html(data["brand_name"]);
					$("#product_name" + line_id).html(data["product_name"]);

					var price_str = "&euro; " + parseFloat(data["price"]).toFixed(2).replace(".", ",");
					$("#price_str" + line_id).html(price_str);

					$("#shopcode" + line_id).val(data["shopcode"]);
					$("#barcode" + line_id).val(data["barcode"]);

					$("#color" + line_id).html(data["color"]);
                	        }
                	}

	                setTotalPrice();
			setTotalAmount();

        	        // Add line

                	addLine();
                }
        });
}

function setPrice(amount, line_id) {
	var price = $("#price" + line_id).val();
	var price_str = "&euro; 0,00";

	if(!isNaN(amount) && !isNaN(price)) {
		var price_str = amount * price;
		price_str = "&euro; " + price_str.toFixed(2).replace(".", ",");
	}

	$("#price_str" + line_id).html(price_str);

	setTotalPrice();
}

function setTotalPrice() {
        var table = $("#product_list");
	var table_rows = table.attr('rows');
        var total_price = 0;

        for(var i = 0;i < table_rows.length;i++) {
                var line_id = table_rows[i].id;

                if(line_id != "" && line_id != TOTAL_PRICE_LINE) {
                	line_id = line_id.replace(PRODUCT_LINE, "");
                        var line_price = parseFloat($("#price" + line_id).val()) * $("#amount" + line_id).val();

                        if(!isNaN(line_price))
                                total_price += line_price;
                }
        }

        $("#total_price").html("&euro; " + total_price.toFixed(2).replace(".", ","));
}

function setTotalAmount() {
	var total_amount = 0;

	$.each($(".amount"), function(key, field) {
		total_amount += parseInt($(field).val());
	});

	$("#total_amount").html(total_amount);
}

function deleteLine(line_id) {
        var table = $("#product_list");
	var table_rows = table.attr('rows');

        for(var i = 0;i < table_rows.length;i++) {
                var id = table_rows[i].id;

		if(id == null)
			continue;

                if(id == line_id)
                       $("#" + line_id).remove();
        }

	setTotalAmount();
	setTotalPrice();
}

function addLine() {
        var table = $("#product_list");
	var table_rows = table.attr('rows');
        var product_length = table_rows.length - 2;
        var line_id = 1;

	var total_amount = 1;

	$.each($(".amount"), function(key, field) {
		total_amount += parseInt($(field).val());
	});

	var total_price_str = $("#total_price").html();

	if(total_price_str == null || total_price_str == "")
		total_price_str = "&euro; 0,00";

	$("#" + TOTAL_PRICE_LINE).remove();

        if(product_length > 0)
                line_id = parseInt(table_rows[product_length].id.replace(PRODUCT_LINE, "")) + 1;

	table.append("<tr class=\"" + PRODUCT_LINE + "\" id=\"" + PRODUCT_LINE + line_id + "\">" +
		 "<td width=\"200\" style=\"border-bottom: 1px solid #CCC\">" + 
        	  "<input type=\"hidden\" name=\"price" + line_id + "\" id=\"price" + line_id + "\">" +
        	  "<input type=\"text\" name=\"shopcode" + line_id + "\" id=\"shopcode" + line_id + "\"  class=\"shopcode input_store\" onChange=\"setProductData(this, " + line_id + ");\">" +
		 "</td>" + 
		 "<td style=\"font-size: 15px; border-bottom: 1px solid #CCC\">" + 
        	  "<input type=\"text\" name=\"barcode" + line_id + "\" id=\"barcode" + line_id + "\"  class=\"barcode input_store\" onChange=\"setProductData(this, " + line_id + ");\">" +
		 "</td>" + 
		 "<td style=\"font-size: 15px; border-bottom: 1px solid #CCC\" name=\"brand_name" + line_id + "\" id=\"brand_name" + line_id + "\"></td>" +
		 "<td style=\"font-size: 15px; border-bottom: 1px solid #CCC\" name=\"product_name" + line_id + "\" id=\"product_name" + line_id + "\"></td>" +
		 "<td style=\"font-size: 15px; border-bottom: 1px solid #CCC\" name=\"color" + line_id + "\" id=\"color" + line_id + "\"></td>" +
		 "<td style=\"font-size: 15px; border-bottom: 1px solid #CCC\">" + 
        	  "<input type=\"text\" name=\"amount" + line_id + "\" id=\"amount" + line_id + "\" size=\"5\" class=\"amount input_store\" onKeyUp=\"setPrice(this.value, " + line_id + "); setTotalAmount();\" value=\"1\">" +
		 "</td>" + 
		 "<td name=\"price_str" + line_id + "\" id=\"price_str" + line_id + "\" style=\"font-size: 15px; border-bottom: 1px solid #CCC\">&euro; 0,00</td>" + 
		 "<td style=\"font-size: 15px; border-bottom: 1px solid #CCC\">" + 
        	  " <a href=\"javascript: deleteLine('" + PRODUCT_LINE + line_id + "');\"><img src=\"../admin/admin_core/img/delete.jpg\"></a>" +
		 "</td>" + 
		 "<td id=\"loader" + line_id + "\"></td>" + 
		"</tr>");

	table.append("<tr id=\"" + TOTAL_PRICE_LINE + "\" style=\"font-size: 15px; font-weight: bold; color: #FF0000\">" +
		 "<td></td>" +
		 "<td></td>" +
		 "<td></td>" +
		 "<td></td>" +
		 "<td></td>" +
		 "<td id=\"total_amount\">"+ total_amount + "</td>" + 
		 "<td id=\"total_price\">" + total_price_str + "</td>" + 
		 "<td></td>" +
		"</tr>");

	$("#shopcode" + line_id).focus();
}

function sendData() {

	// Check

	var send_data = false;

        var table = $("#product_list");
        var table_rows = table.attr('rows');
        var total_price = 0;

        for(var i = 0;i < table_rows.length;i++) {
                var line_id = table_rows[i].id;
               	line_id = line_id.replace(PRODUCT_LINE, "");

		var shopcode = $("#shopcode" + line_id).val();
		var barcode = $("#barcode" + line_id).val();
		var amount = parseInt($("#amount" + line_id).val());

		if(((shopcode != null && shopcode != "") || (barcode != null && barcode != "")) && !isNaN(amount)) 
			send_data = true;
	}

	if(!send_data) {
		open_alert("You must set at least one data line!");
		return;
	}

	// Send

        $.ajax({
                url: "setStore.php",
                type: "POST",
                dataType: "json",
                data: $("#deskForm").serialize(),
                success: function(data) {
			$("." + PRODUCT_LINE).remove();
	                addLine();
			setTotalPrice();

        	        // Popup

			open_alert(data["text"]);
                }
        });
}

function set_data_popup() {
        create_popup("#data_popup");
        $("#data_popup").dialog("option", "width", 480);
        $("#data_popup").dialog("option", "height", 140);
        $("#data_popup").bind("click", function() {
                close_popup("#data_popup");
        });
}

// Cart

function cart_getAmount() {
        $.ajax({
                url: "cart_getAmount.php",
                type: "GET",
                dataType: "json",
                success: function(data) {
			var cart_item_amount = data["cart_item_amount"];
			$("#cart_item_amount").html(cart_item_amount);
                }
        });
}

