/// 
/// 
/// see QuantityManager, QuantityBox, RegistrantBox for interaction with updateTotal()
/// 
/// TODO: add message when user selects '0' for qty: [ You have selected 0 registrants for this class and it will be removed from your cart. ]
/// 

var OLD_BROWSER_MESSAGE = "Your browser is out of date and should be updated. If you feel you have reached this message in error, please phone the Yoga Center.";

function getCourseIndex(doubleUnderscoreId)
{
    var arr = doubleUnderscoreId.split("__");
    var len = arr.length;
    
    if (len < 2)
        return "";
    else
        return arr[len - 1];
}

function getCourseId(doubleUnderscoreId)
{
    var arr = doubleUnderscoreId.split("__");
    var len = arr.length;
    
    if (len < 2)
        return "";
    else
        return arr[0];
}

function getCourseFee(courseId)
{
    var span = document.getElementById(courseId + "__fee");
    var fee = parseFloat(span.innerHTML);
    return fee;
}

/// 
/// returns an array of Course objects
/// 
function getCourses()
{
    ///
    /// iterate through all rows in the page and create course objects
    /// 
    /// 
    /// 
    
    /// 
    /// get all quantity rows
    /// tr: [class_id]__registrant__row__[index]
    /// 
    /// 
    var rows = document.getElementsByTagName("TR");
    var courses = new Array();
    for (var i=0; i<rows.length; i++)
    {
        if (
            rows[i].id != null && 
            rows[i].id != "" && 
            rows[i].id.indexOf("__registrant__row__") != -1
            )
        {
            var row = rows[i];
            var rowId = row.id;
            var index = getCourseIndex(rowId);
            var courseId = getCourseId(rowId);
            var fee = getCourseFee(courseId);
            var course = new Course(row, courseId, index, fee);
            
            courses[courses.length] = course;
        }
    }
    
    return courses;
}

function showHideRegistrantRows()
{
    ///
    /// show all rows less than or equal to the quantity
    /// 
    /// SELECT ID: [course_id]__course__quantity
    /// 
    /// 
    
    var selects = document.getElementsByTagName("SELECT");
    
    for (var i=0; i<selects.length; i++)
    {
        var select = selects[i];
        var id = select.id;
        
        if (id.indexOf("__course__quantity") != -1)
        {
            var quantity = select[select.selectedIndex].value;
            var courseId = getCourseId(id);
            
            for (var j=1; j<10; j++)
            {
                var row = document.getElementById(courseId + "__registrant__row__" + j);
                if (j <= quantity)
                {
                    row.style.display = "block";
                }
                else
                {
                    row.style.display = "none";
                }
            }
        }
    }
}

function updateTotal(loseFocus)
{
    try
    {
        /// 
        /// show or hide the registrant rows
        /// 
        /// 
        showHideRegistrantRows();
        
        
        /// 
        /// calculate subtotal, discount, total
        /// 
        /// 
        
        var subtotal = 0.0;
        var discount = 0.0;
        
        var courses = getCourses();
        for (var i=0; i<courses.length; i++)
        {
            var course = courses[i];
            if (course.IsVisible())
            {
                subtotal += course.Fee();
                discount += course.Discount();
            }
        }
        
        var total = subtotal - discount;
        //var tax = total * 0.09;
        var tax = 0;
        total = total + tax;
        
        /// 
        /// show or hide the discount row
        /// 
        /// 
        var discountRow  = document.getElementById("discountRow");
        if (discount > 0)
            discountRow.style.display = 'block';
        else
            discountRow.style.display = 'none';
        
        
        /// 
        /// update subtotal, discount and total
        /// 
        /// 
        var subtotalSpan = document.getElementById("subtotalSpan");
        var discountSpan = document.getElementById("discountSpan");
        var taxSpan      = document.getElementById("taxSpan");
        var totalSpan    = document.getElementById("totalSpan");
        
        subtotalSpan.innerHTML = moneyFormat(subtotal);
        discountSpan.innerHTML = moneyFormat(discount);
        taxSpan.innerHTML      = moneyFormat(tax);
        totalSpan.innerHTML    = moneyFormat(total);
        
        /*
            fee_span.innerHTML = sub_total + ".00";
            total += parseInt(sub_total);
            totalSpan.innerHTML = total + ".00";
        */
    }
    catch(e)
    {
        alert(OLD_BROWSER_MESSAGE + e.message);
    }
    
    /// 
    /// attempt to blur the quantity select, or
    /// when user uses scroll wheel, the select 
    /// will spin. note that blur() does not work
    /// 
    if (loseFocus)
    {
        try
        {
            courses[0].RegistrantNameTextBox().focus();
        }
        catch(e)
        {
        }
    }
}

function setDiscount()
{
}