if (navigator.userAgent.indexOf("Gecko") != -1)
{
	window.addEventListener("load", initPage, false);
	window.addEventListener("load", fixMouseWheel, false);
	window.addEventListener("load", movedown, false);
	window.addEventListener("scroll", setBarRefreshInterval, false);
	window.addEventListener("resize", setBarRefreshInterval, false);
}
else if (window.attachEvent)
{
	window.attachEvent("onload", initPage);
	window.attachEvent("onload", movedown);
	window.attachEvent("onscroll", setBarRefreshInterval);
	window.attachEvent("onresize", setBarRefreshInterval);
}

function gObj(ele) { return document.getElementById(ele); }

function setDelOption(option)
{
	type=option.value;
	rec_del=(type=='recorded')?9.99:0;
	total=parseFloat(gObj("orig_total").value.replace(',',''));
	new_total=total+rec_del;
	gObj("total_amt").innerHTML=CommaFormatted(new_total.toFixed(2));
	gObj("total").value=CommaFormatted(new_total.toFixed(2));
}

function CommaFormatted(amount)
{
	var delimiter = ",";
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function initPage()
{
	initSIFR();
	moveBar();
	updatePlate();
	var rc = document.getElementById("right-column");
	if (rc)
	{
		var tbl = rc.getElementsByTagName("table").item(0);
		if (tbl && tbl.className == "matrix")
		{
			var anchor = document.getElementById("anchor");
			if (anchor)
				anchor.onmouseover = hidePopup;
			var cells = tbl.getElementsByTagName("td");
			for (var i=0; i<cells.length; i++)
			{
				if (cells[i].className != "grey" && cells[i].parentNode.className != "grey")
				{
					if (cells[i].innerHTML == "")
					{
						cells[i].onmouseover = hidePopup;
					}
					else
					{
						cells[i].onmouseover = showPopup;
						cells[i].onclick = holdPopup;
					}
				}
			}
		}
	}
	var f1 = document.getElementById("form1");
	if (f1)
	{
		var sboxes = f1.getElementsByTagName("select");
		for (var i=0; i<sboxes.length; i++)
		{
			sboxes[i].onchange = function () {
					syncForms("form1", "form2");
					updateValue(this, true);
				}
		}
		var a = f1.getElementsByTagName("a").item(0);
		if (a)
			a.onclick = function () {
					var f = document.getElementById("form2");
					if (f)
						f.submit();
					return false;
				}
	}
	var f2 = document.getElementById("form2");
	if (f2)
	{
		var sboxes = f2.getElementsByTagName("select");
		for (var i=0; i<sboxes.length; i++)
		{
			sboxes[i].onchange = function () {
					syncForms("form2", "form1");
					updateValue(this, true);
				}
		}
		var a = f2.getElementsByTagName("a").item(0);
		if (a)
			a.onclick = function () {
					var f = document.getElementById("form2");
					if (f)
						f.submit();
					return false;
				}
	}
	var submitLink = document.getElementById("submit_link");
	if (submitLink)
		submitLink.onclick = function () {
			var f = document.getElementById("submit_form");
			if (f)
			{
				if (f.className == "buy")
					checkBuyForm(f);
				else
					f.submit();
			}
			else if(f2)
				f2.submit();
			return false;
		}
	var resetLink = document.getElementById("reset_link");
	if (resetLink)
		resetLink.onclick = function () {
			var f = document.getElementById("submit_form");
			if (f)
				f.reset();
			else if(f2)
				f2.reset();
			return false;
		}
	var dvlaForm1 = document.forms['form_prefix_search']; //!!!use another method of node selection
	if (dvlaForm1)
	{
		setGoNextHandlers(dvlaForm1, ["_input_suffix1", "_input_suffix2", "_input_suffix3"]);
	}
	var dvlaForm2 = document.forms['form_current_search']; //!!!use another method of node selection
	if (dvlaForm2)
	{
		setGoNextHandlers(dvlaForm2, ["_input_prefix1", "_input_prefix2", "_input_suffix1", "_input_suffix2", "_input_suffix3"]);
	}
	setTimeout("updatePlateContainerWidth()",10);
}

function initSIFR()
{
	if(typeof sIFR == "function")
	{
		sIFR.replaceElement(named({sSelector:".plate-y .char, .plate-y .char-i, .plate-y .space, .plate .char, .plate .char-i, .plate .space", sFlashSrc:"-images/char.swf", sColor:"#000000", sBgColor:"#ffffff", sWmode:"transparent", sCase:"upper"}));
	};
}

function updatePlateContainerWidth()
{
	var c = document.getElementById("select-pate");
	if (c)
	{
		var divs = c.getElementsByTagName("div");
		for (var i = 0; i < divs.length; i++)
		{
			if (divs[i].className == "in-plate-in")
			{
				var w = 0;
				var sc = divs[i];
				var sdivs = sc.getElementsByTagName("div");
				for (var j = 0; j < sdivs.length; j++)
				{
					 w += sdivs[j].offsetWidth;
				}
				sc.style.width = w + "px";
				c.style.visibility = "hidden";
				c.style.visibility = "visible";
			}
		}
	}
	var lc = document.getElementById("spec-plate");
	if (lc)
	{
		var ldivs = lc.getElementsByTagName("div");
		for (var i = 0; i < ldivs.length; i++)
		{
			if (ldivs[i].className == "in-plate-in")
			{
				var lw = 0;
				var lsc = ldivs[i];
				var lsdivs = lsc.getElementsByTagName("div");
				for (var j = 0; j < lsdivs.length; j++)
				{
					 lw += lsdivs[j].offsetWidth;
				}
				lsc.style.width = lw + "px";
				lc.style.visibility = "visible";
			}
		}
	}
	var cp = document.getElementById("car-preview");
	if (cp)
	{
		var cdivs = cp.getElementsByTagName("div");
		for (var i = 0; i < cdivs.length; i++)
		{
			if (cdivs[i].className == "in-plate-in")
			{
				var cw = 0;
				var csc = cdivs[i];
				var csdivs = csc.getElementsByTagName("div");
				for (var j = 0; j < csdivs.length; j++)
				{
					 cw += csdivs[j].offsetWidth;
				}
				csc.style.width = cw + "px";
				cp.style.visibility = "visible";
			}
		}
	}
}

var timer = null;
function moveBar()
{
	var e = document.getElementById("fixedbar");
	var a = document.getElementById("anchor");
	if (e && a)
	{
		e.style.left = getElementX(a) + 160 + "px";
		var ota = getElementY(a);
		if (e.offsetTop < (ota + e.offsetHeight) 
			|| (document.documentElement.scrollTop + document.documentElement.clientHeight) < (ota + e.offsetHeight))
		{
			e.style.top = document.documentElement.scrollTop + document.documentElement.clientHeight - e.offsetHeight + "px";
		}
		if (e.offsetTop > (getElementY(a) + e.offsetHeight) 
			|| (document.documentElement.scrollTop + document.documentElement.clientHeight) > (getElementY(a) + e.offsetHeight))
		{
			e.style.top = getElementY(a) + "px";
		}
		if (timer != null)
		{
			clearInterval(timer);
			timer = null;
		}
	}
}
	
function setBarRefreshInterval()
{
	if (timer == null)
	{
		timer = setInterval("moveBar()", 100);
	}
	else
	{
		clearInterval(timer);
		timer = setInterval("moveBar()", 100);
	}
}

function fixMouseWheel()
{
	setInterval("mousewheel()", 300);
}

var currScrollTop = 0;
function mousewheel()
{
	if (currScrollTop != document.documentElement.scrollTop)
	{
		moveBar();
		currScrollTop = document.documentElement.scrollTop;
	}
}


/*=== popup ===*/

var oldNode;
function showPopup(evt, node)
{
	if (!node)
		node = this;
	var offsetY = -10; // was 23 - causing problems with users popup disappearing...
	if (oldNode)
		oldNode.className = oldNode.className.replace(" highlight", "");
	oldNode = node;
	hidePopup();
	node.className += " highlight";
	var p = document.getElementById("popup");
	if (p)
	{
		var h = document.all ? p.currentStyle.height : document.defaultView.getComputedStyle(p, '').getPropertyValue("height");
		var w = document.all ? p.currentStyle.width : document.defaultView.getComputedStyle(p, '').getPropertyValue("width");
		h = parseInt(h);
		w = parseInt(w);
		
		var number = node.getAttribute("_number");
		if (number)
		{
			var c = p.getElementsByTagName("div").item(0);
			var html = "";
			for (var i = 0; i < number.length; i++)
			{
				var ch = number.charAt(i).toLowerCase();
				if (ch == "_")
					html += "&nbsp;";
				else
					html += '<img src="-images/small-' + ch + '.gif" height="13"/>'
			}
			c.innerHTML = html;
		}
		var price = node.getAttribute("_price");
		if (price)
		{
			var c = p.getElementsByTagName("span")[1];
			c.innerHTML = "&pound;" + price;
			var sp = p.getElementsByTagName("span")[0];
			sp.innerHTML = "&pound;" + Math.round(parseInt(price.replace(',', ''), 10) * 1.10);
		}
		var links = p.getElementsByTagName("a");
		links[0].href = getHref(node, "enquire"); //details
		links[1].href = getHref(node, "plate-preview"); //preview
		links[2].href = getHref(node, "plate-buy"); //buy
		
		p.style.left = getElementX(node) - Math.floor(w / 2) + Math.floor(node.offsetWidth / 2) + "px";
		p.style.top = getElementY(node) - h + offsetY + "px";
		p.style.display = "block";
	}
}

function hidePopup()
{
	var p = document.getElementById("popup");
	if (p)
	{
		p.style.display = "none";
		p.className = "small";
	}
}

function holdPopup()
{
	showPopup(null, this);
	document.getElementById("popup").className = "";
}

function getHref(node, pre)
{
	var table = node.getAttribute("_table");
	var id = node.getAttribute("_id");
	var type = node.getAttribute("_type");
	//alert(table + " " + id + " " + type)
	if (pre && table && id && type)
		return pre + "-" + type + "-" + table + "-" + id + ".html"; 
	else
		return "#";
}

function syncForms(srcForm, destForm)
{
	var sForm = document.forms[srcForm];
	var dForm = document.forms[destForm];
	if (sForm && dForm)
	{
		for (var i=0; i<sForm.elements.length; i++)
		{
			var e = dForm.elements[sForm.elements[i].name];
			if (e)
			{
				e.value = sForm.elements[i].value;
			}
		}
	}
}

function updateValue(sbox, doRefresh)
{
	var id = sbox.name + "-value";
	var span = document.getElementById(id);
	if (span)
	{
		if (sbox.value == "")
		{
			if (sbox.name.indexOf("number") != -1)
			{
				var sn = document.getElementById("second-number");
				if (sn)
					sn.parentNode.removeChild(sn);
			}
			span.innerHTML = "&nbsp;";
			span.className = "space";
		}
		else
		{
			if (sbox.name.indexOf("number") != -1)
			{
				if (sbox.value.length > 1)
				{
					span.innerHTML = sbox.value.charAt(1);
					span.className = (sbox.value.charAt(1) == "1" ? "char-i" : "char");
					var anotherSpan = document.getElementById("second-number");
					if (!anotherSpan)
					{
						anotherSpan = document.createElement("div");
						anotherSpan.setAttribute("id", "second-number");
						span.parentNode.insertBefore(anotherSpan, span);
					}
					anotherSpan.innerHTML = sbox.value.charAt(0);
					anotherSpan.className = (sbox.value.charAt(0) == "1" ? "char-i" : "char");
				}
				else
				{
					var sn = document.getElementById("second-number");
					if (sn)
						sn.parentNode.removeChild(sn);
					span.innerHTML = sbox.value;
					span.className = (sbox.value == "1" ? "char-i" : "char");
				}
			}
			else
			{
				span.innerHTML = sbox.value;
				span.className = "char";
			}
		}
	}
	setTimeout("updatePlateContainerWidth()",10);
	if (doRefresh)
		initSIFR();
}

function updatePlate()
{
	var f = document.getElementById("form2");
	if (f)
	{
		var sboxes = f.getElementsByTagName("select");
		for (var i=0; i<sboxes.length; i++)
		{
			updateValue(sboxes[i]);
		}
		initSIFR();
	}
}

function checkBuyForm(form)
{
	if (form)
	{
		var msg = "";
		var incomplete = false;
		for (var i=0; i<form.elements.length; i++)
		{
			if (form.elements[i].parentNode.className == "req")
			{
				if (form.elements[i].type != "checkbox" 
					&& form.elements[i].type != "radio"
					&& form.elements[i].value == "")
				{
					var label = form.elements[i].parentNode.previousSibling;
					while (label.tagName != "LABEL" && label.tagName != "BODY")
						label = label.previousSibling;
					if (label.tagName == "LABEL")
						msg += "Field \"" + label.innerHTML.replace(":", "") + "\" is required.<br/>";
					incomplete = true;
				}
			}
			/* Commented out by rcs on sep 07 as requested by Martyn
			if(form.elements[i].name == "terms" && !form.elements[i].checked)
			{
				msg += "You must agree to the terms &amp conditions.<br/>";
				incomplete = true;
			}
			*/
		}
		
		if (incomplete)
		{
			var p = document.getElementById("err-message");
			if (!p)
			{
				p = document.createElement("p");
				//p.setAttribute("id", "err-message");
				p.id = "err-message";
				form.insertBefore(p, form.firstChild);
			}
			p.className = "warning";
			p.innerHTML = msg;
			document.location.href = "#submit_form";
		}
		else
			form.submit();
	}
}

function setGoNextHandlers(form, inputs)
{
	for (var i=0; i<inputs.length; i++)
	{
		var input = form.elements[inputs[i]];
		if (input && input.tagName == "INPUT")
		{
			input.nextElement = (inputs.length > (i + 1)) ? form.elements[inputs[i + 1]] : false;
			input.onkeyup = function () {
					if (this.nextElement && this.value.length == 1 && this.value.match(/[A-Z]/i))
					{
						try
						{
							this.nextElement.focus();
						}
						catch (e) {}
					}
				}
		}
	}
}

/*=== common functions ===*/
function getElementX(object) {return getElementC(object, true)}
function getElementY(object) {return getElementC(object, false)}

function getElementC(element, xAxis)
{
	var initialElement = element;
	var c = 0;

	while (element != null)
	{
		c += (xAxis) ? element.offsetLeft : element.offsetTop;
		window.statusText = element.tagName
		if (element.style.position == "absolute")
			break;
		else
			element = element.offsetParent;
	}

	var elementWnd = document.window;
	if (!elementWnd) return c;

	if (!elementWnd.frameElement) return c;

	return c + getElementC(elementWnd.frameElement, xAxis);
}	


/*----- scroller ------*/

var contentheight = 0;
var containerheight = 0;
var freeze = false;

function movedown()
{
	if (freeze) return;
	var crossobj=document.getElementById? document.getElementById("content") : document.all.content;
	var crossparent=document.getElementById? document.getElementById("container") : document.all.content;
	if(crossobj && crossparent && containerheight == 0 && contentheight == 0)
	{
		crossparent.onmouseover = function () {
				if (movedownvar)
					clearTimeout(movedownvar);
				freeze = true;
			}
		crossparent.onmouseout = function () {
				if (movedownvar)
					clearTimeout(movedownvar);
				freeze = false; 
				movedownvar=setTimeout("movedown()",20);
			}

		containerheight = crossparent.offsetHeight;
		contentheight=crossobj.offsetHeight;
		if (contentheight > containerheight)
			crossobj.innerHTML += crossobj.innerHTML;
	}
	
	var speed=1;
	if (contentheight > containerheight)
	{
		if (window.moveupvar) {clearTimeout(moveupvar)}
		if (parseInt(crossobj.style.top)>=(contentheight*(-1))) {crossobj.style.top=parseInt(crossobj.style.top)-speed+"px";}
			else {crossobj.style.top=-2+"px";}
		movedownvar=setTimeout("movedown()",70);
	}
}




/**
 * SWFObject v1.4.4: Flash Player detection and embed - http://blog.deconcept.com/swfobject/
 *
 * SWFObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * **SWFObject is the SWF embed script formerly known as FlashObject. The name was changed for
 *   legal reasons.
 */
if(typeof deconcept=="undefined"){var deconcept=new Object();}
if(typeof deconcept.util=="undefined"){deconcept.util=new Object();}
if(typeof deconcept.SWFObjectUtil=="undefined"){deconcept.SWFObjectUtil=new Object();}
deconcept.SWFObject=function(_1,id,w,h,_5,c,_7,_8,_9,_a,_b){if(!document.getElementById){return;}
this.DETECT_KEY=_b?_b:"detectflash";
this.skipDetect=deconcept.util.getRequestParameter(this.DETECT_KEY);
this.params=new Object();
this.variables=new Object();
this.attributes=new Array();
if(_1){this.setAttribute("swf",_1);}
if(id){this.setAttribute("id",id);}
if(w){this.setAttribute("width",w);}
if(h){this.setAttribute("height",h);}
if(_5){this.setAttribute("version",new deconcept.PlayerVersion(_5.toString().split(".")));}
this.installedVer=deconcept.SWFObjectUtil.getPlayerVersion();
if(c){this.addParam("bgcolor",c);}
var q=_8?_8:"high";
this.addParam("quality",q);
this.setAttribute("useExpressInstall",_7);
this.setAttribute("doExpressInstall",false);
var _d=(_9)?_9:window.location;
this.setAttribute("xiRedirectUrl",_d);
this.setAttribute("redirectUrl","");
if(_a){this.setAttribute("redirectUrl",_a);}};
deconcept.SWFObject.prototype={setAttribute:function(_e,_f){
this.attributes[_e]=_f;
},getAttribute:function(_10){
return this.attributes[_10];
},addParam:function(_11,_12){
this.params[_11]=_12;
},getParams:function(){
return this.params;
},addVariable:function(_13,_14){
this.variables[_13]=_14;
},getVariable:function(_15){
return this.variables[_15];
},getVariables:function(){
return this.variables;
},getVariablePairs:function(){
var _16=new Array();
var key;
var _18=this.getVariables();
for(key in _18){_16.push(key+"="+_18[key]);}
return _16;},getSWFHTML:function(){var _19="";
if(navigator.plugins&&navigator.mimeTypes&&navigator.mimeTypes.length){
if(this.getAttribute("doExpressInstall")){
this.addVariable("MMplayerType","PlugIn");}
_19="<embed type=\"application/x-shockwave-flash\" src=\""+this.getAttribute("swf")+"\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\"";
_19+=" id=\""+this.getAttribute("id")+"\" name=\""+this.getAttribute("id")+"\" ";
var _1a=this.getParams();
for(var key in _1a){_19+=[key]+"=\""+_1a[key]+"\" ";}
var _1c=this.getVariablePairs().join("&");
if(_1c.length>0){_19+="flashvars=\""+_1c+"\"";}_19+="/>";
}else{if(this.getAttribute("doExpressInstall")){this.addVariable("MMplayerType","ActiveX");}
_19="<object id=\""+this.getAttribute("id")+"\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\">";
_19+="<param name=\"movie\" value=\""+this.getAttribute("swf")+"\" />";
var _1d=this.getParams();
for(var key in _1d){_19+="<param name=\""+key+"\" value=\""+_1d[key]+"\" />";}
var _1f=this.getVariablePairs().join("&");
if(_1f.length>0){_19+="<param name=\"flashvars\" value=\""+_1f+"\" />";}_19+="</object>";}
return _19;
},write:function(_20){
if(this.getAttribute("useExpressInstall")){
var _21=new deconcept.PlayerVersion([6,0,65]);
if(this.installedVer.versionIsValid(_21)&&!this.installedVer.versionIsValid(this.getAttribute("version"))){
this.setAttribute("doExpressInstall",true);
this.addVariable("MMredirectURL",escape(this.getAttribute("xiRedirectUrl")));
document.title=document.title.slice(0,47)+" - Flash Player Installation";
this.addVariable("MMdoctitle",document.title);}}
if(this.skipDetect||this.getAttribute("doExpressInstall")||this.installedVer.versionIsValid(this.getAttribute("version"))){
var n=(typeof _20=="string")?document.getElementById(_20):_20;
n.innerHTML=this.getSWFHTML();return true;
}else{if(this.getAttribute("redirectUrl")!=""){document.location.replace(this.getAttribute("redirectUrl"));}}
return false;}};
deconcept.SWFObjectUtil.getPlayerVersion=function(){
var _23=new deconcept.PlayerVersion([0,0,0]);
if(navigator.plugins&&navigator.mimeTypes.length){
var x=navigator.plugins["Shockwave Flash"];
if(x&&x.description){_23=new deconcept.PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/,"").replace(/(\s+r|\s+b[0-9]+)/,".").split("."));}
}else{try{var axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");}
catch(e){try{var axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
_23=new deconcept.PlayerVersion([6,0,21]);axo.AllowScriptAccess="always";}
catch(e){if(_23.major==6){return _23;}}try{axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash");}
catch(e){}}if(axo!=null){_23=new deconcept.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));}}
return _23;};
deconcept.PlayerVersion=function(_27){
this.major=_27[0]!=null?parseInt(_27[0]):0;
this.minor=_27[1]!=null?parseInt(_27[1]):0;
this.rev=_27[2]!=null?parseInt(_27[2]):0;
};
deconcept.PlayerVersion.prototype.versionIsValid=function(fv){
if(this.major<fv.major){return false;}
if(this.major>fv.major){return true;}
if(this.minor<fv.minor){return false;}
if(this.minor>fv.minor){return true;}
if(this.rev<fv.rev){
return false;
}return true;};
deconcept.util={getRequestParameter:function(_29){
var q=document.location.search||document.location.hash;
if(q){var _2b=q.substring(1).split("&");
for(var i=0;i<_2b.length;i++){
if(_2b[i].substring(0,_2b[i].indexOf("="))==_29){
return _2b[i].substring((_2b[i].indexOf("=")+1));}}}
return "";}};
deconcept.SWFObjectUtil.cleanupSWFs=function(){if(window.opera||!document.all){return;}
var _2d=document.getElementsByTagName("OBJECT");
for(var i=0;i<_2d.length;i++){_2d[i].style.display="none";for(var x in _2d[i]){
if(typeof _2d[i][x]=="function"){_2d[i][x]=function(){};}}}};
deconcept.SWFObjectUtil.prepUnload=function(){__flash_unloadHandler=function(){};
__flash_savedUnloadHandler=function(){};
if(typeof window.onunload=="function"){
var _30=window.onunload;
window.onunload=function(){
deconcept.SWFObjectUtil.cleanupSWFs();_30();};
}else{window.onunload=deconcept.SWFObjectUtil.cleanupSWFs;}};
if(typeof window.onbeforeunload=="function"){
var oldBeforeUnload=window.onbeforeunload;
window.onbeforeunload=function(){
deconcept.SWFObjectUtil.prepUnload();
oldBeforeUnload();};
}else{window.onbeforeunload=deconcept.SWFObjectUtil.prepUnload;}
if(Array.prototype.push==null){
Array.prototype.push=function(_31){
this[this.length]=_31;
return this.length;};}
var getQueryParamValue=deconcept.util.getRequestParameter;
var FlashObject=deconcept.SWFObject;
var SWFObject=deconcept.SWFObject;

