// $Id: our_wines.js, marina Exp $

function display_selected_cat(catid)
{
    if (catid == 1 || catid == 2 || catid == 3) {
        for(var i = 0; i < products_array.length; i++) {
            if(Number(products_array[i][4]) == Number(catid) && document.getElementById('table_'+products_array[i][0])) { 
                document.getElementById('table_'+products_array[i][0]).style.display = "";
            }
            else {
                document.getElementById('table_'+products_array[i][0]).style.display = "none";
            }
        }
    }
    else if (catid == 'all'){
        for(var i = 0; i < products_array.length; i++) {
            document.getElementById('table_'+products_array[i][0]).style.display = "";
        }
    }
}

function check_subtotal() 
{
    var qty, subtotal, product_cost;

    qty = 0;
    subtotal =0;

    for(var x = 0; x < products_array.length; x++) {
        if(products_array[x][1] == 'Y') {
            qty = qty + Number(document.getElementById('qty_'+products_array[x][0]).value);
        }
    }


	for(var x = 0; x < products_array.length; x++) {
		if(products_array[x][1] == 'Y') {

			if (parseInt(bottles_in_cart)+parseInt(qty) >= parseInt(package_qty)) {

				if (document.getElementById('package_'+products_array[x][0]))
					document.getElementById('package_'+products_array[x][0]).style.font = "bold 11px Arial";

				if (document.getElementById('single_bottle_'+products_array[x][0]))
					document.getElementById('single_bottle_'+products_array[x][0]).style.font = "normal 11px Arial";

			} else {

				if (document.getElementById('package_'+products_array[x][0]))
					document.getElementById('package_'+products_array[x][0]).style.font = "normal 11px Arial";

				if (document.getElementById('single_bottle_'+products_array[x][0]))
					document.getElementById('single_bottle_'+products_array[x][0]).style.font = "bold 11px Arial";


			}

		}
	}


    for(var t = 0; t < products_array.length; t++) {
        if(parseInt(bottles_in_cart)+parseInt(qty) >= parseInt(package_qty) && products_array[t][1] == 'Y') {
           subtotal = subtotal + Number(document.getElementById('qty_'+products_array[t][0]).value) * products_array[t][3];
           product_cost = Number(document.getElementById('qty_'+products_array[t][0]).value) * products_array[t][3];
            if (Number(document.getElementById('qty_'+products_array[t][0]).value) > 0){
                if (document.getElementById('package_'+products_array[t][0]))
                    document.getElementById('package_'+products_array[t][0]).style.font = "bold 11px Arial";
                if (document.getElementById('single_bottle_'+products_array[t][0]))
                    document.getElementById('single_bottle_'+products_array[t][0]).style.font = "normal 11px Arial";
            }
        }
        else {
           subtotal = subtotal + Number(document.getElementById('qty_'+products_array[t][0]).value) * products_array[t][2];
           product_cost = Number(document.getElementById('qty_'+products_array[t][0]).value) * products_array[t][2];
            if (Number(document.getElementById('qty_'+products_array[t][0]).value) > 0){
               if (document.getElementById('single_bottle_'+products_array[t][0]))
                    document.getElementById('single_bottle_'+products_array[t][0]).style.font = "bold 11px Arial";
                if (document.getElementById('package_'+products_array[t][0]))
                    document.getElementById('package_'+products_array[t][0]).style.font = "normal 11px Arial";

            }
        }
        document.getElementById('cost_'+products_array[t][0]).innerHTML = currency_symbol+price_format(product_cost,2);
    }
    if (document.getElementById('number_of_bottles'))
        document.getElementById('number_of_bottles').innerHTML = qty;
    if (document.getElementById('package_subtotal'))
        document.getElementById('package_subtotal').innerHTML = currency_symbol+price_format(subtotal,2);
    if (document.getElementById('number_of_bottles_2'))
        document.getElementById('number_of_bottles_2').innerHTML = qty;
    if (document.getElementById('package_subtotal_2'))
        document.getElementById('package_subtotal_2').innerHTML = currency_symbol+price_format(subtotal,2);
    if (document.getElementById('message_for_package')) {
        var message_qty = package_qty - qty - parseInt(bottles_in_cart);
        if (message_qty > 0)
            document.getElementById('message_for_package').innerHTML = "You need only order "+message_qty+" more bottles to save even more money off our wines and benefit from our "+package_qty+" bottle price";
        else 
            document.getElementById('message_for_package').innerHTML = "12 or more bottles selected. You now have the lower 12-bottle price";

    }

}
function price_format(price, precision) {
var x, cnt, top, botom;
    precision = Math.pow(10, precision);
    price = Math.round(price*precision)/precision;
    top = Math.floor(price);
    bottom = Math.round((price-top)*precision)+precision;
    top = top+"";
    bottom = bottom+"";
    cnt = 0;
    for(x = top.length; x >= 0; x--) {
        if(cnt%3 == 0 && cnt > 0 && x > 0)
            top = top.substr(0,x)+","+top.substr(x,top.length);
        cnt++;
    }
    price = top+"."+bottom.substr(1,bottom.length);
    return price;
}
