// This script is a modified version of an original script found here:
// http://www.jeffstateonline.com/advising_center/advising_online/gpacalc.htm

// --- this function formats a GPA in form like x.xxx
function formatGPA(myVal)
{
    s = new String(myVal)
    if (s.indexOf(".") < 0)  s = s + ".";
    if (s.charAt(0) == '.')  s = "0" + s;
    s = s + "000";
    return s.substring(0, 5);
}

function clearform()
{
    document.getElementById("priorHours").value = "";
    document.getElementById("priorGPA").value = "";
    
    // --- check for valid grades and credit hours 
    for (var x = 1; x < 7; x++)
    {
        document.getElementById("ccGrade" + x).value = "";
        document.getElementById("ccHours" + x).value = "";
    }
    
    return 0;
}

// --- this function computes the GPA and displays the results...
function gpacalc()
{
    grades = eval({'A':4,'B':3,'C':2,'D':1,'F':0});
    allgr = 0;
    allcr = 0;
    gpa = 0;
    
    qhr = document.getElementById("priorHours").value
    qpt = document.getElementById("priorGPA").value
    
    // --- check for valid grades and credit hours 
    for (var x = 1; x < 7; x++)
    {
        tempGrade = document.getElementById("ccGrade" + x).value;
        tempHours = document.getElementById("ccHours" + x).value;
        
        if (tempGrade == "" && tempHours != "")
        {
            alert("Course " + x + " does not have a grade.");
            return 0;
        }
        
        if (tempGrade != "" && tempHours == "")
        {
            alert("Course " + x + " does not have credit hours.");
            return 0;
        }
        
        if (tempGrade != "" && tempHours != "")
        {
            if (isNaN(parseFloat(tempHours)))
            {
                alert("Course " + x + " does not have valid credit hours."); 
                return 0;
            }
            
            if (grades[tempGrade.toUpperCase()] != null)
            {
                allgr = allgr + (parseFloat(tempHours,10) * grades[tempGrade.toUpperCase()]);
                allcr = allcr + parseFloat(tempHours,10);
            }
            else
            {
                alert("You must use a grade of A, B, C, D, or F for Class " + x + ".\n\nCheck for extra spaces before or after the grade.\n\nNote that courses completed with a grade of P are not figured into the GPA.");
                return 0;
            }
        }    
    }

    if (qhr == 0 && qpt > 0)
    {
        alert("An invalid value for Quality Hours was specified.");
        return 0;
    }

    if (isNaN(parseFloat(qhr)) || isNaN(parseFloat(qpt)))
    {
        alert("Please specify values for prior Quality Hours and Quality Points.");
        return 0;
    }
    
    if (qhr < 0 || qpt < 0 || qpt > (4.0 * qhr))
    {
        alert("An invalid combination of Quality Hours and Quality Points was specified.");
        return 0;
    }
    
    if (allcr > 0)
    {
        gpa =  allgr / allcr;
    }
    
    i1 = parseFloat(qpt*qhr) + allgr;
    
    i2 = parseFloat(qhr) + allcr;
    
    tot =  i1/i2;
    prior = 0;
    
    if (qhr > 0)
    {
        prior = qpt;
    }
    
    alert("Prior GPA = " + formatGPA(prior) +  "\nCurrent Term = " + formatGPA(gpa) + "\nNew GPA = " + formatGPA(tot) );
    return 0;
}