// Item Configurator - copyright 2007 npavel@ituner.com

// Configurator object
Configurator = function(configName)
{
	this.configName = configName;
	this.configurationPrice = 0;
	this.categoriesObjects = [];
	this.categories = 0;
	this.totalPrice = 0;
	this.submitItemsHTML = "";
	this.submitItemsURL= "";
};

// Item Object

Item = function(itemid, description, price)
{
	this.itemid = itemid;
	this.description = description;
	this.price = parseFloat(price);
	if (isNaN(this.price)) {
		this.price = 0;
	}
};

//<%=getAttribute("item", ID, "custitem2")%>
//<%=getCurrentAttribute("item","salesdescription")%>
// Category object

Category = function(categoryName, itemsArray)
{
	this.categoryName = categoryName;
	this.itemsArray = itemsArray;
	this.items = itemsArray.length/3;
	this.itemsInfoArray = [];
	this.selectedItem = -1;
	this.previousItem = -1;
	this.categoryPrice = 0;
	
	for (idx = 0, curidx = 0; idx < itemsArray.length; idx+=3, curidx++){
			this.itemsInfoArray[curidx] = new Item(this.itemsArray[idx], this.itemsArray[idx+1], this.itemsArray[idx+2].substr(1));
	}

};

// Prototypes
Configurator.prototype.addCategory = function (categoryObject)
{
	this.categoriesObjects.push(categoryObject);
	this.categories = this.categoriesObjects.length;
};

Configurator.prototype.selectItem = function(categoryId, itemId)
{
	this.categoriesObjects[categoryId].previousItem = this.categoriesObjects[categoryId].selectedItem;
	this.categoriesObjects[categoryId].selectedItem = itemId;

	// Update the price in the category
	for(itemidx = 0; itemidx < this.categoriesObjects[categoryId].items; itemidx ++) {
		if (this.categoriesObjects[categoryId].selectedItem == this.categoriesObjects[categoryId].itemsInfoArray[itemidx].itemid ) {
			 this.categoriesObjects[categoryId].categoryPrice = this.categoriesObjects[categoryId].itemsInfoArray[itemidx].price;	
		}
	}
};

Configurator.prototype.updatePrice = function()
{
	this.totalPrice = 0;
	for(idx = 0 ; idx < this.categories; idx++) {
		this.totalPrice += this.categoriesObjects[idx].categoryPrice;
	}
};

// format is: <input type=hidden name='multi' id='multi' value='457,1;458,1;550,1'>
Configurator.prototype.updateItemsSubmit = function()
{
	var prefixHTML = "<input type=hidden name='multi' id='multi' value='";
	var suffixHTML = "'>";
	var valueHTML = "";
	
	for(idx = 0 ; idx < this.categories; idx++) {
		if (this.categoriesObjects[idx].selectedItem == -1) continue;
		valueHTML += this.categoriesObjects[idx].selectedItem + ",1;";
	}
	this.submitItemsHTML = prefixHTML + valueHTML + suffixHTML;
}

Configurator.prototype.updateItemsURL = function()
{
	var prefixHTML = "http://www.mini-box.com/app/site/backend/additemtocart.nl?c=ACCT127230&qtyadd=1&buyid=multi&multi=";
	var valueHTML = "";
	
	for(idx = 0 ; idx < this.categories; idx++) {
		if (this.categoriesObjects[idx].selectedItem == -1) continue;
		valueHTML += this.categoriesObjects[idx].selectedItem + ",1;";
	}
	this.submitItemsURL = prefixHTML + valueHTML
}

Configurator.prototype.displayRawCategories = function()
{
	document.write("Configurator for:" + this.configName + "</br>");
	for(idx = 0 ; idx < this.categories; idx++) {
		document.write("<br><b>" + 	this.categoriesObjects[idx].categoryName + "</b><br>");
		for(itemidx = 0; itemidx < this.categoriesObjects[idx].items; itemidx ++) {
			document.write("<br>&nbsp;&nbsp;&nbsp;" + 	this.categoriesObjects[idx].itemsInfoArray[itemidx].itemid);
			document.write("," + 	this.categoriesObjects[idx].itemsInfoArray[itemidx].description);
			document.write("," + 	this.categoriesObjects[idx].itemsInfoArray[itemidx].price + "<br>");
		}
	}
};


Configurator.prototype.displayForm = function ()
{
	
	//document.write("<form name=\"" + this.configName + "\" method='post' action='http://www.mini-box.com/app/site/backend/additemtocart.nl'>\n");
	//document.write("\074" + "f" + "o" + "r" + "m" + " name="miniboxconfigurator" method='post' action='http://www.mini-box.com/app/site/backend/additemtocart.nl'>\n");
	document.write("<form name='miniboxconfigurator' method='post' action='http://www.mini-box.com/app/site/backend/additemtocart.nl'>\n");
	document.write("<table class='configurat' cellspacing='0' cellpading='0' border='0' valign='top'>\n");
		document.write("\n<tr class='nohover' style='padding: 3px; text-align: center;'>");
		document.write("<td colspan='2' style='border-bottom: 1px dotted #ccc;'><span class='titlu_tabel'>CONFIGURE YOUR SYSTEM</span></td>");
		document.write("</tr>");
		document.write("\n<tr class='nohover2'>");
		document.write("<td colspan='2'></td>");
		document.write("</tr>");
	//document.write("<form method='post' action='http://www.mini-box.com/app/site/backend/additemtocart.nl'>\n");
	for(idx = 0 ; idx < this.categories; idx++) {
		document.write("\n<tr style='padding: 3px;'><td style='text-align: right; margin: 3px;' valign='top'>");
		document.write("<span class=textboldnolink>" + 	this.categoriesObjects[idx].categoryName + ":</span>&nbsp;&nbsp;");
		document.write("</td><td style='text-align: left; margin: 3px;' valign='middle'>");
		document.write("<select STYLE='width: 350px; height: 25px; overflow: scroll; padding: 2px;' id=configurator" + idx + " onChange=\"changeSelectedItem(" + idx + ")\">");
	   		for(itemidx = 0; itemidx < this.categoriesObjects[idx].items; itemidx ++) {
			document.write("\n<option class=smalltext style=\"width:300px\" value=" + this.categoriesObjects[idx].itemsInfoArray[itemidx].itemid + ">" 
					+ this.categoriesObjects[idx].itemsInfoArray[itemidx].description 
					+ " ( $" + this.categoriesObjects[idx].itemsInfoArray[itemidx].price + " ) "
					+ "</option>");
		}
		document.write("</select>\n");
		document.write("</td></tr>\n");
	}		

	
	//this.updateItemsSubmit();	
	this.updateItemsURL();
	this.updatePrice();	
	document.write("\n<tr class='nohover2'>");
	document.write("<td colspan='2' style='border-bottom: 1px dotted #ccc; font-size: 2px;'>&nbsp;</td>");
	document.write("</tr>");
	document.write("<tr><td align=left>\n");
	document.write("<input type='hidden' name='c' value='ACCT127230'>\n");
	document.write("<input type=hidden name='qtyadd' id='qtyadd' value='1'>\n");
	document.write("<input type=hidden name='buyid' id='buyid' value='multi'>\n");
	//document.write("<div id='configuratorItemsSubmit'>\n" + this.submitItemsHTML + "</div>\n");
	//netsuite automaticaly closes tags and submit a input tag from a div tag won't work we need to redirect
	document.write("<div id='configuratorItemsSubmit'>\n");
	document.write(this.submitItemsHTML);
	document.write("</div>\n");
	//document.write("<input name='multi' id='multi' value='498,1;458,1;486,1;466,1;553,1;417,1;193,1;' type='hidden'>");
	document.write("\n"); 
	document.write("</td><td align='left'>\n");
	//document.write("<p align='left'><input class='bgbutton' type='submit' value='Add to cart' onkeypress='window.event.cancelBubble=true;' onclick='document.forms[\"miniboxconfigurator\"].submit();;return false;'></p>\n");
	document.write("<img style='float: right; margin-right: 70px;' src='http://www.mini-box.com/site/images/buy_now.gif'  onclick='configuratorObject.updateItemsURL(); window.location=configuratorObject.submitItemsURL'><nobr><div id='configuratorEstimatedPrice' style='margin-top: 7px; margin-left: 180px; font-size: 14px; font-family: trebuchet ms,arial,hevetica,sans-serif; font-weight: bold; color: #ff9900;'>Price: $" + this.totalPrice.toFixed(2) + "</div></nobr>");

	//document.write("</form>\n");
	document.write("</td></tr></table>\n");
	
}