// JavaScript Document
// selector.js: completes selection of ELRR components, prices and approximate postage
// (c) 2007 Teal Engineering Services Ltd
// All rights reserved   
// v3  1101    PV  Sea Pebble Ltd  Update prices, VAT rate
// v3  1001    PV  Sea Pebble Ltd  Update prices, VAT rate
// v2  0812    PV  Sea Pebble Ltd  Update VAT rate
// v1  080509  PV  Sea Pebble Ltd  Remove tube kit (incl tube)
// v0  071203  PV  Sea Pebble Ltd
function selectELRR () {

	// local variables: postage
  var postRackBase= 17.00 ; // postage for first rack
  var postRackIncr= 8.53 ;  // postage for subsequent racks
  var postKitBase= 1.57 ; // post for first kit 
  var postKitIncr= 1.57 ; // post for subsequent kits
  var postTubeBase= 17.00 ; // postage for first tube
  var postTubeIncr= 17.00 ; // postage for subsequent tubes
	
// local variables: names
  var nameRack= new Array ('ELRR-C 1300mm wide', 'ELRR-C 1300mm wide', 'ELRR-C 1600mm wide', 'ELRR-C 1800mm+ wide') ; // rack type names as fn of vehicle height
  var nameHBR= new Array ('HBS short handle bracket', 'HBR regular handle bracket', 'HBL long handle bracket', 'HBX extra long handle bracket') ; // HBR names as fn of vehicle height
  var nameKit= new Array ('FK37 fixing kit', 'FK50 fixing kit', 'FK60 fixing kit', 'FK70 fixing kit') ; // fixing kit names
  var nameKit3pt= new Array ('FK37 fixing kit (3pt)', 'FK50 fixing kit (3pt)', 'FK60 fixing kit (3pt)', 'FK70 fixing kit (3pt)') ; // fixing kit names

// local variables: indices
  var iRack= -1 ;
  var iKit= -1 ;
  
// local variables: prices
  var priceRack= new Array (295.20, 295.20, 332.50, 357.40) ; // rack prices as fn of vehicle height
  var priceKit= new Array (12.50, 30.83, 30.83, 36.67) ;   // fixing kit as fn of bar width 
  var priceKit3pt= new Array (15.00, 40.83, 40.83, 45.00) ;   // 3 point fixing kit as fn of bar width 
  var priceStdKit= 29.45 ; // SLB
  var priceTubeKit= 26.05 ; // KTCEX
  var priceLadderKit= 31.70 ; // KL01
  var priceCustomKit= 6.80 ; // KCF01
  var priceStraps= new Array (4.58, 5.42, 7.50) ;	// straps prices for 2m, 3m, 5m straps
	
// local variables: IDs of locations in form to which routine pastes
  var typeRackID= 'typeRack' ;
  var typeHBRID= 'typeHBR' ;
  var typeKitID= 'typeKit' ;
  var priceRackOnlyID= 'priceRackOnly' ;
  var priceKitOnlyID= 'priceKitOnly' ;
  var priceRackKitID= 'priceRackKit' ;
  var priceStdKitID= 'priceStdKit' ;
  var priceTubeKitID= 'priceTubeKit' ;
  var priceLadderKitID= 'priceLadderKit' ;
  var priceCustomKitID= 'priceCustomKit' ;
  var priceStraps2mID= 'priceStraps2m' ;
  var priceStraps3mID= 'priceStraps3m' ;
  var priceStraps5mID= 'priceStraps5m' ;
  var priceAccID= 'priceAcc' ;
  var postageID= 'postage' ;
  var ttlPriceID= 'ttlPrice' ;
  var ttlPriceVatID= 'ttlPriceVat' ;
	
// local variables: IDs of hidden form entries to which routine pastes - these appear in FormMail output email
  var priceSelectedRackOnlyID= 'priceSelectedRackEach' ;
  var typeSelectedRackOnlyID= 'typeSelectedRack' ;
  var priceSelectedKitOnlyID= 'priceSelectedKitEach' ;
  var typeSelectedFixingKitID= 'typeSelectedFixingKit' ;
  var priceSelectedRackKitID= 'priceSelectedRacksKitsSubtotal' ;
  var priceSelectedStdKitID= 'priceSelectedStdKitsTotal' ;
  var priceSelectedTubeKitID= 'priceSelectedTubeKitsTotal' ;
  var priceSelectedTubeID= 'priceSelectedTubesTotal' ;
  var priceSelectedLadderKitID= 'priceSelectedLadderKitsTotal' ;
  var priceSelectedCustomKitID= 'priceSelectedCustomKitsTotal' ;
  var priceSelectedStraps2mID= 'priceSelectedStraps2mTotal' ;
  var priceSelectedStraps3mID= 'priceSelectedStraps3mTotal' ;
  var priceSelectedStraps5mID= 'priceSelectedStraps5mTotal' ;
  var priceSelectedAccID= 'priceSelectedAccessoriesSubtotal' ;
  var priceSelectedPostageID= 'pricePostage' ;
  var ttlSelectedPriceID= 'priceAllIncPostage' ;
  var ttlSelectedPriceVatID= 'priceAllIncPostageVat' ;

// local variables: other
  var poundChar= "&pound;" ;
  var poundCharTxt= "£" ;
  var eachString= "<br />each" ;
  var incVatString= " inc VAT" ;
  var vatRate= 1.200 ;
	
// code

// local variables: collect from form
  var vehicleH= document.getElementById('vehicleHeight').value ; // vehicle height (cm)
  var barW= document.getElementById('barWidth').value ;         // roof bar width
  var nRacks= parseFloat(document.getElementById('nRacks').value) ;     // number of roof racks
  var nStdKit= parseFloat(document.getElementById('nStdKit').value) ; // extra standard kits
  var nTubeKit= parseFloat(document.getElementById('nTubeKit').value) ; // number of tube fixing kits 
  var nLadderKit= parseFloat(document.getElementById('nLadderKit').value) ; // number of ladder kits
  var nCustomKit= parseFloat(document.getElementById('nCustomKit').value) ; // number of ladder kits
  var nStraps2m= parseFloat(document.getElementById('nStraps2m').value) ; // number of 2m straps
  var nStraps3m= parseFloat(document.getElementById('nStraps3m').value) ; // number of 3m straps
  var nStraps5m= parseFloat(document.getElementById('nStraps5m').value) ; // number of 5m straps

// paste selected roof rack type and cost
  iRack= -1 ;
	ttlPriceRack= 0.0 ;
	switch( vehicleH ) {
		case "less than 1.5m": 
			iRack= 0 ;
			break ;
		case "1.5m to 1.8m": 
			iRack= 1 ;
			break ;
		case "1.8m to 2.0m": 
			iRack= 2 ;
			break ;
		case "2.0m to 2.4m": 
			iRack= 3 ;
	}
	if (iRack!=-1) {
 		ttlPriceRackOnly= priceRack[iRack] ;
		document.getElementById(typeRackID).innerHTML= nameRack[iRack] ;
		document.getElementById(typeHBRID).innerHTML= nameHBR[iRack] ;
		document.getElementById(priceRackOnlyID).innerHTML= poundChar + toCurrency(ttlPriceRackOnly) + eachString;
		document.getElementById(typeSelectedRackOnlyID).value= nameRack[iRack] + " with " + nameHBR[iRack] ;
		document.getElementById(priceSelectedRackOnlyID).value= poundCharTxt + toCurrency(ttlPriceRackOnly);
	}

// paste selected fixing kit type and cost
  iKit= -1 ;
	switch( barW ) {
		case "up to 37mm": 
			iKit= 0 ;
			break ;
		case "37mm to 50mm": 
			iKit= 1 ;
			break ;
		case "50mm to 60mm": 
			iKit= 2 ;
			break ;
		case "60mm to 70mm": 
			iKit= 3 ;
	}
//  work out price and whether 2pt (130cm or 160cm wide racks) or 3pt (180cm wide racks)
	if (iKit!=-1) {
		if (iRack==3) {
			var ttlPriceKitOnly= priceKit3pt[iKit] ;
			document.getElementById(typeKitID).innerHTML= nameKit3pt[iKit] ;
	  		document.getElementById(priceKitOnlyID).innerHTML= poundChar + ttlPriceKitOnly.toString() + eachString ;
			document.getElementById(typeSelectedFixingKitID).value= nameKit3pt[iKit] ;
			document.getElementById(priceSelectedKitOnlyID).value= poundCharTxt + toCurrency(ttlPriceKitOnly);
		}
		else {
			var ttlPriceKitOnly= priceKit[iKit] ;
			document.getElementById(typeKitID).innerHTML= nameKit[iKit] ;
	  		document.getElementById(priceKitOnlyID).innerHTML= poundChar + ttlPriceKitOnly.toString() + eachString ;
			document.getElementById(typeSelectedFixingKitID).value= nameKit[iKit] ;
			document.getElementById(priceSelectedKitOnlyID).value= poundCharTxt + toCurrency(ttlPriceKitOnly);
		}
	}
	
  var ttlPriceRackKit= (ttlPriceKitOnly + ttlPriceRackOnly) * nRacks ;
	document.getElementById(priceRackKitID).innerHTML= poundChar + toCurrency(ttlPriceRackKit) ;
	document.getElementById(priceSelectedRackKitID).value= poundCharTxt + toCurrency(ttlPriceRackKit) ;
	
// paste selected extra kit prices
	var ttlPriceStdKit= priceStdKit * nStdKit ;
  document.getElementById(priceStdKitID).innerHTML= poundChar + toCurrency(ttlPriceStdKit) ;
  document.getElementById(priceSelectedStdKitID).value= poundCharTxt + toCurrency(ttlPriceStdKit) ;

	var ttlPriceTubeKit= priceTubeKit * nTubeKit ;
	document.getElementById(priceTubeKitID).innerHTML= poundChar + toCurrency(ttlPriceTubeKit) ;
	document.getElementById(priceSelectedTubeKitID).value= poundCharTxt + toCurrency(ttlPriceTubeKit) ;

	var ttlPriceLadderKit= priceLadderKit * nLadderKit ;
	document.getElementById(priceLadderKitID).innerHTML= poundChar + toCurrency(ttlPriceLadderKit) ;
	document.getElementById(priceSelectedLadderKitID).value= poundCharTxt + toCurrency(ttlPriceLadderKit) ;
	
	var ttlPriceCustomKit= priceCustomKit * nCustomKit ;
	document.getElementById(priceCustomKitID).innerHTML= poundChar + toCurrency(ttlPriceCustomKit) ;
	document.getElementById(priceSelectedCustomKitID).value= poundCharTxt + toCurrency(ttlPriceCustomKit) ;
	
	var ttlPriceStraps2m= priceStraps[0] * nStraps2m ;
	document.getElementById(priceStraps2mID).innerHTML= poundChar + toCurrency(ttlPriceStraps2m) ;
	document.getElementById(priceSelectedStraps2mID).value= poundCharTxt + toCurrency(ttlPriceStraps2m) ;
	
	var ttlPriceStraps3m= priceStraps[1] * nStraps3m ;
	document.getElementById(priceStraps3mID).innerHTML= poundChar + toCurrency(ttlPriceStraps3m) ;
	document.getElementById(priceSelectedStraps3mID).value= poundCharTxt + toCurrency(ttlPriceStraps3m) ;
	
	var ttlPriceStraps5m= priceStraps[2] * nStraps5m ;
	document.getElementById(priceStraps5mID).innerHTML= poundChar + toCurrency(ttlPriceStraps5m) ;
	document.getElementById(priceSelectedStraps5mID).value= poundCharTxt + toCurrency(ttlPriceStraps5m) ;
	
	var ttlPriceKit= ttlPriceStdKit + ttlPriceTubeKit + ttlPriceLadderKit + ttlPriceCustomKit + ttlPriceStraps2m + ttlPriceStraps3m + ttlPriceStraps5m ;
	document.getElementById(priceAccID).innerHTML= poundChar + toCurrency(ttlPriceKit) ;
	document.getElementById(priceSelectedAccID).value= poundCharTxt + toCurrency(ttlPriceKit) ;
	
// compute and paste postage	 
	var postage= Math.min(nRacks, 1) * postRackBase + Math.max(nRacks-1, 0) * postRackIncr ; // racks
	var nKits= nStdKit + nTubeKit + nLadderKit ;
  postage+= Math.min(nKits, 1) * postKitBase + Math.max(nKits-1, 0) * postKitIncr ; // kits 
  document.getElementById(postageID).innerHTML= poundChar + toCurrency(postage) ;
  document.getElementById(priceSelectedPostageID).value= poundCharTxt + toCurrency(postage) ;

// compute and paste total price
  var ttlPrice= ttlPriceRackKit + ttlPriceKit + postage ;
	var ttlPriceVat= vatRate * ttlPrice ;
  document.getElementById(ttlPriceID).innerHTML= poundChar + toCurrency(ttlPrice) ;
  document.getElementById(ttlSelectedPriceID).value= poundCharTxt + toCurrency(ttlPrice) ;
  document.getElementById(ttlPriceVatID).innerHTML= poundChar + toCurrency(ttlPriceVat) ;
  document.getElementById(ttlSelectedPriceVatID).value= poundCharTxt + toCurrency(ttlPriceVat) + incVatString ;

} ;

// avoid unwanted precision in converting numeric value to string
function toCurrency (input) {
  var units= Math.floor(input) ;
	var cents= Math.round((input - units) * 100.0) ;
	var leadZ= "" ;
	if (cents < 10) {leadZ= "0"} ;
	return units + "." + leadZ + cents ;
} ;
	
