// Description: Global Topz Quote Calculator.
// Version:     2.0 (Rev: 130)
//
// Copyright 2000-2007 Click Industrial Limited. All rights reserved.
//
// Id: quote.js 130 2007-06-14 14:54:41Z PRYAN
if(!Array.prototype.push)Array.prototype.push=function(){for(var i=0;i<arguments.length;i++){this[this.length]=arguments[i];}}
if(!Array.prototype.pop)Array.prototype.pop=function(){var value=this[this.length - 1];this.length--;return value;}
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"");}
function getFormFieldValues(strForm,strField){var objField,strType,strValues,i;strValues=new Array();objField=document.forms[strForm].elements[strField];if(objField){strType=(objField.type)?objField.type:objField[0].type;}
else{strType="undefined";}
switch(strType){case"undefined":
strValues.push("");break;case"radio":
if(objField.length!=null){for(i=0;i<objField.length;i++){if(objField[i].checked){strValues.push(objField[i].value);break;}}}
else{if(objField.checked)strValues.push(objField.value);}
break;case"select-multiple":
for(i=0;i<objField.length;i++){if(objField[i].selected)strValues.push(objField[i].value);}
break;case"select-one":
if(objField.selectedIndex!=-1){strValues.push(objField.options[objField.selectedIndex].value);}
break;default:
strValues.push(objField.value);}
return strValues;}
function Form(properties){this.fields=[];for(property in properties)this[property]=properties[property];}
Form.prototype.addField=function(properties){var newField=new Field(this,properties);newField.form=this;this.fields[properties["name"]]=newField;return newField;}
Form.prototype.getFieldValue=function(fieldName){return getFormFieldValues(0,fieldName);}
Form.prototype.validate=function(){var field;this.isValid=true;for(field in this.fields){if(this.fields[field].validate){this.isValid=(this.fields[field].validate()&&this.isValid);}}
return this.isValid;}
Form.prototype.init=function(){var field;for(field in this.fields){if(this.fields[field].init){this.fields[field].init();}}}
function Field(parentForm,properties){this.form=parentForm;this.isValid=false;this.errorCode=null;this.preFormat=null;this.re=null;this.postFormat=null;this.required=null;this.value="";for(property in properties)this[property]=properties[property];}
Field.prototype.getElement=function(){if(this.id){return document.getElementById(this.id);}
else
{return document.getElementById(this.name);}}
Field.prototype.getValue=function(){return getFormFieldValues(0,this.name);}
Field.prototype.regexTest=function(){this.isValid=this.pattern.test(this.getValue()[0]);return this.isValid;}
function getSKUPrice(sku,margin){var price=priceList[sku];if(price==null)alert("Error!\nCannot find price for item ("+sku+")");return price*margin;}
function setInnerHTML(elementName,value){var ele=document.getElementById(elementName);if(ele)ele.innerHTML=value;}
function formatGBP(n,prefix){if(!prefix){prefix="&#163;";}
return formatCurrency(n,prefix,"",".",2);}
function formatCurrency(n,prefix,suffix,decSym,decPlaces){var i,f,fs;i=Math.floor(n);f=Math.round((n - i)*Math.pow(10,decPlaces));fs=String(stringFill("0",decPlaces)+ f);fs=fs.substring(fs.length - decPlaces);return(prefix + formatGroup(i)+ decSym + fs + suffix);}
function formatGroup(n){var r,s=String(n),m=s.length%3;for(r=s.slice(0,m);m<s.length;m +=3){if(m!=0)r +=",";r +=s.substr(m,3);}
return r;}
function stringFill(s,n){for(var r="";n>0;n--){r +=s;}
return r;}
function makeNumeric(n){n=parseFloat(n);return(isNaN(n)?0:n);}
var quoteForm;function initForm(){quoteForm=new Form({getPrices:function(margin){var field,total=0;for(field in this.fields)if(this.fields[field].getPrice)total +=this.fields[field].getPrice(margin);return total;}});var material=quoteForm.addField({name:"material",
init:function(){var ele=this.getElement();ele.onchange=this.onChange;ele.onkeypress=this.onChange;ele.onkeyup=this.onChange;this.onChange();},
onChange:function(){img=imgList[quoteForm.fields['material'].getValue()[0]];img=(img)?"/i/small/"+ img +".jpg":"/i/placeholder.gif";document.getElementById("img-material").src=img;},
pattern:	/^.+$/,
validate:function(){return this.regexTest();}});var edge=quoteForm.addField({name:"edge",
init:function(){var ele=this.getElement();ele.onchange=this.onChange;ele.onkeypress=this.onChange;ele.onkeyup=this.onChange;this.onChange();},
onChange:function(){img=imgList[quoteForm.fields['edge'].getValue()[0]];img=(img)?"/i/small/edge/"+ img +".png":"/i/placeholder.gif";document.getElementById("img-edge").src=img;},
pattern:	/^.+$/,
validate:function(){return this.regexTest();}});var depth1=quoteForm.addField({name:"depth1",
pattern:	/^\s*[\d.]+\s*$/,
validate:function(){return this.regexTest();},
getPrice:function(margin){var depth,price=0;if(material.validate()&&edge.validate()&&this.validate()){depth=new Number(this.getValue()[0].trim());price=getSKUPrice(material.getValue()[0],margin);price +=getSKUPrice(edge.getValue()[0],margin);price*=depth;}
return price;}});var depth2=quoteForm.addField({name:"depth2",
pattern:	/^\s*[\d.]+\s*$/,
validate:function(){return this.regexTest();},
getPrice:function(margin){var depth,price=0;if(material.validate()&&edge.validate()&&this.validate()){depth=new Number(this.getValue()[0].trim());price=getSKUPrice(material.getValue()[0].slice(0,-2)+"d2",margin);price +=getSKUPrice(edge.getValue()[0],margin);price*=depth;}
return price;}});var upstand1=quoteForm.addField({name:"upstand1",
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice("upstand&height=h1",margin);}});var upstand2=quoteForm.addField({name:"upstand2",
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice("upstand&height=h2",margin);}});var upstand3=quoteForm.addField({name:"upstand3",
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice("upstand&height=h3",margin);}});var backsplash=quoteForm.addField({name:"backsplash",
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice("backsplash",margin);}});var cutHob=quoteForm.addField({name:"cut-hob",
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice(this.name,margin);}});var cutOver=quoteForm.addField({name:"cut-over",
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice(this.name,margin);}});var cutFlush=quoteForm.addField({name:"cut-flush",
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice(this.name,margin);}});var cutUnder=quoteForm.addField({name:"cut-under",
init:function(){var ele=this.getElement();ele.onchange=this.onChange;ele.onkeyup=this.onChange;},
onChange:function(){grvStandrd.update();grvRecesd.update();},
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice(this.name,margin);}});var cutBelfst=quoteForm.addField({name:"cut-belfst",
init:function(){var ele=this.getElement();ele.onchange=this.onChange;ele.onkeyup=this.onChange;},
onChange:cutUnder.onChange,
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice(this.name,margin);}});var grvStandrd=quoteForm.addField({name:"grv-stndrd",
init:function(){this.update();},
update:function(){cutUnder=quoteForm.getFieldValue("cut-under");cutBelfst=quoteForm.getFieldValue("cut-belfst");var ele=this.getElement();ele.disabled=(cutUnder=="0"&&cutBelfst=="0");if(ele.disabled){ele.selectedIndex=0;}},
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice(this.name,margin);}});var grvRecesd=quoteForm.addField({name:"grv-recesd",
init:function(){this.update();},
update:grvStandrd.update,
getPrice:function(margin){return makeNumeric(this.getValue()[0])*getSKUPrice(this.name,margin);}});var geo=quoteForm.addField({name:"geo",
pattern:	/^.+$/,
validate:function(){return this.regexTest();},
getPrice:function(margin){var price=0;if(this.validate()){price=makeNumeric(getSKUPrice(this.getValue()[0],margin));}
return price;}});var name=quoteForm.addField({name:"name",
pattern:	/\S+/,
validate:function(){return this.regexTest();}});var worktel=quoteForm.addField({name:"worktel",
pattern:	/\S+/,
validate:function(){return this.regexTest();}});var hometel=quoteForm.addField({name:"hometel",
pattern:	/\S+/,
validate:function(){return this.regexTest();}});var mobtel=quoteForm.addField({name:"mobtel",
pattern:	/\S+/,
validate:function(){return this.regexTest();}});var telephone=quoteForm.addField({name:"telephone",
pattern:	/\S+/,
validate:function(){return this.regexTest();}});var email=quoteForm.addField({name:"email",
pattern:	/\S+/,
validate:function(){return this.regexTest();}});var timing=quoteForm.addField({name:"timing",
pattern:	/.+/,
validate:function(){return this.regexTest();}});var btnSubmit=quoteForm.addField({name:"btnSubmit",
init:function(){this.getElement().onclick=this.onClick;},
onClick:function(){var s="";var i=0;if(quoteForm.fields['calc'])quoteForm.fields['calc'].onClick();quoteForm.validate();if(!quoteForm.fields['material'].isValid)s+=++i +". Material\n";if(!quoteForm.fields['edge'].isValid)s+=++i +". Edge\n";if(!quoteForm.fields['depth1'].isValid&&
!quoteForm.fields['depth2'].isValid)s+=++i +". Worktop length\n";if(!quoteForm.fields['geo'].isValid)s+=++i +". Templating & fitting location\n";if(!quoteForm.fields['name'].isValid)s+=++i +". Your name\n";if(!quoteForm.fields['worktel'].isValid&&
!quoteForm.fields['hometel'].isValid&&
!quoteForm.fields['mobtel'].isValid&&
!quoteForm.fields['telephone'].isValid&&
!quoteForm.fields['email'].isValid)s+=++i +". A telephone number or email address\n";if(!quoteForm.fields['timing'].isValid)s+=++i +". Your planned installation timing\n";if(i>0){s="You've missed a "+((i>1)?"few bits":"bit")+
"! Could you please complete the following:\n\n"+ s;alert(s);return false;}
return true;}});quoteForm.init();}
addLoadEvent(initForm);