﻿var Step1View = (function() {

	function fnStep1ViewConstructor() {
		/****************************************************/
		/*                                                  */
		/*                                                  */
		/*            Class Level Private Methods           */
		/*                                                  */
		/*                                                  */
		/****************************************************/
		
		function createProductListItem(oProductList) {
			var oListItem = oProductList.immediateDescendants().first().cloneNode(true);
			initializeListItem(oListItem);
			return oListItem;
		}
	
		function getMessagePositioning(iMouseX, iMouseY) {
			var oPositioning = new Object();
			var oParentPositioning = $$("div.roundedPanel").first().cumulativeOffset();
			var oScrollOffsets = document.viewport.getScrollOffsets();
			oPositioning.iLeft = iMouseX - oParentPositioning.left - 260;
			oPositioning.iTop = iMouseY - oParentPositioning.top + 15;
			return oPositioning;
		}
		
		function getProductCategoryError(sProductCategory) {
		    var aProducts = $$("h5 > strong");
			var oProduct;
			for (var iProduct = 0; iProduct < aProducts.length; iProduct++) {
				oProduct = aProducts[iProduct];
				if (sProductCategory == oProduct.innerHTML)
					return oProduct.up("div");
			}  
		}
		
		function getProductByName(sProductName) {
		    var aProducts = $$("ul > li > h3 > strong");
		    var oProduct;
		    for (var iProduct = 0; iProduct < aProducts.length; iProduct++) {
		        oProduct = aProducts[iProduct];
		        if (oProduct.innerHTML == sProductName)
		            return oProduct.up("li");
		    }
		}
		
		function getProductListByName(sProductName) {
			var aProducts = $$("h5 > strong");
			var oProduct;
			for (var iProduct = 0; iProduct < aProducts.length; iProduct++) {
				oProduct = aProducts[iProduct];
				if (sProductName == oProduct.innerHTML)
					return oProduct.up("h5").next("ul");
			}
		}
		
		function initializeListItem(oListItem) {
			oListItem.down("select").selectedIndex = 0;
			oListItem.down("input").value = "";
			oListItem.down("img[alt='Added to Quote']").addClassName("Invisible");
		}
		
		function insertProductListItemAt(oProductList, oListItem, iIndex) {
			if (iIndex == 0)
				oProductList.insert({top: oListItem});
			else {
				var aProductListItems = oProductList.immediateDescendants();
				if (iIndex == aProductListItems.length)
					oProductList.insert({bottom: oListItem});
				else {
					aProductListItems[iIndex].insert({before: oListItem});
				}
			}
		}
		
		function setListItemValues(oListItem, sLicenseType, iQuantity) {
			var lstLicenseType = oListItem.down("select");
			var aOptions = lstLicenseType.options;
			var oOption;
			var iSelectedIndex = 0;
			for (var iOption = 0; iOption < aOptions.length; iOption++) {
				oOption = aOptions[iOption];
				if (oOption.value == sLicenseType) {
					iSelectedIndex = iOption;
					break;
				}
			}
			lstLicenseType.selectedIndex = iSelectedIndex;
			oListItem.down("input").value = iQuantity;
			
		}
	
		/****************************************************/
		/*                                                  */
		/*                                                  */
		/*           Class Level Priveleged Methods         */
		/*                                                  */
		/*                                                  */
		/****************************************************/
		
		this.addListItemByProductCategory = function(sProductCategory, sValue, sText) {
		    try {
		        getProductListByName(sProductCategory).down("select").insert(new Template("<option value=\"#{value}\">#{text}</option>").evaluate({value: sValue, text: sText}), "bottom");
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.addListItemTextByProductCategoryAndValue = function(sProductCategory, iValue, sText) {
		    try {
		        var aOptions = getProductListByName(sProductCategory).select("select > option[value='" + iValue.toString() + "']");
		        var oOption;
		        for (var iOption = 0; iOption < aOptions.length; iOption++) {
		            oOption = aOptions[iOption];
		            if (oOption.text.indexOf(sText) == -1)
	                    oOption.text += sText;
		        }
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.addNewProductLicense = function(sProductName) {
		    try {
			    var oProductList = getProductListByName(sProductName);
			    var oListItem = createProductListItem(oProductList);
			    oProductList.insert({bottom: oListItem});
			}
		    catch (eException) {
		    
		    }
		}
		
		this.addPopulatedProductLicense = function(sProductName, sId, sQuantity) {
		    try {
		      var oProductList = getProductListByName(sProductName);
			  var oListItem = createProductListItem(oProductList);
			  oListItem.down("select").value = sId;
			  oListItem.down("input").value = sQuantity;
			  oProductList.insert({bottom: oListItem});
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.addNewProductLicenseAt = function(sProductName, iIndex) {
			try {
			    var oProductList = getProductListByName(sProductName);
			    if (iIndex >= 0 && iIndex <= oProductList.immediateDescendants().length) {
				    var oListItem = createProductListItem(oProductList);
				    insertProductListItemAt(oProductList, oListItem, iIndex);
			    }
			}
		    catch (eException) {
		    
		    }
		}
		
		this.addProductLicense = function(sProductName, sLicenseType, iQuantity) {
		    try {
			    var oProductList = getProductListByName(sProductName);
			    var oListItem = createProductListItem(oProductList);
			    setListItemValues(oListItem, sLicenseType, iQuantity);
			    oProductList.insert({bottom: oListItem});
			}
		    catch (eException) {
		    
		    }
		}
		
		this.addProductLicenseAt = function(sProductName, sLicenseType, iQuantity, iIndex) {
			try {
			    var oProductList = getProductListByName(sProductName);
			    if (iIndex >= 0 && iIndex <= oProductList.immediateDescendants().length) {
				    var oListItem = createProductListItem(oProductList);
				    setListItemValues(oListItem, sLicenseType, iQuantity);
				    insertProductListItemAt(oProductList, oListItem, iIndex);
			    }
			}
		    catch (eException) {
		    
		    }
		}
		
		this.addSpecialOffer = function(sDescription) {
		    $("lstSpecialOffers").down("div.Content > ul").insert(new Template("<li>#{description}</li>").evaluate({description: sDescription}), "bottom");
		}
		
		this.clearSpecialOffers = function() {
		    $("lstSpecialOffers").down("div.Content > ul").update("");
		}
		
		this.collapseAccordionItem = function(oAccordionItem) {
			try {
			    oAccordionItem.addClassName("Collapsed");
			}
		    catch (eException) {
		    
		    }
		}
		
		this.collapseAllAccordionItems = function() {
		    try {
			    var aAccordionItems = $$("li.ProductGroup, li.SubSection");
			    var oAccordionItem;
			    for (var iAccordionItem = 0; iAccordionItem < aAccordionItems.length; iAccordionItem++) {
				    oAccordionItem = aAccordionItems[iAccordionItem];
				    oAccordionItem.addClassName("Collapsed");
			    }
			}
		    catch (eException) {
		    
		    }
		}
		
		this.expandAccordionItem = function(oAccordionItem) {
			try {
			    oAccordionItem.removeClassName("Collapsed");
			}
		    catch (eException) {
		    
		    }
		}
		
		this.expandAllAccordionItems = function() {
		    try {
			    var aAccordionItems = $$("li.ProductGroup, li.SubSection");
			    var oAccordionItem;
			    for (var iAccordionItem = 0; iAccordionItem < aAccordionItems.length; iAccordionItem++) {
				    oAccordionItem = aAccordionItems[iAccordionItem];
				    oAccordionItem.removeClassName("Collapsed");
			    }
			}
		    catch (eException) {
		    
		    }
		}
		
		this.hideDeleteButtonsByProductCategory = function(sProductCategory) {
		    try {
		        var aListItems = getProductListByName(sProductCategory).immediateDescendants();
		        for (var iListItem = 0; iListItem < aListItems.length; iListItem++)
		            aListItems[iListItem].select("a").last().addClassName("Invisible");  
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.hideLicenseRequirement = function() {
			try {
			    $("grpLicenseRequirement").addClassName("Hidden");
			}
		    catch (eException) {
		    
		    }
		}
		
		this.hideListItemAdded = function(sProductName, iIndex) {
		    try {
			    var oListItem = getProductListByName(sProductName).immediateDescendants()[iIndex];
			    var oImage = oListItem.down("img[alt='Added to Quote']");
			    var oDeleteButton = oImage.next();
			    oImage.addClassName("Invisible");
			    oDeleteButton.addClassName("Indent");
			}
		    catch (eException) {
		    
		    }
		}
		
		this.hideProductCategoryError = function(sProductCategory) {
		    try {
		        getProductCategoryError(sProductCategory).removeClassName("Error");
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.hideProductSpecialOffer = function(sProduct) {
		    try {
		        getProductByName(sProduct).down("img.SpecialOffer").addClassName("Hidden");
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.hideProductCategorySpecialOffer = function(sProductCategory) {
		    try {
		        getProductListByName(sProductCategory).removeClassName("SpecialOffer");
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.hideSpecialOffers = function() {
			try {
			    $("lstSpecialOffers").addClassName("Hidden");
			}
		    catch (eException) {
		    
		    }
		}
		
		this.modifyProductCategoryListItemAt = function(sProductCategory, iIndex, iId, iQuantity) {
		    try {
		        var oListItem = getProductListByName(sProductCategory).immediateDescendants()[iIndex];
		        oListItem.down("select").value = iId;
		        oListItem.down("input").value = iQuantity;
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.removeProductLicense = function(sProductName) {
			try {
			    var aListItems = getProductListByName(sProductName).immediateDescendants();
			    if (aListItems.length > 0)
				    aListItems.last().remove();
			}
		    catch (eException) {
		    
		    }
		}
		
		this.removeProductLicenseAt = function(sProductName, iIndex) {
		    try {
		        var aListItems = getProductListByName(sProductName).immediateDescendants();
		        if (iIndex >=0 && iIndex < aListItems.length)
				    aListItems[iIndex].remove();
			}
		    catch (eException) {
		    
		    }
		}
		
		this.removeListItemTextByProductCategoryAndValue = function(sProductCategory, iValue, sRemoveText) {
		    try {
		        var aOptions = getProductListByName(sProductCategory).select("select > option[value='" + iValue.toString() + "']");
		        var oOption;
	            for (var iOption = 0; iOption < aOptions.length; iOption++) {
	                oOption = aOptions[iOption];
	                oOption.text = oOption.text.replace(sRemoveText, "");
		        }
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.setListItemTextByProductCategoryAndValue = function(sProductCategory, iValue, sText) {
		    try {
		        var aOptions = getProductListByName(sProductCategory).select("select > option[value='" + iValue.toString() + "']");
		        for (var iOption = 0; iOption < aOptions.length; iOption++) {
		            aOptions[iOption].text = sText;
		        }
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.setSalesPhoneNumber = function(sPhoneNumber) 
		{
		    if ($("lblSalesPhoneNumber"))
		    {
		        $("lblSalesPhoneNumber").update(sPhoneNumber);
		    }
		}
		
		this.showDeleteButtonsByProductCategory = function(sProductCategory) {
		    try {
		        var aListItems = getProductListByName(sProductCategory).immediateDescendants();
		        for (var iListItem = 0; iListItem < aListItems.length; iListItem++)
		            aListItems[iListItem].select("a").last().removeClassName("Invisible");
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.showLicenseRequirement = function() {
			try {
			    var oMessagePositioning = getMessagePositioning();
			    var oLicenseRequirement = $("grpLicenseRequirement");
			    oLicenseRequirement.setStyle({left: oMessagePositioning.iLeft + "px", top: oMessagePositioning.iTop + "px"});
			    oLicenseRequirement.removeClassName("Hidden");
			}
		    catch (eException) {
		    
		    }
		}
		
		this.showLastListItemAdded = function(sProductName) {
		    try {
			    var oListItem = getProductListByName(sProductName).immediateDescendants().last();
			    var oImage = oListItem.down("img[alt='Added to Quote']");
			    var oDeleteButton = oImage.next();
			    oImage.removeClassName("Invisible");
			    oDeleteButton.removeClassName("Indent");
			}
		    catch (eException) {
		    
		    }
		}
		
		this.showListItemAdded = function(sProductName, iIndex) {
			try {
			    var oListItem = getProductListByName(sProductName).immediateDescendants()[iIndex];
			    var oImage = oListItem.down("img[alt='Added to Quote']");
			    var oDeleteButton = oImage.next();
			    oImage.removeClassName("Invisible");
			    oDeleteButton.removeClassName("Indent");
			}
		    catch (eException) {
		    
		    }
		}
		
		this.showProductCategoryError = function(sProductCategory) {
		    try {
		        getProductCategoryError(sProductCategory).addClassName("Error");
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.showProductSpecialOffer = function(sProduct) {
		    try {
		        getProductByName(sProduct).down("img.SpecialOffer").removeClassName("Hidden");
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.showProductCategorySpecialOffer = function(sProductCategory) {
		    try {
		        getProductListByName(sProductCategory).addClassName("SpecialOffer");
		    }
		    catch (eException) {
		    
		    }
		}
		
		this.showSpecialOffers = function(iMouseX, iMouseY) {
			try {
			    var oMessagePositioning = getMessagePositioning(iMouseX, iMouseY);
			    var oSpecialOffers = $("lstSpecialOffers");
			    oSpecialOffers.setStyle({left: oMessagePositioning.iLeft + "px", top: oMessagePositioning.iTop + "px"});
			    oSpecialOffers.removeClassName("Hidden");
			}
		    catch (eException) {
		    
		    }
		}
	}
	
	return fnStep1ViewConstructor;

})();