
function shortlistJob(id, targetButton, notOnMainPage){

	if(!notOnMainPage){
		// need to change some things to indicate that we have a shortlist now
		if(!anyShortlisted()){
			changeApplyButtons();
			displayApplyLink("block");
		}
	}

	// set cookie

	var shortCookie = readCookie("shortlist");
	var cvalue = "";

	if(shortCookie == null) cvalue = id;
	else cvalue = shortCookie + "," + id;

	createCookie("shortlist", cvalue, 365);

	// show status

	var parentEl = targetButton.parentNode;

	if(!notOnMainPage){

		var lastSibling = parentEl.lastChild;
		var jobDetail = parentEl.parentNode;
		var rowKey = jobDetail.id.replace(/jobDetail/, "");

		if(parentEl && lastSibling && jobDetail && rowKey){

			if(parentEl.firstChild.nodeName && parentEl.firstChild.nodeName != "P"){

				var stateMsg = document.createElement("p");
				stateMsg.className = "positiveMsg shortlisted";
				var stateTxt = document.createTextNode("job shortlisted - ");
				stateMsg.appendChild(stateTxt);

				var contLink = document.createElement("a");
				contLink.href = "javascript:closeJob("+rowKey+");";
				contLink.className = "positiveMsg";
				var contTxt = document.createTextNode("continue browsing");
				contLink.appendChild(contTxt);

				stateMsg.appendChild(contLink);

				parentEl.insertBefore(stateMsg, parentEl.firstChild);

			}

		}

	}else{

		if(parentEl){

			var stateMsg = document.createElement("p");
			stateMsg.className = "positiveMsg shortlisted";
			var stateTxt = document.createTextNode("job shortlisted");
			stateMsg.appendChild(stateTxt);

			var grandParent = parentEl.parentNode;

			grandParent.insertBefore(stateMsg, parentEl);

		}

	}

}


function anyShortlisted(){

	var cookieVal = readCookie("shortlist");

	return (!(cookieVal == null || cookieVal == ""));

}


/*
	Change display depending on whether there is a shortlist -----------------------
*/

function displayApplyLink(val){

	document.getElementById("applyBox").style.display = val;

}

function changeApplyButtons(){

	// change Apply buttons too

	var buttons = document.getElementsByTagName("button");

	for(var a=0; a<buttons.length; a++){

		var buttonTextNode = buttons[a].firstChild;

		if(buttonTextNode){

			var buttonText = buttonTextNode.nodeValue;

			if(buttonText == "Apply"){
				buttonTextNode.nodeValue = "Apply for shortlisted jobs";
			}

		}

	}

}


/*
	Cookie functions ---------------------------------------------------------------
	These functions from http://www.quirksmode.org/index.html?/js/cookies.html
*/
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}