// Dom functions




// Multiple Onload Functions to be called
function addLoadListener(fn) {
	if (typeof window.addEventListener != 'undefined') {
		window.addEventListener('load', fn, false);
	} else if (typeof document.addEventListener != 'undefined') {
		document.addEventListener('load', fn, false);
	} else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent('onload', fn);
	} else {
		var oldfn = window.onload;

		if (typeof window.onload != 'function') {
			window.onload = fn;
		} else {
			window.onload = function() {
				oldfn();
				fn();
			};
		}
	}
}

/*////////////////////////////////////////////////////////////
Get Elements By Class Script by Distin Diaz
Source: http://www.dustindiaz.com/getelementsbyclass/
////////////////////////////////////////////////////////////*/
function getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	if ( node == null ) { node = document; }
	if ( tag == null ) { tag = '*'; }
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className ) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function RAgetElementsByName(searchName, node, tag) {
	var nameElements = new Object();
	if ( node == null ) { node = document; }
	if ( tag == null ) { tag = '*'; }
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchName+'(\\s|$)');
	
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].name ) ) {
			nameElements[j] = els[i];
			j++;
		}
	}
	return nameElements;
}


function linkNewWindow(strLoc) {
	window.open(strLoc,'_blank');
	return false;
}


function externalLinks() {
	var links = document.getElementsByTagName('a');
	for(var i = 0; i < links.length; i++) {
		if ( links[i].rel == 'external' ) {
  links[i].className = "externalLinkStyle";
    if (links[i].title == "") {
        links[i].title = "opens in a new window";
    } else {
        links[i].title = links[i].title + " (opens in a new window)";
    }
    links[i].onclick = (function (e) {if (!e) {e = window.event;}if (e.shiftKey || e.ctrlKey || e.altKey) {return;}window.open(this.href);return false;});		}
	}
}

function topLinks() {
	var links = document.getElementsByTagName('a');
	for(var i = 0; i < links.length; i++) {
		if ( links[i].rel == 'top' ) {
			links[i].onclick = function(e) { 	pageup(e); return false; }
            links[i].onkeypress = function(e) {
                 if (!(e.shiftKey || e.ctrlKey || e.altKey || (e.keyCode == 9) ) ) {
                    pageup(e);
                 }
                 return false;
            }
			
		}
	}
}

function printLinks() {
	var spans = getElementsByClass('print', null, 'span');
	for(var i = 0; i < spans.length; i++) {
		spans[i].innerHTML = '<img src="/_images/icons/printpage.gif" alt="" /><a href="#" title="Print Page" onclick="window.print(); return false;">Print Page</a>';
	}
}


function pageup(e) {
		UAGENT = navigator.userAgent.toUpperCase();
		if (UAGENT.indexOf("MSIE") >=0) { posi = event.y; }
		else { posi = e.pageY; }
		moveObje(posi);
	}
		function moveObje(position) {
		move = position / 15;
		point = parseInt(position - move);
		scrollTo(0,point);
		if (point > 0) { setTimeout("moveObje(point)",15); }
	}
	
	function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.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 MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function schoolcheck() {
	var atschool = document.getElementById('MemberAtSchool');
	var schoolname = document.getElementById('MemberSchoolName_label');
	
	if (atschool && schoolname) {
		if (atschool.value == 'Yes') {
			schoolname.innerHTML = schoolname.innerHTML.replace('School Name', 'School Name*');
			schoolname.innerHTML = schoolname.innerHTML.replace('**', '*');
		} else {
			schoolname.innerHTML = schoolname.innerHTML.replace('School Name*', 'School Name');
		}
	}
}

function schoolinit() {
	var atschool = document.getElementById('MemberAtSchool');
	
	if (atschool) {
		if (document.all) {
			atschool.addEvent("change", schoolcheck);
			atschool.captureEvents(Event.CHANGE);
		} else {
			atschool.onchange = schoolcheck;
		}
	}
	
	schoolcheck();
}

function entrydatecheck() {
	var ukres = document.getElementById('MemberResidentUK');
	var doe = document.getElementById('MemberDateOfEntry_label');
		
	if (ukres && doe) {
		if (!ukres.checked) {
			doe.innerHTML = doe.innerHTML.replace('If you have not ticked the above, enter your date of entry to the UK or other member state of the EEA', 'If you have not ticked the above, enter your date of entry to the UK or other member state of the EEA*');
			doe.innerHTML = doe.innerHTML.replace('**', '*');
		} else {
			doe.innerHTML = doe.innerHTML.replace('If you have not ticked the above, enter your date of entry to the UK or other member state of the EEA*', 'If you have not ticked the above, enter your date of entry to the UK or other member state of the EEA');
		}
	}
}

function entrydateinit() {
	var ukres = document.getElementById('MemberResidentUK');
	
	if (ukres) {
		if (document.all) {
			ukres.addEvent("change", entrydatecheck);
			ukres.captureEvents(Event.CHANGE);
		} else {
			ukres.onchange = entrydatecheck;
		}
	}
	
	entrydatecheck();
}


function discheck() {
	var dis = document.getElementById('MemberDisability');
	var distype = document.getElementById('MemberDisabilityType_label');
	
	if (dis && distype) {
		if (dis.value == 'Yes') {
			distype.innerHTML = distype.innerHTML.replace("If you selected 'Yes', please select the nature of your disability", "If you selected 'Yes', please select the nature of your disability*");
			distype.innerHTML = distype.innerHTML.replace('**', '*');
		} else {
			distype.innerHTML = distype.innerHTML.replace("If you selected 'Yes', please select the nature of your disability*", "If you selected 'Yes', please select the nature of your disability");
		}
	}
}

function disinit() {
	var dis = document.getElementById('MemberDisability');
	
	if (dis) {
		if (document.all) {
			dis.addEvent("change", discheck);
			dis.captureEvents(Event.CHANGE);
		} else {
			dis.onchange = discheck;
		}
	}
	
	discheck();
}


function difcheck() {
	var dif = document.getElementById('MemberLearningDifficulties');
	var diftype = document.getElementById('MemberLearningDifficultiesType_label');
	
	if (dif && diftype) {
		if (dif.value == 'Yes') {
			diftype.innerHTML = diftype.innerHTML.replace("If you selected 'Yes', please select the nature of your learning difficulty", "If you selected 'Yes', please select the nature of your learning difficulty*");
			diftype.innerHTML = diftype.innerHTML.replace('**', '*');
		} else {
			diftype.innerHTML = diftype.innerHTML.replace("If you selected 'Yes', please select the nature of your learning difficulty*", "If you selected 'Yes', please select the nature of your learning difficulty");
		}
	}
}

function difinit() {
	var dif = document.getElementById('MemberLearningDifficulties');
	
	if (dif) {
		if (document.all) {
			dif.addEvent("change", difcheck);
			dif.captureEvents(Event.CHANGE);
		} else {
			dif.onchange = difcheck;
		}
	}
	
	difcheck();
}



addLoadListener(externalLinks);
addLoadListener(topLinks);

addLoadListener(schoolinit);
addLoadListener(entrydateinit);
addLoadListener(disinit);
addLoadListener(difinit);