﻿

function TeacherSupport()
{
    //If subject has not been sent or is not set
    if(typeof arguments[0] ==  'undefined' || typeof arguments[0] != 'string')
    {
        throw new Error('Series.init() expects argument 1 to be of \'string\' type');
    }
        
    this.isbn = arguments[0]; 
}


//STATIC PROPERTIES------------------------------------------------------------------------------------------------------------------------------------------------------

TeacherSupport.prototype._SEARCHSTR = '/secondary31/site/search.js?open&';
TeacherSupport.prototype._SEARCH_LABEL =  'TeacherSupport';


//METHODS-----------------------------------------------------------------------------------------------------------------------------------------------------------------	
 
TeacherSupport.prototype.getSearchURL = function()
{
    var tempArray = new Array();
    tempArray.push('start=1');
    tempArray.push('count=1');
    tempArray.push('query=[SectionPath]=Teacher+Support+and+[MetaKeywords]=' + this.isbn);
    tempArray.push('label=' + this._SEARCH_LABEL);
    return this._SEARCHSTR + tempArray.join('&');
};

TeacherSupport.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 teacherSupportStr = oXHR.responseText;
        
    //Check wheter request was successfull
    try{
        eval(teacherSupportStr);
        var results = eval('searchResults' + this._SEARCH_LABEL);
    } catch(oError) {
        throw new Error('TeacherSupport.loadAjax() error on eval(teacherSupportStr) TeacherSupport.load()');
    } 
        
    //If search results exist
    if(typeof results != 'undefined' && results instanceof Array && typeof results[1] != 'undefined' && this.isResultValid(results[1]))
    {                
        this.index = results[1];
        this.loaded = true;
    } /* else {
        throw new Error("TeacherSupport.load() 'viewResultsTeacherSupport(results)' is undefined");
    }
    */
};

//Function which validates a 'viewResults' entry
TeacherSupport.prototype.isResultValid =  function(result)
{
    //1 - docID
    //2 - Distinctive Title
    //3 - Edition Statement
    //4 - Lead Author
    //5 - Publication Date
    //6 - ISBN
    //7 - Price
    //8 - Publisher Name
    //9 - Imprint or Binding
    //10 - Summary (Abstract if it exists otherwise first 300 chars of Main Description)
    //11 - parentID
    //12 - BIC Main Subject Code
    //13 - Subject
    //14 - Area
    //15 - Series
        
    //Variable used to detect invalid properties
    var valid = true;
        
    //Array to store invalid properties
    var tempArray = new Array();
        
    //check DocID    
    if(typeof result.viewDocID != 'string' || result.viewDocID.length == 0)
    {
        valid = false;
        tempArray.push('DocID');
    } 
    
        
    //if there are invalid entries and log is enabled
    if(valid == false && Secondary._LOG_ENABLED)LOG.printH1('TeacherSupport.isResultValid() - invalid result : ' + tempArray.join(', '));
        
    return valid;
};


    

	
















