/* 
* GLOBAL FUNCTIONS are shared by both ADMIN AND FRONT SECTIONS OF THE CMS  
* 
*/
var ajax = (window.XMLHttpRequest || window.ActiveXObject)? true:false;
var loading = '<span class="loading"><img src="http://www.lancsngfl.ac.uk/lgfl_shared/cmsSharedv7/shared/loading-sml-blue.gif" /> Loading....</span>'

$ = function() { //same principle as the prototype function. - great space saver 
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}


cms = Object(); //all new functions can be made part of the cms object

/**
*   Add an event listener to an object
*   @param      object
*   @param      evt  / event
*   @param      func            function
*   @param      capture
*   @return     boolean
*/

cms.addEvent = function(object, evt, func, capture){
	if(typeof func != 'function'){
		return false;
	}
	if(object.addEventListener){
		object.addEventListener(evt, func, capture);
		return true;
	}else if(object.attachEvent){
		object.attachEvent('on'+evt, func);
		return true;
	}
	return false;
}

/**
*   Removes an event listener
*   @param      object
*   @param      evt         event
*   @param      func            function
*   @param      capture
*   @return     boolean
*/

cms.removeEvent = function(object, evt, func, capture){
	if(typeof func != 'function'){
		return false;
	}
	if(object.removeEventListener){
		object.removeEventListener(evt, func, capture);
		return true;
	}else if(object.detachEvent){
		object.detachEvent('on' + evt, func);
		return true;
	}
	return false;
}



function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function checkpass(){
	obj1 = MM_findObj("password");
	obj2 = MM_findObj("repassword");
	if(obj1.value != obj2.value ){
		obj2.value = "";
		obj1.value = "";
		alert("Sorry the password you entered was not the same in both boxes please try again");
		
	}
}

var lgflPopupWindow=null;
function lgflPopup(mypage,myname,w,h,pos,infocus,scroller){
	if(!scroller) scroller = 'yes';
	if(pos=='random'){
	LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
	TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=='center'){
	LeftPosition=(screen.width)?(screen.width-w)/2:100;
	TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!='center' && pos!='random') || pos==null){LeftPosition=100;TopPosition=100;}settings='width='+ w + ',height='+ h + ',top=' + TopPosition + ',left=' + LeftPosition + ',location=no,directories=no,menubar=no,toolbar=no,status=yes,scrollbars='+scroller+',resizable=no,dependent=no';
	lgflPopupWindow=window.open('',myname,settings);
	if(infocus=='front'){lgflPopupWindow.focus();lgflPopupWindow.location=mypage;}	
	return false;
}

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 "";
}

function getElementStyleFull(obj) {
    if (obj.currentStyle) {
        return obj;
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(obj, "");
        return compStyle;
    }
    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';
			}
		}
	}
}

/* *************************************************************************
 * 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];
			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];
			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();
}

var req;
var contentPath;
var conf = new Array();
var targetPath="";
conf['target'] = document.getElementById('content'); //default layer to load content into

/* 
argument 1 - path to the script
argument 2 - the target for the returned html
argument 3 - the method of data transfer GET/POST
*/
function loadXMLDoc(url) {
	req = false;
	xmlDoc = null;
	targetPath = (arguments[1])? arguments[1] : document.getElementById('container')
	if(window.XMLHttpRequest) { // branch for native XMLHttpRequest object
		req = new XMLHttpRequest();
    } else if(window.ActiveXObject) { // branch for IE/Windows ActiveX version
       	try{
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	}catch(e) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
    }
	if(req) {
		req.onreadystatechange = function(){
			xmlDoc = null;
			if (req.readyState == 4){
				// only if "OK"
				if (req.status == 200){
					targetPath.innerHTML = req.responseText;
					return true;
				}else{
					alert("There was a problem retrieving the XML data:\n" + req.statusText);
				}
			}	
		}
		req.open("GET", url, true);
		req.send("");
	}
}
var currentAjaxValue;
function ajaxLoad(url) {
	var req = false;
	var xmlDoc = null;
	if(window.XMLHttpRequest) { // branch for native XMLHttpRequest object
		req = new XMLHttpRequest();
    } else if(window.ActiveXObject) { // branch for IE/Windows ActiveX version
       	try{
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	}catch(e) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
    }
	if(req) {
		req.onreadystatechange = function(){
			xmlDoc = null;
			if (req.readyState == 4){
				// only if "OK"
				if (req.status == 200){
					if(req.responseText=='failed'){ //session timed out so reload parent
						document.location.reload(true);
					}
					return;
					
					/*					
					if(req.responseXML.documentElement) xmlDoc = req.responseXML.documentElement;
					if(xmlDoc!=null){ //XML data
						return req.responseXML; //RETURN
					}else{*/
						//alert(req.responseText);
					//}
				}else{
					alert("There was a problem retrieving the XML data:\n" + req.statusText);
				}
			}	
		}
		req.open("GET", url, true);
		req.send("");
	}
}


function processReqChange(){
    // only if req shows "loaded"
}


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