﻿var _ValueFieldId;
var _CheckBoxId;
var _HyperLinkId;
var _ProductFieldId;
var _CategoryFieldId;
var _CurrentCategory;
var _ProductCount;
var _ListId;

var lnkTopCategory;
var divTopCategories;
var ddlCategory;
var divProductCompareDb;
var divSubCategories;

var txtCompare1;
var txtCompare2;
var txtCompare3;
var txtCompare4;

var gvCompareGrid;

var txtCompareProductIds;

function ValueField()
{
    return document.getElementById(_ValueFieldId);
}

function CheckBox()
{
    return document.getElementById(_CheckBoxId);
}

function HyperLink()
{
    return document.getElementById(_HyperLinkId);
}

function ProductField(object)
{
    if(object.tagName.toUpperCase() == "A")
    {
        return document.getElementById(object.id.replace(_HyperLinkId, _ProductFieldId));
    }
    else if(object.tagName.toUpperCase() == "INPUT")
    {
        return document.getElementById(object.id.replace(_CheckBoxId, _ProductFieldId));
    }
    
    return null;
}

function CategoryField(object)
{
    if(object.tagName.toUpperCase() == "A")
    {
        return document.getElementById(object.id.replace(_HyperLinkId, _CategoryFieldId));
    }
    else if(object.tagName.toUpperCase() == "INPUT")
    {
        return document.getElementById(object.id.replace(_CheckBoxId, _CategoryFieldId));
    }
    
    return null;
}

function SelectProduct(object)
{
    var errorCode = ValidateProduct(object);
    
    //return false;
}

function UnSelectProduct(object)
{
    var errorCode = ValidateProduct(object);
    
    //return false;
}

function ValidateProduct(object)
{
    var errorCode = 0;
    var productField = ProductField(object);
    var categoryField = CategoryField(object);
    var valueField = ValueField();
    
    if(productField == null || categoryField == null || valueField == null)
    {
        errorCode -1; //invalid properties
    }
    else if(valueField.value.length == 0)
    {
        _CurrentCategory = categoryField.value;
        valueField.value = productField.value;
    }
    else if(parseInt(categoryField.value, 10) != parseInt(_CurrentCategory, 10))
    {
        errorCode = -2; // invalid category
    }
    else if(valueField.value.split(",").length == 3)
    {
        errorCode = -3; // max product count
    }
    else if(("," + valueField.value + ",").indexOf("," + productField.value + ",") > -1)
    {
        // product already selected
    }
    else
    {
        valueField.value += "," + productField.value;
    }
    
    //alert(valueField.value);
    return errorCode;
}

function Compare()
{
	var intLoop = 0;
	var intCount = 0;
	var valueField = ValueField();
	
	if(valueField.value.indexOf(",") > -1)
	{
		location.href="Comparison.aspx?CompareIds=" + valueField.value;
	}
	else
	{
		Alert("Compare Products", "Please select at least two products to compare.");
		return false;
	}
}

function StartCompare()
{
	var strCompareIds = "";
	if(parseInt("0" + txtCompare1.getAttribute("ProductId"), 10) > 0)
	{
		strCompareIds += txtCompare1.getAttribute("ProductId") + ",";
	}
	if(parseInt("0" + txtCompare2.getAttribute("ProductId"), 10) > 0)
	{
		strCompareIds += txtCompare2.getAttribute("ProductId") + ",";
	}
	if(parseInt("0" + txtCompare3.getAttribute("ProductId"), 10) > 0)
	{
		strCompareIds += txtCompare3.getAttribute("ProductId") + ",";
	}
//	if(parseInt("0" + txtCompare4.getAttribute("ProductId"), 10) > 0)
//	{
//		strCompareIds += txtCompare4.getAttribute("ProductId") + ",";
//	}
	
	if(strCompareIds.length > 0)
	{
		txtCompareProductIds.value = strCompareIds.substring(0, strCompareIds.length-1);
	}
	else
	{
		txtCompareProductIds.value = "";
	}
	__doPostBack('ctl00$cphContent$lnkGO',txtCompareProductIds.value);
	return false;
}

function GetFirstLevelCategories(CategoryId)
{
	var uniqueString = new Date();
	var state = "test";
	var arrCategories = PageMethods.GetChildCategories(	CategoryId, 
														uniqueString.toString(), 
														GetFirstLevelCategories_Success,
														GetFirstLevelCategories_Failure,
														state);
}

function GetFirstLevelCategories_Success(result)
{
	var arrCategories;
	var intLoop;

	if(typeof(result) != "undefined")
	{
		arrCategories = eval(result);

		if(arrCategories.length > 0)
		{
			while(divSubCategories.childNodes[0] != null)
			{
				divSubCategories.removeChild(divSubCategories.childNodes[0]);
			}

			for(intLoop=0; intLoop < arrCategories.length; intLoop++)
			{
				var anc = document.createElement("A");

				anc.innerHTML = arrCategories[intLoop].Category;
				anc.setAttribute("CategoryId", arrCategories[intLoop].CategoryId);
				anc.setAttribute("onclick", 'GetSubCategories('+arrCategories[intLoop].CategoryId+')');				
				anc.href = "javascript:void(0)";
				anc.className = "comparelinks";
				divSubCategories.appendChild(anc);
				divSubCategories.innerHTML += " ";
			}
			GetSubCategories(arrCategories[0].CategoryId);
		}
		HideOtherCategories();
	}
}

function GetFirstLevelCategories_Failure(result)
{
	alert(result._message);
}

function GetSubCategories(CategoryId)
{
   
	var uniqueString = new Date();
	var state = "test";
	var arrCategories = PageMethods.GetChildCategories(	CategoryId, 
														uniqueString.toString(), 
														GetSubCategories_Success,
														GetSubCategories_Failure,
														state);
}

function GetSubCategories_Success(result)
{
   
	var arrCategories;
	var intLoop;
	if(typeof(result) != "undefined")
	{
		arrCategories = eval(result);
		if(arrCategories.length > 0)
		{
			while(ddlCategory.options[0] != null)
			{
				ddlCategory.removeChild(ddlCategory.options[0]);
			}
			for(intLoop=0; intLoop < arrCategories.length; intLoop++)
			{
				var opt = document.createElement("OPTION");
				
				opt.innerHTML = arrCategories[intLoop].Category;
				opt.value = arrCategories[intLoop].CategoryId;
				ddlCategory.appendChild(opt);
			}
			ddlCategory.selectedIndex = 0;
			GetProductsByCategory();
		}		
	}
}

function GetSubCategories_Failure(result)
{
   
	alert(result._message);
}

function GetProductsByCategory()
{
	var uniqueString = new Date();
	var state = "test";
	var CategoryId = parseInt(ddlCategory.options[ddlCategory.selectedIndex].value);
	var arrProducts = PageMethods.GetProductsByCategory(CategoryId, 
															uniqueString.toString(), 
															GetProductsByCategory_Success,
															GetProductsByCategory_Failure,
															state);
}

function GetProductsByCategory_Success(result)
{
	var arrProducts;
	var intLoop;
	
	if(typeof(result) != "undefined")
	{
		arrProducts = eval(result);
		if(arrProducts.length > 0)
		{
			while(divProductCompareDb.childNodes[0] != null)
			{
				divProductCompareDb.removeChild(divProductCompareDb.childNodes[0]);
			}
			for(intLoop=0; intLoop < arrProducts.length; intLoop++)
			{
				var divProduct = document.createElement("DIV")
				divProduct.innerHTML = arrProducts[intLoop].ProductName;
				divProduct.setAttribute("ProductId", arrProducts[intLoop].ProductId);
				divProduct.className = "div-popup-items";
				divProductCompareDb.appendChild(divProduct);
			}
		}
		
	}
}

function GetProductsByCategory_Failure(result)
{
	alert(result);
}

function StartHighlight(div)
{
	if(parseInt("0" + ActivePopupProductId, 10) > 0 )
	{
		StopHighlight(GetSelectedPopUp().id);
	}
	ActivePopupProductId = document.getElementById(div).getAttribute("ProductId");
	document.getElementById(div).className = "div-highlight";
}
function StopHighlight(div)
{
	ActivePopupProductId = 0;
	document.getElementById(div).className = "div-popup-items";
}
function SelectCategory(div)
{
	lnkTopCategory.innerHTML = document.getElementById(div).innerHTML;
	lnkTopCategory.setAttribute("CategoryId", document.getElementById(div).getAttribute("CategoryId"));
	GetFirstLevelCategories(parseInt("0" + document.getElementById(div).getAttribute("CategoryId"), 10));
}

function ShowOtherCategories()
{
	divTopCategories.style.display = "";
}

function HideOtherCategories()
{
	divTopCategories.style.display = "none";
}

var AutoCompleteTextbox;
var ActivePopupProductId;
var previousAutocompleteString;

function GetCompareDiv()
{
	return document.getElementById(AutoCompleteTextbox.id.replace("ctl00_cphContent_txtCompare", "divProductCompare"));
}

function CheckAutoComplete(e)
{
	var textbox = document.activeElement;
	var autocompleteString = textbox.value;
	var strLength = autocompleteString.length;
	var intLoop;
	AutoCompleteTextbox = textbox;


	GetCompareDiv().style.display = "";
	if(previousAutocompleteString != autocompleteString || autocompleteString == "")
	{
		
		while(GetCompareDiv().childNodes[0] != null)
		{
			GetCompareDiv().removeChild(GetCompareDiv().childNodes[0]);
		}
		
		if(autocompleteString.length >= 0)
		{	
			for(intLoop=0; intLoop < divProductCompareDb.childNodes.length; intLoop++)
			{
				if(divProductCompareDb.childNodes[intLoop].tagName == "DIV")
				{
					if(divProductCompareDb.childNodes[intLoop].innerHTML.toLowerCase().indexOf(autocompleteString.toLowerCase()) > -1)
					{
						var index = divProductCompareDb.childNodes[intLoop].innerHTML.toLowerCase().indexOf(autocompleteString.toLowerCase());
						var divProduct = document.createElement("DIV")
						
						var strOption = divProductCompareDb.childNodes[intLoop].innerHTML;
						if(index > 0)
						{
							divProduct.innerHTML = strOption.substring(0, index) + "<b>" + strOption.substring(index, index + strLength) + "</b>" + strOption.substring(index + strLength, strOption.length);
						}
						else
						{
							divProduct.innerHTML = strOption;
						}
						divProduct.setAttribute("ProductId", divProductCompareDb.childNodes[intLoop].getAttribute("ProductId"));
						if(ActivePopupProductId + '' == divProductCompareDb.childNodes[intLoop].getAttribute("ProductId"))
						{
							divProduct.className = "div-highlight";
						}
						else
						{
							divProduct.className = "div-popup-items";
						}
						divProduct.id = "ProductId" + divProductCompareDb.childNodes[intLoop].getAttribute("ProductId");
						divProduct.onmouseover = function(){StartHighlight(this.id);}
						divProduct.onmouseout = function(){StopHighlight(this.id);}	
						divProduct.onclick = function(){SelectProductForCompare();}
						GetCompareDiv().appendChild(divProduct);
					}
				}
			}
		}
	}
	
	ChangeHighlight(textbox, e);
	previousAutocompleteString = autocompleteString;
}

function InitializeAutoComplete(textbox)
{
	AutoCompleteTextbox = textbox;
}

function FinalizeAutoComplete()
{
	if(parseInt("0" + ActivePopupProductId, 10) > 0)
	{
		StopHighlight(GetSelectedPopUp().id);		
	}
	GetCompareDiv().style.display = "none";
}

function ChangeHighlight(sender, e)
{
	var currentPopup = GetSelectedPopUp();

	if(AutoCompleteTextbox == sender)
	{
		var evt = (typeof(e) != "undefined" ? e : event);
		if(currentPopup != null)
		{
			if(evt.keyCode == 38)
			{
				if(currentPopup.previousSibling != null)
				{
					StopHighlight(currentPopup.id);
					StartHighlight(currentPopup.previousSibling.id);
				}
			}
			else if(evt.keyCode == 40)
			{
				if(currentPopup.nextSibling != null)
				{
					StopHighlight(currentPopup.id);
					StartHighlight(currentPopup.nextSibling.id);
				}
			}
			else if(evt.keyCode == 13)
			{
				SelectProductForCompare();
			}
		}
		else
		{
			if(evt.keyCode == 40)
			{
				if(GetCompareDiv().childNodes[0] != null)
				{
					StartHighlight(GetCompareDiv().childNodes[0].id);
				}
			}
		}
	}
}

function GetSelectedPopUp()
{
	var currentPopup, intLoop;
	for(intLoop=0; intLoop<GetCompareDiv().childNodes.length; intLoop++)
	{
		if(GetCompareDiv().childNodes[intLoop].getAttribute("ProductId") + '' == ActivePopupProductId + '')
		{
			currentPopup = GetCompareDiv().childNodes[intLoop];
			break;
		}
	}
	return currentPopup;
}

function SelectProductForCompare()
{
	var prodId = parseInt("0" + ActivePopupProductId, 10);
	if(prodId > 0)
	{
		AutoCompleteTextbox.value = GetSelectedPopUp().innerHTML;
		AutoCompleteTextbox.setAttribute("ProductId", GetSelectedPopUp().getAttribute("ProductId"));
	}
	FinalizeAutoComplete();
}

function ReformatCompareGrid(gvCompareGrid_id)
{
	var TR = document.getElementById(gvCompareGrid_id).getElementsByTagName("TR")[1]
	var intLoop;
	
	for(intLoop=0; intLoop < TR.getElementsByTagName("TD").length; intLoop++)
	{
		var td = TR.getElementsByTagName("TD")[intLoop];
		td.innerHTML = td.innerHTML.replace("&lt;", "<").replace("&gt;", ">");
	}
}
