﻿
function TeacherSupportFilter()
{
    //If subject has not been sent or is not set
    if(typeof arguments[0] ==  'undefined' || typeof arguments[0] != 'string')
    {
        throw new Error('TeacherSupportFilter contructor expects argument 1 to be of \'string\' type');
    }
        
    this.subject = arguments[0];
    this.category = (typeof arguments[1] != 'undefined' && typeof arguments[1] == 'string') ? arguments[1] : null;
}


//STATIC PROPERTIES------------------------------------------------------------------------------------------------------------------------------------------------------

TeacherSupportFilter.prototype._SEARCHSTR = '/secondary31/site/filter.js?readform&';
//TeacherSupportFilter.prototype._SEARCH_LABEL =  'TeacherSupportFilter';


//METHODS-----------------------------------------------------------------------------------------------------------------------------------------------------------------	
 
TeacherSupportFilter.prototype.getSearchURL = function()
{
    var tempArray = new Array();
    tempArray.push('type=Multi');
    var subcat = (this.category) ? (this.subject + '>' + this.category) : this.subject;
    
      
    tempArray.push('cat=Teacher Support>Stories:Teacher+Support>' + subcat);    
    tempArray.push('start=1');
    tempArray.push('count=100');
    //alert(this._SEARCHSTR + tempArray.join('&'));
    return this._SEARCHSTR + tempArray.join('&');
};

TeacherSupportFilter.prototype.loadAjax = function()
{          
    var oXHR = DH.createXHR();
    oXHR.open('GET',decodeURIComponent(this.getSearchURL()),false);
    oXHR.send(null);  
		
    //If request error
    if(!(oXHR.readyState == 4 && (oXHR.status == 200 || oXHR.status == 304))) return false;
        
    var TeacherSupportFilterStr = oXHR.responseText;
        
    //Check wheter request was successfull
    try{
        eval(TeacherSupportFilterStr);
    } catch(oError) {
        throw new Error('TeacherSupportFilter.loadAjax() error on eval(TeacherSupportFilterStr) TeacherSupportFilter.load()');
    } 
        
    //If teacher support filter results exist
    if(typeof viewResults != 'undefined' && viewResults instanceof Array && viewResults.length > 0)
    {                
        this.index = new Array();
        for(var i = 1; i < viewResults.length; i++)
        {
            if(typeof viewResults[i] != 'undefined' && this.isResultValid(i))
            {
            
                this.index.push(viewResults[i]);                  
            }
        }
        if(Secondary._LOG_ENABLED)LOG.printH1('TeacherSupportFilterloadAjax() - returned ' + this.index.length + ' results');
            
        //If there are no valid results
        if(this.index.length == 0)
        {
            //If log enabled display message
            if(Secondary._LOG_ENABLED)LOG.printH1('TeacherSupportFilterloadAjax() did not return any valid results');
        } else {
            this.loaded = true;
        } 
            
    } else {
        throw new Error("TeacherSupportFilterloadAjax() 'viewResults' is undefined");
    }
};

//Function which validates a 'viewResults' entry
TeacherSupportFilter.prototype.isResultValid =  function(i)
{
    var result = viewResults[i];
    
    //Variable used to detect invalid properties
    var valid = true;
    
    //Check DocID
    if(!result.viewDocID || result.viewDocID.length != 32)valid = false;
    
    //Check Title
    if(!result.viewTitle || result.viewTitle.length < 1)valid = false;
    
    //Check Categories
    if(!result.viewCategories || result.viewCategories.length < 1)valid = false;
    if(!valid && Secondary._LOG_ENABLED)LOG.printH1('TeacherSupportFilter.isResultValid() result [' + i + '] is NOT valid');
       
    return valid;
};

TeacherSupportFilter.prototype.getCategoriesFromSubject = function()
{
    var tempArray = new Array();
    
    for(var i = 0; i < this.index.length; i++)
    {
        var cat = (decodeURIComponent(this.index[i].viewCategories)).split('>')[1];
        
        if(i == 0)
        {
            tempArray.push(cat);
        } else {
            if(!tempArray.hasString(cat))
            {
                tempArray.push(cat);
            }   
        }
    }
    return tempArray.sort();
};

TeacherSupportFilter.prototype.getItemsFromCategory = function(category)
{
    var tempArray = new Array();
    
    for(var i = 0; i < this.index.length; i++)
    {
        var cat = (decodeURIComponent(this.index[i].viewCategories)).split('>')[1];
        
        if(cat.toLowerCase() == category.toLowerCase())
        {
            tempArray.push(this.index[i]);
        }
    }
    return tempArray;
};

//Function which attaches all results from subject grouped according to categories
TeacherSupportFilter.prototype.attachIndex1 = function(oParent)
{
    var categories = this.getCategoriesFromSubject();
    
    //Attach heading
    var h3 = document.createElement('h3');
    h3.appendChild(document.createTextNode(this.subject));
    h3.className = 'teacherSupportIndex';
    oParent.appendChild(h3);
    
    //Create main div
    var mainDiv = document.createElement('div');
    mainDiv.setAttribute('id','teacherSupportIndex');
    
    //Attach index
    for(var i = 0; i < categories.length; i++)
    {
        var h4 = document.createElement('h4');
        h4.appendChild(document.createTextNode(categories[i]));
        mainDiv.appendChild(h4);
        
        var items = this.getItemsFromCategory(categories[i]).sort(sortByViewTitle);
        
        for(var x = 0; x < items.length; x++)
        {
            var title = '- ' + decodeURIComponent(items[x].viewTitle);
            var docID = decodeURIComponent(items[x].viewDocID);
            
            var a = document.createElement('a');
            a.appendChild(document.createTextNode(title));
            a.setAttribute('title',title);
            a.setAttribute('href','/secondary31/site/articleIDs/' + docID + '?open&template=domSecondary');
            mainDiv.appendChild(a);    
        }
    }
    oParent.appendChild(mainDiv);
};

//Function which attaches results from a category
TeacherSupportFilter.prototype.attachIndex2 = function(oParent)
{ 
    //Attach heading
    var h3 = document.createElement('h3');
    h3.appendChild(document.createTextNode(this.subject));
    h3.className = 'teacherSupportIndex';
    oParent.appendChild(h3);
    
    //Create main div
    var mainDiv = document.createElement('div');
    mainDiv.setAttribute('id','teacherSupportIndex');
    
    //Attach index
    var items = this.index.sort(sortByViewTitle);
    for(var x = 0; x < items.length; x++)
    {
        var title = '- ' + decodeURIComponent(items[x].viewTitle);
        var docID = decodeURIComponent(items[x].viewDocID);
            
        var a = document.createElement('a');
        a.appendChild(document.createTextNode(title));
        a.setAttribute('title',title);
        a.setAttribute('href','/secondary31/site/articleIDs/' + docID + '?open&template=domSecondary');
        mainDiv.appendChild(a);    
    }
    oParent.appendChild(mainDiv);
};

//Function which attaches results from a category in a subject
TeacherSupportFilter.prototype.attachIndex3 = function(oParent)
{
    if(!this.category)return;
    
    //Attach heading
    var h3 = document.createElement('h3');
    h3.appendChild(document.createTextNode(this.subject));
    h3.className = 'teacherSupportIndex';
    oParent.appendChild(h3);
    
    //Create main div
    var mainDiv = document.createElement('div');
    mainDiv.setAttribute('id','teacherSupportIndex');
    
    var h4 = document.createElement('h4');
    h4.appendChild(document.createTextNode(this.category));
    mainDiv.appendChild(h4);
        
    var items = this.getItemsFromCategory(this.category).sort(sortByViewTitle);
        
    for(var x = 0; x < items.length; x++)
    {
        var title = '- ' + decodeURIComponent(items[x].viewTitle);
        var docID = decodeURIComponent(items[x].viewDocID);
            
        var a = document.createElement('a');
        a.appendChild(document.createTextNode(title));
        a.setAttribute('title',title);
        a.setAttribute('href','/secondary31/site/articleIDs/' + docID + '?open&template=domSecondary');
        mainDiv.appendChild(a);    
    }
    
    oParent.appendChild(mainDiv);
};

TeacherSupportFilter.prototype.attachNoRecordsMessage = function(oParent,message)
{
    //Attach heading
    var h3 = document.createElement('h3');
    h3.appendChild(document.createTextNode(this.subject));
    h3.className = 'teacherSupportIndex';
    oParent.appendChild(h3);
    
    //Create main div
    var mainDiv = document.createElement('div');
    mainDiv.setAttribute('id','teacherSupportIndex');
    
    var p = document.createElement('p');
    p.style.margin = '0';
    p.style.padding = '20px 0 0 0';
    p.style.fontWeight = 'bold';
    p.appendChild(document.createTextNode(message));
    mainDiv.appendChild(p);
    oParent.appendChild(mainDiv);
};


/*------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/

function sortByViewTitle(a, b)
{
    atitle = decodeURIComponent(a.viewTitle);
    btitle = decodeURIComponent(b.viewTitle);

    if (atitle < btitle)
    {
        return -1;
    } else if (atitle > btitle) {
        return 1;
    } else {
        return 0;
    }
}


    

	
















