function calc (beta0,beta1,beta2,beta3,mHeight,fHeight,cHeight,cWeight) {
  // Adultheight = 0M + 1M*childHeight + 2M*childWeight + 3M*parentsHeightsAvg 
  var pAvgHeight = ((mHeight+fHeight)/2);  
  var aHeight = beta0 + (beta1*cHeight) + (beta2*cWeight) + (beta3*pAvgHeight);
  return Math.round(aHeight);// in Inch
}

function adjustHeight(){

  if ( (document.HeightPredictor.mFeet.value>=0)    || (document.HeightPredictor.mInch.value>=0) ){ 
	var mFeetVal = gMakeNumber(document.HeightPredictor.mFeet.value, 0);
	var mInchVal = gMakeNumber(document.HeightPredictor.mInch.value, 0 );
        if (mInchVal >= 12) { 
		mFeetVal = mFeetVal + Math.floor(mInchVal/12);   
                mInchVal = mInchVal - (Math.floor(mInchVal/12)*12);
		   document.HeightPredictor.mFeet.value = mFeetVal;
		   document.HeightPredictor.mInch.value = mInchVal;
        }
                if ( (mFeetVal*12 + mInchVal) > 96) {
                     document.HeightPredictor.mFeet.value = 0;
                     document.HeightPredictor.mInch.value = 0;                       
                     alert('Sorry, mother\'s height cannot exceed 8 feet.');
                }
  }
  if( (document.HeightPredictor.fFeet.value>=0)    || (document.HeightPredictor.fInch.value>=0) ){ 
       var fFeetVal = gMakeNumber(document.HeightPredictor.fFeet.value, 0);
       var fInchVal = gMakeNumber(document.HeightPredictor.fInch.value, 0 );
       if (fInchVal >= 12) { 
		fFeetVal = fFeetVal + Math.floor(fInchVal/12);   
                fInchVal = fInchVal - (Math.floor(fInchVal/12)*12);
		document.HeightPredictor.fFeet.value = fFeetVal;
		document.HeightPredictor.fInch.value = fInchVal;
       }
                if ( (fFeetVal*12 + fInchVal) > 96) {
                     document.HeightPredictor.fFeet.value = 0;
                     document.HeightPredictor.fInch.value = 0;
                     alert('Sorry, father\' height cannot exceed 8 feet.');
                }

  }
  if( (document.HeightPredictor.cFeet.value>=0)    || (document.HeightPredictor.cInch.value>=0) ){ 
       var cFeetVal = gMakeNumber(document.HeightPredictor.cFeet.value, 0);
       var cInchVal = gMakeNumber(document.HeightPredictor.cInch.value, 0 );
       if (cInchVal >= 12) { 
           cFeetVal = cFeetVal + Math.floor(cInchVal/12);   
           cInchVal = cInchVal - (Math.floor(cInchVal/12)*12);
	   document.HeightPredictor.cFeet.value = cFeetVal;
	   document.HeightPredictor.cInch.value = cInchVal;
       }
                if ( (cFeetVal*12 + cInchVal) > 96) {
                     document.HeightPredictor.cFeet.value = 0;
                     document.HeightPredictor.cInch.value = 0;
                     alert('Sorry, child\'s height cannot exceed 8 feet.');
                }
  }
}


function process () {
  var age = document.HeightPredictor.age.options[document.HeightPredictor.age.selectedIndex].value;
  if(document.HeightPredictor.gender[0].checked){
    var gender = 'female';
  }else if(document.HeightPredictor.gender[1].checked){
    var gender = 'male';
  }else{
    return;
  }

  var mFeetVal = gMakeNumber(document.HeightPredictor.mFeet.value, 0);

  // Converts mInch to an integer where parseInt fails.
  mInchVal = fetchValue(document.HeightPredictor.mInch.value);
  // var mInchVal = gMakeNumber(document.HeightPredictor.mInch.value, 0);

  var mNewFeetVal = Math.floor(mInchVal/12);
  mInchVal = (mInchVal%12);
  document.HeightPredictor.mInch.value=mInchVal;
  mFeetVal+=mNewFeetVal;

  var fFeetVal = gMakeNumber(document.HeightPredictor.fFeet.value, 0);
  
  // RAMAA --  BUG #45492
  fInchVal = fetchValue(document.HeightPredictor.fInch.value);
 //  var fInchVal = gMakeNumber(document.HeightPredictor.fInch.value, 0);

  var fNewFeetVal = Math.floor(fInchVal/12);
  fInchVal = (fInchVal%12);
  document.HeightPredictor.fInch.value=fInchVal;
  fFeetVal+=fNewFeetVal;
  var cFeetVal = gMakeNumber(document.HeightPredictor.cFeet.value, 0);

  // RAMAA --  BUG #45492
  cInchVal = fetchValue(document.HeightPredictor.cInch.value);
 //  var cInchVal = gMakeNumber(document.HeightPredictor.cInch.value, 0);

  var cNewFeetVal = Math.floor(cInchVal/12);
  cInchVal = (cInchVal%12);
  document.HeightPredictor.cInch.value=cInchVal;
  cFeetVal+=cNewFeetVal;

  var cW = gMakeNumber(document.HeightPredictor.cWeight.value, 0);

  // must have mother and father info:
  if ((mFeetVal <= 0) && (mInchVal <= 0)){
    alert('There appears to be an error in the mother\'s height.');
    return;
  }
  if ((fFeetVal <= 0) && (fInchVal <= 0)){
    alert('There appears to be an error in the father\'s height.');
    return;
  }
  if ((age!="LT4") && (cFeetVal <= 0) && (cInchVal <= 0)){
    alert('There appears to be an error in the child\'s height.');
    return;
  }
  if ((age!="LT4") && (cW <=0)){
    alert('There appears to be an error in the child\'s weight.');
    return;
  }
  if (!gender){
    alert('Please select your child\'s gender.');
    return;
  }

  var rs = 0;
  
  var mH = gToInches(mFeetVal, mInchVal);
  var fH = gToInches(fFeetVal, fInchVal);
  var cH = gToInches(cFeetVal, cInchVal);
  
  if (gender == "male") {
   if(age=="LT4") rs=calc(3.000,0,0,1.00000,mH,fH,cH,cW);
   if(age=="040") rs=calc(-10.25670,1.23812,-0.08724,0.50286,mH,fH,cH,cW);
   if(age=="045") rs=calc(-10.71900,1.15964,-0.07445,0.52887,mH,fH,cH,cW);
   if(age=="050") rs=calc(-11.02130,1.10674,-0.06478,0.53919,mH,fH,cH,cW);
   if(age=="055") rs=calc(-11.15560,1.07480,-0.05776,0.53691,mH,fH,cH,cW);
   if(age=="060") rs=calc(-11.11380,1.05923,-0.05295,0.52513,mH,fH,cH,cW);
   if(age=="065") rs=calc(-11.02210,1.05542,-0.04989,0.50692,mH,fH,cH,cW);
   if(age=="070") rs=calc(-10.99840,1.05877,-0.04814,0.48538,mH,fH,cH,cW);
   if(age=="075") rs=calc(-11.02140,1.06467,-0.04726,0.46361,mH,fH,cH,cW);
   if(age=="080") rs=calc(-11.06960,1.06853,-0.04678,0.44469,mH,fH,cH,cW);
   if(age=="085") rs=calc(-11.12200,1.06572,-0.04626,0.43171,mH,fH,cH,cW);
   if(age=="090") rs=calc(-11.15710,1.05166,-0.04525,0.42776,mH,fH,cH,cW);
   if(age=="095") rs=calc(-11.14050,1.02174,-0.04331,0.43593,mH,fH,cH,cW);
   if(age=="100") rs=calc(-11.03800,0.97135,-0.03998,0.45932,mH,fH,cH,cW);
   if(age=="105") rs=calc(-10.82860,0.89589,-0.03481,0.50101,mH,fH,cH,cW);
   if(age=="110") rs=calc(-10.49170,0.81239,-0.02905,0.54781,mH,fH,cH,cW);
   if(age=="115") rs=calc(-10.00650,0.74134,-0.02417,0.58409,mH,fH,cH,cW);
   if(age=="120") rs=calc(-9.35220,0.68325,-0.02008,0.60927,mH,fH,cH,cW);
   if(age=="123") rs=calc(-8.60550,0.63869,-0.01668,0.62279,mH,fH,cH,cW);
   if(age=="130") rs=calc(-7.86320,0.60818,-0.01390,0.62407,mH,fH,cH,cW);
   if(age=="135") rs=calc(-7.13480,0.59228,-0.01162,0.61253,mH,fH,cH,cW);
   if(age=="140") rs=calc(-6.42990,0.59151,-0.00978,0.58762,mH,fH,cH,cW);
   if(age=="145") rs=calc(-5.75780,0.60643,-0.00826,0.54875,mH,fH,cH,cW);
   if(age=="150") rs=calc(-5.12820,0.63757,-0.00699,0.49536,mH,fH,cH,cW);
   if(age=="155") rs=calc(-4.50920,0.68548,-0.00586,0.42687,mH,fH,cH,cW);
   if(age=="160") rs=calc(-3.92920,0.75069,-0.00480,0.34271,mH,fH,cH,cW);
   if(age=="165") rs=calc(-3.48730,0.83375,-0.00370,0.24231,mH,fH,cH,cW);
   if(age=="170") rs=calc(-3.28300,0.93520,-0.00247,0.12510,mH,fH,cH,cW);
   if(age=="175") rs=calc(-3.41560,1.05558,-0.00103,-0.00950,mH,fH,cH,cW);
  } else if (gender == "female") {
   if(age=="LT4") rs=calc(-3.00000,0,0,1.00000,mH,fH,cH,cW);
   if(age=="040") rs=calc(-8.13250,1.24768,-0.19435,0.44774,mH,fH,cH,cW);
   if(age=="045") rs=calc(-6.47656,1.22177,-0.18519,0.41381,mH,fH,cH,cW);
   if(age=="050") rs=calc(-5.13583,1.19932,-0.17530,0.38460,mH,fH,cH,cW); 
   if(age=="055") rs=calc(-4.13791,1.17880,-0.16484,0.36039,mH,fH,cH,cW); 
   if(age=="060") rs=calc(-3.51039,1.15866,-0.15400,0.34105,mH,fH,cH,cW);
   if(age=="065") rs=calc(-3.14322,1.13737,-0.14294,0.32672,mH,fH,cH,cW); 
   if(age=="070") rs=calc(-2.87645,1.11342,-0.13184,0.31748,mH,fH,cH,cW); 
   if(age=="075") rs=calc(-2.66291,1.08525,-0.12086,0.31340,mH,fH,cH,cW); 
   if(age=="080") rs=calc(-2.45559,1.05135,-0.11019,0.31457,mH,fH,cH,cW); 
   if(age=="085") rs=calc(-2.20728,1.01018,-0.09999,0.32105,mH,fH,cH,cW); 
   if(age=="090") rs=calc(-1.87098,0.96020,-0.09044,0.33291,mH,fH,cH,cW); 
   if(age=="095") rs=calc(-1.06330,0.89989,-0.08171,0.35025,mH,fH,cH,cW); 
   if(age=="100") rs=calc(0.33468,0.82771,-0.07397,0.37312,mH,fH,cH,cW); 
   if(age=="105") rs=calc(1.97366,0.74213,-0.06739,0.40161,mH,fH,cH,cW); 
   if(age=="110") rs=calc(3.50436,0.67173,-0.06136,0.42042,mH,fH,cH,cW); 
   if(age=="115") rs=calc(4.57747,0.64150,-0.05518,0.41686,mH,fH,cH,cW); 
   if(age=="120") rs=calc(4.84365,0.64452,-0.04894,0.39490,mH,fH,cH,cW); 
   if(age=="125") rs=calc(4.27869,0.67386,-0.04272,0.35850,mH,fH,cH,cW); 
   if(age=="130") rs=calc(3.21417,0.72260,-0.03661,0.31163,mH,fH,cH,cW); 
   if(age=="135") rs=calc(1.83456,0.78383,-0.03067,0.25826,mH,fH,cH,cW); 
   if(age=="140") rs=calc(0.32425,0.85062,-0.02500,0.20235,mH,fH,cH,cW); 
   if(age=="145") rs=calc(-1.13224,0.91605,-0.01967,0.14787,mH,fH,cH,cW); 
   if(age=="150") rs=calc(-2.35055,0.97319,-0.01477,0.09880,mH,fH,cH,cW); 
   if(age=="155") rs=calc(-3.10326,1.01514,-0.01037,0.05909,mH,fH,cH,cW); 
   if(age=="160") rs=calc(-3.17885,1.03496,-0.00655,0.03272,mH,fH,cH,cW); 
   if(age=="165") rs=calc(-2.41657,1.02573,-0.00340,0.02364,mH,fH,cH,cW); 
   if(age=="170") rs=calc(-0.65579,0.98054,-0.00100,0.03584,mH,fH,cH,cW); 
   if(age=="175") rs=calc(2.26429,0.89246,0.00057,0.07327,mH,fH,cH,cW); 
  } 
  // adult can't be shorter than child
  rs = Math.max(rs, cH);
  document.HeightPredictor.result.value = gToFeetAndInches(rs); 
  //alert(" rs = "+rs+"\n age = "+age+"\n Gen = "+gender+"\n mH = "+mH+"\n fH = "+fH+"\n cH = "+cH+"\n cW = "+cW)
}

// this converts temp to a integer even in the case
// where parseInt fails for example '08'.
function fetchValue(temp)
{
	var str = temp.toString();
	if(str.charAt(0) == '0')  // if first char is '0' then parseInt does NOT always work correctly e.g try '08'!! 
		{
			str[0] = str.charAt(1);
			str[1] = null;
			return eval(str);
		}
	 return gMakeNumber(temp, 0); 
}

// The following function parses the search string in the URL encoding and returns the values to the form.
function adBannerCalc(strQuery)
{
	// Get the gender
	var gender = strGetQueryValue(strQuery, 'Gender');
        if(gender != 'male' && gender != 'female') gender = 'female';

	// set the radio button
	var index = 0;
	if(gender == 'male') index = 1;
	document.HeightPredictor.gender[index].checked = 1;
	
	// get the Age
	var age = strGetQueryValue(strQuery, 'Age');
        if (age == '') age = '050';

	// Correcting for error in URL string.
	// IF age does not correspond to the values in the combo box, set it to the default value 5.0
	var N = document.HeightPredictor.age.length; // number of items in select box
	
	var isPresent = false; // indicator
	for(k = 0; k < N; k++)
	{
		if(age == document.HeightPredictor.age.options[k].value) 
		{
			isPresent = true;
			break;
		}
	}
	if(!isPresent) age = '050';

         // find the correct index in drop down combo
	index = 0;
	while(document.HeightPredictor.age.options[index].value != age)
	{
		// return in case not found
		if(index > 10000) return;
		index++;
	}

	// select the correct index
	document.HeightPredictor.age.selectedIndex = index;

	// calculate child's height.
	if (adjustHeight()) processs();

}

