function calc() {

   var ht;
   var ag = document.heightcalc.age.value;
   var wt = document.heightcalc.weight.value;

   ag = age_test ( ag );
   if ( ag == 0 ) return;

   if ( wt != "" ) wt = remove_outer_spaces ( wt ) ;

   if ( ( wt == "" ) || ( isNaN ( wt ) ) ) { 
     clear_values ( 0, 270, "xPlease enter weight kilograms in digits" ); 
     document.heightcalc.weight.focus ( );
     return;
   }

   if ( ( wt < 40 ) || ( wt > 500 ) ) { 
     clear_values ( 0, 270, 
        "This formula applies to a weight range between 40 and 500 Kgs" ); 
     document.heightcalc.weight.focus ( );
     return; 
   }

   docalc ( wt, ag );

   return;

}  // end function calc

function impcalc() {

   var ht;
   var ag  = document.heightcalc.age.value;
   var swt = document.heightcalc.stonewt.value;
   var pwt = document.heightcalc.lbswt.value;

   ag = age_test ( ag );
   if ( ag == 0 ) return;

   if ( ( swt == "" )   &&  ( pwt == "" ) ) {
     document.heightcalc.stonewt.focus ( );
     clear_values ( 0, 270, "Please enter weight stones and lbs in digits" ); 
     return; 
   }

   if ( swt == "" ) swt = 0; else swt = remove_outer_spaces ( swt ) ;
   if ( pwt == "" ) pwt = 0; else pwt = remove_outer_spaces ( pwt ) ;

   if ( ( isNaN ( swt ) ) || ( isNaN ( pwt ) ) ||
        ( ( swt ==  0 )   && (   pwt ==  0 ) )  )    { 
     document.heightcalc.stonewt.focus ( );
     clear_values ( 0, 270, "Please enter weight stones and lbs in digits" ); 
     return; 
   }

   var lbs = swt * 14;
   lbs += eval ( pwt );
   var wt = lbs / 2.205;
   
   if ( ( wt < 38 ) || ( wt > 508 ) ) { 
     document.heightcalc.stonewt.focus ( );
     clear_values ( 0, 270, 
        "This formula applies to a weight range between 6 and 80 Stones"); 
     return; 
   }

   docalc ( wt, ag );

   return;

}  // end function impcalc

function docalc ( wt, ag ) {

   minimumht =  88.9;   // centimetres
   minimumwt = 37.64;   // kilograms
   var agefactor = 0;
   var wtfactor  = 0;
   var ht        = 0;
   var count     = 1;
   steps = new Array ( 0, 5.9, 6.35, 1.36, 2.72, 2.27 );

   if ( ag > 25 ) agefactor =  ( ag - 25 ) / 11.025;
   ht = minimumht;
   xwt = wt - agefactor;
   if ( xwt > 67 ) xwt -= 0.454;
   wtfactor = minimumwt;
   var lastcount = count;

   while ( xwt > ( wtfactor + ( steps [ count ] * ( count ) ) ) ) {
     ht += ( steps [ count ] * 2.205 * 2.54 );
     wtfactor += ( count ) * steps [ count ];
     ++count;
     if ( count > 5 ) steps [ count ] = 0.089;
   }
   ht += ( ( xwt - wtfactor ) * 2.205 * 2.54 ) / ( count );
     
   var inches = Math.round ( ht / 2.54  );
   var pounds = Math.round ( wt * 2.205 );
   var feet   = Math.round ( ( inches / 12 ) - 0.5 );
   inches -= ( feet * 12 );
   var stones   = Math.round ( ( pounds / 14 ) - 0.5 );
   pounds -= ( stones * 14 );
   document.heightcalc.feetht.value  = Math.round (   feet );
   document.heightcalc.inchht.value  = Math.round ( inches );
   document.heightcalc.stonewt.value = Math.round ( stones );
   document.heightcalc.lbswt.value   = Math.round ( pounds );
   document.heightcalc.height.value  = Math.round ( ht );
   document.heightcalc.weight.value  = Math.round ( wt );

   var checkref;
   checkref = document.referrer.indexOf ( "alphaselect" );
   if ( checkref == -1 )
     window.scrollTo ( 0, 600 );
   else
     window.scrollTo ( 0, 400 );

   return;

} // end funtion docalc

function remove_outer_spaces ( subject ) {

  var temp1 = subject.toString ();
  var temp2;
  var temp3;
  var len  = temp1.length;
  var x;
  var c;
  for ( x = 0; x < len; x++ ) {
    c = temp1.substr ( x, 1 );
    if ( c == " " ) continue;
    temp2 = temp1.substring ( x, len );
    break;
  }
  if ( x == len ) return 0;

  len = temp2.length;
  for ( x = len - 1; x > -1; x-- ) {
    c = temp2.substr ( x, 1 );
    if ( c == " " ) continue;
    temp3 = temp2.substring ( 0, x + 1 );
    break;
  }
  return temp3;
}

function age_test ( age ) {

   if ( age == "" ) { 
     clear_height_values ( 0, 220, "Please enter age in digits" ); 
     return 0;
   }
   
   age = remove_outer_spaces ( age );

   if ( ( age == "" ) || ( isNaN ( age ) ) ) { 
     clear_height_values ( 0, 220, "Please enter age in digits" ); 
     return 0;
   }

   if ( ( age < 21 ) || ( age > 100 ) ) { 
     clear_height_values ( 0, 220, 
        "Please enter age between 21 and 100 years" ); 
     return 0;
   }

   return age;
}

function clear_values ( x, y, message ) {

//  scollto ( x, y );
  var checkref;
  checkref = document.referrer.indexOf ( "alphaselect" );
  if ( checkref == -1 )
    y += 168;
  window.scrollTo ( x, y );
  alert ( message ); 
  document.heightcalc.weight.value  = "";
  document.heightcalc.stonewt.value = "";
  document.heightcalc.lbswt.value   = "";
  document.heightcalc.feetht.value  = "";
  document.heightcalc.inchht.value  = "";
  document.heightcalc.height.value  = "";

  return;

} // end funtion clear_values ()


function clear_height_values ( x, y, message ) {

  var checkref;
  checkref = document.referrer.indexOf ( "alphaselect" );
  if ( checkref == -1 )
    y += 168;
  document.heightcalc.age.focus ( );
  window.scrollTo ( x, y );
  alert ( message ); 
  document.heightcalc.age.value     = "";
  document.heightcalc.feetht.value  = "";
  document.heightcalc.inchht.value  = "";
  document.heightcalc.height.value  = "";

  return;

} // end funtion clear_height_values ()


