

// Where in the form do they start
start_elems = 0;
// How many elements should we check
num_elems   = 30;

// How many elements in the form are active...
total_n = 0;

// Debugging
function showElements(theForm)
{
    str = "Form Elements of form " + theForm.name + ": \n";
    for(i = 0; i < theForm.length; i++)
    {
        str += i + ". " + theForm.elements[i].name + " = " + theForm.elements[i].value + "\n";
    }
    alert(str)
}

function doReset(theForm, start, end, setto)
{
    if(setto == null)
      setto = '';

    for(i=start; i < end; i++)
    {
        theForm.elements[i].value = setto;
    }
}

function calcLimit(xmean, standev, limit){
    xmean = parseFloat(document.control1.xmean.value);
    standev = parseFloat(document.control1.standev.value);
    limit = parseFloat(document.control1.limit.value);
    lower = xmean - (limit*standev)
    upper = xmean + (limit*standev)
    lowersub = 100*lower
    uppersub = 100*upper
    lowertot = Math.round(lowersub)
    uppertot = Math.round(uppersub)
    lowergtot = lowertot/100
    uppergtot = uppertot/100
    document.control1.LowerLimit.value=lowergtot
    document.control1.UpperLimit.value=uppergtot
}

function loadDefaults(CALCMEAN)
{
    num = 20;
    defaults = [100, 108, 104, 101, 97, 100, 98, 103, 106, 98, 107, 92, 
                105, 95, 107, 98, 100, 95, 102, 95];
	for (i = 0; i < num; i++)
	{
		document.CALCMEAN.elements[i].value = defaults[i];
	}
	return true;
}


function calcMean(CALCMEAN)
{
    total_n = 0;
    sub_total = 0.0;
    // Run through all the input, add those that have valid values
    for(i = start_elems; i < num_elems; i++)
    {
        if(parseFloat(document.CALCMEAN.elements[i].value) >= 0)
        {
            sub_total += parseFloat(document.CALCMEAN.elements[i].value);
            total_n++;
        }
    }
    // Do the math
    x = sub_total / total_n;
    y = Math.round(100*x)
    z = y/100
    // Save the values
    document.CALCMEAN.mean.value=z
    document.CALCMEAN.hmean.value=z
    document.control1.xmean.value=z
}

function calcStanDev()
{
    total_n = 0;
    sub_total = 0.0;

    // Need to get the mean
    for(i = start_elems; i < num_elems; i++)
    {
        if(parseFloat(document.CALCMEAN.elements[i].value) >= 0)
        {
            sub_total += parseFloat(document.CALCMEAN.elements[i].value);
            total_n++;
        }
    }
    x = sub_total / total_n;

    // Now the the SD
    xi = 0.0;
    for(i = start_elems; i < num_elems; i++)
    {
        if(parseFloat(document.CALCMEAN.elements[i].value) >= 0)
        {
            xi += Math.pow( (document.CALCMEAN.elements[i].value - x), 2);
        }
    }
    sub = xi/(total_n - 1);
    total = 100*Math.sqrt(sub);
    gtotal = Math.round(total);
    grandtotal = gtotal/100;

    // Save the values
    document.CALCMEAN.StanDev.value=grandtotal
    document.CALCMEAN.hStanDev.value=grandtotal
    document.control1.standev.value=grandtotal
}

function calcCV(){
    total_n   = 0;
    sub_total = 0.0;
    xi        = 0.0;
    // Need to get the mean
    for(i = start_elems; i < num_elems; i++)
    {
        if(parseFloat(document.CALCMEAN.elements[i].value) >= 0)
        {
            sub_total += parseFloat(document.CALCMEAN.elements[i].value);
            // Keep track of x^2 also
            xi        += Math.pow(document.CALCMEAN.elements[i].value, 2);
            total_n++;
        }
    }
    mean = sub_total / total_n;
    sum = sub_total;

    xii = Math.pow(sum,2);
    n = total_n;
    sub = (n*xi - xii)/(n * (n-1))
    standev = Math.sqrt(sub)
    cv = (standev/mean)*100
        cvsub = Math.round(cv)
    cvfinal = cvsub/100

    // Save the values
    document.Cumulative.n2.value=n
    document.Cumulative.si2.value=xi
    document.Cumulative.s2.value=sum
    document.CALCMEAN.hCV.value=cvfinal
    document.CALCMEAN.CV.value=cvfinal
}

function getTotal(){
    n1 = parseFloat(document.Cumulative.n1.value);
    n2 = parseFloat(document.Cumulative.n2.value);
    s1 = parseFloat(document.Cumulative.s1.value);
    s2 = parseFloat(document.Cumulative.s2.value);
    si1 = parseFloat(document.Cumulative.si1.value);
    si2 = parseFloat(document.Cumulative.si2.value);
    n3 = n1 + n2;
    s3 = s1 + s2;
    si3 = si1 + si2;

    // Save the values
    document.Cumulative.hn3.value=n3;
    document.Cumulative.hs3.value=s3;
    document.Cumulative.hsi3.value=si3;
    document.Cumulative.n3.value=n3;
    document.Cumulative.s3.value=s3;
    document.Cumulative.si3.value=si3;
}

function calcCumStanDev2(){
    getTotal()
    n3  = parseFloat(document.Cumulative.n3.value);
    s3  = parseFloat(document.Cumulative.s3.value);
    si3 = parseFloat(document.Cumulative.si3.value);
    s4  = Math.pow(s3,2);
    sb  = (n3*si3) - s4
    sb2 = n3*(n3-1);
    sub = sb/sb2;
    subsub     = Math.sqrt(sub);
    subtotal   = Math.round(100*subsub);
    grandtotal = subtotal/100;

    // Save the values
    document.Cumulative.hn3.value=n3;
    document.Cumulative.hs3.value=s3;
    document.Cumulative.hsi3.value=si3;
    document.Cumulative.n3.value=n3;
    document.Cumulative.s3.value=s3;
    document.Cumulative.si3.value=si3;

    document.Cumulative.hcumstandev.value=grandtotal
    document.Cumulative.cumstandev.value=grandtotal

    document.control1.standev.value=grandtotal
}

function calcCumMean(){
    getTotal()
    n3 = parseFloat(document.Cumulative.n3.value);
    s3 = parseFloat(document.Cumulative.s3.value);
    si3 = parseFloat(document.Cumulative.si3.value);
    sub = s3/n3
    subsub = sub*100
    subtotal = Math.round(subsub)
    total = subtotal/100

    document.Cumulative.hn3.value=n3;
    document.Cumulative.hs3.value=s3;
    document.Cumulative.hsi3.value=si3;
    document.Cumulative.hcummean.value=total;

    document.Cumulative.n3.value=n3;
    document.Cumulative.s3.value=s3;
    document.Cumulative.si3.value=si3;
    document.Cumulative.cummean.value=total;

    document.control1.xmean.value=total
}

function calcCumCV(){
    getTotal();
    calcCumMean();
    calcCumStanDev2();
    s = parseFloat(document.Cumulative.cumstandev.value);
    x = parseFloat(document.Cumulative.cummean.value);
    sub = s/x
    subsub = sub*100*100
    subtotal = Math.round(subsub);
    total = subtotal/100;

    document.Cumulative.hcumcv.value=total;
    document.Cumulative.cumcv.value=total;
}

function calcBean(Formone, Formtwo){
    o = parseFloat(document.calcexp.Formone.value);
    t = parseFloat(document.calcexp.Formtwo.value);
    y = (o+t)/2;
    document.calcexp.mean.value=y
}


