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 $clearHighlight = function(){ 
		if(! RegularExpression.test(highlights)) return false;
   		highlights="";
   		position="1";
   		jQuery("#Highlight > li ").find("a").each(function() {jQuery(this).removeClass("selected");});
   		};

var $clearSize = function(){ 
 		if(!RegularExpression.test(SizeMensFootwear) &&
   		!RegularExpression.test(SizeMensClothing) &&
   		!RegularExpression.test(SizeMensAccessories)
   		) return false;
   		SizeMensFootwear="";
		SizeMensClothing="";
		SizeMensAccessories="";
 		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 $clearSale = function(){ 
 		jQuery("#saleMenu > ul >li > a").removeClass("selected");sale=""
   		};  
   				
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("unavailable");});
   		jQuery("#categoryMenuList > li ").find("ul").each(function() {jQuery(this).fadeOut();});
   		};

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("unavailable")) {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("slow");
				headingObject.removeClass("expanded");
				headingObject.children('a').each(function() {if(jQuery(this).text().indexOf("clear") >-1) jQuery(this).text("open"); });
				if(isFlexcroll)menulistObject.parent("li").attr({style : "display:none"});
				}	
	   		else
		   		{
		   		headingObject.addClass("expanded");
		   		menulistObject.fadeIn("slow");
		   		headingObject.children('a').each(function() {if(jQuery(this).text().indexOf("open") >-1) jQuery(this).text("clear selection"); });
		   		if(isFlexcroll)menulistObject.parent("li").attr({style : "display:block"});
				}
 			};


 
 
 
 
 
jQuery(document).ready(function()
 {
	
	//jQuery("#sizeMenu").fadeOut();
	//$expandTree(jQuery("#priceHeading"),jQuery("#priceRangeMenu"),true);
	
	 
	
	jQuery("#clearAll").click(function() { 
	//alert('Brand '+ !isEmpty(jQuery("#Brand > li > a").attr("href")))
	if(!isEmpty(jQuery("#Brand > li > a").attr("href")))$clearBrand();
	if(!isEmpty(jQuery("#Colour > li > a").attr("href")))$clearColour();
	if(!isEmpty(jQuery("#Highlight > li > a").attr("href")))$clearHighlight();
	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")))$clearSale();
	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("#clearHighlight").click(function() { 
   		//if(jQuery(this).text().indexOf('open') > -1){ $expandTree(jQuery(this).parent(),jQuery("#highlightMenu"),false); return false; }
   		$clearHighlight();$refresh(''); });               
  
  
  
  	jQuery("#clearPrice").click(function() { 
   		//if(jQuery(this).text().indexOf('open') > -1){ $expandTree(jQuery(this).parent(),jQuery("#priceRangeMenu"),true); return false; }
   		$clearPrice();$refresh('');}); 
   		
	jQuery("#clearSale").click(function() { 
   		$clearSale();$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");
//				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("unavailable")) {return false;}
//   							jQuery(this).addClass("selected");
//   							jQuery(this).parent().children("ul").fadeIn();
//   					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("unavailable")) {return false;}
   						position="1";
   						//deselect if the function is selected
   						if(jQuery(this).hasClass("selected"))
   						{	
   						jQuery(this).removeClass("selected");
	   						if(Category.indexOf('|')>0)
	   						{
	   							Category=$modifyCriteria(Category,jQuery(this).attr("id"))
		   						SubCategoryName=$modifyCriteria(SubCategoryName,jQuery(this).text())
		   						if(parentCategoryName=='')
		   						{
		   							CategoryName=jQuery(this).attr("name");
		   						}
		   						else
		   						{
		   							CategoryName=parentCategoryName;
		   						}
		   					}
		   					else
		   					{	
		   						Category=parentCategory;
		   						SubCategoryName=parentSubCategoryName;
		   						//if(parentCategoryName=='')
		   						//{
		   						//alert("blank condition " + jQuery(this).attr("name"));
		   						//	CategoryName=jQuery(this).attr("name");
		   						//}
		   						//else
		   						//{
		   							CategoryName=parentCategoryName;
		   						//}
		   						if(pageName=='Category') //JDSZ 1212..the id is in breadcrumb
		   						{
		   						jQuery("#mainCategory").attr({href : "javascript:;"});
		   						jQuery("#mainCategory").text("CLOTHING/FOOTWEAR/BITS AND PIECES");
		   						}
		   					}		   					
		   					//collapse the child tree.
		   					//jQuery(this).parent().children("ul").fadeOut();
		   					//jQuery("#categoryMenuList > li >ul >li ").find("a").each(function() {jQuery(this).removeClass("selected");
		   					//jQuery(this).removeClass("unavailable");});
		   					//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("unavailable"))
		   					//	jQuery(this).removeClass("unavailable");
		   					//})	
	   					}
   						else
   						{
   							if(jQuery(this).hasClass("unavailable")) {return false;}
   							jQuery(this).addClass("selected");
   							//jQuery(this).parent().children("ul").fadeIn();
   							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());
   							}
   							if(pageName=='Category') //JDSZ 1212..the id is in breadcrumb
		   						{
		   						jQuery("#mainCategory").attr({href : "javascript:;"});
		   						jQuery("#mainCategory").text(SubCategoryName.toUpperCase());
		   						}
   							
   							// 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).addClass("unavailable");
		   					//})
   						}	
   						 // call function to call mercado search   						 
   						changedAttrVal='categoryMenu';
                        $refresh('categoryMenu');
    }); 
  
	jQuery("#departmentMenu > li > a").click(function() { 
	  	if(jQuery(this).hasClass("unavailable")) return false;	
   		department = $attributeModify(jQuery(this),department);$refresh('departmentMenu');}); 
   		
  	jQuery("#Brand > li > a").click(function() { 
     	if(jQuery(this).hasClass("unavailable")) return false;	
   			brands = $attributeModify(jQuery(this),brands);$refresh('Brand');});
   			
  	jQuery("#SizeMensFootwear > li > a").click(function() { 
   		if(jQuery(this).parent().hasClass("unavailable")) return false;		
   			SizeMensFootwear = $attributeModify(jQuery(this),SizeMensFootwear);$refresh('SizeMensFootwear');}); 
   			    
    jQuery("#SizeMensClothing > li > a").click(function() { 
		if(jQuery(this).parent().hasClass("unavailable")) return false;			
   			SizeMensClothing = $attributeModify(jQuery(this),SizeMensClothing);$refresh('SizeMensClothing');}); 
    
     jQuery("#SizeMensAccessories > li > a").click(function() { 
   		if(jQuery(this).parent().hasClass("unavailable")) return false;		
   			SizeMensAccessories = $attributeModify(jQuery(this),SizeMensAccessories);$refresh('SizeMensAccessories');}); 
    
    jQuery("#Colour > li > a").click(function() { 		
	   	if(jQuery(this).parent().hasClass("unavailable")) return false;	
   			colours = $attributeModify(jQuery(this),colours);$refresh('Colour');}); 
   			
   	jQuery("#Highlight > li > a").click(function() { 		
	   	if(jQuery(this).parent().hasClass("unavailable")) return false;	
   			highlights = $attributeModify(jQuery(this),highlights);$refresh('Highlight');}); 		                        
   			
    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("#highlightTree").click(function() {
	   		$expandTree(jQuery("#highlightHeading"),jQuery("#highlightMenu"),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');
                        });               
  		
  	//if(pageName=="CategoryDisplay")
  	//{
	   $loadPage()
  	//}	
  		
  });
