var test_div = document.createElement('div');
test_div.setAttribute('className', 'ignore_test');
var classText = (test_div.className) ? 'className' : 'class';
test_div.style.cssText = 'display: none;';
var cssMethod = (test_div.style.cssText) ? "ie" : "non-ie";
var eventMethod = (test_div.attachEvent) ? "ie" : "non-ie";

function doNothing() {
	var totallyObscureName = "pointless";
}

function preloadImage() {
	var d = document;
	if(d.images) {
		if(!d.MM_p)
			d.MM_p = new Array();
		var i,j = d.MM_p.length, a = preloadImage.arguments;
		for(i=0; i<a.length; i++)
			if(a[i].indexOf("#") != 0) {
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
	}
}

function GetXmlHttpObject() {
	var xmlHttp = null;
	try {
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};

function createAjax() {
	var ajax = false;
	if(window.XMLHttpRequest) {
		ajax = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return ajax;
}

function isAjaxReady(ajax) {
	if (ajax.readyState == 4) {
		return true;
	} else {
		return false;
	}
}

function textBoxFocusEnd(textBox) {
	var charMove = 99999999999;
	if(textBox.setSelectionRange) {
		textBox.focus();
		textBox.setSelectionRange(charMove, charMove);
	} else if(textBox.createTextRange) {
		range = textBox.createTextRange();
		range.collapse(true);
		range.moveEnd('character', charMove);
		range.moveStart('character', charMove);
		range.select();
	}
}

function safariResizeFix(obj) {
	var test_div_resize = document.createElement('div');
	test_div_resize.style.resize = "none";
	if(test_div_resize.style.resize) {
		document.getElementById(obj).style.resize = "none";
	}
}

function currentYPosition() {
	if (self.pageYOffset)
		 return self.pageYOffset;
	if (document.documentElement && document.documentElement.scrollTop)
		return document.documentElement.scrollTop;
	if (document.body.scrollTop)
		 return document.body.scrollTop;
	return 0;
}

function elmYPosition(eID) {
	var elm  = document.getElementById(eID);
	var y    = elm.offsetTop;
	var node = elm;
	while (node.offsetParent && node.offsetParent != document.body) {
		node = node.offsetParent;
		y   += node.offsetTop;
	} return y;
}

function scrollToId(eID) {
	Effect.ScrollTo(eID, {duration: 0.5});
}

function URLEncode(textToEncode) {
	var SAFECHARS = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";
	var HEX = "0123456789ABCDEF";

	var plaintext = textToEncode.replace(/\+/gi, "%2B");
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}
	return encoded;
};

function URLDecode(textToDecode) {
	var HEXCHARS = "0123456789ABCDEFabcdef"; 
	var encoded = textToDecode;
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if(ch == "+") {
			plaintext += " ";
			i++;
		} else if(ch == "%") {
			if (i < (encoded.length-2) 
			&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
			&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				i++;
			}
		} else {
			plaintext += ch;
			i++;
		}
	}
	return plaintext;
}

function disableEnterKey(e) {
	var key;
	key = (window.event) ? window.event.keyCode : e.which;
	if(key == 13) {
		return false;
	} else {
		return true;
	}
}

function ge(id) {
	if(typeof(id) == 'undefined') {
		Util.error('Tried to get an undefined element!');
		return null;
	}
	var obj;
	if(typeof(id) == 'string') {
		obj = document.getElementById(id);
		if(!obj) {
			return null;
		} else if(typeof(obj.id) == 'string' && obj.id == id) {
			return obj;
		} else {
			var candidates = document.getElementsByName(id);
			if(!candidates || !candidates.length) {
				return null;
			}
			var maybe = [];
			for(var ii = 0; ii < candidates.length; ii++) {
				var c = candidates[ii];
				if(!c.id&&id) {
					continue;
				}
				if(typeof(c.id) == 'string' && c.id != id) {
					continue;
				}
				maybe.push(candidates[ii]);
			}
			if(maybe.length != 1) {
				Util.error('ge() failed in a bizarre complicated edge case. Check comments.');
				return null;
			}
			return maybe[0];
		}
	} else {
		return id;
	}
	return null;
}

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 {
		alert("Handler could not be attached");
	}
}

function actionBox(id, action, text, style) {
	var aBox = document.getElementById(id);
	var aBoxLabel = document.getElementById(id + '_label');
	var def_txt = text + "      ";
	var curTxt = aBox.value;
	switch(action) {
		case "setup":
			aBoxLabel.style.display = "none";
			aBox.setAttribute(classText, style + '_u');
			aBox.value = def_txt;
		break
		case "focus":
			if(aBox.value == def_txt) {
				aBox.setAttribute(classText, style + '_f');
				aBox.value = "";
			}
		break
		case "blur":
			if(aBox.value == "") {
				aBox.setAttribute(classText, style + '_u');
				aBox.value = def_txt;
			}
		break
	}
}

function xbarLangChoice(type) {
	var langHolder = document.getElementById('xbar_lang_holder');
	var langBox = document.getElementById('xbar_lang_pop');
	var langStatus = document.getElementById('xbar_lang_status');
	var popSpeed = 0.2;
	switch(type) {
		case 'setup':
			langHolder.setAttribute(classText, 'xbar_lang_current_j');
			langBox.style.display = 'none';
		break
		case 'switch':
			if(langBox.style.display == 'none') {
				var stlogBox = document.getElementById('xbar_stlog_pop');
				if(stlogBox != null && stlogBox.style.display == '') {
					new Effect.BlindUp(stlogBox, {duration: popSpeed});
				}
				new Effect.BlindDown(langBox, {queue: {position: 'end', scope: 'lang_switch', limit: 1}, duration: popSpeed});
			} else {
				new Effect.BlindUp(langBox, {queue: {position: 'end', scope: 'lang_switch', limit: 1}, duration: popSpeed});
			}
		break
		case 'go_en':
			langStatus.innerHTML = '<span class="xbar_lang_loading">Loading...</span>';
			new Effect.BlindUp(langBox, {duration: popSpeed});
		break
		case 'go_cy':
			langStatus.innerHTML = '<span class="xbar_lang_loading">Yn llwytho...</span>';
			new Effect.BlindUp(langBox, {duration: popSpeed});
		break
	}
}

function xbarLang(type, key) {
	if(key == "key") {
		if(event.keyCode == 13) {
			xbarLangChoice(type);
		}
	} else {
		xbarLangChoice(type);
	}
}

function closeTmpCy() {
	var ajax = new GLM.AJAX();
	ajax.callPage("../welsh_msg?noredir=1", doNothing, null);
	document.getElementById('welc_msg_cy').style.display = "none";
}
