<!-- ----------------------------------------------------------------------------------
//
// Description:	Pricing. 
//				Note: The pricing data contained here will be moved to a database 
//				eventually; for now, the dataset is small enough that it can easily be 
//				managed (albeit manually) in this format while other more important
//				website coding tasks are developed.
//		  
// Maintenance:
//
// Date			Developer			Modification
// --------   	------------------	----------------------------------------------
// 01/20/07		Dan Carlson			Initial version created.
//
//--------------------------------------------------------------------------------- -->

maPicInfo = new Array();
maProdInfo = new Array();

//Object for product information.
function ProdInfo(sID, sCode, sDesc, sUnit, sSize, sPrice) {

	this.ID	   = sID;
	this.Code  = sCode;
	this.Desc  = sDesc; 
	this.Unit  = sUnit;
	this.Size  = sSize; 
	this.Price = sPrice; 
}

//Object for picture information.
function PicInfo(sID, sCode, sName, sDesc, sOrient, sGalleries, sProducts) {

	this.ID = sID;
	this.Code = sCode;
	this.Name = sName; 
	this.Desc = sDesc;
	this.Orient = sOrient;
	this.Galleries = sGalleries;
	this.Products  = sProducts; 
}

maPicInfo = new Array();
maProdInfo = new Array();

//-------------------------------------------------------------------------------------
// Attach events.
//-------------------------------------------------------------------------------------
window.onload = InitPage;

//-------------------------------------------------------------------------------------
// Module level variables.
//-------------------------------------------------------------------------------------
var moMain = top.frames;

//-------------------------------------------------------------------------------------
// Add product to shopping cart.
//-------------------------------------------------------------------------------------
function AddToCart() {

	var i;
	var oSrc = event.srcElement;
	
	//Get the pic info.
	i = parseInt(oSrc.getAttribute("PicIndex"));
	var sImageID   = maPicInfo[i].ID;
	var sImageCode = maPicInfo[i].Code;
	var sImageName = maPicInfo[i].Name;
	
	//Get the product info.
	i = parseInt(oSrc.getAttribute("ProdIndex"));
	var sProdCode = maProdInfo[i].Code;
	var sProdDesc = maProdInfo[i].Desc + " - " + maProdInfo[i].Size;
	var sCost = maProdInfo[i].Price;
	var sUnit = maProdInfo[i].Unit;

	//Add item to cart.
	var sItemCode = sImageCode + "-" + sProdCode;
	var sItemDesc = sImageName + " - " + sProdDesc;
	moMain.CartPopup.CartItemAdd(sItemCode, sItemDesc, 1, sCost, sUnit);
}

//-------------------------------------------------------------------------------------
// Build price list.
//-------------------------------------------------------------------------------------
function BuildPriceList(sImageCode) {

	//Clear price list object.
	PriceList.innerHTML = "";
	
	//Get the available products for this image.
	for (var i=0; i<maPicInfo.length; i++) {
		if (maPicInfo[i].Code == sImageCode) {
			var sPicIndex = i;
			var aProducts = maPicInfo[i].Products.split('|');
			break;
		} 
	}
	
	//Display the available products for this image.
	var sLastProd = "";
	var iTop = 0;
	for (var i=0; i<aProducts.length; i++) {
		for (var p=0; p<maProdInfo.length; p++) {
			if (aProducts[i] == maProdInfo[p].Code) {
			
				//Add some extra space when the product changes.
				if (maProdInfo[p].Desc.indexOf("Note Card") >= 0) {
					if (sLastProd.indexOf("Note Card") < 0) iTop += 8; 
				}
				else if (sLastProd != maProdInfo[p].Desc) {
					iTop += 8;
				}
				
				//Insert product name.
				var oItem = document.createElement("LABEL");	
				oItem.id = "ProdName" + p;
				oItem.className = "PriceItem";
				oItem.innerText = maProdInfo[p].Desc;
				oItem.style.left = "+6px";
				oItem.style.top = iTop;
				oItem.style.width  = "170px";
				PriceList.insertAdjacentElement("beforeEnd",oItem);
								
				//Insert product size.
				var oItem = document.createElement("LABEL");	
				oItem.id = "ProdSize" + p;
				oItem.className = "PriceItem";
				oItem.innerText = maProdInfo[p].Size;
				oItem.style.left = "+175px";
				oItem.style.top = iTop;
				oItem.style.width  = "50px";
				PriceList.insertAdjacentElement("beforeEnd",oItem);
								
				//Insert product price.
				var oItem = document.createElement("LABEL");	
				oItem.id = "ProdPrice" + p;
				oItem.className = "PriceItem";
				oItem.innerText = maProdInfo[p].Price;
				oItem.style.left = "+240px";
				oItem.style.top = iTop;
				oItem.style.width  = "50px";
				PriceList.insertAdjacentElement("beforeEnd",oItem);
								
				//Insert "add to cart" action.
				var oItem = document.createElement("LABEL");	
				oItem.id = "ProdAction" + p;
				oItem.className = "PriceAction";
				oItem.innerText = "Add to Cart";
				oItem.style.left = "+310px";
				oItem.style.top = iTop;
				oItem.style.width  = "70px";
				oItem.setAttribute("PicIndex", sPicIndex); 
				oItem.setAttribute("ProdIndex", p); 
				oItem.attachEvent("onmouseover",SetPriceActionHilite);
				oItem.attachEvent("onmouseout",SetPriceActionHilite);
				oItem.attachEvent("onclick",AddToCart);
				PriceList.insertAdjacentElement("beforeEnd",oItem);
				
				//Remember this product.
				sLastProd = maProdInfo[p].Desc;
				
				//Set top of next item.
				iTop += 18;
				//PriceList.style.height = iTop;
			} 
		}
	}
	
	//Show popup and adjust height.
	PricePopup.style.visibility = "visible";
	PriceList.style.height = iTop;
	PricePopup.style.height = iTop + PriceList.offsetTop + 20;
}

//-------------------------------------------------------------------------------------
// Called when image information has been retrieved from the database.
//-------------------------------------------------------------------------------------
function OnImagesLoaded() {

	//Check for error.
	if (dbImages.Error()) {
		return;
	}

	//Get reference to XMLDOMNodeList.
	var oResults = dbImages.DataNodeList();
    
    //If no data, get outta here.
	if (oResults.length == 0) return;

	//Build array of pic/image information.
	for (var i=0; i<oResults.length; i++) {
		var sID		= oResults.item(i).getAttribute("ImageID");
		var sCode	= oResults.item(i).getAttribute("ImageCode");
		var sName	= oResults.item(i).getAttribute("ImageName");
		var sDesc	= oResults.item(i).getAttribute("ImageDesc");
		var sOrient	= oResults.item(i).getAttribute("ImageOrient");
		var sGals	= oResults.item(i).getAttribute("ImageGalleries");
		var sProds	= oResults.item(i).getAttribute("ImageProducts");
		maPicInfo.push(new PicInfo(sID, sCode, sName, sDesc, sOrient, sGals, sProds));
	}
	
	//Load Products.
	ProductsLoadFromDB();
}

//-------------------------------------------------------------------------------------
// Called when product information has been retrieved from the database.
//-------------------------------------------------------------------------------------
function OnProductsLoaded() {

	//Check for error.
	if (dbProducts.Error()) {
		return;
	}

	//Get reference to XMLDOMNodeList.
	var oResults = dbProducts.DataNodeList();
    
    //If no data, get outta here.
	if (oResults.length == 0) return;

	//Build array of product information.
	for (var i=0; i<oResults.length; i++) {
		var sID		= oResults.item(i).getAttribute("ProductID");
		var sCode	= oResults.item(i).getAttribute("ProductCode");
		var sDesc	= oResults.item(i).getAttribute("ProductDesc");
		var sUnit	= oResults.item(i).getAttribute("Unit");
		var sSize	= oResults.item(i).getAttribute("PrintSize");
		var sPrice	= oResults.item(i).getAttribute("Price");
		sPrice = "$" + sPrice;
		maProdInfo.push(new ProdInfo(sID, sCode, sDesc, sUnit, sSize, sPrice));
	}
}

//-------------------------------------------------------------------------------------
// Submits SQL statement to retrieve images and related information from the database.
//-------------------------------------------------------------------------------------
function PicsLoadFromDB() {

	var fa = dbImages.document.all;

	//Start building SQL statement.
	var oSQL = new parent.SQL();
	oSQL.stmt = "qryImagesLoad";

	//Trim the last comma from the SQL code string.
	//oSQL.stmt = oSQL.stmt.substr(0,oSQL.stmt.length - 2); 
		
	//Submit SQL statement to the server.
	fa.UseCustomCOM.value = "N";
	fa.Database.value = "";
	fa.ReturnData.value = "Y";
	fa.Callback.value = "window.parent.OnImagesLoaded()";
	fa.SQL.value = oSQL.stmt;
	fa.ErrorMsg.value = "Unable to retrieve images.";
	fa.frmDB.submit();
}

//-------------------------------------------------------------------------------------
// Submits SQL statement to retrieve Product information from the database.
//-------------------------------------------------------------------------------------
function ProductsLoadFromDB() {

	var fa = dbProducts.document.all;

	//Start building SQL statement.
	var oSQL = new parent.SQL();
	oSQL.stmt = "qryProductsLoad";

	//Trim the last comma from the SQL code string.
	//oSQL.stmt = oSQL.stmt.substr(0,oSQL.stmt.length - 2); 
		
	//Submit SQL statement to the server.
	fa.UseCustomCOM.value = "N";
	fa.Database.value = "";
	fa.ReturnData.value = "Y";
	fa.Callback.value = "window.parent.OnProductsLoaded()";
	fa.SQL.value = oSQL.stmt;
	fa.ErrorMsg.value = "Unable to retrieve products.";
	fa.frmDB.submit();
}

//-------------------------------------------------------------------------------------
// Applies or removes hilite on a price action object.
//-------------------------------------------------------------------------------------
function SetPriceActionHilite() {

	var oSrc = event.srcElement;
	
    if (event.type == "mouseover") {
		oSrc.style.color = "orange";
    }
    else {
		oSrc.style.color = "blue";
 	}
}

-->