/**********************************************
* Constructor for a new Configuration Object
***********************************************/

//-- ABs (Abbreviated functions)
var cEl = createElement;
var cInEl = createInputElement;
var $h = Element.hide;
var $s = Element.show;

//-- Browser detection
var isIE     = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
var isMoz    = document.implementation && document.implementation.createDocument;
var isSafari = ((navigator.userAgent.toLowerCase().indexOf('safari')!=-1))?true:false;

Configuration = Class.create();
Configuration.prototype = {

//~~~( Setup Functions )~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    //-----------------------------------------------------------------------
    //- Setup the object, read in options, and render the skeleton
    initialize : function(container, tab_container, sidebar_container, config_string, config_pages_string, product_id, renewals, maintenance, customer_reseller_level, yearly_renewal_options, options)
    {
		//alert('Customers Reseller Level ' + customer_reseller_level);
        //- Find the container divs
        if(!container) return;
        this.container = $(container);
        if(!this.container)
            alert('Could not find your container (' + this.container + ')');

        if(!sidebar_container) return;
        this.sidebar_container = $(sidebar_container);
        if(!this.sidebar_container)
            alert('Could not find your container (' + this.sidebar_container + ')');

        if(!tab_container) return;
        this.tab_container = $(tab_container);
        if(!this.tab_container)
            alert('Could not find your container (' + this.tab_container + ')');

		this.setOptions(options);	
		this.sidebar_start_location = 25; 
		this.sidebar_curr_location = 25; 
		this.sidebar_dest_location = 0; 
        this.sidebar_timeout_var;
        this.is_sidebar_scrolling = false;
		this.sidebar_container.style.top = this.sidebar_start_location+"px";
        this.sidebar_container.style.position = 'absolute';

		var scrollObserver = this.scrollConfigInfo.bindAsEventListener(this);
		Event.observe(window, 'scroll', scrollObserver, false);

        //- Member variables

		// This array holds references to the form object
		this.formArr = [];
		// This array holds references to each row in the config Summary
		this.configSummaryArr = [];
		// maps a config_item_id to its index in the config and formArr arrays
		this.configItemIdToIndexMap = [];
		// this is our main data structure
        this.config = config_string;
		this.config_pages = config_pages_string;
		this.renewals = renewals.subscription_renewals;
		this.maintenance = maintenance.maintenance;
		// running total of config price
		this.totalPrice;
		// cell where total price is displayed
		this.totalCell;

		this.yearly_options = yearly_renewal_options.yearly_options;

		//- Keeps track of what is the current config row
		this.currentConfigRow = [];

		// preload images
		this.imagesArr = [];
		this.imagesArr[165] = new Image();
		this.imagesArr[165].src = "/catalog/images/tower_case_remastered_175.jpg";
		this.imagesArr[80] = new Image();
		this.imagesArr[80].src = "/catalog/images/3U_case_remastered_250.jpg";

		this.currentEnforcedDependencies = [];
		this.currentExtraItems = [];
		this.formTabs = [];
		this.formTabIdToIndexMap = [];
		this.currentTabIndex = 0;
		this.mainProductAttributes;
		this.addedProducts;

		this.customer_reseller_level = customer_reseller_level;
        this.product_id = product_id;

		if (customer_reseller_level == 1) {
			this.product_price = this.config.product_details.products_reseller_price1;
		} else if (customer_reseller_level == 2) {
			this.product_price = this.config.product_details.products_reseller_price2;
		} else if (customer_reseller_level == 3) {
			this.product_price = this.config.product_details.products_reseller_price3;
		} else {
			this.product_price = this.config.product_details.products_price;
		}

		this.product_price = this.product_price.substring(0, this.product_price.lastIndexOf("0") - 1);

		//for debugging purposes, remove before we go live
		//dumpObj(this.config_pages);
		//dumpObj(this.config);
	},

	//--------------------------------------------------------------------------
	//- Function:		setObjectOptions
	//- Description:	
	//-
	//--------------------------------------------------------------------------
	setOptions : function(options)
	{
		this.options = {};
		Object.extend(this.options, options || {});
	},

	//--------------------------------------------------------------------------
	//- Function:		createFormTabs
	//- Description:	This function creates the form tabs that will contain all
	//-					form elements
	//-
	//--------------------------------------------------------------------------
	createFormTabs : function()
	{
		var cEl = createElement;
		var cInEl = createInputElement;

		var tabsLength = this.config_pages.config_pages.length;

		//- Tabs Container
		this.tabLinksDiv = cEl('div', 'CONFIG_tabs', this.tab_container);
		this.tabLinksDiv.id = 'CONFIG_tabs';
		var tabTabs = this.tabLinksDiv;

		//- Body Container
		var tabBody = this.tabContainerDiv = cEl('div', '', this.container);
		for (var i = 0; i < tabsLength; i++)
		{
			//- Create the unselected and selected tabs
			var selTabContainer   = cEl('div','',tabTabs);
			var unselTabContainer = cEl('div','',tabTabs);
			var bodyTabContainer  = cEl('div','CONFIG_options_container',tabBody);

			currentTabObj = this.config_pages.config_pages[i];
			currentTabId  = currentTabObj.config_page_id;
			this.formTabs[i] = {
				'selTabContainer' 	: selTabContainer,
				'unselTabContainer' : unselTabContainer,
				'bodyTabContainer'  : bodyTabContainer
			};
			this.formTabIdToIndexMap[currentTabId] = i;

			//- First tab looks different
			if(i == 0)
			{
				cEl('div','CONFIG_end_tab_left_selected',selTabContainer);
				cEl('div','CONFIG_tab_left_unselected',unselTabContainer);
			}
			else
			{
				cEl('div','CONFIG_mid_tab_left_selected',selTabContainer);
			}

			var selTextTab   = cEl('div','CONFIG_tab_top_selected',selTabContainer);
			var unselTextTab = cEl('div','CONFIG_tab_top_unselected',unselTabContainer);
			
			//- Last tab looks different
			if(i == tabsLength - 1)
			{
				cEl('div','CONFIG_end_tab_right_selected',selTabContainer);
				cEl('div','CONFIG_tab_right_unselected',unselTabContainer);
				this.formTabs[i].isLastTab = true;
			}
			else
			{
				cEl('div','CONFIG_mid_tab_right_selected',selTabContainer);
				this.formTabs[i].isLastTab = false;
			}


			//- Create the links with the click events
			selTabLink = cEl('a','', selTextTab);
			unselTabLink = cEl('a','', unselTextTab);

			tabClickListener = this.tabClick.bindAsEventListener(this, i);
			Event.observe(unselTabLink, 'click', tabClickListener);

			//- Text for links
//			selTabLink.innerHTML   = currentTabObj.config_page_title;
//			unselTabLink.innerHTML = currentTabObj.config_page_title;

			//this.formTabs[i].linkEl = tabLink;
			if (i == this.currentTabIndex) 
			{
				Element.hide(unselTabContainer);
				Element.show(selTabContainer);
				Element.show(bodyTabContainer);
			} 
			else 
			{
				Element.hide(selTabContainer);
				Element.hide(bodyTabContainer);
				Element.show(unselTabContainer);
			}
		}
		//cEl('br', '', this.container);

		//- Create the buttons
		var butArea = cEl('div','',this.container);
		butArea.id = 'CONFIG_tab_button_area';

		var butCon = this.buttonContainer = cEl('div','CONFIG_button_container',butArea);

		this.nextButton 		= cEl('div','CONFIG_next_button',butCon);
		this.backButton 		= cEl('div','CONFIG_previous_button',butCon);
		this.addToCartButton	= cEl('div','CONFIG_add2cart_button',butCon);

		Element.hide(this.nextButton);
		Element.hide(this.backButton);
		Element.hide(this.addToCartButton);

		this.showTabButtons();

		var nextTabListener = this.nextTab.bindAsEventListener(this);
		var previousTabListener = this.previousTab.bindAsEventListener(this);
		var addToCartListener = this.addToOscommerceCart.bindAsEventListener(this);
		Event.observe(this.addToCartButton, 'click', addToCartListener);
		Event.observe(this.nextButton, 'click', nextTabListener);
		Event.observe(this.backButton, 'click', previousTabListener);
	},

	//--------------------------------------------------------------------------
	//- Function:		createTabButtons
	//- Description:	This function creates the next, back, and add to cart buttons
	//-					at the bottom of the page
	//-
	//--------------------------------------------------------------------------
	showTabButtons : function ()
	{
		Element.hide(this.nextButton);
		Element.hide(this.backButton);
		Element.hide(this.addToCartButton);
		
		if (this.currentTabIndex == 0) 
		{
			Element.show(this.nextButton);
		}
		else if (this.formTabs[this.currentTabIndex].isLastTab) 
		{
			Element.show(this.backButton);
			//this.addToCartButton.style.display = '';
		}
		else 
		{
			Element.show(this.nextButton);
			Element.show(this.backButton);
		}
	},

	//--------------------------------------------------------------------------
	//- Function:		tabClick
	//- Description:	What happens when someone clicks your tab
	//-
	//--------------------------------------------------------------------------
	tabClick : function (event, tab_index)
	{
		dump("Click tab" + tab_index);

//		[0,1,2,3,4,5].each(function(index)
//		{
//			Element.hide('tab_showcase_' + index);
//		});
//		Element.show('tab_showcase_' + tab_index);

		//- Hide currently selected tab and body
		Element.hide(this.formTabs[this.currentTabIndex].selTabContainer);
		Element.show(this.formTabs[this.currentTabIndex].unselTabContainer);
		Element.hide(this.formTabs[this.currentTabIndex].bodyTabContainer);

		//- Show selected tab and body
		Element.hide(this.formTabs[tab_index].unselTabContainer);
		Element.show(this.formTabs[tab_index].bodyTabContainer);
		Element.show(this.formTabs[tab_index].selTabContainer);

		//- Set current tab
		this.currentTabIndex = tab_index;
		this.showTabButtons();
	},

	//--------------------------------------------------------------------------
	//- Function:		nextTab
	//- Description:	This function switches to the next tab
	//-
	//--------------------------------------------------------------------------
	nextTab : function (event)
	{
		this.tabClick(event,this.currentTabIndex + 1);
	},

	//--------------------------------------------------------------------------
	//- Function:		previousTab
	//- Description:	This function switches to the previous tab
	//-
	//--------------------------------------------------------------------------
	previousTab : function (event)
	{
		this.tabClick(event,this.currentTabIndex - 1);
	},

	//--------------------------------------------------------------------------
	//- Function:		createForm
	//- Description:	This function creates all the form elements for the configurator
	//-
	//--------------------------------------------------------------------------
	createForm : function()
	{
		//- Local vars
		var confLength = this.config.configuration.length;
		var formTabs = this.formTabs;
		var formMap  = this.formTabIdToIndexMap;
		this.createFormTabs();

		for (var i = 0; i < confLength; i++ )
		{
			this.formChangeListener = this.formChange.bindAsEventListener(this, { form_index : i });
			var currentConfItem = this.config.configuration[i];
			var conf_item_id = currentConfItem.config_item_id;


			this.configItemIdToIndexMap[conf_item_id] = {"index" : i};
			this.configItemIdToIndexMap[conf_item_id].options = [];

            //- Assume there is a container for these options
            var existingContainer = $(currentConfItem.item_help_url + "_container");


			var bodyCon = formTabs[formMap[currentConfItem.config_page_id]].bodyTabContainer;
			var formItem = this.formArr[i] = {
		//		"divEl" : cEl('div', 'CONFIG_option_container ', existingContainer)
				"divEl" : existingContainer
			};
	

			if (currentConfItem.requires_option == 1)
			{
				$h(formItem.divEl);
				formItem.disabled = true;
			}
			else
			{
				formItem.disabled = false;
			}

/*
			formItem.titleH = cEl("span", "configItemTitle", formItem.divEl);
			formItem.titleH.innerHTML = currentConfItem.item_title;


*/
			formItem.contDiv = cEl('div', "configItemOptionDiv", formItem.divEl);
			formItem.tableDiv = cEl('div', "centerTableOptionDiv", formItem.contDiv);
			formItem.msgP = cEl("span", "configItemMessage", formItem.tableDiv);

/*
			formItem.msgP.innerHTML = currentConfItem.item_message + "&nbsp;";
			cEl('br','',formItem.msgP);
			if (currentConfItem.item_help_url != "")
			{
				var helpLink = cEl('a', "", formItem.msgP);
				helpLink.href = "javascript:popupHelp('" + currentConfItem.item_help_url  + "')";
				var helpLinkImg =  cEl('img', "", helpLink);
				helpLinkImg.src = "/sv_images/blue_arrow_right.gif";
				cEl('span','learnMoreText',helpLink).innerHTML = 'Learn More';
			}

			cEl("br", "", formItem.msgP);
*/
			formItem.infoMsg = cEl("span", "configInfoMsg", formItem.msgP);
			formItem.infoMsg.style.display = "none";


			//- Render the table for the config options
			var tableBorder = cEl('div','configOptionsTableOutline', formItem.tableDiv);
			formItem.tableEl = cEl('table', 'configOptionsTable', tableBorder);
			var tableBody = cEl('tbody', '',formItem.tableEl);

			var optionsLength = currentConfItem.options.length;
			var currentInputType = this.getConfigInputType(currentConfItem);
	
			// Create select box if we can't have multiple selections	
			if (currentInputType == "select_box")
			{
				var tableRow  = cEl('tr','', tableBody);
				var inputCell = cEl('td','', tableRow);
				formItem.selectBox = cEl('select', '',inputCell);
				Event.observe(formItem.selectBox, 'change', this.formChangeListener);
			}

			formItem.prices = [];
			formItem.names = [];
			formItem.inputs = [];
			formItem.rows = [];
			formItem.priceCells = [];

			// Process each option for this current config item
			for (var j = 0; j < optionsLength; j++)
			{

				var curOption = currentConfItem.options[j];

				this.configItemIdToIndexMap[conf_item_id].options[curOption.config_item_option_id] = j;
				
				// get the price and name for this choice
				var price;
				var name;

				if (currentConfItem.config_item_type == 3) 
				{
					name = curOption.name;
					price = "0.00";
				}
				else if (currentConfItem.config_item_type == 0) 
				{
					if (this.customer_reseller_level == 1) {
						price = curOption.options_values_price_tier1;
					} else if (this.customer_reseller_level == 2) {
						price = curOption.options_values_price_tier2;
					} else if (this.customer_reseller_level == 3) {
						price = curOption.options_values_price_tier3;
					} else { 
						price = curOption.options_values_price;
					}
					name = curOption.products_options_values_name;
				} 
				else 
				{
					if (this.customer_reseller_level == 1) {
						price = curOption.products_reseller_price1;
					} else if (this.customer_reseller_level == 2 ) {
						price = curOption.products_reseller_price2;
					} else if (this.customer_reseller_level == 3 ) {
						price = curOption.products_reseller_price3;
					} else {
						price = curOption.products_price;
					}
					name = curOption.products_name;
				}
				price = price.substring(0, price.lastIndexOf("0") - 1);

				formItem.prices[j] = price;
				formItem.names[j] = name;

				if (currentInputType == "select_box")
				{
					formItem.inputs[j] = cEl("option",'',formItem.selectBox);
					formItem.inputs[j].innerHTML = name + " (add " + formatPrice(price)  + ")";

					if (curOption.is_default_option == 1)
					{
						formItem.selectBox.selectedIndex = j;
					}
				}
				else // not a select box so we need a new row for each choice
				{
					var tableRow = formItem.rows[j] = cEl('tr', '', tableBody);

					//- Add mouseover highlighting 
					Event.observe(tableRow, 'mouseover', this.mouseoverRow.bindAsEventListener(this,tableRow));
					Event.observe(tableRow, 'mouseout',  this.mouseoutRow.bindAsEventListener(this,tableRow));

					var inputCell = cEl('td', 'configOptionTitle optionInputCell', tableRow);
					var titleCell = cEl('td', 'configOptionTitle optionLabelCell', tableRow);
					var priceCell = cEl('td', 'configOptionTitle optionPriceCell', tableRow);
					formItem.priceCells[j] = priceCell;

					var productLink;

					if (currentInputType =='text_box') {
						productLink = cEl('a', '', titleCell);
						productLink.setAttribute("href", "javascript:popupProduct(" + curOption.option_id + ")");
					} else {
						productLink = cEl('span', '', titleCell);
					}

					productLink.innerHTML = name;

					if (currentInputType == "text_box")
					{
						formItem.inputs[j] = cInEl(
							"text", 
							"products_id[" + curOption.products_id  + "]", 
							currentConfItem.min_quantity_allowed, "", 
							inputCell);

						formItem.inputs[j].size = 3;
						formItem.inputs[j].maxLength = 3;
						formItem.inputs[j].setAttribute('autocomplete','off');
					

	
						Event.observe(formItem.inputs[j], 'change', this.unfocusTextbox);

						var changeFunc = this.formChangeForText.bindAsEventListener(this, { input : formItem.inputs[j], form_index : i });
						Event.observe(
							formItem.inputs[j], 
							'keypress', 
							this.verifyOnlyNumbers.bindAsEventListener(this,changeFunc)
						);

                        //- Rows click method highlights row and checks the radio box
                        Event.observe(tableRow, 'click', this.phoneRowClick.bindAsEventListener(this, {
                            input : formItem.inputs[j],
                            form_index : i,
							option_id : curOption.option_id
                        }));
						priceCell.innerHTML = "(add " + formatPrice(price) + " each)";
					}
					else if (currentInputType == "check_box")
					{
						//- Create checkbox add onclick event
						formItem.inputs[j] = cInEl(
							"checkbox", 
							"products_id[" + curOption.products_id  + "]",'1',"", 
							inputCell);
						Event.observe(formItem.inputs[j], 'click', this.formChangeListener);

						if (curOption.is_default_option == 1){
							formItem.inputs[j].checked = true;
						}

						//- Rows click method highlights row and checks the check box
						Event.observe(tableRow, 'click', this.optionRowClick.bindAsEventListener(this, {
							input : formItem.inputs[j], 
							form_index : i
						}));
						priceCell.innerHTML = "(add " + formatPrice(price) + ")";
					}
					else if (currentInputType == 'radios')
					{
						formItem.inputs[j] = cInEl(
							"radio", 
							"id[" + curOption.options_id  + "]", 
							curOption.options_values_id, "", 
							inputCell);
						Event.observe(formItem.inputs[j], 'click', this.formChangeListener);

						//- Rows click method highlights row and checks the radio box
						Event.observe(tableRow, 'click', this.optionRowClick.bindAsEventListener(this, {
							input : formItem.inputs[j], 
							form_index : i
						}));

						//- Display price
						priceCell.innerHTML = "(add " + formatPrice(price) + " each)";
						if (curOption.is_default_option == 1 || j == 0){
							formItem.inputs[j].checked = true;
						}
					}

					if (typeof curOption.sub_items != "undefined" && currentInputType != 'radios')
					{
						formItem.inputs[j].sub_items = [];
						var subItemLength = curOption.sub_items.length;

						for (var k = 0; k < subItemLength; k++)
						{
							formItem.inputs[j].sub_items[k] = { "price" : [] };
							formItem.inputs[j].sub_items[k].name = [];
							var subTableRow 	= cEl('tr', '', tableBody);
							var subBlankCell 	= cEl('td', '', subTableRow);
							var subSelectCell 	= cEl('td', '', subTableRow);
							subSelectCell.colSpan = 2;

							formItem.inputs[j].sub_items[k].sub_selectBox = cEl('select', "config_sub_select", subSelectCell);
							Event.observe(formItem.inputs[j].sub_items[k].sub_selectBox, 'change', this.formChangeListener);

							formItem.inputs[j].sub_items[k].sub_selectBox.name = 'id[' + curOption.sub_items[k].options_id + ']';



							var subItemOptionLength = curOption.sub_items[k].sub_item_options.length;
							for (var l = 0; l < subItemOptionLength; l++)
							{
								var currentSubItemOption = curOption.sub_items[k].sub_item_options[l];
								var subOption = 
                        			cEl("option",'',formItem.inputs[j].sub_items[k].sub_selectBox);
								subOption.value = currentSubItemOption.options_values_id;

								var subOptionPrice; 
								if (this.customer_reseller_level == 1) {
                        			subOptionPrice = currentSubItemOption.options_values_price_tier1;
								} else if (this.customer_reseller_level == 2) {
									subOptionPrice = currentSubItemOption.options_values_price_tier2;
								} else if (this.customer_reseller_level == 3) {
									subOptionPrice = currentSubItemOption.options_values_price_tier3;
								} else { 
									subOptionPrice = currentSubItemOption.options_values_price;
								}
								subOptionPrice = subOptionPrice.substring(0, subOptionPrice.lastIndexOf("0") - 1);
								formItem.inputs[j].sub_items[k].price[l] = subOptionPrice;
								formItem.inputs[j].sub_items[k].name[l] = currentSubItemOption.products_options_values_name;

                    			if (currentInputType == "text_box" || currentInputType == 'radios')
								{	
									subOption.innerHTML = currentSubItemOption.products_options_values_name +
										" (add " + formatPrice(subOptionPrice) + " each)";
								}
								else
								{
									subOption.innerHTML = currentSubItemOption.products_options_values_name +
										" (add " + formatPrice(subOptionPrice) + ")";
								}
							}
							cEl('p', "className", subSelectCell);
						}
					}
				}
				formItem.tableDiv = cEl('p', "", formItem.contDiv);
			}

			// if this is a support item, add the auto-renewal check box
		/* This is not needed now, but will be needed when we roll out the new support model

			if (currentConfItem.item_title.indexOf('Support') > -1)
			{
				var tableRow = cEl('tr', "", tableBody);
				var inputCell = cEl('td', "", tableRow);
				inputCell.colSpan = 3;
				formItem.autoRenewCheckBox = cInEl ('checkbox', 1, 1, '', inputCell);
				var qtyTitle = cEl('span', '', inputCell);
				qtyTitle.innerHTML = 'Auto Renew Support';
			}
		*/

			// add qty input box for radios here
			if ( (currentInputType == 'radios' || currentInputType == 'select_box') && currentConfItem.allow_multiple_quantity == 1)
			{
				var tableRow = cEl('tr', "", tableBody);
				var inputCell = cEl('td', "", tableRow);
				inputCell.colSpan = 3;
				//cEl('br', '', inputCell);

				var qtyDiv 	 = cEl('div','users_container',inputCell);
				var qtyTitle = cEl('span', '', qtyDiv);
				

				qtyTitle.innerHTML = '&nbsp;Users: ';
				formItem.qtyBox = cInEl
						("text", conf_item_id, currentConfItem.min_quantity_allowed, "", qtyDiv);

				if (currentConfItem.item_help_url == 'smb_license') {
					formItem.qtyBox.maxLength = 3;
                }
                
				formItem.qtyBox.size = 4;
				formItem.qtyBox.value = 10;
				var span = cEl('span', '', qtyDiv);
				span.innerHTML = '&nbsp;X&nbsp;';
				formItem.eachPriceSpan = cEl('span', '', qtyDiv);
				Event.observe(formItem.qtyBox, 'change', this.formChangeListener);
				Event.observe(formItem.qtyBox, 'keypress', this.verifyOnlyNumbers);

				this._renderRecurringContainer( formItem )

//				var recContainer = cEl('div','',inputCell);
//				var recSpan = cEl('span','',recContainer);
//				recSpan.innerHTML = 'Recurring Cost';

				
			/*	
				var changeFunc = this.formChangeForText.bindAsEventListener(this, { input : formItem.qtyBox, form_index : i });
				Event.observe(
					formItem.qtyBox, 
					'keypress', 
					this.verifyOnlyNumbers.bindAsEventListener(this,changeFunc)
				);*/
			}

			if (currentConfItem.item_help_url == "smb_license")
			{
				this.subscriptionFormItem = formItem;
				this.subscriptionItemIndex = i;
			}

			if (currentConfItem.config_item_type == 3)
			{
				this.renewalItemIndex = i;
			}
		}

		if (typeof this.config.saved_config != "undefined")
		{
			this.loadSavedConfig();
		}

		if (typeof(this.renewalItemIndex) != 'undefined')
		{
			this.updateRenewalPrices(this.renewalItemIndex);
		}

		this.showCurrentConfiguration();
		this.checkAllSelections();
		this.showCurrentConfiguration();
		// for debug, remove before we go live
		//dumpObj(this.formArr);
		//dumpObj(this.configItemIdToIndexMap);
	},



	//--------------------------------------------------------------------------
    //- Function:       l
    //- Description:    This function will set the form values to reflect a saved 
	//-					configuration.
    //-
    //--------------------------------------------------------------------------
	_renderRecurringContainer : function ( formItem )
	{
		//- setup vares
		var recurring_container = $('smb_recurring_container');
		var maintenance = this.maintenance[0];

		//- Render the table for the config options
		var tableBorder	 = cEl('div','configOptionsTableOutline', recurring_container);
		var tableEl = cEl('table', 'configOptionsTable2', tableBorder);
		var tableBody 	 = cEl('tbody', '', tableEl);
		var obj = this;

		this.renewals.each(function(h,index)
		{
			//- Create the list of prices -------------------
			var tableRow = cEl('tr', '', tableBody);
			var inputCell = cEl('td', 'configOptionTitle', tableRow);
			var titleCell = cEl('td', 'configOptionTitle', tableRow);
			var priceCell = cEl('td', 'configOptionTitle', tableRow);

			inputCell.style.cursor = 'default';
			titleCell.style.cursor = 'default';
			priceCell.style.cursor = 'default';

			//- Radio button
			var radio = cInEl("radio",1,h.products_name,"",inputCell);
			radio.disabled = true;


			var price = h.products_price;
			if (obj.customer_reseller_level == 1) {
				price = h.products_reseller_price1;
			} else if (obj.customer_reseller_level == 2) {
				price = h.products_reseller_price2;
			} else if (obj.customer_reseller_level == 3) {
				price = h.products_reseller_price3;
			} else {
				price = h.products_price;
			}
			h.price = price;

			//- Display item and price
			titleCell.innerHTML = h.products_name;
			priceCell.innerHTML = "(" + formatPrice(price) + " each)";

			h.input = radio;
		});

		var titleDiv = cEl('div','options_title', recurring_container).innerHTML = 'Recurring Annual Costs';

		//- Render table for the total breakdown
		var tableBorder	= cEl('div','configOptionsTableOutline', recurring_container);
		var tableEl 	= cEl('table', 'configOptionsTable2', tableBorder);
		var tableBody	= cEl('tbody', '', tableEl);

		//- Subscriptions -----------------
		var subRow 			= cEl('tr', "", tableBody);
		var subTitleCell	= cEl('td', '', subRow);
		var subPriceCell	= cEl('td', '', subRow);

		var qtyTitle = cEl('span','',subTitleCell);
		qtyTitle.innerHTML = '&nbsp;Subscription Renewal (';

		var qtyBox 	= cEl("span", '', subTitleCell);
		var span 	= cEl('span', '', subTitleCell);
		span.innerHTML = '&nbsp;X&nbsp;';
		var eachPriceSpan = cEl('span', '', subTitleCell);


		//- Maintenance  -----------------
		var mainRow 		= cEl('tr', "", tableBody);
		var mainTitleCell 	= cEl('td', '', mainRow);
		var mainPriceCell	= cEl('td', '', mainRow);
		mainTitleCell.innerHTML = '&nbsp;Software Updates / Maintenance';

		var main_price = maintenance.products_price;
		if (this.customer_reseller_level == 1) {
			main_price =  maintenance.products_reseller_price1;
		} else if (this.customer_reseller_level == 2) {
			main_price =  maintenance.products_reseller_price2;
		} else if (this.customer_reseller_level == 3) {
			main_price =  maintenance.products_reseller_price3;
		} else {
			main_price =  maintenance.products_price;
		}
		mainPriceCell.innerHTML = formatPrice(main_price);

		//- Total -----------------------------------
		var totalRow 		= cEl('tr', "", tableBody);
		var totalTitleCell	= cEl('td', 'totalPriceCell', totalRow);
		var totalPriceCell	= cEl('td', 'totalPriceCell', totalRow);
		totalTitleCell.innerHTML = '&nbsp;Annual Total';
		totalPriceCell.innerHTML = '$1,500.00';

		formItem.recurring = { 
			'qtyBox' : qtyBox, 
			'eachPriceSpan' : eachPriceSpan, 
			'subPriceCell' : subPriceCell, 
			'totalPriceCell' : totalPriceCell,
			'main_price' : main_price
		};
	},

	//--------------------------------------------------------------------------
    //- Function:       loadSavedConfig
    //- Description:    This function will set the form values to reflect a saved 
	//-					configuration.
    //-
    //--------------------------------------------------------------------------
	loadSavedConfig : function ()
	{
		var saved_attributes_arr = this.config.saved_config.attributes;
		var saved_products_arr   = this.config.saved_config.products;

		for (var i = 0; i < saved_attributes_arr.length; i++)
		{
			var att = saved_attributes_arr[i];
			var attribute_index = this.configItemIdToIndexMap[att.config_item_id].index;
			var selected_index = this.configItemIdToIndexMap[att.config_item_id].options[att.config_item_option_id];
		
			if (typeof this.formArr[attribute_index].selectBox != 'undefined')
			{
				this.formArr[attribute_index].selectBox.selectedIndex = selected_index;
			}
			else
			{
				this.formArr[attribute_index].inputs[selected_index].checked = true;
			}
		}

		for (var i = 0; i < saved_products_arr.length; i++)
		{
			var prod = saved_products_arr[i];
			var item_index = this.configItemIdToIndexMap[prod.config_item_id].index;

			for (var j = 0; j < prod.config_options.length; j++)
			{
				var prod_option = prod.config_options[j];
				var product_index = 
					this.configItemIdToIndexMap[prod.config_item_id].options[prod_option.config_item_option_id];
				var product_quant = prod_option.quantity;

				if (this.formArr[item_index].inputs[product_index].type == 'text')
				{
					this.formArr[item_index].inputs[product_index].value = product_quant;
				}
				else if (this.formArr[item_index].inputs[product_index].type == 'checkbox')
				{
					this.formArr[item_index].inputs[product_index].checked = true;
				}
				else if (this.formArr[item_index].inputs[product_index].type == 'radio')
				{
					this.formArr[item_index].inputs[product_index].checked = true;
		
					if (typeof this.formArr[item_index].qtyBox != 'undefined') {
						this.formArr[item_index].qtyBox.value = product_quant;
					}
				}
				else if (typeof this.formArr[item_index].selectBox != 'undefined')
				{
					this.formArr[item_index].selectBox.selectedIndex = product_index;
					
					if (typeof this.formArr[item_index].qtyBox != 'undefined') {
						this.formArr[item_index].qtyBox.value = product_quant;
					}
				}

				if (typeof prod_option.sub_attributes != "undefined")
				{
					for (var k = 0; k < prod_option.sub_attributes.length; k++)
					{
						this.formArr[item_index].inputs[product_index].sub_items[prod_option.sub_attributes[k].sub_item_index].sub_selectBox.selectedIndex = 
							prod_option.sub_attributes[k].sub_item_selected_index;
					}
				}
			}
		}
	},

	//--------------------------------------------------------------------------
    //- Function:       checkAllSelections
    //- Description:    This function checks all form elements for dependencies and
	//- 				extra available items, based on currently selected options.
    //-
    //--------------------------------------------------------------------------
	checkAllSelections : function ()
	{
		var confLength = this.config.configuration.length;
        var cEl = createElement;
		dumpObj(this);

        for (var i = 0; i < confLength; i++ )
        {
            var currentConfItem = this.config.configuration[i];

			if (currentConfItem.allow_multiple_selections == 0 && currentConfItem.allow_multiple_quantity == 0)
			{
				this.checkSelectionExtraItems(i, false);
				this.checkSelectionDependencies(i, false);
			}
			this.redrawPriceCells(i);
		}
	},

	//--------------------------------------------------------------------------
    //- Function:       showCurrentConfiguration
    //- Description:    This function checks the form values and displays the
	//-					current configuration and cost to the customer.
    //-
    //--------------------------------------------------------------------------
	showCurrentConfiguration : function ()
	{
		var cEl = createElement;
		var cInEl = createInputElement;

		// remove current configuration
		while(this.sidebar_container.hasChildNodes())
		{
			this.sidebar_container.removeChild(this.sidebar_container.firstChild);
		}

		this.sidebar_container.style.display = "none";

		// display title
		var titleP = cEl("p", 'conf_sidebar_title', this.sidebar_container);
		var	title = cEl("b", "", titleP);
		this.totalPrice = parseFloat(this.product_price);

		title.innerHTML = "Your Configuration:";
		var confTable = cEl("table", 'configTable', this.sidebar_container);
		var confTbody = cEl("tbody", '', confTable);

		var row = cEl('tr', '', confTbody);
		var itemCell = cEl('td', 'configItemCell', row);
		var priceCell = cEl('td', 'configPriceCell', row);

		// display main product name and price
		itemCell.innerHTML = this.config.product_details.products_name;
		priceCell.innerHTML = formatPrice(this.product_price);

		// go through all form elements
		var formLength = this.formArr.length;
		for (var i = 0; i < formLength; i++)	
		{
			var currentConfItem = this.config.configuration[i]
			var currentFormItem = this.formArr[i];
			var currentInputType = this.getConfigInputType(currentConfItem);
			var selectedIndex;

			if (currentInputType == "select_box" || currentInputType == 'radios')
			{
				if (currentInputType == 'select_box') {
					selectedIndex = currentFormItem.selectBox.selectedIndex;
				} 
				else 
				{
					var inputArrLength = currentFormItem.inputs.length;

					for (var j = 0; j < inputArrLength; j++)
					{
						if (currentFormItem.inputs[j].checked == true) {
							selectedIndex = j;
							//this.configSummaryArr[i] = { "selectedRadioIndex" : j };	
						}				
					}
				}

				if (currentConfItem.item_title == 'Select Case')
				{
//					Element.hide('showcase_80');
//					Element.hide('showcase_165');
//					Element.show('showcase_' + currentConfItem.options[selectedIndex].option_id);
				}

				this.configSummaryArr[i] = {"row" : cEl('tr', 'className', confTbody)};
				this.configSummaryArr[i].itemCell = cEl('td', 'configItemCell', this.configSummaryArr[i].row);
				this.configSummaryArr[i].priceCell = cEl('td', 'configPriceCell', this.configSummaryArr[i].row);

				if (currentConfItem.config_item_type == 3)
				{
					this.configSummaryArr[i].renewalRow = cEl('tr', 'className', confTbody);
					this.configSummaryArr[i].renewItemCell = cEl('td', 'configItemCell', this.configSummaryArr[i].renewalRow);
					this.configSummaryArr[i].renewPriceCell = cEl('td', 'configPriceCell', this.configSummaryArr[i].renewalRow);
					this.configSummaryArr[i].renewItemCell.style.paddingLeft = "10px";

					this.configSummaryArr[i].maintRow = cEl('tr', 'className', confTbody);
					this.configSummaryArr[i].maintItemCell = cEl('td', 'configItemCell', this.configSummaryArr[i].maintRow);
					this.configSummaryArr[i].maintPriceCell = cEl('td', 'configPriceCell', this.configSummaryArr[i].maintRow);
					this.configSummaryArr[i].maintItemCell.style.paddingLeft = "10px";
				}

				if (currentConfItem.allow_multiple_quantity == 0)
				{
					this.configSummaryArr[i].itemCell.innerHTML = currentFormItem.names[selectedIndex];

					if (currentConfItem.config_item_type == 3)
					{
						if (selectedIndex != 0)
						{
							var subscriptionQuantity = parseInt($F(this.subscriptionFormItem.qtyBox, 10));
							var subscriptionType = this.getSelectedSubscriptionType();
							var renewalProduct = this.yearly_options[currentConfItem.options[selectedIndex].years][subscriptionType];
							this.configSummaryArr[i].renewItemCell.innerHTML = renewalProduct.products_name + ' x ' + subscriptionQuantity;

							var displayPrice = parseFloat(renewalProduct.products_price * parseFloat(subscriptionQuantity));
							this.configSummaryArr[i].renewPriceCell.innerHTML = formatPrice(displayPrice);

							var maintProduct = this.yearly_options[currentConfItem.options[selectedIndex].years]['maintenance'];
							this.configSummaryArr[i].maintItemCell.innerHTML = maintProduct.products_name;
							this.configSummaryArr[i].maintPriceCell.innerHTML = formatPrice(maintProduct.products_price);
						}
					}
					else
					{
						this.configSummaryArr[i].priceCell.innerHTML = formatPrice(currentFormItem.prices[selectedIndex]);
					}
					this.configSummaryArr[i].price = currentFormItem.prices[selectedIndex];
				}
				else
				{
					var qty = parseInt(currentFormItem.qtyBox.value, 10);
					var price = parseFloat(currentFormItem.prices[selectedIndex] * parseFloat(qty));
					this.configSummaryArr[i].qty = qty;
					this.configSummaryArr[i].itemCell.innerHTML = currentFormItem.names[selectedIndex] + ' x ' + qty;
					this.configSummaryArr[i].priceCell.innerHTML = formatPrice(price);
					this.configSummaryArr[i].price = price;
				}

				if (!currentFormItem.disabled)
				{
					if (parseFloat(currentFormItem.prices[selectedIndex]) <= 0)
					{
						this.configSummaryArr[i].row.style.display = "none";
						
						if (currentConfItem.config_item_type == 3)
						{
							this.configSummaryArr[i].maintRow.style.display = "none";
							this.configSummaryArr[i].renewalRow.style.display = "none";
						}
					}
					this.configSummaryArr[i].disabled = false;
					this.totalPrice += parseFloat(this.configSummaryArr[i].price);
				}
				else
				{
					this.configSummaryArr[i].disabled = true;
					this.configSummaryArr[i].row.style.display = "none";
				}
			}
			else if (currentInputType == "text_box")
			{
				var inputArrLength = currentFormItem.inputs.length;
				this.configSummaryArr[i] = {"items" : []};
				
				for (var j = 0; j < inputArrLength; j++)
				{
					this.configSummaryArr[i].items[j] = {"row" : cEl('tr', 'className', confTbody)};
					this.configSummaryArr[i].items[j].itemCell = cEl('td', 'configItemCell', this.configSummaryArr[i].items[j].row);
					this.configSummaryArr[i].items[j].priceCell = cEl('td', 'configPriceCell', this.configSummaryArr[i].items[j].row);
					var price;

					price = parseFloat(currentFormItem.prices[j]) * parseFloat(currentFormItem.inputs[j].value);
					this.configSummaryArr[i].items[j].itemCell.innerHTML = 
						currentFormItem.names[j] + " x " + parseInt(currentFormItem.inputs[j].value, 10);

					currentFormItem.inputs[j].value = parseInt(currentFormItem.inputs[j].value, 10);	
					this.configSummaryArr[i].items[j].priceCell.innerHTML = formatPrice(price);
					this.configSummaryArr[i].items[j].price = price;
					this.configSummaryArr[i].items[j].qty = parseInt(currentFormItem.inputs[j].value, 10);

					if(!currentFormItem.disabled)
					{
						this.totalPrice += price;	
						this.configSummaryArr[i].disabled = false;
						if (currentFormItem.inputs[j].value <= 0)
						{
							this.configSummaryArr[i].items[j].row.style.display = "none";
						}
					}
					else
					{
						this.configSummaryArr[i].disabled = true;
						this.configSummaryArr[i].items[j].row.style.display = "none";
					}
					this.showCurrentConfigSubItems(currentFormItem, confTbody, i, j, currentFormItem.inputs[j].value);
				}
			}
			else if (currentInputType == "check_box")
			{
				var inputArrLength = currentFormItem.inputs.length;
				this.configSummaryArr[i] = {"items" : []};

				for (var j = 0; j < inputArrLength; j++)
				{
					this.configSummaryArr[i].items[j] = {"row" : cEl('tr', 'className', confTbody)};
					this.configSummaryArr[i].items[j].itemCell = cEl('td', 'configItemCell', this.configSummaryArr[i].items[j].row);
					var price;
					price = parseFloat(currentFormItem.prices[j]);

					if (currentConfItem.item_help_url == 'smb_license_auto_renew')
 					{
						this.configSummaryArr[i].items[j].itemCell.colSpan = 2;
					}
					else
					{
						this.configSummaryArr[i].items[j].priceCell = cEl('td', 'configPriceCell', this.configSummaryArr[i].items[j].row);
						this.configSummaryArr[i].items[j].priceCell.innerHTML = formatPrice(price);
					}
					this.configSummaryArr[i].items[j].itemCell.innerHTML = currentFormItem.names[j];
					this.configSummaryArr[i].items[j].currentCheckedStatus = false;
			
					var qty;
					if (!currentFormItem.disabled)
					{	
						this.configSummaryArr[i].disabled = false;
						if (currentFormItem.inputs[j].checked != true) 
						{
							qty = 0;
							this.configSummaryArr[i].items[j].price = 0;
							this.configSummaryArr[i].items[j].row.style.display = "none";
						}
						else
						{
							this.configSummaryArr[i].items[j].price = price;
							this.totalPrice += price;	
							qty = 1;
						}
					}
					else
					{
						qty = 0;
						this.configSummaryArr[i].items[j].price = 0;
						this.configSummaryArr[i].items[j].row.style.display = "none";
						this.configSummaryArr[i].disabled = true;
					}
					this.showCurrentConfigSubItems(currentFormItem, confTbody, i, j, qty);
				}
			}
		}
		var lineRow = cEl('tr', '', confTbody);
		var lineCell = cEl('td', '', lineRow);
		lineCell.colSpan = 2;
		cEl("hr", "", lineCell);

		var totalRow = cEl('tr', '', confTbody);
		var textCell = cEl('td', 'configItemTotalCell', totalRow);
		this.totalCell = cEl('td', 'configPriceTotalCell', totalRow);
		textCell.innerHTML = "Total Price";
		this.totalCell.innerHTML = formatPrice(this.totalPrice);

		var btnRow = cEl('tr', '', confTbody);
		var btnCell = cEl('td', 'conf_add_button', btnRow);
		btnCell.colSpan = 2;
		var br = cEl('p', '', btnCell);

		var addToCartBtn = cInEl("button",'add2cart','','add_to_cart_btn', btnCell);
		//Event.observe(addToCartBtn, 'click', setActionAndSubmit);
//		var addToCartButton = cEl('div','CONFIG_add2cart_button',btnCell);
		var addToCartListener = this.addToOscommerceCart.bindAsEventListener(this);
		Event.observe(addToCartBtn, 'click', addToCartListener);

		//this.sidebar_container.style.display = "";
		Effect.Appear(this.sidebar_container);
	},

	//--------------------------------------------------------------------------
    //- Function:     	showCurrentConfigSubItems
    //- Description:    This function is called by showCurrentConfiguration.
	//-					It will go through an item and process all its suboptions
	//-					adding their prices to the total and showing them in the
	//-					right column config area if necessary.
    //-
    //--------------------------------------------------------------------------
	showCurrentConfigSubItems : function(currentFormItem, confTbody, itemIndex, inputIndex, qty)
	{
		var cEl = createElement;
		if (typeof currentFormItem.inputs[inputIndex].sub_items != "undefined")
		{
			this.configSummaryArr[itemIndex].items[inputIndex].subItems = [];
			var subItemLength = currentFormItem.inputs[inputIndex].sub_items.length;
			for (var k = 0; k < subItemLength; k++)
			{
				var currentSubItem = currentFormItem.inputs[inputIndex].sub_items[k];
				var selectedSubIndex = currentSubItem.sub_selectBox.selectedIndex;
				var subPrice = parseFloat(currentSubItem.price[selectedSubIndex]) * parseFloat(qty);

				this.configSummaryArr[itemIndex].items[inputIndex].subItems[k] = {"row": cEl('tr', 'className', confTbody)};
				this.configSummaryArr[itemIndex].items[inputIndex].subItems[k].selectedIndex = selectedSubIndex;

				this.configSummaryArr[itemIndex].items[inputIndex].subItems[k].subItemCell =
					cEl('td', 'configItemCell', this.configSummaryArr[itemIndex].items[inputIndex].subItems[k].row);

				this.configSummaryArr[itemIndex].items[inputIndex].subItems[k].subPriceCell =
					cEl('td', 'configPriceCell', this.configSummaryArr[itemIndex].items[inputIndex].subItems[k].row);
				var subPrice;

				this.configSummaryArr[itemIndex].items[inputIndex].subItems[k].subItemCell.innerHTML =
					"&nbsp; - w/ " + currentSubItem.name[selectedSubIndex];

				this.configSummaryArr[itemIndex].items[inputIndex].subItems[k].subPriceCell.innerHTML = formatPrice(subPrice);
				this.configSummaryArr[itemIndex].items[inputIndex].subItems[k].price = subPrice;

				if (!currentFormItem.disabled)
				{
					this.totalPrice += subPrice;
					if (parseInt(subPrice, 10) <= 0)
					{
						this.configSummaryArr[itemIndex].items[inputIndex].subItems[k].row.style.display = "none";
					}
				}
				else
				{
					this.configSummaryArr[itemIndex].items[inputIndex].subItems[k].row.style.display = "none";
				}
			}
		}
	},

	//--------------------------------------------------------------------------
    //- Function:     	addToOscommerceCart
    //- Description:    This function is called to add the current values of 
	//-					the configuration to the customer's osCommerce shopping cart.
    //-
    //--------------------------------------------------------------------------
	addToOscommerceCart : function()
	{
		this.createConfigCartStrings();
        var conf_id;

        if (typeof this.config.saved_config != "undefined") {
			conf_id = this.config.saved_config.config_id;
        } else {
 			conf_id = 0; 
		} 

		var requestUrl = 'configure_add_to_cart.php?prod_attr=' + this.mainProductAttributes 
			+ '&added_prods=' + this.addedProducts + '&product_id=' + this.product_id + '&conf_id=' + conf_id;
		
		var ajaxRequest = new Ajax.Request(requestUrl, {
			onSuccess: function (transport) {
				if (transport.responseText == "OK")
				{
					window.location = "shopping_cart.php";
				}
			}
		});
	},

	//--------------------------------------------------------------------------
    //- Function:      	getSelectedSubscriptionType
    //- Description:    This function gets the subscription type that is currently
	//-					selected in the configuration tool.
    //-
    //--------------------------------------------------------------------------
	getSelectedSubscriptionType : function()
	{
		var subscriptionTypesArr = ['silver', 'gold', 'platinum'];
		var subscriptionTypeIndex;
		var subscriptionType;
		var inputsLength = this.subscriptionFormItem.inputs.length;
                    
		for (var i = 0; i < inputsLength; i++)
		{           
			if (this.subscriptionFormItem.inputs[i].checked == true) { 
				subscriptionTypeIndex = i;
			}       
		}       
        return subscriptionTypesArr[subscriptionTypeIndex];
	},

	//--------------------------------------------------------------------------
    //- Function:      	createConfigCartString	
    //- Description:    This function is called to generate the strings that are passed
	//-					to the PHP function that will add the configuration the customer's
	//-					cart.
    //-
    //--------------------------------------------------------------------------
	createConfigCartStrings : function()
	{
		// go through all form elements
		var formLength = this.formArr.length;
		this.mainProductAttributes = '';
		this.addedProducts = '';

		for (var i = 0; i < formLength; i++)	
		{
			var currentConfItem = this.config.configuration[i]
			var currentFormItem = this.formArr[i];
			var currentInputType = this.getConfigInputType(currentConfItem);

			if (currentFormItem.disabled == true) {
				continue;
			}

			if (currentInputType == "select_box" || currentInputType == 'radios')
			{
				var selectedIndex;
				if (currentInputType == 'select_box')
				{
					selectedIndex = currentFormItem.selectBox.selectedIndex;
				}
				else
				{
					var inputArrLength = currentFormItem.inputs.length;

					for (var j = 0; j < inputArrLength; j++)
					{
						if (currentFormItem.inputs[j].checked == true) {
							selectedIndex = j;
						}				
					}
				}

				if (currentConfItem.config_item_type == 0)
				{
					this.mainProductAttributes += currentConfItem.options[selectedIndex].
options_id + '|' + 
						currentConfItem.options[selectedIndex].options_values_id + '|' +
						currentConfItem.config_item_id + '|' +
						currentConfItem.options[selectedIndex].config_item_option_id + ',';
				} 
				else if (currentConfItem.config_item_type == 3)
				{
					if (selectedIndex != 0)
					{
						var years = currentConfItem.options[selectedIndex].years;
						var subscriptionQuantity = parseInt($F(this.subscriptionFormItem.qtyBox, 10));
						var subscriptionType = this.getSelectedSubscriptionType();

						var renewalProductId = this.yearly_options[years][subscriptionType].products_id;
						var maintProductId = this.yearly_options[years]['maintenance'].products_id;
						
						this.addedProducts += maintProductId + '|1' + '|' +
							currentConfItem.config_item_id + '|' +
							currentConfItem.options[selectedIndex].config_item_option_id + ',';	

						this.addedProducts += renewalProductId + '|' + subscriptionQuantity + '|' +
							currentConfItem.config_item_id + '|' +
							currentConfItem.options[selectedIndex].config_item_option_id + ',';	
					}
				}
				else
				{
					if (currentConfItem.allow_multiple_quantity == 0)
					{
						this.addedProducts += currentConfItem.options[selectedIndex].option_id + '|1' + '|' +
							currentConfItem.config_item_id + '|' +
							currentConfItem.options[selectedIndex].config_item_option_id + ',';	
					}
					else
					{
						var quantity = parseInt(currentFormItem.qtyBox.value, 10);
						this.addedProducts += currentConfItem.options[selectedIndex].option_id + '|' + quantity + '|' +
							currentConfItem.config_item_id + '|' +
							currentConfItem.options[selectedIndex].config_item_option_id;	
					}
				}

/* This is not needed now but will be needed when we roll out our new support model
				if (typeof currentFormItem.autoRenewCheckBox != 'undefined')
				{
					this.addedProducts += '-';
					if (currentFormItem.autoRenewCheckBox.checked) {
						this.addedProducts += '10|39|' + currentConfItem.config_item_id + '|-1|-1|-1*';
					} else {
						this.addedProducts += '10|40|' + currentConfItem.config_item_id + '|-1|-1|-1*';
					}
				}
*/
				this.addedProducts += ',';
			}
			else if (currentInputType == "text_box")
			{
				var inputArrLength = currentFormItem.inputs.length;

				for (var j = 0; j < inputArrLength; j++)
				{
					if (currentFormItem.inputs[j].value > 0)
					{
						var quantity = parseInt(currentFormItem.inputs[j].value, 10);

						if (quantity > 0)
						{
							var productId = currentConfItem.options[j].option_id;	
							this.addedProducts += productId + '|' + quantity + '|' +
								currentConfItem.config_item_id + '|' +
								currentConfItem.options[j].config_item_option_id;

							if (typeof currentFormItem.inputs[j].sub_items != "undefined")
							{
								var subItemArrLength = currentFormItem.inputs[j].sub_items.length;
								this.addedProducts += '-';

								for (var k = 0; k < subItemArrLength; k++)
								{
									var subSelectedIndex = currentFormItem.inputs[j].sub_items[k].sub_selectBox.selectedIndex;
									this.addedProducts += currentConfItem.options[j].sub_items[k].options_id + '|' +
										currentConfItem.options[j].sub_items[k].sub_item_options[subSelectedIndex].options_values_id + 
										'|' + currentConfItem.config_item_id + '|' + j + '|' + k + '|' + subSelectedIndex + '*';
								}
							}
							this.addedProducts += ',';
						}
					}
				}
			}
			else if (currentInputType == "check_box")
			{
				var inputArrLength = currentFormItem.inputs.length;

				for (var j = 0; j < inputArrLength; j++)
				{
					if (currentFormItem.inputs[j].checked == true)
					{
						this.addedProducts += currentConfItem.options[j].option_id + '|1' + '|' +
							currentConfItem.config_item_id + '|' +
							currentConfItem.options[j].config_item_option_id;	

						if (typeof currentFormItem.inputs[j].sub_items != "undefined")
						{
							var subItemArrLength = currentFormItem.inputs[j].sub_items.length;
							this.addedProducts += '-';

							for (var k = 0; k < subItemArrLength; k++)
							{
								var subSelectedIndex = currentFormItem.inputs[j].sub_items[k].sub_selectBox.selectedIndex;
								this.addedProducts += currentConfItem.options[j].sub_items[k].options_id + '|' +
									currentConfItem.options[j].sub_items[k].sub_item_options[subSelectedIndex].options_values_id + '|' +
									currentConfItem.config_item_id + '|' + j + '|' + k + '|' + subSelectedIndex + '*';
							}
						}
						this.addedProducts += ',';
					}
				}
			}
		}
	},

	phoneRowClick : function(event, options)
	{
		var input = options.input;
		var form_index = options.form_index;
		dump('Conf Item ID' + options.option_id);
//		if(options.option_id == 41 ) 		{ carousel.scrollTo(0); }
		if(options.option_id == 120 ) 		{ carousel.scrollTo(0); }
		else if(options.option_id == 83) 	{ carousel.scrollTo(1); }
		else if(options.option_id == 42) 	{ carousel.scrollTo(2); }
//		else if(options.option_id == 52) 	{ carousel.scrollTo(4); }
		else if(options.option_id == 112) 	{ carousel.scrollTo(3); }
		else if(options.option_id == 256) 	{ carousel.scrollTo(4); }
		else if(options.option_id == 106) 	{ carousel.scrollTo(5); }
		else if(options.option_id == 318) 	{ carousel.scrollTo(6); }
		else if(options.option_id == 320) 	{ carousel.scrollTo(7); }
		else if(options.option_id == 321) 	{ carousel.scrollTo(8); }
//		else if(options.option_id == 77) 	{ carousel.scrollTo(6); }
//		else if(options.option_id == 53) 	{ carousel.scrollTo(7); }
		else if(options.option_id == 322) 	{ carousel.scrollTo(9); }
		else if(options.option_id == 323) 	{ carousel.scrollTo(10); }
		else if(options.option_id == 40) 	{ carousel.scrollTo(11); }
		return;
	},



	optionRowClick : function(event, options)
	{
		var input = options.input;
		var form_index = options.form_index;

		if(input.disabled == true)
		{
			return;
		}

		//input.checked = true;
		this.formChange(event, options);
		return;
	},

	//--------------------------------------------------------------------------
    //- Function:      	formChange 
    //- Description:    This function is called when a form element changes. It 
	//-					calls other functions that check dependencies and also 
	//-					the function to update the current config display.
    //-
    //--------------------------------------------------------------------------
	formChange : function(event, options)
	{
		dump("Running formChange");
		var form_index = options.form_index;
		var input = options.input;

		//- Check if the input was sent or row was clicked
		if(!isDefined(input))
		{
			input = Event.element(event);
			if(input.type == 'checkbox' && isIE)
			{
				input.checked = (input.checked) ? false : true;
			}
		}
		else
		{
			//- Row click then, toggle checkbox
			if(input.type == 'checkbox')
			{
				input.checked = (input.checked) ? false : true;
			}
		}

		if(input.type == 'radio')
		{
			input.checked = true;
		}

		//- Run the computational stuff
		this.checkSelectionDependencies(form_index, true);
		this.checkSelectionExtraItems(form_index, true);
		this.checkAllowedQuantity(form_index);

		if (form_index == this.subscriptionItemIndex)
		{
			this.updateRenewalPrices(this.renewalItemIndex);
			this.updateCurrentConfiguration(this.renewalItemIndex);
			this.redrawPriceCells(this.renewalItemIndex);
		}

		this.updateCurrentConfiguration(form_index);
		this.redrawPriceCells(form_index);

		//- If event is triggered by radio, don't propagate to the row.click
		if(!isIE)
		{
			if (isSafari)
			{
				event.cancelBubble = true;
				event.stopPropagation();
			}
			else
			{
				event.preventDefault = false;
				Event.stop(event);
			}
		}
		return;
	},


	//--------------------------------------------------------------------------
    //- Function:      	formChange 
    //- Description:    This function is called when a form element changes. It 
	//-					calls other functions that check dependencies and also 
	//-					the function to update the current config display.
    //-
    //--------------------------------------------------------------------------
	formChangeForText : function(num,options)
	{
		dump("Running formChangeForText");
		var form_index = options.form_index;
		var input = options.input;

		if(input.value == '')
		{
			return;
		}

		//- Run the computational stuff
		this.checkSelectionDependencies(form_index, true);
		this.checkSelectionExtraItems(form_index, true);
		this.checkAllowedQuantity(form_index);

		if (form_index == this.subscriptionItemIndex)
		{
			this.updateRenewalPrices(this.renewalItemIndex);
			this.updateCurrentConfiguration(this.renewalItemIndex);
			this.redrawPriceCells(this.renewalItemIndex);
		}

		this.updateCurrentConfiguration(form_index);
		this.redrawPriceCells(form_index);
	},

	//--------------------------------------------------------------------------
    //- Function:      	formChange 
    //- Description:    When you leave a textbox it resets value to 0 
    //--------------------------------------------------------------------------
	unfocusTextbox : function(event)
	{
		var input = Event.element(event);
        if(input.value == '')
        {
            input.value = '0';
			Event.stop(event);
            return;
        }
	},

	//--------------------------------------------------------------------------
    //- Function:       checkAllowedQuantity
    //- Description:    This function is called when the form is updated. It checks
	//- 				if this item has a minimun required quantity or a maximum allowed, 
    //-                 and if it does it makes sure the quantity entered satisfies this condition.
    //-
    //--------------------------------------------------------------------------
	checkAllowedQuantity: function (form_index)
	{
		var currentConfItem = this.config.configuration[form_index];
		var currentFormItem = this.formArr[form_index];
        var errorMsg;
        var errorFound = false;
  
		if (typeof currentFormItem.qtyBox != 'undefined')
		{
			if (currentFormItem.qtyBox.value == '' || isNaN (currentFormItem.qtyBox.value))
			{
				currentFormItem.qtyBox.value = currentConfItem.min_quantity_allowed;
			}

			if (parseInt(currentFormItem.qtyBox.value, 10) < parseInt(currentConfItem.min_quantity_allowed, 10))
			{
				currentFormItem.qtyBox.value = currentConfItem.min_quantity_allowed;
                errorMsg = '<img src="/catalog/images/info_16.gif"> This item' +
                           ' requires a minimum quantity of ' + currentConfItem.min_quantity_allowed + '.';
                errorFound = true;
            }
            else if (parseInt(currentConfItem.max_quantity_allowed, 10) > 0 &&
						parseInt(currentFormItem.qtyBox.value, 10) > parseInt(currentConfItem.max_quantity_allowed, 10))
            {
				currentFormItem.qtyBox.value = currentConfItem.max_quantity_allowed;
                errorMsg = '<img src="/catalog/images/info_16.gif"> This item' +
                           ' allows a maximum quantity of ' + currentConfItem.max_quantity_allowed + '.';
                errorFound = true;
            }

            if (errorFound)
			{
				currentFormItem.infoMsg.innerHTML = errorMsg;
				if (currentFormItem.infoMsg.style.display == 'none')
				{
					Effect.Appear(currentFormItem.infoMsg,
						{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });
				}
				else
				{
					new Effect.HighlightText(currentFormItem.infoMsg, {startcolor:'#990000', endcolor:'#CC9900'});
				}
			}
			else
			{
				if (currentFormItem.infoMsg.style.display != 'none')
				{
					Effect.Fade(currentFormItem.infoMsg,
						{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });
				}
			}
		}
	},

	//--------------------------------------------------------------------------
    //- Function:       updateCurrentConfiguration
    //- Description:    This function is called when the form is updated. It checks
	//-					the changes made on the form and updates the current
	//-					configuration display accordingly.
    //-
    //--------------------------------------------------------------------------
	updateCurrentConfiguration: function (form_index)
	{
		var currentConfItem = this.config.configuration[form_index];
		var currentFormItem = this.formArr[form_index];
		var currentConfigDisplay = this.configSummaryArr[form_index];
		var currentInputType = this.getConfigInputType(currentConfItem);

		dump("updating the current config");

		if (currentInputType == "select_box" || currentInputType == 'radios')
		{
			var selectedIndex;

			if (currentInputType == 'select_box') { 
				selectedIndex = currentFormItem.selectBox.selectedIndex;
			} 
			else 
			{
				var inputsLength = currentFormItem.inputs.length;
			
				for (var i = 0; i < inputsLength; i++)
				{
					if (currentFormItem.inputs[i].checked == true) {
						selectedIndex = i;
					}
				}
			}

			if (currentConfItem.item_title == 'Select Case')
			{
//				Element.hide('showcase_80');
//				Element.hide('showcase_165');
//				Element.show('showcase_' + currentConfItem.options[selectedIndex].option_id);
			}

			if (currentFormItem.disabled)
			{
				if (!currentConfigDisplay.disabled)
				{
					this.totalPrice -= parseFloat(currentConfigDisplay.price);
					Effect.Fade(currentConfigDisplay.row, {
                        duration : .5,
                        queue: { position:'end', scope: 'configScope' , limit:5 }
					});
				}
				currentConfigDisplay.disabled = true;
			}
			else
			{
				if (currentConfItem.allow_multiple_quantity == 0)
				{
					var newPrice = currentFormItem.prices[selectedIndex];
					var newText = currentFormItem.names[selectedIndex];	
				}
				else
				{
					var newQty = parseInt(currentFormItem.qtyBox.value, 10);
					var newPrice = parseFloat(currentFormItem.prices[selectedIndex] * parseFloat(newQty));
					var newText = currentFormItem.names[selectedIndex] + ' x ' + newQty;
					currentConfigDisplay.qty = newQty;
				}

				if (!currentConfigDisplay.disabled) {
					this.totalPrice -= parseFloat(currentConfigDisplay.price);
				}
				currentConfigDisplay.disabled = false;
				//change title and price
				this.totalPrice += parseFloat(newPrice);
				currentConfigDisplay.price = parseFloat(newPrice);

				if (parseInt(newPrice, 10) > 0) {
					currentConfigDisplay.itemCell.innerHTML = newText;

					if (currentConfItem.config_item_type == 3)
					{
						var subscriptionQuantity = parseInt($F(this.subscriptionFormItem.qtyBox, 10));
						var subscriptionType = this.getSelectedSubscriptionType();
						var renewalProduct = this.yearly_options[currentConfItem.options[selectedIndex].years][subscriptionType];
						currentConfigDisplay.renewItemCell.innerHTML = renewalProduct.products_name + ' x ' + subscriptionQuantity;

						var displayPrice = parseFloat(renewalProduct.products_price * parseFloat(subscriptionQuantity));
						currentConfigDisplay.renewPriceCell.innerHTML = formatPrice(displayPrice);

						var maintProduct = this.yearly_options[currentConfItem.options[selectedIndex].years]['maintenance'];
						currentConfigDisplay.maintItemCell.innerHTML = maintProduct.products_name;
						currentConfigDisplay.maintPriceCell.innerHTML = formatPrice(maintProduct.products_price);
					}
					else
					{
						currentConfigDisplay.priceCell.innerHTML = formatPrice(parseFloat(newPrice));
					}

					dump("Runing configScope Fade");

//					if (currentConfigDisplay.row.style.display == "none") {
						Effect.Appear(currentConfigDisplay.row, 
							{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });
//					} else {
						new Effect.HighlightText(currentConfigDisplay.row, {
							startcolor:'#CC6600', endcolor:'#663300', 
							duration : .5, queue: { position:'end', scope: 'configHighlightScope' , limit:5 }
						});

						if (currentConfItem.config_item_type == 3)
						{
							Effect.Appear(currentConfigDisplay.renewalRow, 
								{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });

							new Effect.HighlightText(currentConfigDisplay.renewalRow, {
								startcolor:'#CC6600', endcolor:'#663300', 
								duration : .5, queue: { position:'end', scope: 'configHighlightScope' , limit:5 }
							});

							Effect.Appear(currentConfigDisplay.maintRow, 
								{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });

							new Effect.HighlightText(currentConfigDisplay.maintRow, {
								startcolor:'#CC6600', endcolor:'#663300', 
								duration : .5, queue: { position:'end', scope: 'configHighlightScope' , limit:5 }
							});
						}
//					}
				} else {
					dump("Runing configScope Fade2");
						if (currentConfItem.config_item_type == 3)
						{
							Effect.Fade( currentConfigDisplay.maintRow,{
	                    	    duration : .5, queue: { position:'end', scope: 'configScope' , limit:5 }
							});

							Effect.Fade( currentConfigDisplay.renewalRow,{
	                    	    duration : .5, queue: { position:'end', scope: 'configScope' , limit:5 }
							});
						}

					//if (currentConfigDisplay.row.style.display != "none") {
						Effect.Fade( currentConfigDisplay.row,{
	                        duration : .5, queue: { position:'end', scope: 'configScope' , limit:5 }
						});
					//} 
				} 
			}	
		}
		else if (currentInputType == "text_box")
		{
			var inputsLength = currentFormItem.inputs.length;

			for (var i = 0; i < inputsLength; i++)
			{
				if (currentFormItem.disabled)
				{
					this.totalPrice -= parseFloat(currentConfigDisplay.items[i].price);
					currentConfigDisplay.items[i].price = 0;
					currentConfigDisplay.items[i].qty = 0;
			
					if (currentConfigDisplay.items[i].row.style.display != "none")
					{
						Effect.Fade(currentConfigDisplay.items[i].row,
							{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });
						this.subtractAndFadeSubItems(currentFormItem, currentConfigDisplay, i);
					}
				}
				else
				{
					var newInputValue = parseInt(currentFormItem.inputs[i].value, 10);
					var newInputPrice = parseFloat(newInputValue) * parseFloat(currentFormItem.prices[i]);

					if (newInputValue == 0)
					{
						if (parseInt(currentConfigDisplay.items[i].price, 10) != 0)
						{
							//change price
							this.totalPrice -= parseFloat(currentConfigDisplay.items[i].price);
							currentConfigDisplay.items[i].price = newInputPrice;
							currentConfigDisplay.items[i].qty = newInputValue;
						}
						if (currentConfigDisplay.items[i].row.style.display != "none")
						{
							// make row and subrows disappear
							Effect.Fade(currentConfigDisplay.items[i].row,
								{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });
							this.subtractAndFadeSubItems(currentFormItem, currentConfigDisplay, i);
						}
					}
					else
					{
						var itemWasHidden = false;
						var hasNewQty = false;
						if (parseFloat(newInputPrice) != parseFloat(currentConfigDisplay.items[i].price))
						{
							this.totalPrice -= parseFloat(currentConfigDisplay.items[i].price);
							this.totalPrice += parseFloat(newInputPrice);
							currentConfigDisplay.items[i].priceCell.innerHTML = formatPrice(newInputPrice);
							currentConfigDisplay.items[i].itemCell.innerHTML = currentFormItem.names[i] + " x " + newInputValue;

							if (currentConfigDisplay.items[i].qty == 0)
							{
								//make row appear
								itemWasHidden = true;
								Effect.Appear(currentConfigDisplay.items[i].row,
									{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });
							} else {
								new Effect.HighlightText(currentConfigDisplay.items[i].row, {startcolor:'#CC6600', endcolor:'#663300'});
							}
						}

						if (newInputValue != currentConfigDisplay.items[i].qty) {
							hasNewQty = true;
						}
						currentConfigDisplay.items[i].qty   = newInputValue;
						currentConfigDisplay.items[i].price = parseFloat(newInputPrice);
						this.addAndShowSubItems(currentFormItem, currentConfigDisplay, i, itemWasHidden, hasNewQty, newInputValue);
					}
				}
			}
		}
		else if (currentInputType == "check_box")
		{
			var inputsLength = currentFormItem.inputs.length;

			for (var i = 0; i < inputsLength; i++)
			{
				if (currentFormItem.disabled)
                {
                    this.totalPrice -= parseFloat(currentConfigDisplay.items[i].price);
                    currentConfigDisplay.items[i].price = 0;
                    currentConfigDisplay.items[i].qty = 0;

                    if (currentConfigDisplay.items[i].row.style.display != "none")
                    {
                        Effect.Fade(currentConfigDisplay.items[i].row,
							{ duration : .5,queue: { position:'end', scope: 'configCBScope' , limit:5 } });
						this.subtractAndFadeSubItems(currentFormItem, currentConfigDisplay, i);
                    }
                }
                else
                {
					if (!currentFormItem.inputs[i].checked)
					{
						var newInputPrice = parseFloat(0);

						if (parseInt(currentConfigDisplay.items[i].price, 10) != 0)
						{
							//change price
							this.totalPrice -= parseFloat(currentConfigDisplay.items[i].price);
							currentConfigDisplay.items[i].price = newInputPrice;
						}
						if (currentConfigDisplay.items[i].row.style.display != "none")
						{
							currentConfigDisplay.items[i].row.isFading = true;
							// make row and subrows disappear
							Effect.Fade(currentConfigDisplay.items[i].row,
								{ duration : .5,queue: { position:'end', scope: 'configCBScope' , limit:5 } });
							this.subtractAndFadeSubItems(currentFormItem, currentConfigDisplay, i);
						}
					}
					else
					{
						var newInputPrice = parseFloat(currentFormItem.prices[i]);
						var itemWasHidden = false;

						if (parseFloat(newInputPrice) != parseFloat(currentConfigDisplay.items[i].price) ||
                        		currentConfItem.item_help_url == 'smb_license_auto_renew')
						{
							this.totalPrice -= parseFloat(currentConfigDisplay.items[i].price);
							this.totalPrice += parseFloat(newInputPrice);
							currentConfigDisplay.items[i].itemCell.innerHTML = currentFormItem.names[i];

							if (currentConfItem.item_help_url != 'smb_license_auto_renew') 
							{
								currentConfigDisplay.items[i].priceCell.innerHTML = formatPrice(newInputPrice);
							}

							if (currentConfigDisplay.items[i].row.style.display == "none" || currentConfigDisplay.items[i].row.isFading)
							{
								//make row appear
								itemWasHidden = true;
								currentConfigDisplay.items[i].row.isFading = false;
								Effect.Appear(currentConfigDisplay.items[i].row,
									{ duration : .5,queue: { position:'end', scope: 'configCBScope' , limit:5 } });
							}
							else {
								new Effect.HighlightText(currentConfigDisplay.items[i].row, {startcolor:'#CC6600', endcolor:'#663300'});
							}
						}
						currentConfigDisplay.items[i].price = parseFloat(newInputPrice);
						this.addAndShowSubItems(currentFormItem, currentConfigDisplay, i, itemWasHidden, false, 1);
					}
				}
			}
		}
		this.totalCell.innerHTML =  formatPrice(parseFloat(this.totalPrice));
	},

	//--------------------------------------------------------------------------
    //- Function:       addAndShowSubItems
    //- Description:    This function will go through all the sub items of a text
	//-					or check box inputs and add their values to the
	//-					configuration total if necessary. It will also make them
	//-					appear in the current config if necessary.
    //-
    //--------------------------------------------------------------------------
	addAndShowSubItems : function(currentFormItem, currentConfigDisplay, inputIndex, itemWasHidden, hasNewQty, newInputValue)
	{
		if (typeof currentFormItem.inputs[inputIndex].sub_items != "undefined")
		{
		    var subItemsLength = currentFormItem.inputs[inputIndex].sub_items.length;
		    for (var j = 0; j < subItemsLength; j++)
		    {
		        var subItemSelectedIndex = currentFormItem.inputs[inputIndex].sub_items[j].sub_selectBox.selectedIndex;
		        if (subItemSelectedIndex != currentConfigDisplay.items[inputIndex].subItems[j].selectedIndex 
						|| itemWasHidden || hasNewQty)
        		{
		            currentConfigDisplay.items[inputIndex].subItems[j].selectedIndex = subItemSelectedIndex;
        		    var newSubPrice = parseFloat(currentFormItem.inputs[inputIndex].sub_items[j].price[subItemSelectedIndex]) *
		                parseFloat(newInputValue);

        		    this.totalPrice -= parseFloat(currentConfigDisplay.items[inputIndex].subItems[j].price);
		            this.totalPrice += newSubPrice;

        		    currentConfigDisplay.items[inputIndex].subItems[j].price = newSubPrice
		            currentConfigDisplay.items[inputIndex].subItems[j].selectedIndex = subItemSelectedIndex;

        		    if (parseInt(newSubPrice, 10) > 0)
		            {
        		        currentConfigDisplay.items[inputIndex].subItems[j].subPriceCell.innerHTML =  formatPrice(newSubPrice);
		                currentConfigDisplay.items[inputIndex].subItems[j].subItemCell.innerHTML =
        		            "&nbsp; - w/" +currentFormItem.inputs[inputIndex].sub_items[j].name[subItemSelectedIndex]

		                if (currentConfigDisplay.items[inputIndex].subItems[j].row.style.display == "none") {
                		    Effect.Appear(currentConfigDisplay.items[inputIndex].subItems[j].row,
								{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });
        		        } else {
		                    new Effect.HighlightText(currentConfigDisplay.items[inputIndex].subItems[j].row, 
								{startcolor:'#CC6600', endcolor:'#663300'});
                		}
        		    } else {
		                if (currentConfigDisplay.items[inputIndex].subItems[j].row.style.display != "none") {
                		    Effect.Fade(currentConfigDisplay.items[inputIndex].subItems[j].row,
							{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });
        		        }
		            }
        		}
			}
		}
	},

	//--------------------------------------------------------------------------
    //- Function:       subtractAndFadeSubItems
    //- Description:    This function will go through all the sub items of a text
	//-					or check box inputs and subtract their values from the
	//-					configuration total if necessary. It will also fade them
	//-					out from displaying in the current config if necessary.
    //-
    //--------------------------------------------------------------------------
	subtractAndFadeSubItems : function(currentFormItem, currentConfigDisplay, inputIndex)
	{
		if (typeof currentFormItem.inputs[inputIndex].sub_items != "undefined")
		{                           
			var subItemsLength = currentFormItem.inputs[inputIndex].sub_items.length;
			for (var j = 0; j < subItemsLength; j++)
			{                           
				this.totalPrice -= parseFloat(currentConfigDisplay.items[inputIndex].subItems[j].price);
				currentConfigDisplay.items[inputIndex].subItems[j].price = 0;

				if (currentConfigDisplay.items[inputIndex].subItems[j].row.style.display != "none") {
				Effect.Fade(currentConfigDisplay.items[inputIndex].subItems[j].row,
					{ duration : .5,queue: { position:'end', scope: 'configScope' , limit:5 } });
				}   
			}                           
		} 
	},

	//--------------------------------------------------------------------------
    //- Function:       redrawPriceCells
    //- Description:    This function will update the prices of all radio and checkbox items
	//-					to show the effect the selection will have on the total price
	//-					instead of the cost of the selection
    //-
    //--------------------------------------------------------------------------
	redrawPriceCells : function (form_index)
	{
		var currentConfItem = this.config.configuration[form_index];
		var currentConfSummary = this.configSummaryArr[form_index];
		var selectedItemPrice = currentConfSummary.price;
		var currentFormItem = this.formArr[form_index];
		var currentInputType = this.getConfigInputType(currentConfItem);
		var inputsLength = currentFormItem.inputs.length;
		var chosenRadioIndex;

		if (currentConfItem.config_item_type == 3)
		{
			this.updateRenewalPrices(form_index);
		}

		if (currentInputType == 'check_box' || currentInputType == 'radios')
		{
			if (currentInputType == 'radios' && currentConfItem.allow_multiple_quantity == 1)
			{
				var eachPrice = selectedItemPrice / parseInt(currentFormItem.qtyBox.value, 10);
				currentFormItem.eachPriceSpan.innerHTML = formatPrice(eachPrice) + " each = " + formatPrice(selectedItemPrice) + " total";

				//-- dWP added for recurring, not elegant
				if(isDefined(currentFormItem.recurring))
				{
					var rec = currentFormItem.recurring;
					var renewals = this.renewals;

					//- Figure out which one is selected
					var product;
                	for (var i = 0; i < inputsLength; i++)
	                {
    	                if ( currentFormItem.inputs[i].checked == true) 
						{
        	               product = currentConfItem.options[i];
            	        }
                	}

					//- Go through each and figure out what was selected
					renewals.each(function(h,index){
						if(typeof(product) != 'undefined' && h.match_id == product.products_id)
						{
							h.input.checked = true;
							var val = rec.qtyBox.innerHTML = currentFormItem.qtyBox.value;
							rec.eachPriceSpan.innerHTML = formatPrice(h.price) + ")";

							var subTotal = h.price*val;
							rec.subPriceCell.innerHTML = formatPrice(subTotal);
							rec.totalPriceCell.innerHTML = formatPrice(parseInt(subTotal, 10) + parseInt(rec.main_price, 10));
						}
					});
				}
				//-- ENd dwp
			}

			for (var i = 0; i < inputsLength; i++)
			{
				if (currentInputType == 'check_box')
				{
					if (currentConfItem.item_help_url == 'smb_license_auto_renew')
					{
						currentFormItem.priceCells[i].innerHTML = '';
						if (currentFormItem.inputs[i].checked == true)
						{
							this.addOptionRowHighlight(currentFormItem.rows[i]);
						}
						else
						{
							this.removeOptionRowHighlight(currentFormItem.rows[i]);
						}
					}
					else
					{
						if (currentFormItem.inputs[i].checked == true)
						{
							currentFormItem.priceCells[i].innerHTML = '<b>(included in price)</b>';
							this.addOptionRowHighlight(currentFormItem.rows[i]);
						} 
						else 
						{
							var price = currentFormItem.prices[i];
							currentFormItem.priceCells[i].innerHTML = '(add ' +  formatPrice(price) + ')';
							this.removeOptionRowHighlight(currentFormItem.rows[i]);
						}
					}
				}
				else
				{
					if (currentConfItem.allow_multiple_quantity == 1) {
						var priceDifference = (currentFormItem.prices[i] * currentFormItem.qtyBox.value) - selectedItemPrice;
 					} else {
						var priceDifference = currentFormItem.prices[i] - selectedItemPrice;
					}

					if (priceDifference < 0) { 
						currentFormItem.priceCells[i].innerHTML = '(subtract ' + formatPrice(Math.abs(priceDifference)) + ')';
					} else if (priceDifference == 0 && currentFormItem.inputs[i].checked == true) {
						currentFormItem.priceCells[i].innerHTML = '<b>(included in price)</b>';
					} else if (priceDifference == 0 && currentFormItem.inputs[i].checked == false) {
						currentFormItem.priceCells[i].innerHTML = '(add $0.00)';
					} else {
						currentFormItem.priceCells[i].innerHTML = '(add ' +  formatPrice(priceDifference) + ')';
					}

					if (currentFormItem.inputs[i].checked == true)
					{
						this.addOptionRowHighlight(currentFormItem.rows[i]);
					} 
					else 
					{
						this.removeOptionRowHighlight(currentFormItem.rows[i]);
					}
				}
			}
		}
	},

	addOptionRowHighlight : function (row)
	{
		var cells = row.getElementsByTagName('td');
		$A(cells).each(function(e) 
		{ 
			Element.addClassName(e,'selected'); 
		});
		return;
	},

	removeOptionRowHighlight : function (row)
	{
		var cells = row.getElementsByTagName('td');
		$A(cells).each(function(e) 
		{ 
			Element.removeClassName(e,'selected'); 
		});
		return;
	},

	//--------------------------------------------------------------------------
    //- Function:       updateRenewalPrices
    //- Description:    This function checks the quantity and type of subscriptions
	//-					that have been selected and based on that determines the cost
	//-					of a 1 year, 2 year and 4 year upfront renewal, as well as
	//-					what types of renewals need to be purchased.
    //-
    //--------------------------------------------------------------------------
	updateRenewalPrices : function (renewalItemIndex)
	{
		var renewalConfItem = this.config.configuration[renewalItemIndex];
		var renewalFormItem = this.formArr[renewalItemIndex];
		var subscriptionQuantity = parseInt($F(this.subscriptionFormItem.qtyBox, 10));
		var subscriptionType = this.getSelectedSubscriptionType();

		renewalFormItem.prices[1] =  parseFloat(this.yearly_options[1]['maintenance'].products_price, 10) + 
							subscriptionQuantity * parseFloat(this.yearly_options[1][subscriptionType].products_price, 10);

		renewalFormItem.prices[2] =  parseFloat(this.yearly_options[2]['maintenance'].products_price, 10) + 
							subscriptionQuantity * parseFloat(this.yearly_options[2][subscriptionType].products_price, 10);

		renewalFormItem.prices[3] =  parseFloat(this.yearly_options[4]['maintenance'].products_price, 10) + 
							subscriptionQuantity * parseFloat(this.yearly_options[4][subscriptionType].products_price, 10);
	},

	//--------------------------------------------------------------------------
    //- Function:       checkSelectionExtraItems
    //- Description:    This function is called to check if the selection of an 
	//-					option in a drop down should trigger the availability of
	//-					additional config items.
    //-
    //--------------------------------------------------------------------------
	checkSelectionExtraItems : function(form_index, do_fades)
	{
		var currentConfig = this.config.configuration[form_index];
		var alreadyDisplayed = [];
		var extraItemIndices = [];
		var tempCurrentExtraItems = [];
		var currentInputType = this.getConfigInputType(currentConfig);
		var chosenIndex;
		var obj = this;
		var item2IndexMap = this.configItemIdToIndexMap;
		var formArray = this.formArr;


		//if (currentConfig.allow_multiple_quantity == 0 && currentConfig.allow_multiple_selections == 0)
		if (currentInputType == 'select_box' || currentInputType == 'radios')
		{
			if (currentInputType == 'select_box')
			{
				chosenIndex = formArray[form_index].selectBox.selectedIndex;
			}
			else
			{
				var inputsLength = formArray[form_index].inputs.length;

                for (var i = 0; i < inputsLength; i++)
                {
                    if (formArray[form_index].inputs[i].checked == true) {
                        chosenIndex = i;
                    }
                }
			}

			if(currentConfig.options[chosenIndex])
			{				
				currentConfig.options[chosenIndex].extra_items.each(function(item,i)
				{
					var extra_config_item_id = item.extra_config_item_id;
					var extra_item_index = item2IndexMap[extra_config_item_id].index;
					extraItemIndices.push(extra_item_index);
				});
			}
		}


		if (typeof this.currentExtraItems[form_index] != "undefined")
		{
			for(var j = 0; j < this.currentExtraItems[form_index].length; j++)
			{
				var current_extra_item_index = this.currentExtraItems[form_index][j];

				if (extraItemIndices.indexOf(current_extra_item_index) < 0)
				{
					if (do_fades)
					{
						Effect.Fade(formArray[current_extra_item_index].divEl, {
							duration : .5,
							queue	 : { position:'end', scope: 'optionsScope' , limit:5 }
						});
					} 
					else 
					{
						formArray[current_extra_item_index].divEl.style.display = "none";
					}
				}
				else //no need to hide this, we're gonna display it again soon
				{
					alreadyDisplayed.push(current_extra_item_index);
				}

				this.formArr[current_extra_item_index].disabled = true;
				this.updateCurrentConfiguration(current_extra_item_index);

				if (typeof this.formArr[current_extra_item_index].selectBox  != "undefined")
				{
					this.formArr[current_extra_item_index].selectBox.selectedIndex = 0;
				}
			}
		}

		this.currentExtraItems[form_index] = [];
		for (var i = 0; i < extraItemIndices.length; i++)
		{
			extra_item_index = extraItemIndices[i];
			this.currentExtraItems[form_index].push(extra_item_index);

			if (alreadyDisplayed.indexOf(extra_item_index) < 0)
			{
				if (do_fades) {
					Effect.Appear(this.formArr[extra_item_index].divEl, {
						duration : .5,
						queue: { position:'end', scope: 'optionsScope' , limit:5 }
					});
				} else {
					this.formArr[extra_item_index].divEl.style.display = "";
				}
			}
			this.formArr[extra_item_index].disabled = false;
			this.updateCurrentConfiguration(extra_item_index);
		}
	},

	//--------------------------------------------------------------------------
    //- Function:       checkSelectionDependencies
    //- Description:    This function is called when a select box is changed to
	//-					see if the change reqruies that any dependencies change.
    //-
    //--------------------------------------------------------------------------
	checkSelectionDependencies : function(form_index, do_fade)
	{
		var currentConfig = this.config.configuration[form_index];
		var currentFormItem = this.formArr[form_index];
		var currentInputType = this.getConfigInputType(currentConfig);
		var chosenIndex;

		// If there are any dependencies currently enforced for this item, remove them now
		if (typeof this.currentEnforcedDependencies[form_index]	!= "undefined")
		{
			for (var j = 0; j < this.currentEnforcedDependencies[form_index].length; j++)
			{
				var current_enforced_index = this.currentEnforcedDependencies[form_index][j];
				if (do_fade){
					Effect.Fade(this.formArr[current_enforced_index].infoMsg,
							{ duration : .5,queue: { position:'end', scope: 'depScope' , limit:5 } });
				} else {
					this.formArr[current_enforced_index].infoMsg.style.display = "none";
				}

				if (typeof this.formArr[current_enforced_index].selectBox != 'undefined')
				{
					this.formArr[current_enforced_index].selectBox.disabled = false;
					this.formArr[current_enforced_index].selectBox.style.className = 'activeForm';	
					this.formArr[current_enforced_index].selectBox.className = 'activeForm';	
				}
				else
				{
					var inputsLength = this.formArr[current_enforced_index].inputs.length;

	                for (var i = 0; i < inputsLength; i++)
    	            {
        	            this.formArr[current_enforced_index].inputs[i].disabled = false;

						if (currentInputType == 'radios')
						{
        	            	this.formArr[current_enforced_index].inputs[i].style.className = 'activeRadio';
        	            	this.formArr[current_enforced_index].inputs[i].className = 'activeRadio';
						}
						else
						{
        	            	this.formArr[current_enforced_index].inputs[i].style.className = 'activeForm';
        	            	this.formArr[current_enforced_index].inputs[i].className = 'activeForm';
						}
	                }
				}
			}
		}

		// Make sure this is a select box we're dealing with	
		if (currentInputType == 'select_box' || currentInputType == 'radios')
		{
			if (currentInputType == 'select_box')
			{
				chosenIndex = currentFormItem.selectBox.selectedIndex;
			}
			else
			{
				var inputsLength = currentFormItem.inputs.length;

                for (var i = 0; i < inputsLength; i++)
                {
                    if (currentFormItem.inputs[i].checked == true) {
                        chosenIndex = i;
                    }
                }
			}
			this.currentEnforcedDependencies[form_index] = [];

			var numDependencies = [];
			if( currentConfig.options[chosenIndex])
			{
				numDependencies = currentConfig.options[chosenIndex].required_options.length;
			}
			for(var i = 0; i < numDependencies; i++)
			{
				var req_config_item_id = currentConfig.options[chosenIndex].required_options[i].config_item_id;	
				var req_option_id = currentConfig.options[chosenIndex].required_options[i].config_item_option_id; 
				var req_item_index = this.configItemIdToIndexMap[req_config_item_id].index;
				var req_option_id_index = this.configItemIdToIndexMap[req_config_item_id].options[req_option_id];

				if (typeof this.formArr[req_item_index].selectBox != 'undefined')
				{
					this.formArr[req_item_index].selectBox.selectedIndex = req_option_id_index;	
				}
				else
				{
					this.formArr[req_item_index].inputs[req_option_id_index].checked = true;	
				}

				this.formArr[req_item_index].infoMsg.innerHTML = '<img src="/catalog/images/info_16.gif"> Your selection of ' + 
					currentConfig.item_title + ": <i>" + currentFormItem.names[chosenIndex] + 
					"</i> requires the selection of <i>" +
					this.formArr[req_item_index].names[req_option_id_index] + "</i>";

				if (do_fade) {
					Effect.Appear(this.formArr[req_item_index].infoMsg,
							{ duration : .5,queue: { position:'end', scope: 'depScope' , limit:5 } });
				} else {
					this.formArr[req_item_index].infoMsg.style.display = "";
				}
				
				if (typeof this.formArr[req_item_index].selectBox != 'undefined')
				{
					this.formArr[req_item_index].selectBox.disabled = true;	
					this.formArr[req_item_index].selectBox.style.className = 'disabledForm';	
					this.formArr[req_item_index].selectBox.className = 'disabledForm';	
				}
				else
				{
					var inputsLength = this.formArr[req_item_index].inputs.length;

	                for (var i = 0; i < inputsLength; i++)
    	            {
						if(i == req_option_id_index)
						{ 
							continue;
						}
						this.formArr[req_item_index].inputs[i].disabled = true;
						if (currentInputType == 'radios')
						{
        	            	this.formArr[req_item_index].inputs[i].style.className = 'disabledRadio';
        	            	this.formArr[req_item_index].inputs[i].className = 'disabledRadio';
						}
						else
						{
        	            	this.formArr[req_item_index].inputs[i].style.className = 'disabledForm';
        	            	this.formArr[req_item_index].inputs[i].className = 'disabledForm';
						}
	                }
				}

				this.currentEnforcedDependencies[form_index].push(req_item_index);

				this.updateCurrentConfiguration(req_item_index);
				this.checkSelectionExtraItems(req_item_index, true);
			}
		}
	},

	//--------------------------------------------------------------------------
    //- Function:       verifyOnlyNumbers
    //- Description:    This function is called when a keypress is detected in a
	//-					quantity text box to make sure the input is only numbers.
    //-
    //--------------------------------------------------------------------------
	verifyOnlyNumbers : function(event,func)
	{
		var ffCharacter = String.fromCharCode(event.charCode);
		var ieCharacter = String.fromCharCode(event.keyCode);
		var keyPadNum;

		var ffResult = ffCharacter.match( '^[0-9]+$' );
		var ieResult = ieCharacter.match( '^[0-9]+$' );

		if(event.keyCode > 95 && event.keyCode < 106)
		{
			keyPadNum = true;		
		}
		
		if (!ffResult && !ieResult && event.keyCode != 9 && event.keyCode != 8 && event.keyCode !=46 && !keyPadNum)
		{
			Event.stop(event);
		}
		if (typeof(func) != 'undefined')
		{
			window.setTimeout(func,1000);
		}
	},

	//--------------------------------------------------------------------------
    //- Function:       getConfigInputType 
    //- Description:    Determines the input type of a config item based upon
	//-					if multiple selections/quantity are allowed.
    //-
    //--------------------------------------------------------------------------
	getConfigInputType : function (configItem)
	{
		// check current input type
		if (configItem.allow_multiple_selections == 0 && configItem.allow_multiple_quantity == 0) {
			//currentInputType = "select_box";
			currentInputType = "radios";
		} else if (configItem.allow_multiple_selections == 1 && configItem.allow_multiple_quantity == 0) {
			currentInputType = "check_box";
		} else if (configItem.allow_multiple_selections == 0 && configItem.allow_multiple_quantity == 1) {
			//currentInputType = "select_box";
			currentInputType = "radios";
		} else if (configItem.allow_multiple_selections == 1 && configItem.allow_multiple_quantity == 1) {
			currentInputType = "text_box";
		}

		return currentInputType;
	},

	//--------------------------------------------------------------------------
    //- Function:       scrollConfigInfo
    //- Description:    This function moves the config info div so it is always
	//-					at the top of the screen.
    //-
    //--------------------------------------------------------------------------
	scrollConfigInfo : function (event)
	{
		var scrollOffset = 140;
		var shiftAmount = 8;
		var scrollAmount = window.pageYOffset
                			|| document.documentElement.scrollTop
                			|| document.body.scrollTop
                			|| 0;

		if (scrollAmount > scrollOffset)
		{
			this.sidebar_dest_location = this.sidebar_start_location + scrollAmount - scrollOffset;
		}
		else
		{
			this.sidebar_dest_location = this.sidebar_start_location;
		}

		if (Math.abs(this.sidebar_curr_location - this.sidebar_dest_location) < shiftAmount) 
		{
			this.is_sidebar_scrolling = false;
			clearTimeout(this.sidebar_timeout_var);
			this.sidebar_container.style.top = this.sidebar_dest_location + "px";
		}
		else
		{
			if (this.sidebar_curr_location < this.sidebar_dest_location)
			{
				this.sidebar_container.style.top = (this.sidebar_curr_location + shiftAmount) + "px";	
				this.sidebar_curr_location = parseInt(this.sidebar_container.style.top);
			}
			else
			{
				this.sidebar_container.style.top = (this.sidebar_curr_location - shiftAmount) + "px";	
				this.sidebar_curr_location = parseInt(this.sidebar_container.style.top);
			}
			this.is_sidebar_scrolling = true;
			var obj = this;
			this.sidebar_timeout_var = setTimeout(function (){ obj.scrollConfigInfo(); }, 75);
		}
	},

	addToCartOver : function ()
	{
		document.body.style.cursor = 'hand';
		document.documentElement.style.cursor = 'hand';
	},

	addToCartOut : function ()
	{
		document.body.style.cursor = 'auto';
		document.documentElement.style.cursor = 'auto';
	},

    mouseoverRow : function(event,row)
    {
        var cells = row.getElementsByTagName('td');
        $A(cells).each(function(e)
        {
			Element.addClassName(e,'hover');
        });
        return true;
    },

    mouseoutRow : function(event,row)
    {
        var cells = row.getElementsByTagName('td');
        $A(cells).each(function(e) 
		{ 
			Element.removeClassName(e,'hover'); 
		});
        return;
    }
}


function formatPrice(value)
{
	var val = parseFloat(value).toFixed(2);
	x = val.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return '$' + x1 + x2;
}

Effect.HighlightText = Class.create();
Object.extend(Object.extend(Effect.HighlightText.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({ startcolor: '#ffff99' }, arguments[1] || {});
    this.start(options);
  },
  setup: function() {
    if(this.element.getStyle('display')=='none') { this.cancel(); return; }

    if(!this.options.endcolor)
      this.options.endcolor = this.element.getStyle('color').parseColor('#000000');
    if(!this.options.restorecolor)
      this.options.restorecolor = this.element.getStyle('color');

    // init color calculations
    this._base  = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this));
    this._delta = $R(0,2).map(function(i){ return parseInt(this.options.endcolor.slice(i*2+1,i*2+3),16)-this._base[i] }.bind(this));
  },
  update: function(position) {
    this.element.setStyle({color: $R(0,2).inject('#',function(m,v,i){
      return m+(Math.round(this._base[i]+(this._delta[i]*position)).toColorPart()); }.bind(this)) });
  },
  finish: function() {
    this.element.setStyle({color: this.options.restorecolor});
  }
});

