// JavaScript Document

// Declare variables
var browserName = navigator.appName; 
var browserVer = parseInt(navigator.appVersion);

// Set up object for AJAX requests
var request = setupNewAjaxRequest();

function setupNewAjaxRequest() {
	var request = null;
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}
	return request;
}

// Returns the value of an XML node
function getXMLValue(node) {
	return (node && node.firstChild) ? node.firstChild.nodeValue : ""
}

// Replaces node content with text
function replaceText(elem, newtext) {
	while (elem.firstChild != null) elem.removeChild(elem.firstChild);
	elem.appendChild(document.createTextNode(newtext));
}

// Select an item in a drop-down list based on the	value or text
function selectDropDownItem(val, list, mode) {
	for (var x = 0; x < list.options.length; x++) {
		if (mode == "text" && val == list.options[x].text) { list.selectedIndex = x; return; }
		else if (mode == "value" && val == list.options[x].value) { list.selectedIndex = x; return; }
	}
}

// Displays a full image when a thumbnail is clicked
function showProductImage(obj) {
	// Get the image contained in the link object
	var imgThumb = obj.getElementsByTagName("img")[0];
	var imgFull = document.getElementById("imgMain");
	if (imgThumb && imgFull) {
		var divFull = imgFull.parentNode;
		while (divFull.firstChild) divFull.removeChild(divFull.firstChild);
		var imgFull = document.createElement("img");
		imgFull.src = imgThumb.src.replace("_smthumb.jpg", "_view.jpg");
		imgFull.setAttribute("alt", imgThumb.getAttribute("alt"));
		imgFull.id = "imgMain";
		if (imgFull.complete) divFull.appendChild(imgFull);
		imgFull.onload = function() { divFull.appendChild(this); }
	}
}

// Image preloader script
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];}}
}

// Sets up image rollovers
function setupNav() {
	// 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 thisImage = pageImages[x].src.replace(location.protocol + '//' + location.hostname, '');
		if (thisImage.indexOf('/nav/') != -1) {
			// Preload rollover image
			var normalURL = thisImage;
			var rolloverURL = thisImage.replace('.', '_on.');
			// Setup rollover events
			addRollover(pageImages[x], normalURL, rolloverURL);
		}
	}
	// Get all of the image buttons on this page and iterate through them to determine if any are rollovers
	var inputElements = document.getElementsByTagName("input");
	for (var x = 0; x < inputElements.length; x++) {
		if (inputElements[x].getAttribute("type") == "image") {
			var thisImage = inputElements[x].src.replace(location.protocol + '//' + location.hostname, '');
			if (thisImage.indexOf('/nav/') != -1) {
				// Preload rollover image
				var normalURL = thisImage;
				var rolloverURL = thisImage.replace('.', '_on.');
				// Setup rollover events
				addRollover(inputElements[x], normalURL, rolloverURL);
			}
		}
	}
}

// Sets up image rollovers
function setupButtons() {
	// 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 thisImage = pageImages[x].src.replace(location.protocol + '//' + location.hostname, '');
		if (thisImage.indexOf('/buttons/') != -1) {
			// Preload rollover image
			var normalURL = thisImage;
			var rolloverURL = thisImage.replace('.', '_on.');
			var hitURL = thisImage.replace('.', '_hit.');
			// Setup rollover events
			addRollover(pageImages[x], normalURL, rolloverURL, hitURL);
		}
	}
	// Get all of the image buttons on this page and iterate through them to determine if any are rollovers
	var inputElements = document.getElementsByTagName("input");
	for (var x = 0; x < inputElements.length; x++) {
		if (inputElements[x].getAttribute("type") == "image") {
			var thisImage = inputElements[x].src.replace(location.protocol + '//' + location.hostname, '');
			if (thisImage.indexOf('/buttons/') != -1) {
				// Preload rollover image
				var normalURL = thisImage;
				var rolloverURL = thisImage.replace('.', '_on.');
				var hitURL = thisImage.replace('.', '_hit.');
				// Setup rollover events
				addRollover(inputElements[x], normalURL, rolloverURL, hitURL);
			}
		}
	}
}

// Image button rollover/press handler handler
function addRollover(img, normalURL, rolloverURL) {
	preloadImages(rolloverURL);
	img.onmouseover = function() { img.src = rolloverURL; }
	img.onmouseout = function() { img.src = normalURL; }
	if (arguments.length > 3) {
		var hitURL = arguments[3];
		preloadImages(hitURL);
		img.onmousedown = function() { img.src = hitURL; }
		img.onmouseup = function() { img.src = rolloverURL; }
	}
}

// Pricing OnLoad directive
function globalLoad() {
	// Preload images
	setupNav();
	// Sets up image rollovers
	setupButtons();
	preloadImages("/images/icons/backlink_on.gif");
	preloadImages("/images/icons/toplink_on.gif");
	preloadImages("/images/icons/bulletlink_on.gif");
}

if (window.addEventListener) window.addEventListener("load", globalLoad, false);
else if (window.attachEvent) window.attachEvent("onload", globalLoad);