
/******  Global Vars ******/ 
var subjectCode;
var tbCode;


/****** Initial Call and Delay Functions ******/    
function GetTextbooksBySubject(subject)
{
    $('stepThree').style.display = 'none';
    $('stepTwo').style.display = 'none';
    
    if (subject != '')
    {
        subjectCode = subject;
        $('SubjectSpinner').style.display = 'inline';
        
        setTimeout('DoTBLookup();', 0);
    }
}

function GetTextbookSections(textbookCode)
{
    $('stepThree').style.display = 'none';

    if (textbookCode != '')
    {
        tbCode = textbookCode;
        
        // Show spinner
        $('TextbookSpinner').style.display = 'inline';
        
        setTimeout('DoTSLookup();', 0);
    }
}


/****** Ajax Calls ******/ 
function DoTBLookup()
{
    // Do Ajax call
    TextbookAlignment.GetTextbooksAsListItemsBySubject(subjectCode, GetTextbooksAsListItemsBySubject_callback);
}

function DoTSLookup()
{
    // Do Ajax call
    TextbookAlignment.GetChapterUnitAsListItems(tbCode, GetChapterUnitAsListItems_callback);
}


/****** Callbacks ******/
function GetTextbooksAsListItemsBySubject_callback(result)
{
    // Update
    Try.these(
        function() {
            createElementsByElementCall('TextbooksDDL', result, 'Code', 'Title');
            return true;
        }, 
        function() {
            createElementsByInnerHtml('TextbooksDDL', result, 'Code', 'Title');
        }
     );
    
    // Hide Spinner
    $('SubjectSpinner').style.display = 'none';
    $('stepTwo').style.display = 'block';
}

function GetChapterUnitAsListItems_callback(result)
{   
    // Update
    Try.these(
        function() {
            createElementsByElementCall('TextbookSectionDDL', result, 'ID', 'Description');
            return true;
        }, 
        function() {
            createElementsByInnerHtml('TextbookSectionDDL', result, 'ID', 'Description');
        }
     );
     
    // Hide Spinner
    $('TextbookSpinner').style.display = 'none';
    $('stepThree').style.display = 'block';
}
   
/******  Create Element Functions  ******/
   
// Updates via creating option elements
function createElementsByElementCall(dropDownListName, result, valueName, displayName)
{
    Element.update(dropDownListName, '');

    // Create the data table           
    var dt = result.value;

    // Loop through the rows, create the options
    if (dt != null && typeof(dt) == 'object' && dt.Rows != null)
    {
        for (var i = 0; i < dt.Rows.length; i++)
        {
            var opt = document.createElement('option');
            opt.text = dt.Rows[i][displayName];
            opt.value = dt.Rows[i][valueName];
                     
            $(dropDownListName).options.add(opt);
        }
    }            
    
    // Add the select
    var opt = document.createElement('option');
    opt.text = 'Select an Option';
    opt.value = '';
    opt.selected = 'true';
    $(dropDownListName).options.add(opt, 0);
}

// Updates via creating and appending innerHTML
function createElementsByInnerHtml(dropDownListName, result, valueName, displayName)
{
    Element.update(dropDownListName, '');

    // Create the data table           
    var dt = result.value;
    var newInnerHtml = '';

    // Loop through the rows, create the options
    if (dt != null && typeof(dt) == 'object' && dt.Rows != null)
    {
        for (var i = 0; i < dt.Rows.length; i++)
        {
            newInnerHtml += '<option value="' + dt.Rows[i][valueName] + '">' + dt.Rows[i][displayName] + '</option>';
        }
    }            
    
    // Add the select
    newInnerHtml = '<option value="" selected="true">Select an Option</option>' + newInnerHtml;
    Element.update(dropDownListName, newInnerHtml);
}

/****** Do the search ******/
function doTextbookSearch()
{
    var url = '/SearchByTextbook.aspx?q=1';
    url += '&tbcode=' + tbCode;
    
    if ($F('TextbookSectionDDL') != '')
        url += '&tsid=' + $F('TextbookSectionDDL');
       
    window.location.href = url;
}

function doODTextbookSearch()
{
    var url = '/PortalSearch.aspx?tbcode=' + tbCode;
    
    if ($F('TextbookSectionDDL') != '')
        url += '&tsid=' + $F('TextbookSectionDDL');
       
    window.location.href = url;
}
