var oldFringeRate;      //global Variables.

function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
 

        IN:
                NUM - the number to format
                decimalNum - the number of decimal places to format the number to
                bolLeadingZero - true / false - display a leading zero for
                                                                                numbers between -1 and 1
                bolParens - true / false - use parenthesis around negative numbers
                bolCommas - put commas as number separators.
 
        RETVAL:
                The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

        var tmpNum = num;
        var iSign = num < 0 ? -1 : 1;           // Get sign of number
        
        // Adjust number so only the specified number of numbers after
        // the decimal point are shown.
        tmpNum *= Math.pow(10,decimalNum);
        tmpNum = Math.round(Math.abs(tmpNum))
        tmpNum /= Math.pow(10,decimalNum);
        tmpNum *= iSign;                                        // Readjust for sign
        
        
        // Create a string object to do our formatting on
        var tmpNumStr = new String(tmpNum);

        // See if we need to strip out the leading zero or not.
        if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
                if (num > 0)
                        tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
                else
                        tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
                
        // See if we need to put in the commas
        if (bolCommas && (num >= 1000 || num <= -1000)) {
                var iStart = tmpNumStr.indexOf(".");
                if (iStart < 0)
                        iStart = tmpNumStr.length;

                iStart -= 3;
                while (iStart >= 1) {
                        tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
                        iStart -= 3;
                }               
        }

        // See if we need to use parenthesis
        if (bolParens && num < 0)
                tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

        var decNum = tmpNumStr.split('.');
        var strDecNum = new String(decNum[1]);
//      alert("hello there!");
        if (strDecNum.length == 1) {
                tmpNumStr = tmpNumStr + "0";
        }
        return tmpNumStr;               // Return our formatted string!
}


function getRadioValue() {
        for (var i=0; i < document.calculator.bPackage.length; i++){  
                if (document.calculator.bPackage[i].checked) {
                        myValue = document.calculator.bPackage[i].value;
                }
        }
        return myValue;
}

function packageAdjustments(){
        var newTitle = getRadioValue();

        var docHeading = document.getElementById("docHeading");
        var fringeRate = document.getElementById("fringeRate");
        var newPercent;

        docHeading.firstChild.nodeValue = newTitle;
        document.calculator.outputPackage.value = newTitle;
        
        if (newTitle == "Davis Bacon"){
                newPercent = 25;
        } else {
                newPercent = 15;
        }
        document.calculator.percentPB.value = newPercent;
}


function returnFringeRate(){
        var fringeRate = document.getElementById("fringeRate");
        
        if (!(isNaN(oldFringeRate))){
                document.calculator.fringeRate.value = oldFringeRate;
                packageAdjustments();
        } 
}


function saveFringeRate(){
        var fringeRate = document.getElementById("fringeRate");

        if (!(isNaN(document.calculator.fringeRate.value))){
                oldFringeRate = document.calculator.fringeRate.value;
        } 
        document.calculator.fringeRate.value = 3.16;
        packageAdjustments();

}

function calculate(){

        var pbPercent, intHourlyWages, intFringeRate, intEmployeeNum, intNB_CashWages, intWB_CashWages;
        var intNB_PayBurden, intWB_PayBurden, intNB_FringePaid, intWB_FringePaid, intNB_CostPerHour, intWB_CostPerHour;
        var intNB_HourlySavings, intWB_HourlySavings, intEmployeeNum; 
        
        
        intEmployeeNum = parseInt(document.calculator.employeeNum.value);
        intHourlyWages = parseFloat(document.calculator.hourlyWages.value);
        intFringeRate = parseFloat(document.calculator.fringeRate.value);

//      Proceed with calculations only if all 3 numbers have been input. 
        if (!(isNaN(intEmployeeNum) || isNaN(intHourlyWages) || isNaN(intFringeRate))){ 
                intNB_CashWages = intHourlyWages + intFringeRate; 
                intWB_CashWages = intHourlyWages; 
        
                pbPercent = document.calculator.percentPB.value/100;
                
                with (document.calculator){     
                        NB_cashWages.value = FormatNumber(intNB_CashWages,2,true,true,true); 
                        WB_cashWages.value = FormatNumber(intWB_CashWages,2,true,true,true); 
                
                        intNB_FringePaid = 0.00;
                        intWB_FringePaid = intFringeRate;
                        NB_fringePaid.value = FormatNumber(intNB_FringePaid,2,true,true,true); 
                        WB_fringePaid.value = FormatNumber(intWB_FringePaid,2,true,true,true); 
                        
                        intNB_PayBurden = intNB_CashWages * pbPercent;
                        intWB_PayBurden = intWB_CashWages * pbPercent;
                        NB_payBurden.value = FormatNumber(intNB_PayBurden,2,true,true,true); 
                        WB_payBurden.value = FormatNumber(intWB_PayBurden,2,true,true,true); 
                        
                        intNB_CostPerHour = intNB_CashWages + intNB_FringePaid + intNB_PayBurden;
                        intWB_CostPerHour = intWB_CashWages + intWB_FringePaid + intWB_PayBurden;
                        NB_costPerHour.value = FormatNumber(intNB_CostPerHour,2,true,true,true); 
                        WB_costPerHour.value = FormatNumber(intWB_CostPerHour,2,true,true,true); 
                        
                        intNB_HourSavings = 0.00;
                        intWB_HourSavings = intNB_CostPerHour - intWB_CostPerHour;
                        NB_hourSavings.value = FormatNumber(intNB_HourSavings,3,true,true,true); 
                        WB_hourSavings.value = FormatNumber(intWB_HourSavings,3,true,true,true); 
                        
                        intNB_MonthSavings = intNB_HourSavings * 173;
                        intWB_MonthSavings = intWB_HourSavings * 173;
                        NB_monthSavings.value = FormatNumber(intNB_MonthSavings,2,true,true,true); 
                        WB_monthSavings.value = FormatNumber(intWB_MonthSavings,2,true,true,true); 
                        
                        intNB_AnnualSavings = intNB_MonthSavings * 12;
                        intWB_AnnualSavings = intWB_MonthSavings * 12;
                        NB_annualSavings.value = FormatNumber(intNB_AnnualSavings,2,true,true,true); 
                        WB_annualSavings.value = FormatNumber(intWB_AnnualSavings,2,true,true,true); 
                        
                        intNB_EmployerSavings = intNB_AnnualSavings * intEmployeeNum;
                        intWB_EmployerSavings = intWB_AnnualSavings * intEmployeeNum;
                        NB_employerSavings.value = FormatNumber(intNB_EmployerSavings,0,true,true,true); 
                        WB_employerSavings.value = FormatNumber(intWB_EmployerSavings,0,true,true,true); 
                } // end of with statement.
        }       
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
