// Global JavaScript Document

// Declare variables
var browserName = navigator.appName; 
var browserVer = parseInt(navigator.appVersion);

// Image swapping functions
function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

// Validates existence of a value in the Find Product form
function validateFindProductForm(form) {
	var FormValid = false;
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == 'text') {
			if (form.elements[i].value != '') FormValid = true;
		}
	}
	if (FormValid) return true;
	else {
		alert("Please enter one or more search terms.");
		return false;
	}
}

// Asks for confirmation to proceed, or cancel
function clickConfirm(theMessage) {
	if (confirm(theMessage)) return true;
	else return false;
}

// Returns a random number
function rand(number) {
	return Math.ceil(Math.random()*number);
}

// Function to set the visitor cookie
function SetClickTracksCookie() {
	var thisCookie = GetCookie("CLICKTRACKSCOOKIE");
	if (thisCookie == null) {
		//Setup the random cookie value
		var myValue = new Date();
		var randNum = rand(100);		
		myValue = myValue.toGMTString() + "_" + randNum;
		//Setup the expiry date to expire in 2030
		var expiryDate = new Date();
		var date2010 = "Fri, 31 Dec 2030 23:00:00 EST";
		var dt = Date.parse(date2010);
		expiryDate.setTime(dt);
		SetCookie("CLICKTRACKSCOOKIE", myValue, expiryDate, "/", window.location.hostname);
	}
}

// Cookie get/set/delete functions
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}
function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// Sets the visitor cookie if it does not exist
SetClickTracksCookie();

// Resizes the current browser window
function resizeWindow(valX, valY) {
	var maxX = Math.min(valX, screen.availWidth - 50);
	var maxY = Math.min(valY, screen.availHeight - 50);
	var moveX = 0;
	var moveY = 0;
	if (window.screenLeft) {
		if (window.screenLeft + maxX > screen.availWidth) moveX = screen.availWidth - (window.screenLeft + maxX);
		if (window.screenTop + maxY > screen.availHeight) moveY = screen.availHeight - (window.screenTop + maxY);
		window.moveBy(moveX, moveY);
	} else if (window.screenX) {
		if (window.screenX + maxX > screen.availWidth) moveX = screen.availWidth - (window.screenX + maxX);
		if (window.screenY + maxY > screen.availHeight) moveY = screen.availHeight - (window.screenY + maxY);
		window.moveBy(moveX, moveY);
	} else {
		window.moveTo(0, 0);
	}
	if (window.innerWidth) {
		iWidth = window.innerWidth;
		iHeight = window.innerHeight;
	} else if (document.documentElement.clientWidth) {
		iWidth = document.documentElement.clientWidth;
		iHeight = document.documentElement.clientHeight;
	} else {
		iWidth = document.body.clientWidth;
		iHeight = document.body.clientHeight;
	}
	iWidth = maxX - iWidth;
	iHeight = maxY - iHeight;
	window.resizeBy(iWidth, iHeight);
}



// Define all of the rollover button graphics
var rolloverImages = new Array('nav_home',
							   'nav_shopping_list',
							   'nav_store_locator');

// Sets up image rollovers
function setupRollovers() {
	// Get all of the images on this page and iterate through them to determine if any are rollovers
	var pageImages = document.getElementsByTagName("img");
	for (var x = 0; x < pageImages.length; x++) {
		// Get the base name of the current image
		var tmparr1 = pageImages[x].src.split('/');
		var tmparr2 = tmparr1[tmparr1.length - 1].split('.');
		var imgname = tmparr2[0];
		// Check the current image name against the list of rollovers and see if there is a match
		for (var i = 0; i < rolloverImages.length; i++) {
			if (imgname == rolloverImages[i]) {
				// Preload rollover image
				var normalURL = '/images/interface/' + imgname + '.gif';
				var rolloverURL = '/images/interface/' + imgname + '_on.gif';
				// Setup rollover events
				addRollover(pageImages[x], normalURL, rolloverURL);
				// Exit the loop
				break;
			}
		}
	}
}

// Image button rollover handler
function addRollover(img, normalURL, rolloverURL) {
	preloadImages(rolloverURL);
	img.onmouseover = function() { img.src = rolloverURL; }
	img.onmouseout = function() { img.src = normalURL; }
}

// Image preloading function
function preloadNav() {
	preloadImages('/images/interface/nav_customer_info_on.gif',
				  '/images/interface/nav_hand_tools_on.gif',
				  '/images/interface/nav_hardware_on.gif',
				  '/images/interface/nav_lighting_electrical_on.gif',
				  '/images/interface/nav_power_tools_on.gif');
}

// Pricing OnLoad directive
function globalLoad() {
	// Preload images
	preloadNav();
	// Sets up image rollovers
	setupRollovers();
	// Sets the visitor cookie if it does not exist
	SetClickTracksCookie();
}

if (window.addEventListener) window.addEventListener("load", globalLoad, false);
else if (window.attachEvent) window.attachEvent("onload", globalLoad);
