//------------------------------------------------------------------------------
// File: shop_picker.js
// Desc: Javascript for operating the drop-down product configurator on the
//       shop.php page.
// By:   Gareth Williams, BeVivid
// Date: Jan 2010 
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}
//------------------------------------------------------------------------------

// Prepare references to our drop-down boxes.
var config1_select;
var config2_select;
var config3_select;
$(document).ready( function() {
	config1_select = $('#config1');
	config2_select = $('#config2');
	config3_select = $('#config3');
});

// Prepare a reference to our products table.
var products_table;
$(document).ready( function() {
	products_table = $('#productstablebody');
	add_table_help_label();
	get_config1_list();
});

function get_config1_list()
{
	// Generate our list of available product configurations.
	var config1_list = [];
	for( var i = 0; i < js_prod.length; i++ )
	{
		// Add this product configuration to the list if it isn't already there.
		if( config1_list.indexOf( js_prod[i].config_1 ) == -1 )
		{
			config1_list.push( js_prod[i].config_1 );
		}
	}
	// Populate the drop-down product config box with our configurations.
	config1_select.empty();
	config1_select.append( '<option value="">Select...</option>' );
	for( var i = 0; i < config1_list.length; i++ )
	{
		config1_select.append( '<option value="' + config1_list[i] + '">' + config1_list[i] + '</option>' );
	}
	return config1_list;
}

function get_config2_list( config1 )
{
	// Generate our list of available product configurations.
	var config2_list = [];
	for( var i = 0; i < js_prod.length; i++ )
	{
		// Add this product configuration to the list if it isn't already there.
		if( config2_list.indexOf( js_prod[i].config_2 ) == -1 )
		{
			// But don't add it if its other config fields don't match.
			if( ( config1 && js_prod[i].config_1 != config1 ) )
				continue;
			config2_list.push( js_prod[i].config_2 );
		}
	}
	// Populate the drop-down product config box with our configurations.
	config2_select.empty();
	config2_select.append( '<option value="">Select...</option>' );
	for( var i = 0; i < config2_list.length; i++ )
	{
		config2_select.append( '<option value="' + config2_list[i] + '">' + config2_list[i] + '</option>' );
	}
	return config2_list;
}

function get_config3_list( config1, config2 )
{
	// Generate our list of available product configurations.
	var config3_list = [];
	for( var i = 0; i < js_prod.length; i++ )
	{
		// Add this product configuration to the list if it isn't already there.
		if( config3_list.indexOf( js_prod[i].config_3 ) == -1 )
		{
			// But don't add it if its other config fields don't match.
			if( ( config1 && js_prod[i].config_1 != config1 ) || ( config2 && js_prod[i].config_2 != config2 ) )
				continue;
			config3_list.push( js_prod[i].config_3 );
		}
	}
	// Populate the drop-down product config box with our configurations.
	config3_select.empty();
	config3_select.append( '<option value="">Select...</option>' );
	for( var i = 0; i < config3_list.length; i++ )
	{
		config3_select.append( '<option value="' + config3_list[i] + '">' + config3_list[i] + '</option>' );
	}
	return config3_list;
}

function choose_config1()
{
	config2_select.empty();
	config3_select.empty();
	products_table.empty();
	add_table_help_label();
	if( document.getElementById('config1').value )
		get_config2_list( document.getElementById('config1').value );
}

function choose_config2()
{
	config3_select.empty();
	products_table.empty();
	add_table_help_label();
	if( document.getElementById('config2').value )
		get_config3_list( document.getElementById('config1').value, document.getElementById('config2').value );
}

function choose_config3()
{
	products_table.empty();
	if( document.getElementById('config3').value )
		filter_products( document.getElementById('config1').value, document.getElementById('config2').value, document.getElementById('config3').value );
	else
		add_table_help_label();
}

function filter_products()
{
	// Loop through the list of products and show them in our table.
	products_table.empty();
	var counter = 0;
	for( i = 0; i < js_prod.length; i++ )
	{
		if( document.getElementById('config1').value && js_prod[i].config_1 != document.getElementById('config1').value )
			continue;
		if( document.getElementById('config2').value && js_prod[i].config_2 != document.getElementById('config2').value )
			continue;
		if( document.getElementById('config3').value && js_prod[i].config_3 != document.getElementById('config3').value )
			continue;
		counter++;
		products_table.append( '<tr><td>'
		+ '<label for="basket_productqty_' + counter + '"><em>' + js_prod[i].product_code + '</em> -- <strong>' + js_prod[i].product_name + '</strong></label>'
		+ '</td><td>'
		+ '&pound;' + js_prod[i].config_price.toFixed(2) + ''
		+ '</td><td>'
		+ '<input name="basket_productqty_' + counter + '" type="text" size="4" value="0" />'
		+ '<input name="basket_productid_' + counter + '" type="hidden" value="' + js_prod[i].product_id + '" />'
		+ '<input name="basket_productname_' + counter + '" type="hidden" value="" />'
		+ '<input name="basket_productcode_' + counter + '" type="hidden" value="" />'
		+ '<input name="basket_weight_' + counter + '" type="hidden" value="" />'
		+ '<input name="basket_productprice_' + counter + '" type="hidden" value="" />'
		+ '</td></tr>' );
	}
	// Add a buy button so the user can add their selected products to the basket.
	if( js_prod.length > 0 )
		products_table.append( '<tr><td></td><td></td><td><input name="submit" type="submit" value="" class="buybut" /></td></tr>' );
}
