function perRound(num, precision) {
	var precision = 3; //default value if not passed from caller, change if desired
	// remark if passed from caller
	precision = parseInt(precision); // make certain the decimal precision is an integer
    var result1 = num * Math.pow(10, precision);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, precision);
    return zerosPad(result3, precision);
}

function zerosPad(rndVal, decPlaces) {
    var valStrg = rndVal.toString(); // Convert the number to a string
    var decLoc = valStrg.indexOf("."); // Locate the decimal point
    // check for a decimal 
    if (decLoc == -1) {
        decPartLen = 0; // If no decimal, then all decimal places will be padded with 0s
        // If decPlaces is greater than zero, add a decimal point
        valStrg += decPlaces > 0 ? "." : "";
    }
    else {
        decPartLen = valStrg.length - decLoc - 1; // If there is a decimal already, only the needed decimal places will be padded with 0s
    }
     var totalPad = decPlaces - decPartLen;    // Calculate the number of decimal places that need to be padded with 0s
    if (totalPad > 0) {
        // Pad the string with 0s
        for (var cntrVal = 1; cntrVal <= totalPad; cntrVal++) 
            valStrg += "0";
        }
    return valStrg;
}
// send the value in as "num" in a variable

// clears field of default value
function clear_field(field) {
		if (field.value==field.defaultValue) {
			field.value=''
		}
	}

function gRegister(strFunctionCall){
var strCaller="";
strCaller=strFunctionCall;
return true;
}
function gUnregister(){
strCaller="";
return true;
}

function gGetLastError(){
var intLength=objErrors.length;
if(intLength>0) return objErrors[intLength-1];
else return false;
}

function gWhine(strErrorIn){
var msg='';
msg+='Sorry, minor error occurred.\n\n';
msg+=strErrorIn+'\n';
alert(msg);
}

function gIsNumber(strNumber,strMin,strMax){
/**
 *gIsNumber(strNumber,strMin,strMax)
 *strMin and strMax may be null.
 *if strMin and/or strMax are not null, all are verified as numbers,
 *and strMin<=strNumber and/or strNumber<=strMax
 */
gRegister("gIsNumber");
var strTempNumber=strNumber;//make a copy
strTempNumber=""+strTempNumber; //make sure copy is string
if(strTempNumber.length==0) return false;
for(i=0;i<strTempNumber.length;i++){
if(!((strTempNumber.charAt(i)=="0")||
(strTempNumber.charAt(i)=="1")||
(strTempNumber.charAt(i)=="2")||
(strTempNumber.charAt(i)=="3")||
(strTempNumber.charAt(i)=="4")||
(strTempNumber.charAt(i)=="5")||
(strTempNumber.charAt(i)=="6")||
(strTempNumber.charAt(i)=="7")||
(strTempNumber.charAt(i)=="8")||
(strTempNumber.charAt(i)=="9")||
(strTempNumber.charAt(i)=="-")||
(strTempNumber.charAt(i)=="."))){
return false;
}
}
if(strTempNumber.indexOf(".")!=-1){
var strDecident=strTempNumber.substr(strTempNumber.indexOf("."));
var blnBadDecident=false;
if(strDecident==".") blnBadDecident=true;
for(i=1;i<strDecident.length;i++){
if((strDecident.charAt(i)==".")||
(strDecident.charAt(i)=="-")){
blnBadDecident=true;
}
}
if(blnBadDecident){
return false;
}
}

if ( strTempNumber.indexOf("-") != -1 ) {//if it includes a - test:
if ( strTempNumber.length == 1 ) return false;//bad if - is the only character
if ( strTempNumber.indexOf("-") != 0 ) return false;//bad if - is not the first character
}

if(strMin){
var strTempMin=strMin;
strTempMin=""+strTempMin;
if(strTempMin.length==0) return false;
for(i=0;i<strTempMin.length;i++){
if(!((strTempMin.charAt(i)=="0")||
(strTempMin.charAt(i)=="1")||
(strTempMin.charAt(i)=="2")||
(strTempMin.charAt(i)=="3")||
(strTempMin.charAt(i)=="4")||
(strTempMin.charAt(i)=="5")||
(strTempMin.charAt(i)=="6")||
(strTempMin.charAt(i)=="7")||
(strTempMin.charAt(i)=="8")||
(strTempMin.charAt(i)=="9")||
(strTempMin.charAt(i)=="-")||
(strTempMin.charAt(i)=="."))){
return false;
}
}
if(strNumber<parseFloat(strMin)) return false;
}
if(strMax){
var strTempMax=strMax;
strTempMax=""+strTempMax;
if(strTempMax.length==0) return false;
for(i=0;i<strTempMax.length;i++){
if(!((strTempMax.charAt(i)=="0")||
(strTempMax.charAt(i)=="1")||
(strTempMax.charAt(i)=="2")||
(strTempMax.charAt(i)=="3")||
(strTempMax.charAt(i)=="4")||
(strTempMax.charAt(i)=="5")||
(strTempMax.charAt(i)=="6")||
(strTempMax.charAt(i)=="7")||
(strTempMax.charAt(i)=="8")||
(strTempMax.charAt(i)=="9")||
(strTempMin.charAt(i)=="-")||
(strTempMax.charAt(i)=="."))){
return false;
}
}
if(strNumber>parseFloat(strMax)) return false;
}
gUnregister();
return true;
}

function gMakeNumber(strNumber,strAltValue){
/**
 *gMakeNumber(strNumber,strAltValue)
 *If strNumber is a number, returns strNumber
 *else returns strAltValue
 */
if(gIsNumber(strNumber)) return parseInt(strNumber)
else return strAltValue;
}


function gConvertNumberToPercent(strToConvert){
/**
 *gConvertNumberToPercent(strToConvert)
 *converts numbers and numeric strings to percent strings.
 *A value not either returns false.
 */
gRegister("gConvertNumberToPercent");
if(gIsNumber(strToConvert,null,null)){
strToConvert=strToConvert*100;
strToConvert=strToConvert+"%";
}else{
gUnregister();
return false;
}
gUnregister();
return strToConvert;
}

function gConvertPercentToNumber(strToConvert){
/**
 *gConvertPercentToNumber(strToConvert)
 *converts percent strings to numbers and numeric strings.
 *A value not either returns false.
 */
gRegister("gConvertPercentToNumber");
if(strToConvert.indexOf("%")==strToConvert.length-1){
var strNewValue=strToConvert.substring(0,strToConvert.indexOf("%"));
if(gIsNumber(strNewValue,null,null)){
strToConvert=strNewValue/100;
}
}else{
gUnregister();
return false;
}
gUnregister();
return strToConvert;
}

function gCommify(strToConvert){
/**
 *gCommify(strToConvert)
 *commifies numbers and numeric strings.
 *A non-numeric string without commas returns false.
 */
gRegister("gCommify");
var strNewValue=strToConvert;
if(gIsNumber(strNewValue,null,null)){
strNewValue=strNewValue.toString();
if(strNewValue.indexOf(".")!=-1){
var strDecident=strNewValue.substring(strNewValue.indexOf("."));
strNewValue=strNewValue.substring(0,strNewValue.indexOf("."));
}
var intLength=strNewValue.length;
//var intComma=((strNewValue.length-1)-((strNewValue.length-1) % 3))/3;//finds number of commas.
for(i=3;i<intLength;i=i+3){
strNewValue=strNewValue.substring(0,intLength-i)+","+strNewValue.substring(intLength-i);
}
if(strDecident) strNewValue=strNewValue+strDecident;
strToConvert=strNewValue;
}else{
gUnregister();
return false;
}
gUnregister();
return strToConvert;
}

function gDeCommify(strToConvert){
/**
 *gDeCommify(strToConvert)
 *decommifies strings.
 *A number or string without commas returns false.
 */
gRegister("gDeCommify");
var strNewValue=strToConvert;
strNewValue=strNewValue.toString();
if(strNewValue.indexOf(",")!=-1){
while(strNewValue.indexOf(",")!=-1){
strNewValue=strNewValue.substring(0,strNewValue.indexOf(","))+strNewValue.substring(strNewValue.indexOf(",")+1);
}
strToConvert=strNewValue;
}else{
gUnregister();
return false;
}
gUnregister();
return strToConvert;
}

function gToInches(strFeet,strInches,strMin,strMax){
/**
 *gToInches(strFeet,strInches,strMin,strMax)
 *strMin and strMax may be null.If not, they should be inches values.
 *if strMin and/or strMax are not null, all are converted to inches,
 *and strMin<=strDate and/or strDate<=strMax
 */
gRegister("gToInches");
if(!gIsNumber(strFeet)) return false;
if(!gIsNumber(strInches)) return false;
strInches+=(strFeet*12);
if(strMin){
if(!gIsNumber(strMin)) return false;
if(strInches<strMin) return false;
}
if(strMax){
if(!gIsNumber(strMax)) return false;
if(strInches>strMax) return false;
}
gUnregister();
return strInches;
}

function gToFeetAndInches(strInches,strMin,strMax){
/**
 *gToFeetAndInches(strInches,strMin,strMax)
 *strMin and strMax may be null.
 *if strMin and/or strMax are not null, return a string of x'y",
 *where x=feet and y=inches,
 *and strMin<=strDate and/or strDate<=strMax
 */
gRegister("gToFeetAndInches");
if(!gIsNumber(strInches)) return false;
if(strMin){
if(!gIsNumber(strMin)) return false;
if(strInches<strMin) return false;
}
if(strMax){
if(!gIsNumber(strMax)) return false;
if(strInches>strMax) return false;
}
var strReturnValue='';
strReturnValue=(strInches-(parseInt(strInches)%12))/12+'\''+(parseInt(strInches)%12)+'"';
gUnregister();
return strReturnValue;
}

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 process (callerObj) {
var age = callerObj.form.age.options[callerObj.form.age.selectedIndex].value;
if(callerObj.form.gender[0].checked){
var gender = 'female';
}else if(callerObj.form.gender[1].checked){
var gender = 'male';
}else{
return;
}

var mFeetVal = gMakeNumber(callerObj.form.mFeet.value, 0);
var mInchVal = gMakeNumber(callerObj.form.mInch.value, 0);
var mNewFeetVal = Math.floor(mInchVal/12);
mInchVal = (mInchVal%12);
callerObj.form.mInch.value=mInchVal;
mFeetVal+=mNewFeetVal;

var fFeetVal = gMakeNumber(callerObj.form.fFeet.value, 0);
var fInchVal = gMakeNumber(callerObj.form.fInch.value, 0);
var fNewFeetVal = Math.floor(fInchVal/12);
fInchVal = (fInchVal%12);
callerObj.form.fInch.value=fInchVal;
fFeetVal+=fNewFeetVal;

var cFeetVal = gMakeNumber(callerObj.form.cFeet.value, 0);
var cInchVal = gMakeNumber(callerObj.form.cInch.value, 0);
var cNewFeetVal = Math.floor(cInchVal/12);
cInchVal = (cInchVal%12);
callerObj.form.cInch.value=cInchVal;
cFeetVal+=cNewFeetVal;

var cW = gMakeNumber(callerObj.form.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); 
} 

//deal with extreme values
if ( rs <= 0 || rs < cH ) {
alert("We are unable to calculate an adult height\n" +
"for your child based on the information you have provided\n\n" +
"Please check to be sure you have entered the correct values");
} else {
callerObj.form.result.value = gToFeetAndInches(rs);
}
}
