String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function toggleDisplay(id) {
	if (document.getElementById(id).style.display == '') {
		document.getElementById(id).style.display = 'none';
	} else {
		document.getElementById(id).style.display = '';
	}
}

function replaceHtml(el, html) {
	var oldEl = typeof el === "string" ? document.getElementById(el) : el;
	/*@cc_on // Pure innerHTML is slightly faster in IE
		oldEl.innerHTML = html;
		return oldEl;
	@*/
	var newEl = oldEl.cloneNode(false);
	newEl.innerHTML = html;
	oldEl.parentNode.replaceChild(newEl, oldEl);
	/* Since we just removed the old element from the DOM, return a reference
	to the new element, which can be used to restore variable references. */
	return newEl;
};

function showDiv(divList){
	var i = divList.split(",");
	for (a=0;a<i.length;a++) {
		if(document.getElementById(i[a]) != null){
			if (document.getElementById(i[a]).style.display == 'none'){
				document.getElementById(i[a]).style.display = '';
			} 
		}
	}
}

function hideDiv(divList){
	var i = divList.split(",");
	for (a=0;a<i.length;a++) {
		if(document.getElementById(i[a]) != null){
			if (document.getElementById(i[a]).style.display != 'none'){
				document.getElementById(i[a]).style.display = 'none';
			}	
		}
	}
}


function numbersonly(myfield, e, dec) {
	var key;
	var keychar;
	if (window.event) key = window.event.keyCode;
	else if (e) key = e.which;
	else return true;
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )  return true;
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1)) return true;
	// decimal point jump
	else if (dec && (keychar == ".")) {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else return false;
}


function changeclass(idref, classname) {   
	var el = document.getElementById(idref); 
	var attributeNode = el.getAttributeNode("class"); 
	if( attributeNode ) { attributeNode.value = classname; } 
	else { el.setAttribute("class", classname); } 
}

/*  http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html */
function jscss(a,o,c1,c2){
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className);
    break;
  }
}


/* Loop through an entire form and require all text/password fields w/safe values */
function validateClean(theForm) {
	var re  = new RegExp("^[-_.0-9A-Za-z, ]*$","i");
	var reEmail = new RegExp("^[0-9A-Z]+([-_.0-9A-Z])*@[0-9A-Z]+([-.][0-9A-Z]+)*[.][A-Z]{2,4}$","i");
	var isOptional = 0;
	var errorMsg = '';
	if (document.getElementById){
		for (i=0;i<theForm.length;i++){
			var tempobj=theForm.elements[i];
			if (tempobj.type) {
				if((tempobj.type.toLowerCase()=="text") || (tempobj.type.toLowerCase()=="password")) {
					if (tempobj.className.toLowerCase().indexOf('ptional') > 0) {
						/* nothing, class='optional' */
					}else if (tempobj.value.length < 1) {
						errorMsg = 'Please complete all fields.';
					} else if ((tempobj.name.toLowerCase() == 'email') && (!reEmail.test(tempobj.value))){
						errorMsg = 'Please enter a valid email address.'; 
					} else if ((tempobj.name.toLowerCase() != 'email')  && (!re.test(tempobj.value))){
						errorMsg = 'Only alphanumeric characters or underscores allowed!';
					}
					if (errorMsg.length > 0) {
						alert(errorMsg); tempobj.focus(); return false;
				   }
				}
			}
		}
	}
	return true;
}