
// Show/hide search filters pop-out collection:
function toggleSearchFilters(el) {
	var thisEl = document.getElementById(el);
	thisEl.style.display = ((thisEl.style.display=="none" || thisEl.style.display=="") ? "block" : "none");
}
//-------------------------------------------------------------------------------------------------

// Basic hide-element func:
function hideEl(el) {
	thisEl = document.getElementById(el);
	thisEl.style.display = "none";
}
//-------------------------------------------------------------------------------------------------

// Basic show-element func:
function showEl(el) {
	var thisEl = document.getElementById(el);
	thisEl.style.display = "block";
}
//-------------------------------------------------------------------------------------------------

// Class changer:
function doChangeClass(theObj, theClass) {
	document.getElementById(theObj).className = theClass;
}
//-------------------------------------------------------------------------------------------------

// Pop a window, fixed pos but settable size:
function popWin(theURL, w, h) {
	//var popWin = window.open(theURL, 'popHandle'+(Math.floor(Math.random()*100)), 'scrollbars=yes,status=no,toolbar=no,menubar=no,resizable=yes,screenX=50,screenY=50,top=50,left=50,width='+w+',height='+h); return true;
	var popWin = window.open(theURL, 'popHandle_1', 'scrollbars=yes,status=no,toolbar=no,menubar=no,resizable=yes,screenX=50,screenY=50,top=50,left=50,width='+w+',height='+h); return true;
}
//-------------------------------------------------------------------------------------------------

// Toggle show / hide
function toggleShowHide(el) {
	var thisEl = document.getElementById(el);
	thisEl.style.display = ( (thisEl.style.display=="none"  ||  thisEl.style.display=="") ? "block" : "none");
}
//-------------------------------------------------------------------------------------------------

// Toggle show / hide for search expander
function toggleExpanderShowHide(el) {
	var thisEl = document.getElementById(el);
	displayMode = thisEl.style.display;

	if (displayMode == "block") { thisEl.style.display = "none"; }
	else if (displayMode == "none") { thisEl.style.display = "block"; }
	else if (displayMode == "") { thisEl.style.display = "none"; }
	//thisEl.style.display = ( (displayMode=="block"  ||  displayMode=="") ? "none" : "block");

	if (thisEl.style.display=="none") { document.getElementById('prodSrchFormExpander').className = "expanderIconPlus"; }
	else if (thisEl.style.display=="block") { document.getElementById('prodSrchFormExpander').className = "expanderIconMinus"; }
}
//-------------------------------------------------------------------------------------------------

// CSS class mass action: hide
// Future: mod for other actions
function classAction_Hide(theClass) {
	var allTags = document.getElementsByTagName('*');
	for (i=0; i<allTags.length; i++) {
		if (allTags[i].className == theClass) { allTags[i].style.display = 'none'; }
	}
}
//-------------------------------------------------------------------------------------------------

// CSS class mass actioner
function classMassAction(theClass, theAction) {
	var allTags = document.getElementsByTagName('*');
	if (theAction == "hide") {
		for (i=0; i<allTags.length; i++) { if (allTags[i].className == theClass) { allTags[i].style.display = 'none'; } }
	}
	if (theAction == "show") {
		for (i=0; i<allTags.length; i++) { if (allTags[i].className == theClass) { allTags[i].style.display = 'block'; } }
	}
	return;
}
//-------------------------------------------------------------------------------------------------

// Because IE doesn't support getElementsByClassName
function getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	if (node == null ) { node = document; }
	if (tag == null) { tag = '*'; }
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) { classElements[j] = els[i]; j++; }
	}
	return classElements;
}
//-------------------------------------------------------------------------------------------------

// Change tab classes, tab body and the tab l/h and r/h ends.
// Show the content we want to see, and hide the rest.
// NOTE - this depends on the same number of tabs as tabbedContent[i] blocks!
// And indexing - start from 0. Match tab 'index' to block 'index' i.e. tab0 = content0
function doTabs(thisID) {
	var i = 0;
	// Collect all the tabs
	var allTabs = getElementsByClass('btnTabBase', null, null); // No document.getElementsByClassName, so use custom func

	// For each tab, set class on LH, middle, RH:
	for (i=0; i<allTabs.length; i++) {
		if (i != thisID) {
			// OFF / HIDE
			document.getElementById('btnTab_'+i).className = "btnTabBase btnTab-Off";
			document.getElementById('tabLH_'+i).className = "btnTabLHEnd btnTabLHS-off";
			document.getElementById('tabRH_'+i).className = "btnTabRHEnd btnTabRHS-off";
			hideEl('tabbedContent'+i);
		} else {
			//ON / SHOW
			document.getElementById('btnTab_'+i).className = "btnTabBase btnTab-On";
			document.getElementById('tabLH_'+i).className = "btnTabLHEnd btnTabLHS-on";
			document.getElementById('tabRH_'+i).className = "btnTabRHEnd btnTabRHS-on";
			showEl('tabbedContent'+i);
		}
	}
}
//-------------------------------------------------------------------------------------------------

// Roll funcs together, for Suppliers home page A-Z selector control
function setSupplierTabContent(thisID, startLetter) {
	// Sort the tabs, AJAX some data:
	doTabs(thisID);
	AJAXgetSupplierData(startLetter);

	// Collect the tabs. Show the one we want, hide the rest:
	var allTabs = getElementsByClass('btnTabBase', null, null);
	for (i=0; i<allTabs.length; i++) {
		if (i != thisID) { hideEl('tabbedContent'+i); }
		else { showEl('tabbedContent'+i); }
	}
}
//-------------------------------------------------------------------------------------------------

// Supplier home page: set the <select> class in the A-Z dropdown tab to it's "on" state, when
// either a letter in the block is clicked, or the drop-down itself is changed.
// Or to it's "off" state when the "show all" tab is clicked.

function setSupplierTabDDclass(state) {
	if (state == "on")  { doChangeClass('azSelectorDD', 'floatLeft tabInput-On'); }
	if (state == "off") { doChangeClass('azSelectorDD', 'floatLeft tabInput-Off'); }
}
//-------------------------------------------------------------------------------------------------

// For user profile landing page: nav buttons and hint blocks
function doProfileHintBlocks(id, action, numButtons) {
	//var numButtons = 4;
	if (numButtons === undefined) { numButtons = 1; }

	if (action == 'hide') {
		for (i=1; i<=numButtons; i++) { hideEl('navHint'+i); }
	}
	
	if (action == 'show') {
		// Show the one we want:
		showEl('navHint'+id);
		// Hide all except our "on" hint:
		for (i=1; i<=numButtons; i++) {
			if (i != id) { hideEl('navHint'+i); }
		}
	}
}
//-------------------------------------------------------------------------------------------------

// Check that a pop window has a parent, i.e. was opened from another page as intended, and not http'd directly (e.g. search engine result).
// Param is pixel width: needs corresponding CSS class, e.g. .w500 and so on

function checkPopHasParent(w) {
	if (window.opener == null) {
		document.write('<div class="errWrapper errWrapperPopTop w'+w+'">');
		document.write('<p class="errHeading">Warning<\/p>');
		document.write('<p>This page is a pop-up window, but has not been opened from it\'s parent page. Content may be incomplete.<\/p>');
		document.write('<p>Please visit the main website by <a href="/">clicking here<\/a>.<\/p>');
		document.write('<\/div>');
	}
}
//-------------------------------------------------------------------------------------------------

// Check that a pop window has a parent, i.e. was opened from another page as intended, and not http'd directly (e.g. search engine result).
// Param is the DOM ID of a container which will hold the generated error message.
// Could be expanded to send browser to a full-page version of the pop window layout.
function XXcheckPopHasParent(errmsgContainerId) {
	if (window.opener == null) {
		popErrMsg = '<p class="errHeading">Warning<\/p>';
		popErrMsg += '<p>This page is a pop-up window, but has not been opened from it\'s parent page. Content may be incomplete.<\/p>';
		popErrMsg += '<p>Please visit the main website by <a href="/">clicking here<\/a>.<\/p>';
		document.getElementById(errmsgContainerId).innerHTML = popErrMsg;
		document.getElementById(errmsgContainerId).style.display = "block";
	}
}
//-------------------------------------------------------------------------------------------------

// For pages with tabbed content. It sets all tabbed content visible, so the page can be printed with all it's content - not just current tab.
function doPrepTabsAndPrint(theClass) {
	classMassAction(theClass, 'show');

	// Collect the tabs. Make first visbile and "on", hide the res:
	var allTabs = getElementsByClass('btnTabBase', null, null);
	for (i=0; i<allTabs.length; i++) {
		if (i==0) {
			doChangeClass('btnTab_0', 'btnTabBase btnTab-On');
			showEl('btnTab_0');
		}
		else { hideEl('btnTab_'+1); }
	}
	window.print();
}
//-------------------------------------------------------------------------------------------------

/**
 * set the basket item number in the page header
 * 
 * for example if the number if affected by an ajax 
 * call but we don't want to reload the whole page
 */
function setBasketItems(ii){
	$('#mhBasketItemsCount a span').html(ii);
}

/**
 * add a single product to the basket
 * (updates item number display on success)
 */
function addToBasket(id){
	var url = '/site/api/?method=AddToBasket&id='+id;
	$.facebox({ajax: url});		
}

/**
 * add a product to favourites
 */
function addToFavourites(id){
	var url = '/site/api/?method=AddToFavourites&id='+id;
	$.facebox({ajax: url});		
}
