//global initializations
attachLoadEvent(mapExternalLinks);
attachLoadEvent(calculateContentHeight);
window.onresize = calculateContentHeight;

// Matco Tools Global JavaScript Document
//------------------------------------------------------------------------

//(cross-browser) use to attach functionality of a function to a page when it loads
function attachLoadEvent(loadEvent) {
	if (window.addEventListener) {
		window.addEventListener("load", loadEvent, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", loadEvent);
	} else if (window.onload) {
		window.onload = loadEvent;
	}
}

//returns a collection of elements based on class name
document.getElementsByClassName = function(clsName){
	if (!document.getElementsByTagName) return;
	var retVal = new Array();
	var elements = document.getElementsByTagName("*");
	for(var i = 0;i < elements.length;i++){
		if(elements[i].className.indexOf(" ") >= 0){
			var classes = elements[i].className.split(" ");
			for(var j = 0;j < classes.length;j++){
				if(classes[j] == clsName)
					retVal.push(elements[i]);
			}
		}
		else if(elements[i].className == clsName)
			retVal.push(elements[i]);
	}
	return retVal;
}

//maps <a> links with attribute rel="external" to open in a new window
function mapExternalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target="_blank";
		}
	}
}

function calculateContentHeight() {
	var content = document.getElementById("content");
	var windowHeight = (window.innerHeight ? window.innerHeight : document.documentElement.clientHeight);
	var headerHeight = document.getElementById("header").offsetHeight;
	var footerHeight = document.getElementById("footer").offsetHeight;
	var contentHeight = content.offsetHeight;
	if(content.origHeight === undefined) {
		content.origHeight = contentHeight;
	}
	var dividerHeight = (document.getElementsByClassName("divider")[0].offsetHeight)*2;//there are two dividers in the layout
	var newHeight = windowHeight - headerHeight - footerHeight - dividerHeight;
	
	if(newHeight > content.origHeight) {
		content.hasResized = true;
		content.style.height = (windowHeight - headerHeight - footerHeight - dividerHeight) + "px";
	} else if(content.hasResized){
		content.style.height = content.origHeight + "px";
	}
}

//(cross-browser) finds the Y position of an object from the top of the page 
function findTopPosition( oLink ) {
	if( oLink.offsetParent ) {
   		for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
     			posX += oLink.offsetLeft;
     			posY += oLink.offsetTop;
   		}
   		return posY;
	} else {
   		return oLink.y;
	}
}

//used to set variables for tool catalog functionality
var agt=navigator.userAgent.toLowerCase();
var ie4 = (document.all) ? 1 : 0;
var ns6 = (document.getElementById && !document.all) ? 1 : 0;
var showmenu = 0;
if (ie4 == 1) { showmenu = 1;}
if (agt.indexOf("opera") != -1) {
    showmenu = 0;
}
if (agt.indexOf("mac") != -1) {
    showmenu = 0;
}

//script for add cart button (originated in header)
function addCart(longItemId, theForm) {
    theForm.quantity.value = 1;
    theForm.item.value = longItemId;
    theForm.submit();
}


//rollover and preloader functions for tabs, etc (originated in header)
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_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_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];}
}


//display script for default enewsletter
function displayWindow(url, width, height) {

        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=yes,scrollbars=yes,menubar=no');

        Win.resizeTo(width + 10, height + 10);

        Win.focus();
}

// Function will move the focus to the nextInput after the maxLength
// has been reached.  Also, this function will allow only numbers to
// be typed in the input fields.  If the nextInput equals "no", the 
// function won't move to any specified input field.

function moveFocus_NumOnly(thisInput, maxLength, nextInput)
{
	thisInput.value = thisInput.value.replace(/[^\d]+/g, ''); 

	if(thisInput.value.length == maxLength){
		if(nextInput != "no")
		{
			if(nextInput.value.length == 0)
				nextInput.focus();
				return;
		}
	}	
}

function chrOnly(i)
{
	if(i.value.length>0)
	{
		i.value = i.value.replace(/[^\D]+/g, ''); 
	}
}

function intOnly(i)
{
	if(i.value.length>0)
	{
		i.value = i.value.replace(/[^\d]+/g, ''); 
	}
}

function onlyDigitsDecimals(i)
{
	if(i.value.length>0)
	{
		i.value = i.value.replace(/[^0-9.]+/g, ''); 
	}
}


function changeBorder(containerID, onORoff){
	div = document.getElementById(containerID);
	if(onORoff == "on"){
		div.style.borderLeft = "2px solid #ffffff";	
	}else{
		div.style.borderLeft = "2px solid #005592";	
	}
}