﻿

function OnixItemSearch()
{
    //If subject has not been sent or is not set
    if(typeof arguments[0] ==  'undefined' || typeof arguments[0] != 'string')
    {
        throw new Error('OnixItemSearch() constructor expects argument 1 to be of \'string\' type');
    }
    
    if(typeof arguments[1] ==  'undefined' || typeof arguments[1] != 'string')
    {
        throw new Error('OnixItemSearch() constructor expects argument 2 to be of \'string\' type');
    }
    
    this.isbn = arguments[0];
    this.searchLabel = arguments[1];
    //Send Ajax request and get response
    this.loaded = this.loadAjax();
}


//STATIC PROPERTIES------------------------------------------------------------------------------------------------------------------------------------------------------

OnixItemSearch.prototype._SEARCHSTR = '/secondary/onix/onixsearch.js?open&';
//OnixItemSearch.prototype._SEARCH_LABEL =  'TeacherSupportItemSearch';


//METHODS-----------------------------------------------------------------------------------------------------------------------------------------------------------------	
 
OnixItemSearch.prototype.getSearchURL = function()
{
    var tempArray = new Array();
    tempArray.push('query=([ISBN]=' + this.isbn + ')+and+[FORM]=Product');
    tempArray.push('start=1');
    tempArray.push('count=1');
    tempArray.push('label=' + this.searchLabel);    
    return this._SEARCHSTR + tempArray.join('&');
};

OnixItemSearch.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('searchOnixResults' + this.searchLabel);
    } catch(oError) {
        throw new Error('OnixItemSearch.loadAjax() error on eval(searchOnixResults) Serries.load()');
    }
        
    //If search results exist
    if(typeof results != 'undefined' && results instanceof Array && results.length > 0)
    {                
        this.tsIndex = new Array();
        for(var i = 1; i < results.length; i++)
        {
            if(typeof results[i] != 'undefined')
            {
                this.tsIndex.push(results[i]);                  
            }
        }
        if(Secondary._LOG_ENABLED)LOG.printH1('OnixItemSearch.loadAjax() - returned ' + this.tsIndex.length + ' results');
            
        //If there are no valid results
        if(this.tsIndex.length == 0)
        {
            return false;
            //If log enabled display message
            if(Secondary._LOG_ENABLED)LOG.printH1('OnixItemSearch.loadAjax() did not return any valid results');
        } else {
            return true;
        } 
            
    } else {
            throw new Error("OnixItemSearch.load() 'results' is undefined");
    }
};













