<!--
// Copyright e-Business Systems Limited.		www.e-businesssystems.co.uk
// This script may not be used without prior written permission from e-Business Systems and payment of the appropriate licence fee

// version 2 - 14/11/07
//------------------------------------------------------

var ebsl_setup_complete = false;
var selection_panel;


function Ebsl_selection_panel_setup()
{
	ebsl_setup_complete = true;
	selection_panel.Initialise();
}


//------------------------------------------------------


function Selection_panel_control()
{
	this.products = new Array();
	this.products_count = 0;
	this.selections = new Array();
	this.selections_count = 0;
	this.currently_selected_product = -1;
	/* functions:
	Add_lampouch_product
	Add_lamfilm_product
	Add_bscrew_product
	Add_selection
	Initialise
	Reset
	Recalculate
	*/
}

Selection_panel_control.prototype.locked_classname = "selection_locked";
Selection_panel_control.prototype.unlocked_classname = "selection_unlocked";
Selection_panel_control.prototype.please_select_classname = "please_select";
Selection_panel_control.prototype.fixed_entry_classname = "";

Selection_panel_control.prototype.locked_help_function = Help_locked;
Selection_panel_control.prototype.unlocked_help_function = Help_unlocked;
Selection_panel_control.prototype.please_select_help_function = Help_select;
Selection_panel_control.prototype.fixed_entry_help_function = Help_fixed_entry;
Selection_panel_control.prototype.clear_help_function = Help_clear;


Selection_panel_control.prototype.Initialise = function ()
{
	var i,j,k;
	var found;
	var dropdown_index;
	
	//set up links
	for (i = 0; i < this.products.length; i++)
	{
		for (j = 0; j < this.selections.length; j++)
		{
			if (this.products[i].attributes[j] !="")
			{
				found = false;
				for (k = 0; k < this.selections[j].dropdown_list.length; k++)
				{
					if (this.selections[j].dropdown_list[k] == this.products[i].attributes[j])
					{
						this.products[i].dropdown_ref[j] = k;
						dropdown_index = k;
						found = true;
						break;
					}
				}	
				if (!found)
				{
					//add option to end of dropdown
					dropdown_index = this.selections[j].dropdown_list.length;
					this.selections[j].Add_dropdown_entry(this.products[i].attributes[j]);
					this.products[i].dropdown_ref[j] = dropdown_index;
				}
				
				//add link back to product
				this.selections[j].product_links[dropdown_index].push(i);
			}
		}
	}
	
	this.Reset();
	
	
	//set headings
	for (i = 0; i < this.selections.length; i++)
	{
		k = Gebid("selection_title_" + i);
		k.innerHTML = "<h3>" + this.selections[i].title + "</h3>"
	}
}




Selection_panel_control.prototype.Reset = function()
{
	var i,j,k;
	
	if (ebsl_setup_complete)
	{
		for (i = 0; i < this.selections.length; i++)
		{
			
			this.selections[i].Reset();
			this.selections[i].Set_default();
		
		}	
		
		this.Recalculate();
		document.selection_form.qty.value = 1;
	}
	
	
}







Selection_panel_control.prototype.Recalculate = function()
{
	var i,j,k,m,n,p,q,r,s,t;
	var need_to_recalculate;
	var valid;
	var something_has_changed = false;
	
	if (!ebsl_setup_complete)
	{
		return;
	}
	
	
	//set all products as valid
	for (i = 0; i < this.products.length; i++)
	{
		this.products[i].is_valid = true;
	}
	
	
	
	//set all dropdown options as valid
	for (i = 0; i < this.selections.length; i++)
	{
		for (j = 0; j < this.selections[i].dropdown_entry_is_valid.length; j++)
		{
			this.selections[i].dropdown_entry_is_valid[j] = true;
		}
	}
	
	
	//if any dropdown is locked, any products that do not have that attribute should be set as invalid;  
	//(however, in a locked dropdown, show the full set of dropdown options that are not incompatible with other locked dropdowns)
	for (i = 0; i < this.selections.length; i++)
	{
		if (this.selections[i].is_locked)
		{
			for (j = 0; j < this.products.length; j++)
			{
				if (this.products[j].attributes[i] != this.selections[i].current_selected_value)
				{
					this.products[j].is_valid = false;
				}
			}
		}
	}
	
	do
	{
		need_to_recalculate = false;
		
		
		
		
		//if any dropdown entry has no valid products, set it to invalid
		for (i = 0; i < this.selections.length; i++)
		{
			for (j = 0; j < this.selections[i].product_links.length; j++)
			{
				
				if (this.selections[i].dropdown_entry_is_valid[j])
				{
					valid = false;
					for (k = 0; k < this.selections[i].product_links[j].length; k++)
					{
						if (this.products[this.selections[i].product_links[j][k]].is_valid)
						{
							valid = true;
							break;
						}
					}
					if (!valid)
					{
						this.selections[i].dropdown_entry_is_valid[j] = false;
						need_to_recalculate = true;
						
					}
				}
			}
		
		}	
		
		
		if (need_to_recalculate)
		{
			something_has_changed = true;
		}
		
		
		
		// if any product has options  which are not valid, set product to invalid
		for (i = 0; i < this.products.length; i++)
		{
			
			if (this.products[i].is_valid)
			{
				valid = true;
				for (j = 0; j < this.products[i].dropdown_ref.length; j++)
				{
					if (this.products[i].dropdown_ref[j])
					{
						if (!this.selections[j].dropdown_entry_is_valid[this.products[i].dropdown_ref[j]])
						{
							valid = false;
							break;
						}
					}
				}
				if (!valid)
				{
					this.products[i].is_valid = false;
					need_to_recalculate = true;
				}
			}			
			
		}
		
		
	}
	while (need_to_recalculate);
	
	
	
	
	//redisplay options as necessary
	
	var selected_dropdown_has_changed;
	var	please_select;
	var valid;
	for (i = 0; i < this.selections.length; i++)
	{
		
		//count the valid options
		this.selections[i].valid_items_count = 0;
		for (j = 0; j < this.selections[i].dropdown_entry_is_valid.length; j++)
		{
			
			if (this.selections[i].dropdown_entry_is_valid[j])
			{
				this.selections[i].valid_items_count += 1;
			}
		}
		
		
		//clear the current dropdown content
		k = Gebid("selection_dropdown_" + i);
		k.innerHTML = "";
		
		
		//if no valid options, hide the option; otherwise show it
		p = Gebid("selection_" + i);
		p.style.display = (this.selections[i].valid_items_count > 0 ? '' : 'none');
		
		
		//if the currently selected option is not valid,we need a "please select" option
		for (m = 0; m < this.selections[i].dropdown_list.length; m++)
		{
			please_select = true;
			if (this.selections[i].dropdown_list[m] == this.selections[i].current_selected_value)
			{
				if (this.selections[i].dropdown_entry_is_valid[m])
				{
					please_select = false;
					
					break;
				}
			}
		}	
		
		
		
		//if only 1 option and not locked, just display it; 
		if ((this.selections[i].valid_items_count == 1) && (!this.selections[i].is_locked))
		{
			//find the valid option if necessary
			if (please_select)
			{
				for (m = 0; m < this.selections[i].dropdown_entry_is_valid.length; m++)
				{
						if (this.selections[i].dropdown_entry_is_valid[m])
						{
							this.selections[i].current_selected_value = this.selections[i].dropdown_list[m];
							
							break;
						}
					
				}	
			}
			
			k.innerHTML = "<span>" + this.selections[i].current_selected_value + "</span>";
		}
		//otherwise display the options as a dropdown
		else
		{
			
			q = "<select onchange='Selection_changed(event)' size='1' " +  "id='ebsl_drop_" + i + "' name='" + this.selections[i].field_name + "'>";
			
			
			if (please_select)
			{
				q += "<option selected value='Please select' class='red'>Please select</option>";
				this.selections[i].current_selected_value = "Please select";
			}
			
			for (m = 0; m < this.selections[i].dropdown_list.length; m++)
			{
				if (this.selections[i].dropdown_entry_is_valid[m])
				{
					q += "<option" + (this.selections[i].dropdown_list[m] == this.selections[i].current_selected_value ? ' selected' : '') + " value='" + this.selections[i].dropdown_list[m] + "'>" + this.selections[i].dropdown_list[m] + "</option>";
				}
				else if (this.selections[i].is_locked)
				{
					//in a locked dropdown, show the full set of dropdown options that are not incompatible with other locked dropdowns
					//i.e. there is at least one product in the invalid option that is not ruled out by all the other options (apart from this one)
					valid = false;
					var product_ok;
					var this_product_attribute;	//########
					for (s = 0; s < this.selections[i].product_links[m].length; s++)
					{
						product_ok = true;
						//for each valid option apart from this one
						for (j = 0; j < this.selections.length; j++)
						{
							if ((j != i)&&(this.selections[j].valid_items_count > 0))
							{
								//if the other option is locked, product must match the selected attribute
								if (this.selections[j].is_locked)
								{
									if (this.products[this.selections[i].product_links[m][s]].attributes[j] != this.selections[j].current_selected_value)
									{
										product_ok = false;
										break;
									}
								}
								
								//if the other option is not locked, product must match one of the available options
								else
								{
									var found_a_match = false;
									for (t = 0; t < this.selections[j].dropdown_list.length; t++)
									{
										if(this.products[this.selections[i].product_links[m][s]].attributes[j] == this.selections[j].dropdown_list[t])
										{
											if(this.selections[j].dropdown_entry_is_valid[t])
											{
												found_a_match = true;
												break;
											}
										}
									}
									
									if (!found_a_match)
									{
										product_ok = false;
										break;
									}
								}
							}
							
							if (!product_ok)
							{
								break;
							}
						}
						
						if (product_ok)
						{
							valid = true;
							break;
						}
					}
					if (valid)
					{
						q += "<option value='" + this.selections[i].dropdown_list[m] + "'>" + this.selections[i].dropdown_list[m] + "</option>";
					}
				}
			}
			
			q += "</select>";
			
			k.innerHTML = q;
			
			
		}
		
		
		//set the icon image class and help info
		n = Gebid("selection_help_" + i);
		if (this.selections[i].is_locked)
		{
			n.className = this.locked_classname;
			n.onmouseover = this.locked_help_function;
			n.onmouseout = this.clear_help_function;
			n.onclick = Unlock;
			
		}
		else if (please_select)
		{
			if ((this.selections[i].valid_items_count > 1))
			{
				n.className = this.please_select_classname;
				n.onmouseover = this.please_select_help_function;
				n.onmouseout = this.clear_help_function;
			}
			else
			{
				n.className = this.fixed_entry_classname;
				n.onmouseover = this.fixed_entry_help_function;
				n.onmouseout = this.clear_help_function;
			}
			n.onclick = null;
		}
		else
		{
			n.className = this.unlocked_classname;
			n.onmouseover = this.unlocked_help_function;
			n.onmouseout = this.clear_help_function;
			n.onclick = Lock;
		}
		
		
		
		
		
	}	
	
	
	//set the price info
	k = Gebid("selection_price_value");
	k.innerHTML = "";
	this.currently_selected_product = -1;
	
	
	for (i = 0; i < this.products.length; i++)
	{
		if (this.products[i].is_valid)
		{
			var match_found = true;
			for (j = 0; j < this.selections.length; j++)
			{
				if ((this.selections[j].valid_items_count > 0) && (this.selections[j].current_selected_value != this.products[i].attributes[j]))
				{
						match_found = false;
						break;
				}
			}
			if (match_found)
			{
				this.currently_selected_product = i;
				break;
			}
		}
	}
	if (this.currently_selected_product >= 0)
	{
		k.innerHTML = "<span>&pound;" + this.products[this.currently_selected_product].price_ex + "</span><input type='hidden' name='part_code' value='" + this.products[this.currently_selected_product].part_code + "'>";
	}
	else
	{
		k.innerHTML = "<input type='hidden' name='part_code' value=''>";
	}
}





Selection_panel_control.prototype.Add_lampouch_product = function(part_code, description, price_ex, finish, size, thickness, without_carrier, pack_size )
{
	this.products[this.products_count] = new Selection_panel_product(this.products_count,part_code, description, price_ex );
	this.products[this.products_count].attributes[0] = finish;
	this.products[this.products_count].attributes[1] = size;
	this.products[this.products_count].attributes[2] = thickness;
	this.products[this.products_count].attributes[3] = without_carrier;
	this.products[this.products_count].attributes[4] = pack_size;
	
	this.products_count += 1;
}






Selection_panel_control.prototype.Add_lamfilm_product = function(part_code, description, price_ex, finish, thickness, core, width, length, pack_size )
{
	this.products[this.products_count] = new Selection_panel_product(this.products_count,part_code, description, price_ex );
	this.products[this.products_count].attributes[0] = finish;
	this.products[this.products_count].attributes[1] = thickness;
	this.products[this.products_count].attributes[2] = core;
	this.products[this.products_count].attributes[3] = width;
	this.products[this.products_count].attributes[4] = length;
	this.products[this.products_count].attributes[5] = pack_size;
	
	this.products_count += 1;
}






Selection_panel_control.prototype.Add_bscrews_product = function(part_code, description, price_ex, finish, size,  pack_size )
{
	this.products[this.products_count] = new Selection_panel_product(this.products_count,part_code, description, price_ex );
	this.products[this.products_count].attributes[0] = finish;
	this.products[this.products_count].attributes[1] = size;
	this.products[this.products_count].attributes[2] = pack_size;
	
	this.products_count += 1;
}






Selection_panel_control.prototype.Add_selection = function(title,field_name,default_value)
{
	this.selections[this.selections_count] = new Selection_panel_selection(this.selections_count,title,field_name,default_value);
	this.selections_count += 1;
}





//------------------------------------------------------
function Selection_panel_product(id,part_code, description, price_ex)
{
	this.id = id;
	this.part_code = part_code;
	this.description = description;
	this.price_ex = price_ex;	//text
	this.attributes = new Array();
	this.dropdown_ref = new Array();
	this.is_valid = true;
	this.min_so_qty = 1;
	/* 
	for lampouch, attributes are:
	0 = finish;
	1 = size;
	2 = thickness;
	3 = without_carrier;
	4 = pack_size;
	
	for lamfilm, attributes are:
	0 = finish;
	1 = thickness;
	2 = core;
	3 = width;
	4 = length;
	5 = pack_size;
	
	
	for bscrews, attributes are:
	0 = finish;
	1 = size;
	2 = pack_size;
	*/
}



//------------------------------------------------------
function Selection_panel_selection(id,title,field_name,default_value)
{
	this.id = id;
	this.title = title;
	this.field_name = field_name;
	this.default_value = default_value;
	this.dropdown_list = new Array();	//list of dropdown items in sequence
	this.dropdown_entry_is_valid = new Array();		//list of indicators stating whether each dropdown option is valid or not
	this.product_links = new Array; //list of products by dropdown item
	this.is_locked = false;
	this.current_selected_value = "";
	this.valid_items_count = 0;	//set upon recalculation
	
	/* functions:
	Add_dropdown_entry
	Sort_numeric
	Set_default
	Reset
	Lock
	*/
}


Selection_panel_selection.prototype.Lock = function ()
{
	this.is_locked = true;
	//need to recalculate after this
}

Selection_panel_selection.prototype.Unlock = function ()
{
	var i;
	this.is_locked = false;
	
	//need to recalculate after this
}

Selection_panel_selection.prototype.Add_dropdown_entry = function(entry)
{
	this.product_links[this.dropdown_list.length] = new Array();
	this.dropdown_list.push(entry);
	
}

Selection_panel_selection.prototype.Set_default = function()
{
	this.current_selected_value = this.default_value;
}



Selection_panel_selection.prototype.Sort_numeric = function(entry)
{
	this.dropdown_list.sort(function(a,b)
		{
			return ((isInteger(a) && isInteger(b)) ? (a - b) : (isInteger(a) ? -1 : 1));
		});
}



Selection_panel_selection.prototype.Reset = function()
{
	var i;
	this.is_locked = false;
	
	
	for (i = 0; i < this.dropdown_list.length; i++)
	{
		this.dropdown_entry_is_valid[i] = true;
	}	
}

//------------------------------------------------------

function isInteger (s)
{
    var i;

    if (isEmpty(s))
    if (isInteger.arguments.length == 1) return 0;
    else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    return true;
}

function isEmpty(s)
{
    return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
    return ((c >= "0") && (c <= "9"))
}

function Gebid(id)
{
	return document.getElementById(id);
}





function Show_help(obj,help_ref)
{
	if (ebsl_setup_complete)
	{
		
		if (help_ref == "")
		{
			var k = document.getElementById("show_help");
			if (k)
			{
				k.parentNode.removeChild(k);
			}
		}
		else
		{
			var help_text = "";
			var help_title = "";
			var to = "";
			switch (help_ref)
			{
				case "locked":
					help_title = "Locked selection";
					help_text = "The locked symbol indicates that you have specifically selected this option value and that you do not want it to change.  A locked value may narrow down the range of options in other dropdown selections.  If you want to UNLOCK this option so that it does not limit other options, click once on the padlock.";
					break
				case "select":
					help_title = "Please select";
					help_text = "You need to select an item from the choices in the dropdown.";
					break
				case "unlocked":
					help_title = "Unlocked selection";
					help_text = "This selection is not locked.  If you want to LOCK this selection so that it does not change, click on the padlock.  NB locking a selection may limit the range of choices in other dropdown selections.";
					break
				
			}
			
			if (help_text != "")
			{
				
				var help_div_inner = document.createElement('div');
				help_div_inner.id = 'show_help_inner';
				help_div = document.createElement('div');
				help_div.id = 'show_help';
				help_div.appendChild(help_div_inner);
				var help_div_element = document.createElement('h3');
				help_div_element.appendChild(document.createTextNode(help_title));
				help_div_inner.appendChild(help_div_element);
				help_div_inner.appendChild(document.createTextNode(help_text));
				obj.appendChild(help_div);
				
				
			}
		}
	}
}




function Help_unlocked()
{
	Show_help(this,"unlocked");
	this.className = "selection_unlocked_picture";
}

function Help_locked()
{
	Show_help(this,"locked");
}

function Help_select()
{
	Show_help(this,"select");
}

function Help_fixed_entry()
{
	
}


function Help_clear()
{
	Show_help(this,"");
	if (this.className == "selection_unlocked_picture")
	{
		this.className = "selection_unlocked";
	}
}

function Lock()
{
	var i = parseInt(this.id.substr(this.id.length - 1, 1));
	selection_panel.selections[i].Lock();
	selection_panel.Recalculate();
	selection_panel.clear_help_function();
}

function Unlock()
{
	var i = parseInt(this.id.substr(this.id.length - 1, 1));
	selection_panel.selections[i].Unlock();
	selection_panel.Recalculate();
	selection_panel.clear_help_function();
}

function Selection_changed(e)
{
	var field;
	if (!e) var e = window.event;
	if (e.target) field = e.target;
	else if (e.srcElement) field = e.srcElement;
	if (field.nodeType == 3) field = field.parentNode;// handle Safari bug
		

	var i = parseInt(field.id.substr(field.id.length - 1, 1));
	t = field.options[field.selectedIndex].text;
	if (t != "Please select")
	{
		selection_panel.selections[i].current_selected_value = t;
		selection_panel.selections[i].Lock();
		selection_panel.Recalculate();
	}
}



function Buy_selection()
{
	
	
	var part_code = document.selection_form.part_code.value;
	var quantity = document.selection_form.qty.value;
	if (part_code.length > 0)
	{
		
		window.open("quantity_input.asp?quantity=" + quantity + "&part_code=" + escape(part_code) ,"input_quantity_win","width=500,height=500,toolbar=no,status=no,scrollbars=yes,resizable=yes");
	}
	else
	{
		window.alert("Please choose a value \nfrom each of the dropdown selections \nbefore adding the item to your basket");
	}
	return false;
}

function Buy_selection2()
{
	Buy_selection();
}


function Buy(part_code){
	
		window.open("quantity_input.asp?quantity=1&part_code=" + part_code,"input_quantity_win","width=500,height=500,toolbar=no,status=no");
	
}




//------------------------------------------------------


//-->