/* Function to addEventListener to onload
 * @param func - a function which should be executed once the page has loaded
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * it will work even if something has previously been assigned to window.onload
 * without using addLoadEvent itself. 
 */
// The button to "click" when enter is pressed
var enterKeyFunction = null;

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

/*
 * Function to clear a text field
 * @param thefield
 *     the field to clear
 */

function cleartext(thefield) {
	if (thefield.defaultValue == thefield.value) {
		thefield.value = "";
	}
}

/* 
 * Function to set onmouseover to our buttons and preload images
 */
function GetButtons() {
	
	var preloads = new Array();
	var buttons = document.getElementsByTagName('input'); // get all input elements
	
	for(var i = 0; i < buttons.length; i++) {
		if (buttons[i].type == "image") { // check if the input type is 'image'
			if (buttons[i].className.indexOf('hover') != -1) { // check if button contains the 'hover' class
				
				// Preload images into preloads array
				preloads[i] = new Image();
				preloads[i].src = buttons[i].src.replace('.gif', '_on.gif');
				
				buttons[i].onmouseover = function() { // Set the onmouseover for the buttons equal to btn_name_on.gif
			
				if (this.src.length < 8) {
				return false;
				}
					
				if (this.src.substring(this.src.length - 7,this.src.length) != "_on.gif") {
					this.src = this.src.replace('.gif', '_on.gif');
				}
				
				}
				
				buttons[i].onmouseout = function() { // Set the onmouseout for the buttons equal to btn_name.gif
					
					
				  if (this.src.length < 8) {
				  return false;
				  }
					
					if (this.src.substring(this.src.length - 7,this.src.length) == "_on.gif") {
					this.src = this.src.replace('_on.gif', '.gif');
					}
					
				}
			}
		}
	}
}

function DisableButton(btn, disable) {
  if (btn.disabled != disable) {
    var replace;
    if (disable) {
      replace = "_ds.gif";
    } else {
      replace = ".gif";
    }
    btn.style.cursor = disable ? "default" : "pointer";
    btn.disabled = disable;
            
    switch ( btn.src.substring(btn.src.length - 7,btn.src.length) ) {
      case "_on.gif":
        btn.src = btn.src.replace('_on.gif', replace);
        break;
      case "_ds.gif":
        btn.src = btn.src.replace('_ds.gif', replace);
        break;
      default:
        btn.src = btn.src.replace('.gif', replace);
        break;
    }
  }
}



function printPage() {
window.print();
}

function openNewWindow(e) {
  var features = '';
  var start;
  var end;
  var width;
  var height;
  var thisurl = this.href;
  width = parseInt(getDimensionFrom(this, 'w'))>0 ? getDimensionFrom(this, 'w') : '';
  height = parseInt(getDimensionFrom(this, 'h'))>0 ? getDimensionFrom(this, 'h') : '';
  if (height.length>0 || width.length>0) {
    features += height.length>0 ? 'height='+height+',' : '';
    features += width.length>0 ? 'width='+width+',' : '';
    features += (thisurl.indexOf("help.aspx") > 0) ? 'scrollbars=yes,' : '';
  }
  features += getFeatures(this);
   
  if (!e) var e = window.event;
  if (features.length>0) {
    if (features.substr(features.length-1,1) == ",")
      features = features.substr(0, features.length-1);
    window.open(this.href, '_new', features);
  } else {
    window.open(this.href);
  }
  return false;
}

function getDimensionFrom(obj, attrib) {
  if (obj.className.indexOf(" "+attrib) != -1) {
    start = obj.className.indexOf(" "+attrib) + 1
    end = obj.className.indexOf(" ", start);
    end = (end == -1) ? obj.className.length - start : end - start;
    return obj.className.substr(start+1, end-1);
  } else {
    return "";
  }
}

function getFeatures(obj) {
  var features = "";
  if (obj.className.indexOf(" scroll") != -1) {
    features += "scrollbars=yes,";
  }
  return features;
}

addLoadEvent(GetAnchors);
addLoadEvent(GetButtons);
