
/**
 * Shortcut function.
 */
var $ = function(id) {
  return document.getElementById(id);
};

/**
 * The roof object
 */
var Roof = {
  theCalculator: null,
  fullWidth: 0, 
  fullLength: 0, 
  wallLength: 0, 
  raisedEdgeLength: 0, 
  gutterLength: 0,
  corners:0,
  
  calculate: function() {
    this.theCalculator = $('roof-calc');
	if (this.theCalculator) {
	  if (!this.checkField('FullWidth')) return;
	  if (!this.checkField('FullLength')) return;
	  if (!this.checkField('WallLength')) return;
	  if (!this.checkField('RaisedEdgeLength')) return;
	  if (!this.checkField('GutterLength')) return;
	  if (!this.checkField('Corners')) return;
	  
	  this.runCalculation();
	  return false;
	}
  }, 
  
  checkField: function(fieldName) {
    if (this.theCalculator[fieldName]) {
	  if (this.theCalculator[fieldName].value) {
		if (isNaN(this.theCalculator[fieldName].value)) {
		  alert("The value you have entered for '" + fieldName + "' is not a number.");
		  this.theCalculator[fieldName].focus();
		  this.theCalculator[fieldName].select();
		  return false;
		}
		
		if (fieldName == 'WallLength' || fieldName == 'RaisedEdgeLength' || fieldName == 'GutterLength') {
		  this.theCalculator[fieldName].value = Math.ceil(this.theCalculator[fieldName].value/3) * 3;
		}
	    
		switch (fieldName) {
		  case 'FullWidth':
		    Roof.fullWidth = this.theCalculator[fieldName].value;
			break;
		  case 'FullLength':
		    Roof.fullLength = this.theCalculator[fieldName].value;
			break;
		  case 'WallLength':
		    Roof.wallLength = this.theCalculator[fieldName].value;
			break;
	      case 'RaisedEdgeLength':
		    Roof.raisedEdgeLength = this.theCalculator[fieldName].value;
			break;
		  case 'GutterLength':
		    Roof.gutterLength = this.theCalculator[fieldName].value;
			break;
		  case 'Corners':
		    Roof.corners = this.theCalculator[fieldName].value;
			break;
		}
		return true;
	  }
	  else {
	    alert("Please specify a value for '" + fieldName + "'.");
		this.theCalculator[fieldName].focus();
	  }
	}
	return false;
  }, 
  
  runCalculation: function() {
	this.clearResults();
	  
	var area = Roof.fullWidth * Roof.fullLength;
	this.appendResult('Area (m<sup>2</sup>)', area);
	
	var resin = Math.ceil((1.5 * area) / 10) * 10;
	this.appendResult('Roofing resin (kg)', resin);
	
	var topcoat = Math.ceil((0.5 * area) / 10) * 10;
	this.appendResult('Topcoat (kg)', topcoat);
	
	var catalyst = Math.ceil((0.8 * area) / 5) * 5;
	this.appendResult('Catalyst (l)', catalyst);
	
	var puadhesive = Math.ceil(Math.round(area/8));
	this.appendResult('PU adhesive (tubes)', puadhesive);
	
	var csm = Math.ceil((0.6 * area) / 33) * 33;
	this.appendResult('CSM (kg)', csm);
  }, 
  
  clearResults: function() {
    var resultsArea = $('results');
	if (resultsArea) {
	  resultsArea.innerHTML = '<hr size="1" style="color:#417474" />';
	  resultsArea.style.display = 'none';
	}
  },
  
  appendResult: function(title, value) {
    var resultsArea = $('results');
	if (resultsArea) {
	  if (resultsArea.style.display != 'block') resultsArea.style.display = 'block';
	  resultsArea.innerHTML += '<label style="font-weight:bold">' + title + '</label><div style="width:210px">' + value + '</div>';
	}
  }
};

function loadShoppingCartSummary() {
  var theSummary = document.getElementById("cart");
  theSummary.style.display = "none";
  var items = getCartItem(3);
  if (items != 0) {
    changeOpac(80, "cart");
    theSummary.style.display = "block";
  }
}

function expandBasket() {
  var exp = document.getElementById("exp-basket");
  if (exp) {
    exp.style.display = (exp.style.display == "none" || exp.style.display == "") ? "block" : "none";
  }
}

function changeOpac(opacity, id) {
  var object = document.getElementById(id).style; 
  object.opacity = (opacity / 100);
  object.MozOpacity = (opacity / 100);
  object.KhtmlOpacity = (opacity / 100);
  object.filter = "alpha(opacity=" + opacity + ")";
}

function emailFriend(productName) {
  var pageName = window.location;
  var message = "I thought you might be interested in this from 'Glasplies' - " + productName + "\n\nYou can view it at " + pageName;
  message = encodeURI(message);
  window.location.href = "mailto:?subject=" + productName + " from Glasplies.&body=" + message;
}


var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id) {	
  // cancel close timer
  mcancelclosetime();

  // close old layer
  mclose();

  // get new layer and show it
  ddmenuitem = document.getElementById(id);
  ddmenuitem.style.visibility = 'visible';
  ddmenuitem.style.zindex = '999999';
}

// close showed layer
function mclose() {
  if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime() {
  closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
  if (closetimer) {
    window.clearTimeout(closetimer);
	closetimer = null;
  }
}

// close layer when click-out
document.onclick = mclose; 

/***********************************************************************
*
* getMiniCart		-	fetches cart content summary
*
* Returns:		-	HTML string for summary display
*
* Graham Bradley 2007
* Comments & bug reports to web@gbradley.co.uk
*
* To make editing the output HTML easier, output code has been split into
* nine 'template' strings.
*
*	tableOpen	-	the opening table code
*	itemLine	-	repeated for each item in the cart
*	shippingLine	-	for displaying shipping charges (if any)
*	vatLine		-	for displaying VAT on items & shipping (if any)
*	discountLine	-	for displaying discount total (if any)
*	surchargeLine	-	for displaying surcharge total (if any)
*	totalLine	-	for displaying the cart total
*	tableClose	-	the closing table code
*	emptyLine	-	displayed when cart is empty
*
* Within these templates, you can use variables to insert values into
* the summary, similar to variables in Actinic:
*
*	VAR:QUANTITY	-	The quantity of item in cart
*	VAR:TITLE	-	Abbreviated name of item
*	VAR:LINK	-	URL of item via the cgi-bin
*	VAR:PRICE	-	Formatted price of item
*	VAR:SHIPPING	-	Total of shipping applied
*	VAR:VAT		-	Total VAT on cart items & shipping
*	VAR:DISCOUNT	-	Total of discounts applied
*	VAR:SURCHARGE	-	Total of surcharges applied
*	VAR:TOTAL	-	Grand total
*
* Variables can only be used in the templates in which they are found.	
*
************************************************************************/

function getMiniCart(){

var tableOpen="<table width='100%'>";
var itemLine="<tr><td valign='top'>VAR:QUANTITYx </td><td><a href='VAR:LINK'>VAR:TITLE...</a></td><td align='right' valign='top'>VAR:PRICE</td></tr>"
var shippingLine="<tr><td colspan='2'>Shipping</td><td align='right'>VAR:SHIPPING</td></tr>"
var vatLine="<tr><td colspan='2'>VAT</td><td align='right'>VAR:VAT</td></tr>"
var discountLine="<tr><td colspan='2'>Discounts</td><td align='right'>VAR:DISCOUNT</td></tr>"
var surchargeLine="<tr><td colspan='2'>Surcharges</td><td align='right'>VAR:SURCHARGE</td></tr>"
var totalLine="</table><table width='100%'><tr><td><b>Total</b></td><td align='right'><b>VAR:TOTAL</b></td></tr>"
var tableClose="</table>"
var emptyLine="No items in cart.";

var cookie=getCartItem(4);

if (!cookie){
	return emptyLine;
	}
var re=new Array();
var match=new Array();
var total=0;
var str;
re[1]=/cur=([^&]*)&!/g;
re[2]=/&!(.*)/g;
re[3]=/ss=(.*)cur=/i;
re[4]=/tx=(.*)sh=/i;
re[5]=/sh=(.*)ss=/i;
for (i=1;i<re.length;i++){
	match[i]=re[i].exec(cookie);
	match[i]=match[i][1];
	}
lines=match[2].split("&!");
store=new Array();
re[0]=/^(\d*)x/;
re[1]=/x([^>]*)>/;
re[2]=/>([^>]*)>/;
re[3]=/>([^>]*)$/;
str=tableOpen;
for (i=0;i<lines.length;i++){
	for (j=0;j<re.length-2;j++){
		detail=lines[i].match(re[j]);
		store[j]=detail[1];
		}
	str+=itemLine.replace("VAR:QUANTITY",store[0]).replace("VAR:LINK",match[3]+"?PRODREF="+store[1]+"&NOLOGIN=1").replace("VAR:TITLE",store[2]).replace("VAR:PRICE",match[1]+((store[3]*1).toFixed(2)));
	total+=(store[3]*1);
	}
if (match[5] > 0){
	str+=shippingLine.replace("VAR:SHIPPING",match[1]+(match[5]/100).toFixed(2));
	total=total+=(match[5]/100);
	}
if (match[4] > 0){
	str+=vatLine.replace("VAR:VAT",match[1]+(match[4]/100).toFixed(2));
	total=total+=(match[4]/100);
	}
gTotal=getCartItem(1);
var r=/&#(\d*);/gi;
var m=new Array();
var f=new Array();
while((m = r.exec(gTotal))!=null){
	f[f.length]=m[1];
	}
for (i=0;i<f.length;i++){
	gTotal=gTotal.replace("&#"+f[i]+";","");
	}
total=total.toFixed(2);
gTotal=(gTotal/100).toFixed(2);
if (total>gTotal){
	str=str+=discountLine.replace("VAR:DISCOUNT","-"+match[1]+(total-gTotal).toFixed(2));
	}
else if (total<gTotal){
	str=str+=surchargeLine.replace("VAR:SURCHARGE",match[1]+(gTotal-total).toFixed(2));
	}
str+=totalLine.replace("VAR:TOTAL",match[1]+(gTotal));
str+=tableClose;
return str;
}


//slideshow

var fadebgcolor="#fff"

//NO need to edit beyond here//
 
var fadearray=new Array() //array to cache fadeshow instances
var fadeclear=new Array() //array to cache corresponding clearinterval pointers
 
var dom=(document.getElementById) //modern dom browsers
var iebrowser=document.all
 
function fadeshow(theimages, fadewidth, fadeheight, borderwidth, delay, pause, displayorder){
this.pausecheck=pause
this.mouseovercheck=0
this.delay=delay
this.degree=10 //initial opacity degree (10%)
this.curimageindex=0
this.nextimageindex=1
fadearray[fadearray.length]=this
this.slideshowid=fadearray.length-1
this.canvasbase="canvas"+this.slideshowid
this.curcanvas=this.canvasbase+"_0"
if (typeof displayorder!="undefined")
theimages.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)
this.theimages=theimages
this.imageborder=parseInt(borderwidth)
this.postimages=new Array() //preload images
for (p=0;p<theimages.length;p++){
this.postimages[p]=new Image()
this.postimages[p].src=theimages[p][0]
}
 
var fadewidth=fadewidth+this.imageborder*2
var fadeheight=fadeheight+this.imageborder*2
 
if (iebrowser&&dom||dom) //if IE5+ or modern browsers (ie: Firefox)
document.write('<div id="master'+this.slideshowid+'" style="position:relative;width:'+fadewidth+'px;height:'+fadeheight+'px;overflow:hidden;"><div id="'+this.canvasbase+'_0" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);opacity:0.1;-moz-opacity:0.1;-khtml-opacity:0.1;background-color:'+fadebgcolor+'"></div><div id="'+this.canvasbase+'_1" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);opacity:0.1;-moz-opacity:0.1;-khtml-opacity:0.1;background-color:'+fadebgcolor+'"></div></div>')
else
document.write('<div><img name="defaultslide'+this.slideshowid+'" src="'+this.postimages[0].src+'"></div>')
 
if (iebrowser&&dom||dom) //if IE5+ or modern browsers such as Firefox
this.startit()
else{
this.curimageindex++
setInterval("fadearray["+this.slideshowid+"].rotateimage()", this.delay)
}
}

function fadepic(obj){
if (obj.degree<100){
obj.degree+=10
if (obj.tempobj.filters&&obj.tempobj.filters[0]){
if (typeof obj.tempobj.filters[0].opacity=="number") //if IE6+
obj.tempobj.filters[0].opacity=obj.degree
else //else if IE5.5-
obj.tempobj.style.filter="alpha(opacity="+obj.degree+")"
}
else if (obj.tempobj.style.MozOpacity)
obj.tempobj.style.MozOpacity=obj.degree/101
else if (obj.tempobj.style.KhtmlOpacity)
obj.tempobj.style.KhtmlOpacity=obj.degree/100
else if (obj.tempobj.style.opacity&&!obj.tempobj.filters)
obj.tempobj.style.opacity=obj.degree/101
}
else{
clearInterval(fadeclear[obj.slideshowid])
obj.nextcanvas=(obj.curcanvas==obj.canvasbase+"_0")? obj.canvasbase+"_0" : obj.canvasbase+"_1"
obj.tempobj=iebrowser? iebrowser[obj.nextcanvas] : document.getElementById(obj.nextcanvas)
obj.populateslide(obj.tempobj, obj.nextimageindex)
obj.nextimageindex=(obj.nextimageindex<obj.postimages.length-1)? obj.nextimageindex+1 : 0
setTimeout("fadearray["+obj.slideshowid+"].rotateimage()", obj.delay)
}
}
 
fadeshow.prototype.populateslide=function(picobj, picindex){
var slideHTML=""
if (this.theimages[picindex][1]!="") //if associated link exists for image
slideHTML='<a href="'+this.theimages[picindex][1]+'" target="'+this.theimages[picindex][2]+'">'
slideHTML+='<img src="'+this.postimages[picindex].src+'" border="'+this.imageborder+'px">'
if (this.theimages[picindex][1]!="") //if associated link exists for image
slideHTML+='</a>'
picobj.innerHTML=slideHTML
}
 
 
fadeshow.prototype.rotateimage=function(){
if (this.pausecheck==1) //if pause onMouseover enabled, cache object
var cacheobj=this
if (this.mouseovercheck==1)
setTimeout(function(){cacheobj.rotateimage()}, 100)
else if (iebrowser&&dom||dom){
this.resetit()
var crossobj=this.tempobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
crossobj.style.zIndex++
fadeclear[this.slideshowid]=setInterval("fadepic(fadearray["+this.slideshowid+"])",50)
this.curcanvas=(this.curcanvas==this.canvasbase+"_0")? this.canvasbase+"_1" : this.canvasbase+"_0"
}
else{
var ns4imgobj=document.images['defaultslide'+this.slideshowid]
ns4imgobj.src=this.postimages[this.curimageindex].src
}
this.curimageindex=(this.curimageindex<this.postimages.length-1)? this.curimageindex+1 : 0
}
 
fadeshow.prototype.resetit=function(){
this.degree=10
var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
if (crossobj.filters&&crossobj.filters[0]){
if (typeof crossobj.filters[0].opacity=="number") //if IE6+
crossobj.filters(0).opacity=this.degree
else //else if IE5.5-
crossobj.style.filter="alpha(opacity="+this.degree+")"
}
else if (crossobj.style.MozOpacity)
crossobj.style.MozOpacity=this.degree/101
else if (crossobj.style.KhtmlOpacity)
crossobj.style.KhtmlOpacity=this.degree/100
else if (crossobj.style.opacity&&!crossobj.filters)
crossobj.style.opacity=this.degree/101
}
 
 
fadeshow.prototype.startit=function(){
var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
this.populateslide(crossobj, this.curimageindex)
if (this.pausecheck==1){ //IF SLIDESHOW SHOULD PAUSE ONMOUSEOVER
var cacheobj=this
var crossobjcontainer=iebrowser? iebrowser["master"+this.slideshowid] : document.getElementById("master"+this.slideshowid)
crossobjcontainer.onmouseover=function(){cacheobj.mouseovercheck=1}
crossobjcontainer.onmouseout=function(){cacheobj.mouseovercheck=0}
}
this.rotateimage()
}