jQuery.noConflict();

function isEmpty(anyValue)
{ whiteSpace = new RegExp(/^\s+$/);if(anyValue==null || whiteSpace.test(anyValue) || anyValue=="")return true; else return false;}

String.prototype.startsWith = function(t) {return (t.toLowerCase()== this.substring(0, t.length).toLowerCase()); } 
String.prototype.endsWith = function(t) { return (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase());  } 


var $clearBrand = function(){ 
 		if(! RegularExpression.test(brands)) return false;
   		brands="";
   		position="1";
   		jQuery("#Brand  > li ").find("a").each(function() {jQuery(this).removeClass("selected");});	
   		};	

var $clearColour = function(){ 
 		if(! RegularExpression.test(colours)) return false;
   		colours="";
   		position="1";
   		jQuery("#Colour > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		};

var $clearSize = function(){ 
 		if(!RegularExpression.test(SizeMensFootwear) &&
   		!RegularExpression.test(SizeWomensFootwear) &&
   		!RegularExpression.test(SizeJuniorsFootwear) &&
   		!RegularExpression.test(SizeMensClothing) &&
   		!RegularExpression.test(SizeWomensClothing)&&
   		!RegularExpression.test(SizeJuniorsClothing)&&
   		!RegularExpression.test(SizeInfantsFootwear)&&
   		!RegularExpression.test(SizeInfantsClothing)&&
   		!RegularExpression.test(SizeMensAccessories)&&
   		!RegularExpression.test(SizeWomensAccessories)&&
   		!RegularExpression.test(SizeJuniorsAccessories)&&
   		!RegularExpression.test(SizeNurseryFootwear)&&
   		!RegularExpression.test(SizeNurseryClothing)) return false;
   		SizeMensFootwear="";
		SizeWomensFootwear="";
		SizeJuniorsFootwear="";
		SizeMensClothing="";
		SizeWomensClothing="";
		SizeJuniorsClothing="";
		SizeInfantsFootwear="";
 		SizeInfantsClothing="";
 		SizeNurseryFootwear="";
 		SizeNurseryClothing="";
 		SizeMensAccessories="";
 		SizeWomensAccessories="";
 		SizeJuniorsAccessories="";
   		position="1";
   		jQuery("#SizeMensFootwear > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeWomensFootwear > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeJuniorsFootwear > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeMensClothing > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeWomensClothing > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeJuniorsClothing > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeInfantsFootwear > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeInfantsClothing > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeNurseryFootwear > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeNurseryClothing > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeMensAccessories > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeWomensAccessories > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		jQuery("#SizeJuniorsAccessories > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		};	

var $clearPrice = function(){ 
 		prices="";
   		position="1";
   		jQuery("#Price > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		};
   		
var $clearCategory = function(){ 
 		position="1";
   		Category=parentCategory;
   		CategoryName=parentCategoryName;
		SubCategoryName=parentSubCategoryName;
		childCategory="";
		childSubCategoryName="";
		childCategoryName="";
   		jQuery("#categoryMenuList > li ").find("a").each(function() {jQuery(this).removeClass("selected");
   		jQuery(this).removeClass("disabled");});
//   		jQuery("#categoryMenuList > li ").find("ul").each(function() {jQuery(this).fadeOut("fast");});
   		};

var $modifyCriteria = function(originalString, replaceString){
			if(originalString.startsWith(replaceString+"|"))
			originalString=originalString.replace(replaceString+"|","");
			if(originalString.endsWith("|"+replaceString))originalString=originalString.substring(0 ,originalString.length - (replaceString.length + 1));
			originalString=originalString.replace("|"+replaceString+"|","|");
			return originalString;
		};

var $checkInputinCriteria = function(originalString, replaceString){
			if(originalString.startsWith(replaceString+"|"))return true;
			if(originalString.endsWith("|"+replaceString))return true;
			if(originalString.indexOf("|"+replaceString+"|")>-1) return true;
			if(originalString==replaceString) return true;
			return false;
		};
		
var $AddCriteria = function(originalString, replaceString){
			if(originalString.length < 1)		
		   			originalString=originalString + replaceString;
		   		else
		   			originalString=originalString+ "|" + replaceString;
		   	return originalString;
		};
 		
var $attributeModify = function(jQObject, criteriaString){
			//deselect if the function is selected
   			if(jQObject.hasClass("selected"))
   				{
	   				jQObject.removeClass("selected");
	   				jQObject.attr({title : "Select "+jQObject.attr("id")});
	   					if(criteriaString.indexOf('|')>0)
	   						{
	   							criteriaString=$modifyCriteria(criteriaString,jQObject.attr("id"))
		   					}
		   				else
		   					criteriaString="";
	   			}
   				else
   				{
   					if(jQObject.hasClass("disabled")) {return false;}
   						jQObject.addClass("selected");
   						jQObject.attr({title : "Deselect "+jQObject.attr("id")});
   				if(!$checkInputinCriteria(criteriaString,jQObject.attr("id")))
   						{
		   					criteriaString=$AddCriteria(criteriaString,jQObject.attr("id"))
	   					}
   				}
   			position="1";	
   			return criteriaString;
		};
 	

var $expandTree = function(headingObject, menulistObject,isFlexcroll){ 		
 		if(headingObject.hasClass("expanded"))
				{
				menulistObject.fadeOut("fast");
				headingObject.removeClass("expanded");
				headingObject.children('a').each(function() {if(jQuery(this).text().indexOf("clear") >-1) jQuery(this).text("open"); });
				if(isFlexcroll =='true' && browserName.indexOf("Internet Explorer") > -1)menulistObject.parent("li").fadeOut("fast");;
				}	
	   		else
		   		{
		   		if(isFlexcroll =='true' && browserName.indexOf("Internet Explorer") > -1 )menulistObject.parent("li").fadeIn("fast");
		   		headingObject.addClass("expanded");
		   		menulistObject.fadeIn("fast");
		   		headingObject.children('a').each(function() {if(jQuery(this).text().indexOf("open") >-1) jQuery(this).text("clear selection"); });
		   		
				}
 			};


 
 
 
 
 
jQuery(document).ready(function()
 {
	
	jQuery("#clearAll").click(function() { 
	if(!isEmpty(jQuery("#Brand > li > a").attr("href")))$clearBrand();
	if(!isEmpty(jQuery("#Colour > li > a").attr("href")))$clearColour();
	if(!isEmpty(jQuery("#sizeHeading > a").attr("href")))$clearSize();
	if(!isEmpty(jQuery("#Price > li > a").attr("href")))$clearPrice();
	if(!isEmpty(jQuery("#categoryMenuList > li > a").attr("href")))$clearCategory();
	if(!isEmpty(jQuery("#saleMenu > ul >li > a").attr("href"))){ jQuery("#saleMenu > ul >li > a").removeClass("selected");sale=""};
	OfferCode="";
	$refresh('');	 
 	}); 
 	
 	jQuery("#clearSize").click(function() { 
   		if(jQuery(this).text().indexOf('open') > -1){ $expandTree(jQuery(this).parent(),jQuery("#sizeMenu"),'false'); return false; }
   		$clearSize();$refresh(''); }); 
  
  	jQuery("#clearBrand").click(function() { 
    	if(jQuery(this).text().indexOf('open') > -1){ $expandTree(jQuery(this).parent(),jQuery("#brandsMenu"),'true'); return false; }
    	$clearBrand();$refresh(''); }); 
                 
   	jQuery("#clearColour").click(function() { 
   		if(jQuery(this).text().indexOf('open') > -1){ $expandTree(jQuery(this).parent(),jQuery("#colourMenu"),'false'); return false; }
   		$clearColour();$refresh(''); });               
  
  	jQuery("#clearPrice").click(function() { 
   		if(jQuery(this).text().indexOf('open') > -1){ $expandTree(jQuery(this).parent(),jQuery("#priceRangeMenu"),'true'); return false; }
   		$clearPrice();$refresh('');}); 
               
    jQuery("#clearCategory").click(function() { 
   		if(jQuery(this).text().indexOf('open') > -1){ $expandTree(jQuery(this).parent(),jQuery("#categoryMenu"),'true'); return false; }
  		$clearCategory();$refresh(''); }); 
               
  
  
	jQuery("#categoryMenuList > li > ul> li> a").click(function() {  
  		if(jQuery(this).hasClass("selected"))
   			{
				jQuery(this).removeClass("selected");
				jQuery(this).attr({title : "Select "+jQuery(this).text()});
				if(childCategory.indexOf('|')>0)
					{
	   							childCategory=$modifyCriteria(childCategory,jQuery(this).attr("id"))
		   						childSubCategoryName=$modifyCriteria(childSubCategoryName,jQuery(this).text())
		   			}
		   					else
		   			{
		   						childCategory="";
		   						childSubCategoryName="";
		   						childCategoryName="";
		   			}
		   					
	   		}	
  		else
  			{
  						
  					if(jQuery(this).hasClass("disabled")) {return false;}
   							jQuery(this).addClass("selected");
   							jQuery(this).attr({title : "Deselect "+jQuery(this).text()});
   							jQuery(this).parent().children("ul").fadeIn("fast");
   					if(childCategory.indexOf(jQuery(this).attr("id"))<0)
   					{
	   							if(childCategory== Category)childCategory="";
	  							if(childSubCategoryName== SubCategoryName)childSubCategoryName="";
   								childCategoryName=jQuery(this).attr("name");
   								childCategory=$AddCriteria(childCategory,jQuery(this).attr("id"));
   								childSubCategoryName=$AddCriteria(childSubCategoryName,jQuery(this).text());
   					}
  			}
			//CategoryName=childCategoryName;//Category=childCategory;//SubCategoryName=childSubCategoryName;
			changedAttrVal='categoryMenu';
            $refresh('categoryMenu');
      }); 
  
  	jQuery("#categoryMenuList > li > a").click(function() { 
   			if(jQuery(this).hasClass("disabled")) {return false;}
   						position="1";
   						//deselect if the function is selected
   						if(jQuery(this).hasClass("selected"))
   						{
	   						jQuery(this).removeClass("selected");
	   						jQuery(this).attr({title : "Select "+jQuery(this).text()});
	   						if(Category.indexOf('|')>0)
	   						{
	   							Category=$modifyCriteria(Category,jQuery(this).attr("id"))
		   						SubCategoryName=$modifyCriteria(SubCategoryName,jQuery(this).text())
		   						CategoryName=parentCategoryName;
		   					}
		   					else
		   					{
		   						Category=parentCategory;
		   						SubCategoryName=parentSubCategoryName;
		   						CategoryName=parentCategoryName;
		   					}
		   					//collapse the child tree.
		   					jQuery(this).parent().children("ul").fadeOut("fast");
		   					jQuery("#categoryMenuList > li >ul >li ").find("a").each(function() {jQuery(this).removeClass("selected");
		   					jQuery(this).removeClass("disabled");});
		   					//deselect selected child options
		   					childCategory="";
		   					childSubCategoryName="";
		   					childCategoryName="";
	
		   					// last day fix to avoid selection of multiple category
		   					jQuery("#categoryMenuList > li  ").children("a").each(function() {
		   						if(jQuery(this).hasClass("disabled"))
								{
		   						jQuery(this).removeClass("disabled");
								jQuery(this).parent().children("ul").fadeIn("fast");
								}
	   					})	
	   					}
   						else
   						{
   							if(jQuery(this).hasClass("disabled")) {return false;}
   							jQuery(this).addClass("selected");
   							jQuery(this).attr({title : "Deselect "+jQuery(this).text()});
   							jQuery(this).parent().children("ul").fadeIn("fast");
   							if(Category.indexOf(jQuery(this).attr("id"))<0)
   							{
	   							if(Category== parentCategory)Category="";
	  							if(SubCategoryName== parentSubCategoryName)SubCategoryName="";
   								CategoryName=jQuery(this).attr("name");
   								Category=$AddCriteria(Category,jQuery(this).attr("id"));
   								SubCategoryName=$AddCriteria(SubCategoryName,jQuery(this).text());
   							}
   							// last day fix to avoid selection of multiple category

   							var test = jQuery(this).text();
		   					jQuery("#categoryMenuList > li  ").children("a").each(function() {
		   						if(jQuery(this).text() != test)
								{
		   						jQuery(this).parent().children("ul").fadeOut("fast");
								jQuery(this).addClass("disabled");
								}
		   					})	
   						}	
   						 // call function to call mercado search
   						changedAttrVal='categoryMenu';
                        $refresh('categoryMenu');
    }); 
  
	jQuery("#departmentMenu > li > a").click(function() { 
	  	if(jQuery(this).hasClass("disabled")) return false;	
   		department = $attributeModify(jQuery(this),department);$refresh('departmentMenu');}); 
   		
  	jQuery("#Brand > li > a").click(function() { 
     	if(jQuery(this).hasClass("disabled")) return false;	
   			brands = $attributeModify(jQuery(this),brands);$refresh('Brand');});
   			
  	jQuery("#SizeMensFootwear > li > a").click(function() { 
   		if(jQuery(this).hasClass("disabled")) return false;		
   			SizeMensFootwear = $attributeModify(jQuery(this),SizeMensFootwear);$refresh('SizeMensFootwear');}); 
   			    
    jQuery("#SizeWomensFootwear > li > a").click(function() { 
       	if(jQuery(this).hasClass("disabled")) return false;			
       		SizeWomensFootwear = $attributeModify(jQuery(this),SizeWomensFootwear);$refresh('SizeWomensFootwear');});
    
    jQuery("#SizeJuniorsFootwear > li > a").click(function() { 		
		if(jQuery(this).hasClass("disabled")) return false;	
   			SizeJuniorsFootwear = $attributeModify(jQuery(this),SizeJuniorsFootwear);$refresh('SizeJuniorsFootwear');});
    
    jQuery("#SizeNurseryFootwear > li > a").click(function() { 		
    	if(jQuery(this).hasClass("disabled")) return false;	
   			SizeNurseryFootwear = $attributeModify(jQuery(this),SizeNurseryFootwear);$refresh('SizeNurseryFootwear');});	
    
    jQuery("#SizeInfantsFootwear > li > a").click(function() { 		
    	if(jQuery(this).hasClass("disabled")) return false;	
   			SizeInfantsFootwear = $attributeModify(jQuery(this),SizeInfantsFootwear);$refresh('SizeInfantsFootwear');});	 
    
    jQuery("#SizeMensClothing > li > a").click(function() { 
		if(jQuery(this).hasClass("disabled")) return false;			
   			SizeMensClothing = $attributeModify(jQuery(this),SizeMensClothing);$refresh('SizeMensClothing');}); 
                        
     jQuery("#SizeWomensClothing > li > a").click(function() {
        if(jQuery(this).hasClass("disabled")) return false;	 		
   			SizeWomensClothing = $attributeModify(jQuery(this),SizeWomensClothing);$refresh('SizeWomensClothing');});
    
    jQuery("#SizeJuniorsClothing > li > a").click(function() { 	
       	if(jQuery(this).hasClass("disabled")) return false;		
   			SizeJuniorsClothing = $attributeModify(jQuery(this),SizeJuniorsClothing);$refresh('SizeJuniorsClothing'); });
     
     jQuery("#SizeInfantsClothing > li > a").click(function() { 	
       	if(jQuery(this).hasClass("disabled")) return false;		
   			SizeInfantsClothing = $attributeModify(jQuery(this),SizeInfantsClothing);$refresh('SizeInfantsClothing'); });
         
       jQuery("#SizeNurseryClothing > li > a").click(function() { 	
       	if(jQuery(this).hasClass("disabled")) return false;		
   			SizeNurseryClothing = $attributeModify(jQuery(this),SizeNurseryClothing);$refresh('SizeNurseryClothing');}); 
    
     jQuery("#SizeMensAccessories > li > a").click(function() { 
   		if(jQuery(this).hasClass("disabled")) return false;		
   			SizeMensAccessories = $attributeModify(jQuery(this),SizeMensAccessories);$refresh('SizeMensAccessories');}); 
    
    jQuery("#SizeWomensAccessories > li > a").click(function() { 
       	if(jQuery(this).hasClass("disabled")) return false;			
   			SizeWomensAccessories = $attributeModify(jQuery(this),SizeWomensAccessories);$refresh('SizeWomensAccessories');});
    
    jQuery("#SizeJuniorsAccessories > li > a").click(function() { 		
    	if(jQuery(this).hasClass("disabled")) return false;	
   			SizeJuniorsAccessories = $attributeModify(jQuery(this),SizeJuniorsAccessories);$refresh('SizeJuniorsAccessories');});
     
	jQuery("#Colour > li > a").click(function() { 		
	   	if(jQuery(this).hasClass("disabled")) return false;	
   			colours = $attributeModify(jQuery(this),colours);$refresh('Colour');});                         
   			
    jQuery("#brandTree").click(function() {
	   		$expandTree(jQuery("#brandHeading"),jQuery("#brandsMenu"),'true');});                               
  
    jQuery("#categoryTree").click(function() {
	   		$expandTree(jQuery("#categoryHeading"),jQuery("#categoryMenu"),'true');
	   	});       
   
   jQuery("#sizeTree").click(function() {
	   		$expandTree(jQuery("#sizeHeading"),jQuery("#sizeMenu"),'false');
	   	});  
  
  	jQuery("#colourTree").click(function() {
	   		$expandTree(jQuery("#colourHeading"),jQuery("#colourMenu"),'false');
	   	});  
	   	
	jQuery("#priceTree").click(function() {
	   		$expandTree(jQuery("#priceHeading"),jQuery("#priceRangeMenu"),'true');
	   	});  
    
    jQuery("#Price > li > a").click(function() { 
 						//deselect if the function is selected
   						if(jQuery(this).hasClass("selected"))
   						{
	   						jQuery(this).removeClass("selected");
	   						prices="";	
	   					}
	   					
   						else
   						{  // deselect previously selected
   							jQuery("#Price > li ").find("a").each(function() {
   								jQuery(this).removeClass("selected");
  						 	});
   							prices="";
   						 	// select new one	
   							jQuery(this).addClass("selected");
   							if(prices.indexOf(jQuery(this).text())<0)
   							{
   								prices=$AddCriteria(prices,jQuery(this).attr("id"))
   							}
   						}	
   						position="1";
   						 // call function to call mercado search
                        $refresh('Price');
                        }); 
            
    jQuery("#saleMenu > ul > li > a").click(function() { 	
         				//deselect if the function is selected
   						if(jQuery(this).hasClass("selected"))
   						{
	   						jQuery(this).removeClass("selected");
	   						sale="";
	   					}
   						else
   						{
   							jQuery(this).addClass("selected");
   							sale = jQuery(this).attr("id")
   						}
   						position="1";	
                        $refresh('saleMenu');
                        });               
  		
  
  	
  	
	$expandTree(jQuery("#priceHeading"),jQuery("#priceRangeMenu"),'true');
	jQuery("#categoryMenuList > li ").find("ul").each(function() {jQuery(this).fadeOut();});
	$expandTree(jQuery("#colourHeading"),jQuery("#colourMenu"),'false')
	$loadPage()
  		
  });
