/* http://brothercake.com/site/resources/scripts/domready/
**************************************************************************/
function domFunction(f, a) {
	var n = 0;
	var t = setInterval(function() {
		var c = true;
		n++;
		if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
			c = false;
			if(typeof a == 'object') {
				for(var i in a) {
					if((a[i] == 'id' && document.getElementById(i) == null) || (a[i] == 'tag' && document.getElementsByTagName(i).length < 1)) { 
						c = true; 
						break; 
					}
				}
			}
			if(!c) { f(); clearInterval(t); }
		}
		if(n >= 60) {
			clearInterval(t);
		}
	}, 250);
};

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")
   {
	 var oLink = anchors[i];
	   if(oLink.className.search(/noicon/) == -1)
	   { // find the non-noicon ones, and set a class of linkpic
	     anchor.setAttribute("class", "link_ext");
	   }
	 anchor.target = "_blank";
   }
  }
};

function disclaimerLinks() 
{
 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") == "disclaimer")
   {
	 anchor.target = "_blank";
	
	anchor.onclick = function() {
	//alert('This is a disclaimer'+" \n"+this.getAttribute("href"));
	openPopUp('/disclaimer.php?link='+ escape(this.getAttribute("href")));
    return false;
	  }
	
	
   }
  }
};

function openPopUp(linkURL) {
	window.open(linkURL,'popup','width=400,height=400')
	/*for testing
	window.open(linkURL,'popup','width=800,height=400,resizable=yes, directories=yes, location=yes, menubar=yes, status=yes, toolbar=yes') */
}


function registerReset() {
  if (!document.getElementById) return;
  if (document.getElementById('reset')){
	  document.getElementById('reset').onclick = function() {
	var frm = document.getElementById("register_member_form").reset();
    return false;
	  }
  }
}

// function to add on- events to forms.
function initFormFocus() {
    document.email.emailAdd.onfocus = function () {if (this.value == 'Enter email address'){this.value = ''}};
    document.email.emailAdd.onblur = function () {if (this.value == ''){this.value = 'Enter email address'}};

	/* doc   formname   inputid */
    document.siteSearch.searchText.onfocus = function () {if (this.value == 'Search'){this.value = ''}};
    document.siteSearch.searchText.onblur = function () {if (this.value == ''){this.value = 'Search'}};

	/* no search results */
	if (document.getElementById('search_nr')) {
	document.siteSearch_nr.searchText_nr.onfocus = function () {if (this.value == 'Search'){this.value = ''}};
    document.siteSearch_nr.searchText_nr.onblur = function () {if (this.value == ''){this.value = 'Search'}};
	}
}

// turns services menu into a jump menu
function initServiceMenu(){
	if (document.serviceMenu){
	var selObj = document.getElementById('serviceSelect');
	document.serviceMenu.serviceSelect.onchange = function () {var selIndex = selObj.selectedIndex;self.location=selObj.options[selIndex].value};
	}
}

/* for email signup form */
function clickOn() {
	if (document.getElementById('currentY')) {
	document.getElementById('currentY').onclick = function() {
	Element.show('followUp');
	  }
	}
}
function clickOff() {
	if (document.getElementById('currentN')) {
	document.getElementById('currentN').onclick = function() {
	Element.hide('followUp');
	  }
	}
}

function printScreen() {
	if (document.getElementById('functionPrint')) {
	document.getElementById('functionPrint').onclick = function() {
	window.print(); return false;
	}
  }
}

/* hides follow-up questions unless 'yes' clicked */
function hideFollowUp(){
	if (!document.getElementById) return;
	if (document.getElementById('followUp')) {
	document.getElementById('followUp').style.display = 'none';
	}
	if (document.getElementById('followUpMsg')) {
	document.getElementById('followUpMsg').style.display = 'none';
	}	
}


/* Load the scripts
************************************************************************* */
var pboys = new domFunction(function() {
	externalLinks();
	disclaimerLinks();
	registerReset();
	// callModal();
	});


// Nifty function to add onload events without overwriting
// ones already there courtesy of the lovely and talented
// Simon Willison http://simon.incutio.com/
function addLoadEvent(func) {   
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
        window.onload = func;
    } else {
        window.onload = function(){
        oldonload();
        func();
        }
    }
}

addLoadEvent(initFormFocus);
addLoadEvent(initServiceMenu);
addLoadEvent(clickOn);
addLoadEvent(clickOff);
addLoadEvent(hideFollowUp);
addLoadEvent(printScreen);

