Rob van der Woude's Scripting Pages

JavaScript source code for conversions

  1. // JavaScript functions for robvanderwoude.com
  2. // (c) 2010 Rob van der Woude
  3. // https://www.robvanderwoude.com
  4.  
  5. function changedAcre( ) {
  6. 	var areaAcre = parseFloat( document.AreaConversion1.Acre.value );
  7. 	var areaSqM = areaAcre * 4046.8564224;
  8. 	var areaAre = areaSqM / 100;
  9. 	var areaSqKm = areaSqM / 1000000;
  10. 	var areaHectare = areaSqM / 10000;
  11. 	var areaSqYd = areaSqM / 0.83612736;
  12. 	var areaSqFt = areaSqYd * 9;
  13. 	var areaSqIn = areaSqFt * 144;
  14. 	var areaSqMi = areaSqM / 2589988.110336;
  15. 	document.AreaConversion1.Acre.value = areaAcre.toFixed(4)
  16. 	document.AreaConversion1.Are.value = areaAre.toFixed(4)
  17. 	document.AreaConversion1.Hectare.value = areaHectare.toFixed(5)
  18. 	document.AreaConversion1.SqFt.value = areaSqFt.toFixed(2)
  19. 	document.AreaConversion1.SqIn.value = areaSqIn.toFixed(1)
  20. 	document.AreaConversion2.SqKm.value = areaSqKm.toFixed(6)
  21. 	document.AreaConversion2.SqM.value = areaSqM.toFixed(2)
  22. 	document.AreaConversion2.SqMi.value = areaSqMi.toFixed(6)
  23. 	document.AreaConversion2.SqYd.value = areaSqYd.toFixed(2)
  24. 	return true;
  25. }
  26.  
  27. function changedAre( ) {
  28. 	var areaAre = parseFloat( document.AreaConversion1.Are.value );
  29. 	var areaSqM = areaAre * 100;
  30. 	var areaSqKm = areaSqM / 1000000;
  31. 	var areaHectare = areaSqM / 10000;
  32. 	var areaAcre = areaSqM / 4046.8564224;
  33. 	var areaSqYd = areaSqM / 0.83612736;
  34. 	var areaSqFt = areaSqYd * 9;
  35. 	var areaSqIn = areaSqFt * 144;
  36. 	var areaSqMi = areaSqM / 2589988.110336;
  37. 	document.AreaConversion1.Acre.value = areaAcre.toFixed(4)
  38. 	document.AreaConversion1.Are.value = areaAre.toFixed(4)
  39. 	document.AreaConversion1.Hectare.value = areaHectare.toFixed(5)
  40. 	document.AreaConversion1.SqFt.value = areaSqFt.toFixed(2)
  41. 	document.AreaConversion1.SqIn.value = areaSqIn.toFixed(1)
  42. 	document.AreaConversion2.SqKm.value = areaSqKm.toFixed(6)
  43. 	document.AreaConversion2.SqM.value = areaSqM.toFixed(2)
  44. 	document.AreaConversion2.SqMi.value = areaSqMi.toFixed(6)
  45. 	document.AreaConversion2.SqYd.value = areaSqYd.toFixed(2)
  46. 	return true;
  47. }
  48.  
  49. function changedAtmosphere( ) {
  50. 	var pressureAtm = parseFloat( document.PressureConversion.Atmosphere.value );
  51. 	var pressureBar = pressureAtm * 1.01325;
  52. 	var pressurePa  = pressureBar * 100000;
  53. 	var pressurePsi = pressureBar * 14.5037744;
  54. 	document.PressureConversion.Atmosphere.value = pressureAtm.toFixed(3);
  55. 	document.PressureConversion.Bar.value = pressureBar.toFixed(3);
  56. 	document.PressureConversion.Pascal.value = pressurePa.toFixed(0);
  57. 	document.PressureConversion.Psi.value = pressurePsi.toFixed(3);
  58. 	return true;
  59. }
  60.  
  61. function changedBar( ) {
  62. 	var pressureBar = parseFloat( document.PressureConversion.Bar.value );
  63. 	var pressureAtm = pressureBar / 1.01325;
  64. 	var pressurePa  = pressureBar * 100000;
  65. 	var pressurePsi = pressureBar * 14.5037744;
  66. 	document.PressureConversion.Atmosphere.value = pressureAtm.toFixed(3);
  67. 	document.PressureConversion.Bar.value = pressureBar.toFixed(3);
  68. 	document.PressureConversion.Pascal.value = pressurePa.toFixed(0);
  69. 	document.PressureConversion.Psi.value = pressurePsi.toFixed(3);
  70. 	return true;
  71. }
  72.  
  73. function changedBarrel( ) {
  74. 	var numBarrel = parseFloat( document.VolumeConversion.Barrel.value );
  75. 	var numGallon = numBarrel * 42;
  76. 	var numPint = numGallon * 8;
  77. 	var numFlOz = numGallon * 128;
  78. 	var numLitre = numPint * 0.473176473;
  79.     document.VolumeConversion.Barrel.value = numBarrel.toFixed(3);
  80.     document.VolumeConversion.FlOz.value = numFlOz.toFixed(1);
  81.     document.VolumeConversion.Gallon.value = numGallon.toFixed(3);
  82.     document.VolumeConversion.Litre.value = numLitre.toFixed(3);
  83.     document.VolumeConversion.Pint.value = numPint.toFixed(2);
  84. 	return true;
  85. }
  86.  
  87. function changedCelsius() {
  88. 	var tempC = Number( document.TemperatureConversion.Celsius.value );
  89. 	var tempK = tempC + 273.15;
  90. 	var tempF = tempK * 1.8 - 459.67;
  91. 	document.TemperatureConversion.Celsius.value = tempC.toFixed(2);
  92. 	document.TemperatureConversion.Fahrenheit.value = tempF.toFixed(2);
  93. 	document.TemperatureConversion.Kelvin.value = tempK.toFixed(2);
  94. 	return true;
  95. }
  96.  
  97. function changedDecimal( ) {
  98. 	var numI = parseInt( document.RomanNumerals.Decimal.value );
  99. 	if ( numI < 1 || numI > 4999 ) {
  100. 		alert( 'Integer must be in the range of 1..4999' );
  101. 		return false;
  102. 	} else{
  103. 		numR = Decimal2Roman( numI );
  104. 		document.RomanNumerals.Roman.value = numR;
  105. 		return true;
  106. 	}
  107. }
  108.  
  109. function changedFahrenheit() {
  110. 	var tempF = Number( document.TemperatureConversion.Fahrenheit.value );
  111. 	var tempK = ( tempF + 459.67 ) / 1.8;
  112. 	var tempC = tempK - 273.15;
  113. 	document.TemperatureConversion.Celsius.value = tempC.toFixed(2);
  114. 	document.TemperatureConversion.Fahrenheit.value = tempF.toFixed(2);
  115. 	document.TemperatureConversion.Kelvin.value = tempK.toFixed(2);
  116. 	return true;
  117. }
  118.  
  119. function changedFeet( ) {
  120. 	var strFeetInch = document.LengthConversion.Feet.value;
  121. 	var pattern1 = /\d+(\.\d+)?[\'\"]$/;
  122. 	var errmsg = "Feet/inches value must be in feet'inches\" or feet' or inches\" format";
  123. 	if ( strFeetInch.search( pattern1 ) == -1 ) {
  124. 		alert( errmsg );
  125. 		return false;
  126. 	}
  127. 	var pattern2 = /^(((\d+)\')?)(((\d+(\.\d+)?)\")?)$/;
  128. 	if ( strFeetInch.search( pattern2 ) == -1 ) {
  129. 		alert( errmsg );
  130. 		return false;
  131. 	}
  132. 	var posFt = strFeetInch.indexOf( "'" );
  133. 	var posIn = strFeetInch.indexOf( '"' );
  134. 	if ( posFt > -1 && posIn > -1 && posIn < posFt ) {
  135. 		alert( errmsg );
  136. 		return false;
  137. 	}
  138. 	if ( posFt > -1 && posIn > -1 ) {
  139. 		var numFeet = parseInt( strFeetInch.substring( 0, posFt ) );
  140. 		var numInch = parseFloat( strFeetInch.substring( posFt + 1, posIn ) );
  141. 	} else if ( posFt == -1 && posIn == -1 ) {
  142. 		var numFeet = 0;
  143. 		var numInch = 0;
  144. 	} else if ( posFt > -1 ) {
  145. 		var numFeet = parseInt( strFeetInch.substring( 0, posFt ) );
  146. 		var numInch = 0;
  147. 	} else {
  148. 		var numFeet = 0;
  149. 		var numInch = parseFloat( strFeetInch.substring( 0, posIn ) );
  150. 	}
  151. 	if ( numInch > 11 ) {
  152. 		numFeet = numFeet + parseInt( numInch / 12 );
  153. 		numInch = numInch % 12;
  154. 	}
  155. 	if ( numInch == 0 && numFeet == 0 ) {
  156. 		strFeetInch = "0";
  157. 	} else if ( numInch == 0 ) {
  158. 		strFeetInch = numFeet + "'";
  159. 	} else if ( numFeet == 0 ) {
  160. 		strFeetInch = numInch.toFixed(1) + '"';
  161. 	} else {
  162. 		strFeetInch = numFeet + "'" + numInch.toFixed(1) + '"';
  163. 	}
  164. 	numInch = numInch + ( 12 * numFeet );
  165. 	var numMeters = numInch * 0.0254;
  166. 	document.LengthConversion.Feet.value = strFeetInch;
  167. 	document.LengthConversion.Meters.value = numMeters.toFixed(2);
  168. 	return true;
  169. }
  170.  
  171. function changedFlOz( ) {
  172. 	var numFlOz = parseFloat( document.VolumeConversion.FlOz.value );
  173. 	var numGallon = numFlOz / 128;
  174. 	var numBarrel = numGallon / 42;
  175. 	var numPint = numGallon * 8;
  176. 	var numLitre = numPint * 0.473176473;
  177.     document.VolumeConversion.Barrel.value = numBarrel.toFixed(3);
  178.     document.VolumeConversion.FlOz.value = numFlOz.toFixed(1);
  179.     document.VolumeConversion.Gallon.value = numGallon.toFixed(3);
  180.     document.VolumeConversion.Litre.value = numLitre.toFixed(3);
  181.     document.VolumeConversion.Pint.value = numPint.toFixed(2);
  182. 	return true;
  183. }
  184.  
  185. function changedGallon( ) {
  186. 	var numGallon = parseFloat( document.VolumeConversion.Gallon.value );
  187. 	var numBarrel = numGallon / 42;
  188. 	var numPint = numGallon * 8;
  189. 	var numFlOz = numGallon * 128;
  190. 	var numLitre = numPint * 0.473176473;
  191.     document.VolumeConversion.Barrel.value = numBarrel.toFixed(3);
  192.     document.VolumeConversion.FlOz.value = numFlOz.toFixed(1);
  193.     document.VolumeConversion.Gallon.value = numGallon.toFixed(3);
  194.     document.VolumeConversion.Litre.value = numLitre.toFixed(3);
  195.     document.VolumeConversion.Pint.value = numPint.toFixed(2);
  196. 	return true;
  197. }
  198.  
  199. function changedGrain( ) {
  200. 	var wtGrain = parseInt( document.WeightConversion.Grain.value );
  201. 	var wtPound = wtGrain / 7000;
  202. 	var wtKg = wtPound * 0.45359237;
  203. 	var wtStone = parseInt( wtPound / 14 );
  204. 	var intPound = parseInt( wtPound + 0.5 - ( 14 * wtStone ) );
  205. 	if ( intPound == 0 ) {
  206. 		var strStone = wtStone.toString( ) + " stone";
  207. 	} else {
  208. 		var strStone = wtStone.toString( ) + " stone " + intPound.toString( );
  209. 	}
  210. 	document.WeightConversion.Stone.value = strStone;
  211. 	document.WeightConversion.Grain.value = wtGrain;
  212. 	document.WeightConversion.Pound.value = WritePound( wtPound );
  213. 	document.WeightConversion.Kg.value = wtKg.toFixed(3);
  214. 	return true;
  215. }
  216.  
  217. function changedHectare( ) {
  218. 	var areaHectare = parseFloat( document.AreaConversion1.Hectare.value );
  219. 	var areaSqM = areaHectare * 10000;
  220. 	var areaSqKm = areaSqM / 1000000;
  221. 	var areaAre = areaSqM / 100;
  222. 	var areaAcre = areaSqM / 4046.8564224;
  223. 	var areaSqYd = areaSqM / 0.83612736;
  224. 	var areaSqFt = areaSqYd * 9;
  225. 	var areaSqIn = areaSqFt * 144;
  226. 	var areaSqMi = areaSqM / 2589988.110336;
  227. 	document.AreaConversion1.Acre.value = areaAcre.toFixed(4)
  228. 	document.AreaConversion1.Are.value = areaAre.toFixed(4)
  229. 	document.AreaConversion1.Hectare.value = areaHectare.toFixed(5)
  230. 	document.AreaConversion1.SqFt.value = areaSqFt.toFixed(2)
  231. 	document.AreaConversion1.SqIn.value = areaSqIn.toFixed(1)
  232. 	document.AreaConversion2.SqKm.value = areaSqKm.toFixed(6)
  233. 	document.AreaConversion2.SqM.value = areaSqM.toFixed(2)
  234. 	document.AreaConversion2.SqMi.value = areaSqMi.toFixed(6)
  235. 	document.AreaConversion2.SqYd.value = areaSqYd.toFixed(2)
  236. 	return true;
  237. }
  238.  
  239. function changedHPImpE( ) {
  240.     var pwrHPImpE = parseFloat( document.PowerConversion.HPImpE.value );
  241.     var pwrWatt = pwrHPImpE * 746;
  242.     var pwrHPImpM = pwrWatt / 745.69987158227022;
  243.     var pwrHPMetric = pwrWatt / 735.49875;
  244.     document.PowerConversion.HPImpE.value = pwrHPImpE.toFixed(3);
  245.     document.PowerConversion.HPImpM.value = pwrHPImpM.toFixed(3);
  246.     document.PowerConversion.HPMetric.value = pwrHPMetric.toFixed(3);
  247.     document.PowerConversion.Watt.value = pwrWatt.toFixed(1);
  248.     return true;
  249. }
  250.  
  251. function changedHPImpM( ) {
  252.     var pwrHPImpM = parseFloat( document.PowerConversion.HPImpM.value );
  253.     var pwrWatt = pwrHPImpM * 745.69987158227022;
  254.     var pwrHPImpE = pwrWatt / 746;
  255.     var pwrHPMetric = pwrWatt / 735.49875;
  256.     document.PowerConversion.HPImpE.value = pwrHPImpE.toFixed(3);
  257.     document.PowerConversion.HPImpM.value = pwrHPImpM.toFixed(3);
  258.     document.PowerConversion.HPMetric.value = pwrHPMetric.toFixed(3);
  259.     document.PowerConversion.Watt.value = pwrWatt.toFixed(1);
  260.     return true;
  261. }
  262.  
  263. function changedHPMetric( ) {
  264.     var pwrHPMetric = parseFloat( document.PowerConversion.HPMetric.value );
  265.     var pwrWatt = pwrHPMetric * 735.49875;
  266.     var pwrHPImpE = pwrWatt / 746;
  267.     var pwrHPImpM = pwrWatt / 745.69987158227022;
  268.     document.PowerConversion.HPImpE.value = pwrHPImpE.toFixed(3);
  269.     document.PowerConversion.HPImpM.value = pwrHPImpM.toFixed(3);
  270.     document.PowerConversion.HPMetric.value = pwrHPMetric.toFixed(3);
  271.     document.PowerConversion.Watt.value = pwrWatt.toFixed(1);
  272.     return true;
  273. }
  274.  
  275. function changedKelvin( ) {
  276. 	var tempK = Number( document.TemperatureConversion.Kelvin.value );
  277. 	var tempC = tempK - 273.15;
  278. 	var tempF = tempK * 1.8 - 459.67;
  279. 	document.TemperatureConversion.Celsius.value = tempC.toFixed(2);
  280. 	document.TemperatureConversion.Fahrenheit.value = tempF.toFixed(2);
  281. 	document.TemperatureConversion.Kelvin.value = tempK.toFixed(2);
  282. 	return true;
  283. }
  284.  
  285. function changedKg( ) {
  286. 	var wtKg = parseFloat( document.WeightConversion.Kg.value );
  287. 	var wtPound = wtKg / 0.45359237;
  288. 	var wtGrain = parseInt( wtPound * 7000 );
  289. 	var wtStone = parseInt( wtPound / 14 );
  290. 	var intPound = parseInt( wtPound + 0.5 - ( 14 * wtStone ) );
  291. 	if ( intPound == 0 ) {
  292. 		var strStone = wtStone.toString( ) + " stone";
  293. 	} else {
  294. 		var strStone = wtStone.toString( ) + " stone " + intPound.toString( );
  295. 	}
  296. 	document.WeightConversion.Stone.value = strStone;
  297. 	document.WeightConversion.Grain.value = wtGrain;
  298. 	document.WeightConversion.Pound.value = WritePound( wtPound );
  299. 	document.WeightConversion.Kg.value = wtKg.toFixed(3);
  300. 	return true;
  301. }
  302.  
  303. function changedKilometers( ) {
  304. 	var numkm = parseFloat( document.DistanceConversion.Kilometers.value );
  305. 	var nummi = numkm / 1.609344;
  306. 	var numnm = numkm / 1.852;
  307. 	document.DistanceConversion.Kilometers.value = numkm.toFixed(2);
  308. 	document.DistanceConversion.Miles.value = nummi.toFixed(2);
  309. 	document.DistanceConversion.Nautical.value = numnm.toFixed(2);
  310. 	return true;
  311. }
  312.  
  313. function changedKnots( ) {
  314. 	var numKnots = parseFloat( document.SpeedConversion.Knots.value );
  315. 	var numKph = numKnots * 1.852;
  316. 	var numMph = numKph / 1.609344;
  317. 	var numMps = numKph / 3.6;
  318. 	document.SpeedConversion.Kph.value = numKph.toFixed(2);
  319. 	document.SpeedConversion.Mps.value = numMps.toFixed(2);
  320. 	document.SpeedConversion.Mph.value = numMph.toFixed(2);
  321. 	document.SpeedConversion.Knots.value = numKnots.toFixed(2);
  322. 	return true;
  323. }
  324.  
  325. function changedKph( ) {
  326. 	var numKph = parseFloat( document.SpeedConversion.Kph.value );
  327. 	var numMps = numKph / 3.6;
  328. 	var numMph = numKph / 1.609344;
  329. 	var numKnots = numKph / 1.852;
  330. 	document.SpeedConversion.Kph.value = numKph.toFixed(2);
  331. 	document.SpeedConversion.Mps.value = numMps.toFixed(2);
  332. 	document.SpeedConversion.Mph.value = numMph.toFixed(2);
  333. 	document.SpeedConversion.Knots.value = numKnots.toFixed(2);
  334. 	return true;
  335. }
  336.  
  337. function changedLitre( ) {
  338. 	var numLitre = parseFloat( document.VolumeConversion.Litre.value );
  339. 	var numGallon = numLitre / 3.785411784;
  340. 	var numBarrel = numGallon / 42;
  341. 	var numFlOz = numGallon * 128;
  342. 	var numPint = numGallon * 8;
  343.     document.VolumeConversion.Barrel.value = numBarrel.toFixed(3);
  344.     document.VolumeConversion.FlOz.value = numFlOz.toFixed(1);
  345.     document.VolumeConversion.Gallon.value = numGallon.toFixed(3);
  346.     document.VolumeConversion.Litre.value = numLitre.toFixed(3);
  347.     document.VolumeConversion.Pint.value = numPint.toFixed(2);
  348. 	return true;
  349. }
  350.  
  351. function changedMeters( ) {
  352. 	var numMeters = parseFloat( document.LengthConversion.Meters.value );
  353. 	var numFeet = Math.floor( numMeters / 0.3048 );
  354. 	var numInches = 12 * ( numMeters / 0.3048 - Math.floor( numMeters / 0.3048 ) );
  355. 	document.LengthConversion.Meters.value = numMeters.toFixed(2);
  356. 	document.LengthConversion.Feet.value = numFeet + "'" + numInches.toFixed(1) + '"';
  357. 	return true;
  358. }
  359.  
  360. function changedMiles( ) {
  361. 	var nummi = parseFloat( document.DistanceConversion.Miles.value );
  362. 	var numkm = nummi * 1.609344;
  363. 	var numnm = numkm / 1.852;
  364. 	document.DistanceConversion.Kilometers.value = numkm.toFixed(2);
  365. 	document.DistanceConversion.Miles.value = nummi.toFixed(2);
  366. 	document.DistanceConversion.Nautical.value = numnm.toFixed(2);
  367. 	return true;
  368. }
  369.  
  370. function changedMph( ) {
  371. 	var numMph = parseFloat( document.SpeedConversion.Mph.value );
  372. 	var numKph = numMph * 1.609344;
  373. 	var numMps = numKph / 3.6;
  374. 	var numKnots = numKph / 1.852;
  375. 	document.SpeedConversion.Kph.value = numKph.toFixed(2);
  376. 	document.SpeedConversion.Mps.value = numMps.toFixed(2);
  377. 	document.SpeedConversion.Mph.value = numMph.toFixed(2);
  378. 	document.SpeedConversion.Knots.value = numKnots.toFixed(2);
  379. 	return true;
  380. }
  381.  
  382. function changedMps( ) {
  383. 	var numMps = parseFloat( document.SpeedConversion.Mps.value );
  384. 	var numKph = numMps * 3.6;
  385. 	var numMph = numKph / 1.609344;
  386. 	var numKnots = numKph / 1.852;
  387. 	document.SpeedConversion.Kph.value = numKph.toFixed(2);
  388. 	document.SpeedConversion.Mps.value = numMps.toFixed(2);
  389. 	document.SpeedConversion.Mph.value = numMph.toFixed(2);
  390. 	document.SpeedConversion.Knots.value = numKnots.toFixed(2);
  391. 	return true;
  392. }
  393.  
  394. function changedNautical( ) {
  395. 	var numnm = parseFloat( document.DistanceConversion.Nautical.value );
  396. 	var numkm = numnm * 1.852;
  397. 	var nummi = numkm / 1.609344;
  398. 	document.DistanceConversion.Kilometers.value = numkm.toFixed(2);
  399. 	document.DistanceConversion.Miles.value = nummi.toFixed(2);
  400. 	document.DistanceConversion.Nautical.value = numnm.toFixed(2);
  401. 	return true;
  402. }
  403.  
  404. function changedPascal( ) {
  405. 	var pressurePa = parseFloat( document.PressureConversion.Pascal.value );
  406. 	var pressureBar = pressurePa / 100000;
  407. 	var pressureAtm = pressureBar / 1.01325;
  408. 	var pressurePsi = pressureBar * 14.5037744;
  409. 	document.PressureConversion.Atmosphere.value = pressureAtm.toFixed(3);
  410. 	document.PressureConversion.Bar.value = pressureBar.toFixed(3);
  411. 	document.PressureConversion.Pascal.value = pressurePa.toFixed(0);
  412. 	document.PressureConversion.Psi.value = pressurePsi.toFixed(3);
  413. 	return true;
  414. }
  415.  
  416. function changedPint( ) {
  417. 	var numPint = parseFloat( document.VolumeConversion.Pint.value );
  418. 	var numGallon = numPint / 8;
  419. 	var numBarrel = numGallon / 42;
  420. 	var numFlOz = numGallon * 128;
  421. 	var numLitre = numPint * 0.473176473;
  422.     document.VolumeConversion.Barrel.value = numBarrel.toFixed(3);
  423.     document.VolumeConversion.FlOz.value = numFlOz.toFixed(1);
  424.     document.VolumeConversion.Gallon.value = numGallon.toFixed(3);
  425.     document.VolumeConversion.Litre.value = numLitre.toFixed(3);
  426.     document.VolumeConversion.Pint.value = numPint.toFixed(2);
  427. 	return true;
  428. }
  429.  
  430. function changedPound( ) {
  431. 	var wtPound = ReadPound( document.WeightConversion.Pound.value );
  432. 	var wtGrain = parseInt( wtPound * 7000 + 0.5 );
  433. 	var wtKg = wtPound * 0.45359237;
  434. 	var wtStone = parseInt( wtPound / 14 );
  435. 	var intPound = parseInt( wtPound + 0.5 - ( 14 * wtStone ) );
  436. 	if ( intPound == 0 ) {
  437. 		var strStone = wtStone.toString( ) + " stone";
  438. 	} else {
  439. 		var strStone = wtStone.toString( ) + " stone " + intPound.toString( );
  440. 	}
  441. 	document.WeightConversion.Stone.value = strStone;
  442. 	document.WeightConversion.Grain.value = wtGrain;
  443. 	document.WeightConversion.Pound.value = WritePound( wtPound );
  444. 	document.WeightConversion.Kg.value = wtKg.toFixed(3);
  445. 	return true;
  446. }
  447.  
  448. function changedPsi( ) {
  449. 	var pressurePsi = parseFloat( document.PressureConversion.Psi.value );
  450. 	var pressureBar = pressurePsi / 14.5037744;
  451. 	var pressureAtm = pressureBar / 1.01325;
  452. 	var pressurePa  = pressureBar * 100000;
  453. 	document.PressureConversion.Atmosphere.value = pressureAtm.toFixed(3);
  454. 	document.PressureConversion.Bar.value = pressureBar.toFixed(3);
  455. 	document.PressureConversion.Pascal.value = pressurePa.toFixed(0);
  456. 	document.PressureConversion.Psi.value = pressurePsi.toFixed(3);
  457. 	return true;
  458. }
  459.  
  460. function changedRoman( ) {
  461. 	var numR = document.RomanNumerals.Roman.value.toUpperCase( );
  462. 	var numI = Roman2Decimal( numR ).toString( );
  463. 	document.RomanNumerals.Decimal.value = numI;
  464. 	document.RomanNumerals.Roman.value = Decimal2Roman( numI );
  465. 	return true;
  466. }
  467.  
  468. function changedSqFt( ) {
  469. 	var areaSqFt = parseFloat( document.AreaConversion1.SqFt.value );
  470. 	var areaSqIn = areaSqFt * 144;
  471. 	var areaSqYd = areaSqFt / 9;
  472. 	var areaSqM = areaSqYd * 0.83612736;
  473. 	var areaSqKm = areaSqM / 1000000;
  474. 	var areaAre = areaSqM / 100;
  475. 	var areaHectare = areaSqM / 10000;
  476. 	var areaAcre = areaSqM / 4046.8564224;
  477. 	var areaSqMi = areaSqM / 2589988.110336;
  478. 	document.AreaConversion1.Acre.value = areaAcre.toFixed(4)
  479. 	document.AreaConversion1.Are.value = areaAre.toFixed(4)
  480. 	document.AreaConversion1.Hectare.value = areaHectare.toFixed(5)
  481. 	document.AreaConversion1.SqFt.value = areaSqFt.toFixed(2)
  482. 	document.AreaConversion1.SqIn.value = areaSqIn.toFixed(1)
  483. 	document.AreaConversion2.SqKm.value = areaSqKm.toFixed(6)
  484. 	document.AreaConversion2.SqM.value = areaSqM.toFixed(2)
  485. 	document.AreaConversion2.SqMi.value = areaSqMi.toFixed(6)
  486. 	document.AreaConversion2.SqYd.value = areaSqYd.toFixed(2)
  487. 	return true;
  488. }
  489.  
  490. function changedSqIn( ) {
  491. 	var areaSqIn = parseFloat( document.AreaConversion1.SqIn.value );
  492. 	var areaSqFt = areaSqIn / 144;
  493. 	var areaSqYd = areaSqFt / 9;
  494. 	var areaSqM = areaSqYd * 0.83612736;
  495. 	var areaSqKm = areaSqM / 1000000;
  496. 	var areaAre = areaSqM / 100;
  497. 	var areaHectare = areaSqM / 10000;
  498. 	var areaAcre = areaSqM / 4046.8564224;
  499. 	var areaSqMi = areaSqM / 2589988.110336;
  500. 	document.AreaConversion1.Acre.value = areaAcre.toFixed(4)
  501. 	document.AreaConversion1.Are.value = areaAre.toFixed(4)
  502. 	document.AreaConversion1.Hectare.value = areaHectare.toFixed(5)
  503. 	document.AreaConversion1.SqFt.value = areaSqFt.toFixed(2)
  504. 	document.AreaConversion1.SqIn.value = areaSqIn.toFixed(1)
  505. 	document.AreaConversion2.SqKm.value = areaSqKm.toFixed(6)
  506. 	document.AreaConversion2.SqM.value = areaSqM.toFixed(2)
  507. 	document.AreaConversion2.SqMi.value = areaSqMi.toFixed(6)
  508. 	document.AreaConversion2.SqYd.value = areaSqYd.toFixed(2)
  509. 	return true;
  510. }
  511.  
  512. function changedSqKm( ) {
  513. 	var areaSqKm = parseFloat( document.AreaConversion2.SqKm.value );
  514. 	var areaSqM = areaSqKm * 1000000;
  515. 	var areaAre = areaSqM / 100;
  516. 	var areaHectare = areaSqM / 10000;
  517. 	var areaAcre = areaSqM / 4046.8564224;
  518. 	var areaSqYd = areaSqM / 0.83612736;
  519. 	var areaSqFt = areaSqYd * 9;
  520. 	var areaSqIn = areaSqFt * 144;
  521. 	var areaSqMi = areaSqM / 2589988.110336;
  522. 	document.AreaConversion1.Acre.value = areaAcre.toFixed(4)
  523. 	document.AreaConversion1.Are.value = areaAre.toFixed(4)
  524. 	document.AreaConversion1.Hectare.value = areaHectare.toFixed(5)
  525. 	document.AreaConversion1.SqFt.value = areaSqFt.toFixed(2)
  526. 	document.AreaConversion1.SqIn.value = areaSqIn.toFixed(1)
  527. 	document.AreaConversion2.SqKm.value = areaSqKm.toFixed(6)
  528. 	document.AreaConversion2.SqM.value = areaSqM.toFixed(2)
  529. 	document.AreaConversion2.SqMi.value = areaSqMi.toFixed(6)
  530. 	document.AreaConversion2.SqYd.value = areaSqYd.toFixed(2)
  531. 	return true;
  532. }
  533.  
  534. function changedSqM( ) {
  535. 	var areaSqM = parseFloat( document.AreaConversion2.SqM.value );
  536. 	var areaSqKm = areaSqM / 1000000;
  537. 	var areaAre = areaSqM / 100;
  538. 	var areaHectare = areaSqM / 10000;
  539. 	var areaAcre = areaSqM / 4046.8564224;
  540. 	var areaSqYd = areaSqM / 0.83612736;
  541. 	var areaSqFt = areaSqYd * 9;
  542. 	var areaSqIn = areaSqFt * 144;
  543. 	var areaSqMi = areaSqM / 2589988.110336;
  544. 	document.AreaConversion1.Acre.value = areaAcre.toFixed(4)
  545. 	document.AreaConversion1.Are.value = areaAre.toFixed(4)
  546. 	document.AreaConversion1.Hectare.value = areaHectare.toFixed(5)
  547. 	document.AreaConversion1.SqFt.value = areaSqFt.toFixed(2)
  548. 	document.AreaConversion1.SqIn.value = areaSqIn.toFixed(1)
  549. 	document.AreaConversion2.SqKm.value = areaSqKm.toFixed(6)
  550. 	document.AreaConversion2.SqM.value = areaSqM.toFixed(2)
  551. 	document.AreaConversion2.SqMi.value = areaSqMi.toFixed(6)
  552. 	document.AreaConversion2.SqYd.value = areaSqYd.toFixed(2)
  553. 	return true;
  554. }
  555.  
  556. function changedSqMi( ) {
  557. 	var areaSqMi = parseFloat( document.AreaConversion2.SqMi.value );
  558. 	var areaSqM = areaSqMi * 2589988.110336;
  559. 	var areaSqKm = areaSqM / 1000000;
  560. 	var areaAre = areaSqM / 100;
  561. 	var areaHectare = areaSqM / 10000;
  562. 	var areaAcre = areaSqM / 4046.8564224;
  563. 	var areaSqYd = areaSqM / 0.83612736;
  564. 	var areaSqFt = areaSqYd * 9;
  565. 	var areaSqIn = areaSqFt * 144;
  566. 	document.AreaConversion1.Acre.value = areaAcre.toFixed(4)
  567. 	document.AreaConversion1.Are.value = areaAre.toFixed(4)
  568. 	document.AreaConversion1.Hectare.value = areaHectare.toFixed(5)
  569. 	document.AreaConversion1.SqFt.value = areaSqFt.toFixed(2)
  570. 	document.AreaConversion1.SqIn.value = areaSqIn.toFixed(1)
  571. 	document.AreaConversion2.SqKm.value = areaSqKm.toFixed(6)
  572. 	document.AreaConversion2.SqM.value = areaSqM.toFixed(2)
  573. 	document.AreaConversion2.SqMi.value = areaSqMi.toFixed(6)
  574. 	document.AreaConversion2.SqYd.value = areaSqYd.toFixed(2)
  575. 	return true;
  576. }
  577.  
  578. function changedSqYd( ) {
  579. 	var areaSqYd = parseFloat( document.AreaConversion2.SqYd.value );
  580. 	var areaSqM = areaSqYd * 0.83612736;
  581. 	var areaSqKm = areaSqM / 1000000;
  582. 	var areaAre = areaSqM / 100;
  583. 	var areaHectare = areaSqM / 10000;
  584. 	var areaAcre = areaSqM / 4046.8564224;
  585. 	var areaSqFt = areaSqYd * 9;
  586. 	var areaSqIn = areaSqFt * 144;
  587. 	var areaSqMi = areaSqM / 2589988.110336;
  588. 	document.AreaConversion1.Acre.value = areaAcre.toFixed(4)
  589. 	document.AreaConversion1.Are.value = areaAre.toFixed(4)
  590. 	document.AreaConversion1.Hectare.value = areaHectare.toFixed(5)
  591. 	document.AreaConversion1.SqFt.value = areaSqFt.toFixed(2)
  592. 	document.AreaConversion1.SqIn.value = areaSqIn.toFixed(1)
  593. 	document.AreaConversion2.SqKm.value = areaSqKm.toFixed(6)
  594. 	document.AreaConversion2.SqM.value = areaSqM.toFixed(2)
  595. 	document.AreaConversion2.SqMi.value = areaSqMi.toFixed(6)
  596. 	document.AreaConversion2.SqYd.value = areaSqYd.toFixed(2)
  597. 	return true;
  598. }
  599.  
  600. function changedStone( ) {
  601. 	var strStone = document.WeightConversion.Stone.value;
  602. 	var pattern = /^\d+\s+stone(\s+\d+)?$/i;
  603. 	if ( strStone.search( pattern ) == -1 ) {
  604. 		alert( "Stones (and pounds) must be input as\n\n\t<stones> stone [ <pounds> ]\n\nE.g. 18 pounds must be input as\n\n\t1 stone 4\n\nAnd 28 pounds must be input as\n\n\t2 stone" );
  605. 		return false;
  606. 	}
  607. 	var arrStone = new Array( );
  608. 	arrStone = strStone.split( " " );
  609. 	if ( arrStone.length > 2 ) {
  610. 		var wtPound = ( 14 * parseInt( arrStone[0] ) ) + parseInt( arrStone[2] );
  611. 	} else {
  612. 		var wtPound = 14 * parseInt( arrStone[0] );
  613. 	}
  614. 	var wtGrain = parseInt( wtPound * 7000 + 0.5 );
  615. 	var wtKg = wtPound * 0.45359237;
  616. 	var wtStone = parseInt( wtPound / 14 );
  617. 	var intPound = parseInt( wtPound + 0.5 - ( 14 * wtStone ) );
  618. 	if ( intPound == 0 ) {
  619. 		var strStone = wtStone.toString( ) + " stone";
  620. 	} else {
  621. 		var strStone = wtStone.toString( ) + " stone " + intPound.toString( );
  622. 	}
  623. 	document.WeightConversion.Stone.value = strStone;
  624. 	document.WeightConversion.Grain.value = wtGrain;
  625. 	document.WeightConversion.Pound.value = WritePound( wtPound );
  626. 	document.WeightConversion.Kg.value = wtKg.toFixed(3);
  627. 	return true;
  628. }
  629.  
  630. function changedWatt( ) {
  631.     var pwrWatt = parseFloat( document.PowerConversion.Watt.value );
  632.     var pwrHPImpE = pwrWatt / 746;
  633.     var pwrHPImpM = pwrWatt / 745.69987158227022;
  634.     var pwrHPMetric = pwrWatt / 735.49875;
  635.     document.PowerConversion.HPImpE.value = pwrHPImpE.toFixed(3);
  636.     document.PowerConversion.HPImpM.value = pwrHPImpM.toFixed(3);
  637.     document.PowerConversion.HPMetric.value = pwrHPMetric.toFixed(3);
  638.     document.PowerConversion.Watt.value = pwrWatt.toFixed(1);
  639.     return true;
  640. }
  641.  
  642. function Decimal2Roman( numDecimal ) {
  643. 	var intDecimal = parseInt( numDecimal );
  644. 	var strRoman = "";
  645. 	// First, add an "M" for every multiple of 1000
  646. 	while ( intDecimal >= 1000 ) {
  647. 		intDecimal = intDecimal - 1000;
  648. 		strRoman = strRoman + "M";
  649. 	}
  650. 	// Next, add "CM" for 900, or "D" for 500, or "CD" for 400
  651. 	if ( intDecimal >= 900 ) {
  652. 		intDecimal = intDecimal - 900;
  653. 		strRoman = strRoman + "CM";
  654. 	} else if ( intDecimal >= 500 ) {
  655. 		intDecimal = intDecimal - 500;
  656. 		strRoman = strRoman + "D";
  657. 	} else if ( intDecimal >= 400 ) {
  658. 		intDecimal = intDecimal - 400;
  659. 		strRoman = strRoman + "CD";
  660. 	}
  661. 	// Add a "C" for every remaining multiple of 100
  662. 	while ( intDecimal >= 100 ) {
  663. 		intDecimal = intDecimal - 100;
  664. 		strRoman = strRoman + "C";
  665. 	}
  666. 	// Add "XC" for 90, or "L" for 50, or "XL" for 40
  667. 	if ( intDecimal >= 90 ) {
  668. 		intDecimal = intDecimal - 90;
  669. 		strRoman = strRoman + "XC";
  670. 	} else if ( intDecimal >= 50 ) {
  671. 		intDecimal = intDecimal - 50;
  672. 		strRoman = strRoman + "L";
  673. 	} else if ( intDecimal >= 40 ) {
  674. 		intDecimal = intDecimal - 40;
  675. 		strRoman = strRoman + "XL";
  676. 	}
  677. 	// Add an "X" for every remaining multiple of 10
  678. 	while ( intDecimal >= 10 ) {
  679. 		intDecimal = intDecimal - 10;
  680. 		strRoman = strRoman + "X";
  681. 	}
  682. 	// Add "IX" for 9, or "V" for 5, or "IV" for 4
  683. 	if ( intDecimal >= 9 ) {
  684. 		intDecimal = intDecimal - 9;
  685. 		strRoman = strRoman + "IX";
  686. 	} else if ( intDecimal >= 5 ) {
  687. 		intDecimal = intDecimal - 5;
  688. 		strRoman = strRoman + "V";
  689. 	} else if ( intDecimal >= 4 ) {
  690. 		intDecimal = intDecimal - 4;
  691. 		strRoman = strRoman + "IV";
  692. 	}
  693. 	// Finally, add an "I" for every remaining multiple of 1
  694. 	while ( intDecimal >= 1 ) {
  695. 		intDecimal = intDecimal - 1;
  696. 		strRoman = strRoman + "I";
  697. 	}
  698. 	// Return the result
  699. 	return strRoman;
  700. }
  701.  
  702. function ReadPound( strPound ) {
  703. 	// var strPound = document.WeightConversion.Pound.value;
  704. 	var pattern = /\d+\s+\d+\/16/;
  705. 	if ( strPound.search( pattern ) != -1 ) {
  706. 		var arrPound = new Array( );
  707. 		arrPound = strPound.split( " " );
  708. 		var numPound = parseInt( arrPound[0] );
  709. 		if ( arrPound.length > 1 ) {
  710. 			var arrOunce = new Array( );
  711. 			arrOunce = arrPound[1].split( "/" );
  712. 			numPound = numPound + ( parseInt( arrOunce[0] ) / 16 );
  713. 		}
  714. 		return numPound;
  715. 	} else {
  716. 		return parseFloat( strPound );
  717. 	}
  718. }
  719.  
  720. function Roman2Decimal( strRoman ) {
  721. 	var intRoman = 0;
  722. 	var arrRoman = [];
  723. 	var i;
  724. 	// Store each "digit" of the Roman numeral in an array
  725. 	for ( i = 0; i < strRoman.length; i++ ) {
  726. 		arrRoman[i] = strRoman.substring( i, i + 1 ).toUpperCase( );
  727. 	}
  728. 	// Then convert each "digit" to its numeric value
  729. 	for ( i = 0; i < arrRoman.length; i++ ) {
  730. 		switch ( arrRoman[i] ) {
  731. 			case "M": {
  732. 				arrRoman[i] = 1000;
  733. 				break;
  734. 			}
  735. 			case "D": {
  736. 				arrRoman[i] = 500;
  737. 				break;
  738. 			}
  739. 			case "C": {
  740. 				arrRoman[i] = 100;
  741. 				break;
  742. 			}
  743. 			case "L": {
  744. 				arrRoman[i] = 50;
  745. 				break;
  746. 			}
  747. 			case "X": {
  748. 				arrRoman[i] = 10;
  749. 				break;
  750. 			}
  751. 			case "V": {
  752. 				arrRoman[i] = 5;
  753. 				break;
  754. 			}
  755. 			case "I": {
  756. 				arrRoman[i] = 1;
  757. 				break;
  758. 			}
  759. 		}
  760. 	}
  761. 	// Now comes the hard part: for each "digit" decide if it will be
  762. 	// added or subtracted, based on the value of the following "digit"
  763. 	for ( i = 0; i < arrRoman.length - 1; i++ ) {
  764. 		if ( arrRoman[i] < arrRoman[i+1] ) {
  765. 			// E.g. "I" in "IX" (9): subtract 1
  766. 			intRoman = intRoman - arrRoman[i];
  767. 		} else if ( arrRoman[i] == arrRoman[i+1] ) {
  768. 			// E.g. "I" in "XII" (12), "III" (3) or in "IIX" (ancient notation for 8).
  769. 			// The latter should actually be "VIII" in "modern" roman numerals, but
  770. 			// "IIX" was used in ancient times, so let's just be prepared.
  771. 			// We'll add the value to the next position in the array, so it will be
  772. 			// reevaluated in the next iteration of the loop.
  773. 			// Note: this trick will definitely fail on invalid notations like "IIIX".
  774. 			arrRoman[i+1] = arrRoman[i] + arrRoman[i+1];
  775. 			arrRoman[i] = 0;
  776. 		} else {
  777. 			// arrRoman( i ) > arrRoman( i + 1 )
  778. 			// E.g. "V" in "XV" (15): add 5
  779. 			intRoman = intRoman + arrRoman[i];
  780. 		}
  781. 	}
  782. 	// The last "digit" doesn't have a following "digit" so it
  783. 	// can be added without having to test a following "digit"
  784. 	intRoman = intRoman + arrRoman[arrRoman.length-1];
  785. 	// Return the calculated value
  786. 	return intRoman;
  787. }
  788.  
  789. function WritePound( numPound ) {
  790. 	var intPound = parseInt( numPound );
  791. 	var strPound = intPound.toString( ); 
  792. 	var intOunce = parseInt( ( numPound - intPound ) * 16 + 0.5 );
  793. 	if ( intOunce > 0 ) {
  794. 		strPound = strPound + "  " + intOunce.toString( ) + "/16";
  795. 	}
  796. 	return strPound;
  797. }
  798.