function calc() {

   var wt;
   var ag = document.weightcalc.age.value;
   var ht = document.weightcalc.height.value;

   ag = age_test ( ag );
   if ( ag == 0 ) return;

   if ( ht != "" ) ht = remove_outer_spaces ( ht ) ;

   if ( ( ht == "" ) || ( isNaN ( ht ) ) ) { 
     clear_values ( 0, 270, "Please enter height centimetres in digits" ); 
     document.weightcalc.height.focus ( );
     return; 
   }

   if ( ( ht < 90 ) || ( ht > 300 ) ) { 
     clear_values ( 0, 270, 
        "This formula applies to a height range between 90 and 300 Cms"); 
     document.weightcalc.height.focus ( );
     return; 
   }

   docalc ( ht, ag );

   return;

}  // end function calc

function impcalc() {

   var wt;
   var ag  = document.weightcalc.age.value;
   var fht = document.weightcalc.feetht.value;
   var iht = document.weightcalc.inchht.value;

   ag = age_test ( ag );
   if ( ag == 0 ) return;

   if ( ( fht == "" )   &&  ( iht == "" ) ) {
     clear_values ( 0, 270, 
        "Please enter height feet and inches in digits" ); 
     document.weightcalc.feetht.focus ( );
     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_values ( 0, 270, 
        "Please enter height feet and inches in digits" ); 
     document.weightcalc.feetht.focus ( );
     return; 
   }

   var inches = fht * 12;
   inches += eval ( iht );
   var ht     = inches * 2.54;
   
   if ( ( inches < 36 ) || ( inches > 120 ) ) { 
     clear_values ( 0, 270, 
        "This formula applies to a height range between 3 and 10 Feet"); 
     document.weightcalc.feetht.focus ( );
     return; 
   }

   docalc ( ht, ag );

   return;

}  // end function impcalc


function docalc ( ht, ag ) {

   minimumht =  88.9;   // centimetres
   minimumwt = 37.64;   // kilograms
   var agefactor = 0;
   var htfactor  = 0;
   var wt        = 0;
   var count     = 1;
   steps = new Array ( 0, 33.02, 35.56, 7.62, 15.24, 12.7 );

   if ( ag > 25 ) agefactor =  ( ag - 25 ) / 11.025;
   wt = agefactor + minimumwt;
   if ( ht > 174 ) wt -= 0.454;
   htfactor = minimumht;

   while ( ht > ( htfactor + ( steps [ count ] ) ) ) {
     wt +=  ( ( ht - htfactor ) / 2.54 ) / 2.205;
     htfactor += steps [ count ];
     ++count;
     if ( count > 5 ) steps [ count ] = 0.5;
   }
   wt += ( ( ht - htfactor ) / 2.54 ) / 2.205;

   document.weightcalc.weight.value = Math.round ( wt );

   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.weightcalc.feetht.value  = Math.round (   feet );
   document.weightcalc.inchht.value  = Math.round ( inches );
   document.weightcalc.stonewt.value = Math.round ( stones );
   document.weightcalc.lbswt.value   = Math.round ( pounds );
   document.weightcalc.height.value  = Math.round ( ht );

  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_weight_values ( 0, 220, "Please enter age in digits" ); 
     return 0;
   }
   
   age = remove_outer_spaces ( age );

   if ( ( age == "" ) || ( isNaN ( age ) ) ) { 
     clear_weight_values ( 0, 220, "Please enter age in digits" ); 
     return 0;
   }

   if ( ( age < 21 ) || ( age > 100 ) ) { 
     clear_weight_values ( 0, 220, 
        "Please enter age between 21 and 100 years" ); 
     return 0;
   }

   return age;
}


function clear_values ( x, y, message ) {

  checkref = document.referrer.indexOf ( "alphaselect" );
  if ( checkref == -1 )
    y += 168;
  window.scrollTo ( x, y );
  alert ( message ); 
  document.weightcalc.weight.value  = "";
  document.weightcalc.stonewt.value = "";
  document.weightcalc.lbswt.value   = "";
  document.weightcalc.feetht.value  = "";
  document.weightcalc.inchht.value  = "";
  document.weightcalc.height.value  = "";

  return;

} // end funtion clear_values ()


function clear_weight_values ( x, y, message ) {

  document.weightcalc.age.focus ( );
  checkref = document.referrer.indexOf ( "alphaselect" );
  if ( checkref == -1 )
    y += 168;
  window.scrollTo ( x, y );
  alert ( message ); 
  document.weightcalc.age.value     = "";
  document.weightcalc.weight.value  = "";
  document.weightcalc.stonewt.value = "";
  document.weightcalc.lbswt.value   = "";

  return;

} // end funtion clear_weight_values ()


