﻿RegisterNamespace("VP.VerticalMatrix");

VP.VerticalMatrix.listSeperator = ",";
VP.VerticalMatrix.compareMatrixPageUrl = "";
VP.VerticalMatrix.SelectAllText = "";
VP.VerticalMatrix.SelectNoneText = "";
VP.VerticalMatrix.LeadFormUrlForAll = "";
VP.VerticalMatrix.LeadFormUrlForSelected = "";
VP.VerticalMatrix.ContentIdParameter = "";


//Add or remove product id to list when check box is checked and unchecked.
VP.VerticalMatrix.AddRemoveProduct = function(checkbox, hdnProductList)
{
	if (checkbox.checked)
	{
		VP.VerticalMatrix.AddToSelectedProducts(checkbox.value, hdnProductList);
	}
	else
	{
		VP.VerticalMatrix.RemoveFromSelectedProducts(checkbox.value, hdnProductList);
	}
	VP.VerticalMatrix.SelectAll.ChangeSelectButtonText();
	return true;
}

//Calls the horizontal matrix page for product comparison.
VP.VerticalMatrix.Compare = function(hdnSlectedProduct) {
	var hdnValue = $("#" + hdnSlectedProduct).val();
	var selectedProducts = hdnValue.split(VP.VerticalMatrix.listSeperator);
	
	if (hdnValue == "") {
		alert("Please select at least one product");
	}
	else if (selectedProducts.length > 5) {
		alert("No more than five items can be selected. This could happen if there are multiple item "
			+ "pages and you have selected items on other pages");
	}
	else {
		location.href = VP.VerticalMatrix.compareMatrixPageUrl + "&compare=" 
				+ $("#" + hdnSlectedProduct).val();
	}
}

//Adds the list of selected product ids to the given hyperlink object.
VP.VerticalMatrix.AddSelectedProductsToLink = function(PagerLink, hdnSlectedProduct)
{			
	PagerLink.href = VP.VerticalMatrix.AddSelectedProductsToUrl(PagerLink.href, hdnSlectedProduct);
	return true;
}

//Adds the list of selected product ids to the given url.
VP.VerticalMatrix.AddSelectedProductsToUrl = function(url, hdnSlectedProduct)
{		
	var urlComponents = url.split("&");		
	var foundSelect = false;
	
	for (var i=0; i < urlComponents.length; i++)
	{
		if (urlComponents[i].indexOf("sel=") >= 0)
		{
			urlComponents[i] = "sel=" + $("#" + hdnSlectedProduct).val();
			foundSelect = true;			
		}
	}
	if (foundSelect)
	{	
		url = urlComponents.join("&");	
	}
	else
	{
		url += "&sel=" + $("#" + hdnSlectedProduct).val();
	}
	return url;
}

//Adds to the list of selected products. 
VP.VerticalMatrix.AddToSelectedProducts = function(productId, hdnProductList)
{
	var selectedProducts = $("#" + hdnProductList).val().split(VP.VerticalMatrix.listSeperator);
	
	if (VP.VerticalMatrix.FoundElementInArray(selectedProducts, productId))
	{
		return false;
	}
	else
	{
		if ($("#" + hdnProductList).val() == "")
		{
			$("#" + hdnProductList).val(productId);
		}
		else
		{
			var productList = $("#" + hdnProductList).val() + VP.VerticalMatrix.listSeperator + productId
			$("#" + hdnProductList).val(productList);
		}
	}
}

//Removes the given product id from the list of selected product ids.
VP.VerticalMatrix.RemoveFromSelectedProducts = function(productId, hdnProductList)
{
	var selectedProducts = $("#" + hdnProductList).val().split(VP.VerticalMatrix.listSeperator)
	var newProductList = "";
	var index = 0;
	
	for(var i = 0; i < selectedProducts.length; i++)
	{
		if(selectedProducts[i] != productId)
		{
			if(index > 0)
			{
				newProductList += VP.VerticalMatrix.listSeperator;
			}
			newProductList += selectedProducts[i];
			index++;
		}
	}
	
	$("#" + hdnProductList).val(newProductList);
}

//Returns true if the given element value is found within the given array.
VP.VerticalMatrix.FoundElementInArray = function(array, elementValue)
{
	for (var i=0; i <array.length; i++)
	{
		if (array[i] == elementValue)
		{
			return true;
		}
		return false;
	}
}

VP.VerticalMatrix.RequestInfomationForMultipeProduct = function(hdnProductList)
{
	if ($("#" + hdnProductList).val() == "")
	{
		alert("Please select at least one product");
	}
	else
	{
		var leadFormUrl = VP.VerticalMatrix.LeadFormUrlForSelected;
		var oldValue = VP.VerticalMatrix.ContentIdParameter + "=";
		var newValue = oldValue + $("#" + hdnProductList).val();
		var navigateUrl = leadFormUrl.replace(oldValue, newValue);
		
		location.href = navigateUrl 
	}
}

//request information for all products.
VP.VerticalMatrix.RequestInfomationForAllProduct = function()
{
	location.href = VP.VerticalMatrix.LeadFormUrlForAll;
}

//select all or none check boxes 
VP.VerticalMatrix.SelectAll = function(hdnProductList)
{
	var checkboxList = $(".mpCheckBox input:checkbox")
	var buttonText = $(".selectButton").val();
	var nextTextForButton = "";
	
	for (var i = 0; i < checkboxList.length; i++)
	{	
		if(buttonText == VP.VerticalMatrix.SelectAllText)
		{
			checkboxList[i].checked = true;
			VP.VerticalMatrix.AddToSelectedProducts(checkboxList[i].value, hdnProductList);
		}
		else
		{
			checkboxList[i].checked = false;
			VP.VerticalMatrix.RemoveFromSelectedProducts(checkboxList[i].value, hdnProductList);
		}
	}
	VP.VerticalMatrix.SelectAll.ChangeSelectButtonText();
}

//Change select button text according to check box selection.
VP.VerticalMatrix.SelectAll.ChangeSelectButtonText = function()
{
	var checkboxList = $(".mpCheckBox input:checkbox");
	var noOfCheckedOnPage = 0;
	for (var i = 0; i < checkboxList.length; i++)
	{	
		if(checkboxList[i].checked == true)
		{
			noOfCheckedOnPage = noOfCheckedOnPage + 1;
		}
	}
	
	var selectButtons = $(".selectButton");
	
	if(noOfCheckedOnPage < checkboxList.length)
	{
		$(selectButtons).val(VP.VerticalMatrix.SelectAllText);
	}
	else
	{
		$(selectButtons).val(VP.VerticalMatrix.SelectNoneText);
	}
}
