//Global Inventory Object
var inv;

//Gloabally available function to Instatiate and Load the Inventory Object.
// Param imagePath
// the path to the image to be displayed if stock level icons are to be displayed instead of actual
// stock levels.
function showInv(ProductId,doc,e,showWarehouse,showETA,imagePath){
	if((showWarehouse) || (showETA)){
		inv = new Inventory();
		inv.Load(doc,e,showWarehouse,showETA,imagePath);
		top.control.location.replace("../Catalog/InventoryPopup.aspx?ProductId=" + ProductId);
	}
}
/*-----------------------------------------------------------------------------
	Class Declarations
-----------------------------------------------------------------------------*/
/*
Inventory Class
	This Class Stores the Inventory data for a Product and has the code to display the information.
*/
function Inventory(){
	var p_Xcoord=10;
	var p_Ycoord=10;
	var m_arrWH=new Array;
	var m_arrETA=new Array();
	var m_pid;
	var m_doc;
	var m_div;
	var m_showWarehouse;
	var m_showETA;
	var m_imagePath;

	this.Warehouse=function(){return m_arrWH;}
	this.ETAs=function(){return m_arrETA;}

	this.Load=p_load;	
	this.SetProductId=p_setProductId;
	this.GetProductId=p_getProductId;
	this.AddWarehouse=p_addWarehouse;
	this.AddETA=p_addETA;
	this.Show=p_show;
	this.Hide=p_hide;
	this.ShowTab=p_showTab;

	function p_load(doc, e, showWarehouse, showETA, imagePath){
		m_div=doc.getElementById('invPopup');
		m_doc=doc;
		m_showWarehouse = showWarehouse;
		m_showETA = showETA;
		m_imagePath = imagePath;
		
		try{
			p_Xcoord=e.clientX-5+doc.body.scrollLeft;
			p_Ycoord=e.clientY-5+doc.body.scrollTop;
			if (doc.body.clientWidth<p_Xcoord+280)
				p_Xcoord=doc.body.clientWidth-280;
		}catch(er){
			//do nothing
		}
	}
	function p_setProductId(ProductId){
		m_pid=ProductId;
		m_arrWH.length=0;
		m_arrETA.length=0;
	}
	function p_getProductId(){
		return m_pid;
	}
	function p_addWarehouse(warehouse,qty, image, text){
		m_arrWH.push(new Warehouse(warehouse, qty, image, text));
	}
	function p_addETA(eta,qty){
		m_arrETA.push(new ETA(eta, qty));
	}
	function p_show(){
		var sInv, sStock, sETA;

		if (m_arrWH.length>0 && m_showWarehouse){
			sStock='<TABLE id="invStockTbl" style="margin:0px; padding:2px;border:none; width:100%">' +
				'<THEAD><TR>' +
				'<TD><B>Branch</B></TD>' +
				'<TD align="RIGHT"><B>Qty</B></TD></TR></THEAD>' +
				'<TBODY>';
			for(var i=0; i<m_arrWH.length; i++){
			    
			    // If the image name is '' then display actual values otherwise, display stock level
			    // indicator insted.
			    if( m_arrWH[i].image == '' ){
				    sStock+='<TR><TD>' + m_arrWH[i].Warehouse + '</TD><TD align="RIGHT">' + m_arrWH[i].Qty + '</TD></TR>';
				}
				else{
				    sStock+='<TR><TD>' + m_arrWH[i].Warehouse + '</TD><TD align="RIGHT"><img src="' + m_imagePath + m_arrWH[i].image + '" title="' + m_arrWH[i].text + '" ></TD></TR>';
				}
				    
			}
			sStock+='</TBODY></TABLE>';
		}else{
			sStock='<div id="invStockTbl">This item is out of stock!</div>';
		}

		if (m_arrETA.length>0 && m_showETA){
			sETA='<TABLE id="invETATbl"  style="margin:0px; padding:2px;border:none; width:100%;">' +
				'<THEAD><TR>' +
				'<TD><B>ETA</B></TD>' +
				'<TD align="RIGHT"><B>Qty</B></TD></TR></THEAD>' +
				'<TBODY>';
			for(var i=0; i<m_arrETA.length; i++){
				sETA+='<TR><TD>' + m_arrETA[i].ETA + '</TD><TD align="RIGHT">' + m_arrETA[i].Qty + '</TD></TR>';
			}
			sETA+='</TBODY></TABLE>';
		}else{
			sETA='<div id="invETATbl">This item has no backordered stock.</div>';
		}
		(m_showWarehouse ? "$2.00" : "$10.00")
		sInv=''+
		'<TABLE style="margin:0px;padding:0px;width:100%; border:1px solid black;background: #f0f0f0;">' +
		'<TBODY>' +
		'<TR>' +
		'<TD style="margin:0px;width:100%; background:#2222ff;color:#fff; padding:3px;">' +
		'<A onclick="top.inv.Hide();return false" href="#" style="float:right; text-decoration:none; border:1px solid #fff;font-size:13px; width:15px; text-align: center; color: #fff;font-family: arial;">X</A>Inventory' + 
		'</TD></TR>' +
		'<TR>' +
		'<TD style="PADDING: 0px; width:100%;">' +
		'<ul style="border-bottom : 1px solid black; margin:0; padding-bottom : 19px; padding-left : 10px;">' +
		'<li style="display:' + (m_showWarehouse ? 'inline' : 'none') +'; list-style-type:none;"><a id="invStockTab" href="#" onClick="top.inv.ShowTab(0); return false;" style="background:#fff;border:1px solid #000; color:#000; float:left; font-size:11px; font-weight:normal; line-height:14px; margin-right:8px; padding:2px 10px; text-decoration:none;">Stock</a></li>' +
		'<li style="display:' + (m_showETA ? 'inline' : 'none') +'; list-style-type:none;"><a id="invETATab"   href="#" onClick="top.inv.ShowTab(1); return false;" style="background:#fff;border:1px solid #000; color:#000; float:left; font-size:11px; font-weight:normal; line-height:14px; margin-right:8px; padding:2px 10px; text-decoration:none;">ETAs</a></li>' +
		'</ul>' +

		'<TABLE style="border: 1px solid black;border-top:none; width:100%;background: #fff; padding:0px; margin:0px;">' +
		'<TBODY>' +
		'<TR>' +
		'<TD style="PADDING: 0px; width:100%;">' +
		'<DIV style="BORDER:none; OVERFLOW: auto; HEIGHT: 100px">' + 
		sStock + 
		sETA +
		'<DIV>' +
		'</TD></TR></TBODY></TABLE>' +
		'</TD></TR></TBODY></TABLE>';

		top.setText(m_div,sInv);
		m_showWarehouse ? p_showTab(0) : p_showTab(1);
		
		// Check to see if there is any stock for the product. If not then show the ETA tab by default.
		var totalStock = 0;
		
		for( var i = 0; i < m_arrWH.length; ++i ){
		    totalStock += m_arrWH[ i ].Qty;
		}
		
		if( totalStock == 0 ){
		    p_showTab( 1 );
		}
		
		m_div.style.visibility="visible";
		m_div.style.left=p_Xcoord;
		m_div.style.top=p_Ycoord;
	}
	function p_hide(){
		m_div.style.visibility="hidden";
	}
	function p_showTab(idx){
		var cur, unselected, curTbl, unTbl;
		if(idx==0){
			cur=m_doc.getElementById('invStockTab');
			curTbl=m_doc.getElementById('invStockTbl');
			unselected=m_doc.getElementById('invETATab');
			unTbl=m_doc.getElementById('invETATbl');
		}else{
			cur=m_doc.getElementById('invETATab');
			curTbl=m_doc.getElementById('invETATbl');
			unselected=m_doc.getElementById('invStockTab');
			unTbl=m_doc.getElementById('invStockTbl');
		}

		cur.style.borderBottomColor='#fff';
		curTbl.style.display='';
		unselected.style.borderBottomColor='#000';
		unTbl.style.display='none';
	}
}
/*	
 * Warehouse Class
 * Used to store Quantities of Product per warehouse
 * Param: warehouse
 * The name of the warehouse.
 * Param: qty
 * the actual stock level in the warehouse for a product.
 * Param: image
 * the name of the image file for the stock level indicator.
 * Param: text
 * the tool tip text that accompanies the stock level indicator image.
 * Note: if image is '' then will display actual stock levels, otherwise display stock level
 * indicator icons.
 */
function Warehouse(warehouse, qty, image, text){
	this.Warehouse=warehouse;
	this.Qty=qty;
	this.image = image;
	this.text = text;
}
/*	
ETA Class
	Used to store Quantities of Product on backorder and their Duedate of arrival
*/
function ETA(eta, qty){
	this.ETA=eta;
	this.Qty=qty;
}