﻿//  simple paddle length selector
//  (c) Sporting Composites, 2007
//  All rights reserved 
//  v0  070717 PV, Sea Pebble Ltd

function paddleSelect (OutputElement) {

//  writes selected paddle length to DOM element OutputElement
//  reads one, optionally two, user height fields and computes paddle length
//  paddle length is extracted by simple table lookup
//  input DOM elements are
//		userHeightA  user supplied height, required
//		userHeightB  user supplied height, required only if ft/in selected
//      paddlingType user selected: see switch (uType) statement below for permitted values
//      lengthUnits  user selected units, one of:
//        'cm'  userHeightA is height in cm
//        'in'  userHeightA is height in inches
//        'ft'  userHeightA is height in feet, userHeightB is height in incremental inches

//  input variables
	var uHeightString= document.getElementById('userHeightA').value ; 
	var uTypeString= document.getElementById('paddlingType').value ; 
	var uUnitsString= document.getElementById('lengthUnits').value ; 
	
//  local variables
	var inToCm= 2.54 ; // inches to cm
	var paddleUnitCm= 'cm' ;
	var paddleUnitIn= 'in' ;
	var resultStyleStart= '<b>' ;
	var resultStyleStop= '</b>' ;
	
//  arrays of heights and paddle lengths by paddling type: kayak paddles
	var pKHeightsSeaT= new Array (150,   161,   172,   177,   184,   194,   200) ;
	var pKLengthsSeaT= new Array (210,   215,   217.5, 220,   225,   230,   230) ;
	var pKHeightsGPur= new Array (150,   161,   173,   181,   187,   194,   200) ;
	var pKLengthsGPur= new Array (195,   197.5, 200,   202.5, 205,   210,   210) ;
	var pKHeightsPlay= new Array (150,   161,   173,   181,   188,   196,   200) ;
	var pKLengthsPlay= new Array (180,   185,   190,   195, 197.5,   200,   200) ;
	var pKHeightsKids= new Array (105,   123,   138,   153,   170) ;
	var pKLengthsKids= new Array (180,   185,   190,   195,   195) ;
//  see sw_07_111a_paddle_selector_build.xls for derivation

//  arrays of heights and paddle lengths by paddling type: canoe paddles
	var pCHeightsKids= new Array (105,   115,   138,   162,   170) ;
	var pCLengthsKids= new Array (120,   125,   130,   135,   135) ;
	var pCHeightsAdul= new Array (135,   146,   161,   176,   187,   195,   200) ;
	var pCLengthsAdul= new Array (130,   135,   140,   145,   150,   155,   155) ;
	var pCHeightsAduB= new Array (135,   138,   146,   161,   176,   187,   195,   200) ;
	var pCLengthsAduB= new Array (130,   135,   140,   145,   150,   155,   160,   160) ;
	var pCHeightsStan= new Array (170,   174,   177,   179,   182,   184,   187) ;
	var pCLengthsStan= new Array ( 76,    77,    78,    79,    80,    81,    82) ;
//  see sw_07_111b_paddle_selector_build.xls for derivation

//  warning strings
 	var outOfRangeNote=	'*NB: the height you have entered is outside the range of our automated selector. Please get in touch directly to discuss your needs.' ; 
	var inRangeNote= 'Paddle recommendations are an approximate guide only. If in doubt please get in touch directly to discuss your needs.' ;
	
// code

// choose relevant array based on selected paddling type, set paddle unit string also
    paddleUnitString= paddleUnitCm ; // default is cm
	uType= uTypeString.toLowerCase() ;
	switch (uType) {

//      kayaks
		case "kkids":
			pHeights= pKHeightsKids ;
			pLengths= pKLengthsKids ;
			break ;
		case "kplay":
			pHeights= pKHeightsPlay ;
			pLengths= pKLengthsPlay ;
			break ;
		case "kgpur":
			pHeights= pKHeightsGPur ;
			pLengths= pKLengthsGPur ;
			break ;
		case "kseat":
			pHeights= pKHeightsSeaT ;
			pLengths= pKLengthsSeaT ;
			break ;

//      canoes
		case "ckids":
			pHeights= pCHeightsKids ;
			pLengths= pCLengthsKids ;
			break ;
		case "cadul":
			pHeights= pCHeightsAdul ;
			pLengths= pCLengthsAdul ;
			break ;
		case "cadub":
			pHeights= pCHeightsAduB ;
			pLengths= pCLengthsAduB ;
			break ;
		case "cstan":
			pHeights= pCHeightsStan ;
			pLengths= pCLengthsStan ;
		    paddleUnitString= paddleUnitIn ; // standup surf paddles in inches
			break ;
	}

//  check for empty text submission - return, no action yet required
    if (uHeightString=="") {return} ;
		
//  check for non numeric input: only required for cm or inch input
    uHeight= parseFloat(uHeightString) ;
	if (isNaN(uHeight)) {
    	resultString= "height must be a number" ;
		document.getElementById(OutputElement).innerHTML= resultString ; 
    	return ;
    }		
	
//  setup pointers to bottom and top of selected array
    var iBottom= 0 ;
    var iTop= pLengths.length-1 ;
    var chooseLength= pLengths[iBottom] ;

//  convert to cm if needed
	uUnits= uUnitsString.toLowerCase() ;
	switch (uUnits) {
 		case 'in':
 			uHeight= uHeight * inToCm  ;
			break ;
 		case 'ft':
//          feet and inches to uHeight is feet, need to get inches from uHeightB 
			uHeightStringB= document.getElementById('userHeightB').value ; 
		    uHeightB= parseFloat(uHeightStringB) ;
 			uHeight= (uHeight * 12 + uHeightB) * inToCm  ;
			break ;
	}

//  check for underflow
    if (uHeight < pHeights[iBottom]) {
    	resultString= pLengths[iBottom].toString() + paddleUnitString + " or less*" ;
		resultString= resultStyleStart + resultString + resultStyleStop ;
		document.getElementById(OutputElement).innerHTML= resultString ; 
 		document.getElementById('note').innerHTML= outOfRangeNote ;
    	return ;
    }		

// check for overflow
    if (uHeight >= pHeights[iTop]) {
    	resultString= pLengths[iTop].toString() + paddleUnitString + " or more*" ;
		resultString= resultStyleStart + resultString + resultStyleStop ;
		document.getElementById(OutputElement).innerHTML= resultString ; 
 		document.getElementById('note').innerHTML= outOfRangeNote ;
    	return ;
    }		

//  clear out of range note
		document.getElementById('note').innerHTML= inRangeNote ;

//  find in array
	for (var i in pHeights) {
		if (uHeight < pHeights[i]) {break} ;
		chooseLength= pLengths[i] ;
	}
  resultString= resultStyleStart + chooseLength.toString() + paddleUnitString + resultStyleStop ;
	document.getElementById(OutputElement).innerHTML= resultString ; 

} ;

function setPaddleSelectUnits (unitSelectElement) {

// takes an input selector string from unitSelectElement
// for cm and inch selectors, writes appropriate unit field to unitsCellA
// for ft/inch selector, writes appropriate unit field to unitsCellA and unitsCellB
// and writes selector HTML to valueCellA and valueCellB

//  permitted selector values are cm, in or ft

//  writes to the following DOM elements which must exist:
//		valueCellA	element for entering the (required) first height value
//      valueCellB  element for entering the (ft/in only) 2nd height value

// 	local vars: HTML fields
	var cmHtml= '<input type="text" name="userHeightA" id="userHeightA"  size="6">cm'
	var inHtml= '<input type="text" name="userHeightA" id="userHeightA"  size="6">inches'
	var feetHtml= '<select name="userHeightA" id="userHeightA" size="1"><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option></select>feet'
	var inchesHtml= '<select name="userHeightB" id="userHeightB" size="1"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option  value="11">11</option></select>inches'
	var paddleUnitCm= '<p style="align:left;">cm</p>' ;
	var paddleUnitIn= '<p style="align:left;">inches</p>' ;
	var paddleUnitFt= '<p style="align:left;">feet</p>' ;
	
//  code

//  get the selector for the measurement units
	uUnitsString= document.getElementById(unitSelectElement).value ;
	
//  select appropriate action
	uUnits= uUnitsString.toLowerCase() ;
	switch (uUnits) {
		case "cm":
			document.getElementById('valueCellA').innerHTML= cmHtml ; 
			document.getElementById('valueCellB').innerHTML= '' ; 
			break ;
		case "in":
			document.getElementById('valueCellA').innerHTML= inHtml ; 
			document.getElementById('valueCellB').innerHTML= '' ; 
			break ;
		case "ft":
			document.getElementById('valueCellA').innerHTML= feetHtml ; 
			document.getElementById('valueCellB').innerHTML= inchesHtml ; 
	}
	
}
