/**************************************************
***************************************************
***  Generic Fastenal Scripts          		***
***                                             ***
***                                             ***
***                                             ***
***************************************************
**************************************************/

// Container object for global variables
var FASTENAL = {};

//Cart summary	
FASTENAL.globalCurrent;	// global references to current element
FASTENAL.globalRelated;
FASTENAL.current;	// setTimeout() call for cart summary delay
FASTENAL.related;
FASTENAL.time;

var preventCartState = false;

/* comment out for now, so web studio preview app can work
if (top.location!=self.location){
    top.location.replace(self.location);
}
*/

var fastenal = {
    byId: function(obj) {
        return document.getElementById(obj);
    },
    
    byClass: function(obj) {
        return document.getElementsByClassName(obj);
    },
    
    gotoUrl: function(url) {
        window.location = url;
    },
    
    toggle: function(obj, link) {
        obj = document.getElementById(obj);

        if (obj) {
          if (getStyle(obj,"display") == "block")
              obj.style.display = "none";
          else obj.style.display = "block";

          i = 0;
          
          // toggles max/min child nodes if present
          if (link) {
            for (i=0;i<link.childNodes.length;i++) {
                if (link.childNodes[i].className == "maximize") {
                    link.childNodes[i].className = "minimize";
                } else if (link.childNodes[i].className == "minimize") {
                    link.childNodes[i].className = "maximize";
                }
            }
          }
        }        
    },
    
    setAllCheckboxes: function(FormName, FieldName, CheckValue) {
        SetAllCheckboxes(FormName, FieldName, CheckValue);
    },
    
    enableOn: function(str, obj, target) {
        if (obj.value == str) {
            target.disabled = false;
            target.focus();
        } else {
            target.value = "";
            target.disabled = true;
        }
    },
    
  addLoadEvent: function(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = func;
    } else {
      window.onload = function() {
        if (oldonload) oldonload();
        func();
      }
    }
  }
};

function showPopup(ref) {
  var location = ref.getAttribute("href");
  
  if (location != null || location != "") {
    var name = "fcom_help_popup";
    var features = "width=500,height=400,";
    features += "directories=no,location=no,menubar=no,";
    features += "resizable=no,scrollbars=yes,status=yes,toolbar=no";
    popupUrl(location, name, features);
    
  }
    
  return false;
}

// open a popup window with params

function popupUrl(url, name, features) {

	var newWindow = window.open(url, name, features);

	//newWindow.focus();
}

function SetAllCheckboxes(FormName, FieldName, CheckValue) {
    // Required: FormName = The name of the form that will have Checkboxes modified.
    // Required: FieldName = The name of the checkboxes that will be modified.
    // Required: CheckValue = true(selected) or false(deselected)
    if (!document.forms[FormName]) return;
    var objCheckBoxes = document.forms[FormName].elements[FieldName];
    if (!objCheckBoxes) return;
    var countCheckBoxes = objCheckBoxes.length;
    if (!countCheckBoxes) objCheckBoxes.checked = CheckValue;
    else {
        for(var count=0;count<countCheckBoxes;count++) {
        objCheckBoxes[count].checked = CheckValue;
        }
    }
}










//Generic Useful Code
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function findPos(obj) {
  var curleft = 0;
  var curtop = 0;
  
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent); /* this line is correct, this will return false when obj.offsetParent == 'HTML' */
    
    return [curleft, curtop];
  }
}

function insertAfter(parent, newElement, referenceElement){
  parent.insertBefore(newElement, referenceElement.nextSibling);
} 

function insertLast(el, newEl) {
    if (el) {
    
    alert(el.parent.nodeType);
	
	if (el.parent) {
	    var parentNode = el.parent;
	    
	    var lastChild = parentNode.lastChild;
	    
	    lastChild.nextSibling = newEl;
	    
	    alert(newEl + "inserted after " + el);
	}
    }
}

//create function, it expects 2 values.
function insertAfter(newElement,targetElement) {
	//target is what you want it to go after. Look for this elements parent.
	var parent = targetElement.parentNode;
	
	//if the parents lastchild is the targetElement...
	if(parent.lastchild == targetElement) {
		//add the newElement after the target element.
		parent.appendChild(newElement);
		} else {
		// else the target has siblings, insert the new element between the target and it's next sibling.
		parent.insertBefore(newElement, targetElement.nextSibling);
		}
}

function rmWhiteSpace(str) {
    spaceFree = str.replace(/\s+/g,'');
 
    return spaceFree;   
}

// Creates the isContainedIn function
if ( !window.Element ) {
	Element = function() {};
  
  var element = new Element;
  _isContainedIn = element.isContainedIn;
}

function objContainsThis(obj, target) {
  var target = document.getElementById(target);
  var obj = document.getElementById(obj);

  do {
    if (target.id == obj.id) return true;
  } while (obj = obj.parentNode);
  
  return false;
}
// The following code is to support IE...  THIS CODE NEEDS TO STAY
if (!document.getElementsByClassName) { // This check makes sure the default class isn't over written.
    document.getElementsByClassName = function(className) {
        var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
        var allElements = document.getElementsByTagName("*");
        var results = [];
        
        var element;
        for (var i = 0; (element = allElements[i]) != null; i++) {
            var elementClass = element.className;
            if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
                results.push(element);
        }
        
        return results;
    }
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  return [ myWidth, myHeight ];
}

function addLoadEvent(func) { 
    var oldonload = window.onload; 
    
    if (typeof window.onload != 'function') { 
      window.onload = func; 
    } else { 
      window.onload = function() { 
        if (oldonload) { 
          oldonload(); 
        }
        
        func(); 
      } 
    } 
} 

function setPath( sExec , sParam ) {
  var fullPath = window.location.toString();
  var endLocation = fullPath.indexOf(SITE_ROOT, SITE_ROOT.length);
  var newPath = SITE_ROOT + sExec + "?" + sParam;
      
  return newPath;
}

function getStyle(elem, name) {
    // J/S Pro Techniques p136
  if (elem) {
    if (elem.style[name]) {
        return elem.style[name];
    } else if (elem.currentStyle) {
        return elem.currentStyle[name];
    }
    else if (document.defaultView && document.defaultView.getComputedStyle) {
        name = name.replace(/([A-Z])/g, "-$1");
        name = name.toLowerCase();
        s = document.defaultView.getComputedStyle(elem, "");
        return s && s.getPropertyValue(name);
    } else {
        return null;
    }
  }
}

function preloadImage(path) {
  pic1= new Image(100,100); 
  pic1.src= path; 
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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 eraseCookie(name) {
	createCookie(name,"",-1);
}

function isVisible(e) {
  //returns true is should be visible to user.
  if (typeof e == "string") {
    e = document.getElementById(e);
  }
  while (e.nodeName.toLowerCase() != 'body' && e.style.display.toLowerCase() != 'none' && e.style.visibility.toLowerCase() != 'hidden') {
    e = e.parentNode;
  }
  if (e.nodeName.toLowerCase() == 'body') {
    return true;
  } else{
    return false;
  }
}

function getParameter( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function popWindow(D,C,E,B) {
  var A=(screen.width-E)/2;
  var H=(screen.height-B)/2;
  var F="height="+B+",width="+E+",screenX="+A+",left="+A+",screenY="+H+",top="+H+",location=no,scrollbars=yes,menubar=no,resizable=no,status=no,toolbar=no";
  var G=window.open(D,C,F);
  G.focus();
}