// Common JavaScript functions

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function doImageButtonMove(objX, bUp) {
	//background: url(/Templates/Australia/images/catalogue.jpg) repeat 0px -115px; 
	objX.style.backgroundPosition = '0px ' + (bUp ? '-115' : '0') + 'px';
	
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


function doStartMenu() {
	
	anylinkmenu.init("menuanchorclass");
	
}


function tabSelected(idTabSelected) {
	
	var tabSelected = document.getElementById(idTabSelected);
	var idSelected  = getCurrentlySelected();
	
	if (idSelected!=-1) {
		var currTab 	= document.getElementById(idSelected);
		// Make the old tab inactive
		currTab.className 		= "inactive_tab_centre";
		document.getElementById(idSelected + '_left').style.background  = "url('/images_common/Product_pages/inactive_tab_left.gif')";
		document.getElementById(idSelected + '_right').style.background = "url('/images_common/Product_pages/inactive_tab_right.gif')";
		document.getElementById(getPageName(currTab.innerHTML).toLowerCase() + "_content.asp").style.display='none';
	}
	
	
	// Make the new tab active
	tabSelected.className 	= "active_tab_centre";
	document.getElementById(idTabSelected + '_left').style.background  = "url('/images_common/Product_pages/active_tab_left.gif')";
	document.getElementById(idTabSelected + '_right').style.background = "url('/images_common/Product_pages/active_tab_right.gif')";
	document.getElementById(getPageName(tabSelected.innerHTML).toLowerCase() + "_content.asp").style.display='';
		
}

// Function to run when the user moves the cursor over a tab
function mouseOverTab(tabID) {
	document.body.style.cursor = 'pointer';
}

// Function to run when the user moves the cursor off a tab
function mouseOutTab(tabID) {
	document.body.style.cursor = 'default';
}

// Returns the current selected tab id
function getCurrentlySelected() {
	
	var testTab, currSelected;
	
	currSelected = -1;
	
	for (var i=1; i<=20; i++) {
		testTab = document.getElementById('tab_' + i);
		if (testTab) {
		if (testTab.className=='active_tab_centre') {
				currSelected = i;
				break;
			}
		}
	}
	
	return 'tab_' + currSelected;
	
}

function getPageName(alphane) {
	
	var toReturn='';
	for(var j=0; j<alphane.length; j++) {
		  var alphaa = alphane.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  
		  if (alphaa==' ') alphaa="_";
		  
		  if ( (hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || (alphaa=='_') || (alphaa=='-')) {
			  toReturn = toReturn + alphaa;
		  }
	}
	
	//alert(toReturn);
	return toReturn;

}

function selectFirstTab() {
	var tab = document.getElementById('tab_1');
	if (tab) tabSelected('tab_1');
}

// this function is run everytime a page loads.
// it's used to 'jump' to a tab on first load of the page.
// when using the search function, the tab name is passed thru, otherwise it'll default to the first
function jumpToTab(tabNm) {
	
	if ((tabNm!=null)&&(tabNm!='')) {

		var iTabIndex=1;
		var sTabName;
		var bFound = false;
		var objTab = null;
		
		while ((document.getElementById('tab_' + iTabIndex)) && (!bFound)) {

			sTabName = (getPageName(document.getElementById('tab_' + iTabIndex).innerHTML)).toLowerCase();
			
			//alert("Looking at " + sTabName + " against " + tabNm);
			
			if (tabNm==sTabName) { // got it
				objTab = document.getElementById('tab_' + iTabIndex);
				bFound = true;
				
			} else {
				iTabIndex++;
				
			}
			
			if (iTabIndex>=20) {
				bFound = false;
				objTab = null;
			}
			
			
		}
		
		if ((bFound)&&(objTab!=null)) {
			tabSelected('tab_' + iTabIndex);
		} else {
			selectFirstTab();
		}
		
	} else {
		selectFirstTab();
	}
	
}

/***************************************************************/

function entsub(event,ourformName) {

	if (event && event.which == 13) {
		if (ourformName=='StoreLocator') {
			storeSearch();
		} 
		  
	} else {
	  
		if ( event.keyCode == 13) {
			if (ourformName=='StoreLocator') {
				storeSearch();
			} 
			  
		} else {  
		
			return true;
			
		}
	}
	
	
}


function searchSite() {
	
	var sSearchString = document.forms['SearchSite'].searchstring.value;
	
	if (sSearchString=='') {
		alert('Please enter an appropriate search text');
		
	} else if (sSearchString.length>30) {
		alert('Please limit the search text to 30 characters or less.');
		
	} else {
		document.forms['SearchSite'].submit();
		
	}
	
	return false;
	
}


function storeSearch() {
	
	sPost = document.forms['StoreLocator'].postcode.value;
	sState="";
	
	if (document.forms['StoreLocator'].state.selectedIndex!=0) {

		sState = document.forms['StoreLocator'].state[document.forms['StoreLocator'].state.selectedIndex].text;
		
	}
	
	if ( (sPost=='') || (sPost=='Enter Postcode/Suburb') || (removeSpaces(sPost)=='') ) {
		alert('Please Enter A Suburb or Postcode to Look For');
	
	} else {
	
		var sURL = "/Home_improvement_store/StoreLocator/StoreSearch.asp?postcode=" + sPost + "&state=" + sState;
		window.open(sURL, 'StoreLocator', 'width=820,height=550,scrollbars=yes');
	
		//sURL = 'http://www.stratcodirect.com/storelocator/findStore.do?SAction=SEARCH&searchString=' + sPost  + '&State=' + sState;
		//winX = window.open(	sURL,'storelocator','width=720,height=500,scrollbars=yes');	
		//window.location.href = sURL;		
	}

	return false;
	
}


function removeSpaces(string) {
	
	var tstring = "";
	
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	
	return tstring;
	
}



function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function storeFocus() {

	if (document.forms['StoreLocator'].postcode.value=='Enter Postcode/Suburb') {
		document.forms['StoreLocator'].postcode.value='';
	}
	
}


function storeRemoveFocus() {
	
	if (document.forms['StoreLocator'].postcode.value=='') {
		document.forms['StoreLocator'].postcode.value='Enter Postcode/Suburb';
	}
	
}






function menuhighlighton(objX) {

	objX.className = 'menulinkRollover';

}


function menuhighlightoff(objX, classname) {

	if (menuhighlightoff.arguments[1]!=null ) {
		objX.className= classname;
		
	} else {
		objX.className = "menulink";
		
	}
	
	
}

iRowCount = 0;
function toggleCategory() {
	// check that browser supports getElementById
	if (!document.getElementById) {
		return false;
	}
	
	iRowCount=0;
	setTimeout('hideRow()', 20);
}

function hideRow() {

	objX = document.getElementById('toggle' + iRowCount);
	
	if (objX) {
		if (objX.style.display=='') {
			objX.style.display='none';
		} else {
			objX.style.display='';
		}
	
		iRowCount++;
	
		setTimeout('hideRow()', 20);
	} 

}

function searchRowHover(objX) {

	objX.style.backgroundColor='#EBEBEB';
	document.body.style.cursor = 'hand';

}

function searchRowOut(objX) {

	objX.style.backgroundColor='#FFFFFF';
	document.body.style.cursor = 'default';
}


function searchStoreRowHover(objX) {
	
	var tds = objX.getElementsByTagName('td');
	
	for (var i=0; i < tds.length; i++) {
		tds[i].style.backgroundColor = '#EBEBEB';
	}
	
	document.body.style.cursor = 'hand';
	
}


function searchStoreRowOut(objX) {
	
	var tds = objX.getElementsByTagName('td');
	
	for (var i=0; i < tds.length; i++) {
		tds[i].style.backgroundColor = '#FFFFFF';
	}
	
	document.body.style.cursor = 'default';

}


function launchURL(sURL, sWidth, sHeight, bAllFunctions) {

	sAll = "no";
	if ((bAllFunctions!=null) && (bAllFunctions==true)) {
		sAll = "yes";
	}

	sFeatures="width=" + sWidth + ",height=" + sHeight + ",status=no,scrollbars=yes,toolbar=" + sAll + ",menubar=" + sAll + ",resizable=" + sAll;
	x = window.open(sURL, "stratcoURL", sFeatures);
	x.focus();
	
}


function selectProductBrochures() {
	
	document.getElementById('brochurePanel').style.display		= '';
	document.getElementById('availabilityPanel').style.display	= 'none';
	document.getElementById('galleryPanel').style.display		= 'none';
	
}


function selectProductImages() {
	
	if (document.getElementById('galleryPanelNoImages')) { // a dummy DIV to indicate no gallery images.
		
		alert("There are currently no images available for this product.");
		
	} else {
	
		document.getElementById('brochurePanel').style.display		= 'none';
		document.getElementById('availabilityPanel').style.display	= 'none';
		document.getElementById('galleryPanel').style.display		= '';
	
	}
	
}


function selectProductAvailability() {
	
	document.getElementById('brochurePanel').style.display		= 'none';
	document.getElementById('availabilityPanel').style.display	= '';
	document.getElementById('galleryPanel').style.display		= 'none';
	
}


function modelesswin(url,mwidth,mheight,sTitle) {
	
	var newWin;

	if (document.all&&window.print) {
		
		eval('newWin = window.showModelessDialog(url,"popUpWindow","help:0;resizable:1;dialogWidth:'+mwidth+'px;dialogHeight:'+mheight+'px")');
	
	} else {
		
		eval('newWin = window.open(url,"popUpWindow","width='+mwidth+'px,height='+mheight+'px,resizable=1,scrollbars=1")');
		
	}
	
	if (sTitle!=null) {
		newWin.document.write("<HTML>");
		newWin.document.write("<HEAD>");
		newWin.document.write("<TITLE>" + sTitle + "</TITLE>");
		newWin.document.write("</HEAD>");
	}
	
}


function isInt(sVal) {
	
	if ((sVal==null) || (sVal=="")) {
		return false;
	}
	
	var sCurrChar="";
	
	for (var i=0; i < sVal.length; i++) {
		
		sCurrChar = sVal.charAt(i);
		
		if (   (sCurrChar!="0") && (sCurrChar!="1") && (sCurrChar!="2") && (sCurrChar!="3") && (sCurrChar!="4") && (sCurrChar!="5")
			&& (sCurrChar!="6") && (sCurrChar!="7") && (sCurrChar!="8") && (sCurrChar!="9")	) {
			
			return false;
			
		}
		
	}
	
	return true;
	
}

// function to format a number with separators. returns formatted number.
// num - the number to be formatted
// decpoint - the decimal point character. if skipped, "." is used
// sep - the separator character. if skipped, "," is used
function FormatNumberBy3(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}


