// JavaScript Document
function cms_addOnloadEvents(func) {
  if (!document.getElementById | !document.getElementsByTagName) return
	var oldonload=window.onload
	if (typeof window.onload != 'function') { window.onload=func }
	else {
		window.onload=function() { oldonload(); func() }
	}
}

function addEvent(obj, evType, fn, useCapture){ 
	 if (obj.addEventListener) { 
	   obj.addEventListener(evType, fn, useCapture);  
	   return true;  
	 } else if (obj.attachEvent) { 
	   var r = obj.attachEvent('on' + evType, fn);  
	   return r;  
	 } else { 
	   obj['on' + evType] = fn; 
	 } 
}

/* 
* function below retrieves the actual style applied to an element
* rework from http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html
*/
function getElementStyle(obj, IEStyleProp, CSSStyleProp) {
    if (obj.currentStyle) {
        return obj.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(obj, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

/* *************************************************************************
 * Set external links in open in new window (standards compliant)
 * Makes use of "rel" attribute.
 * e.g. <a href="" title="" rel="external"></a>
 */
function setExtLinkTarget() {
	if (document.getElementsByTagName) {
		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') {
				anchor.target = '_blank';
				
				if(anchor.childNodes[0].tagName){
					if(anchor.childNodes[0].tagName.toLowerCase() !='img') anchor.className = 'link-external';
				}else{
					 anchor.className = 'link-external';	
				}
				anchor.title = (anchor.title.indexOf('(New window)') > -1) ? anchor.title : anchor.title + ' (Opens in a new window)';
			}
		}
	}
}

/* *************************************************************************
 * Change the bg colour of all input fields when onfocus onblur (standards compliant)
 */

function setInputEvents() {
	if (document.getElementsByTagName) {
		var anchors = document.getElementsByTagName('input');
		for (var i = 0; i < anchors.length; i++) {  
			var inputAnchor = anchors[i];
			inputAnchor.onfocus=function(){ this.select(); }
			addEvent(inputAnchor,'focus',changeFormFocus,false);
			addEvent(inputAnchor,'blur',changeFormBlur,false);
		}
	}
}

function setTextareaEvents() {
	if (document.getElementsByTagName) {
		var anchors = document.getElementsByTagName('textarea');
		for (var i = 0; i < anchors.length; i++) {
			var inputAnchor = anchors[i];
			inputAnchor.onfocus=function(){ this.select(); }
			addEvent(inputAnchor,'focus',changeFormFocus,false);
			addEvent(inputAnchor,'blur',changeFormBlur,false);
		}
	}
}

function changeFormFocus(e){
	var obj = e.target || window.event.srcElement;
	/* could not just get the style  e.g.' prevColBG=obj.style.backgroundColor' as that only gets inline style value */
	prevColBG = getElementStyle(obj, 'backgroundColor', 'background-color');
	prevCol = getElementStyle(obj, 'color', 'color');
	obj.style.backgroundColor="#FFFF80";
	obj.style.color="#000000";
}
function changeFormBlur(e){
	var obj = e.target || window.event.srcElement;
	if(prevColBG) obj.style.backgroundColor=prevColBG;
	if(prevCol) obj.style.color=prevCol;
}

/*
* onload events for forms
*/
function formEvents(){
	setInputEvents();
	setTextareaEvents();
}

function setActiveStyleSheet(title) {
	if (document.getElementsByTagName) {
		var i, a, main;
		for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
			if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
				a.disabled = true;
				if(a.getAttribute('title') == title) {
					a.disabled = false;
				}
			}
		}
	}
}

/*
* onLoad events
*/
cms_addOnloadEvents(setExtLinkTarget);
cms_addOnloadEvents(formEvents);