/* Set of conversion routines between various different measurements*/

/* Input: ozs as number, return stone lbs ozs as a string */
function ozsToStoneLbOzs(inputOzs) {

   parseOzs(inputOzs)
   return stone.toString() + " " + lbs.toString() + " " + ozs.toString()
}
/* Input: ozs as number, return stone lbs ozs as a string with text */
function ozsToStoneLbOzsTxt(inputOzs) {

   parseOzs(inputOzs)
   stoneLbOzs = ""

   stoneLbOzs += stone.toString() + " stone"

   lbsS = ""
   if (lbs != 1) lbsS = "s"
   stoneLbOzs += " " + lbs.toString() + " lb" + lbsS

   ozsS = ""
   if (ozs != 1) ozsS = "s"
   stoneLbOzs += " " + ozs.toString() + " oz" + ozsS

   return stoneLbOzs
}
function parseOzs(next) {

   stone = next/224
   next  = next%224
   stone = parseInt(stone)

   lbs   = next/16
   next  = next%16
   lbs = parseInt(lbs)

   ozs   = next.toFixed(0)
}

/* Input: string containing stone pounds ounces return weight in ozs */
function stoneToOzs(weightString) {
   weightarray =  weightString.split(/\s* \s*/)
   weightl = weightarray.length
   for (i = 0; i < weightl; i++) {
      weightarray[i] = parseFloat(weightarray[i])
   }
   switch(weightl) {
      case 1:
         ozs = weightarray[0]*224
      break;
      case 2:
         ozs = weightarray[0]*224 + weightarray[1]*16
      break;
      case 3:
         ozs = weightarray[0]*224 + weightarray[1]*16 + weightarray[2]
      break;
      default:
         ozs = 0
   }
   return ozs
}

/* Input: string containing hh mm ss, return time in seconds */
function timeToSecs(timeString) {

   timearray =  timeString.split(/\s* \s*/)
   timel = timearray.length
   for (i = 0; i < timel; i++) {
      timearray[i] = parseFloat(timearray[i])
   }
   switch(timel) {
      case 1:
         secs = timearray[0]*3600
      break;
      case 2:
         secs = timearray[0]*3600 + timearray[1]*60
      break;
      case 3:
         secs = timearray[0]*3600 + timearray[1]*60 + timearray[2]
      break;
      default:
         secs = 0
   }
   return secs
}
/* Input: secs as number, return hh mm ss as a string */
function secsToHhmmss(inputSecs) {

   parseSecs(inputSecs)
   return hh.toString() + " " + mm.toString() + " " + ss.toString()
}
/* Input: secs as number, return hh mm ss as a string with text */
function secsToHhmmssTxt(inputSecs) {

   parseSecs(inputSecs)
   hhmmss = ""

   essH = ""
   if (hh != 1) essH = "s"
   hhmmss += hh.toString() + " hour" + essH

   essM = ""
   if (mm != 1) essM = "s"
   hhmmss += " " + mm.toString() + " minute" + essM

   essS = ""
   if (ss != 1) essS = "s"
   hhmmss += " " + ss.toString() + " second" + essS

   return hhmmss
}
function parseSecs(next) {

   hh    = next/3600
   hh    = parseInt(hh)
   next  = next%3600

   next  = next.toFixed(0)
   mm    = next/60
   mm    = parseInt(mm)
   next  = next%60

   ss   = next.toFixed(0)
}

/* Input: string containing miles yards feet inches change to inches */
function myfiToInches(inputMyfi) {

   distArray =  inputMyfi.split(/\s* \s*/)
   distl = distArray.length
   for (i = 0; i < distl; i++) {
      distArray[i] = parseFloat(distArray[i])
   }

   switch(distl) {
      case 1:
         inches = distArray[0]*63360
      break;
      case 2:
         inches = distArray[0]*63360 + distArray[1]*36
      break;
      case 3:
         inches = distArray[0]*63360 + distArray[1]*36 + distArray[2]*12
      break;
      case 4:
         inches = distArray[0]*63360 + distArray[1]*36 + distArray[2]*12 + distArray[3]
      break;
      default:
         inches = 0
   }
   return inches
}


/* Input: inches as number, return ff ii  */
function inchesToFi(inputInches) {

   parseInches2(inputInches)
   return ff.toString() + " " + ii.toString()
}
/* Input: inches as number, return mi yy ff ii  */
function inchesToMyfi(inputInches) {

   parseInches(inputInches)
   return mi.toString() + " " + yy.toString() + " " + ff.toString() + " " + ii.toString()
}
/* Input: inches as number, return ff ii as a string with text */
function inchesToFiTxt(inputInches) {

   parseInches2(inputInches)
   miyyss = ""

   essF = ""
   if (ff != 1) essF = " feet"
   else         essF = " foot"
   miyyss += ff.toString() +  essF

   essI = ""
   if (ii != 1) essI = "es"
   miyyss += " " + ii.toString() + " inch" + essI

   return miyyss
}
/* Input: inches as number, return mi yy ff ii as a string with text */
function inchesToMyfiTxt(inputInches) {

   parseInches(inputInches)
   miyyss = ""

   essM = ""
   if (mi != 1) essM = "s"
   miyyss += mi.toString() + " mile" + essM

   essY = ""
   if (yy != 1) essY = "s"
   miyyss += " " + yy.toString() + " yard" + essY

   essF = ""
   if (ff != 1) essF = " feet"
   else         essF = " foot"
   miyyss += " " + ff.toString() +  essF

   essI = ""
   if (ii != 1) essI = "es"
   miyyss += " " + ii.toString() + " inch" + essI

   return miyyss
}
function parseInches(next) {

   mi    = next/63360
   next  = next%63360
   mi    = parseInt(mi)

   yy    = next/36
   next  = next%36
   yy    = parseInt(yy)

   ff    = next/12
   next  = next%12
   ff    = parseInt(ff)

   ii    = next.toFixed(0)
}
function parseInches2(next) {
   ff    = next/12
   next  = next%12
   ff    = parseInt(ff)

   ii    = next.toFixed(2)
}

/* Input: string, should contain a number */
/* number can be list of digits or digits with a decimal place.  Any number of leading and trailing blanks */
function number(list) {
   if (/^(\s*\d+\s*$)|(\d+[.]\d*\s*$)/.test(list))
        {return true  }
   else {return false }
}
/* Input: string, should contain list of numbers*/
/* number can be list of digits or digits with a decimal place.  Any number of leading and trailing blanks */
function numberList(list) {
   if (/^((\s*\d+\s*)|(\s*\d+[.]\d*\s*))+$/.test(list))
        {return true  }
   else {return false }
}
/* Input: string, should contain list of integers*/
/* integer is just list of digits.  Any number of leading and trailing blanks */
function integerList(list) {
   if (/^(\s*\d+\s*)+$/.test(list))
        {return true  }
   else {return false }
}

function trim(s) {
        s = s.replace(/(^\s*)|(\s*$)/gi,"");
        s = s.replace(/[ ]{2,}/gi," ");
        s = s.replace(/\n /,"\n");
        return s;
}

function ozsToKilos(inputOzs) {
   return(parseFloat((inputOzs*0.0283495231).toFixed(3)))
}
function kilosToOzs(inputKilos) {
   return(parseFloat((inputKilos*35.2739619).toFixed(3)))
}
function kmToMiles(inputKm) {
   return(parseFloat((inputKm*0.621371192).toFixed(3)))
}
function milesToKm(inputMiles) {
   return(parseFloat((inputMiles*1.609344).toFixed(3)))
}

function mToIn(inputM) {
   return(parseFloat((inputM*39.3700787).toFixed(3)))
}

function cmToIn(inputCm) {
   return(parseFloat((inputCm*0.393700787).toFixed(3)))
}
function inToCm(inputInches) {
   return(parseFloat((inputInches*2.54).toFixed(3)))
}
function inToM(inputInches) {
   return(parseFloat((inputInches*0.0254).toFixed(3)))
}


