﻿// Copyright Mountain Hardwoods 2009
// Code may only be used under license.
// Licensing Fees start at 10,000 GBP per week, 4 week minimum contract.
// Use of code in part or full incurs same licensing fees.
// Code-lifters will be prosecuted.

var minprice=30;  // Prices can never be less than this

var maxwidth=1280;   // Will not allow quotes for larger than this
var minwidth=640;     // Will not allow quotes for smaller 

var maxlength=3000;  // Will not allow quotes for longer than this
var minlength=640;  // Will not allow quotes for shorter than this

var standarddelivery=45;
var maxstandarddelivery=4000;

// Processing variables
var clicked;

var pfactor=1;

var pdes="Bespoke";

var gcprice;
var pwidth;
var plength;
var pvalid;
var i;
var pstock;
var pstocklength;
var pstockwidth;
var pstockprice;
var pNA="N/A";

var lft;
var lin;
var lmm;
var wft;
var win;
var wmm;
var num;
var dp;

var win2 = 0;
var lin2 = 0;

var typenum;

var wname = new Array();
var lname = new Array();

lname[1] = "l1mm";
wname[1] = "w1mm";
lname[2] = "l2m";
wname[2] = "w2m";
lname[3] = "l2cm";
wname[3] = "w2cm";
lname[4] = "l3cm";
wname[4] = "w3cm";
lname[5] = "l4ft";
wname[5] = "w4ft";
lname[6] = "l4in";
wname[6] = "w4in";
lname[7] = "l5in";
wname[7] = "w5in";

var perror="";

var mA=1;
var mB=1;
var mC=100;
var mD=50;

// Function Make Quote
// ------------------------------------------------------------------------------------------
function cquote(plength,pwidth) {

  gcoilingprice=Math.floor((plength/1000)*(pwidth/1000)*35.9375);
  
  // Make sure that these strings are in default settings
  pvalid=1;
  perror="";
  
  // Reset styling of elements to non-error colors
  document.getElementById('flength').style.color = '#000000';
  document.getElementById('fwidth').style.color = '#000000';
  
  for (i=1; i<8; i++) {
    document.getElementById(lname[i]).style.border="solid #000000 1px";
    document.getElementById(lname[i]).style.backgroundColor="#ffffff";
    document.getElementById(wname[i]).style.border="solid #000000 1px";
    document.getElementById(wname[i]).style.backgroundColor="#ffffff";
  }


  // Check that the length is shorter than the maximum length
  if (plength>maxlength) {
    pvalid=0;
	
	perror=perror+"Please call our sales team for tables longer than "+maxlength+"mm.<br><br> "
	document.getElementById('flength').style.color = '#ff0000';
	for (i=1; i<8; i++) {
      document.getElementById(lname[i]).style.border="solid #ff0000 1px";
      document.getElementById(lname[i]).style.backgroundColor="#ffbfbf";
    }
  }

  // Check that the length is longer than the minimum length
  if (plength<minlength) {
    pvalid=0;

	perror=perror+"Please call our sales team for tables  shorter than "+minlength+"mm.<br><br> "
	document.getElementById('flength').style.color = '#ff0000';
  	for (i=1; i<8; i++) {
      document.getElementById(lname[i]).style.border="solid #ff0000 1px";
      document.getElementById(lname[i]).style.backgroundColor="#ffbfbf";
    }
  }
  
  // Check that the width is smaller than the maximum width
  if (pwidth>maxwidth) {
    pvalid=0;

	perror=perror+"Please call our sales team for tables  wider than "+maxwidth+"mm."
	document.getElementById('fwidth').style.color = '#ff0000';
	for (i=1; i<8; i++) {
      document.getElementById(wname[i]).style.border="solid #ff0000 1px";
      document.getElementById(wname[i]).style.backgroundColor="#ffbfbf";
    }
  }

  // Check that the width is larger than the minimum width
  if (pwidth<minwidth) {
    pvalid=0;

	perror=perror+"Please call our sales team for tables  narrower than "+minwidth+"mm."
	document.getElementById('fwidth').style.color = '#ff0000';
	for (i=1; i<8; i++) {
      document.getElementById(wname[i]).style.border="solid #ff0000 1px";
      document.getElementById(wname[i]).style.backgroundColor="#ffbfbf";
    }
  }
 
  if (pstockprice<minprice) {
    pstockprice=minprice;
  }

  gcprice=Math.floor(mA*(((Math.floor(getQuote(plength,pwidth)*pfactor))*mB)+mC+((((plength+pwidth)*2)*mD)/1000))+ gcoilingprice);
  
  if (pvalid!=1) {
return 'N/A';
   }else{
  	return gcprice;
  }

}

function makequote(l,w){



   gccq=cquote(l,w);
 
  if (pvalid!=1) {
	document.getElementById('addtocart').style.visibility="hidden";
	document.getElementById('addtocartx').style.visibility="visible";
	document.getElementById('quoteerrormessage').style.color = '#ff0000';
	document.getElementById('onlinediscount').style.visibility="hidden";
	document.getElementById('fonlineprice').innerHTML='N/A';
    document.getElementById('fship').innerHTML='£ N/A';
    document.getElementById('gc_price').value=0;
    document.getElementById('gc_title').value= "No Item";
	
	
	
	
  }else{ 
	document.getElementById('addtocartx').style.visibility="hidden";
	document.getElementById('addtocart').style.visibility="visible";
	document.getElementById('onlinediscount').style.visibility="visible";
	document.getElementById('quoteerrormessage').style.color = '#000000';
    perror="Please note that all orders will be processed to the nearest millimetre. Imperial measurements are calculated at 1 inch = 25.4mm.";
	//gcprice=Math.floor(mA*(((Math.floor(getQuote(plength,pwidth)*pfactor))*mB)+mC+((((plength+pwidth)*2)*mD)/1000))+ gcoilingprice);

    }



 
    document.getElementById('gc_price').value="£"+gccq;
    document.getElementById('fship').innerHTML='£ '+standarddelivery;


	//alert(document.getElementById('chinsoil').value);

	
    pdes=pdes.replace('Custom','Bespoke');
    pdes=pdes.replace('Worktop','Table');
	
	
	  //document.getElementById('fprice').innerHTML=Math.ceil(gcprice*1.25);
	  //document.getElementById('fonlineprice').innerHTML=gcprice;
	  document.getElementById('fonlineprice').innerHTML=gccq;
	  document.getElementById('gc_title').value= pdes + " - " + l + "mm x " + w + "mm";
	
    //document.getElementById('gc_price').value="£"+gcprice ;
    document.getElementById('gc_price').value="£"+gccq ;


  
  document.getElementById('quoteerrormessage').innerHTML=perror;

}
// ------------------------------------------------------------------------------------------
// End Function Make Quote


// Function Adjust Price
// ------------------------------------------------------------------------------------------
function adjustprice() {
  getfeet();
  makequote(document.getElementById('l1mm').value,document.getElementById('w1mm').value);
}
// ------------------------------------------------------------------------------------------
// End Function Adjust Price



// Function Get Quote
// ------------------------------------------------------------------------------------------
function getquote(plength,pwidth) {
  document.getElementById('l1mm').value=plength;
  document.getElementById('w1mm').value=pwidth;
  makequote(plength,pwidth);
}
// ------------------------------------------------------------------------------------------
// End Function Get Quote


// Function Get Metric
// ------------------------------------------------------------------------------------------
function getmetric() {
  lft=document.getElementById('l4ft').value;
  lin=document.getElementById('l4in').value;
  wft=document.getElementById('w4ft').value;
  win=document.getElementById('w4in').value;
    
  wmm=((win*2) + (wft*24))*12.7;
  lmm=((lin*2) + (lft*24))*12.7;
  
  wmm=Math.round(wmm);
  lmm=Math.round(lmm);
  
  document.getElementById('l1mm').value=lmm;
  document.getElementById('w1mm').value=wmm;
  
  document.getElementById('l2m').value=(Math.floor(lmm/1000));
  document.getElementById('w2m').value=(Math.floor(wmm/1000));
  document.getElementById('l2cm').value=(lmm-(Math.floor(lmm/1000)*1000))/10;
  document.getElementById('w2cm').value=(wmm-(Math.floor(wmm/1000)*1000))/10;
  
  document.getElementById('l3cm').value=lmm/10;
  document.getElementById('w3cm').value=wmm/10;
  
  
  document.getElementById('fwidth').innerHTML=wmm;
  document.getElementById('flength').innerHTML=lmm;

  makequote(lmm,wmm);
}
// ------------------------------------------------------------------------------------------
// End Function Get Metric



// Function Get Feet
// ------------------------------------------------------------------------------------------
function getfeet() {
  lmm=Math.round(document.getElementById('l1mm').value);
  wmm=Math.round(document.getElementById('w1mm').value);
  
  lin=lmm/25.4;
  win=wmm/25.4;
  
  lft=Math.floor(lin/12);
  wft=Math.floor(win/12);
  
  lin=Math.round(((lin-(lft*12)))*100)/100; 
  win=Math.round(((win-(wft*12)))*100)/100;
  
  if (clicked!=2) {
    document.getElementById('l2m').value=(Math.floor(lmm/1000));
    document.getElementById('w2m').value=(Math.floor(wmm/1000));
    document.getElementById('l2cm').value=(lmm-(Math.floor(lmm/1000)*1000))/10;
    document.getElementById('w2cm').value=(wmm-(Math.floor(wmm/1000)*1000))/10;
  }
  
  if (clicked!=3) {
  document.getElementById('l3cm').value=lmm/10;
  document.getElementById('w3cm').value=wmm/10;
  }
  
  if (clicked!=4) {  
  document.getElementById('l4ft').value=lft;
  document.getElementById('l4in').value=lin;
  document.getElementById('w4ft').value=wft;
  document.getElementById('w4in').value=win;
  }
  
  if (clicked!=5) {
  document.getElementById('l5in').value=((lft*12)+lin);
  document.getElementById('w5in').value=((wft*12)+win);
  }
  
  document.getElementById('fwidth').innerHTML=wmm;
  document.getElementById('flength').innerHTML=lmm;
  
  makequote(lmm,wmm);  
}
// ------------------------------------------------------------------------------------------
// End Function Get Feet


// Function Round Number
// ------------------------------------------------------------------------------------------
function roundnum(num,dp) {
  num=num*(10^dp);
  num=Math.floor(num);
  num=num/(10^dp);
  return num;
}
// ------------------------------------------------------------------------------------------
// End Function Round Number


// Function conv
// ------------------------------------------------------------------------------------------
function conv(typenum) {

  clicked=typenum;

  if (typenum==1) {
    getfeet();
  }
  if (typenum==2) {
    document.getElementById('l1mm').value=Math.round((document.getElementById('l2m').value*1000)+(document.getElementById('l2cm').value*10));
    document.getElementById('w1mm').value=Math.round((document.getElementById('w2m').value*1000)+(document.getElementById('w2cm').value*10));
	getfeet();
  }
  if (typenum==3) {
    document.getElementById('l1mm').value=((document.getElementById('l3cm').value*10));
    document.getElementById('w1mm').value=((document.getElementById('w3cm').value*10));
	getfeet();
  }
  if (typenum==4) {
    lft=document.getElementById('l4ft').value;
    lin=document.getElementById('l4in').value;
    wft=document.getElementById('w4ft').value;
    win=document.getElementById('w4in').value;
  
    win2=Math.round(wft*12) + (win/10)*10;
	lin2=Math.round(lft*12) + (lin/10)*10;
  
    document.getElementById('l5in').value=lin2;
    document.getElementById('w5in').value=win2;
	getmetric();
  }
  if (typenum==5) {
    document.getElementById('l4ft').value=Math.floor(document.getElementById('l5in').value/12);
	document.getElementById('w4ft').value=Math.floor(document.getElementById('w5in').value/12);
    document.getElementById('l4in').value=(document.getElementById('l5in').value - Math.floor(document.getElementById('l5in').value/12)*12);
	document.getElementById('w4in').value=(document.getElementById('w5in').value - Math.floor(document.getElementById('w5in').value/12)*12);
	getmetric();
  }
}
// ------------------------------------------------------------------------------------------
// End Function conv




