// ===========================================================================================
// Copyright 2010
// by Asset Generation Elite Services, Inc.
// ===========================================================================================
function attrObj(num, name, descr, def_opt)			// Attribute Object
{
	this.num     = num;					// Unique index number of this Attribute
	this.name    = name;				// Unique name of this Attribute (e.g. "Takoda Foldable Tote Color Selection")
	this.descr   = descr;				// Description of this Attribute (e.g. "Color", "Size", etc.)
	this.def_opt = def_opt;				// Which option should be "pre-selected" for this attribute
	this.opt     = new Array();
	if (num >= 10000)
	{
		this.num     = num - 10000;
		this.def_opt = -1;				// -1 means "do not display this attribute to user" ... it is for reference only
	}
}

attrObj.prototype.optionAdd = function(opshun)
{
	if (opshun == null)
		return;

	this.opt.push(opshun);				// Add option to end of option list
}

function attrFindByName(lyst, name)
{
	var found = null;
	for(var i = 0; i < lyst.length; i++)
	{
		if (name == lyst[i].name)
		{
				found = lyst[i];
				break;
		}
	}
	return(found);
}

function attrFindByNum(lyst, num)
{
	var found = null;
	for(var i = 0; i < lyst.length; i++)
	{
		if (num == lyst[i].num)
		{
				found = lyst[i];
				break;
		}
	}
	return(found);
}

// ===========================================================================================
var ProduxLinkBase  = "";
var ProduxImageBase = "";

function produxCommonInfo(linkbase, imagebase)
{
	ProduxLinkBase  = linkbase;			// pre-pend to produx Link-to URLs
	ProduxImageBase = imagebase;		// pre-pend to produx Image-path URLs
}

// ===========================================================================================

function produxObj(code, name, keywords, popularity, price, image, lynk, short, descr)
{
	this.code      = code;				// Item's Code (index, SKU, whatevah)
	this.name      = name;				// Name of Item
	this.keywords  = keywords;			// Keywords (sub-string searchable)
	this.popular   = popularity;		// How Popular is this produx? (1 - 10 .. or more!)
	this.price     = price;				// Item's Price
	this.image     = ProduxImageBase + image;	// Image of Item
	this.lynk      = ProduxLinkBase  + lynk;	// Name of Item
	this.short     = short;				// Short Description
	this.descr     = descr;				// Full Description
	this.sale      = -1;				// Discount (if any) for this Produx
	this.special   = "";				// Discription of Discount for this Produx
	this.attrib    = new Array();		// Applicable Attributes
	this.attrib_select = new Array();	// Current Selection for each attribute (on this produx)
}

produxObj.prototype.Sale = function(sale, special)
{
	this.sale    = sale;				// Current Discounted Price for this Item
	this.special = special;				// Discription of Current Discount for this Item
}

produxObj.prototype.AttrAdd = function(attr)
{
	if (attr == null)
		return;

	this.attrib.push(attr);
	this.attrib_select.push(attr.def_opt);
}

produxObj.prototype.displayOptionGenerate = function(disp_opt_string)
{
	var disp_opt = "";

	// Encode display options into return value

	return(disp_opt);
}

produxObj.prototype.display = function(disp_opt, w, h)
{
	var innerHTML = "";

	var fsize  = -1;
	var pixcent = 35;
	var texcent = 0;

	var optsize = regExpfindValueByKey(disp_opt, ":size:", " ");
	if (optsize == "M")
	{
		fsize = 0;
		pixcent = 50;
		texcent = 0;
	}
	if (optsize == "L")
	{
		fsize = 1;
		pixcent = 20;
		texcent = 55;
	}

	var pic_height = regExpfindValueByKey(disp_opt, ":pixtall:", " ");
	if (pic_height <  20) pic_height =  20;
	if (pic_height > 100) pic_height = 100;

	var maxW = parseInt(w * pixcent / 100);
	var maxH = parseInt((h * pic_height) / 100) - 10;	if (maxH < 20) maxH = 20;

	// Build inner HTML according to display Options
	innerHTML += "<center>\n";
//	innerHTML += disp_opt + "<br />W" + w + " H" + h + "\n";
	innerHTML += "<table height=\"" + h + "\" width=\"100%\">\n";
	
	innerHTML += "<tr><td valign=\"middle\" width=\"" + pixcent + "%\">\n";
	innerHTML += "<center><img src=\"" + this.image + "\" style=\"opacity:0.0;filter:alpha(opacity=0)\" ";
	innerHTML += "width=\"" + maxW + "\" height=\"" + maxH + "\" ";
	innerHTML += "onload=\"this.width=imgWidth(this.src, " + maxW + ", " + maxH + "); ";
	innerHTML += "this.height=imgHeight(this.src, " + maxW + ", " + maxH + "); ";
	innerHTML += "objOpacitySet(this, 100);\"></center>\n";
	innerHTML += "</td><td valign=\"middle\" width=\"" + (100 - texcent - pixcent) + "%\">\n";

	innerHTML += "<table><tr><td>\n";
	innerHTML += "<font size=\"" + num2signed(fsize-1) + "\"><strong>" + this.name + "</strong></font>\n";
	innerHTML += "</td></tr><tr><td>\n";
	innerHTML += "<font size=\"" + num2signed(fsize-2) + "\">" + this.short + "</font>\n";
	innerHTML += "</td></tr><tr><td>\n";
	innerHTML += "<font size=\"" + num2signed(fsize-1) + "\">";
	if (this.sale > 0)
	{
		innerHTML += "<strike>" + num2price(this.price) + "</strike> ";
		innerHTML += "<strong>" + num2price(this.sale) + "</strong> ";
		innerHTML += "(" + this.special + ")\n";
	}
	else
	{
		innerHTML += "<strong>" + num2price(this.price) + "</strong> ";
	}
	innerHTML += "</font></td></tr><tr><td>\n";
	innerHTML += "<font size=\"" + num2signed(fsize) + "\">";
	innerHTML += "<a href=\"" + this.lynk + "\" target=\"__storlynk\"><strong>Buy Now</strong></a>";
//	innerHTML += "<form action=\"" + this.lynk + "\"><input type=submit  target=\"__storlynk\" value=\"Add to Cart\" /></form>\n";
	innerHTML += "</font>\n";
	innerHTML += "</td></tr></table>\n";

	if (optsize == "L")
	{
		innerHTML += "</td><td valign=\"middle\" width=\"" + texcent + "%\">\n";
		innerHTML += "<font size=\"" + num2signed(fsize-4) + "\">" + this.descr + "</font>\n";
	}

	innerHTML += "</td></tr></table>\n";
	innerHTML += "</center>\n";

	return(innerHTML);
}

produxObj.prototype.displayTableLine = function()
{
	var maxW      = 40;
	var maxH      = 40;
	var fsize     = 0;
	var innerHTML = "";

	// Build inner HTML according to display Options
	innerHTML += "<tr valign=\"middle\"><td><font size=\"" + num2signed(fsize-1) + "\">" + this.code + "</font>";
	innerHTML += "</td><td>\n";
	innerHTML += "<a href=\"" + this.lynk + "\" target=\"__storlynk\">";
	innerHTML += "<img src=\"" + this.image + "\" style=\"opacity:0.0;filter:alpha(opacity=0)\" ";
	innerHTML += "width=\"" + maxW + "\" height=\"" + maxH + "\" ";
	innerHTML += "onload=\"this.width=imgWidth(this.src, " + maxW + ", " + maxH + "); ";
	innerHTML += "this.height=imgHeight(this.src, " + maxW + ", " + maxH + "); ";
	innerHTML += "objOpacitySet(this, 100);\">\n";
	innerHTML += "</a>";

	innerHTML += "</td><td colspan=3>\n";
	innerHTML += "<a href=\"" + this.lynk + "\" target=\"__storlynk\">";
	innerHTML += "<font size=\"" + num2signed(fsize-1) + "\"><strong>" + this.name + "</strong></font>\n";
	innerHTML += "</a>";

	innerHTML += "</td><td>\n";
	innerHTML += "<font size=\"" + num2signed(fsize-2) + "\">" + this.short + "</font>\n";

	innerHTML += "</td><td>\n";
	innerHTML += "<font size=\"" + num2signed(fsize-1) + "\">";
	if (this.sale > 0)
	{
		innerHTML += "<strike>" + num2price(this.price) + "</strike> ";
		innerHTML += "<strong>" + num2price(this.sale) + "</strong> ";
		innerHTML += "<br/>(" + this.special + ")\n";
	}
	else
	{
		innerHTML += "<strong>" + num2price(this.price) + "</strong> ";
	}
	innerHTML += "</font>";
	
	innerHTML += "</td><td>\n";
	innerHTML += "<font size=\"" + num2signed(fsize-1) + "\">";
	innerHTML += "<a href=\"" + this.lynk + "\" target=\"__storlynk\"><strong>See Details</strong></a>";
	innerHTML += "</font>\n";
	innerHTML += "</td></tr>\n";

	return(innerHTML);
}

produxObj.prototype.izCode = function(kode)
{
	return((kode == this.code)? true:false);
}

produxObj.prototype.izMatch = function(key)
{
	return((this.keyword.indexOf(key) != -1)? true:false);
}

function produxKeywordDisplayTable(lyst, keyword)
{
	var found = null;
	for(var i = 0; i < lyst.length; i++)
	{
		if (keyword == lyst[i].keywords)
		{
			echo(lyst[i].displayTableLine());
		}
	}
}

function produxFind(lyst, kode)
{
	var found = null;
	for(var i = 0; i < lyst.length; i++)
	{
		if (kode == lyst[i].code)
		{
				found = lyst[i];
				break;
		}
	}
	return(found);
}

function produxShuffle(lyst)
{
	var giveLyst = lyst.slice();						// copy the array

	var i;
	var rindex;
	var lm;
	for(i=0; i < giveLyst.length; i++)
	{
		rindex = randomInt(0, giveLyst.length - 1);
		lm = giveLyst.splice(rindex, 1);					// pluck from somewhere
		giveLyst.unshift(lm[0]);							// move it to beginning
	}
	giveLyst.reverse();										// reverse Array order
	for(i=0; i < giveLyst.length; i++)
	{
		rindex = randomInt(0, giveLyst.length - 1);
		lm = giveLyst.splice(rindex, 1);					// pluck from somewhere
		giveLyst.unshift(lm[0]);							// move it to beginning
	}

	return(giveLyst);
}

function produxMatchKey(prod, key, usecase)
{
	if (usecase)
	{
		if (prod.name.indexOf(key) != -1)
			return(true);
		if (prod.keywords.indexOf(key) != -1)
			return(true);
		if (prod.short.indexOf(key) != -1)
			return(true);
		if (prod.descr.indexOf(key) != -1)
			return(true);
	}
	else // case-insensitive
	{
		if (prod.name.toLowerCase().indexOf(key.toLowerCase()) != -1)
			return(true);
		if (prod.keywords.toLowerCase().indexOf(key.toLowerCase()) != -1)
			return(true);
		if (prod.short.toLowerCase().indexOf(key.toLowerCase()) != -1)
			return(true);
		if (prod.descr.toLowerCase().indexOf(key.toLowerCase()) != -1)
			return(true);
	}

	return(false);
}

// ===========================================================================================
