﻿

function MoreInfo()
{
    //If subject has not been sent or is not set
    if(typeof arguments[0] ==  'undefined' || typeof arguments[0] != 'string')
    {
        throw new Error('RelatedTitles.init() expects argument 1 to be of \'string\' type');
    }
    
    //If  series has not been sent or is not set
    if(typeof arguments[1] ==  'undefined' || typeof arguments[1] != 'string')
    {
        throw new Error('RelatedTitles.init() expects argument 2 to be of \'string\' type');
    }
    
    //If  isbn has not been sent or is not set
    if(typeof arguments[2] ==  'undefined' || typeof arguments[2] != 'string')
    {
        throw new Error('RelatedTitles.init() expects argument 3 to be of \'string\' type');
    }
    
    this.subject = arguments[0];    
    this.area = arguments[1];
    this.isbn = arguments[2];
    
    //Send Ajax request and get response
    this.loaded = this.loadAjax();
}


//STATIC PROPERTIES------------------------------------------------------------------------------------------------------------------------------------------------------

MoreInfo.prototype._SEARCHSTR = '/secondary/onix/onixsearch.js?open&';
MoreInfo.prototype._SEARCH_LABEL =  'MoreInfo';


//METHODS-----------------------------------------------------------------------------------------------------------------------------------------------------------------	
 
MoreInfo.prototype.getSearchURL = function()
{
    var tempArray = new Array();
    //tempArray.push('query=[Division]=Secondary+and+[EAN13]=' + this.isbn + '+and+![ed_TextType]=Table of contents+and+[Form]=Related');
    tempArray.push('query=[Division]=Secondary+and+[ISBN]=' + this.isbn + '+and+[ed_TextType]=Long description+and+[Form]=Related');
    tempArray.push('start=1');
    tempArray.push('count=1');
    tempArray.push('label=' + this._SEARCH_LABEL);    
    return this._SEARCHSTR + tempArray.join('&');
};

MoreInfo.prototype.getLinkHref = function()
{
    var tempArray = new Array();
    tempArray.push('div=Secondary');
    tempArray.push('cat=' + this.subject + '>' + this.area);
    tempArray.push('template=domSecondary');
    tempArray.push('onixtype=more_info');
    tempArray.push('ed=site/seced31.nsf');
    return '/secondary/onix/all/' + decodeURIComponent(this.moreInfoIndex[0].viewCol1) + '?open&' + decodeURIComponent(tempArray.join('&'));
};

MoreInfo.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 searchOnixResults = oXHR.responseText;
        
    //Check wheter request was successfull
    try{
        eval(searchOnixResults);
        var results = eval('searchOnixResultsMoreInfo');
    } catch(oError) {
        throw new Error('MoreInfo.loadAjax() error on eval(searchOnixResults) Serries.load()');
    }
        
    //If search results exist
    if(typeof results != 'undefined' && results instanceof Array && results.length > 0)
    {                
        this.moreInfoIndex = new Array();
        for(var i = 1; i < results.length; i++)
        {
            if(typeof results[i] != 'undefined' && this.isResultValid(results[i]))
            {
                this.moreInfoIndex.push(results[i]);                  
            }
        }
        if(Secondary._LOG_ENABLED)LOG.printH1('MoreInfo.loadAjax() - returned ' + this.moreInfoIndex.length + ' results');
            
        //If there are no valid results
        if(this.moreInfoIndex.length == 0)
        {
            return false;
            //If log enabled display message
            if(Secondary._LOG_ENABLED)LOG.printH1('MoreInfo.loadAjax() did not return any valid results');
        } else {
            return true;
        } 
            
    } else {
            throw new Error("MoreInfo.load() 'results' is undefined");
    }
};

//Function which validates a 'viewResults' entry
MoreInfo.prototype.isResultValid =  function(result, i)
{
    //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.viewCol1 != 'string' || result.viewCol1.length < 1)
    {
        valid = false;
        tempArray.push('DocID');
    }
      
    //if there are invalid entries and log is enabled
    if(valid == false && Secondary._LOG_ENABLED)LOG.printH1('MoreInfo.isResultValid()[' + i + '] - invalid result : ' + tempArray.join(', '));
        
    return valid;
};


MoreInfo.prototype.attach =  function(oParent)
{
    if(!this.loaded)return;  
    var a = document.createElement('a');
    a.setAttribute('href',this.getLinkHref());
    a.setAttribute('title','More');    
    if(DH.getURLParam('onixtype') && DH.getURLParam('onixtype') == 'more_info')
    {
        a.className = 'current';
    }
    var span = document.createElement('span');
    span.appendChild(document.createTextNode('More'));
    a.appendChild(span);
    var li = document.createElement('li');
    li.appendChild(a);
    oParent.appendChild(li);    
};


    

	
















