function formFocus(theForm, theCurrent, theDesired) {
	if (theForm.value == theCurrent) {
		theForm.value = theDesired;
	}
}

function open_window(fileName, windowName, windowWidth, windowHeight, scrollbars, resizable) {
	window.open(fileName, windowName, 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=' + scrollbars + ', resizable=' + resizable + ', left=' + centre(windowWidth, screen.availWidth) + ', top=' + centre(windowHeight, screen.availHeight) + ', width=' + windowWidth + ', height=' + windowHeight);
}

function centre(size,area) {
	return (area/2)-(size/2);
}

function runShow() {
	theSlides[j].fade();
	j++;
	if (j == theSlides.length) j = 0; 
	theSlides[j].appear();
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function changeImage(theValue, theSelect) {
	$$('img.opt-' + theSelect).each(function(el) {
		el.hide();
	});
	$$('img#opt-' + theValue).each(function(el) {
		el.show();
	});
}

function swapImage(theImage) {
	var tempLink = $("main-0-image-link").href;
	var tempImage = $("main-0-image").src;
	$("main-0-image-link").href = $("main-" + theImage + "-image-link").href;
	$("main-0-image").src = $("main-" + theImage + "-image").src;
	$("main-" + theImage + "-image-link").href = tempLink;
	$("main-" + theImage + "-image").src = tempImage;
}

var showHide = Class.create();
showHide.prototype = {
	initialize : function(links, holder, location) {
		var loc = document.location.href.match(/#(\w+)/) ? RegExp.$1 : location;
		this.links = links;
		this.holder = holder;
		this.holder.without(loc).each(this.hide.bind(this));
		this.show(loc);
		this.links.each(function(el) {
			var element = el.match(/#(\w.+)/) ? RegExp.$1 : location;
			if (loc == element) el.addClassName('selected');
		});
		this.links.each(this.setup.bind(this));
	},
	setup : function(elm) {
		Event.observe(elm, 'click', this.activate.bindAsEventListener(this), false)
	},
	activate : function(ev) {
		var elm = Event.findElement(ev, "a");
		this.holder.without(elm).each(this.hide.bind(this));
		this.show(this.hash(elm));
		elm.addClassName('selected');
		this.links.without(elm).each(function(el) {
			el.removeClassName('selected');
		});
		ev.stop();
	},
	hash : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	hide : function(elm) {
		$(elm).hide();
		//this.links.without(elm).each(removeClassName('selected'));
	},
	show : function(elm) {
		$(elm).show();
	}
}

function ExtractNum(stringNum) {
	//We're doing string functions to make sure that we're getting the price after "(+" of each option labels
	stringNo = stringNum.slice(stringNum.lastIndexOf("(+"),stringNum.length);
	
	var blnIsNegative = false;
	
	if (stringNo.length < 2) {
		//It didn't find "(+" so it must be "(-"
		stringNo = stringNum.slice(stringNum.lastIndexOf("(-"),stringNum.length);
		blnIsNegative = true;
	}
	if (stringNum.lastIndexOf("(+") == -1 && stringNum.lastIndexOf("(-") == -1) {stringNo = 0;}
	var parsedNo = ""; 
	for(var n=0; n<stringNo.length; n++) {
		var i = stringNo.substring(n,n+1); 
		if(i=="1"||i=="2"||i=="3"||i=="4"||i=="5"||i=="6"||i=="7"||i=="8"||i=="9"||i=="0"||i==".")
			parsedNo += i; 
		if(i==","){
			blnComma = true;
			parsedNo += "."; }
	} 
	
	if (parsedNo.length > 0) {
		if (blnIsNegative) 
			{return '-' + parsedNo;} 
		else 
			{return parsedNo;} 
		} 
	else {return 0;}
}

function checkStock(id,outOfStockItems) {
	// Build up out options selections
	var selections = new Array();
	var selectionCount = 0;
	var numOptionsTotal = 0;
	var numOptionsTotalNoDisc = 0;
	var blnApplyDisc = false;
	
	var theSelect = $$('#options' + id + ' select');
	var theRadio = $$('#options' + id + ' input[type="radio"]');
	var theCheck = $$('#options' + id + ' input[type="checkbox"]');
	var theLabel = $$('#options' + id + ' label');
	var theImage = $$('#options' + id + ' img');
						   
	theSelect.each(function(el) {
		if (el.name.substring(0,6)=='OPT_ID') {
			selections[selectionCount] = el.value;
			selectionCount++;
			numOptionsTotal = numOptionsTotal + parseFloat(ExtractNum(el[el.selectedIndex].text));
			if (el.id.indexOf('OPT_ID93')>-1) {
				if (el[el.selectedIndex].text == 'No Automation') {
					$$('img.opt-113').each(function(el) {
						el.style.visibility = 'hidden';
					});
				} else {
					$$('img.opt-113').each(function(el) {
						el.style.visibility = 'visible';
					});
				}
				theLabel.each(function(elm) {
					if (elm.htmlFor.indexOf('OPT_ID113')>-1) {
						el[el.selectedIndex].text == 'No Automation' ? elm.hide() : elm.show();
					}
				});
				theSelect.each(function(elm) {
					if (elm.id.indexOf('OPT_ID113')>-1) {
						el[el.selectedIndex].text == 'No Automation' ? elm.hide() : elm.show();
					}
				});
				theImage.each(function(elm) {
					if (elm.id.indexOf('OPT_ID113')>-1) {
						el[el.selectedIndex].text == 'No Automation' ? elm.style.visibility = 'hidden' : elm.style.visibility = 'visible';
					}
				});
			}
			if (el.id.indexOf('OPT_ID113')>-1) {
				numOptionsTotalNoDisc = numOptionsTotalNoDisc + parseFloat(ExtractNum(el[el.selectedIndex].text));
			}
		}
	});

	theRadio.each(function(el) {
		if (el.name.substring(0,6)=='OPT_ID') {
			if (el.checked) {
				selections[selectionCount] = el.value;
				selectionCount++;

				switch (el.value) {
					case '2742':
						blnApplyDisc = true;
						decDiscount = 0.9;
						break;
					case '2743':
						blnApplyDisc = true;
						decDiscount = 0.875;
						break;
					case '2744':
						blnApplyDisc = true;
						decDiscount = 0.85;
						break;
					case '2745':
						blnApplyDisc = true;
						decDiscount = 0.825;
						break;
					case '2746':
						blnApplyDisc = true;
						decDiscount = 0.8;
						break;
					case '2747':
						blnApplyDisc = true;
						decDiscount = 0.775;
						break;
					case '1473':
						blnApplyDisc = true;
						decDiscount = 0.75;
						break;
					case '2748':
						blnApplyDisc = true;
						decDiscount = 0.725;
						break;
					case '2749':
						blnApplyDisc = true;
						decDiscount = 0.7;
						break;
					case '2750':
						blnApplyDisc = true;
						decDiscount = 0.675;
						break;
					case '2256':
						blnApplyDisc = true;
						decDiscount = 0.65;
						break;
					case '2751':
						blnApplyDisc = true;
						decDiscount = 0.625;
						break;
					case '2752':
						blnApplyDisc = true;
						decDiscount = 0.6;
						break;
					case '2753':
						blnApplyDisc = true;
						decDiscount = 0.575;
						break;
					case '2754':
						blnApplyDisc = true;
						decDiscount = 0.55;
						break;
					default:
						theLabel.each(function(ell) {
							if (ell.htmlFor == el.id) {
								numOptionsTotal = numOptionsTotal + parseFloat(ExtractNum(ell.innerHTML));
								if (el.id.indexOf('OPT_ID113')>-1) {
									numOptionsTotalNoDisc = numOptionsTotalNoDisc + parseFloat(ExtractNum(ell.innerHTML))
								}
							}
						});
						break;
				}
			}
		}
	});

	theCheck.each(function(el) {
		if (el.name.substring(0,6)=='OPT_ID') {
			if (el.checked) {
				selections[selectionCount] = el.value;
				selectionCount++;

				theLabel.each(function(ell) {
					if (ell.htmlFor == el.id) {
						numOptionsTotal = numOptionsTotal + parseFloat(ExtractNum(ell.innerHTML));
						numOptionsTotalNoDisc = numOptionsTotalNoDisc + parseFloat(ExtractNum(ell.innerHTML))
					}
				});
			} else {
				selections[selectionCount] = $('options' + id).elements['NOCHECK_' + el.name].value;
				selectionCount++;
			}
		}
	});
	
	var txtPrice = $('txtPrice' + id);
	var origPrice = $('origPrice' + id);
	var rrpPrice = $('rrpPrice' + id);
	var numFixedPlaces = $('numFixedPlaces' + id);
	var numTax = $('numTax' + id);
	var strPrice = "s" + origPrice.value;

	if (blnApplyDisc) {
		txtPrice.value = parseFloat(origPrice.value.replace(',','.')) +  parseFloat(numOptionsTotal) - parseFloat(numOptionsTotalNoDisc)
		txtPrice.value = (parseFloat(txtPrice.value) * decDiscount) + parseFloat(numOptionsTotalNoDisc);
	} else {
		txtPrice.value= parseFloat(origPrice.value.replace(',','.')) +  parseFloat(numOptionsTotal)
	}
	txtPrice.value = (parseFloat(txtPrice.value)*(1/numTax.value)).toFixed(numFixedPlaces.value);
	
	$('extax').replace('<span class="extax" id="extax">&pound;' + parseFloat(txtPrice.value * numTax.value).toFixed(2) + ' (ex VAT)</span>');
	$$('.rrp .value').invoke('update','&pound;' + ((parseFloat(rrpPrice.value) + numOptionsTotal)/numTax.value).toFixed(2));


	var youSave = parseFloat(rrpPrice.value.replace(',','.')) + parseFloat(numOptionsTotal)
	youSave = youSave/numTax.value
	youSave = (youSave - parseFloat(txtPrice.value)).toFixed(2)

	$$('.rrp .yousave').invoke('update','&pound;' + youSave);
	
	selections = (selections.sort());
	var selection = selections.join('-');
	var isOutOfStock = false;
	
	// Does this combination exist in out outofstock array?
	for(i=0; i<outOfStockItems.length; i++) {
		if(outOfStockItems[i]==selection) {
			isOutOfStock = true;
			break;
		}
	}
	
	/*if(isOutOfStock) {
		goOutOfStock(id);
	} else {
		goInStock(id);
	}*/
}

document.observe("dom:loaded", function() {
	$$('a[rel="external"]').each(function(el) {
		el.writeAttribute('target', '_blank');
	});
});

var email_regex = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
