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

/*
	Taken from the macromedia version
*/
function showHideLayers() { //v6.0
	var i,p,v,obj,args=showHideLayers.arguments;
  	for (i=0; i<(args.length-2); i+=3) {
  		if ((obj=MM_findObj(args[i]))!=null) {
	  		v=args[i+2];
	  		var class_name;
	    	if (obj.style) {
	    		obj=obj.style;
	    		if (v == 'show') {
					v = 'visible';
					d = 'inline';
	    		} else {
					v = 'hidden';
					d = 'none';
	    		}
	    	}
	    	obj.display = d;
	    	obj.visibility = v;
	    }
	}
}

function changeClass(n,newClassName) {
	var obj;
	if ((obj=MM_findObj(n))!=null) {
		obj.className = newClassName;
	}
}

function validEmail(email) {
	if (email == ''
		|| email.indexOf('@') == -1
		|| email.indexOf( '.' ) == -1
		) {
		return false;
	}
	return true;
}

/*
	Required becuase the sort by's fall outside of the searchform so there contents need to be "pasted" back in.
	@params extraOptionsArray is an array of name value pairs for passing extra options to be included in the searchform post.
	This is used by pr_search_main.thtml so far - GPB 20061213.
*/
function jobSearchSortBy(input, searchform, extraOptionsArray) {
	if ( searchform.keywords.value.match(/^e.g. /)) {
		searchform.keywords.value='';
	}
	if (searchform.so != null) {
		searchform.so.value = input.value;
	}

	appendFormInputs(searchform,extraOptionsArray);	
	searchform.submit();
}

function jobSearchListType(input, searchform, extraOptionsArray) {
	if (searchform.ls != null) {
		searchform.ls.value = input.value;
	}
	appendFormInputs(searchform,extraOptionsArray);	
	searchform.submit();
}

function appendFormInputs(doc, extraOptionsArray) {

	if (extraOptionsArray != null) {
		for (var i = 0; i < extraOptionsArray.length;i+=2) {
			var input = document.createElement("input");
			input.type = "hidden";
			input.name = extraOptionsArray[i];
			input.value = extraOptionsArray[i+1];
			doc.appendChild(input);
		}
	}
}


function checkCVSearchCriteria(frm, submitEvent) {
	var result = true;
	if (frm.keywords.value.length < 1
			&& frm.location.value.length < 1
			&& frm.cat.selectedIndex < 0
			&& frm.contract_type.selectedIndex < 1
			&& frm.salary_minimum.selectedIndex < 1
			&& frm.salary_maximum.selectedIndex < 1
	   ) {
		alert('Please specify your search criteria.');
		result = false;
	}

	if (result && frm.savealert != null && frm.savealert.value == 1 ) {
		// savealert gets set to 1 when any CV Alert inputs get focus, and 0 onBlur.
		result = checkAlert(frm);
	}
	return result;
}

function checkAlert(element) {
    var frm = element;
    var result = true;
    var message = '';
    var messages = new Array();
    if (frm != null) {
        // Put the validation in reverse order so that the focus goes into the first invalid input as displayed to the user
        if (frm.email != null) {
            if (! validEmail(frm.email.value) ) {
                frm.email.focus();
                changeClass('alertEmail', 'textfielderror');
                messages[messages.length] = 'Please provide a valid email address to send this CV Alert to';
                result = false;
            } else {
                changeClass('alertEmail', 'textfield');
            }
        }
        if (frm.v != null) {
            if (frm.v.value.length < 1) {
                frm.v.focus();
                messages[messages.length] = 'Please give your CV Alert a name';
                changeClass('alertName', 'textfielderror');
                result = false;
            } else {
                changeClass('alertName', 'textfield');
            }
        }


    }
    if (messages.length > 0) {
        for (var i = messages.length - 1; i >= 0 ;i--) {
            message += messages[i] + "\n";
        }
        alert(message);
    }
    return result;
}

function getCVSearchLocation(startingUrl,page) {
    // If the user had the fulloptions down, leave them down and vis-versa // GPB 20060807
    return startingUrl
    	+ '&g='
		+ page
		+ '&fulloptions='
		+ document.cvsearch.fulloptions.value
		+ '&mopr='
		+ document.cvsearch.mopr.value;
}



// Tab Content: 7 functions below, for latest jobs on home page
// Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
// Last updated: June 29th, 06
// Localy modified to remove global variables

function expandcontent(linkobj, tabcontentIDs){
	var ulid=linkobj.parentNode.parentNode.id
	var ullist=document.getElementById(ulid).getElementsByTagName("li")
	for (var i=0; i<ullist.length; i++){
		ullist[i].className=""
		if (typeof tabcontentIDs[ulid][i]!="undefined")
			document.getElementById(tabcontentIDs[ulid][i]).style.display="none"
	}
	linkobj.parentNode.className="selected"
	document.getElementById(linkobj.getAttribute("rel")).style.display="block"
	saveselectedtabcontentid(ulid, linkobj.getAttribute("rel"), tabcontentIDs)
}

function savetabcontentids(ulid, relattribute, tabcontentIDs){
	if (typeof tabcontentIDs[ulid]=="undefined")
		tabcontentIDs[ulid]=new Array()
	tabcontentIDs[ulid][tabcontentIDs[ulid].length]=relattribute
}

function saveselectedtabcontentid(ulid, selectedtabid){
		setCookie(ulid, selectedtabid)
}

function getullistlinkbyId(ulid, tabcontentid){
	var ullist=document.getElementById(ulid).getElementsByTagName("li")
	for (var i=0; i<ullist.length; i++){
		if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel")==tabcontentid){
			return ullist[i].getElementsByTagName("a")[0]
			break
		}
	}
}

function initializetabcontent(){
	var tabcontentIDs=new Object()
	for (var i=0; i<arguments.length; i++){
		var clickedontab=getCookie(arguments[i])
		var ulobj=document.getElementById(arguments[i])
		var ulist=ulobj.getElementsByTagName("li")
		for (var x=0; x<ulist.length; x++){
			var ulistlink=ulist[x].getElementsByTagName("a")[0]
			if (ulistlink.getAttribute("rel")){
				savetabcontentids(arguments[i], ulistlink.getAttribute("rel"), tabcontentIDs)
				ulistlink.onclick=function(){
					expandcontent(this, tabcontentIDs)
					return false
				}
				if (ulist[x].className=="selected" && clickedontab=="")
					expandcontent(ulistlink, tabcontentIDs)
			}
		}
		if (clickedontab!=""){
			var culistlink=getullistlinkbyId(arguments[i], clickedontab)
		if (typeof culistlink!="undefined")
			expandcontent(culistlink, tabcontentIDs)
		else
			expandcontent(ulist[0].getElementsByTagName("a")[0], tabcontentIDs)
		}
	}
}


function getCookie(Name){
	var re=new RegExp(Name+"=[^;]+", "i");
	if (document.cookie.match(re))
		return document.cookie.match(re)[0].split("=")[1]
	return ""
}

function setCookie(name, value){
	document.cookie = name+"="+value
}


// Treat areas as links: 4 funtions below
// http://www.digital-web.com/articles/the_pinball_effect/
// Locally modified to remove global var

function pinballEffect()
{
	var W3CDOM = (document.createElement && document.getElementsByTagName);
	if (!W3CDOM) return;
	var allElements = document.getElementsByTagName('*');
	var originalBackgrounds=new Array();
	for (var i=0; i<allElements.length; i++)
	{
		if (allElements[i].className.indexOf('pinball-scoop') !=-1)
		{
			allElements[i].onmouseover = mouseGoesOver;
			allElements[i].onmouseout = mouseGoesOut;
			allElements[i].onclick = mouseGoesClick;
		}
	}
}

function mouseGoesOver()
{
	originalClassNameString = this.className;
	this.className += " pinball-on";
	this.style.cursor = "pointer";
	this.style.cursor = "hand";

}

function mouseGoesOut()
{
	this.className = originalClassNameString;
}

function mouseGoesClick()
{
	var allThisAreasElements = this.getElementsByTagName('*');
	for (var j=0; j<allThisAreasElements.length; j++)
	{
		if (allThisAreasElements[j].className.indexOf('pinball-sinkhole') != -1)
		{
			var url = allThisAreasElements[j].href;
			window.location = url;
		}
	}
}
/* job term checkboxes turned on and off as any one is checked */
function jobTermOC(input) {
	if (input == null || input.form == null || input.form.contract_all == null) return;
	if (input.name != 'contract_all') {
		input.form.contract_all.checked = false;
	} else {
		var frm = input.form;
		if (frm.contract_all.checked) {
			for (var i = 0; i < frm.length;i++) {
				if (frm[i].name != null && frm[i].name.match(/^contract_/)) {
					if (frm[i].name != 'contract_all') {
						frm[i].checked = false;
					}
				}
			}
		}
	}
}

function Ads(ads){
	this.counter = 0;
	this.classCounter = 1;
	this.ads = ads;
}

Ads.prototype = {
	getNextAd : function(){
		return this.ads[this.counter++];
	},
	showAd : function(ad){
		return ad != null ? '<li class="'+(this.classCounter++ % 2 ? 'odd' : 'even')+'">'+ad+'</li>' : '';
	},
	showCustomAd : function(){
		return this.showAd(this.getNextAd());
	}
}
													   
