/******************************************************************************
* eshopobjects.js                                                             *
* ---------------                                                             *
*                                                                             *
* OBJECTS DECLARATION FOR E-SHOP ITEMS                                        *
*                                                                             *
*******************************************************************************
*                                                                             *
* Copyright 2000-2002 Isotools, all right reserved                            *
*                                                                             *
******************************************************************************/

///////////////////////////////////////////////////////////////////////////////
//
// ITEM OBJECTS
//
function ShopItem()
{
	this.id;
	this.name;
	this.reference;
	this.price;
	this.reduction
	this.duty;
	this.quantity;
	this.options = new Array();
}

ShopItem.prototype.initialize = function(name, id, ref, price, reduction, duty, quantity)
{
	this.name = name;
	this.id = id;
	this.reference = ref;
	this.price = price;
	this.reduction = reduction;
	this.duty = duty;
	this.quantity = quantity;
}

ShopItem.prototype.makeFromCookieString = function(itemString)
{
	var properties = itemString.split('$');
	
	this.name = unescape(properties[0]);
	this.id = unescape(properties[1]);
	this.reference = unescape(properties[2]);
	this.price = properties[3];
	this.reduction = properties[4];
	this.duty = properties[5];
	this.quantity = properties[6];

	// get options
	var i;
	for (i=7 ; i<properties.length ; i++)
	{
		var strOption = properties[i];
		if (strOption == "" || strOption == null)
			continue;
		var option = new ShopItemOption();
		option.makeFromCookieString(strOption);
		this.options[this.options.length] = option;
	}
}

ShopItem.prototype.dumpCookieString = function()
{
	var cookieString = "";

	cookieString += escape(this.name) + "$";
	cookieString += escape(this.id) + "$";
	cookieString += escape(this.reference) + "$";
	cookieString += this.price + "$";
	cookieString += this.reduction + "$";
	cookieString += this.duty + "$";
	cookieString += this.quantity + "$";

	var i;
	for (i=0 ; i<this.options.length ; i++)
		cookieString += this.options[i].dumpCookieString() + "$";

	cookieString += "#";

	return cookieString;
}

//
// dutyDisplayMode : to know if we have to return HT price 
//                   or TTC price
//
ShopItem.prototype.getUnitPrice = function(dutyDisplayMode) 
{
	// unit price : we have to look at dutymode, reduction, options.
	var itemUnitPrice = parseFloat(this.price);
	
	// first add option price modification
	for (j=0 ; j<this.options.length ; j++)
	{
		var option = this.options[j];
		var k;
		for (k=0 ; k<option.values.length ; k++)
		{
			var value = option.values[k];
			if (value.selected == true)
			{
				if (value.priceModification == "extraCharge")
					itemUnitPrice = parseFloat(itemUnitPrice) + parseFloat(value.amount);
				else if (value.priceModification == "reduction")
					itemUnitPrice = parseFloat(itemUnitPrice) - parseFloat(value.amount);
			}
		}
	}
	
	// then apply reductions if any
	itemUnitPrice = applyReductions(itemUnitPrice, this.reduction);
	
	// then look at dutyMode to know how to return price
	if (dutyDisplayMode != priceMode)
	{
		if (priceMode == "HT")
			itemUnitPrice = getPriceTTC(itemUnitPrice, this.duty);
		else
			itemUnitPrice = getPriceHT(itemUnitPrice, this.duty);
	}

	return itemUnitPrice;
}

ShopItem.prototype.writeBasket = function(cookieName)
{
	document.write("<tr>");
	document.write("<td class='fontFamilyStandardText fontColorStandardText fontSizeNormal borderBottomShopItemSeparator' valign='top'>");

	// item name
	document.write("<span class='fontColorShopItemName'>"+this.name+"</span><br>");
	// item reference
	document.write(this.reference);

	if (this.options.length > 0)
		document.write("<br>")

	// item options
	var j;
	for (j=0 ; j<this.options.length ; j++)
		this.options[j].writeBasket(cookieName, this);
	document.write("<br></td>");

	// unit price : we have to look at dutymode, reduction, options.
	var itemUnitPrice = this.getUnitPrice(priceDisplay);
	if (displayPrices == "true")
	{
		document.write("<td class='fontFamilyStandardText fontColorStandardText fontSizeNormal borderBottomShopItemSeparator' valign='top' align='right'>");
		document.write(round(itemUnitPrice) + "&nbsp;" + currency + "</td>");
	}

	// item quantity
	document.write("<td class='fontFamilyStandardText fontColorStandardText fontSizeNormal borderBottomShopItemSeparator' valign='top' align='center'>");
	document.write("&nbsp;<input name='qte"+this.id+"' ' size='1' value='" + this.quantity + "'>");
	document.write("<a href='#' onClick='changeItemQuantity(\"" + cookieName + "\", \"" + this.id + "\", window.document.basket.qte"+this.id+".value, true);return false;'><img border='0' src='./iso_icons/1changequantity.png' align='absmiddle'></a>");
	document.write("</td>");

	// item totalPrice
	if (displayPrices == "true")
	{
		var itemTotalPrice = round(parseFloat(round(itemUnitPrice))*parseInt(this.quantity));
		document.write("<td class='fontFamilyStandardText fontColorStandardText fontSizeNormal borderBottomShopItemSeparator' valign='top' align='right'>" + itemTotalPrice + "&nbsp;" + currency + "</td>");
	}

	document.write("</tr>");

}

ShopItem.prototype.showOrder = function(cookieName)
{
	document.write("<tr>");
	document.write("  <td class='fontFamilyStandardText fontColorStandardText fontSizeNormal borderBottomShopItemSeparator' valign='top'>");
	
	// item name
	document.write("  <span class='fontColorShopItemName'>"+this.name+"</span><br>");
	// item reference
	document.write(this.reference);

	if (this.options.length > 0)
		document.write("<br>")

	// item options
	var j;
	for (j=0 ; j<this.options.length ; j++)
		this.options[j].showOrder(cookieName, this, false);
	document.write("    <br></td>");

	// unit price : we have to look at dutymode, reduction, options.
	var itemUnitPrice = this.getUnitPrice(priceDisplay);
	if (displayPrices == "true")
	{
		document.write("<td class='fontFamilyStandardText fontColorStandardText fontSizeNormal borderBottomShopItemSeparator' valign='top' align='right'>");
		document.write(round(itemUnitPrice) + "&nbsp;" + currency + "</td>");
	}

	// item quantity
	document.write("<td class='fontFamilyStandardText fontColorStandardText fontSizeNormal borderBottomShopItemSeparator' valign='top' align='center'>" + this.quantity + "</td>");
	
	// item totalPrice
	if (displayPrices == "true")
	{
		var itemTotalPrice = round(parseFloat(round(itemUnitPrice))*parseInt(this.quantity));
		document.write("<td class='fontFamilyStandardText fontColorStandardText fontSizeNormal borderBottomShopItemSeparator' valign='top' align='right'>" + itemTotalPrice + "&nbsp;" + currency + "</td>");
	}
	
	document.write("</tr>");
}

ShopItem.prototype.showPrintableOrder = function(cookieName)
{
	var str = "";

	str += "<tr>";
	str += "<td valign='top'>";
	
	// item name
	str += "<b>"+this.name+"</b><br>";
	// item reference
	str += this.reference;

	if (this.options.length > 0)
		str += "<br>"

	// item options
	var j;
	for (j=0 ; j<this.options.length ; j++)
		str += this.options[j].showOrder(cookieName, this, true);
	str += "<br></td>";

	// unit price : we have to look at dutymode, reduction, options.
	var itemUnitPrice = this.getUnitPrice(priceDisplay);
	if (displayPrices == "true")
	{
		str += "<td valign='top' align='right'>";
		str += round(itemUnitPrice) + "&nbsp;" + currency + "</td>";
	}

	// item quantity
	str += "<td valign='top' align='center'>" + this.quantity + "</td>";
	
	// item totalPrice
	if (displayPrices == "true")
	{
		var itemTotalPrice = round(parseFloat(round(itemUnitPrice))*parseInt(this.quantity));
		str += "<td valign='top' align='right'>" + itemTotalPrice + "&nbsp;" + currency + "</td>";
	}
	
	str += "</td>";
	str += "</tr>";

	var colspan = (displayPrices == "true") ? "4" : "2";
	str += "<tr><td colspan='"+colspan+"'><img src='./iso_icons/black.gif' width='100%' height='1'></td></tr>";

	return str;
}

ShopItem.prototype.fillOrderForm = function()
{
	var itemStr = "";
	// item name
	itemStr += this.name + " (ref:" + this.reference + ")";

	if (this.options.length > 0)
		itemStr += "\n";

	// item options
	var j;
	for (j=0 ; j<this.options.length ; j++)
		itemStr += this.options[j].fillOrderForm(this);

	// unit price : we have to look at dutymode, reduction, options.
	var itemUnitPrice = this.getUnitPrice(priceDisplay);
	if (displayPrices == "true")
	{
		itemStr += thesaurus.shop_unitPriceTitle + " = " + round(itemUnitPrice) + " " + currency + "\n";
	}

	// item quantity
	itemStr += thesaurus.shop_quantityTitle + " : " + this.quantity + "\n";
	
	// item totalPrice
	if (displayPrices == "true")
	{
		var itemTotalPrice = round(parseFloat(round(itemUnitPrice))*parseInt(this.quantity));
		itemStr += thesaurus.shop_priceTitle + " = " + itemTotalPrice + " " + currency + "\n";
	}

	itemStr += "----------------------------------------------------------\n";

	return itemStr;
}

//------------------------------------------------------------
function ShopItemOption()
{
	this.name;
	this.bMultiSel;
	this.values = new Array();
}

ShopItemOption.prototype.initialize = function(name, multiSel)
{
	this.name = name;
	this.bMultiSel = (multiSel == "true");
}

ShopItemOption.prototype.makeFromCookieString = function(optionString)
{
	var properties = optionString.split('&');
	
	this.name = unescape(properties[0]);
	this.bMultiSel = (properties[1] == "true");

	// get values
	var i;
	for (i=2 ; i<properties.length ; i++)
	{
		var strValue = properties[i];
		if (strValue == "" || strValue == null)
			continue;
		var value = new ShopItemOptionValue();
		value.makeFromCookieString(strValue);
		this.values[this.values.length] = value;
	}
}

ShopItemOption.prototype.dumpCookieString = function()
{
	var cookieString = "";

	cookieString += escape(this.name) + "&";
	cookieString += (this.bMultiSel == true)?"true&":"false&";
	
	var i;
	for (i=0 ; i<this.values.length ; i++)
		cookieString += this.values[i].dumpCookieString() + "&";

	return cookieString;
}

ShopItemOption.prototype.writeBasket = function(cookieName, item)
{
	document.write("<li><b>" + this.name + " :&nbsp;</b></li>");
	if (this.bMultiSel == true)
	{
		document.write("<br>");
		var k;
		for (k=0 ; k<this.values.length ; k++)
		{
			var value = this.values[k];
			//var label = replace(value.name, ' ', '&nbsp;');
			var label = value.name
			if (value.priceModification == "extraCharge")
				label += "&nbsp;(+" + value.amount + "&nbsp;" + currency + ")";
			else if (value.priceModification == "reduction")
				label += "&nbsp;(-" + value.amount + "&nbsp;" + currency + ")";
			
			var valueName = escape(value.name);
			document.write("<input type=\"checkbox\" value=\"" + valueName + "\" ");
			if (value.selected == true)
				document.write(" checked");

			var optionName = escape(this.name);
			document.write(" onClick=\"changeItemOptionValue('" + cookieName + "', '" + item.id + "', '" + optionName +"', this.value, true)\">&nbsp;" + label + "<br>");
		}
	}
	else
	{
		var optionName = escape(this.name);
		document.write("<select onChange='changeItemOptionValue(\"" + cookieName + "\", \"" + item.id + "\", \"" + optionName +"\", this.options[this.selectedIndex].value , true)'>");
		document.write("<option value=\"\"> </option>");
		var k;
		for (k=0 ; k<this.values.length ; k++)
		{
			var value = this.values[k];
			var label = value.name;
			if (value.priceModification == "extraCharge")
				label += " (+" + value.amount + "&nbsp;" + currency + ")";
			else if (value.priceModification == "reduction")
				label += " (-" + value.amount + "&nbsp;" + currency + ")";
			
			var valueName = escape(value.name);
			document.write("<option value=\"" + valueName + "\"");
			if (value.selected == true)
			{
				document.write(" selected");
			}
			document.write(">" + label + "</option>");
		}
		document.write("</select><br>");
	}
}

ShopItemOption.prototype.showOrder = function(cookieName, item, bPrintable)
{
	var html = "";

	var isSelected = false;
	var k;
	for (k=0 ; k<this.values.length ; k++)
	{
		var value = this.values[k];
		if (value.selected == true)
		{
			isSelected = true;
			break;
		}
	}

	if (isSelected == false)
		return "";

	if (bPrintable == true)
		html += "<li>" + this.name + " :</li>";
	else
		document.write("<li><b>" + this.name + " :</b></li>");

	for (k=0 ; k<this.values.length ; k++)
	{
		var value = this.values[k];
		if (value.selected == true)
		{
			var label = "";
			if (this.bMultiSel == true)
				label += "<br>";
			label += ((this.bMultiSel == true)?"&nbsp;&nbsp;&nbsp;&nbsp;":"&nbsp;") + value.name;
			if (value.priceModification == "extraCharge")
				label += " (+" + value.amount + "&nbsp;" + currency + ")";
			else if (value.priceModification == "reduction")
				label += " (-" + value.amount + "&nbsp;" + currency + ")";
			
			if (bPrintable == true)
				html += label
			else
				document.write(label);
		}
	}
	if (bPrintable == true)
		return html;
}

ShopItemOption.prototype.fillOrderForm = function(item)
{
	var optionStr = "";

	var isSelected = false;
	var k;
	for (k=0 ; k<this.values.length ; k++)
	{
		var value = this.values[k];
		if (value.selected == true)
		{
			isSelected = true;
			break;
		}
	}

	if (isSelected == false)
		return "";

	optionStr += this.name + " : ";

	for (k=0 ; k<this.values.length ; k++)
	{
		var value = this.values[k];
		if (value.selected == true)
			optionStr += "[" + value.name + "] ";
	}

	optionStr += "\n";
	
	return optionStr;
}
//------------------------------------------------------------
function ShopItemOptionValue()
{
	this.name;
	this.priceModification;
	this.amount;
	this.selected;
}

ShopItemOptionValue.prototype.initialize = function(name, priceModif, amount)
{
	this.name = name;
	this.priceModification = priceModif;
	this.amount = amount;
	this.selected = false;
}

ShopItemOptionValue.prototype.makeFromCookieString = function(valueString)
{
	var properties = unescape(valueString).split('£');

	this.name = unescape(properties[0]);
	this.priceModification = properties[1];
	this.amount = properties[2];
	this.selected = (properties[3] == "true");
}

ShopItemOptionValue.prototype.dumpCookieString = function()
{
	var cookieString = "";

	cookieString += escape(this.name) + "£";
	cookieString += this.priceModification + "£";
	cookieString += this.amount + "£";
	cookieString += (this.selected == true)?"true":"false";

	return cookieString;
}
//
///////////////////////////////////////////////////////////////////////////////
 