//*********************************************************
// Support script to dynamically change the contents of a static htm page
// to display an inventory indicator.
// Uses the XmlHttpRequest object which is supported in
// Internet Explorer 5.0+, Safari 1.2, 
// Mozilla 1.0 / Firefox, and Netscape 7.
// Author: Robert Kozell
// Date: 11/12/2005

//**********************************************************
// Define the XmlHttpRequest object.  It must remain scoped at the script level
var xhttp;
// Sync the onload event
window.onload = window_onload;

//*********************************************************
var OutOfStockDisplay =
	'<table border="0" width="100%">' +
		'<tr>' +
			'<td width="100%">' +
				'<IMG BORDER="0" SRC="/sorrysoldout2.gif" width="525" height="60" ' +
				'alt="Sorry, This item is Sold Out">' +				
			'</td>' +
		'</tr>' +
	'</table>';
	
var InStockDisplay =
	'<table border="0" width="100%">' +
		'<tr>' +
			'<td width="100%">' +
				'<IMG BORDER="0" SRC="/instock.gif" width="525" height="60" ' +
				'alt="In stock.">' +				
			'</td>' +
		'</tr>' +
	'</table>';
	
//*********************************************************
// Script Functions

function window_onload() 
{	
	checkServerInventory();
}

function setInventoryIndication( itemID, quantity )
{	
	var htm
	if( quantity <= 0 )
	{
		htm = OutOfStockDisplay;
	}
	else
	{
		htm = InStockDisplay;
	}
	
	if( htm != null )
	{
		InventoryPlaceholder.innerHTML = htm;	
	}	
}

function checkServerInventory()
{	
	if( document.getElementById("InventoryPlaceholder") == null )
	{
		alert("No InventoryPlaceholder control is defined on this page");
		return;
	}
      
    try
    {
		// Instantiate XmlHttpRequest				
		if( window.ActiveXObject )
		{ 
			// Internet Explorer
			xhttp = new ActiveXObject( "Msxml2.XMLHTTP" ); 
		} 
		else if ( window.XMLHttpRequest )
		{ 
			// Mozilla - based browser 
			xhttp = new XMLHttpRequest(); 
		} 
		else
		{
			return;
		}  
    
		// Hook the event handler
		xhttp.onreadystatechange = HandlerOnReadyStateChange;
		
		// Prepare the call, http method=GET, false=asynchronous call
		// The DateTime paramater forces the browser not the cache the result
		xhttp.open("GET", 
			"/Admin/InventoryCheck.aspx?ItemID=" + ItemID + "&DateTime=" + new Date(),
			true);
			
		// Send the call    
		xhttp.send("");
	}
	catch( e )
	{
		alert( e );
	}
}

function HandlerOnReadyStateChange()
{	
    // This handler is called 4 times for each state change of xhttp
    // States are: 0=uninitialized, 1=loading, 2=loaded, 3=interactive,
    // 4=complete
    if( xhttp.readyState == 4 )
    {
		if( xhttp.status == 200 )
		{			
			// Success			
			var id = getNodeValue( xhttp.responseXML, "ItemID" );
			var quantity = getNodeValue( xhttp.responseXML, "Quantity" );						
			if( quantity != null )
			{
				setInventoryIndication( id, quantity );		
			}					
        }
	}
}

// Retrieves the text value of tagname from the xml document
function getNodeValue( ResponseXML, tagname )
{	
	var text;
	try
	{
		var theNodeList = ResponseXML.getElementsByTagName( tagname );		
		if( theNodeList.length > 0 )
		{	
			var node = theNodeList[0];
			var childNode = node.firstChild;
			while( childNode != null )
			{
				if( childNode.nodeType == 3 )
				{
					// Text Node
					text = childNode.nodeValue;
					break;
				}				
				childNode = childNode.nextSibling;
			}				
		}	
	}
	catch( e )
	{
		alert(e);
	}
	return text;	
}
    

