// JScript File
var expandNodeID = 0;

var theForm = document.getElementById("ShopForm");
if (!theForm) {
    theForm = document.ShopForm;
}

/*
	FUNCTION : Trim
	PARAMS : str - string to be trimmed
	PURPOSE : to trim a string.
*/
function Trim(str)
{
	var trimmed = "";
	if(str)
	{
		trimmed = str.replace(/^[\s]*/i,'');
		trimmed = trimmed.replace(/[\s]*$/i,'');
	}
	return trimmed;
}

/*
	FUNCTION : PadString
	PARAMS : value - value to be padded
			padChar - char used for padding
			length - length to be set on the string after padding
			bRight - whether teh paddign to be done on right hand side of string
	PURPOSE : to pad a string
*/
function PadString(value,padChar,length,bRight)
{
	var sPaddedValue = "";
	for(var i=0;i<length;i++)
	{
		sPaddedValue += padChar;
	}
	if(bRight)
	{
		sPaddedValue = value + sPaddedValue;
		sPaddedValue = sPaddedValue.substr(0,length);
	}
	else
	{
		sPaddedValue = sPaddedValue + value;
		sPaddedValue = sPaddedValue.substr((sPaddedValue.length - length),length);
	}
	return sPaddedValue;
}

function KeyPress(evt)
{
	if(window.event && window.event.srcElement)
    {
        e=window.event.srcElement;
        evt = window.event;
    }
    else if(evt.target)
    {
        e=evt.target;
    }
    else
    {
        e=evt;
    }
    if(evt.keyCode == '13')
    {
		var elementName = Trim(e.id);
		if(elementName=='SearchString')
		{
			Navigate('SearchProducts');
		}
		else if(elementName.substr(0, 13)=='CustomerLogin')
		{
			var loginButton = document.getElementById('CustomerLogin_LoginImageButton');
			if(document.dispatchEvent)
			{
				loginButton.dispatchEvent(loginButton.onclick);
				__doPostBack('CustomerLogin$LoginImageButton');
			}
			else
			{
				loginButton.click();
			}
		}
    }
}

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

function ShowHideDropDown(show)
{
	var visibility = "hidden";
	if(show)
	{
		visibility = "visible";
	}
	
	var oDiv = document.getElementById("ListContent");
	var oselect = document.getElementById("SearchCategoryID");
	oselect.style.visibility = visibility;
	var aSelects = oDiv.getElementsByTagName("SELECT");
	for(var i=0;i<aSelects.length;i++)
	{
		aSelects[i].style.visibility = visibility;
	}

}

function Navigate(navigationPath,navigationArgument)
{
	if(navigationArgument)
	{
		theForm.CurrentNavigationArgument.value= navigationArgument;
	}
	if(navigationPath != 'BrowseCategory')
	{
        ExpandCollapseTree(false);
	}
	theForm.CurrentNavigationPath.value = navigationPath;
	var navigateLink = document.getElementById("NavigateContent");
	if(document.dispatchEvent)
	{
        navigateLink.dispatchEvent(navigateLink.onclick);
        __doPostBack('NavigateContent');
	}
	else
	{
	    theForm.NavigateContent.click();
	}
}

function SwapExpansionStatus(e)
{
	var status = GetAttribute(e,"status");
	if (status=="Collapsed")
	{ 
		status = "Expanded"; 
	} 
	else if (status=="Expanded")
	{
		status = "Collapsed";
	}
	SetAttribute(e,"status",status); 
    return status;
}
        
function ExpandCollapseTree(expand)
{
    var cssClass = "TreeNodeText";
    if(expand)
    {
        cssClass = "TreeNodeSelectedText";
    }
    if(expandNodeID)
    {
        var selectedItem = document.getElementById('NODE' + expandNodeID);
	    SetAttribute(selectedItem,"class",cssClass);
        var breadCrumbTrailValue = GetAttribute(selectedItem,"BreadCrumbTrail");
        if(breadCrumbTrailValue.length > 0)
        {
	        var objNamColl = breadCrumbTrailValue.split(';');
	        for(var i = objNamColl.length-1;i>=0;i-=1)
	        {
		        var objName = objNamColl[i];
		        if(objName.length>0)
		        {
                    var obj = document.getElementById(objName);
                    if(obj && obj.parentElement)
                    {
                        obj.className = cssClass;
                        obj.parentElement.className = "expand";
                        if(expand)
                        {
                            obj.parentElement.style.display = "block"; 
                            SetDisplay(obj.parentElement, "Expand");
                        }
                        else
                        {
                            SetDisplay(obj.parentElement, "Collapsed");
                        }
                    }
		        }
	        }
        }
    }
    return breadCrumbTrailValue;
}

function SetDisplay(e, status)
{
    if(e.childNodes)
    {
	    var display = "block";
	    if (status=="Collapsed")
	    {
		    display = "none";
	    }
	    for (var i=1;i<e.childNodes.length;i++) 
	    {
		    e.childNodes.item(i).style.display = display; 
	    }
    }
}
	
function ExpandTreeNode(e)
{ 
    if(e)
    {
        var status = SwapExpansionStatus(e);
        SetDisplay(e, status);
        if(GetAttribute(e,"HasChildren")!="true")
        {
            ExpandCollapseTree(false);
            expandNodeID = GetAttribute(e,"NodeID");
            ExpandCollapseTree(true);
            document.getElementById("ParentID").value = expandNodeID;
            document.getElementById("BreadCrumbTrail").innerText = GetAttribute(e,"BreadCrumbTrail").replace(/;/gi," > ");
            Navigate('BrowseCategory',GetAttribute(e,"BreadCrumbTrail").replace(/;/gi," > "));
        }
    }    
} 
		
function SetAttribute(obj,attributeName,attributeValue)
{
	var attributes = obj.attributes;
	if(attributes && attributes[attributeName])
	{
		attributes[attributeName].value = attributeValue;
	}
	else if(obj.setAttribute)
	{
		obj.setAttribute(attributeName,attributeValue);
	}
    else
    {
		attributes[attributeName].value = attributeValue;
    }
}

function GetAttribute(obj,attributeName)
{
	var attributes = obj.attributes;
	if(attributes && attributes[attributeName])
	{
		return obj.attributes[attributeName].value;
	}
	else if(obj.getAttribute)
	{
		return obj.getAttribute(attributeName);
	}
}
		
function TreeHit(evt)
{
    if(window.event && window.event.srcElement)
    {
        e=window.event.srcElement;
    }
    else if(evt.target)
    {
        e=evt.target;
    }
    else
    {
        e=evt;
    }
    if (GetAttribute(e,"class")!="expand")
    {
        e=e.parentNode;
    }
    if (GetAttribute(e,"class")=="expand") ExpandTreeNode(e); 
} 
if(document.attachEvent)
{
    document.attachEvent("onclick",TreeHit)
    document.attachEvent("onkeypress",KeyPress)
}
else if(document.addEventListener)
{
    document.addEventListener("click",TreeHit, false);
    document.addEventListener("onkeypress",KeyPress, false);
}
