

function PriceAvailability(isbn,quantity)
{


	if(typeof isbn != 'string' || isbn.length != 13)
	{
		//throw new Error('Constructor of Price and Availability expects a valid isbn argument: [' + isbn + ']')
	}
	
    this.isbn = isbn;
    //this.isbn = "9780732958749";
    this.quantity = 1;
    if(quantity)this.quantity = quantity;
    
    // GET request variables
    this.searchVars = new Array();
    this.searchVars['ClientID'] = 'PAL';
    //this.searchVars['ClientPassword'] = 'xxxx';
    this.searchVars['ProductIDType'] = '15';
    if(isbn)this.searchVars['ProductIDValue'] = isbn;    
    this.searchVars['SupplyQuantity'] = this.quantity;
    this.searchVars['CurrencyCode'] = 'AUD';
    
	this.load();    
}


PriceAvailability.prototype.load = function()
{
	this.loadAjax();
	this.getPrice();
	this.getAvailability();
	this.getShipDate();
};

        
PriceAvailability.prototype.loadAjax = function()
{
	//var sUrl = 'http://secondary3.itechne.com/site/OnixPA02.nsf/PAService?readform&' + this.getSearchVars();
	//var sUrl = 'http://secondary3.itechne.com/site/OnixPA02_test.nsf/PAService?readform&' + this.getSearchVars();
	//document.write(sUrl);

	if(window.location.toString().indexOf('macmillan.com.au') != -1) // macmillan server live PA db
	{
		var sUrl = '/site/OnixPA02.nsf/PAService?readform&' + this.getSearchVars();
	} else { 
		var sUrl = 'http://secondarydev.itechne.com/site/OnixPA02_test.nsf/PAService?readform&' + this.getSearchVars();    
	}
	
	
	    	this.xml = null;

    var oXHR = DH.createXHR();
    oXHR.open('GET',sUrl,false);
    oXHR.send(null); 
    if(!(oXHR.readyState == 4 && (oXHR.status == 200 || oXHR.status == 304))) return false;

	try 
        {
		var responseType = oXHR.responseXML.getElementsByTagName('ResponseType')[0].hasChildren;
	}
	catch (err) {}

	if(!responseType)
	{
		this.xml = oXHR.responseXML;
	} else {
	  	this.xml = null;
	}
 
};
        
PriceAvailability.prototype.getSearchVars = function()
{
	var tempArray = new Array();
	for(i in this.searchVars)
    {
		if(typeof this.searchVars[i] != 'undefined' && typeof this.searchVars[i] != 'function')
		{
    		tempArray.push(i + '=' + this.searchVars[i]);
		}
	}
    return (tempArray.join('&'));
};
        
PriceAvailability.prototype.parseXML = function(text)
{
	// code for IE
	if (window.ActiveXObject)
	{
		var doc=new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(text);
    } else { // code for Mozilla, Firefox, Opera, etc.
    	var parser=new DOMParser();
    	var doc=parser.parseFromString(text,"text/xml");
    }
    return doc;
};
        
PriceAvailability.prototype.getPrice = function()
{
	if(!this.xml)return;

		// New xml walk to work in IE6
                var getLevel1 = XMLWalk(this.xml.childNodes, "PriceAvailabilityResponse");
                var getLevel2 = XMLWalk(getLevel1, "ProductPriceAvailability");
                var getLevel3 = XMLWalk(getLevel2, "SupplierPriceAvailability");
                var getLevel4 = XMLWalk(getLevel3, "RetailPrice");
                var getLevel5 = XMLWalk(getLevel4, "PriceAmount");

		var oPrice = getLevel5[0].nodeValue;

	// var oPrice = this.xml.getElementsByTagName('SupplierPriceAvailability')[0].getElementsByTagName('PriceAmount')[0].firstChild.nodeValue;
            
    //if book not found
    if(oPrice == 'Entry not found in index')return;
            
    if(oPrice.indexOf('$') != -1)
    {
    	this.price = oPrice;
    } else {
    	this.price = '$' + oPrice;
    }
};
        
PriceAvailability.prototype.getAvailability = function()
{
	if(!this.xml)return;

                // XMLWalk for Instock
                var getLevel1 = XMLWalk(this.xml.childNodes, "PriceAvailabilityResponse");
                var getLevel2 = XMLWalk(getLevel1, "ProductPriceAvailability");
                var getLevel3 = XMLWalk(getLevel2, "SupplierPriceAvailability");
                var getLevel4 = XMLWalk(getLevel3, "InStock");
                if (!getLevel4[0]) return;
                var oAvailability = getLevel4[0].nodeValue;
		// var oAvailability = this.xml.getElementsByTagName('SupplierPriceAvailability')[0].getElementsByTagName('InStock')[0].firstChild;

    if(!oAvailability)return;
    		//oAvailability = oAvailability.nodeValue;
    if(oAvailability.toLowerCase().indexOf('true') != -1)
    {
    	this.available = true;
    }
};


        
PriceAvailability.prototype.getShipDate = function()
{
	if(!this.xml || this.available)return;

                var getLevel1 = XMLWalk(this.xml.childNodes, "PriceAvailabilityResponse");
                var getLevel2 = XMLWalk(getLevel1, "ProductPriceAvailability");
                var getLevel3 = XMLWalk(getLevel2, "SupplierPriceAvailability");
                var getLevel4 = XMLWalk(getLevel3, "ExpectedShipDate");
                //var shipDate = getLevel4[0].nodeValue;
		if (getLevel4[0] == null) return;
		var shipDate = getLevel4[0].nodeValue;
          
	//var shipDate = this.xml.getElementsByTagName('SupplierPriceAvailability')[0].getElementsByTagName('ExpectedShipDate')[0].firstChild;
	//if(!shipDate || shipDate.nodeValue.length != 8)return;         

		if (!shipDate)return;
		if (shipDate.length != 8)return;

    //var tempDate = shipDate.nodeValue;
            
            var year = parseInt(shipDate.substring(0,4));
            // strip leading zeros
            if  (shipDate.substring(4,5)=="0") {
            var month = parseInt(shipDate.substring(5,6));
            }
            else {
            var month = parseInt(shipDate.substring(4,6));    
            }  
            if  (shipDate.substring(6,7)=="0") {
            var day = parseInt(shipDate.substring(7,8));
            }
            else {
            var day = parseInt(shipDate.substring(6));
            }   

    
    var todayDate = new Date();
    var tmpShipDate = new Date();
    tmpShipDate.setDate(day);
    tmpShipDate.setMonth(month-1);
    tmpShipDate.setYear(year);
    
    if (tmpShipDate < todayDate) {
	    this.shipDate = "";
    }
    else {
        this.shipDate = day + '/' + month + '/' + year; 
    }

};




function XMLWalk(nodesToWalk, FindByName)
{
    for (var i=0; i < nodesToWalk.length; i++) { if (nodesToWalk[i].nodeType == 1 && nodesToWalk[i].nodeName == FindByName) { return nodesToWalk[i].childNodes; } }
}


    

