var searchFieldIsActive = false;

if (!document.getElementById){
	document.getElementById = function(){return null;}
	document.getElementsByTagName = function(){return null;}
}

// Load functions
onLoadList = new Array();

// Example: addOnLoad('someFunction');
function addOnLoad(func){
	onLoadList[onLoadList.length] = func;
}

function runOnLoad(){
	for(i in onLoadList){
		var addPar = "";
		// alert(onLoadList[i])
		if (onLoadList[i].indexOf("(") == -1) addPar = "()";
		eval(onLoadList[i]+addPar);
	}
}

// Init
addOnLoad('mouseoverlinks');
addOnLoad('parseLinks');
addOnLoad('SetSearchFieldIsActive');
if(document.all)document.onkeydown = checkDown;
if(location.href.indexOf('WBCMODE=AuthoringReedit') > -1 || location.href.indexOf('WBCMODE=AuthoringNew') > -1){addOnLoad('initToggle');}
window.onload = runOnLoad;

//Mouseover event
function mouseoverlinks() {
  if (!document.getElementById) return
  var imgOriginSrc;
  var imgTemp = new Array();
  var imgarr = document.getElementsByTagName('img');
  for (var i = 0; i < imgarr.length; i++) {
    if (imgarr[i].getAttribute('hsrc')) {
        imgTemp[i] = new Image();
        imgTemp[i].src = imgarr[i].getAttribute('hsrc');
        imgarr[i].onmouseover = function() {
            imgOriginSrc = this.getAttribute('src');
            this.setAttribute('src',this.getAttribute('hsrc'))
        }
        imgarr[i].onmouseout = function() {
            this.setAttribute('src',imgOriginSrc)
        }
    }
  }
}

function parseLinks() {
	// make sure the browser supports the object
	if (document.getElementsByTagName) {
		// get the links
		var a = document.getElementsByTagName('a');
		
		// loop through each link (negatively for increased performance)
		for (var i=a.length;i!=0;i--) {
		
			// make alias variable to ease fingers
			var c = a[i-1];
			
			// make sure URL has a href
			if (!c.href) continue;
			
			// convert www links to externals, docs, pdfs
								
			if((c.target.indexOf('_blank') != -1 || c.href.indexOf('.txt') != -1) && !isImgLink(c))c.className='external';
			if(c.href.indexOf('.doc') != -1 && !isImgLink(c))c.className='externalDoc';
			if(c.href.indexOf('.pdf') != -1 && !isImgLink(c))c.className='externalPdf';
			if(c.href.indexOf('.swf') != -1 && !isImgLink(c))c.className='externalSwf';			
			if((c.href.indexOf('.wmv') != -1 || c.href.indexOf('.mpg') != -1 || c.href.indexOf('.mov') != -1) && !isImgLink(c))c.className='externalMov';
			if(c.href.indexOf('.zip') != -1 && !isImgLink(c))c.className='externalZip';
			
			
			if (c.className.indexOf('external') == -1) continue;
			
			// the handler that handles clicking
			c.onclick = function() {
			
				// open the window
				// var win = window.open(this.href,'','width=530,height=450,scrollbars=1,toolbar=1,status=1,resizable=1');
				var win = window.open(this.href);
				// if the window doesn't open, continue normally
				if (win) return false;
			}
		}
	}
}


//But not if imgLink
function isImgLink(obj){
	var imgLink = false;
	if(obj.childNodes.length > 0){
		for(i=0;i < obj.childNodes.length;i++){
			if(obj.childNodes[i].nodeName=='IMG'){
				imgLink=true;
				break;
			}
		}	
	}
	return imgLink;
}


//checks input field for valid e-mail, new toplvl's allowed (ie. info). alert message set by <input type="hidden" name="strTxt"> for language versioning
function validateEmail(strValue,strTxt) {
	var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;
  	if(objRegExp.test(strValue)){return true;}
  	else{alert(strTxt);return false;}
}

//Dummy print function
function fncPF(){
	window.print();
}

// Gosearch on Enter

function SetSearchFieldIsActive(){
	document.getElementById('TextBox_search').onfocus = function(){
		searchFieldIsActive = true;
	}
	document.getElementById('TextBox_search').onblur = function(){
		searchFieldIsActive = false;
	}
}

function goSearch(){
	searchStr = document.getElementById('TextBox_search').value;
	if(searchStr != ''){
		searchUrl = document.getElementById('searchUrl').value;
		location.href = searchUrl + 'search?QUERY=' + searchStr;
	}
}

function checkDown(e) {
	if(searchFieldIsActive){
		var ieKey=event.keyCode; 
		if (ieKey == 13){
			goSearch();
			return false;
		}
	}
}

// Creuna.Cms functions

function initToggle(){
	elems = getCheckBoxesByParentNodeClassName('mcmsLibToggle');
	for(i=0;i<elems.length;i++){
		
		//Show already active on onLoad
		if(elems[i].checked){
			elems[i].parentNode.parentNode.className = 'on';
		}
		
		//Attatch functions
		elems[i].onmousedown = setToggle;
		elems[i].onmouseout = maxCompat;
	}
}

function setToggle(){
	this.parentNode.parentNode.className = (this.checked)?'':'on';
}

function maxCompat(){
	this.checked = (this.parentNode.parentNode.className == 'on')?true:false;
}

function getCheckBoxesByParentNodeClassName(clName){
	checkBoxCollection = new Array();
	inputs = document.getElementsByTagName('input');
	for(i=0;i<inputs.length;i++){
		if(inputs[i].type == 'checkbox' && inputs[i].parentNode.className == clName){
			checkBoxCollection.push(inputs[i]);
		}
	}
	return checkBoxCollection;
}
