function calc() {

   var ht = document.agecalc.height.value;
   var wt = document.agecalc.weight.value;

   if ( ht != "" ) ht = remove_outer_spaces ( ht ) ;
   if ( wt != "" ) wt = remove_outer_spaces ( wt ) ;

   if ( ( ht == "" ) || ( isNaN ( ht ) ) ) { 
   clear_height_values ( "m", 0, 270, 
      "Please enter height centimetres in digits" ); 
     return; 
   }

   if ( ( ht < 90 ) || ( ht > 300 ) ) { 
     clear_height_values ( "m", 0, 270, 
        "This formula applies to a height range between 90 and 300 Cms"); 
     return; 
   }

   if ( ( wt == "" ) || ( isNaN ( wt ) ) ) { 
     clear_weight_values ( "m", 0, 270, 
        "Please enter weight kilograms in digits" ); 
     return;
   }

   if ( ( wt < 40 ) || ( wt > 500 ) ) { 
     clear_weight_values ( "m", 0, 270, 
        "This formula applies to a weight range between 40 and 500 Kgs" ); 
     return; 
   }

   docalc ( ht, wt );

   return;

}  // end function calc


function impcalc() {

   var fht = document.agecalc.feetht.value;
   var iht = document.agecalc.inchht.value;
   var swt = document.agecalc.stonewt.value;
   var pwt = document.agecalc.lbswt.value;

   if ( ( fht == "" )   &&  ( iht == "" ) ) {
     clear_height_values ( "i", 0, 270, 
        "Please enter height feet and inches in digits" ); 
     return; 
   }

   if ( fht == "" ) fht = 0; else fht = remove_outer_spaces ( fht ) ;
   if ( iht == "" ) iht = 0; else iht = remove_outer_spaces ( iht ) ;

   if ( ( isNaN ( fht ) ) || ( isNaN ( iht ) ) ||
        ( ( fht ==  0 )   && (   iht ==  0 ) )  )    { 
     clear_height_values ( "i", 0, 270, 
        "Please enter height feet and inches in digits" ); 
     return; 
   }

   var inches = fht * 12;
   inches += eval ( iht );
   var ht     = inches * 2.54;
   
   if ( ( inches < 36 ) || ( inches > 120 ) ) { 
     clear_height_values ( "i", 0, 270, 
        "This formula applies to a height range between 3 and 10 Feet"); 
     return; 
   }

   if ( ( swt == "" )   &&  ( pwt == "" ) ) {
     clear_weight_values ( "i", 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 ) )  )    { 
     clear_weight_values ( "i", 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 ) ) { 
     clear_weight_values ( "i", 0, 270, 
        "This formula applies to a weight range between 6 and 80 Stones"); 
     return; 
   }

   docalc ( ht, wt );

   return;

}  // end function impcalc


function docalc ( ht, wt ) {

   minimumht =  88.9;   // centimetres
   minimumwt = 37.64;   // kilograms
   var ag;
   var agefactor = 0;
   var htfactor  = 0;
   var xwt       = 0;
   var count     = 1;
   steps = new Array ( 0, 33.02, 35.56, 7.62, 15.24, 12.7 );

   xwt = minimumwt;
   if ( ht > 174 ) xwt -= 0.454; // adjustment on ht/wt ratio given table
   htfactor = minimumht;

   while ( ht > ( htfactor + ( steps [ count ] ) ) ) {
     xwt +=  ( ( ht - htfactor ) / 2.54 ) / 2.205;
     htfactor += steps [ count ];
     ++count;
     if ( count > 5 ) steps [ count ] = 0.5;
   }
   xwt += ( ( ht - htfactor ) / 2.54 ) / 2.205;
   
//   agefactor = ( ( wt - xwt ) * 2.205 ) / 5;
   agefactor = ( ( wt - xwt ) * 2.205 );
   if ( agefactor <= 10 ) agefactor  =  0;
   if ( agefactor >  10 ) agefactor -= 10;
   var false_age_factor = agefactor;
   if ( agefactor >  5 ) false_age_factor =  5 + ( ( agefactor -  5 ) /  2 );
   if ( agefactor > 15 ) false_age_factor = 10 + ( ( agefactor - 15 ) /  9 );
   if ( agefactor > 60 ) false_age_factor = 15 + ( ( agefactor - 60 ) / 34 );

   ag = 25 + false_age_factor;
     
   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.agecalc.feetht.value  = Math.round (   feet );
   document.agecalc.inchht.value  = Math.round ( inches );
   document.agecalc.stonewt.value = Math.round ( stones );
   document.agecalc.lbswt.value   = Math.round ( pounds );
   document.agecalc.height.value  = Math.round ( ht );
   document.agecalc.weight.value  = Math.round ( wt );
   document.agecalc.age.value     = Math.round ( ag );

   var checkref;
   checkref = document.referrer.indexOf ( "alphaselect" );
   if ( checkref == -1 )
    window.scrollTo ( 0, 568 );
   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;
} // end function remove_outer_spaces ( subject )


function clear_height_values ( mori, x, y, message ) {

  var checkref;
  checkref = document.referrer.indexOf ( "alphaselect" );
  if ( checkref == -1 )
    window.scrollTo ( 0, 388 );
  else
    window.scrollTo ( 0, 220 );
  alert ( message ); 
  if ( mori == "i" ) document.agecalc.feetht.focus ( );
  else document.agecalc.height.focus ( );
  document.agecalc.age.value     = "";
  document.agecalc.feetht.value  = "";
  document.agecalc.inchht.value  = "";
  document.agecalc.height.value  = "";

  return;

} // end funtion clear_height_values ()


function clear_weight_values ( mori, x, y, message ) {

  var checkref;
  checkref = document.referrer.indexOf ( "alphaselect" );
  if ( checkref == -1 )
    window.scrollTo ( 0, 538 );
  else
    window.scrollTo ( 0, 270 );
  alert ( message ); 
  if ( mori == "i" ) document.agecalc.stonewt.focus ( );
  else document.agecalc.weight.focus ( );
  document.agecalc.age.value     = "";
  document.agecalc.stonewt.value  = "";
  document.agecalc.lbswt.value  = "";
  document.agecalc.weight.value  = "";

  return;

} // end funtion clear_weight_values ()

