﻿
    var pageLoaded = false;
    
	$(document).ready(function() {        
        hideCheckBoxes("catCmbbox", "catDrop2");
        hideCheckBoxes("typeCmbbox","typeDrop2");   
        hideCheckBoxes("sizeCmbbox","sizeDrop2");
        hideCheckBoxes('cityCmbbox','cityDrop2');

        CheckHdnValues("ctl00_Main_hdnGenders", 4, '#genderSet', "genid", 'gender', 'genders')
        CheckHdnValues("ctl00_Main_hdnCategories", 5, '#catCmbbox', "catid", 'category', 'categories')
        CheckHdnValues("ctl00_Main_hdnProdTypes", 6, '#typeCmbbox', "typeid", 'type', 'types')
        CheckHdnValues("ctl00_Main_hdnSizes", 8, '#sizeCmbbox', "sizeid", 'size', 'sizes')
        
        CheckHdnValues("ctl00_Main_hdnBrands", 7, '#brandSet', "brandid", 'brand', 'brands')
        CheckHdnValues("ctl00_Main_hdnProvinces", 9, '#provinceSet', "pid", 'province', 'provinces')
        CheckHdnValues("ctl00_Main_hdnCities", 10, '#cityCmbbox', "cid", 'city', 'cities')
        CheckHdnValues("ctl00_Main_hdnFollowees", 11, '#followeesSet', "followerid", 'followee', 'followees')
        
        pageLoaded = true;      
        
         
	});
		
	function showSelection(id, name)
    {
        
        $('#savedCmbBox .tally').html(name);
        $('.searchBoxDropDown').hide();
        $('#ctl00_Main_hdnSelectedSaved').val(id);
        
        CallServer(id);
        location.href = "Index.aspx?userprofileid=" + id;
    }
    
    function CheckHdnValues(hdn, dropdownId, dropdownName, itemPrefix, single, plural)
	{
		var values = $('#' + hdn).val();
		var message = " " + plural +  " currently selected";	
		var tallyBox = dropdownName + ' .tally';
		var count = 0;
		
		if (values != null)
		{
			CheckItems(values.split(","), itemPrefix, dropdownId);
			
			count = TallyItems(dropdownName)
		}
		
		if (count == 1)
			message = " " + single + " currently selected";	
						
		$(tallyBox).html(count + message);
		
	}
	
	function CheckItems(array, itemPrefix, dropdownId)
	{
		// Go through the list of selected values in order
		// to ensure the dropdowns are set correctly
		for (var i=0; i < array.length; i++)
		{
			var val = array[i];
			if (val != null && val != "")
			{
				// Check the box if it isn't already
				if (!$('#' + itemPrefix + val).attr("checked"))
				{
					$('#' + itemPrefix + val).attr("checked","checked")

					// Griz: Might be able to optimize this and only call 
					// it if the item isn't already checked.
					
					// Set up the select boxes
					OnSelectItem(dropdownId, val);
				}
			}
		}
	}
	
	function TallyItems(dropdownName)
	{
		var parentElement = dropdownName + ' .searchBoxDropDown';
		var count = 0;
		
		$(parentElement + " :checked").each( 
			function(){
			    var currentId = $(this).attr("id");
			    
				// Access to form element via $(this)
				count++;
			}
		);
		
		return count;
	}

	function CheckItemsInHiddenValue(hdn, ctrl, id)
	{
	    var values = $('#' + hdn).val();
	    
	    if (values != null)
	    {
	        var array = values.split(",");
    	    
	        // Go through the list of selected values in order
	        // to ensure the dropdowns are set correctly
	        for (var i=0; i < array.length; i++)
	        {
	            var val = array[i];
	            if (val != null && val != "")
	            {
	                // Check the box if it isn't already
	                if (!$('#' + id + val).attr("checked"))
	                    $('#' + id + val).attr("checked","checked")

                    // Griz: Might be able to optimize this and only call 
                    // it if the item isn't already checked.
                    
                    // Set up the select boxes
	                OnSelectItem(ctrl, val);
	            }
	        }
	    }
	}

    
    function tallyAllDropDowns()
    {         
        tallySelectedItems('#catCmbbox','category','categories');
        tallySelectedItems('#typeCmbbox','type','types');
        tallySelectedItems('#sizeCmbbox','size','sizes');
        
        tallySelectedItems('#provinceSet','province','provinces');
        tallySelectedItems('#cityCmbbox','city','cities');
        tallySelectedItems('#followeesSet','followee','followees');

        tallySelectedItems('#brandSet','brand','brands');
        tallySelectedItems('#genderSet','gender','genders');
    }

function tallySelectedItems(section,label1,label2) {
		var parentElement = section + ' .searchBoxDropDown';
		var tallyBox = section + ' .tally';
		
		var currentTally = 0;
		
		var message = " " + label2 +  " currently selected";	
		$(parentElement + " :checked").each( 
			function(){
			    var currentId = $(this).attr("id");
			    
				// Access to form element via $(this)
				currentTally++;
				
				if (currentTally == 1) {
					message = " " + label1  + " currently selected";
				}
			}
		);
	
		$(tallyBox).html(currentTally + message);
		
		var deselect = $(section + " .close div");
		if (deselect != null)
		{
		    if (currentTally > 0)
		        $(section + " .close div").show();
            else
                $(section + " .close div").hide();
        }
        //else
        //    alert("Nope");
	}
	
	function showCheckBoxes(el, dropdown) {
		var box = "#" + el + " input";
		
		var dd = document.getElementById(dropdown);	
		
		if (dd != null)
		    dd.style.display = "block";
				
		if ($(box) != null)
		    $(box).parent().parent().parent().parent().css({'color':'#000'});
		
	}
	
	
	function hideCheckBoxes(el, dropdown) {
		var box = "#" + el + " input";
		var tallyBox = "#" + el + " .tally";
		
		var dd = document.getElementById(dropdown);	
		if (dd != null)
		{
		    dd.style.display = "none";
		    HideCmbSelections(dropdown);
		}
		    		
		if ($(box) != null)
		    //$(box).parent().parent().parent().css({'color':'#999'});
			$(box).parent().parent().parent().parent().css({'color':'#999'});
			
		
	}
	
	
	
	// Display the proper select boxes 
	function OnSelectItem(ctrlid, id)
    {
        switch(ctrlid)
        {
            case 4: //gender
				// Get the items stored in the hidden element
                var storedValues = GetStoredValues('ctl00_Main_hdnGenders', id);
                
                if(storedValues != null && vout != "")  // If values exist...
                {
                    var vcat = document.getElementById('catDrop2');	
                    var strValues = storedValues.toString();

                    if( strValues.indexOf(",") > 0 || strValues.length == 0 )        
                    {   // 1. multiple genders or no genders
   
                        // Hide category section
                        hideCheckBoxes("catCmbbox", "catDrop2");
                        //hideCheckBoxes("catCmbbox", "categoryDrop");
                        
                        
                        // Clear categories
                        ClearAllCategories();  
                        
                        var array = strValues.split(",");  
                        if (array != null && array[0] != "")
                        {
                            for (var i=0; i < array.length; i++)
                            {   // Hide all genders because more than one was selected
                                var vpcat = document.getElementById("genderid" + array[i]);	
                                if(vpcat != null)
						            vpcat.style.display = "none";
						    }
						}
						else        
                        {   // Hide the "unselected" gender
                            var vpcat = document.getElementById("genderid" + id);	
                            if(vpcat != null)
					            vpcat.style.display = "none";
                        }
                    }
                    else
                    {   // 2. only one gender
  
                        // Show category selection
						showCheckBoxes("catCmbbox", "catDrop2");						                        
						//showCheckBoxes("catCmbbox", "categoryDrop");

						// Set up the new gender
                        var vpcat = document.getElementById("genderid" + storedValues);	
                        if(vpcat != null)
						    vpcat.style.display = "block";
                    }
                }                
                
                tallySelectedItems('#genderSet','gender','genders');
                break;
                
            case 5: //category
				
                var categories = GetStoredValues('ctl00_Main_hdnCategories', id);
                
                if (categories != null)
                {                    
                    // size/product
                    var strCategory = categories.toString();
                    
                    if(strCategory.indexOf("," ) > 0 || strCategory.length == 0 )
                    {
                        // 1. multiple categories or no categories
                        // hide product type section
                        hideCheckBoxes("typeCmbbox","typeDrop2");

                        // Hide product size  section
                        hideCheckBoxes("sizeCmbbox","sizeDrop2");
    					
                        // Clear product types
                        ClearAllTypes();
                                       
                        // Clear product sizes
                        ClearAllSizes();
                        
                        var array = strCategory.split(",");
                            
                        if (array != null && array[0] != "")
                        {
                            for (var i=0; i < array.length; i++)
                            {   // Hide all category/size because more than one was selected
                                var vpcat = document.getElementById("scatid" + array[i]);
                                if(vpcat != null)	
						            vpcat.style.display = "none";
            						
                                // Set up the new category/type
                                vpcat = document.getElementById("tcatid" + array[i]);	
                                if(vpcat != null)	
                                    vpcat.style.display="none";
					        }
					    }
					    else        
                        {   // Hide the "unselected" categories/size
                            var vpcat = document.getElementById("scatid" + id);
                            if(vpcat != null)	
					            vpcat.style.display = "none";
        						
                            // Set up the new category/type
                            vpcat = document.getElementById("tcatid" + id);	
                            if(vpcat != null)	
                                vpcat.style.display="none";
                        }
                    }
                    else
                    {
                        //2. only one category
                        //display product type section
                        showCheckBoxes("typeCmbbox","typeDrop2");
    					
                        // Display product size  section
                        showCheckBoxes("sizeCmbbox","sizeDrop2");
                        
                        // Set up the new category/size
                        var vpcat = document.getElementById("scatid" + strCategory);
                        if(vpcat != null)	
						    vpcat.style.display = "block";
    						
                        // Set up the new category/type
                        var vpcat2 = document.getElementById("tcatid" + strCategory);	
                        if(vpcat2 != null)	
                            vpcat2.style.display="block";
                    }
                }
                
                tallySelectedItems('#catCmbbox','category','categories');
                break;
 
            case 6: //type
                var vout = GetStoredValues('ctl00_Main_hdnProdTypes', id);
                
                tallySelectedItems('#typeCmbbox','type','types');
                break;
                
           case 7: //brand
                var vout = GetStoredValues('ctl00_Main_hdnBrands', id);
                
                tallySelectedItems('#brandSet','brand','brands');
                break;
                
            case 8: //size
				tallySelectedItems('#sizeCmbbox','size','sizes');
                var vout=GetStoredValues('ctl00_Main_hdnSizes', id);
                break;

            case 9: //PROVINCE
				
                var province = GetStoredValues('ctl00_Main_hdnProvinces', id);
                var strCites = province.toString();
                
                if(strCites.indexOf(",") > 0 || strCites.length == 0 )
                {
                    // Hide city section
                    hideCheckBoxes('cityCmbbox','cityDrop2');
					
                    //1. multiple provinces
                    ClearAllCities();
                    
                    var array = strCites.split(",");
                    if (array != null && array[0] != "")
                    {
                        for (var i=0; i < array.length; i++)
                        {   // Hide all genders because more than one was selected
                            var vpcat = document.getElementById("provid" + array[i]);	
                            if(vpcat != null)
					            vpcat.style.display = "none";
					    }
					}
					else        
                    {   // Hide the "unselected" gender
                        var vpcat = document.getElementById("provid" + id);	
                        if(vpcat != null)
				            vpcat.style.display = "none";
                    }
                }
                else
                {
                    //2. only one province
					showCheckBoxes('cityDrop2', 'cityDrop2');
					                    
                    // Display city section
                    //showCheckBoxes('cityDrop2', 'cityDrop2');
					
                    // Set up the new province
                    var vpcity = document.getElementById("provid" + province);
                    if (vpcity != null)
					    vpcity.style.display="block";
                }
                
                tallySelectedItems('#provinceSet','province','provinces');
                break;
                
            case 10: //city
                var vout = GetStoredValues('ctl00_Main_hdnCities', id);
                
                tallySelectedItems('#cityCmbbox','city','cities');
                break;
                 
            case 11: //followees
                var vout = GetStoredValues('ctl00_Main_hdnFollowees', id);
                
                tallySelectedItems('#followeesSet','followee','followees');
                break;
                
            default:
                break;
        }
    }


//    function ClearAllProvinces()
//    {
//        var vprovs = document.getElementById('ctl00_Main_hdnAllProvinceIds');
//        if(vprovs!=null && vprovs.value!=null)
//        {
//            var arprovs = vprovs.value.split(",");
//            if(arprovs.length>0)
//            {                  
//                var vout="";
//                var bNew=true;
//                for (var j=0; j<arprovs.length; j++)
//                {
//                    var vprovid = "provid" + arprovs[j];
//                    var vpcity = document.getElementById(vprovid);	
//                    if(vpcity!=null)
//                        vpcity.style.display="none";
//                }
//            }
//        }
//    }


   //take hidden file with comma-separated cities
    function ClearAllCities()
    {
        var vcities = document.getElementById('ctl00_Main_hdnAllCityIds');
         if(vcities != null && vcities.value != null)
         {
            var arcities = vcities.value.split(",");
            if(arcities.length > 0)
            {                  
                for (var j=0; j < arcities.length; j++)
                {
                    var vcityid = "cityid" + arcities[j];
                    var vpcity = document.getElementById(vcityid);	
                    if(vpcity!=null)
                        vpcity.checked=false;
                }
            }
            
            // Clear the hidden comma-separated field 
            var  vhdn = document.getElementById('ctl00_Main_hdnCities');	
            if(vhdn != null)
                 vhdn.value = "";
                 
            tallySelectedItems('#cityCmbbox','city','cities');
        }
    }
    
    
    function ClearAll()
    {
//        HideCmbSelections('#catDrop2');
//        HideCmbSelections('#typeDrop2');
//        HideCmbSelections('#sizeDrop2');
        ClearComboBox('#catCmbbox');
        ClearComboBox('#typeCmbbox');
        ClearComboBox('#sizeCmbbox');
        
        ClearComboBox('#provinceSet');
        ClearComboBox('#cityCmbbox');
        ClearComboBox('#followeesSet');

        ClearComboBox('#brandSet');
        ClearComboBox('#genderSet');
        
        $("#ctl00_Main_txtPriceFrom").val("");
        $("#ctl00_Main_txtPriceTo").val("");
        
        ClearAllCategories();
        
        tallyAllDropDowns();
        
        $("#ctl00_Main_hdnGenders").val("");
        $("#ctl00_Main_hdnCategories").val("");
        $("#ctl00_Main_hdnProdTypes").val("");
        $("#ctl00_Main_hdnSizes").val("");
        
        $("#ctl00_Main_hdnBrands").val("");
        $("#ctl00_Main_hdnProvinces").val("");
        $("#ctl00_Main_hdnCities").val("");
        $("#ctl00_Main_hdnFollowees").val("");
        
        $("#ctl00_Main_hdnDate").val("");
        $("#ctl00_Main_hdnSelectedSaved").val("");
        
        CallServer("ClearAll");
    }
    
    function HideCmbSelections(drop)
    {
         $('#' + drop +' div').each(
            function(){
			    var currentId = $(this).attr("id");
			    
			    if ($("#" + currentId) != null)
			        $("#" + currentId).hide();        

			}
        );
    }
    
    function ClearComboBox(cmb)
    {
        $(cmb + ' .searchBoxDropDown :checked').each(
            function(){
			    var currentId = $(this).attr("id");
			    
                $("#" + currentId).removeAttr("checked");        
			}
        );
               
        
        //$(function () { // this line makes sure this code runs on page load
//	    $(cmb + ' .searchBoxDropDown ').click(function () {
//		    $(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked);
//	    });
    //});
    }
  
    // Unchecks all selected genders in the genders dropdown
    //
    function ClearAllGenders()
    {
        ClearComboBox('#genderSet');
        $("#ctl00_Main_hdnGenders").val("");
        tallySelectedItems('#genderSet','gender','genders');
    }
    
    function ClearAllBrands()
    {
        ClearComboBox('#brandSet');
        $("#ctl00_Main_hdnBrands").val("");
        tallySelectedItems('#brandSet','brand','brands');
    }
    
    function ClearAllProvinces()
    {
        ClearComboBox('#provinceSet');
        $("#ctl00_Main_hdnProvinces").val("");
        tallySelectedItems('#provinceSet','province','provinces');
    }
    
    function ClearCities() 
    {
        ClearComboBox('#cityCmbbox');
        $("#ctl00_Main_hdnCities").val("");  
        tallySelectedItems('#cityCmbbox','city','cities');
    }
    
    function ClearAllFollowees()
    {
        ClearComboBox('#followeesSet');
        $("#ctl00_Main_hdnFollowees").val("");
        tallySelectedItems('#followeesSet','followee','followees');
    }
  
    // Unchecks all selected categories in the category dropdown
    //
    function ClearAllCategories()
    {
        $('#catCmbbox .searchBoxDropDown :checked').each(
            function(){
			    var currentId = $(this).attr("id");
			    
                $("#" + currentId).removeAttr("checked");        
			}
        )
    
        // Hide product type section
        hideCheckBoxes("typeCmbbox","typeDrop2");
        		
		// Hide product size  section
		hideCheckBoxes("sizeCmbbox","sizeDrop2");
        
        // Clear product types
        ClearAllTypes();
        
        //clear product sizes
        ClearAllSizes();
        
        // Clear the hidden comma-separated field 
        var hdn = document.getElementById('ctl00_Main_hdnCategories'); 
        if(hdn != null)
             hdn.value = "";
        
        tallySelectedItems('#catCmbbox','category','categories');          
    }

    // Unchecks all selected sizes in the size dropdown
    //
    function ClearAllSizes()
    {            
        $('#sizeCmbbox .searchBoxDropDown :checked').each(
            function(){
			    var currentId = $(this).attr("id");

                $("#" + currentId).removeAttr("checked");        
			}
        )
            
        // Clear the hidden comma-separated field 
        var  vhdn = document.getElementById('ctl00_Main_hdnSizes');	
        if(vhdn != null)
             vhdn.value = "";
             
        tallySelectedItems('#sizeCmbbox','size','sizes');
    }
    
    // Unchecks all selected types in the type dropdown
    //
    function ClearAllTypes()
    {    
        $('#typeCmbbox .searchBoxDropDown :checked').each(
            function(){
			    var currentId = $(this).attr("id");

                $("#" + currentId).removeAttr("checked");        
			}
        )        
            
        // Clear the hidden comma-separated field 
        var  vhdn = document.getElementById('ctl00_Main_hdnProdTypes');	
        if(vhdn != null)
             vhdn.value = "";
             
        tallySelectedItems('#typeCmbbox','type','types');
    }

    // Maintains the list of checked items in the specified
    // hidden element
    //
    // cntrl - The hidden element
    // id - The Checkbox id to to add or remove from the element
    function GetStoredValues(cntrl, id)
    {
        var  hdnElement = document.getElementById(cntrl);	
        var vout = "";

        if(hdnElement != null && hdnElement.value != "")
        {
            var values = hdnElement.value.split(",");
            if(values.length > 0)
            {                  
                var bNew = true;
                
                // For each value found in hidden element...
                for (var j=0; j < values.length; j++)
                {
                    // See if value already exists in the hidden element
                    if(values[j] == id)
                        bNew = false;         
                                              
                    else
                       // Keep all existing values 
                       vout += ((vout == "") ? "" : ",") + values[j];
                }
                
                // Add new value
                if(bNew)                   
                    vout += (vout=="") ? id : "," + id;
            }
        }	
        else
        {
            vout = id;
        }

        // Add comma seperated values to specified hidden element
        if (pageLoaded)
            hdnElement.value = vout;

        return hdnElement.value;
   }

    
    
    function SearchByGendAndCat(vGender,vCat)
    {
        ClearAll();
        var vhdn        = document.getElementById("ctl00_Main_hdnGenderCat");
        vhdn.value     =  vGender + "," + vCat;
        var vhdnEvent  = document.getElementById("ctl00_Main_hdnEvent");
        vhdnEvent.value="SearchByGendAndCat";
        
        var vhdnSearchControl   = document.getElementById("ctl00_Main_hdnSearchControl");
        vhdnSearchControl.value = "SearchControl";
        
        document.forms["aspnetForm"].submit();
    }
    
    
    
		$(document).ready(function() {
			// Handle the Search dropdowns
			$('.searchBoxDropDown').hide();
			
			//Handle the left nav dropdowns
			$('.leftNavDropDown').hide();
			

            // Show the appropriate gender category based on what the user last clicked
			var selected = $('#ctl00_Main_hdnSelectedLeftNav').val();
			var genders = $('#ctl00_Main_hdnGenders');
			
			if (selected != null)
            {
//			    var boxNumber = 0;
//			    
//			    if (genders != null)
//			    {
//			        boxNumber = genders.val();
//			        if (gVal.length == 1)
//			        {
//			            if (boxNumber == 1)
//			                selected = "#WomenDrop";
//                        else if (boxNumber == 2) 
//                            selected = "#MenDrop";
//                        else if (boxNumber == 3) 
//                            selected = "#GirlsDrop";
//                        else if (boxNumber == 4) 
//                            selected = "#BoysDrop";
//                    }
//			    }    
//			    else 
//			    {
                    if (selected == "#WomenDrop") 
                        boxNumber = 1;
                    else if (selected == "#MenDrop") 
                        boxNumber = 2;
                    else if (selected == "#GirlsDrop") 
                        boxNumber = 3;
                    else if (selected == "#BoysDrop") 
                        boxNumber = 4;
//                }
                
                
                $(selected).show();
			    $('#box' + boxNumber).css({'background-color' : '#fff'});
    			
			    $('a#Women').click(function() {
				    $('.searchBoxDropDown').hide();
				    //$('.boxLink').css({'background-color' : '#B3B3B3'});
				    $('.boxLink').css({'background-color' : '#efefef'});
				    $('#box1').css({'background-color' : '#fff'});
                    
//                    var isVisible = $('#WomenDrop').is(":visible");
//                    if (isVisible)
//                        $('.boxLink').css({'background-color' : '#B3B3B3'});
//                    else
//                        $('#box1').css({'background-color' : '#fff'});

                    $('#WomenDrop').slideToggle(250);
                                       
				    $('.leftNavDropDown').hide();
				    CallServer("#WomenDrop");
				    return false;
			    });
			    $('a#Men').click(function() {
				    $('.searchBoxDropDown').hide();
				    //$('.boxLink').css({'background-color' : '#B3B3B3'});
				    $('.boxLink').css({'background-color' : '#efefef'});
				    $('#box2').css({'background-color' : '#fff'});
				    
//				    var isVisible = $('#MenDrop').is(":visible");
//                    if (isVisible)
//                        $('.boxLink').css({'background-color' : '#B3B3B3'});
//                    else
//                        $('#box2').css({'background-color' : '#fff'});
                        
				    $('#MenDrop').slideToggle(250);
				    $('.leftNavDropDown').hide();
				    CallServer("#MenDrop");
				    return false;
			    });
			    $('a#Girls').click(function() {
				    $('.searchBoxDropDown').hide();
				    //$('.boxLink').css({'background-color' : '#B3B3B3'});
				    $('.boxLink').css({'background-color' : '#efefef'});
				    $('#box3').css({'background-color' : '#fff'});
				    
//				    var isVisible = $('#GirlsDrop').is(":visible");
//                    if (isVisible)
//                        $('.boxLink').css({'background-color' : '#B3B3B3'});
//                    else
//                        $('#box3').css({'background-color' : '#fff'});
                        
				    $('#GirlsDrop').slideToggle(250);
				    $('.leftNavDropDown').hide();
				    CallServer("#GirlsDrop");
				    return false;
			    });
			    $('a#Boys').click(function() {
				    $('.searchBoxDropDown').hide();
				    //$('.boxLink').css({'background-color' : '#B3B3B3'});
				    $('.boxLink').css({'background-color' : '#efefef'});
				    $('#box4').css({'background-color' : '#fff'});
				    
//				    var isVisible = $('#BoysDrop').is(":visible");
//                    if (isVisible)
//                        $('.boxLink').css({'background-color' : '#B3B3B3'});
//                    else
//                        $('#box4').css({'background-color' : '#fff'});
                        
				    $('#BoysDrop').slideToggle(250);
				    $('.leftNavDropDown').hide();
				    CallServer("#BoysDrop");
				    return false;
			    });
			}
			
		});
		
		
