    //this file uses prototypejs and scriptaculous plugin
    // created by David Pirek
	// modifified by Quang Nguyen
    //************************************************************************************
    
    //setting global variables
    //'B' name space3
    var sSubCat = ""
    aSubCat = new Array();
    var StrSubCat = ""
	
	// Page#
    var sPage = ""

    var strElmId
    var ajaxUrl = ""
   
    //createst string comma separated string out of array
    function B_joinArray() //cid
    { 
        StrSubCat = aSubCat.join("--");
        return(StrSubCat);
    };
   
    //generates query sting that loads AJAX data
    function MakeSelection(strType, strValue, strElmId, strPaId, strPaId2)
    {
		//Effect.BlindDown('loadingp', { duration: 0.1 });
        
		//checks for class being selected        
		var selTrue = Element.hasClassName(strElmId,"selected").toString();
		$(strElmId).toggleClassName('selected');
        
        //BRAND
        //*****************************************************
        if (strType == 'cid')
        {
            //checks of selection was made through current CSS class: creates 'toggle effect'
            if (selTrue == 'true')
            {
            //deleting item from the array
                var B_found = -1;
                for(var B_index = 0; B_index < aSubCat.length; B_index++) {
                    if(aSubCat[B_index] == strValue) { B_found = B_index; break; }
                }
                if(B_found >= 0) {
                    
                    //handles bug in splice function (splice cannot delete '0' value)
                    if (aSubCat.length == 1)
                    {
                        aSubCat.splice(B_found);
                    }
                    else
                    {
                        //handles bug in splice function (splice cannot delete '0' value)
                        if(B_found == 0)
                        {
                            aSubCat.splice(0,1);
                        }
                        else
                        {
                            aSubCat.splice(B_found, B_found);
                        }
                    }
                }
                sSubCat = B_joinArray();
            }
            else
            {
            //adding item from the array
                 //handles bug in splice function (splice cannot delete '0' value)
                 if (aSubCat.length == 0)
                 {
                    aSubCat.push(strValue); 
                 }
                 else
                 {
                    aSubCat.push(strValue); 
                 }
                 sSubCat = B_joinArray();
            };

        };
		
		sSubCat = (sSubCat=='') ? 0 : sSubCat; // it'll return default list when (none of subcat is selected later) OR (all subcats are deselected later).

		if (sSubCat==0) clearthis("cid", strPaId2); // it'll return default list when (none of subcat is selected later) OR (all subcats are deselected later).
  		else ajaxCall('', sSubCat, strPaId2, strPaId); //ajax call
       
    };


    //generates query sting that loads AJAX data
    function MakeSelection2(strValue, cid, pid2, pid)
    {
		Element.setOpacity('loadingp2bg', 0.4 );
		Effect.BlindDown('loadingp2', { duration: 0.1 });
		
		sPage = strValue;
	    ajaxCall2(sPage, cid, pid2, pid);
    };

    //AJAX CALL function
	// cid - current category id
	// pid - current parent id
	// pid2 - child of current parent id / parent id level 2
	function ajaxCall(tmp, cid, pid2, pid)
    {
        //$('products').hide();
        ajaxUrl = 'http://www.iogear.com/solutions/clist.php?pid=' + pid + '&pid2=' + pid2 + '&cid=' + cid;
        //Element.hide('products'); 
		new Ajax.Updater
            (
                'products', ajaxUrl, 
                {
                    method: 'get',
 					encoding: 'ISO-8859-1',
                   onSuccess: function() 
                        {
                            Element.show('products');
                            //Effect.Appear('products', { duration: 0.3 });
                        },
					onComplete: function()
						{
							Effect.SwitchOff('loadingp');
							new Effect.Opacity('loadingp2bg', {duration:0.6, from:0.4, to:1});
						},
					evalScripts: true
                }
            );
    };

	//AJAX CALL function
    function ajaxCall2(page, cid, pid2, pid)
    {	
        //$('products').hide();
        ajaxUrl = 'http://www.iogear.com/solutions/clist.php?page=' + page + '&pid=' + pid + '&pid2=' + pid2 + '&cid=' + cid;
        //Element.hide('products'); 
		new Ajax.Updater
            (
                'products', ajaxUrl, 
                {
                    method: 'get',
                    onSuccess: function() 
                        {
                            Element.show('products');
                        },
					onComplete: function()
						{
							Effect.SwitchOff('loadingp2');
							new Effect.Opacity('loadingp2bg', {duration:0.6, from:0.4, to:1});
						},
					evalScripts: true
                }
            );							
    };
	
    //Clear Functions    
    function clearthis(strClearItem, pid)
    {
        pid = (pid!=='') ? pid : ''; 
		//BRAND
        if (strClearItem == 'cid')
        {
            aSubCat = new Array();
			sPage = 1;
			ajaxCall('', 0,'',pid);
            clearCssClass('brand_wrapper', 'selected');
        }
        else
        {
            //alert("none selected will clear all");
            ajaxCall('', '', '','');
            clearCssClass('subcat_wrapper', 'selected');
            clearCssClass('brand_wrapper', 'selected');
            
        }
    };
    
    //clears classes of children with in ID (wrapper id, class to be cleared)
    function clearCssClass(WrapperId, CssClass)
    {
		
        allNodes = $(WrapperId).getElementsByClassName(CssClass); 
        for(i=0; i < allNodes.length; i++) 
        { 
            $(allNodes[i]).removeClassName(CssClass);
        }
		/*
		$(WrapperId).removeClassName(CssClass);*/
    }