//General library of support functions for StaffCV Jobseeker website
function $(id) {return getSingleElement(id);}
function getSingleElement( id ) {
	if( document.getElementById )
		return document.getElementById(id);
	else
		return document.all(id);
}

function getElements( name ) {
	if( document.getElementsByName )
		return document.getElementsByName(name);
	else
		return document.all(name);
}

function getAncestor( element, tag )
{
	if( element.tagName == tag ) return element;
	if( element.parentNode ) return getAncestor( element.parentNode, tag );
}

function radioSelectedValue(radioButtons) {
	for(var i=0; i < radioButtons.length; i++)
		if (radioButtons[i].checked == true)
				return radioButtons[i].value;
}

function radioSelectValue(radioButtons, valueToSelect) {
	for(var i=0; i < radioButtons.length; i++)
		if (radioButtons[i].value == valueToSelect)
			radioButtons[i].checked=true;
}

function indexOf(a, o) {
	for(i=0;i<a.length;i++) {
		if( a[i]==o ) return i;
	}
	return -1;
}

function checkInvalidChars(e) {
	//just add any character to this string
	var iChars = "<";
	for (var i = 0; i < e.value.length; i++) {
		if (iChars.indexOf(e.value.charAt(i)) != -1) {
			e.value = removeCharacters(e);
			alert ("The character '<' was detected in your answer.\n This is an invalid character and has been removed.");
			return false;
		}
	}
}

function removeCharacters( obj ) {
	//regexp of characters to remove
	var regChars = /</gi;
	return obj.value.replace(regChars, "");
}


function maskKeyPress(objEvent) {
	//variables
	var iKeyCode, strKey;  
	//browser detection
	var strUserAgent = navigator.userAgent.toLowerCase(); 
	var isIE = strUserAgent.indexOf("msie") > -1; 
	var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 
	var isNS6 = strUserAgent.indexOf("netscape6") > -1; 

	//regular expressions
	var reInValidChars = /</;
	var reKeyboardChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;
	
	if (isIE) {
		iKeyCode = objEvent.keyCode;
	} else {
		iKeyCode = objEvent.which;
	}
	
	strKey = String.fromCharCode(iKeyCode);

	if (reInValidChars.test(strKey) && !reKeyboardChars.test(strKey) && !checkClipboardCode(objEvent, strKey)) {
		//alert("Invalid Character Detected!\nKeyCode = " + iKeyCode + "\nCharacter =" + strKey);
		return false;
	}
}

function checkClipboardCode(objEvent, strKey) {
	var reClipboardChars = /[cvxz]/i;
	var strUserAgent = navigator.userAgent.toLowerCase(); 
	var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
	if (isNS6)
		return objEvent.ctrlKey && reClipboardChars.test(strKey);
	else
		return false;
}

function getElement(id) {
	return document.getElementById(id);
}

function show(id) {
	getElement(id).style.display='';
}

function hide(id) {
	getElement(id).style.display='none';
}

	// get the true offset of anything on NS4, IE4/5 & NS6, even if it's in a table!
	function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }
	function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt,"Top"); }
	
	function getAbsPos(elt,which) {
		iPos = 0;
		while (elt != null) {
			iPos += elt["offset" + which];
			elt = elt.offsetParent;
			//1-Nov-2006 Andrew Shearer (Gemini 2567)
			//re-introduced the following condition removed under Gemini 1966.
			//Changed to use 'html' so that pages without frames operate correctly.
			if (elt && elt.tagName.toLowerCase()!='html') {
				iPos -= elt["scroll" + which];
			}

		}
		return iPos;
	}

	//absolute position of the mouse on the document
	function getMouseX(e) {
		var posX;                      
		if (!e) var e = window.event;
		if (e.pageX) 
			posX = e.pageX;
		else if (e.clientX )
			posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		return posX;
	}

	function getMouseY(e) {
		var posY;
		if (!e) var e = window.event;
		if (e.pageY)
			posY = e.pageY;
		else if (e.clientY)
			posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		return posY;
	}
	
	function windowWidth() {
		if (window.innerWidth!=null) 
			return window.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth)
			return document.documentElement.clientWidth;
		else if (document.body!=null)
			return document.body.clientWidth;
		else
			return 0;
	}
	function windowHeight() {
		if (window.innerHeight!=null)
			return window.innerHeight
		else if (document.documentElement && document.documentElement.clientHeight)
			return document.documentElement.clientHeight
		else if (document.body!=null)
			return document.body.clientHeight
		else
			return null;
	}
	function doServerFunction(Value)  {	
	//window.status+=Value;
	// 22 = CancelWithConfirm
	if( Value != 22 || confirm("Are you sure you want to cancel this process and discard your changes?"))
		//buttonBar.disabled = true;
		document.mainForm.fn.value=Value;
		document.mainForm.submit();
    }