/* ------------------------------------------------------------------
	Filename:        common.js
  Author:          AKQA
  Description:     Basic JavaScript functions
  								 Using mootools.js
        	        
  -------------------------------------------------------------------
 */
	
	
isIE6 = checkIsIE6();
var userAgent = navigator.userAgent.toLowerCase();

/* Standard bubble tool tips (object,boolean) */
var showToolTipFlag = false;
// function to show error messages on first click, and
// then allow the user to continue on second click.
function submitForm(formID) {
  var errorVisible = false;        
  var elements = document.getElementsBySelector('.error');
  for (var i = 0; i < elements.length; i++) {
      if (elements[i].style.display == 'none') {
             elements[i].style.display = 'block';
        } else {
           errorVisible = true;
         }
      }
  if (errorVisible) {
      document.getElementById(formID).submit();

  }          
  return errorVisible;
}

// this JavaScript will be run when the page has completed loading
function displayInit(classToElementListMap) {
	var elements = classToElementListMap['noScriptHide'];
	if ( elements != null ) {
		for (var i = 0; i < elements.length; i++) {
			removeClass(elements[i], 'noScriptHide');
		}
	}
	var displayChange = {
  			'noScript' : 'none',
  			'hasScript' : 'block',
  			'hasScriptI' : 'inline',
  			'btnRounded' : 'block',
  			'btnPlain' : 'none',
  			'btnPlainCancel' : 'none'
  	};
	
	for (cl in displayChange) {
		var nd = displayChange[cl];
		var elements = classToElementListMap[cl];
		if (elements) {
			for (var i = 0; i < elements.length; i++) {
				elements[i].style.display = nd;
			}
		}
	}
}


// moved to a function so that this can be called in info box windows
function setUpButtons() {
	// show rounded buttons - hide plain buttons
  	setDisplayByClass({
  		'.btnRounded' : 'block',
  		'.btnPlain' : 'none',
  		'.btnPlainCancel' : 'none'
  	});
  	
	var elements = getAllChildren(document);
	for (var i = 0; i < elements.length; i++) {
    	if (hasClass(elements[i], 'btnTriggerFormSubmit')) {
			addEvent(elements[i],"click",function(ev){		
				stopEvent(ev);
				setDisplayForMatchingElements('.toolTip', 'none');
				submitForm();
			  return false;
			});
    	}
	}
}

function init()  {
  var iBase = TextResizeDetector.addEventListener(onFontResize,null);
  applyMenuSizing(iBase);
}
function onFontResize(e,args) {
 var elements = document.getElementsBySelector('.mainMenu li');
  for (var i = 0; i < elements.length; i++) {	
		if (hasChild(elements[i],'a.mainItem')) {
			listEl.style.height='auto';
		}
	}
	elements = document.getElementsBySelector('.mainMenu li a.mainItem');
  for (var i = 0; i < elements.length; i++) {	
		anchorEl.style.height='auto';
	}	
	applyMenuSizing(args[0].iSize);
}

function applyMenuSizing(menuFontSize) {
	if (menuFontSize < 15) {
		removeClass(document.getElementsBySelector('.mainMenu'),'largeFont');
	} else {
		addClass(document.getElementsBySelector('.mainMenu'),'largeFont');
	}
}

//id of element to check for and insert control
TextResizeDetector.TARGET_ELEMENT_ID = 'mainMenu';
//function to call once TextResizeDetector has init'd
TextResizeDetector.USER_INIT_FUNC = init;

function mainMenuInit() {
	var mainMenuElements = document.getElementsBySelector('.mainMenu li a.mainItem');
	for (var i = 0; i < mainMenuElements.length; i++) {				
		addEvent(mainMenuElements[i],'mouseover', function(event) {	
		   element = getEventSourceElement(event);
    	 var subMenuElements= element.getAllChildren();
   	    for (var i = 0; i < subMenuElements.length; i++) {
         if(hasClass(subMenuElements[i],'.subMenu')){
				     subEl.style.top = getCoordinates(element).height-5;
			      }
		    }
		    return false;
	   });
  }
}
/* This event handles any default text values for text inputs, as well as
	 colour changes for default/user entered text */

function textValuesInit(classToElementListMap) {
	// Manipulate text inside inputs
	var elements = classToElementListMap['default'];
	if (elements) {
		for (var i = 0; i < elements.length; i++) {
			var sOriginalValue = '';
			// if this is the original value, then save the text
			if(sOriginalValue.length == 0)	sOriginalValue = elements[i].value;
			//Clear default value of search box on focus
			addEvent(elements[i],'focus', function(e) {
				 element = getEventSourceElement(e);
				 stopEvent(e);		
				 if(!hasClass(element,'notOriginalValue')) {
					 element.value = "";
					 removeClass(element,'default');
				}
				return false;
			});
			//Place default text back in search box if left empty
			addEvent(elements[i],'blur', function(e) {
				el = getEventSourceElement(e);
				stopEvent(e);
				if((el.value == '') || (el.value == sOriginalValue)) {
					el.value = sOriginalValue; 
					removeClass(el,'notOriginalValue');
					addClass(el,'default');
				}else {
					addClass(el,'notOriginalValue');
				}
				return false;
			});
			
			//switch class to highlight active input field
			addEvent(elements[i],'keyup', function(e) {
				stopEvent(e);
				el = getEventSourceElement(e);
				removeClass(el,'default');
				return false;
			});		
    	}
	}
}

function setUpAccordion(classNameToElementListMap) {
	// accordions
	var togglers = classNameToElementListMap['filterHeader'];
	if (togglers) {
		for (var i = 0; i < togglers.length; i++) {	
			nextSibling = getNextSibling(togglers[i]);
			if(i==0){
				addClass(togglers[i],'filterHeaderActive');
				nextSibling.style.display = 'block';
			}else{
				nextSibling.style.display = 'none';
			}
			addEvent(togglers[i],"click",function(ev){		
				stopEvent(ev);
				element = getEventSourceElement(ev);
				if(element != null ){
					toggleItem(element,'filterHeaderActive');
				}
				return false;
			});
		}
	}
  	// inner accordian	
	
  	var togglers = classNameToElementListMap['filterHeaderInner'];
	if (togglers) {
		for (var i = 0; i < togglers.length; i++) {	
			nextSibling = getNextSibling(togglers[i]);
			if(i==0){
				addClass(togglers[i],'filterHeaderInnerActive');
				nextSibling.style.display = 'block';
			}else{
				nextSibling.style.display = 'none';
			}
			addEvent(togglers[i],"click",function(ev){		
				stopEvent(ev);
				element = getEventSourceElement(ev);
				if(element != null ){
					toggleItem(element,'filterHeaderInnerActive');
				}
				return false;
			});
		}
	}	
}

function toggleItem(toggler,togglerClassName){ 
    
    if(toggler.nodeName == "H4"){//fix for IE
    	toggler = toggler.parentNode;
    }	
    if(toggler != null  && togglerClassName != null  && !hasClass(toggler,togglerClassName)){
    var activeTogglers = document.getElementsBySelector('.'+togglerClassName);
     	for (var i = 0; i < activeTogglers.length; i++) {		
     	    activeSibling = getNextSibling(activeTogglers[i]);
     	    removeClass(activeTogglers[i],togglerClassName);
     	    activeSibling.style.display = 'none';
       }
       element = getNextSibling(toggler);
      if(element != null ){
          addClass(toggler,togglerClassName);  
          element.style.display = 'block';
        }
    }   
}

// tooltips
function setUpToolTips(classNameToElementListMap) {
	// tool tip help - right arrow
	var userAgent = navigator.userAgent.toLowerCase();
	
	if(userAgent.indexOf('msie 8') > -1)
	{
		var premiumDeliveryPriceDsvIe8=document.getElementById("premiumDeliveryPriceDsvIe8");
		if(null!=premiumDeliveryPriceDsvIe8)
		premiumDeliveryPriceDsvIe8.style.cssText="margin-right:15px;margin-top:3px";
	}
	
	var toolTipLinks = classNameToElementListMap['toolTipLink'];
	if (toolTipLinks) {
		for (var i = 0; i < toolTipLinks.length; i++) {		
			toolTipLinks[i].style.display = 'inline';
			toolTipLinks[i].onclick = function(ev) {
				showToolTip(this,true);
				return false;
			};
		}
   }
	// tool tip help - left arrow 
	toolTipLinks = classNameToElementListMap['toolTipLinkLeft'];
	if (toolTipLinks) {
		for (var i = 0; i < toolTipLinks.length; i++) {		
			toolTipLinks[i].style.display = 'inline';
			toolTipLinks[i].onclick = function(ev) {		
				showToolTip(this,false);
				return false;
			};
		}
   }	
	// tool tip help - Comet Named Day Changes
	toolTipLinks = classNameToElementListMap['toolTipLinkRight'];
   	if (toolTipLinks) {
		for (var i = 0; i < toolTipLinks.length; i++) {		
			toolTipLinks[i].style.display = 'inline';
			if(userAgent.indexOf('msie 8') > -1)
			{
			toolTipLinks[i].onclick = function(ev) {		
			showDsvIe8ToolTip(this,'Right');
			return false;
			};
			}else{
				
			toolTipLinks[i].onclick = function(ev) {		
				showToolTip(this,'Right');
				return false;
			};
			}
			
	}
   }	
}




function showDsvIe8ToolTip(el,appearOnRight) {
	
	
	if(showToolTipFlag == false)
	{
	var toolTipWidth = 274;
	var arrowOffsetX = 3;
	var arrowOffsetY = 34;
	var toolTipMultiples = false;
	var toolTipId = 'toolTipHelpBubble';
	var errorMsg = '<h6 class="error" style="display:block">An error has occurred</h6>' + 
		'<p class="info">Either the \'rel\' attribute or the linking container of the tooltip icon is not correctly defined!</p>';
	
	if (el) {		
		
		// set position of where the tooltip should spawn from
		
		position = getElementAbsolutePosition(el);
		
		// get middle coordinate of icon, and then adjust tooltip y position relative to where the image arrow appears
		
		position.x += (el.offsetWidth/2);
		position.y += (el.offsetHeight/2) - arrowOffsetY;
		
		// get the properties of the window
		page = getSize(window);
		
		/* Added this code to display the CND tooltip properly.
		 */
		if(appearOnRight != 'Right'){
			
			/* if the tooltip is meant to sit on the right, but could go off
			 the right side of the screen, place it to the left */
			if (appearOnRight && (position.x + toolTipWidth) > (page.size.x)) appearOnRight = false;		
			
			/* check the left side has enough space for the tooltip, if not place it on the 
			 right. if both sides are not wide enough, it'll go to the right as this is default behaviour */
			if (!appearOnRight && (position.x - toolTipWidth) < 0) appearOnRight = true;
		
			// if it's the left, adjust the position
			if (!appearOnRight) position.x += -toolTipWidth;
		
			// adjust tooltip x position relative to where the image arrow appears
			position.x += (appearOnRight) ? -arrowOffsetX : arrowOffsetX;
		
			// set the CSS base class relative to the arrow direction
			var cssDirection = (!appearOnRight) ? "toolTipRight" : "toolTipLeft";
			cssDirection="toolTipLeft";
			var toolTipIdElement = document.getElementById(toolTipId);
    	
    		// remove any existing toolTips if multiples are not allowed
			if ((!toolTipMultiples) && (null!= toolTipIdElement)) toolTipIdElement.parentNode.removeChild(toolTipIdElement);
		
			// add a unique tooltipe Id if multiples are allowed
			if (toolTipMultiples) toolTipId += position.x + '-' + position.y;
	
			var toolTipBase = document.createElement('div');
    		toolTipBase.setAttribute( 'id',toolTipId); 
    		toolTipBase.setAttribute(document.all ? "className" : "class","toolTip "+ cssDirection);
        	
			toolTipBase.style.top  = (position.y)+'px';
			toolTipBase.style.left = (position.x)+'px';
			
		} else {
			
			// set the CSS base class relative to the arrow direction
			var cssDirection = "toolTipRight";
			var toolTipIdElement = document.getElementById(toolTipId);
		
			var toolTipBase = document.createElement('div');
	    	toolTipBase.setAttribute( 'id',toolTipId); 
	    	toolTipBase.setAttribute(document.all ? "className" : "class","toolTip "+ cssDirection);
	    	// Hardcoded this values for only CND.
	    	if(userAgent.indexOf('msie 6') > -1)
	    	{
	    	toolTipBase.style.top  = '111px';
			toolTipBase.style.left = '-110px';
	    	}
	    	else if (userAgent.indexOf('firefox') > -1)
	    	{
	    		toolTipBase.style.top  = '108px';
				toolTipBase.style.left = '-136px';
		    }	
	    	else if ((userAgent.indexOf('chrome') > -1) && (userAgent.indexOf('safari') > -1))
	    	{
	    		toolTipBase.style.top  = '115px';
				toolTipBase.style.left = '-136px';
		    }
	    	else if (userAgent.indexOf('msie') > -1)
	    	{
	    		toolTipBase.style.top  = '110px';
				toolTipBase.style.left = '-135px';
		    }
	    	else 
	    	{	    		
	    		toolTipBase.style.top  = '115px';
				toolTipBase.style.left = '-136px';	
	    	}
	    	
	    }
		
		// create markup for tool tip
		var toolTipArrow = document.createElement('div');
    toolTipArrow.setAttribute(document.all ? "className" : "class", "toolTipArrow");
    toolTipBase.appendChild(toolTipArrow);
		
	 var toolTipBody = document.createElement('div');
   toolTipBody.setAttribute(document.all ? "className" : "class", "toolTipBody");
   toolTipArrow.appendChild(toolTipBody);
		
	 var toolTipFooter = document.createElement('div');
   toolTipFooter.setAttribute(document.all ? "className" : "class", "toolTipFooter");
   toolTipBase.appendChild(toolTipFooter);
		
		// create close button for help text
		var toolTipClose = document.createElement('a');
		toolTipClose.setAttribute(document.all ? "className" : "class", "toolTipClose");
		toolTipClose.innerHTML = 'Close';

	   toolTipClose.onclick = function(e){
		  	removeElement = this.parentNode.parentNode.parentNode;
		  	removeElement.parentNode.removeChild(removeElement);
      		var selectTags = document.getElementsBySelector('select');
      		for (var i = 0; i < selectTags.length; i++) {	
				selectTags[i].style.visibility = 'visible';
			}
      		showToolTipFlag = false;
      		return false;		
		};		
	  toolTipBody.appendChild(toolTipClose);
		
		// set a warning message should no help text been defined.
		var helpText = errorMsg;	
		
		
			var helpText = '<p class="info" style="color:#000000;"><img src=\'/assets/www/product_browse/images/DSV_Named_Day.GIF\'/> Named Day Delivery allows you to specify the date and time you\'d like your item(s) delivered. Enter your postcode to see available delivery times. Then, when you get to checkout, you can select your preferred delivery time.';		
			
				var toolTipHeader = document.createElement('h6');
				//toolTipHeader.innerHTML = 'Help Text';
				toolTipBody.appendChild(toolTipHeader);
			
		
	
		toolTipHelpText = helpText;		
		var toolTipText = document.createElement('div');
		toolTipText.innerHTML = toolTipHelpText;
	    toolTipBody.appendChild(toolTipText);		
		injectAfter(toolTipBase,el);
	
		// internet explorer 6 will place select boxes above the dropdown, hide them
		if (window.ie6){
		  var selectTags = document.getElementsBySelector('select');
      for (var i = 0; i < selectTags.length; i++) {	
								// get the coordinates of the tooltips and dropdowns
				toolTipCoords =  getCoordinates(toolTipBase);
				dropDownCoords = getCoordinates(selectTags[i]);
				
				// if a drop down is underneath the tooltip, then hide it.
				if (((dropDownCoords.left > toolTipCoords.left) && (dropDownCoords.left < toolTipCoords.right) &&
						(dropDownCoords.top > toolTipCoords.top) && (dropDownCoords.top < toolTipCoords.bottom)) ||
						((dropDownCoords.left > toolTipCoords.left) && (dropDownCoords.left < toolTipCoords.right) &&
						(dropDownCoords.bottom > toolTipCoords.top) && (dropDownCoords.bottom < toolTipCoords.bottom)) ||
						((dropDownCoords.right > toolTipCoords.left) && (dropDownCoords.right < toolTipCoords.right) &&
						(dropDownCoords.top > toolTipCoords.top) && (dropDownCoords.top < toolTipCoords.bottom)) ||
						((dropDownCoords.right > toolTipCoords.left) && (dropDownCoords.right < toolTipCoords.right) &&
						(dropDownCoords.bottom > toolTipCoords.top) && (dropDownCoords.bottom < toolTipCoords.bottom))) {
					obj.setStyle('visibility', 'hidden');
				}
			}
		}
	}
	showToolTipFlag = true;
	return false;
	}
}



function showToolTip(el,appearOnRight)
{ 
	
	
	
	if(showToolTipFlag == false)
	{
	var toolTipWidth = 274;
	var arrowOffsetX = 3;
	var arrowOffsetY = 34;
	var toolTipMultiples = false;
	var toolTipId = 'toolTipHelpBubble';
	var errorMsg = '<h6 class="error" style="display:block">An error has occurred</h6>' + 
		'<p class="info">Either the \'rel\' attribute or the linking container of the tooltip icon is not correctly defined!</p>';

	if (el) {		
		// set position of where the tooltip should spawn from

		position = getElementAbsolutePosition(el);

		// get middle coordinate of icon, and then adjust tooltip y position relative to where the image arrow appears

		position.x += (el.offsetWidth/2);
		position.y += (el.offsetHeight/2) - arrowOffsetY;

		// get the properties of the window
		page = getSize(window);

		/* Added this code to display the CND tooltip properly.
		 */
		if(appearOnRight != 'Right'){
			/* if the tooltip is meant to sit on the right, but could go off
			 the right side of the screen, place it to the left */
			if (appearOnRight && (position.x + toolTipWidth) > (page.size.x)) appearOnRight = false;		

			/* check the left side has enough space for the tooltip, if not place it on the 
			 right. if both sides are not wide enough, it'll go to the right as this is default behaviour */
			if (!appearOnRight && (position.x - toolTipWidth) < 0) appearOnRight = true;

			// if it's the left, adjust the position
			if (!appearOnRight) position.x += -toolTipWidth;

			// adjust tooltip x position relative to where the image arrow appears
			position.x += (appearOnRight) ? -arrowOffsetX : arrowOffsetX;

			// set the CSS base class relative to the arrow direction
			var cssDirection = (!appearOnRight) ? "toolTipRight" : "toolTipLeft";
			cssDirection="toolTipLeft";
			var toolTipIdElement = document.getElementById(toolTipId);

    		// remove any existing toolTips if multiples are not allowed
			if ((!toolTipMultiples) && (null!= toolTipIdElement)) toolTipIdElement.parentNode.removeChild(toolTipIdElement);

			// add a unique tooltipe Id if multiples are allowed
			if (toolTipMultiples) toolTipId += position.x + '-' + position.y;

			var toolTipBase = document.createElement('div');
    		toolTipBase.setAttribute( 'id',toolTipId); 
    		toolTipBase.setAttribute(document.all ? "className" : "class","toolTip "+ cssDirection);

			toolTipBase.style.top  = (position.y)+'px';
			toolTipBase.style.left = (position.x)+'px';

		} else {
			var toolTipLinkRightId=document.getElementById("toolTipLinkRightId");
			
			var nectarPoints=document.getElementById("nectarPoints");
			var nowPriceLabel=document.getElementById("nowPriceLabel");
			
			var nectarPointsposition = getElementAbsolutePosition(nectarPoints);
			var nowPriceLabelposition=getElementAbsolutePosition(nowPriceLabel);
			var toolTipLinkRightIdposition=getElementAbsolutePosition(toolTipLinkRightId);
			
			// set the CSS base class relative to the arrow direction
			var cssDirection = "toolTipRight";
			var toolTipIdElement = document.getElementById(toolTipId);

			var toolTipBase = document.createElement('div');
	    	toolTipBase.setAttribute( 'id',toolTipId); 
	    	toolTipBase.setAttribute(document.all ? "className" : "class","toolTip "+ cssDirection);
	    	// Hardcoded this values for only CND.
	    	if(userAgent.indexOf('msie 6') > -1)
	    	{
	    	toolTipBase.style.top  = ((nectarPointsposition.y -nowPriceLabelposition.y )+68) +'px';
			toolTipBase.style.left = '-110px';
	    	}
	    	else if (userAgent.indexOf('firefox') > -1)
	    	{
	    		toolTipBase.style.top  =((nectarPointsposition.y -nowPriceLabelposition.y )+68) +'px';
				toolTipBase.style.left = '-136px';
			
		    }	
	    	else if ((userAgent.indexOf('chrome') > -1) && (userAgent.indexOf('safari') > -1))
	    	{
	    	
	    		toolTipBase.style.top  = ((nectarPointsposition.y -nowPriceLabelposition.y )+70) +'px';
				toolTipBase.style.left = '-136px';
			
		    }
	    	else if (userAgent.indexOf('msie') > -1)
	    	{
	    		toolTipBase.style.top  = ((nectarPointsposition.y -nowPriceLabelposition.y )+69) +'px';
				toolTipBase.style.left = '-135px';
		    }
	    	else 
	    	{	    		
	    		toolTipBase.style.top  = ((nectarPointsposition.y -nowPriceLabelposition.y )+68) +'px';
				toolTipBase.style.left = '-136px';	
	    	}
		}

		// create markup for tool tip
		var toolTipArrow = document.createElement('div');
    toolTipArrow.setAttribute(document.all ? "className" : "class", "toolTipArrow");
    toolTipBase.appendChild(toolTipArrow);

	 var toolTipBody = document.createElement('div');
   toolTipBody.setAttribute(document.all ? "className" : "class", "toolTipBody");
   toolTipArrow.appendChild(toolTipBody);

	 var toolTipFooter = document.createElement('div');
   toolTipFooter.setAttribute(document.all ? "className" : "class", "toolTipFooter");
   toolTipBase.appendChild(toolTipFooter);

		// create close button for help text
		var toolTipClose = document.createElement('a');
		toolTipClose.setAttribute(document.all ? "className" : "class", "toolTipClose");
		toolTipClose.innerHTML = 'Close';

	   toolTipClose.onclick = function(e){
		  	removeElement = this.parentNode.parentNode.parentNode;
		  	removeElement.parentNode.removeChild(removeElement);
      		var selectTags = document.getElementsBySelector('select');
      		for (var i = 0; i < selectTags.length; i++) {	
				selectTags[i].style.visibility = 'visible';
			}
      		showToolTipFlag = false;
      		return false;		
		};		
	  toolTipBody.appendChild(toolTipClose);

		// set a warning message should no help text been defined.
	//  alert("document.getElementById(el.rel).innerHTML;\\\\\\\\\\\\\\\\\\\"+document.getElementById(el.rel).innerHTML;)
		var helpText = errorMsg;		
		if (el.rel) {
			var helpText = document.getElementById(el.rel).innerHTML;		
			if (el.title != '') {
				var toolTipHeader = document.createElement('h6');
				toolTipHeader.innerHTML = el.title;
				toolTipBody.appendChild(toolTipHeader);
			}
		}
		toolTipHelpText = helpText;		
		var toolTipText = document.createElement('div');
	  toolTipText.innerHTML = toolTipHelpText;
	  toolTipBody.appendChild(toolTipText);		
		injectAfter(toolTipBase,el);

		// internet explorer 6 will place select boxes above the dropdown, hide them
		if (window.ie6){
		  var selectTags = document.getElementsBySelector('select');
      for (var i = 0; i < selectTags.length; i++) {	
								// get the coordinates of the tooltips and dropdowns
				toolTipCoords =  getCoordinates(toolTipBase);
				dropDownCoords = getCoordinates(selectTags[i]);

				// if a drop down is underneath the tooltip, then hide it.
				if (((dropDownCoords.left > toolTipCoords.left) && (dropDownCoords.left < toolTipCoords.right) &&
						(dropDownCoords.top > toolTipCoords.top) && (dropDownCoords.top < toolTipCoords.bottom)) ||
						((dropDownCoords.left > toolTipCoords.left) && (dropDownCoords.left < toolTipCoords.right) &&
						(dropDownCoords.bottom > toolTipCoords.top) && (dropDownCoords.bottom < toolTipCoords.bottom)) ||
						((dropDownCoords.right > toolTipCoords.left) && (dropDownCoords.right < toolTipCoords.right) &&
						(dropDownCoords.top > toolTipCoords.top) && (dropDownCoords.top < toolTipCoords.bottom)) ||
						((dropDownCoords.right > toolTipCoords.left) && (dropDownCoords.right < toolTipCoords.right) &&
						(dropDownCoords.bottom > toolTipCoords.top) && (dropDownCoords.bottom < toolTipCoords.bottom))) {
					obj.setStyle('visibility', 'hidden');
				}
			}
		}
	}
	showToolTipFlag = true;
	return false;
	}
}

/**function passwordStrength() {
	// Get the password input element
	var pwd = $('password');
	// If password input element exists
	if ($chk(pwd)) {
		// Add event on keyup
		pwd.addEvent('keyup', function(e) {
			e = new Event(e).stop();
			// Get the password strength indicator element
			var elPwdStrength = $E('strong.strength');
			// Define regular expressions to be used for the different levels - by variants, we mean different character types/case, eg: a, A, 1, _, $ would all be different variants.
			var veryStrongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g"); //very strong - eg: J0*8eY$9_Cx (lots of variants *)
			var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).*$", "g"); //strong - eg: J0n8eyS9 (min 8 characters, three variants *)
			var mediumRegex = new RegExp("^(?=.{6,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g"); //medium - eg: Jonney (min 6 characters, two variants *)
			var weakRegex = new RegExp("(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g"); //weak - eg: Jon (two variants *)
			var veryWeakRegex = new RegExp("((?=.*[A-Z])|(?=.*[a-z])|(?=.*[0-9])).*$", "g"); //insecure - eg:  jon (one variant *)
			var min6Regex = new RegExp("(?=.{6,}).*", "g"); //No password or insufficient characters
			
			// Conditional statements to test password strength - setting text and background image position accordingly
			if (pwd.value.length == 0) {
				elPwdStrength.setText('&nbsp;');
				elPwdStrength.setStyle('background-position', '0px -100px');
			}
			else if (veryStrongRegex.test(pwd.value)) {
				elPwdStrength.setText("Very Strong");
				elPwdStrength.setStyle('background-position', '0px 0px');
			}
			else if (strongRegex.test(pwd.value)) {
				elPwdStrength.setText("Strong");
				elPwdStrength.setStyle('background-position', '0px -20px');
			}
			else if (mediumRegex.test(pwd.value)) {
				elPwdStrength.setText("Medium");
				elPwdStrength.setStyle('background-position', '0px -40px');
			}
			else if (false == min6Regex.test(pwd.value) && weakRegex.test(pwd.value)) {
				elPwdStrength.setText('Weak');
				elPwdStrength.setStyle('background-position', '0px -60px');
			}
			else if (veryWeakRegex.test(pwd.value)) {
				elPwdStrength.setText('Very Weak');
				elPwdStrength.setStyle('background-position', '0px -80px');
			}
			else {
				elPwdStrength.setText("No Password");
				elPwdStrength.setStyle('background-position', '0px -100px');
			}
		})
	}
	
}**/

function initialize(){

	var classNameToElementListMap = {};
	
	var elements = getAllChildren(document);
    for (var i = 0; i < elements.length; i++) {
    	var el = elements[i];
    	var cl = el.className;
    	if (cl) {
			var cls = cl.split(" ");
			for (var j = 0; j < cls.length; j++) {
				cl = cls[j];
				var list = classNameToElementListMap[cl];
				if (list) {
					list.push(el);
				} else {
					list = new Array();
					list[0] = el;
					classNameToElementListMap[cl] = list;
				}
	    	}
    	}
	}

	displayInit(classNameToElementListMap);
	mainMenuInit();
	textValuesInit(classNameToElementListMap);
	setUpAccordion(classNameToElementListMap);
	setUpToolTips(classNameToElementListMap);   
}

addEvent(window,"load",initialize);

function ShowMenu(menu) {
	var thisMenu = document.getElementById(menu);
	if (thisMenu != null) {
		thisMenu.style.visibility="visible";
	}
	
	if (isIE6) {
	
		hideSelectsIntersectingWith(thisMenu);

	}	
}
function HideMenu(menu) {
	var thisMenu = document.getElementById(menu);
	if (thisMenu != null) {
	thisMenu.style.visibility="hidden";
	}

	if (isIE6) {
	 	unhideAllCurrentlyHiddenSelects();
	}
}

function HideAllMenu() {
	var i=1;
	while (i < 14) {
		i=i+1;
		to = document.getElementById('subMenu' + i);
		if (to != null) {
			to.style.visibility="hidden";
		}
	}
	if (isIE6) {
	 	unhideAllCurrentlyHiddenSelects();
	}

}

function alignMenu(menuItem){
	tt = document.getElementById('menu' + menuItem);
	sm = document.getElementById('subMenu' + menuItem);
	var i=1;
	var o = 0;
	while (i <menuItem) {
		i=i+1;
		to = document.getElementById('menu' + i);
		if (to != null)
		{
			o = o + to.offsetWidth;
			if (o > 930) {
				o = 0;
			}
		}
	}
  if (sm != null) {

	if ((o + sm.offsetWidth) > 930)	{ // if will be positioned too far right
		if ((o - sm.offsetWidth) > -84)	{ //if will not be possitionted too far left
			sm.style.left = tt.offsetWidth - sm.offsetWidth + "px";
		} else {
			sm.style.left = "-" + (o - (tt.offsetWidth - 84)) + "px";
		}
	}
	else {
		sm.style.left = 0 + "px";
	}
    }
  
}

currentlyHiddenSelects = null;

function hideSelectsIntersectingWith(element) {
	unhideAllCurrentlyHiddenSelects();
	var selects = document.getElementsByTagName("SELECT");
	for (var i = 0; i < selects.length; i++) {
		var sel = selects[i];
		if (elementsIntersect(sel, element)) {
			if (currentlyHiddenSelects == null) {
				currentlyHiddenSelects = new Array();
			}
			currentlyHiddenSelects[currentlyHiddenSelects.length] = sel;
			sel.style.visibility="hidden";
		}
	}
				
}

function unhideAllCurrentlyHiddenSelects() {
	if (currentlyHiddenSelects != null) {
		for (var i = 0; i < currentlyHiddenSelects.length; i++) {
			var sel = currentlyHiddenSelects[i];
			sel.style.visibility="visible";
		}
	}
	currentlyHiddenSelects = null;
}	

//onkeypress="return textAreaLimitInsert('myId', 100)" onkeyup="textAreaTruncate('myId', 100)"
function textAreaLimitInsert(textAreaId, maxLength) { 
    textAreaObj = document.getElementById(textAreaId);
    if (textAreaObj.value.length >= maxLength)
    { 
        return false;
    }
}

function textAreaTruncate(textAreaId, maxLength) { 
    textAreaObj = document.getElementById(textAreaId);
    if (textAreaObj.value.length>maxLength){ 
		textAreaObj.value=textAreaObj.value.substring(0,maxLength);
    }  
} 
function checkEnter(e,frm){ //e is event object passed from function invocation and frm is the form name
	var characterCode;         
	characterCode = e.keyCode; //character code is contained in IE's keyCode property
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		frm.submit(); //submit the form         
	}
}
function checkCheckoutEnter(e,f){ //e is event object passed from function invocation and f is the element name
	var characterCode;  
	characterCode = e.keyCode; //character code is contained in IE's keyCode property
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		document.getElementById(f).click(); //click the button        
	}
}
function doFocusButton(f){ // f is the button name where focus wants during onloading the screen
         var useridFocus = document.getElementById(f);
         if(useridFocus != null)
             useridFocus.focus();
}
function submitAddressFormOnEnter(e,f){ //e is event object passed from function invocation and f is the element id
  var characterCode;  
  var objId;
  var targ;
  if (!e) var e = window.event;
	if (e.target){
    targ = e.target;   
  }else if (e.srcElement){ 
    targ = e.srcElement;   
  }	
  if(targ){
   if (targ.nodeType == 3){// defeat Safari bug		
       targ = targ.parentNode;       
    }  
    objId = targ.id;  
   var desiredIndex = objId.indexOf('Postcode');     
   if (desiredIndex!= -1){     
      f = objId.substring(0, desiredIndex)+ 'FindAddress';    
   }else{
       desiredIndex = objId.indexOf('Selectedaddress'); 
      if (desiredIndex!= -1){     
         f = objId.substring(0, desiredIndex)+ 'SelectAddress';    
      }
   }   
  }  
	characterCode = e.keyCode; //character code is contained in IE's keyCode property
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)        
		document.getElementById(f).click(); //submit the form	     
	}
}


function checkIsIE6() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (/msie[\/\s](\d+\.\d+)/.test(userAgent)) {
		var ieversion = new Number(RegExp.$1);
		return (ieversion < 7);
	} else {
		return false;
	}
}

function changeUrlParam(originalUrl, paramName, value) {
	var paramMatch = '?' + paramName + '=';
	var paramIndex = originalUrl.indexOf(paramMatch);
	if (paramIndex > -1) {
		var urlAfterIndex = originalUrl.indexOf('&', paramIndex + 1);
		if (urlAfterIndex > -1) {
			return originalUrl.substring(0, paramIndex) + paramMatch + value + originalUrl.substring(urlAfterIndex);
		} else {
			return originalUrl.substring(0, paramIndex) + paramMatch + value;
		}
	}
	paramMatch = '&' + paramName + '=';
	paramIndex = originalUrl.indexOf(paramMatch);
	if (paramIndex > -1) {
		var urlAfterIndex = originalUrl.indexOf('&', paramIndex + 1);
		if (urlAfterIndex > -1) {
			return originalUrl.substring(0, paramIndex) + paramMatch + value + originalUrl.substring(urlAfterIndex);
		} else {
			return originalUrl.substring(0, paramIndex) + paramMatch + value;
		}
	}
	if (originalUrl.indexOf('?') > 0) {
		return originalUrl + paramMatch + value;
	} else {
		return originalUrl + '?' + paramName + '=' + value;
	}
}
	

