﻿var bResetDefaults = false;

function createPDF()
{
alert('This function is currently not available');
}


// Calculates the object's absolute position, and width and height
function GetAbsPosition(object) {
var position = new Object;
position.x = 0;
position.y = 0;
position.bottom = 0;
position.right = 0;

if( object ) {
position.x = object.offsetLeft;
position.y = object.offsetTop;

if( object.offsetParent ) {
var parentpos = GetAbsPosition(object.offsetParent);
position.x += parentpos.x;
position.y += parentpos.y;
}
}

position.cx = object.offsetWidth;
position.cy = object.offsetHeight;
position.bottom = position.y + position.cy;
position.right = position.x + position.cx;
return position;
}

function setDefaultValues() {
    //Set flag to disable calculations until all fields have been updated
    bResetDefaults = true;   

    //Total Word
    window.document.getElementById("JPTotalWords").value = "100000";
    window.document.getElementById("JPSampleWords").value = "5000";

    //Translator
    window.document.getElementById("JPTranslatorCPW").value = "0.10"; //10 cents
    window.document.getElementById("JPTranslatorWPD").value = "2500";

    //Post Editor
    window.document.getElementById("JPHumanPostEditorCPW").value = "0.05"; //5 cents
    window.document.getElementById("JPHumanPostEditorWPD").value = "5000";

    //Machine Translation
    window.document.getElementById("JPMachineTranslationCPW").value = "0.005"; //0.5 cents
    window.document.getElementById("JPMachineTranslationWPD").value = "500000";

    //Machine Translation Post Editor
    window.document.getElementById("JPMachineTranslationPostEditorCPW").value = "0.03"; //3 cents
    window.document.getElementById("JPMachineTranslationPostEditorWPD").value = "10000";

    
    //Segment Count
    window.document.getElementById("SC1").value = "0"; 
    window.document.getElementById("SC2").value = "0"; 
    window.document.getElementById("SC3").value = "0"; 
    window.document.getElementById("SC4").value = "0"; 
    window.document.getElementById("SC5").value = "0"; 
    window.document.getElementById("SC6").value = "0"; 
    window.document.getElementById("SC7").value = "0"; 
    window.document.getElementById("SC8").value = "0"; 
    window.document.getElementById("SC9").value = "0"; 
    window.document.getElementById("SC10").value = "0"; 
    window.document.getElementById("SC11").value = "0"; 
    window.document.getElementById("SC12").value = "0"; 

    //Effort to correct
    window.document.getElementById("ETC2").value = "100"; 
    window.document.getElementById("ETC3").value = "80"; 
    window.document.getElementById("ETC4").value = "60"; 
    window.document.getElementById("ETC5").value = "50"; 
    window.document.getElementById("ETC6").value = "30"; 
    window.document.getElementById("ETC7").value = "30"; 
    window.document.getElementById("ETC8").value = "20"; 
    window.document.getElementById("ETC9").value = "20"; 
    window.document.getElementById("ETC10").value = "5"; 
    window.document.getElementById("ETC11").value = "5"; 
    window.document.getElementById("ETC12").value = "5"; 
    
    //
    window.document.getElementById("SLRTC2").selectedIndex = "0"; 
    window.document.getElementById("SLRTC3").selectedIndex = "0"; 
    window.document.getElementById("SLRTC4").selectedIndex = "1"; 
    window.document.getElementById("SLRTC5").selectedIndex = "1"; 
    window.document.getElementById("SLRTC6").selectedIndex = "1"; 
    window.document.getElementById("SLRTC7").selectedIndex = "1"; 
    window.document.getElementById("SLRTC8").selectedIndex = "1"; 
    window.document.getElementById("SLRTC9").selectedIndex = "1"; 
    window.document.getElementById("SLRTC10").selectedIndex = "1";
    window.document.getElementById("SLRTC11").selectedIndex = "1";
    window.document.getElementById("SLRTC12").selectedIndex = "1";
    
    
    //////////////// TEST ONLY
    //Total Word
    window.document.getElementById("JPTotalWords").value = "1000";//"10000";
    window.document.getElementById("JPSampleWords").value = "500";

    //Translator
    window.document.getElementById("JPTranslatorCPW").value = "0.10"; //10 cents
    window.document.getElementById("JPTranslatorWPD").value = "2500";

    //Post Editor
    window.document.getElementById("JPHumanPostEditorCPW").value = "0.05"; //5 cents
    window.document.getElementById("JPHumanPostEditorWPD").value = "5000";

    //Machine Translation
    window.document.getElementById("JPMachineTranslationCPW").value = "0.005"; //0.5 cents
    window.document.getElementById("JPMachineTranslationWPD").value = "500000";

    //Machine Translation Post Editor
    window.document.getElementById("JPMachineTranslationPostEditorCPW").value = "0.03"; //3 cents
    window.document.getElementById("JPMachineTranslationPostEditorWPD").value = "10000";

    //Segment Count
    window.document.getElementById("SC1").value = "34"; 
    window.document.getElementById("SC2").value = "1"; 
    window.document.getElementById("SC3").value = "2"; 
    window.document.getElementById("SC4").value = "3"; 
    window.document.getElementById("SC5").value = "4"; 
    window.document.getElementById("SC6").value = "5"; 
    window.document.getElementById("SC7").value = "6"; 
    window.document.getElementById("SC8").value = "7"; 
    window.document.getElementById("SC9").value = "8"; 
    window.document.getElementById("SC10").value = "9";
    window.document.getElementById("SC11").value = "10";
    window.document.getElementById("SC12").value = "11";
    
    //Reset all calculations
    bResetDefaults=false; //Enable calculations
    Calc();
}


function isNumberKey(e, bAllowDecimal, iDecimalPlaces, dMaxValue) {
    var key;
    var keychar;
    var value;
    var pos;
    var dec;

//TODO: handle maxvalue

    if (window.event) {
        key = window.event.keyCode;
    }
    else if (e) {
        key = e.which;
    }
    else {
        return true;
    }
    keychar = String.fromCharCode(key);
    value = e.srcElement.value;

    if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) {
        return true;
    }
    else if ((("0123456789").indexOf(keychar) > -1)) {


        pos = findPos(value, '.');
        if (pos > 0) {
            dec = value.substring(pos, value.length);
            if (dec.length > 2) {
                return false;
            }
        }
        return true;
    }
    else if (bAllowDecimal && (keychar == ".")) {
        //chck to make sure there is no decimal already
        if (value.indexOf('.') > -1)
            return false;
    }
    else
        return false;
}

function setInnerText(sID, sValue)
{
    if(document.all) //Check for IE
    {
        window.document.getElementById(sID).innerText = sValue;
    } 
    else //Firefox
    {
        window.document.getElementById(sID).textContent = sValue;
    }
}

function getFloatFromValue(sID)
{
    sWork = window.document.getElementById(sID).value;
    return parseFloat(sWork);
}

function getIntFromValue(sID)
{
    sWork = window.document.getElementById(sID).value;
    return parseInt(sWork);
}

function Calc() {
    var bValidData = true;

    //Job Parameters
    var iJPTotalWords = 0;
    var iJPSampleWords = 0;
    var dJPTranslatorCPW = 0.0;
    var iJPTranslatorWPD = 0;
    var dJPHumanPostEditorCPW = 0.0;
    var iJPHumanPostEditorWPD = 0;
    var dJPMachineTranslationCPW = 0.0;
    var iJPMachineTranslationWPD = 0;
    

    //Skills and Cost Comparison
    var dCostPerWord = 0.0;
    var iWordsPerDay = 0;
    var dCostPerDay = 0.0;
    var dTotalJobCost = 0.0;
    var dTotalJobDays = 0.0;


    var iSegmentCount = 0;
    var iTotalSegments = 0;
    var iSegment1Count = 0;
    var iTotalErrorSegments = 0;    
    var dAverageWordsPerSegment = 0.0;
    var dErrorSegmentsPercentageOfTotal = 0.0;

    //Check to see if we are in the middle of resetting the default values
    if (bResetDefaults)
    {
        //Reset in progress. Abort calculations.
        return 0;
    }   

    //LOAD JOB PARAMETERS
    //Total Words
    iJPTotalWords = getIntFromValue("JPTotalWords");
    window.document.getElementById("JPTotalWords").value = trimNumber(iJPTotalWords);

    //Sample Words
    iJPSampleWords = getIntFromValue("JPSampleWords");
    window.document.getElementById("JPSampleWords").value = trimNumber(iJPSampleWords);

    //Translator
    dJPTranslatorCPW = getFloatFromValue("JPTranslatorCPW");
    window.document.getElementById("JPTranslatorCPW").value = trimNumber(dJPTranslatorCPW);
    iJPTranslatorWPD = getIntFromValue("JPTranslatorWPD");
    window.document.getElementById("JPTranslatorWPD").value = trimNumber(iJPTranslatorWPD);

    //Post Editor
    dJPHumanPostEditorCPW = getFloatFromValue("JPHumanPostEditorCPW");
    window.document.getElementById("JPHumanPostEditorCPW").value = trimNumber(dJPHumanPostEditorCPW);
    iJPHumanPostEditorWPD = getIntFromValue("JPHumanPostEditorWPD");
    window.document.getElementById("JPHumanPostEditorWPD").value = trimNumber(iJPHumanPostEditorWPD);

    //Machine Translation
    dJPMachineTranslationCPW = getFloatFromValue("JPMachineTranslationCPW");
    window.document.getElementById("JPMachineTranslationCPW").value = trimNumber(dJPMachineTranslationCPW);
    iJPMachineTranslationWPD = getIntFromValue("JPMachineTranslationWPD");
    window.document.getElementById("JPMachineTranslationWPD").value = trimNumber(iJPMachineTranslationWPD);

    //Machine Translation Post Editor
    dJPMachineTranslationPostEditorCPW = getFloatFromValue("JPMachineTranslationPostEditorCPW");
    window.document.getElementById("JPMachineTranslationPostEditorCPW").value = trimNumber(dJPMachineTranslationPostEditorCPW);
    iJPMachineTranslationPostEditorWPD = getIntFromValue("JPMachineTranslationPostEditorWPD");
    window.document.getElementById("JPMachineTranslationPostEditorWPD").value = trimNumber(iJPMachineTranslationPostEditorWPD);

    //SEGMENT COUNTS
    //Count the total number and format segments
    iTotalSegments=0;
    for(iRowIndex = 1;iRowIndex<=12;iRowIndex++)
    {
        iSegmentCount = (window.document.getElementById("SC" + iRowIndex).value) * 1;
        iTotalSegments += iSegmentCount;
        if (iRowIndex==1)
        {
            iSegment1Count = iSegmentCount;
        }
        window.document.getElementById("SC" + iRowIndex).value = trimNumber(iSegmentCount);   
    }
    
    //Calculate additional information about segments need to calculate table
    iTotalErrorSegments = iTotalSegments - iSegment1Count;
    dAverageWordsPerSegment = Math.round((iJPSampleWords / iTotalSegments)*10)/10; //Based on the sample size

    //CALCULATE TABLE VALUES
    var iSkillLevelRequiredToCorrect = -1;
    var iSegmentCount = 0;
    var iEffortToCorrect = 0;
    var iApproxWords = 0;
    var dTotalEffortCorrectionSegments = 0.0;
    var dTotalEffortReductionSegments = 0.0;
    var dSkillCostPerWord = 0.0;
    var dTotalSegmentCostNoMT = 0.0;
    var dSegmentCorrectionCostPostMT = 0.0;
    var dTotalSegmentMTCost = 0.0;
    var dSegmentCorrectionCostPlusMTCost = 0.0;
    var dCostSavings = 0.0;
    var dSavingPercent = 0.0;
    var dSegmentMTCost = 0.0;

    //Totals
    var dCombinedTotalEffortCorrectionSegments = 0.0;
    var dCombinedTotalEffortReductionSegments = 0.0;
    var dCombineddTotalSegmentMTCost = 0.0;
    var dCombinedTotalSegmentCostNoMT = 0.0;
    var dCombinedSegmentCorrectionCostPostMT = 0.0;
    var dCombinedCostSavings = 0.0;
    
    for(iRowIndex = 1;iRowIndex<=12;iRowIndex++)
    {
        //Get the number of segments 
        iSegmentCount = window.document.getElementById("SC" + iRowIndex).value;
        if (iRowIndex == 1)
        {
            //Set the skill level to Machine Translation
            iSkillLevelRequiredToCorrect = -1;
            //No effort to correct as row 1 is correct already.
            iEffortToCorrect = 0;
        }
        else
        {
            //Get the skill level
            iSkillLevelRequiredToCorrect = window.document.getElementById("SLRTC" + iRowIndex).selectedIndex;
            //Get the effort to correct
            iEffortToCorrect = window.document.getElementById("ETC" + iRowIndex).value;
        }

        //Set the skill cost per word based on skill level       
        switch (iSkillLevelRequiredToCorrect)
        {
            case -1:
                dSkillCostPerWord = dJPMachineTranslationCPW;
                break;
            case 0:
                dSkillCostPerWord = dJPTranslatorCPW + dJPHumanPostEditorCPW;
                break;
            case 1:
                dSkillCostPerWord = dJPMachineTranslationPostEditorCPW; 
                break;
        }
        
        //Make sure there were segments specified for thsi row
        if (iSegmentCount > 0)
        {
            dTotalEffortCorrectionSegments = iSegmentCount * (iEffortToCorrect / 100);
            dTotalEffortReductionSegments = iSegmentCount - dTotalEffortCorrectionSegments;
            iApproxWords = dAverageWordsPerSegment * iSegmentCount;
            //cost per word handled above
            dTotalSegmentMTCost = iSegmentCount * dJPMachineTranslationCPW * dAverageWordsPerSegment; //Cost for MT of all segments
            dTotalSegmentCostNoMT = (iApproxWords * dJPTranslatorCPW) + (iApproxWords * dJPHumanPostEditorCPW); //Cost if translated with human translator and post edited with human post editor
            dSegmentCorrectionCostPostMT = dTotalEffortCorrectionSegments * iApproxWords * dSkillCostPerWord; //cost to make corrections on MT  
            dSegmentCorrectionCostPlusMTCost = dTotalSegmentMTCost + dSegmentCorrectionCostPostMT;
            dCostSavings = dTotalSegmentCostNoMT - dSegmentCorrectionCostPlusMTCost;
            dSavingPercent = (100 / dTotalSegmentCostNoMT) * dCostSavings;

        }
        else
        {
            //No segments
            dTotalEffortCorrectionSegments = 0;
            dTotalEffortReductionSegments = 0;
            iApproxWords = 0;
            dTotalSegmentMTCost = 0;
            dTotalSegmentCostNoMT = 0;
            dSegmentCorrectionCostPostMT = 0;
            dSegmentCorrectionCostPlusMTCost = 0;
            dCostSavings = 0;            
            dSavingPercent = 0;
        }

        setInnerText("TES" + iRowIndex,CurrencyFormatted(Math.round(dTotalEffortCorrectionSegments*1000)/1000));
        setInnerText("TERS" + iRowIndex,CurrencyFormatted(Math.round(dTotalEffortReductionSegments*100)/100));
        setInnerText("AW" + iRowIndex,Math.round(iApproxWords*100)/100);
        setInnerText("SCPW" + iRowIndex,Math.round(dSkillCostPerWord*1000)/1000);
        setInnerText("TMTC" + iRowIndex,CurrencyFormatted(Math.round(dTotalSegmentMTCost*100)/100));
        setInnerText("TSCNMT" + iRowIndex,CurrencyFormatted(Math.round(dTotalSegmentCostNoMT*100)/100));
        setInnerText("SRCPMT" + iRowIndex,CurrencyFormatted(Math.round(dSegmentCorrectionCostPostMT*100)/100));
        setInnerText("TCMTC" + iRowIndex,CurrencyFormatted(Math.round(dSegmentCorrectionCostPlusMTCost*100)/100));
        setInnerText("CS" + iRowIndex,CurrencyFormatted(Math.round(dCostSavings*100)/100));
        setInnerText("SP" + iRowIndex,CurrencyFormatted((Math.round(dSavingPercent*100)/100)) + "%");
    
    
        //Increment combined totals
        dCombinedTotalEffortCorrectionSegments += dTotalEffortCorrectionSegments;
        dCombinedTotalEffortReductionSegments += dTotalEffortReductionSegments;
        dCombineddTotalSegmentMTCost += dTotalSegmentMTCost;
        dCombinedTotalSegmentCostNoMT += dTotalSegmentCostNoMT;
        dCombinedSegmentCorrectionCostPostMT += dSegmentCorrectionCostPostMT;
        dCombinedCostSavings += dCostSavings;
    }
    
    //Update combined totals on form
    setInnerText("totalSC",trimNumber(iTotalSegments));
    setInnerText("totalTES",CurrencyFormatted((Math.round(dCombinedTotalEffortCorrectionSegments*100)/100)));
    setInnerText("totalTERS",CurrencyFormatted((Math.round(dCombinedTotalEffortReductionSegments*100)/100)));
    setInnerText("totalMTC",CurrencyFormatted((Math.round(dCombineddTotalSegmentMTCost*100)/100)));
    setInnerText("totalTSCNMT",CurrencyFormatted((Math.round(dCombinedTotalSegmentCostNoMT*100)/100)));
    setInnerText("totalSRCPMT",CurrencyFormatted((Math.round(dCombinedSegmentCorrectionCostPostMT*100)/100)));
    setInnerText("totalTCMTC",CurrencyFormatted((Math.round((dCombinedSegmentCorrectionCostPostMT + dCombineddTotalSegmentMTCost)*100)/100)));    
    setInnerText("totalCS",CurrencyFormatted((Math.round(dCombinedCostSavings*100)/100)));
    setInnerText("totalSP",CurrencyFormatted((100.0-(Math.round(((100/dCombinedTotalSegmentCostNoMT)*(dCombinedSegmentCorrectionCostPostMT + dCombineddTotalSegmentMTCost))*100)/100))) + '%');














return 0;

    dErrorSegmentsPercentageOfTotal = Math.round(((100 / iTotalSegments) * iTotalErrorSegments)*100)/100;

    if (iTotalSegments == 0 | iJPTotalWords == 0)
    {
        bValidData=false;      
        setInnerText("summaryAWPS",'NO DATA');
        setInnerText("summaryESOT",'NO DATA');  
    }
    else
    {
        bValidData=true;      
        setInnerText("summaryAWPS",dAverageWordsPerSegment);
        setInnerText("summaryESOT",dErrorSegmentsPercentageOfTotal + "%");        
    }

    
    //Set the basic report values
    setInnerText("summaryTW",addCommas(iJPTotalWords)); 
    setInnerText("totalSC",addCommas(iTotalSegments));    
    setInnerText("summaryTSS",addCommas(iTotalSegments));    
    setInnerText("summaryTES",addCommas(iTotalErrorSegments));    
    
    //Translator
    dCostPerWord = dJPTranslatorCPW;
    iWordsPerDay = iJPTranslatorWPD;
    dCostPerDay = iJPTranslatorWPD * dCostPerWord;
    dTotalJobCost = iJPTotalWords * dCostPerWord;
    dTotalJobDays = iJPTotalWords / iWordsPerDay;
    setInnerText("TCPW",dCostPerWord);    
    setInnerText("TWPD",iWordsPerDay);    
    setInnerText("TCPD",dCostPerDay);     
    setInnerText("TTJC",Math.round(dTotalJobCost*100)/100);
    setInnerText("TTJD",Math.round(dTotalJobDays*100)/100);

/*
    //Post Editor
//    dCostPerWord = dJPPostEditorCPW;
//    iWordsPerDay = iJPPostEditorWPD;
//    dCostPerDay = iJPPostEditorWPD * dCostPerWord;
    dTotalJobCost = iJPTotalWords * dCostPerWord;
    dTotalJobDays = iJPTotalWords / iWordsPerDay;
    setInnerText("PECPW",dCostPerWord;    
    setInnerText("PEWPD",iWordsPerDay;    
    setInnerText("PECPD",dCostPerDay;     
    setInnerText("PETJC",Math.round(dTotalJobCost*100)/100;
    setInnerText("PETJD",Math.round(dTotalJobDays*100)/100;

    //Machine Translation
    dCostPerWord = dJPMachineTranslationCPW;
    iWordsPerDay = iJPMachineTranslationWPD;
    dCostPerDay = iJPMachineTranslationWPD * dCostPerWord;
    dTotalJobCost = iJPTotalWords * dCostPerWord;
    dTotalJobDays = iJPTotalWords / iWordsPerDay;
    setInnerText("MTCPW",dCostPerWord;    
    setInnerText("MTWPD",iWordsPerDay;    
    setInnerText("MTCPD",dCostPerDay;     
    setInnerText("MTTJC",Math.round(dTotalJobCost*100)/100;
    setInnerText("MTTJD",Math.round(dTotalJobDays*100)/100;


return 0;

    /////Skill Costs and Capability
    TCPW.innerText = "0.1"; //27. Translator Cost Per Word
    PECPW.innerText = "0.05"; //28. Post Edit Cost Per Word
    MTCPW.innerText = "0.01"; //29. Machine Translation Cost Per Word

    TWPD.innerText = "3,000"; //30. Translator Words Per Day
    PEWPD.innerText = "1,000"; //31. Post Edit Words Per Day
    MTWPD.innerText = CommaFormatted(toFixed(((1000 * 8) * 60), 2)); //32. Machine Translation Words Per Day

    //Segment Count
    var sc1 = setInnerText("SC1").value; //B1   
    setInnerText("SC1").value = trimNumber(sc1); //B1   
    var sc2 = setInnerText("SC2").value; //B2
    setInnerText("SC2").value = trimNumber(sc2); //B2
    var sc3 = setInnerText("SC3").value; //B3
    setInnerText("SC3").value = trimNumber(sc3); //B3
    var sc4 = setInnerText("SC4").value; //B4
    setInnerText("SC4").value = trimNumber(sc4); //B4
    var sc5 = setInnerText("SC5").value; //B5
    setInnerText("SC5").value = trimNumber(sc5); //B5
    var sc6 = setInnerText("SC6").value; //B6
    setInnerText("SC6").value = trimNumber(sc6); //B6
    var sc7 = setInnerText("SC7").value; //B7
    setInnerText("SC7").value = trimNumber(sc7); //B7    
    var sc8 = setInnerText("SC8").value; //B8
    setInnerText("SC8").value = trimNumber(sc8); //B8
    var sc9 = setInnerText("SC9").value; //B9
    setInnerText("SC9").value = trimNumber(sc9); //B9
    var sc10 = setInnerText("SC10").value; //B10
    setInnerText("SC10").value = trimNumber(sc10); //B10
    var sc11 = setInnerText("SC11").value; //B11
    setInnerText("SC11").value = trimNumber(sc11); //B11
    var sc12 = setInnerText("SC12").value; //B12
    setInnerText("SC12").value = trimNumber(sc12); //B12
    totalSC.innerText = addCommas(sum(sc1, sc2, sc3, sc4, sc5, sc6, sc7, sc8, sc9, sc10, sc11, sc12));
    //Effort to correct
    //var etc1 = setInnerText("ETC1").value; //C1
    //setInnerText("ETC1").value = trimNumber(etc1); //C1
    var etc2 = setInnerText("ETC2").value; //C2
    setInnerText("ETC2").value = trimNumber(etc2); //C2
    var etc3 = setInnerText("ETC3").value; //C3
    setInnerText("ETC3").value = trimNumber(etc3); //C3
    var etc4 = setInnerText("ETC4").value; //C4
    setInnerText("ETC4").value = trimNumber(etc4); //C4
    var etc5 = setInnerText("ETC5").value; //C5
    setInnerText("ETC5").value = trimNumber(etc5); //C5
    var etc6 = setInnerText("ETC6").value; //C9
    setInnerText("ETC6").value = trimNumber(etc6); //C9
    var etc7 = setInnerText("ETC7").value; //C7
    setInnerText("ETC7").value = trimNumber(etc7); //C7
    var etc8 = setInnerText("ETC8").value; //C8
    setInnerText("ETC8").value = trimNumber(etc8); //C8
    var etc9 = setInnerText("ETC9").value; //C9
    setInnerText("ETC9").value = trimNumber(etc9); //C9
    var etc10 = setInnerText("ETC10").value; //C10
    setInnerText("ETC10").value = trimNumber(etc10); //C10
    var etc11 = setInnerText("ETC11").value; //C11
    setInnerText("ETC11").value = trimNumber(etc11); //C11
    var etc12 = setInnerText("ETC12").value; //C12
    setInnerText("ETC12").value = trimNumber(etc12); //C12
    //Total effort segments
    //TES1.innerText = CommaFormatted(toFixed((sc1 * etc1), 2)); //D1
    TES2.innerText = CommaFormatted(toFixed((sc2 * etc2), 2)); //D2
    TES3.innerText = CommaFormatted(toFixed((sc3 * etc3), 2)); //D3
    TES4.innerText = CommaFormatted(toFixed((sc4 * etc4), 2)); //D4
    TES5.innerText = CommaFormatted(toFixed((sc5 * etc5), 2)); //D5
    TES6.innerText = CommaFormatted(toFixed((sc6 * etc6), 2)); //D6
    TES7.innerText = CommaFormatted(toFixed((sc7 * etc7), 2)); //D7
    TES8.innerText = CommaFormatted(toFixed((sc8 * etc8), 2)); //D8
    TES9.innerText = CommaFormatted(toFixed((sc9 * etc9), 2)); //D9
    TES10.innerText = CommaFormatted(toFixed((sc10 * etc10), 2)); //D10
    TES11.innerText = CommaFormatted(toFixed((sc11 * etc11), 2)); //D11
    TES12.innerText = CommaFormatted(toFixed((sc12 * etc12), 2)); //D12
    //Total effort reduction segments
    TERS1.innerText = "0.00";  //E1
    TERS2.innerText = CommaFormatted(toFixed((sc2 - replaceCommas(TES2.innerText)), 2)); //E2
    TERS3.innerText = CommaFormatted(toFixed((sc3 - replaceCommas(TES3.innerText)), 2)); //E3
    TERS4.innerText = CommaFormatted(toFixed((sc4 - replaceCommas(TES4.innerText)), 2)); //E4
    TERS5.innerText = CommaFormatted(toFixed((sc5 - replaceCommas(TES5.innerText)), 2)); //E5
    TERS6.innerText = CommaFormatted(toFixed((sc6 - replaceCommas(TES6.innerText)), 2)); //E6
    TERS7.innerText = CommaFormatted(toFixed((sc7 - replaceCommas(TES7.innerText)), 2)); //E7
    TERS8.innerText = CommaFormatted(toFixed((sc8 - replaceCommas(TES8.innerText)), 2)); //E8
    TERS9.innerText = CommaFormatted(toFixed((sc9 - replaceCommas(TES9.innerText)), 2)); //E9
    TERS10.innerText = CommaFormatted(toFixed((sc10 - replaceCommas(TES10.innerText)), 2)); //E10
    TERS11.innerText = CommaFormatted(toFixed((sc11 - replaceCommas(TES11.innerText)), 2)); //E11
    TERS12.innerText = CommaFormatted(toFixed((sc12 - replaceCommas(TES12.innerText)), 2)); //E12
    //Skill level required to correct     
    //Skill cost per word
    //SCPW1.innerText = CommaFormatted(toFixed(replaceCommas(TCPW.innerText), 2)); //G1
    SCPW2.innerText = CommaFormatted(toFixed(replaceCommas(TCPW.innerText), 2)); //G2
    SCPW3.innerText = CommaFormatted(toFixed(replaceCommas(TCPW.innerText), 2)); //G3
    SCPW4.innerText = CommaFormatted(toFixed(replaceCommas(PECPW.innerText), 2)); //G4
    SCPW5.innerText = CommaFormatted(toFixed(replaceCommas(PECPW.innerText), 2)); //G5
    SCPW6.innerText = CommaFormatted(toFixed(replaceCommas(PECPW.innerText), 2)); //G6
    SCPW7.innerText = CommaFormatted(toFixed(replaceCommas(PECPW.innerText), 2)); //G7
    SCPW8.innerText = CommaFormatted(toFixed(replaceCommas(PECPW.innerText), 2)); //G8
    SCPW9.innerText = CommaFormatted(toFixed(replaceCommas(PECPW.innerText), 2)); //G9
    SCPW10.innerText = CommaFormatted(toFixed(replaceCommas(PECPW.innerText), 2)); //G10
    SCPW11.innerText = CommaFormatted(toFixed(replaceCommas(PECPW.innerText), 2)); //G11
    SCPW12.innerText = CommaFormatted(toFixed(replaceCommas(PECPW.innerText), 2)); //G12

    summaryTSS.innerText = totalSC.innerText; //17. Total Sample Size (N/A)
    summaryTW.innerText = addCommas(totalWord); //18. Total Word
    //devide by zero
    //    summaryAWPS.innerText = addCommas(Math.round(replaceCommas(summaryTW.innerText) / replaceCommas(summaryTSS.innerText))); //19. Round(B18/B17, 0)
    summaryAWPS.innerText = addCommas(Math.round(replaceCommas(summaryTW.innerText) / 1)); //19. Round(B18/B17, 0)

    //Total segment cost(no MT)
    //TSCNMT1.innerText = CommaFormatted(toFixed(((replaceCommas(sc1) * replaceCommas(SCPW1.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H1
    TSCNMT2.innerText = CommaFormatted(toFixed(((sc2 * replaceCommas(SCPW2.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H2
    TSCNMT3.innerText = CommaFormatted(toFixed(((sc3 * replaceCommas(SCPW3.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H3
    TSCNMT4.innerText = CommaFormatted(toFixed(((sc4 * replaceCommas(SCPW4.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H4
    TSCNMT5.innerText = CommaFormatted(toFixed(((sc5 * replaceCommas(SCPW5.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H5
    TSCNMT6.innerText = CommaFormatted(toFixed(((sc6 * replaceCommas(SCPW6.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H6
    TSCNMT7.innerText = CommaFormatted(toFixed(((sc7 * replaceCommas(SCPW7.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H7
    TSCNMT8.innerText = CommaFormatted(toFixed(((sc8 * replaceCommas(SCPW8.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H8
    TSCNMT9.innerText = CommaFormatted(toFixed(((sc9 * replaceCommas(SCPW9.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H9
    TSCNMT10.innerText = CommaFormatted(toFixed(((sc10 * replaceCommas(SCPW10.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H10
    TSCNMT11.innerText = CommaFormatted(toFixed(((sc11 * replaceCommas(SCPW11.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H11
    TSCNMT12.innerText = CommaFormatted(toFixed(((sc12 * replaceCommas(SCPW12.innerText)) * replaceCommas(summaryAWPS.innerText)), 2)); //H12
    totTSCNMT.innerText = CommaFormatted(toFixed(sum(
    //replaceCommas(TSCNMT1.innerText),
        replaceCommas(TSCNMT2.innerText),
        replaceCommas(TSCNMT3.innerText),
        replaceCommas(TSCNMT4.innerText),
        replaceCommas(TSCNMT5.innerText),
        replaceCommas(TSCNMT6.innerText),
        replaceCommas(TSCNMT7.innerText),
        replaceCommas(TSCNMT8.innerText),
        replaceCommas(TSCNMT9.innerText),
        replaceCommas(TSCNMT10.innerText),
        replaceCommas(TSCNMT11.innerText),
        replaceCommas(TSCNMT12.innerText)), 2));  //H-TOTAL


    //Segment repair cost(post MT)
    cSLRTC2 = (SLRTC2.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    cSLRTC3 = (SLRTC3.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    cSLRTC4 = (SLRTC4.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    cSLRTC5 = (SLRTC5.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    cSLRTC6 = (SLRTC6.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    cSLRTC7 = (SLRTC7.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    cSLRTC8 = (SLRTC8.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    cSLRTC9 = (SLRTC9.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    cSLRTC10 = (SLRTC10.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    cSLRTC11 = (SLRTC11.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    cSLRTC12 = (SLRTC12.childNodes[0][0].selected == true) ? TCPW.innerText : PECPW.innerText;
    //SRCPMT1.innerText = "0.00"; //I1
    SRCPMT2.innerText = CommaFormatted(toFixed((cSLRTC2 * etc2), 2)); //I2
    SRCPMT3.innerText = CommaFormatted(toFixed((cSLRTC3 * etc3), 2)); //I3
    SRCPMT4.innerText = CommaFormatted(toFixed((cSLRTC4 * etc4), 2)); //I4
    SRCPMT5.innerText = CommaFormatted(toFixed((cSLRTC5 * etc5), 2)); //I5
    SRCPMT6.innerText = CommaFormatted(toFixed((cSLRTC6 * etc6), 2)); //I6
    SRCPMT7.innerText = CommaFormatted(toFixed((cSLRTC7 * etc7), 2)); //I7
    SRCPMT8.innerText = CommaFormatted(toFixed((cSLRTC8 * etc8), 2)); //I8
    SRCPMT9.innerText = CommaFormatted(toFixed((cSLRTC9 * etc9), 2)); //I9
    SRCPMT10.innerText = CommaFormatted(toFixed((cSLRTC10 * etc10), 2)); //I10
    SRCPMT11.innerText = CommaFormatted(toFixed((cSLRTC11 * etc11), 2)); //I11
    SRCPMT12.innerText = CommaFormatted(toFixed((cSLRTC12 * etc12), 2)); //I12
    totSRCPMT.innerText = CommaFormatted(toFixed(sum(cSLRTC2, cSLRTC3, cSLRTC4, cSLRTC5, cSLRTC6, cSLRTC7, cSLRTC8, cSLRTC9, cSLRTC10, cSLRTC11, cSLRTC12), 2)); //I-TOTAL

    //Cost savings
    CS1.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc1)), 2)); //J1
    CS2.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc2)), 2)); //J2
    CS3.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc3)), 2)); //J3
    CS4.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc4)), 2)); //J4
    CS5.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc5)), 2)); //J5
    CS6.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc6)), 2)); //J6
    CS7.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc7)), 2)); //J7
    CS8.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc8)), 2)); //J8
    CS9.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc9)), 2)); //J9
    CS10.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc10)), 2)); //J10
    CS11.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc11)), 2)); //J11
    CS12.innerText = CommaFormatted(toFixed((replaceCommas(translatorCPW) * replaceCommas(sc12)), 2)); //J12



    //    //CS1.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT1.innerText) - replaceCommas(SRCPMT1.innerText)), 2)); //J1
    //    CS2.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT2.innerText) - replaceCommas(SRCPMT2.innerText)), 2)); //J2
    //    CS2.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT3.innerText) - replaceCommas(SRCPMT3.innerText)), 2)); //J3
    //    CS4.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT4.innerText) - replaceCommas(SRCPMT4.innerText)), 2)); //J4
    //    CS5.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT5.innerText) - replaceCommas(SRCPMT5.innerText)), 2)); //J5
    //    CS6.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT6.innerText) - replaceCommas(SRCPMT6.innerText)), 2)); //J6
    //    CS7.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT7.innerText) - replaceCommas(SRCPMT7.innerText)), 2)); //J7
    //    CS8.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT8.innerText) - replaceCommas(SRCPMT8.innerText)), 2)); //J8
    //    CS9.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT9.innerText) - replaceCommas(SRCPMT9.innerText)), 2)); //J9
    //    CS10.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT10.innerText) - replaceCommas(SRCPMT10.innerText)), 2)); //J10
    //    CS11.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT11.innerText) - replaceCommas(SRCPMT11.innerText)), 2)); //J11
    //    CS12.innerText = CommaFormatted(toFixed((replaceCommas(TSCNMT12.innerText) - replaceCommas(SRCPMT12.innerText)), 2)); //J12
    //    totCS.innerText = CommaFormatted(toFixed((replaceCommas(totTSCNMT.innerText) - replaceCommas(totSRCPMT.innerText)), 2)); //J-TOTAL
    //Saving percent (N/A)
    SP1.innerText = "0" + "%"; //K1
    SP2.innerText = "0" + "%"; //K2
    SP3.innerText = "0" + "%"; //K3
    SP4.innerText = "0" + "%"; //K4
    SP5.innerText = "0" + "%"; //K5
    SP6.innerText = "0" + "%"; //K6
    SP7.innerText = "0" + "%"; //K7
    SP8.innerText = "0" + "%"; //K8
    SP9.innerText = "0" + "%"; //K9
    SP10.innerText = "0" + "%"; //K10
    SP11.innerText = "0" + "%"; //K11
    SP12.innerText = "0" + "%"; //K12
    totSP.innerText = "0" + "%"; //K-TOTAL

    //20. Total Error Segments
    summaryTES.innerText = CommaFormatted(toFixed(sum(sc2, sc3, sc4, sc5, sc6, sc7, sc8, sc9, sc10, sc11), 2)); //sum(B3:B12)

    //21. Error Segments % of Total (devide by zero)
    //summaryESOT.innerText = CommaFormatted(toFixed((100 / replaceCommas(summaryTSS.innerText)) * (replaceCommas(summaryTES.innerText) / 100), 2)) + "%";
    summaryESOT.innerText = CommaFormatted(toFixed((100 / 1) * (replaceCommas(summaryTES.innerText) / 100), 2)) + "%";

    //22. Total Effort to Fix Errors
    summaryTETFE.innerText = CommaFormatted(toFixed(sum(
        replaceCommas(TES2.innerText),
        replaceCommas(TES3.innerText),
        replaceCommas(TES4.innerText),
        replaceCommas(TES5.innerText),
        replaceCommas(TES6.innerText),
        replaceCommas(TES7.innerText),
        replaceCommas(TES8.innerText),
        replaceCommas(TES9.innerText),
        replaceCommas(TES10.innerText),
        replaceCommas(TES11.innerText)), 2));

    //23. Total Effort Reduction
    summaryTER.innerText = CommaFormatted(toFixed(sum(
        replaceCommas(TERS1.innerText),
        replaceCommas(TERS2.innerText),
        replaceCommas(TERS3.innerText),
        replaceCommas(TERS4.innerText),
        replaceCommas(TERS5.innerText),
        replaceCommas(TERS6.innerText),
        replaceCommas(TERS7.innerText),
        replaceCommas(TERS8.innerText),
        replaceCommas(TERS9.innerText),
        replaceCommas(TERS10.innerText),
        replaceCommas(TERS11.innerText)), 2));

    //24. Total Effort Reduction % (devide by zero)
    //summaryTERP.innerText = CommaFormatted(toFixed((100 / replaceCommas(summaryTSS.innerText)) * (replaceCommas(summaryTER.innerText) / 100), 2)) + "%";
    summaryTERP.innerText = CommaFormatted(toFixed((100 / 1) * (replaceCommas(summaryTER.innerText) / 100), 2)) + "%";

    //Cost Per Day
    TCPD.innerText = CommaFormatted(toFixed((replaceCommas(TCPW.innerText) * replaceCommas(TWPD.innerText)), 2)); //Translator Cost Per Day
    PECPD.innerText = CommaFormatted(toFixed((replaceCommas(PECPW.innerText) * replaceCommas(PEWPD.innerText)), 2));  //Post Edit Cost Per Day
    MTCPD.innerText = CommaFormatted(toFixed((replaceCommas(MTCPW.innerText) * replaceCommas(MTWPD.innerText)), 2));  //Machine Translation Cost Per Day
    //Total Job Cost 
    TTJC.innerText = CommaFormatted(toFixed((replaceCommas(TCPD.innerText) * totalWord), 2)); //Translator Total Job Cost
    PETJC.innerText = CommaFormatted(toFixed((replaceCommas(PECPD.innerText) * totalWord), 2)); //Post Edit Total Job Cost
    MTTJC.innerText = CommaFormatted(toFixed((replaceCommas(MTCPD.innerText) * totalWord), 2)); //Machine Translation Total Job Cost
    //Total Job Days
    TTJD.innerText = CommaFormatted(toFixed((totalWord / replaceCommas(TWPD.innerText)), 2)); //Translator Total Job Days
    PETJD.innerText = CommaFormatted(toFixed((totalWord / replaceCommas(PECPD.innerText)), 2)); //Post Edit Total Job Days
    MTTJD.innerText = CommaFormatted(toFixed((totalWord / replaceCommas(MTCPD.innerText)), 2)); //Machine Translation Total Job Days

    /////Job Parameters
    //34. Without Machine Translation
    //--Translator
    womtTCost.innerText = CommaFormatted(toFixed((replaceCommas(summaryTW.innerText) * replaceCommas(TCPW.innerText)), 2)); //35.
    womtPECost.innerText = CommaFormatted(toFixed((replaceCommas(PECPW.innerText) * replaceCommas(summaryTW.innerText)), 2)); //36.
    womtTotCost.innerText = CommaFormatted(toFixed(sum(replaceCommas(womtTCost.innerText), replaceCommas(womtPECost.innerText)), 2)); //37.

    womtTDays.innerText = CommaFormatted(toFixed((replaceCommas(summaryTW.innerText) / replaceCommas(TWPD.innerText)), 2)); //39.
    womtPEDays.innerText = CommaFormatted(toFixed((replaceCommas(summaryTW.innerText) / replaceCommas(PEWPD.innerText)), 2)); //40.
    womtTotDays.innerText = CommaFormatted(toFixed(sum(replaceCommas(womtTDays.innerText), replaceCommas(womtPEDays.innerText)), 2)); //41.

    //43. With Machine Translation
    wmtMTCost.innerText = CommaFormatted(toFixed((replaceCommas(summaryTW.innerText) * replaceCommas(MTCPW.innerText)), 2)); //44.
    wmtTCost.innerText = CommaFormatted(toFixed(sum(replaceCommas(SRCPMT2.innerText), replaceCommas(SRCPMT3.innerText)), 2)); //45.
    wmtPECost.innerText = CommaFormatted(toFixed(sum(replaceCommas(SRCPMT5.innerText), replaceCommas(SRCPMT6.innerText), replaceCommas(SRCPMT7.innerText), replaceCommas(SRCPMT8.innerText), replaceCommas(SRCPMT9.innerText), replaceCommas(SRCPMT10.innerText), replaceCommas(SRCPMT11.innerText)), 2));  //46.
    wmtTotCost.innerText = CommaFormatted(toFixed(sum(replaceCommas(wmtMTCost.innerText), replaceCommas(wmtTCost.innerText), replaceCommas(wmtPECost.innerText)), 2)); //47.
    wmtMTDays.innerText = CommaFormatted(toFixed((replaceCommas(summaryTW.innerText) / replaceCommas(MTWPD.innerText)), 2)); //49.
    //wmtTDays
    wmtTDays.innerText = CommaFormatted(toFixed(((sum(sc3, sc4) * replaceCommas(summaryAWPS.innerText)) / replaceCommas(TWPD.innerText)), 2)); //50.    
    wmtPEDays.innerText = CommaFormatted(toFixed((replaceCommas(summaryTW.innerText) / replaceCommas(PEWPD.innerText)), 2)); //51.
    wmtTotDays.innerText = CommaFormatted(toFixed(sum(replaceCommas(wmtMTDays.innerText), replaceCommas(wmtTDays.innerText), replaceCommas(wmtPEDays.innerText)), 2)); //52.

    //////Cost and Time Savings 
    //54. Savings
    catsCS.innerText = CommaFormatted(toFixed((replaceCommas(womtTotCost.innerText) - replaceCommas(wmtTotCost.innerText)), 2)); //56.
    catsCSP.innerText = CommaFormatted(toFixed(((100 / replaceCommas(womtTotCost.innerText)) * (replaceCommas(catsCS.innerText) / 100)), 2)) + "%"; //57.
    catsTS.innerText = CommaFormatted(toFixed((replaceCommas(womtTotDays.innerText) - replaceCommas(wmtTotDays.innerText)), 2)); //58.
    catsTSP.innerText = CommaFormatted(toFixed((1 - (100 / replaceCommas(womtTotDays.innerText)) * (replaceCommas(wmtTotDays.innerText) / 100)), 2)) + "%"; //59.
*/
}

function sum() {
    n = sum.arguments.length;
    total = 0;
    for (i = 0; i < n; ++i) {
        if (!isNaN(parseFloat(sum.arguments[i]))) {
            total += parseFloat(sum.arguments[i]);
        }
    }

    return total;
}

function addCommas(number) {
    number = '' + number;
    if (number.length > 3) {
        var mod = number.length % 3;
        var output = (mod > 0 ? (number.substring(0, mod)) : '');
        for (i = 0; i < Math.floor(number.length / 3); i++) {
            if ((mod == 0) && (i == 0))
                output += number.substring(mod + 3 * i, mod + 3 * i + 3);
            else
                output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
        }
        return (output);
    }
    else return number;
}

function toFixed(num, pre) {
    if (!isNaN(num)) {
        num = num.toString().replace(/\,/g, '');

        num *= Math.pow(10, pre);
        num = (Math.round(num, pre) + (((num - Math.round(num, pre)) >= 0.5) ? 1 : 0)) / Math.pow(10, pre);

        return num.toFixed(pre);
    }
}

function findPos(searchIn, searchFor) {
    var pos = searchIn.indexOf(searchFor);
    return pos;
}

function CurrencyFormatted(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

function CommaFormatted(amount) {
    //TODO: get this working properly
    return amount;
    
    
    var delimiter = ","; // replace comma if desired
    var aa = amount.split('.', 2)
    var d = aa[1];
    var i = parseInt(aa[0]);
    if (isNaN(i)) { return ''; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while (n.length > 3) {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if (d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;

    return amount;
}

function replaceCommas(val) {
    return val.replace(/\,/g, '');
}


function removeZeros(val) {
    var regEx1 = /^[0]+/;
    var regEx2 = /[0]+$/;
    var regEx3 = /[.]$/;

    var before = '';
    var after = '';

    before = val;

    after = before.replace(regEx1, '');  // Remove leading 0's
    if (after.indexOf('.') > -1) {
        after = after.replace(regEx2, '');  // Remove trailing 0's
    }
    after = after.replace(regEx3, '');  // Remove trailing decimal

    return after;
}

function trimNumber1(s) {
    while (s.substr(0, 1) == '0' && s.length > 1) { s = s.substr(1, 9999); }
    return s;
}

function trimNumber2(s) {
    return s.replace(/^0+/, '');
}

function trimNumber(s) {
    return s * 1;
}

function zeroPadding(num, count) {
    var numZeropad = num + '';
    while (numZeropad.length < count) {

        numZeropad = "0" + numZeropad;
    }
    return numZeropad;
}

function countWords(sText)
{
    //this function clears out the tabs and carrage returns, replacing them with spaces. Then counts the spaces to give a total number of words.
    sText = sText + ' ';
    sText = sText.replace('/n','/r');
    sText.replace('/t',' ');
    while(sText.indexOf('/r ') > -1)
    {
        sText = sText.replace('/r ','/r');
    }
    while(sText.indexOf(' /r') > -1)
    {
        sText = sText.replace(' /r','/r');
    }
    while(sText.indexOf('/r/r') > -1)
    {
        sText = sText.replace('/r/r','/r');
    }
    for (i = 0; i < 10; i++) 
    {
        sText = sText.replace(i,' ');
    }        
    while(sText.indexOf('  ') > -1)
    {
        sText = sText.replace('  ',' ');
    }
    sText = sText.replace('/r',' ');
    //Could make this better by removing punctuation, but this is close enough for are pretty accurate word count
    //Data is now ready count the number of spaces
    //There is a faster way than this I am sure, but I am on a plane and dont have access to a javascript manual :)
    var iWordCount = 0;
    while(sText.indexOf(' ') > -1)
    {
        iWordCount++;
        sText = sText.substr(sText.indexOf(' ')+1);
    }
}

bCalcScriptLoaded = true;
