var curpic="mainimage";
var curpic1="cont1";
var curpic2="cont2";
var curpic3="cont3";

var functionref=0;
var content=0;
var contentref=0;
var inc=0;

var functionref1=0;
var content1=0;
var contentref1=0;
var inc1=0;

var functionref2=1;
var content2=0;
var contentref2=0;
var inc2=0;

var functionref3=1;
var content3=0;
var contentref3=0;
var inc3=0;

var changematrix=1;

var priceband="";
var sizeprice=0;
var backingprice=0;
var newprice=0;

var winW=0;
var winH=0;
var lpos=0;
var tpos=0;

var aband= new Array(18,20,28,38);

var bband= new Array(22,32,42);

var cband= new Array(32,36,38,45);

var dband= new Array(38,45);


var dk = new Array(0,14,14,14);

var ck = new Array(0,0,3.50,3.50,3.50);

var cartQuant=new Array();
var cartItem=new Array();
var cartCost=new Array();
var cartCode=new Array();
var cartId=new Array();

var val;
var val2;
var t;
var timer_is_on=0;

var code;
var quantity;
var orderval;
var id;

var purchases;

var cartlist;
var Mcartlist;
var totalcartprice=0;
var Mtotalcartprice=0;
var mailcart;

var ordercode;
var ordervalue;
var ukpnp=6.50;
var items;
var mycart;


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curleft,curtop];
	}
}

function viewcart(){
	location='http://www.merlinglass.co.uk/cart.php';
}

function checkout(){
	location='https://www.merlinglass.co.uk/checkout.php';
}

function hideMiniCart(){
	document.getElementById('minicart').style.display='none';
	//matrixfade();
}

function showMiniCart(){
	mycart=readCookie('cart');
	if(mycart){
	
		totalcartprice=0;
		cartlist="";
		for(i=0;i<purchases.length;i++){
			
			var quantity="",prodcolor="",prodsize="",prodname="",prodtype="",glass="",prodfinish="",plate="";
			
			prodfeatures=purchases[i].split(':');
			
			code=prodfeatures[0];
			cartCode[i]=prodfeatures[0];
			cartQuant[i]=prodfeatures[1];
			cartCost[i]=parseFloat(prodfeatures[2]);
			cartId[i]=prodfeatures[3];
			totalcartprice=totalcartprice+cartCost[i];
			prodattributes=code.split('-');
			
			for(n=0;n<prodattributes.length;n++){
				switch(n){
					case 0:
						var prodtype=gettype(prodattributes[0])+' ';
						break;
					case 1:
						var prodname=capitalize(prodattributes[1])+' ';
						break;
					case 2:
						if(prodattributes[2]!='x')
						var prodcolor=getcolor(prodattributes[2])+' ';
						break;
					case 3:
						if(prodattributes[3]!='x')
						prodattributes[3]=='cle/f' ? glass='in Clear Glass ' : glass='in Frosted Glass ';
						break;
					case 4:
						if(prodattributes[4]!='x')
						prodattributes[0]=='gdk' ? plate=getplate(prodattributes[4])+' ' : plate=getcollar(prodattributes[4])+' ';
						break;
					case 5:
						if(prodattributes[5]!='x')
						prodattributes[5]=='pol' ? prodfinish='on Polished ' : prodfinish='on Satin ';
						break;
					case 6:
						if(prodattributes[6]!='x')
						prodsize=prodattributes[6]+' mm ';
						break;
					default:
						break;
				}
			}
			cartItem[i]=prodcolor+prodsize+prodname+prodtype+glass+prodfinish+plate;
			
			cartlist=cartlist+'<div class=cartrow><div class=cartquant>'+cartQuant[i]+'</div><div class=cartItem>'+cartItem[i]+'</div><div class=cartprice>'+cartCost[i].toFixed(2)+'</div></div>';
		}
		revealCart();
	}	
}

function revealCart(){
	winsize();
	findcenter('width=550 height=50');
	if(items==1 && mycart.match(/J-/i)){
		ukpnp=2.50;
	}
	else{
		ukpnp=6.50;
	}
	ordervalue=(totalcartprice+ukpnp);
	
	document.getElementById('minicart').innerHTML='<div class=cartrowtitle>Your Shopping Cart</div>'+cartlist+'<div class=cartrow><div class=cartquant>&nbsp;</div><div class=cartItem>plus UK Carriage</div><div class=cartprice>'+ukpnp.toFixed(2)+'</div></div><div id=cartotal>Total: £ '+ordervalue.toFixed(2)+'</div></div>';
	document.getElementById('minicart').style.left=50+'px';
	document.getElementById('minicart').style.top=50+'px';
	
	document.getElementById('minicart').style.display='block';
	
	//matrixfadeup();
}

function winsize(){
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
 			}
 		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
			}
	}
}


function findcenter(sizestring){
	var myReg= new RegExp("width=(.*) height=(.*)");
	var results= sizestring.match(myReg);
	
	lpos=Math.round(parseFloat((winW-results[1])/2));
	tpos=Math.round(parseFloat((winH-results[2])/2));
}

function capitalize(str){
	var words=str.split(' ');
	var newname="";
	for(p=0;p<words.length;p++){
		neword=words[p].substr(0,1).toUpperCase()+words[p].substr(1,words[p].length);
		newname=newname+neword+' ';
	}
	return(newname);
}

function getcollar(collar){
	switch(collar){
		case 'bra':
			return('Brass Collar');
			break;
		case 'alu':
			return('White Metal Collar');
			break;
		case 'nic':
			return('Nickel Collar');
			break;
		case 'chr':
			return('Chrome Collar');
			break;
		case 'sil':
			return('Silver Collar');
			break;
		default:
			break;
	}
}

function getplate(plate){
	switch(plate){
		case 'bra':
			return('Brass Plate');
			break;
		case 'nic':
			return('Nickel Plate');
			break;
		case 'chr':
			return('Chrome Plate');
			break;
		case 'sil':
			return('Silver Plate');
			break;
		default:
			break;
	}
}

function gettype(type){
	switch(type){
		case 'gdk':
			return('Door Knob');
			break;
		case 'gck':
			return('Cupboard Knob');
			break;
		case 'gv':
			return('Vase');
			break;
		case 'sb':
			return('Scent Bottle');
			break;
		case 'gf':
			return('Fruit');
			break;
		case 'f':
			return('Flower');
			break;
		case 'j':
			return('Jewellery');
			break;
		case 'df':
			return('Door Furniture');
			break;
		default:
			break;
	}
}

function getcolor(color){
	switch(color){
		case 'cle/c':
			return('Clear');
			break;
		case 'amb/c':
			return('Amber');
			break;
		case 'per/c':
			return('Peridot Green');
			break;
		case 'dar/c':
			return('Dark Green');
			break;
		case 'ame/c':
			return('Amethyst Purple');
			break;
		case 'aub/c':
			return('Aubergine Purple');
			break;
		case 'cob/c':
			return('Cobalt Blue');
			break;
		case 'bri/c':
			return('Bright Blue');
			break;
		case 'sap/c':
			return('Sapphire Blue');
			break;
		default:
			break;
	}
}

function checkCookieItems(){
	mycart=readCookie('cart');
	if(mycart){
		purchases = mycart.split(',');
		items = purchases.length;
		var contents = items+' items';
		if(items>0){
			document.getElementById('cartview').style.display='block';
			document.getElementById('cartcontents').innerHTML=contents;
		}
		else{
			document.getElementById('cartview').style.display='none';
		}
	}
}

function checkCookieItems2(){
	mycart=readCookie('cart');
	if(mycart){
		purchases = mycart.split(',');
		items = purchases.length;
		var contents = items+' items';
		if(items>0){
			document.getElementById('cartview').style.display='block';
			document.getElementById('cartcontents').innerHTML=contents;
		}
		else{
			document.getElementById('cartview').style.display='none';
		}
		location='http://www.merlinglass.co.uk/checkout.php';
	}
}

function createCart(){
	mycart=readCookie('cart');
	if(mycart){
		purchases = mycart.split(',');
		items = purchases.length;
		var contents = items+' items';
		if(items>0){
			document.getElementById('cartview').style.display='block';
			document.getElementById('cartcontents').innerHTML=contents;
		}
		else{
			document.getElementById('cartview').style.display='none';
		}
	buildMCart()
	}
	else{
		eraseCookie('cart');
		location='http://www.merlinglass.co.uk/merlin-glass-catalogue.php';
	}
}

function buildMCart(){
	Mtotalcartprice=0;
	Mcartlist="";
	for(i=0;i<purchases.length;i++){
		
		var quantity="",prodcolor="",prodsize="",prodname="",prodtype="",glass="",prodfinish="",plate="";
		
		prodfeatures=purchases[i].split(':');
		
		code=prodfeatures[0];
		cartCode[i]=prodfeatures[0];
		cartQuant[i]=prodfeatures[1];
		cartCost[i]=parseFloat(prodfeatures[2]);
		cartId[i]=prodfeatures[3];
		Mtotalcartprice=Mtotalcartprice+cartCost[i];
		prodattributes=code.split('-');
		
		for(n=0;n<prodattributes.length;n++){
			switch(n){
				case 0:
					var prodtype=gettype(prodattributes[0])+' ';
					break;
				case 1:
					var prodname=capitalize(prodattributes[1])+' ';
					break;
				case 2:
					if(prodattributes[2]!='x')
					var prodcolor=getcolor(prodattributes[2])+' ';
					break;
				case 3:
					if(prodattributes[3]!='x')
					prodattributes[3]=='cle/f' ? glass='in Clear Glass ' : glass='in Frosted Glass ';
					break;
				case 4:
					if(prodattributes[4]!='x')
					prodattributes[0]=='gdk' ? plate=getplate(prodattributes[4])+' ' : plate=getcollar(prodattributes[4])+' ';
					break;
				case 5:
					if(prodattributes[5]!='x')
					prodattributes[5]=='pol' ? prodfinish='on Polished ' : prodfinish='on Satin ';
					break;
				case 6:
					if(prodattributes[6]!='x')
					prodsize=prodattributes[6]+' mm ';
					break;
				default:
					break;
			}
		}
		cartItem[i]=prodcolor+prodsize+prodname+prodtype+glass+prodfinish+plate;
		if(prodattributes[0].match("gv") || prodattributes[0].match("sb")){
			Mcartlist=Mcartlist+'<div class=Mcartrow><div class=Mcartquant><div class=dec></div><div class=quant>'+cartQuant[i]+'</div><div class=inc></div></div><div class=cartitem><div class=McartItem>'+cartItem[i]+'</div><div class=Mcartprice>'+cartCost[i].toFixed(2)+'</div></div><div class=del title=\"delete this item\" onClick=\"delCart('+i+')\">X</div></div>';
		}
		else{
			Mcartlist=Mcartlist+'<div class=Mcartrow><div class=Mcartquant><div title=\"click to decrease quantity\" class=dec onClick=\"decCart('+i+')\"><</div><div class=quant title=\"click on arrows to change the quantity\">'+cartQuant[i]+'</div><div title=\"click to increase quantity\" class=inc onClick=\"incCart('+i+')\">></div></div><div class=cartitem><div class=McartItem>'+cartItem[i]+'</div><div class=Mcartprice>'+cartCost[i].toFixed(2)+'</div></div><div class=del title=\"delete this item\" onClick=\"delCart('+i+')\">X</div></div>';
		}
	}
	revealMCart();
}

function revealMCart(){
	if(items==1 && mycart.match(/J-/i)){
		ukpnp=2.50;
	}
	else{
		ukpnp=6.50;
	}
	Mtotalcartprice=(Mtotalcartprice+ukpnp);
	
	document.getElementById('maxicart').innerHTML=Mcartlist+"<div id=ukpnprow><div id=ukpnp><div id=ukpnpcost>"+ukpnp.toFixed(2)+"</div><div id=ukpnptitle>* plus UK Carriage :</div></div></div><div id=Mcarttotalrow><div id=pnpdisclaimer>* see <a href=\"shipping.html\">Shipping</a> for details of Overseas and Highlands & Islands carriage costs</div><img src=\"\images/delcart15a.gif\" onClick=\"eraseCart()\" id=cartdel title=\"click to delete cart\"><div id=Mcartotal>£"+Mtotalcartprice.toFixed(2)+"</div><div id=Mcartotaltitle>Total:</div></div>";
}

function createCheckCart(oid){
	if(!oid){
		mycart=readCookie('cart');
		if(mycart){
			ordercode=mycart;
			
			purchases = mycart.split(',');
			items = purchases.length;
			var contents = items+' items';
			if(items>0){
				document.getElementById('cartview').style.display='block';
				document.getElementById('cartcontents').innerHTML=contents;
			}
			else{
				document.getElementById('cartview').style.display='none';
			}
			buildCheckCart()
		}
	}
	else{
		eraseCookie('cart',1);
	}
}

function buildCheckCart(){
	totalcartprice=0;
	cartlist="";
	mailcart="";
	for(i=0;i<purchases.length;i++){
		
		var quantity="",prodcolor="",prodsize="",prodname="",prodtype="",glass="",prodfinish="",plate="";
		
		prodfeatures=purchases[i].split(':');
		
		code=prodfeatures[0];
		cartCode[i]=prodfeatures[0];
		cartQuant[i]=prodfeatures[1];
		cartCost[i]=parseFloat(prodfeatures[2]);
		cartId[i]=prodfeatures[3];
		totalcartprice=totalcartprice+cartCost[i];
		prodattributes=code.split('-');
		
		for(n=0;n<prodattributes.length;n++){
			switch(n){
				case 0:
					var prodtype=gettype(prodattributes[0])+' ';
					break;
				case 1:
					var prodname=capitalize(prodattributes[1])+' ';
					break;
				case 2:
					if(prodattributes[2]!='x')
					var prodcolor=getcolor(prodattributes[2])+' ';
					break;
				case 3:
					if(prodattributes[3]!='x')
					prodattributes[3]=='cle/f' ? glass='in Clear Glass ' : glass='in Frosted Glass ';
					break;
				case 4:
					if(prodattributes[4]!='x')
					prodattributes[0]=='gdk' ? plate=getplate(prodattributes[4])+' ' : plate=getcollar(prodattributes[4])+' ';
					break;
				case 5:
					if(prodattributes[5]!='x')
					prodattributes[5]=='pol' ? prodfinish='on Polished ' : prodfinish='on Satin ';
					break;
				case 6:
					if(prodattributes[6]!='x')
					prodsize=prodattributes[6]+' mm ';
					break;
				default:
					break;
			}
		}
		cartItem[i]=prodcolor+prodsize+prodname+prodtype+glass+prodfinish+plate;
		
		cartlist=cartlist+'<div class=cartrow><div class=cartquant>'+cartQuant[i]+'</div><div class=cartItem>'+cartItem[i]+'</div><div class=cartprice>'+cartCost[i].toFixed(2)+'</div></div>';
		mailcart=mailcart+cartQuant[i]+' '+cartItem[i]+' '+cartCost[i].toFixed(2)+'\r\n';
	}
	if(items==1 && mycart.match(/J-/i)){
		ukpnp=2.50;
	}
	else{
		ukpnp=6.50;
	}
	ordervalue=(totalcartprice+ukpnp);
	revealCheckCart();	
}

function processorder(){
	var expmon = document.checkoutform.expirymonth.value;
	var expyear = document.checkoutform.expiryear.value
	var startmon = document.checkoutform.startmonth.value;
	var startyear = document.checkoutform.startyear.value
	var marker=0;
	if(document.checkoutform.name.value){
		if(confirm('Click on OK to confirm this purchase?')){
			for(i=0;i<document.checkoutform.elements.length;i++){
				document.checkoutform.elements[i].style.borderColor='#ccc';
				if(document.checkoutform.elements[i].id=='check' && (document.checkoutform.elements[i].value=="" || document.checkoutform.elements[i].value=="MM" || document.checkoutform.elements[i].value=="YYYY")){
					document.checkoutform.elements[i].style.borderColor='#9E9EFC';
					marker++;
				}
			}
			
			if(marker>0){
				alert('please complete any fields marked in blue')
			}
			else{
				document.checkoutform.expiry.value=expmon+'/'+expyear;
				document.checkoutform.startdate.value=startmon+'/'+startyear;
				document.checkoutform.submit();
			}
		}
	}
}

function myclientlogin(){
	document.clientlogin.loginemail.value != "" ? document.clientlogin.submit() : alert('you must enter a valid email address')
}


function revealCheckCart(){
	document.getElementById('minicart').innerHTML=cartlist+"<div id=Mcarttotalrow><div id=Mcartotal>£"+totalcartprice.toFixed(2)+"</div><div id=Mcartotaltitle>Total:</div></div>";
	document.checkoutform.mailcart.value=mailcart+'\r\nTotal: £ '+ordervalue.toFixed(2);
	document.checkoutform.ordervalue.value=ordervalue.toFixed(2);
	document.checkoutform.ordervaluedisplay.value=ordervalue.toFixed(2);
	document.checkoutform.ordercode.value=ordercode;
}

function addtocart(){
	code=document.purchaseform.code.value;
	quantity=document.purchaseform.quantity.value;
	orderval=document.purchaseform.orderval.value;
	id=document.purchaseform.id.value
	var value=code+':'+quantity+':'+orderval+':'+id;
	
	createCookie('cart',value,2);
	checkCookieItems();
}

function addtocheckout(){
	code=document.purchaseform.code.value;
	quantity=document.purchaseform.quantity.value;
	orderval=document.purchaseform.orderval.value;
	id=document.purchaseform.id.value
	var value=code+':'+quantity+':'+orderval+':'+id;
	
	createCookie('cart',value,2);
	checkCookieItems2();
}

function incCart(ind){
	var basecost=parseFloat(cartCost[ind])/parseInt(cartQuant[ind]);
	cartQuant[ind]=parseInt(cartQuant[ind])+1;
	cartCost[ind]=basecost*cartQuant[ind];
	reCreateCookie();
}

function decCart(ind){
	var basecost=parseFloat(cartCost[ind])/parseInt(cartQuant[ind]);
	if(cartQuant[ind] !=1){
		cartQuant[ind]=(cartQuant[ind]-1);
		cartCost[ind]=basecost*cartQuant[ind];
		reCreateCookie();
	}
	else{
		alert('you cannot do that');
	}
}

function delCart(ind){
	if(confirm('delete this item?')){
		cartCode.splice(ind,1)
		cartItem.splice(ind,1)
		cartQuant.splice(ind,1)
		cartCost.splice(ind,1)
		cartId.splice(ind,1)
		reCreateCookie();
	}
}

function reCreateCookie(){
	var name='cart';
	var value="",cartcode="",cartquantity=0,cartorderval=0,cartid="";
	var date = new Date();
	date.setTime(date.getTime()+(2*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	for(i=0;i<cartCode.length;i++){
		cartcode=cartCode[i];
		cartquantity=cartQuant[i];
		cartorderval=cartCost[i];
		cartid=cartId[i];
		
		value!="" ? value=value+','+cartcode+':'+cartquantity+':'+cartorderval+':'+cartid : value=cartcode+':'+cartquantity+':'+cartorderval+':'+cartid;
	}
	
	document.cookie = name+"="+value+expires+"; path=/";
	
	createCart();
}

function createCookie(name,value,days,del) {
	var oldvalue=readCookie('cart');
	
	if(oldvalue){
		if(!oldvalue.match(code)){
			value=oldvalue+','+value;
		}
		else{
			if(!del)alert('you already have that in your basket, you can change the quantity at the checkout');
			value=oldvalue;
		}
	}
	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 eraseCookie(name,del) {
	createCookie(name,"",-1,del);
}

function eraseCart() {
	if(confirm('click to delete entire cart?')){
		var len=cartCode.length;
		var ind=0;
		
		cartCode.splice(ind,len)
		cartItem.splice(ind,len)
		cartQuant.splice(ind,len)
		cartCost.splice(ind,len)
		cartId.splice(ind,len)
		reCreateCookie();
	}
}

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 resetorderval(){
	var quant=parseInt(document.purchaseform.quantity.value);
	var curprice=parseFloat(document.purchaseform.price.value);
	document.purchaseform.orderval.value=(quant*curprice).toFixed(2);
}

function counter(){
	val=parseInt(document.purchaseform.quantity.value);
	(val==1 && val2==-1) ? val2=0 : val2=val2;
	document.purchaseform.quantity.value=parseInt(val+val2);
	resetorderval();
	t=setTimeout("counter()",500);
}

function doTimer(num){
	num=='inc' ? val2=parseInt(1) : val2=parseInt(-1);
	if (!timer_is_on){
  	timer_is_on=1;
  	counter();
  }
}

function stopCount(){
clearTimeout(t);
timer_is_on=0;
}

function revisesizeprice(i){
		switch(priceband){
		case 'aband':
			sizeprice=aband[i];
			break;
		case 'bband':
			sizeprice=bband[i];
			break;
		case 'cband':
			sizeprice=cband[i];
			break;
		case 'dband':
			sizeprice=dband[i];
			break;
		default:
			break;
	}
}
	
	function revisebackingprice(i){
	priceband!="" ? backingprice=ck[i] : backingprice=dk[i];
}

function checkedbox(oBj,flag){
	for(i=0;i<oBj.length;i++){
		if(oBj[i].checked){
			if(flag){
				flag==1 ? revisebackingprice(i) : revisesizeprice(i);
			}
			return(oBj[i].value);
		}
	}
}

function setcode(){
	if(document.purchaseform.id.value !=29 && document.purchaseform.id.value !=36){
		document.purchaseform.quantity.value=1;
		priceband=document.purchaseform.priceband.value;
		oldprice=parseFloat(document.purchaseform.basecost.value);
		
		var code="";
		if(cat)code=cat;
		if(prod)code=code+'-'+prod;
		if(document.purchaseform.colour){code=code+'-'+checkedbox(document.purchaseform.colour,0);}else{code=code+'-'+'x';}
		if(document.purchaseform.finish){code=code+'-'+checkedbox(document.purchaseform.finish,0);}else{code=code+'-'+'x';}
		if(document.purchaseform.backingplate){code=code+'-'+checkedbox(document.purchaseform.backingplate,1);}else{code=code+'-'+'x';}
		if(document.purchaseform.platefinish){code=code+'-'+checkedbox(document.purchaseform.platefinish,0);}else{code=code+'-'+'x';}
		if(document.purchaseform.sizes){code=code+'-'+checkedbox(document.purchaseform.sizes,2);}else{code=code+'-'+'x';}
		document.purchaseform.code.value=code.toLowerCase();
		
		sizeprice>0 ? newprice=parseFloat(sizeprice+backingprice) : newprice=parseFloat(oldprice+backingprice);
		
		document.purchaseform.price.value=newprice.toFixed(2);
		
		resetorderval();
	}
}

function SetOpacity(elem, opacityAsInt)
{
	var opacityAsDecimal = opacityAsInt;
	
	if (opacityAsInt > 100)
		opacityAsInt = opacityAsDecimal = 100; 
	else if (opacityAsInt < 0)
		opacityAsInt = opacityAsDecimal = 0; 
	
	opacityAsDecimal /= 100;
	if (opacityAsInt < 1)
		opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0
	
	elem.style.opacity = opacityAsDecimal;
	elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}

function FadeOpacity(elemId, fromOpacity, toOpacity, time, fps)
{
	var steps = Math.ceil(fps * (time / 1000));
	var delta = (toOpacity - fromOpacity) / steps;
	
	FadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
}

function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep)
{
    SetOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));

    if (stepNum < steps){
        setTimeout("FadeOpacityStep('" + elemId + "', " + (stepNum+1) + ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", timePerStep);
    }
    else{
    	switch(changematrix){
    		case 1:
    			switchcontent1();
    			break;
    		case 2:
    			switchcontent2();
    			break;
    		case 3:
    			switchcontent3();
    			break;
    	}
    }
}

function switchcontent1(){
	inc1==16 ? inc1=0 : inc1=inc1;
	if(functionref1==0){
    if(contentref1==0){
			content1=images1[inc1];
			inc1++;
			contentref1=1;
			changematrix=2;
		}
		else{
			content1=matrix1;
			contentref1=0;
		}
    document.getElementById(curpic1).innerHTML=content1;
    matrixfadeup1();
   }else{
    matrixfade1();
   }
}

function matrixfade1(){
	functionref1=0;
	setTimeout("FadeOpacity(curpic1, 100, 0, 1000, 12)",3000);
}

function matrixfadeup1(){
	functionref1=1;
	setTimeout("FadeOpacity(curpic1, 0, 100, 1500, 12)",100);
}

function switchcontent2(){
	inc2==16 ? inc2=0 : inc2=inc2;
	if(functionref2==0){
    if(contentref2==0){
			content2=matrix2;
			inc2++;
			contentref2=1;
			changematrix=3;
		}
		else{
			content2=images2[inc2];
			contentref2=0;
		}
    document.getElementById(curpic2).innerHTML=content2;
    matrixfadeup2();
    }else{
    	matrixfade2();
    	}
}

function matrixfade2(){
	functionref2=0;
	setTimeout("FadeOpacity(curpic2, 100, 0, 1000, 12)",2000);
}

function matrixfadeup2(){
	functionref2=1;
	setTimeout("FadeOpacity(curpic2, 0, 100, 1500, 12)",100);
}

function switchcontent3(){
	inc3==16 ? inc3=0 : inc3=inc3;
	if(functionref3==0){
    if(contentref3==0){
			content3=images3[inc3];
			inc3++;
			contentref3=1;
			changematrix=1;
		}
		else{
			content3=matrix3;
			contentref3=0;
		}
    document.getElementById(curpic3).innerHTML=content3;
    matrixfadeup3();
    }else{
    	matrixfade3();
    	}
}

function matrixfade3(){
	functionref3=0;
	setTimeout("FadeOpacity(curpic3, 100, 0, 1000, 12)",3000);
}

function matrixfadeup3(){
	functionref3=1;
	setTimeout("FadeOpacity(curpic3, 0, 100, 1500, 12)",500);
}

function imgswitch(oBj){
	var tempimg=document.getElementById('mainimage').src;
	document.getElementById('mainimage').src=oBj.src.replace(/_t/,"");
	oBj.src=tempimg.replace(/\.jpg/,"_t.jpg");
}

function gallswitch(oBj){
	document.getElementById('mainimage').src=oBj.src;
}

function myemail(){
	document.getElementById('emailbox').innerHTML='<a href="mailto:info@merlinglass.co.uk">info@merlinglass.co.uk</a>';
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return(myHeight);
}

function tableheight(){
	//var hite=window.screen.availHeight;
	var hite=alertSize()-170;
	document.getElementById('mainbox').style.height=hite+'px';
}

function tableheight3(){
	//var hite=window.screen.availHeight;
	
	var hite=alertSize()-180;
	document.getElementById('mainbox').style.height=hite+'px';
}

function tableheight2(){
	//var hite=window.screen.availHeight;
	var hite2=alertSize()-180;
	var hite=470;
	document.getElementById('mainbox').style.height=hite2+'px';
	document.getElementById('maintable').style.height=hite+'px';
	document.getElementById('text1').style.marginTop=(hite-360)+'px'
	document.getElementById('cont2').style.marginTop=(hite-400)+'px'
	document.getElementById('text3').style.marginTop=(hite-360)+'px'
}

function fadeup(oBj){
	oBj.style.borderColor="#DCDCE0";
	oBj.style.opacity=.50;
	oBj.style.KhtmlOpacity=.50;
	oBj.style.mozOpacity=.50;
	oBj.style.filter='alpha(opacity=50)';
}

function fadedown(oBj){
	oBj.style.borderColor="#F0F0F4";
	oBj.style.opacity=1;
	oBj.style.KhtmlOpacity=1;
	oBj.style.mozOpacity=1;
	oBj.style.filter='alpha(opacity=100)';
}

var count=0;
var lastcount=0;

function fadein(oBj){
	oBj.style.opacity=.70;
	oBj.style.KhtmlOpacity=.70;
	oBj.style.mozOpacity=.70;
	oBj.style.filter='alpha(opacity=70)';
}

function fadeout(oBj){
	oBj.style.opacity=1;
	oBj.style.KhtmlOpacity=1;
	oBj.style.mozOpacity=1;
	oBj.style.filter='alpha(opacity=100)';
}

function shimmer(){
	if(count>0){
		lastcount=count;
		document.getElementById(lastcount).style.filter='alpha(opacity=100)';
	}
	
	count=rand(1,16);
	document.getElementById(count).style.filter='alpha(opacity=50)';
	
	setTimeout("shimmer()",200);
}

function rand( min, max ) {   
    // Returns a random number     
    //    
    // version: 810.1317   
    // discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_rand   
  
    // +   original by: Leslie Hoare   
    // +   bugfixed by: Onno Marsman   
    // *     example 1: rand(1, 1);   
    // *     returns 1: 1   
    var argc = arguments.length;   
    if (argc == 0) {   
        min = 0;   
        max = 2147483647;   
    } else if (argc == 1) {   
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');   
    }   
    return Math.floor(Math.random() * (max - min + 1)) + min;   
}  

function move(loc){
	location=loc;
}

function greyup(a){
	a.style.color='#bbb';
	window.status='';return true;
}

function greydown(a){
	a.style.color='#373737';
}

function gallSetOpacity(elem, opacityAsInt)
{
	var opacityAsDecimal = opacityAsInt;
	
	if (opacityAsInt > 100)
		opacityAsInt = opacityAsDecimal = 100; 
	else if (opacityAsInt < 0)
		opacityAsInt = opacityAsDecimal = 0; 
	
	opacityAsDecimal /= 100;
	if (opacityAsInt < 1)
		opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0
	
	elem.style.opacity = opacityAsDecimal;
	elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}

function gallFadeOpacity(elemId, fromOpacity, toOpacity, time, fps)
{
	var steps = Math.ceil(fps * (time / 1000));
	var delta = (toOpacity - fromOpacity) / steps;
	
	gallFadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
}

function gallFadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep)
{
    gallSetOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));

    if (stepNum < steps){
        setTimeout("gallFadeOpacityStep('" + elemId + "', " + (stepNum+1) + ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", timePerStep);
    }
    else{
    		if(functionref==0){
    			document.getElementById('minicart').style.display='none';
    		}
    	}
}

function matrixfade(){
	if(functionref==1){
		functionref=0;
		setTimeout("gallFadeOpacity('minicart', 100, 10, 800, 12)",100);
	}
}

function matrixfadeup(){
	if(functionref==0){
		functionref=1;
		setTimeout("gallFadeOpacity('minicart', 30, 100, 600, 12)",100);
	}
}

function selectcard(cardcode,card){
	document.checkoutform.card.value=card;
	document.checkoutform.cardcode.value=cardcode;
}

function showlist(oBj2,oBj){
	var pos=findPos(oBj2);
	document.getElementById(oBj).style.left=pos[0]+'px';
	document.getElementById(oBj).style.top=(pos[1]+20)+'px';
	document.getElementById(oBj).style.display='block';
}

function hidelist(oBj){
	document.getElementById(oBj).style.display='none';
}

function checkcard(){
	var cardcode = document.checkoutform.cardcode.value;
	var cardnumber = document.checkoutform.cardnumber.value;
	verifyCard(cardcode,cardnumber);
}

function clearit(oBj){
	oBj.value="";
}

function mailchecker(){
	document.checkoutform.mail.value==0 ? document.checkoutform.mail.value=1 : document.checkoutform.mail.value=0;
}

function getgallery(gall){
	document.trans.galleryid.value=gall;
	document.trans.submit();
}
