/*
 *  name: global.js
 *  description: JS function list for EffinghamMagazine.com
 *  author: bstripling
 *  date: 7/29/2008
 *
 * includes these functions:
 * addLoadEvent
 * externalLinks
 * clearInput
 * 
 *------------------------------------------------------------*/

/**********************************************************************************************
  better way to load JS functions than window.onload
**********************************************************************************************/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


/**********************************************************************************************
  unobtrusive external links
**********************************************************************************************/

function externalLinks() {
 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.getAttribute("rel") == "nofollow external"))
     anchor.target = "_blank";
 }
}

addLoadEvent(externalLinks);


/**********************************************************************************************
  clear input fields on focus
**********************************************************************************************/

function clearInput(fieldId) {
	// Pass this function the ID of a form field to have its contents erased on focus.
	if (!document.getElementById || !document.getElementById(fieldId)) {
		return;
	}
	document.getElementById(fieldId).onfocus = function() {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	};
	document.getElementById(fieldId).onblur = function() {
		if (this.value == '') {
			this.value = this.defaultValue;
		}
	};
}

function checkFormField(formId,fieldId) {
	if (!document.getElementById || !document.getElementById(formId) || !document.getElementById(fieldId)) {
		return;
	}
	clearInput(fieldId);
	document.getElementById(formId).checkField = fieldId;
	document.getElementById(formId).onsubmit = function() {
		if (document.getElementById(this.checkField).value == document.getElementById(this.checkField).defaultValue) {
			return false;
		}
	};
}

addLoadEvent(function() {
	checkFormField('search_form','search_input');
	checkFormField('newsletter_form','email_address');
});


/**********************************************************************************************

**********************************************************************************************/

