<!--


//general functions for control of price recalculation
//copyright e-Business Systems Limited - www.e-businesssystems.co.uk

//Special Version adapted for Stephen Einhorn 16/10/04
//revised July 2008
//revised May 2010 re engraving

var basket_form_has_been_submitted = false;
var add_message_timer;

function ebsl_Control_setup2()
{
	ebsl_setup_complete = true;
}






function Count_non_space_letters(text)
{
	var i;
	var len = 0;
	var test;
	var result;
	if (text.length <= 0)
	{
		return len;
		
	}
	
	for (i = 0; i < text.length; i++) 
	{
		
		len += 1;
		test = /[\s\.\,\-'`"]/;
		result = test.exec(text.substring(i,i + 1));
		if ((result != null))
		{
			len -= 1;
		}
		
		
		
	} 
	return len;
}



function Round2(num){
	return Math.round((num+0.0001)*100)/100;
}



function Convert_ntc2(num){
	var curr = num.toString();
	var len = curr.length;
	var x = curr.indexOf(".");
	if (x < 0) curr = curr + ".00";
	else curr += (((len-x) == 2) ? "0" : "");
	x = curr.length
	if (x>7) curr = curr.substring(0,(x-6)) + "," + curr.substring((x-6),x);
	if (x>10) curr = curr.substring(0,(x-9)) + "," + curr.substring((x-9),(x+1));
	return curr;
}



function Comma_insert_in_int(num)
{	
	var curr = num.toString();
	var x = curr.length;
	if (x>3) curr = curr.substring(0,(x-3)) + "," + curr.substring((x-3),x);
	if (x>6) curr = curr.substring(0,(x-6)) + "," + curr.substring((x-6),(x+1));
	return curr;
}


function Se_currency_round(price,exchange_rate)
{
	price = (price * exchange_rate) + 0.000001;
	
	if (exchange_rate != 1)
	{
		price = Math.round(price + 0.4999);
	}
	
	if (Math.abs(price - Math.round(price)) < 0.01)
	{
		price = Comma_insert_in_int(Math.round(price));
	}
	else
	{
		price = Convert_ntc2(Round2(price));
	}
	
	return price;
}


function Recalculate_price2()
{
	
	var i,j,k;
	var price;
	
	
	
	if (there_are_price_bands)
	{
		price = prices.Recalculate(document.form1.metal_choice.value,document.form1.ring_size.value,document.form1.stone_type.value);
	}
	
	
	
	if (!there_are_price_bands)
	{
		price = calculated_price_ex_engraving;
	}
	



	//calculate engraving price
		
	if (engraving)
	{
		
		price += engraving.Engraving_total();
		
	}
	
	
	//rounding and formatting	
	price = Se_currency_round(price,exchange_rate);
	if (modern_browser)
	{
		document.getElementById("price").innerHTML = currency_symbol + price;
	}
			
}









function Validate_form2(form_ref)
{
	
	var test;
	var result;
	
	
	
	Recalculate_price();
	
	if (false)
	{
		Set_submit("recalculate");
	}
	
	if (form_ref.engraving_lettering.value.length > 0)
	{
		if (form_ref.engraving_style.value.toLowerCase() == "not selected")
		{
			window.alert("Please select an engraving style \nor delete the engraving lettering");
			form_ref.engraving_style.focus();
			return false;
		}
		
		
		
		test = /\\/;
		result = test.exec(form_ref.engraving_lettering.value);
		if ((result != null))
		{
			window.alert("We are sorry that for technical reasons our website can't accept the character \"\\\" in the engraving lettering.");
			form_ref.engraving_lettering.focus();
			return false;
		}
		
		
	}
	
	
	Single_submit(form_ref);
	
	
	
	//add to basket
	var d = new Date();
	var t = d.getTime();
	ajax.Make_request("doPost",Add_to_basket_error, 0, 'add_to_basket.asp?t=' + t,formData2QueryString(form_ref),Basket_add_response);
	

	var add_message = Gebid("added_to_basket");
	add_message.innerHTML = "<h3>" + form_ref.product_description.value + "</h3> <p>has been added to your basket</p>";
	
	var add_message_container = Gebid("added_to_basket_container");
	add_message_container.style.display = "block";
	add_message_timer = setTimeout(Add_message_off,(1000 * 2));
	
	
	
	return false;			
}


function Add_message_off()
{
	clearTimeout(add_message_timer);
	var add_message = Gebid("added_to_basket_container");

	add_message.style.display = "none";
	
}
		
  



function Single_submit(form_ref)
{
	//form_ref.submit_basket.disabled = true;	//not working with image submit
	basket_form_has_been_submitted = true;
}
		
  
  
  
function Single_submit_clear(form_ref)
{
	//form_ref.submit_basket.disabled = false;	//not working with image image submit
	basket_form_has_been_submitted = false;
}



function Basket_add_response(str)
{
	
	Single_submit_clear(document.form1);
	
	
	//redisplay the basket panel
	basket.Refresh_request();
	
}







function Add_to_basket_error(str)
{
	Single_submit_clear(document.form1);
	window.alert("error - " + str);
	
	
	//to be done #########################
}





function Bands_control()
{
	this.list_price = 0;
	this.price_band_count = 0;
	this.bands = new Array();
	this.Add_band = Add_new_band;
	this.band_message = "";
	this.Recalculate = Recalculate_prices;
	this.drop_down_ranges = new Array();
	this.drop_down_range_controls = new Array();
	this.Drop_downs_need_refreshing = Check_drop_downs_need_refreshing;
	this.Add_drop_down_range_entry = Add_new_drop_down_range_entry;
	this.Add_drop_down_range_control = Add_new_drop_down_range_control;
}


function Add_new_band(band_no,param_code_type,param_code,price,price_type,size_from,size_to,description)
{
	this.bands[band_no] = new Band(band_no,param_code_type,param_code,price,price_type,size_from,size_to,description);
	
}



function Add_new_drop_down_range_control(id_no,param_code_type,old_value,new_value)
{
	this.drop_down_range_controls[id_no] = new Drop_down_range_control(id_no,param_code_type,old_value,new_value);
	
}




function Add_new_drop_down_range_entry(id_no,param_code_type,param_code,value_1,value_2,value_3)
{
	this.drop_down_ranges[id_no] = new Drop_down_range(id_no,param_code_type,param_code,value_1,value_2,value_3);
	
}




function Band(band_no_x,param_code_type_x,param_code_x,price_x,price_type_x,size_from_x,size_to_x,description_x)
{
	this.band_no = band_no_x;
	this.param_code_type = param_code_type_x;
	this.param_code = param_code_x;
	this.price = price_x;
	this.price_type = price_type_x;
	this.size_from = size_from_x;
	this.size_to = size_to_x;
	this.description = description_x;
	
	
}


function Drop_down_range_control(id_no_x,param_code_type_x,old_value_x,new_value_x)
{
	this.id_no = id_no_x;
	this.param_code_type = param_code_type_x;
	this.old_value = old_value_x;
	this.new_value = new_value_x;
}



function Drop_down_range(id_no_x,param_code_type_x,param_code_x,value_1_x,value_2_x,value_3_x)
{
	this.id_no = id_no_x;
	this.param_code_type = param_code_type_x;
	this.param_code = param_code_x;
	this.value_1 = value_1_x;
	this.value_2 = value_2_x;
	this.value_3 = value_3_x;
	
}




function Check_drop_downs_need_refreshing(param_code_type,new_value)
{
	var i;
	var control_index = -1;
	var needs_refreshing = true;
	var old_from = "";
	var old_to = "";
	var new_from = "zzzz";
	var new_to = "zzzz";
	
	
	
	for (i = 0; i < this.drop_down_range_controls.length; i++)
	{
		
		if ((param_code_type == this.drop_down_range_controls[i].param_code_type) ) 
		{
			control_index = i;
			this.drop_down_range_controls[i].old_value = this.drop_down_range_controls[i].new_value;
			this.drop_down_range_controls[i].new_value = new_value;
		}
	
	}
	
	
	if (control_index >=0)
	{
		for (i = 0; i < this.drop_down_ranges.length; i++)
		{
			
			if ((param_code_type == this.drop_down_ranges[i].param_code_type) ) 
			{
				if (this.drop_down_range_controls[control_index].old_value == this.drop_down_ranges[i].param_code)
				{
					old_from = this.drop_down_ranges[i].value_1;
					old_to = this.drop_down_ranges[i].value_2;
				}
				
				if (this.drop_down_range_controls[control_index].new_value == this.drop_down_ranges[i].param_code)
				{
					new_from = this.drop_down_ranges[i].value_1;
					new_to = this.drop_down_ranges[i].value_2;
				}
			}
		
		}
		
		
		if ((old_from == new_from) && (old_to == new_to))
		{
			needs_refreshing = false;
		}
	}
	
	return needs_refreshing;
	
}





function Recalculate_prices(metal,ring_size,stone)
{
	
	var i;
	var price = 0;
	var found_metal_price = false;
	var found_stone_price = false;
	
	for (i = 0; i < this.price_band_count; i++)
	{
		
		if (("metals" == this.bands[i].param_code_type) && (metal == this.bands[i].param_code) && (ring_size >= this.bands[i].size_from) && (ring_size <= this.bands[i].size_to)) 
		{
			price = this.bands[i].price;
			found_metal_price = true;
		
		}
		
		if ((stone != "") && ("stone_type" == this.bands[i].param_code_type) && (stone == this.bands[i].description) && (!found_stone_price) ) 
		{
			price += this.bands[i].price;
			found_stone_price = true;
		
		}
		
		
	}
	
	
	if (!found_metal_price)
	{
		price = this.list_price;
	}
	
	if ((!found_stone_price) && (stone != ""))
	{
		price = this.list_price;
	}
	
	return price;

}






















//-----------  product image control ----------------------




var picture_list;






function Picture_evaluate(dropdown_changed)
{
	var metal_dropdown_ref = Gebid("metal_choice");
	var stone_dropdown_ref = Gebid("stone_type");
	var main_product_image_ref = Gebid("main_product_image");
	var bigger_pic_image_ref = Gebid("pic_1");
	var product_description_ref_1 = Gebid("product_title_1");
	var product_description_ref_2 = Gebid("product_description");

	
	
	var picture_clarification_ref = Gebid("picture_clarification");
	var selected_metal = "";
	var selected_metal_description = "";

	var selected_stone = "";
	var new_product_description = description_original;
	
	if (metal_dropdown_ref)
	{
		if (metal_dropdown_ref.options)
		{
			selected_metal = metal_dropdown_ref.options[metal_dropdown_ref.selectedIndex].value;
			selected_metal_description = metal_dropdown_ref.options[metal_dropdown_ref.selectedIndex].text;

		}
	}
	
	if (stone_dropdown_ref)
	{
		if (stone_dropdown_ref.options)
		{	
			selected_stone = stone_dropdown_ref.options[stone_dropdown_ref.selectedIndex].value;
			
		}
	}
	
	
	
	var new_image = picture_list.Select_image(selected_metal,selected_stone,dropdown_changed);
	picture_clarification_ref.innerHTML = picture_list.picture_mismatch_message;
	
	
	main_product_image_ref.src = (main_product_image_ref.src).substr(0,(main_product_image_ref.src).indexOf("large_images/",0) + 13) + new_image + ".jpg"; 
	bigger_pic_image_ref.src = (bigger_pic_image_ref.src).substr(0,(bigger_pic_image_ref.src).indexOf("xl/",0) + 3) + new_image + ".jpg"; 
	
	
	
	if ((selected_metal_description !="") )
	{
		new_product_description =  new_product_description + " " + selected_metal_description;
	}
	

	if ((selected_stone !="") && (selected_stone.toLowerCase() != "not selected") && (selected_stone.toLowerCase() != " not selected"))
	{
		
		var new_stone_desc = selected_stone.replace(/ approx .+ct/i," ");
		
		new_product_description = new_product_description + " & " + new_stone_desc;
	}
	
	product_description_ref_1.innerHTML = new_product_description;
	product_description_ref_2.value = new_product_description;

}






//--------------------------------
function Ebsl_picture_list_control(default_pic)
{
	//copyright e-Business Systems Limited - www.e-businesssystems.co.uk
	this.default_pic = default_pic.substr(0,default_pic.length - 4);
	this.current_pic = this.default_pic;
	this.picture_items = new Array();	//list of picture items
	this.code_translation_items = new Array();	//list of code translation items
	this.current_selected_item = -1;
	this.picture_mismatch_message = "";	//this explains if the picture is not a perfect match to the metal/stone selected
	this.best_match_metal = "";
	this.best_match_stone = "";
	this.stone_from_best_match_metal = "";
	this.metal_from_best_match_stone = "";
	this.best_match_metal_pic = "";
	this.best_match_stone_pic = "";
	this.metal_is_matched = false;
	this.stone_is_matched = false;
	this.dropdown_changed = ""; //set to "s" or "m" depending on which dropdown has just been changed
	 
	/* functions:
	Add_picture_item
	Add_code_translation_item
	Lookup_code
	Select_image
	Select_best_metal_match
	Metal_description
	Stone_description
	*/

}





Ebsl_picture_list_control.prototype.Stone_description = function(stone_code)
{
	var i;
	var stone_desc = "";
	
	
	if (stone_code == "DIA")
	{
		stone_desc = " The stone shown in the picture is a Diamond."
	}
	else
	{
		for (i = 0; i < this.code_translation_items.length; i++)
		{
			if (this.code_translation_items[i].code == stone_code)
			{
				stone_desc = " The stone shown in the picture is a " + this.code_translation_items[i].dropdown_value + ".";
				break;
			}
		}
	}
	
	
	
	
		
	return stone_desc;

	
}




Ebsl_picture_list_control.prototype.Metal_description = function(metal_code)
{
	var metal_desc = " The metal shown in the picture is ";
	
	switch (metal_code)
	{
		case "S":
				metal_desc += "Silver" + ".";
				break;
		
		case "W":
				metal_desc += "White Gold" + ".";
				break;
		
		case "R":
				metal_desc += "Red Gold" + ".";
				break;
		
		case "P":
				metal_desc += "Platinum" + ".";
				break;
		case "Y":
				metal_desc += "Yellow Gold" + ".";
				break;
				
		case "T":
				metal_desc += "Yellow Gold" + ".";
				break;
				
		case "A":
				metal_desc += "Aluminium" + ".";
				break;
				
		case "B":
				metal_desc += "Brass" + ".";
				break;
				
		case "Z":
				metal_desc += "Zinc" + ".";
				break;
				
		case "M":
				metal_desc += "Mixed Metals" + ".";
				break;
				
		case "L":
				metal_desc += "Titanium" + ".";
				break;
				
		
				
		default:
				metal_desc = "";
				break;
		
	}
	
	return metal_desc;
}







Ebsl_picture_list_control.prototype.Select_best_metal_match = function(pic_index)
{
	this.best_match_metal_pic = this.picture_items[pic_index].pic_code;
	this.best_match_metal = this.picture_items[pic_index].metal;
	this.stone_from_best_match_metal = this.picture_items[pic_index].stone;
}



Ebsl_picture_list_control.prototype.Select_image = function(selected_metal,selected_stone,dropdown_changed)
{
	
	
	this.dropdown_changed = dropdown_changed;
	
	var metal_code = this.Lookup_code(selected_metal);
	var stone_code = this.Lookup_code(selected_stone);
	
	this.best_match_metal = "";
	this.best_match_stone = "";
	this.stone_from_best_match_metal = "";
	this.metal_from_best_match_stone = "";
	this.best_match_metal_pic = "";
	this.best_match_stone_pic = "";
	this.metal_is_matched = false;
	this.stone_is_matched = false;
	
	var i;
	var found = "";
	
	//first look for exact match
	for (i = 0; i < this.picture_items.length; i++)
	{
		if ((this.picture_items[i].metal == metal_code) && (this.picture_items[i].stone == stone_code))
		{
			//exact match
			found = this.picture_items[i].pic_code;
			this.metal_is_matched = true;
			this.stone_is_matched = true;
			
			break;
		}
		else
		{
			//see if this is the next best match so far
			if (!this.metal_is_matched)
			{
				if (this.picture_items[i].metal == metal_code) 
				{
					this.metal_is_matched = true;
					this.Select_best_metal_match(i);
				}
				else 
				{
					if (this.best_match_metal_pic == "")
					{
						this.Select_best_metal_match(i);
					}
					else
					{
						
						switch (metal_code)
						{
							case "S":
									switch (this.picture_items[i].metal)
									{
										case "W":
												this.Select_best_metal_match(i);
												break;
								
											
											
										case "P":
												if ("W".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												break;
											
											
											
										case "Y":
												if ("WP".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												break;
											
											
											
										case "R":
												if ("WPR".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												break;
										
										
										
									}
									break;
								
								
								
							case "W":
									switch (this.picture_items[i].metal)
									{
										case "S":
												this.Select_best_metal_match(i);
												break;
								
											
											
										case "P":
												if ("S".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												
												break;
											
											
											
										case "Y":
												if ("SP".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												
												break;
											
											
											
										case "R":
												if ("SPY".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												break;
										
										
										
									}
									break;
								
								
								
							case "R":
									switch (this.picture_items[i].metal)
									{
										case "Y":
												this.Select_best_metal_match(i);
												break;
								
											
											
										case "W":
												if ("Y".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												
												break;
											
											
											
										case "P":
												if ("YW".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												
												break;
											
											
											
										case "S":
												if ("YWP".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												break;
										
										
										
									}
									break;
								
								
								
							case "P":
									switch (this.picture_items[i].metal)
									{
										case "W":
												this.Select_best_metal_match(i);
												break;
								
											
											
										case "S":
												if ("W".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												
												break;
											
											
											
										case "Y":
												if ("WS".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												
												break;
											
											
											
										case "R":
												if ("WSY".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												break;
										
										
										
									}
									break;
								
								
								
							case "Y":
									switch (this.picture_items[i].metal)
									{
										case "W":
												this.Select_best_metal_match(i);
												break;
								
											
											
										case "R":
												if ("W".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												
												break;
											
											
											
										case "P":
												if ("WR".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												
												break;
											
											
											
										case "S":
												if ("WRP".indexOf(this.best_match_metal,0) < 0)
												{
													this.Select_best_metal_match(i);
												}
												break;
										
										
										
									}
									break;
								
								
								
								
						}
					}
				}
			}
			
			if  (this.picture_items[i].stone == stone_code)
			{
				this.stone_is_matched = true;
				this.best_match_stone_pic = this.picture_items[i].pic_code;
				this.best_match_stone = stone_code;
				this.metal_from_best_match_stone = this.picture_items[i].metal;
			}
			
		}
	}	
	
	
	
	if (found != "")
	{
		this.picture_mismatch_message = "";
	}
	else	
	{
		//then no exact match on metal and stone
		this.picture_mismatch_message = "Please note that the picture displayed is not an exact match to your dropdown selection.";
		
		
		
		
		if ((this.stone_is_matched) && ((this.dropdown_changed == "s")||(!this.metal_is_matched)))
		{
			found = this.best_match_stone_pic;
			this.picture_mismatch_message +=   this.Metal_description(this.metal_from_best_match_stone);
		}
		else if (this.metal_is_matched)
		{
			found = this.best_match_metal_pic;
			this.picture_mismatch_message +=   this.Stone_description(this.stone_from_best_match_metal);
		}
		else if (this.best_match_metal_pic != "")
		{
			found = this.best_match_metal_pic;
			this.picture_mismatch_message +=   this.Metal_description(this.best_match_metal);
			this.picture_mismatch_message +=   this.Stone_description(this.stone_from_best_match_metal);
		}
		else
		{
			found = this.default_pic;
		}
		
		
		
	}
	
	this.current_pic = found;
	return found;
	
	
}






Ebsl_picture_list_control.prototype.Lookup_code = function(dropdown_code)
{
	var i;
	var found = "";
	
	for (i = 0; i < this.code_translation_items.length; i++)
	{
		if (this.code_translation_items[i].dropdown_value == dropdown_code)
		{
			found = this.code_translation_items[i].code;
			break;
		}
	}	
	return found;

	
}





Ebsl_picture_list_control.prototype.Add_code_translation_item = function(dropdown_value,code)
{
	var translation_item = new Ebsl_code_translation_item(dropdown_value,code);
	this.code_translation_items.push(translation_item);
	
}





Ebsl_picture_list_control.prototype.Add_picture_item = function(metal,stone,pic_code)
{
	var picture_item = new Ebsl_picture_item(metal,stone,pic_code);
	this.picture_items.push(picture_item);
	
}









//--------------------------------
function Ebsl_code_translation_item(dropdown_value,code)
{
	//copyright e-Business Systems Limited - www.e-businesssystems.co.uk
	this.dropdown_value = dropdown_value;
	this.code = code;
	this.code_type = ((code.length == 1) ? "M" : "S");
}




//--------------------------------
function Ebsl_picture_item(metal,stone,pic_code)
{
	//copyright e-Business Systems Limited - www.e-businesssystems.co.uk
	this.metal = metal;
	this.stone = stone;
	this.pic_code = pic_code;
	
}


























//-->