//update instructions:
// don't forget to change delcost from 14*numTubs to whatever
// then, obvisouly, change pallet quantities
// don't forget samplerequest too... and samplerequestsent
// and then change the cost of a sample on the base of every product


var twoCode = 'SA';


if (WM_readCookie('postcode')) {
			//slice off the first two characters
			pCode = WM_readCookie('postcode')
			upperCode = pCode.toUpperCase()
			twoCode = upperCode.substring(0,2)
		}

function popup(url) {
	newwindow=window.open(url,'explain','scrollbars, resizable, height=480, width=460');
	if (window.focus) {newwindow.focus()}
	return false;
	}


function twoDecimals ( numeric ) {
   if ( typeof( numeric ) == String ) tal = parseFloat( numeric );
   talTx = "0" +Math.round( 100.0*numeric ) +"0"
   if ( talTx.length < 5 ) {
       preZero = "_000";
       taltx = preZero.substring(0,6-talTx.length) +talTx.substring(1);
   };
   talTx = talTx.substring(1,talTx.length-3) +"."
           + talTx.substring(talTx.length-3,talTx.length-1);
   return talTx;
}; //end twoDecimals ( number ) -> string

	
function instantQuote() {
	var delCost;
	var current = new Date(); 
	var refBaseTime = current.getTime();
	refTimeString = new String(refBaseTime);
	refTimeLong = refTimeString.slice(1,9);
	refTimeFirstFour = refTimeLong.slice(0,4);
	refTimeLastFour = refTimeLong.slice(4,9);
	refTime = new String(refTimeFirstFour + '-' + refTimeLastFour);
	WM_setCookie('ref', refTime, 10);
	
	
	if (WM_readCookie('deliverymethod') == 'h_tub') {
		var abs_numTubs = WM_readCookie('quantity')/20;
		var numTubs = Math.ceil(abs_numTubs);
		delCost = 16*numTubs
	}

	if (WM_readCookie('deliverymethod') == 'h_self') {
		delCost = 0
	}

	if (WM_readCookie('deliverymethod') == 'h_bongo') {
		var abs_numPallets = WM_readCookie('quantity')/720
		var numPallets = Math.ceil(abs_numPallets)
		var numSixPallets = Math.floor(numPallets/6)
		var remSixPallets = numPallets-(numSixPallets*6)
		
		// set regular expressions (B\d means B followed by any digit)
		var regZoneZero  = /(SA)/i;
		var regZoneOne   = /(AL|B\d|BA|BB|BD|BL|BR|BS|CB|CH|CR|CV|CW|DA|DE|DN|DY|EN|GL|GU|HA|HD|HP|HR|HX|IG|KT|L\d|LE|LS|LU|M\d|MK|NG|NN|OL|OX|PE|PR|RG|RH|RM|S\d|SG|SK|SL|SM|SN|ST|TF|TW|UB|WA|WD|WF|WN|WR|WS|WV)/i; // E\d|N\d|NW|SE|
		var regZoneTwo = /(BH|BN|CF|CM|CO|DH|DL|DT|FY|HG|HU|IP|LA|LN|ME|NE|NP|NR|PO|SO|SP|SR|SS|TA|TS|YO)/i; //SW|W\d
		var regZoneThree  = /(CA|CT|EX|LD|LL|PL|TN|TQ|SY)/i;
		var regZoneFour  = /(DG|EH|FK|G\d|KA|KY|ML|TD|TR)/i;
		var regZoneFive  = /(AB|DD|IV|KW|PA|PH)/i;
		var regZoneSix = /(E\d|EC|N\d|NW|SE|SW|W\d|WC)/i
		
		//IT'S THE TWOCODE THING THAT'S PROBLEMATIC! SE11 ETC WILL BE HARD WORK. NEED TO USE A SPACE AS A SEPARATOR INSTEAD AND USE /d/d and /d for every code....
		//(E1|E2|EC|N1|N5|N7|NW1|NW5|NW6|NW8|SE1|SE11|SE18|SE17|SW1|SW3|SW4|SW5|SW6|SW7|SW8|SW9|SW10|SW11|SW13|SW18|W1|W2|W3|W4|W6|W8|W9|W10|W11|W12|W14|WC)/i
		// for the prices below, 13 pounds per pallet is added to cover bagging, packing etc.
// perform error-check -- does the postcode exist?
		
		if ((twoCode.search(regZoneZero) != 0) && (twoCode.search(regZoneOne) != 0) && (twoCode.search(regZoneTwo) != 0) && (twoCode.search(regZoneThree) != 0) && (twoCode.search(regZoneFour) != 0) && (twoCode.search(regZoneFive) != 0) && (twoCode.search(regZoneSix) != 0)) {
			remDelivery = "x";
			priceSixPallets = "x";
		}
			
		if (twoCode.search(regZoneZero) == 0) {
			if (remSixPallets == 0) {remDelivery = 0}
			if (remSixPallets == 1) {remDelivery = 44.25} //40.25
			if (remSixPallets == 2) {remDelivery = 88.5} //79.5
			if (remSixPallets == 3) {remDelivery = 132.75} //88.75
			if (remSixPallets == 4) {remDelivery = 177} //158
			if (remSixPallets == 5) {remDelivery = 221.25} //197.25
			priceSixPallets = 265.5 //236.5
		}
		if (twoCode.search(regZoneOne) == 0) {
			if (remSixPallets == 0) {remDelivery = 0}
			if (remSixPallets == 1) {remDelivery = 70.5}
			if (remSixPallets == 2) {remDelivery = 138.5}
			if (remSixPallets == 3) {remDelivery = 204}
			if (remSixPallets == 4) {remDelivery = 267}
			if (remSixPallets == 5) {remDelivery = 327.5}
			priceSixPallets = 385.5
		}
		if (twoCode.search(regZoneTwo) == 0) {
			if (remSixPallets == 0) {remDelivery = 0}
			if (remSixPallets == 1) {remDelivery = 64.25}
			if (remSixPallets == 2) {remDelivery = 126}
			if (remSixPallets == 3) {remDelivery = 185.25}
			if (remSixPallets == 4) {remDelivery = 242}
			if (remSixPallets == 5) {remDelivery = 296.25}
			priceSixPallets = 348
		}
		if (twoCode.search(regZoneThree) == 0) {
			if (remSixPallets == 0) {remDelivery = 0}
			if (remSixPallets == 1) {remDelivery = 70.5}
			if (remSixPallets == 2) {remDelivery = 138.5}
			if (remSixPallets == 3) {remDelivery = 204}
			if (remSixPallets == 4) {remDelivery = 267}
			if (remSixPallets == 5) {remDelivery = 327.5}
			priceSixPallets = 385.5
		}
		if (twoCode.search(regZoneFour) == 0) {
			if (remSixPallets == 0) {remDelivery = 0}
			if (remSixPallets == 1) {remDelivery = 79.25}
			if (remSixPallets == 2) {remDelivery = 156}
			if (remSixPallets == 3) {remDelivery = 230.25}
			if (remSixPallets == 4) {remDelivery = 302}
			if (remSixPallets == 5) {remDelivery = 371.25}
			priceSixPallets = 438
		}
		if (twoCode.search(regZoneFive) == 0) {
			if (remSixPallets == 0) {remDelivery = 0}
			if (remSixPallets == 1) {remDelivery = 98}
			if (remSixPallets == 2) {remDelivery = 193.5}
			if (remSixPallets == 3) {remDelivery = 286.5}
			if (remSixPallets == 4) {remDelivery = 377}
			if (remSixPallets == 5) {remDelivery = 465}
			priceSixPallets = 550.5
		}
		if (twoCode.search(regZoneSix) == 0) {
			if (remSixPallets == 0) {remDelivery = 0}
			if (remSixPallets == 1) {remDelivery = 78}
			if (remSixPallets == 2) {remDelivery = 153.5}
			if (remSixPallets == 3) {remDelivery = 226.5}
			if (remSixPallets == 4) {remDelivery = 297}
			if (remSixPallets == 5) {remDelivery = 365}
			priceSixPallets = 430.5
		} 
		delCost = (numSixPallets*priceSixPallets)+(remDelivery)
		// for h_bongo
	}
	var matCost = WM_readCookie('price')*WM_readCookie('quantity')/1000
	// vat calculation and deduction of 10% for CWO occurs here
	var matanddelCost = (matCost+delCost)*1
	var discountCost = matanddelCost
	if (discountCost >= 20) {
	discountCost = (matCost+delCost)*1
	}
	
	var vatCost = discountCost*0.175
	var totalCost = twoDecimals(discountCost+vatCost)

	WM_setCookie('deliverycost', delCost , 100000000)	
	WM_setCookie('materialcost', matCost, 100000000)	
	WM_setCookie('matanddel', matanddelCost, 100000000)
	WM_setCookie('discountcost', discountCost, 100000000)	
	WM_setCookie('VAT', vatCost, 100000000);
	WM_setCookie('price', totalCost, 100000000);

	totalCost = twoDecimals(totalCost);
	vatCost = twoDecimals(vatCost);
	discountCost = twoDecimals(discountCost);

	var totalCost_array=totalCost.split(".");
	var vatexcCost_array=discountCost.split(".");
		
	if (totalCost_array[0] == "") {totalCost_array[0] = '0'}
	if (vatexcCost_array[0] == "") {vatexcCost_array[0] = '0'}
		
		if (WM_readCookie('tax') == 'i_vatinc') {
		document.write('<span class="iqsymbol">&pound;</span><span class="iqpounds">' + totalCost_array[0] + '</span>.' + totalCost_array[1]);
		}
		if (WM_readCookie('tax') == 'i_vatexc') {
		document.write('<span class="iqsymbol">&pound;</span><span class="iqpounds">' + vatexcCost_array[0] + '</span>.' + vatexcCost_array[1]);
		}

	
/*	document.write("<div style=\"position: absolute; width: 50%; padding: 10px; top: 10px; right: 10px; z-index: 1; border: solid black 1px; background: #fff\"><p>Quantity:" + WM_readCookie('quantity') +"kg<br>Product: " + WM_readCookie('product') + "<br><br>Delivery Cost: " + twoDecimals(delCost) + "<br>Material Cost: " + twoDecimals(matCost) + "<br>Material + Delivery = " + twoDecimals(matCost+delCost) + "<br>Material + Delivery - 10% = " + twoDecimals(discountCost) + "<br>VAT = " + twoDecimals(WM_readCookie('VAT')) + "<br>(Material + Delivery -10%) + VAT = " + totalCost + "</p></div>"); */
	
}


function fillform() {

	if (WM_readCookie('customername') && WM_readCookie('quantity') && WM_readCookie('product')){
	    mySubj = new String('Order: ' + WM_readCookie('customername') + ' - ' + WM_readCookie('quantity') + 'kgs ' + WM_readCookie('product') + ' - Order Ref:' + refTime);
		document.iqd.subject.value = mySubj;
	}
	if (WM_readCookie('product')){
		document.iqd._product.value = WM_readCookie('product');
	}
	if (WM_readCookie('emailaddress')){
		document.iqd.email.value = WM_readCookie('emailaddress');
	}
	if (WM_readCookie('customername')){
		document.iqd.a_name.value = WM_readCookie('customername');
		}
	if (WM_readCookie('telephone')){
		document.iqd.b_tel.value = WM_readCookie('telephone');
		}
	if (WM_readCookie('emailaddress')){
		document.iqd.c_email.value = WM_readCookie('emailaddress');
		}
	if (WM_readCookie('housenameornumber')){
		document.iqd.d_house.value = WM_readCookie('housenameornumber');
		}
	if (WM_readCookie('streetorroad')){
		document.iqd.e_street.value = WM_readCookie('streetorroad');
		}
	if (WM_readCookie('towny')){
		document.iqd.f_town.value = WM_readCookie('towny');
		}
	if (WM_readCookie('postcode')){
		document.iqd.g_postcode.value = WM_readCookie('postcode');
		}
	if (WM_readCookie('deliverymethod')){
		if (WM_readCookie('deliverymethod') == 'h_self'){
			document.iqd.h_self.checked = true;
			}
		if (WM_readCookie('deliverymethod') == 'h_tub'){
			document.iqd.h_tub.checked = true;
			}
		if (WM_readCookie('deliverymethod') == 'h_bongo'){
			document.iqd.h_bongo.checked = true;
			}
		}
	if (WM_readCookie('tax') == 'i_vatinc'){
		document.iqd.i_vatinc.checked = true;
		}
	if (WM_readCookie('tax') == 'i_vatexc'){
		document.iqd.i_vatexc.checked = true;
		}
	if (WM_readCookie('tax') == ''){
		WM_setCookie('tax', 'i_vatinc', 100000000);
		}
	if (WM_readCookie('quantity')){
		document.iqd.j_quantity.value = WM_readCookie('quantity');
		}

		
// now checks that a full set of details exist to determine whether to request details
	if (WM_readCookie('customername') && WM_readCookie('telephone') && WM_readCookie('emailaddress') && WM_readCookie('housenameornumber') && WM_readCookie('streetorroad') && WM_readCookie('towny') && WM_readCookie('postcode') && WM_readCookie('deliverymethod') && WM_readCookie('tax') && WM_readCookie('quantity')){
	 	document.getElementById("instantquotedetails").style.display='none';
		document.getElementById("instantquote").style.display='inline'
		}
	}


function fillmoreform() {	
//fills hidden fields in the email to give the cost breakdown.


	var edelCost = WM_readCookie('deliverycost')	
	var ematCost = WM_readCookie('materialcost')
	var ematanddelCost = WM_readCookie('matanddel')
	var ediscountCost = WM_readCookie('discountcost')	
	var evatCost = WM_readCookie('VAT');
	var etotalCost = WM_readCookie('price');


/*	document.write("<div style=\"position: absolute; width: 50%; padding: 10px; top: 200px; right: 10px; z-index: 1; border: solid black 1px; background: #fff\"><p>Quantity:" + WM_readCookie('quantity') +"kg<br>Product: " + WM_readCookie('product') + "<br><br>Delivery Cost: " + twoDecimals(edelCost) + "<br>Material Cost: " + twoDecimals(ematCost) + "<br>Material + Delivery = " + twoDecimals(ematanddelCost) + "<br>Material + Delivery - 10% = " + twoDecimals(ediscountCost) + "<br>VAT = " + twoDecimals(evatCost) + "<br>(Material + Delivery -10%) + VAT = " + etotalCost + "</p></div>"); */


	document.iqd.delcost.value = twoDecimals(edelCost);
	document.iqd.matcost.value = twoDecimals(ematCost);
	document.iqd.matndel.value = twoDecimals(ematanddelCost);
	document.iqd.less10p.value = twoDecimals(ediscountCost);
	document.iqd.____vat.value = twoDecimals(evatCost);
}

function Form_Validator(theForm)
{
  var regCommas   = /[^0-9]/;  
  var quantCheck = theForm.j_quantity.value;
  if (theForm.j_quantity.value == "") {
    alert("Please enter a quantity");
    theForm.j_quantity.focus();
    return (false);
  }
  if (quantCheck.search(regCommas) != -1) {
    alert("Please use numbers only -- no commas, spaces, letters or spaces.");
    theForm.j_quantity.focus();
    return (false);
  }
  if (theForm.a_name.value == "") {
    alert("Please enter your name");
    theForm.a_name.focus();
    return (false);
  }
  if (theForm.b_tel.value == "") {
    alert("Please enter your telephone number");
    theForm.b_tel.focus();
    return (false);
  }
  if (theForm.c_email.value == "") {
    alert("Please enter your email address");
    theForm.c_email.focus();
    return (false);
  }
  if (theForm.d_house.value == "") {
    alert("Please enter your house name or number");
    theForm.d_house.focus();
    return (false);
  }
  if (theForm.e_street.value == "") {
    alert("Please enter your street or road");
    theForm.e_street.focus();
    return (false);
  }
  if (theForm.f_town.value == "") {
    alert("Please enter your town");
    theForm.f_town.focus();
    return (false);
  }
  if (WM_readCookie('emailaddress')){
	document.iqd.email.value = WM_readCookie('emailaddress');
  }
  if (theForm.g_postcode.value == "") {
    alert("Please enter your postcode");
    theForm.g_postcode.focus();
    return (false);
  }

  var regZones  = /(SA|B\d|WS|AL|BA|BB|BD|BL|BR|BS|CB|CH|CR|CV|CW|DA|DE|DN|DY|E\d|EN|GL|GU|HA|HD|HP|HR|HX|IG|KT|L\d|LE|LS|LU|M\d|MK|N\d|NG|NN|NW|OL|OX|PE|PR|RG|RH|RM|S\d|SE|SG|SK|SL|SM|SN|ST|TF|TW|UB|WA|WD|WF|WN|WR|WV|BH|BN|CF|CM|CO|CT|DH|DL|DT|EC|FY|HG|HU|IP|LA|LN|ME|NE|NP|NR|PO|SO|SP|SR|SS|SW|TA|TN|TS|W\d|WC|YO|CA|EX|LD|LL|PL|SY|TQ|DG|EH|FK|G\d|KA|KY|ML|TD|TR|AB|DD|IV|KW|PA|PH)/i;

	if (theForm.g_postcode.value != "") {
		//slice off the first two characters
		pCode = theForm.g_postcode.value
		upperCode = pCode.toUpperCase()
		twoCode = upperCode.substring(0,2)
		if (twoCode.search(regZones) != 0) {
			alert("The postcode you have entered is not a valid UK mainland postcode. Please enter a valid postcode.");
    		theForm.g_postcode.focus();
   			return (false);
		}
	}

  return (true);
}
	
	
	
function quoteDetails() {
	document.write('Based on <strong>' + WM_readCookie('quantity') + 'kg</strong> (' + WM_readCookie('quantity')/1000 + ' tonnes)<br>');
	if (WM_readCookie('deliverymethod') == 'h_bongo') {
	document.write('delivered to <strong>' + WM_readCookie('postcode') + '</strong> in <strong>720kg <a href="explainbongobags.html" title="Tell me more about Bongo Bags - opens in new window" target="explain" onclick="popup(\'explainbongobags.html\');">Bongo Bags</a></strong>.<br>');}
	if (WM_readCookie('deliverymethod') == 'h_tub') {
	document.write('delivered to <strong>' + WM_readCookie('postcode') + '</strong> in <strong>20kg <a href="explaintinytubs.html" title="Tell me more about tiny tubs - opens in new window" target="explain" onclick="popup(\'explaintinytubs.html\');">Tiny Tubs</a></strong> (actual quantity may vary by 10%).<br>');}
	if (WM_readCookie('deliverymethod') == 'h_self') {
	document.write('that I will <strong><a href="explainselfcollect.html" title="Tell me more about collecting material myself - opens in new window" target="explain" onclick="popup(\'explainselfcollect.html\');">collect myself</a></strong>.<br>');}
	if (WM_readCookie('deliverymethod') == 'h_self') {
		if (WM_readCookie('tax') == 'i_vatinc') {
		document.write('Price includes aggregate levy (where applicable) and VAT.');
		}
		if (WM_readCookie('tax') == 'i_vatexc') {
		document.write('Price includes aggregate levy (where applicable), but excludes VAT.');
		}
	}
	else if (WM_readCookie('tax') == 'i_vatinc') {
	document.write('Price includes aggregate levy (where applicable), delivery and VAT.');
	}
	else if (WM_readCookie('tax') == 'i_vatexc') {
	document.write('Price includes aggregate levy (where applicable) and delivery, but excludes VAT.');
	}
}
