<!--

// --- Ajax Request ------------------------------------------------------------

var TT_HTTP = {};
TT_HTTP._factory = null;
TT_HTTP._factories = [
    function() { return new XMLHttpRequest(); },
    function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
    function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
];


TT_HTTP.newRequest = function()
{
    // if an appropriate factory was already found, use it
    if (TT_HTTP._factory != null)
        return TT_HTTP._factory();
    // else search a working factory, one that creates a request
    for(var i = 0; i < TT_HTTP._factories.length; i++)
    {
        try
        {
            var factory = TT_HTTP._factories[i];
            var request = factory();
            if (request != null) {
                TT_HTTP._factory = factory;
                return request;
            }
        }
        catch(e) { continue; }
    }
    // no factory method did work, each did throw an exception
    TT_HTTP._factory = function() {
        throw new Error("XMLHttpRequest not supported");
    }
    TT_HTTP._factory(); // throw exception
}


// --- Search by country or by language ----------------------------------------

function onSearchtypeChange(obj)
{
    document.getElementById('js-country-block').style.display = 'none';
    document.getElementById('js-language-block').style.display = 'none';

    if (document.getElementById('js-searchtype-country').checked) {
        document.getElementById('js-country-block').style.display = 'block';
        _showIframeUS(false);
    }
    
    if (document.getElementById('js-searchtype-language').checked) {
        document.getElementById('js-language-block').style.display = 'block';
        _showIframeUS(false);        
    }
    
    var radioStateStd = document.getElementById('js-searchtype-statestandard');
    if (radioStateStd && radioStateStd.checked) {
        document.getElementById('js-country-block').style.display = 'none';  
        document.getElementById('js-language-block').style.display = 'none';        
        _showIframeUS(true);
    }
    
    return true;
}


function _showIframeUS(bool)
{
    var iframe = document.getElementById('js-iframe-US');
    if (iframe) {
        iframe.style.display = bool ? 'block' : 'none';
    }
    
    var contents = document.getElementById('js-maincontents');    
    if (contents) {
        contents.style.display = bool ? 'none' : 'block';
    }
    
    document.getElementById('js-searchbutton').style.display = bool ? 'none' : 'block';
}

// --- Dropdowns ---------------------------------------------------------------

function onCountryDropdownChange(obj, flags)
{
    var countryId = obj.options[obj.selectedIndex].value;
    updateGradeDropdown(countryId, flags);
    updateSubjectDropdown(countryId, flags);    
    return true;
}

function updateGradeDropdown(countryId, flags)
{
    var request = TT_HTTP.newRequest();
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            var status; 
            try { status = request.status } catch(e) { status = 0; };
            if (status == 200) {
                setDropdownOptions('js-grade-select', request.responseText, flags);
            }
        }
    }
    request.open('GET', '/ajax_query_dropdown.php?type=grade&country='+countryId+'&flags='+flags, true);
    request.send(null);
}

function updateSubjectDropdown(countryId, flags)
{
    var request = TT_HTTP.newRequest();
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            var status; 
            try { status = request.status } catch(e) { status = 0; };
            if (status == 200) {
                setDropdownOptions('js-subject-select', request.responseText, flags);
            }
        }
    }
    request.open('GET', '/ajax_query_dropdown.php?type=subject&country='+countryId+'&flags='+flags, true);
    request.send(null);
}

function setDropdownOptions(id, optionsHtml, flags)
{
    var sel = document.getElementById(id);
    if (sel) {
        // Another IE 5/6/7 bug: This browser can't handle innerHTML on <select> elements, 
        // so we have to use a fragile workaround...
        if (typeof(sel.outerHTML) == 'undefined')
        {
            // sane browser
            sel.innerHTML = optionsHtml; 
        }
        else {
            // Internet Explorer
            var old = sel.outerHTML.split('>', 2);
            sel.outerHTML = old[0] + '>' + optionsHtml + '</select>';
        }
        sel = document.getElementById(id);                
        sel.disabled = (optionsHtml.search(/value="-"/) != -1);
    }
}
-->
