// --- Harvey Mackay --- www.harveymackay.com --
// --- Common JS functions --------------------
// --- Created by ideapark -- May 2007! ------
// Functions in this file are meant to run (or at least be accessible) on every page of the site


// addLoadEvent() is a method of running multiple functions at window.onload
// To avoid onload conflicts, run all such functions using this method

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// Call site-wide window.onload functions here

addLoadEvent(startList);
addLoadEvent(shareColumn);




function startList() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("topNav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(/ ?over/, "");
				}
			}
		}
	}
}



// if there is a "Share This Column With A Friend" form, shareColumn adds two pieces of functionality:
// 1. It replaces the email input with the label text, then removes the text when the user clicks in the input
// 2. It validates the form by checking whether the email input has been changed before submitting

function shareColumn() {
	var errorClass = 'forward_column_error';
	var errorClassRE = new RegExp('(^|\b) *' + errorClass + '(\b|$)','gi');
	var forms = document.getElementsByTagName('form');
	for (var i=0,j=forms.length; i<j; i++) {
		var form = forms[i];
		if (form.className.indexOf('forward_column') != -1) {
			form.eaInput = document.getElementById('ea');
			form.eaLabel = document.getElementById('eaLabel');
			// 1. hide label, put label's text in input
			form.eaLabel.style.display = 'none';
			form.eaInput.value = form.eaLabel.innerHTML;
			form.eaInput.defaultValue = form.eaLabel.innerHTML;
			form.eaInput.onfocus = function() {
				if (this.value == this.defaultValue) {
					this.value = '';
				}
				this.className = this.className.replace(errorClassRE,'');
			}
			form.eaInput.onblur = function() {
				if (this.value == '') {
					this.value = this.defaultValue;
				}
			}
			// 2. validate form on submit
			form.onsubmit = function() {
				if (this.eaInput.value == this.eaInput.defaultValue) {
					this.eaInput.className = this.eaInput.className.replace(errorClassRE,'');
					this.eaInput.className += ' ' + errorClass;
					return false;
				}
			}
		}
	}
}



/* randomProduct modifies the href of an anchor and the src of its first nested image with a random item from a speficied list */

function randomProduct(source,targetAnchor,hrefPath,imgPath) {
	
	if (source.indexOf('URL:') != 0) {
		var contents = source;
	}
	
	var targetAnchor = document.getElementById(targetAnchor);
	
	var products = new Array();
	products = contents.split('\n');
	
	var rnumb = Math.floor(Math.random()*products.length);
	
	targetAnchor.href = hrefPath + products[rnumb].replace(/(.+?),.+/,'$1');
	targetAnchor.getElementsByTagName('img')[0].src = imgPath + products[rnumb].replace(/.+?,(.+)/,'$1');
	
}



