//======================== Generic load event function. Allows numerous function to be loaded on the same page without breaking anything
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


//======================== W3C compliant Popup script
function popupLauncher()
{
  if( document && document.body && document.getElementById && document.getElementsByTagName ) //check to see if browser understands the methods
   {
     var alinks = document.getElementsByTagName('A'); // get all the links

     for( i=0; i < alinks.length; i++ ) // store then all one by one
     {
      if( alinks[i].rel == "external" ) // check each one to see if it has the rel attribute set to external
       {
         try
         {
          alinks[i].target = "_blank";  // if it does open new window
         }
         catch(e)
          {}
       }
     }
   }
}

//======================== search form (used to clear and reset all input fields)
function resetFields(whichform) {
	return; // disabled, it's too irritating!
  for (var i=0; i<whichform.elements.length; i++) {// get all the element tags within a form
    var element = whichform.elements[i];
    if (element.type == "submit") continue;       // check if element is a submit button
    if (!element.defaultValue) continue;			// check if element doesn't have some default text and stop if it doesn't
    element.onfocus = function() {					// function for if element is in focus
    if (this.value == this.defaultValue) {		// check if the elements text is the default when page was loaded
      this.value = "";								// if it is delete it
     }
    }
    element.onblur = function() {					// function for if element this element no-longer in focus
      if (this.value == "") {						// check if the elements text is empty
        this.value = this.defaultValue;			// if the elements text is empty put the default value back
      }
    }
  }
}
//======================== Function used by the reset form function. Allows the reset form function to be used for all input fields, not matter how many there are
function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {   // get all the forms
    var thisform = document.forms[i];
    resetFields(thisform);							// apply reset form function to all the forms
  }
}

//======================== Function to check is javascript is activated. If it is the class js-active is assigned to html tag
	var newclassname = 'js-active'; 				// define class
	var ob = document.getElementsByTagName('HTML');// get the html tag
	if (ob.length > 0){
	  ob = ob[0];									// check if there is already a class applied to html tag
	  if (ob.className == ''){
	      ob.className = newclassname;				// if there isn't then add class
	  } else {
	     ob.className += ' ' + newclassname;		// if there is a class already present add class js-active after it
	  }
	}


//======================== IE6 antiflicker (stops the ie flicker on background images
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

addLoadEvent(prepareForms);
addLoadEvent(popupLauncher);

