// add load listener allows multiple events to be attached to onload
function addLoadListener(fn){
  if (typeof window.addEventListener != 'undefined'){
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined'){
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined'){
    window.attachEvent('onload', fn);
  }
  else{
    var oldfn = window.onload;
    if (typeof window.onload != 'function'){
      window.onload = fn;
    }
    else{
      window.onload = function(){
        oldfn();
        fn();
      };
    }
  }
}

// JSTarget function by Roger Johansson, www.456bereastreet.com, with modifications by Ben Kroll, webkroll.com
var JSTarget = {
	init: function(att,val,warning) {
		if (document.getElementById && document.createElement && document.appendChild) {
			var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'class' : att;
			var strVal = ((typeof val == 'undefined') || (val == null)) ? 'non-html' : val;
			var strWarning = ((typeof warning == 'undefined') || (warning == null)) ? ' (opens in a new window)' : warning;
			var oWarning;
			var arrLinks = document.getElementsByTagName('a');
			var oLink;
			// original regex below
			//var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
			var oRegExp = new RegExp("(^|\\s)(("+ strVal + ")|(" + strVal + "_(\\d){1,}x(\\d){1,}))(\\s|$)");
			
			for (var i = 0; i < arrLinks.length; i++) {
				oLink = arrLinks[i];
				if ((strAtt == 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(strAtt)))) {
					oWarning = document.createElement("em");
					oWarning.appendChild(document.createTextNode(strWarning));
					oLink.appendChild(oWarning);
					oLink.onclick = JSTarget.openWin;
				}
			}
			oWarning = null;
		}
	},
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {

			/* 
			check for different class names to assign popup sizes.
			custom sized popup are also recognised if the class name 
			is constructed like: popup_<width>x<height>
			
			e.g. popup_400x600
			
			- added by krollster@gmail.com; 15/01/2007 
			*/
			var strWSize = '';
			//bloody IE6 always needs an extra treatment
			var className = (this.getAttribute('class')) ? this.getAttribute('class'): this.getAttribute('classname');
			var strDim = className.substr(className.lastIndexOf('_')+1);
			var wWidth = strDim.substr(0,strDim.indexOf('x'));
			var wHeight = strDim.substr(strDim.indexOf('x')+1);
			
			if (wWidth !== '' && wHeight !== ''){			
				strWSize = 'width=' + wWidth + ',height=' + wHeight;
			}
			else {
				switch (className){
					case 'popup_sml':
						strWSize = 'width=200,height=200';
					 break;
					case 'popup_med':
						strWSize = 'width=300,height=300';
					 break;
					case 'popup_lrg':
						strWSize = 'width=400,height=400';
					 break;				 
					default:
						strWSize = 'width=200,height=200';
					 break;
				}
			}
			
			// create a timestamp to attach to the window name so we can create a relativly (to the milisecond) 
			// unique window name to avoid popups from overwriting each other
			var dtNow = new Date();
			
			var oWin = window.open(this.getAttribute('href'), 'popup' + dtNow.getTime(), strWSize + ",scrollbars=yes,resizable=yes");
			if (oWin) {
				if (oWin.focus) oWin.focus();
				return false;
			}
			oWin = null;
			return true;
		}
	},
	/*
	addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	*/
	addEvent: function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn]( window.event );}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};
JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","popup_sml","");});
JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","popup_med","");});
JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","popup_lrg","");});
JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","popup","");});

// function to hide compact login box labels when the field is selected
function initOverLabels () {
  if (!document.getElementById) return;      
  var labels, id, field;
  // Set focus and blur handlers to hide and show 
  // labels with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    if (labels[i].className == 'overlabel') {
      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      } 
      // Change the applied class to hover the label 
      // over the form field.
      labels[i].className = 'overlabel-apply';
      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }
      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };
      // Handle clicks to label elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };
    }
  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.left = (hide) ? '-10000em' : '0';
      //labels[i].style.color = (hide) ? '#FFFFFF' : '#FFFFFF';
			return true;
    }
  }
}

JSTarget.addEvent(window, 'load', function(){setTimeout(initOverLabels, 50);});