// sg_main - main JS for SecondGuess

var isDl = 0;								// 1 if Deputy Leader
var openTab = 0;						// current MP selector tab
var mainLoaded = 0,
		mpSelLoaded = 0,
		summGridLoaded = 0;

var Mps = new Array();				// array of MP details
		MpEls = new Array();			// .. and elements, for snappiness
		MpElsSet = new Array();		// array of set elements, for ditto
		MpTabsStart = new Array();		// the starting MP number of each display tab
		MpTabsDone = new Array(0,0,0,0,0,0,0,0,0,0);
		NMpTabs = 9;							// get from server at some point

var userChA    = new Array(0,0,0,0,0);	// userChoices(n, n=1..5) contains mp
var userChASav = new Array(0,0,0,0,0);	// backup, for revert
var userSgA    = new Array();	// userSecondGuesses (nominators)

var timeLines_mps = new Array();			// each new mp will have an entry here
var timeLines_vals = new Array ();		// each mp has a sub-array of values
var timeLines_start;
var timeLines_end;

var nominees = new Array();
var nNominees = 0;

var loggedIn = 0,
		userNo,
		us_uname = '',
		us_email = 'some email address, Xhuh?',
		us_age   = 0,
		us_sex   = '',
		us_union = 0,
		us_labparty = 0,
		us_elist = -1,
		
		us_urborrur = 0,
		us_politics = 0,
		us_greatestpm = 0,
		us_class = 0,
		us_iraq = 0,
		us_edu = 0,
		us_privnhs = 0,
		us_npower = 0,
		us_trident = 0,
		us_socordem = 0,
		us_devorinv = 0,
		us_treatorprev = 0;
		
var	addedToRecentMps = 0;			// so we know what message to give
		
	/* */
	/* Different text size for three screen sizes */
	if (screen.width < 1100)
	  document.write('<style type="text/css"> @import url(css/lores.css); </style>');
	else if (screen.width < 1400)
	  document.write('<style type="text/css"> @import url(css/midres.css); </style>');
	else
	  document.write('<style type="text/css"> @import url(css/hires.css); </style>');
	/* Different style variations for IE and not IE */
	if (isIE)
	  document.write('<style type="text/css"> @import url(css/ie.css); </style>');
	else
	  document.write('<style type="text/css"> @import url(css/ienot.css); </style>');

/* */

						<!--
						/*
						// Insert click tracking URL here
								document.phpAds_ct0 ='Insert_Clicktrack_URL_Here'
						
							 var awrz_rnd = Math.floor(Math.random()*99999999999); 
							 var awrz_protocol = location.protocol.indexOf('https')>-1?'https:':'http:';
							 if (!document.phpAds_used) document.phpAds_used = ',';
							 document.write ("<" + "script language='JavaScript' type='text/javascript' src='"); 
							 document.write (awrz_protocol+"//mslogin.co.uk/m129/adjs.php?n=a003cf2e");
							 document.write ("&zoneid=17&target=_blank&withtext=1&block=1&blockcampaign=1");
							 document.write ("&exclude=" + document.phpAds_used);
							 document.write ("&loc=" + escape(window.location));
							 if (document.referrer)
									document.write ("&referer=" + escape(document.referrer ));
							 document.write ('&r=' + awrz_rnd);
							 document.write ("&ct0=" + escape(document.phpAds_ct0));
							 document.write ("'><" + "/script>");
						*/
						//-->


function checkRadioField (f, fld, val) {
	// checck radio field fld with value val in form f - assumed object
    for (var i = 0; i < f.length; i++) {
			var e = f.elements[i];
			if ((e.type == "radio") && (e.name == fld) && (e.value == val)) {
				e.checked = true;
				return;
			}
		} // for
} // checkRadioField

function setTextField (fld, val, f) {
	// set text field fld with value val in form f (optional)
	getEl(fld).value = val;
} // setTextField

displayUnamePw.usernameNode = null;
function displayUnamePw (disp) {
	// display userNameAndPw according to disp
	// because of IE problem, must move node
	var f = getEl("userProfileForm");
	debug ("calling displayUnamePw");
	if (disp) {		// display it
		if (displayUnamePw.usernameNode) { // we have hidden it
			debug (" - displaying");
			f.insertBefore (displayUnamePw.usernameNode, f.firstChild);
			displayUnamePw.usernameNode = null;	// delete the reference
			debug (" - displayed");
		}
	} // if (disp)
	else {				// hide it
		if (displayUnamePw.usernameNode == null) {	// put it in storage
			debug (" - hiding");
			displayUnamePw.usernameNode = f.removeChild(getEl("userNameAndPw"));
			debug (" - hidden");
		}
	}
	debug (" - done", 1);
} // displayUnamePw

function fillUserProfile () {
	// fill user profile form from user variables
	var f = getEl("userProfileForm")	
	f.reset();				// first reset the form!
	if (!userNo)			// and skip out if not logged in
		return;
	// debug ("setting email", 1);
	// setTextField ("udFormName", us_uname); - don't need, since removed from document tree
	
	setElHTML ("userNameSpan", us_uname);
	setTextField ("udFormEmail", us_email);
	// debug ("set email", 1);

	setTextField ("udFormAge", us_age);
	setTextField ("udFormUnion", us_union);
	checkRadioField (f, "udFormSex", us_sex);
	checkRadioField (f, "udFormParty", us_labparty);
	checkRadioField (f, "udFormElist", us_elist);

	checkRadioField (f, "udFormTownOrCountry", us_urborrur);
	checkRadioField (f, "udFormPolitics", us_politics);
	checkRadioField (f, "udFormGreatestPM", us_greatestpm);
	checkRadioField (f, "udFormClass", us_class);
	checkRadioField (f, "udFormSupportIraq", us_iraq);
	checkRadioField (f, "udFormSupportChoiceInEducation", us_edu);
	checkRadioField (f, "udFormSupportPrivateNHS", us_privnhs);
	checkRadioField (f, "udFormSupportNuclearPower", us_npower);
	checkRadioField (f, "udFormSupportTrident", us_trident);
	checkRadioField (f, "udFormSocOrDem", us_socordem);
	checkRadioField (f, "udFormDevOrEnv", us_devorinv);
	checkRadioField (f, "udFormTreatOrPreventIllness", us_treatorprev);

	setRadioStyles (f);
	
} // fillUserProfile

function resetUserProfileForm() {
	// calls form reset if registering, else fillUserProfile if editing profile
	
} // resetUserProfileForm

function showHeadTab(n) {
	// Called when clicking on a mp selector head tab
	// debug ("doing tab " + n + " - "); for (var i in MpTabsDone) debug (i + '=' + MpTabsDone[i] + "  "); debug("",1);
	var el = 'mpTab' + openTab;
	removeStyle (el, 'mpHeadSel');
	if (MpTabsDone[openTab])
		setStyle (el, 'mpHeadUnselActive');
	else
		setStyle (el, 'mpHeadUnsel');
	var el = 'mpTab' + n;
	// removeStyle (el, 'mpHeadUnsel');
	setStyle (el, 'mpHeadSel');
	
	dispEl ("mpTable" + openTab, 0);
	dispEl ("mpTable" + n, 1);

	openTab = n;
} // showHeadTab

function makeMp (surname, forename, party, constit) {
	// create the MP object
	this.surname = surname;
	this.forename = forename;
	this.party = party;
	this.constit = constit;
	this.nominator = 0;			// is not a nominator
	this.nominee = 0;				// .. and has no nominee
	this.conf = 0;					// .. or confidence
	this.choice = 0;				// is not chosen
	this.rapidLookup = 0;		// not yet added
} // makeMp

function addMp (idx, surname, forename, party, constit) {
	Mps[idx] = new makeMp (surname, forename, party, constit);
} // addMp

function makeSecondGuess (nominee, conf) {
	// make second guess object
	this.nominee = nominee;
	this.conf = conf;
} // makeSecondGuess

function addSecondGuess (nominator, nominee, conf) {
	userSgA[nominator] = new makeSecondGuess (nominee, conf);
} // addMp

makeMpEls.done = 0;
function makeMpEls() {
	// make MpEls array for rapid access; ~3x as fast!
	// note - can do only after grid loaded.
	if (makeMpEls.done)
		return;
	// debug ("Before MpEls[5]" + MpEls[5]);
	for (var i in Mps) {
		MpEls[i] = getEl("mp"+i);
		MpElsSet[i] = 0;
	}
	// debug (", after MpEls[5]" + MpEls[5],1);
	makeMpEls.done = 1;
} // makeMpEls

function showNSummRows (n) {
	// show first n summary rows, hide remainder
	var i;
	for (i = 1; i <= 10; i++)
		if (i <= n)
			{} // dispEl("summTabRow"+i, 1, 1);
		else
			dispEl("summTabRow"+i, 0);
	// dispEl ("DOMsummGrid", 1); - leave for menu
}

function dispPerc (n) {
	// display percentage with 1 dp
	if (n==0)
		return "0.0%";
	else {
		n = String(n);
		n = n.substr(0, n.length-1) + '.' + n.substr(n.length-1,1) + '%';	// n.substr(-1,1) should work  in IE but doesnot
		return n;
	}
} // dispPerc

function populateSummGrid() {
	// populate summary grid with info from nominees array
	setElHTML("stName1", "Hello there");

	var i;
	var n = 0;
	var nom;
	var mp;
	var diag = 'Added';
	for (i in nominees) {
		n++;
		diag = diag + '   ' + i + '/' + "stName"+n;
		nom = nominees[i];
		mp = Mps[i];
		n = nominees[i].sortPos;
		setElHTML("stName"+n, mp.forename + ' ' + mp.surname);
		setElHTML("stMp"+n, dispPerc(nom.percMp) + ' (' + nom.totCountMp + ')');
		setElHTML("stUn"+n, dispPerc(nom.percUserUnion) + ' (' + nom.totCountUserUnion + ')');
		setElHTML("stPt"+n, dispPerc(nom.percUserParty) + ' (' + nom.totCountUserParty + ')');
		// setElHTML("stOt"+n, dispPerc(nom.percUserOther) + ' (' + nom.totCountUserOther + ')');
		// setElHTML("stTot"+n, dispPerc(nom.percTot)); //  + ' (' + nom.totCountUserOther + ')');
		
	} // for  i
	// setElHTML("masthead", diag);
}

function mpSelDisp (n, short) {
	// return display name of MP for selection box
	var mp = Mps[n];
	if (!mp) {
		debug ("mpSelDisp " + n, 1);
		debug ("MPs " + Mps, 1);
		return;
	}
	if (short)
		if (isDevInt)
			return mp.forename + " " + mp.surname + " [" + n + "]";
		else
			return mp.forename + " " + mp.surname;
	else
		if (isDevInt)
			return mp.forename + " " + mp.surname + " (" + mp.constit + ") [" + n + "]";
		else
			return mp.forename + " " + mp.surname + " (" + mp.constit + ")";
}

// states for selMp (numeric constants
var stateValNull   = 0;			// default state
var stateValAdd    = 1;			// add primary value (choice or nominee)
var stateValChange = 2;			// change ditto
var stateValDel    = 3;			// delete ditto
var stateValNeeAdd = 4;			// add nominee

// state indices for selMp (numeric constants)
var ucStateInd  = 1;
var nomStateInd = 2;
var neeStateInd = 3;
selMp.currState = 0;

// current selMp choices 
selMp.ucCurr  = 0;			// current user choice
selMp.nomCurr = 0;			// current nominator choice
selMp.neeCurr = 0;			// current nominee choice

// current selMp states
selMp.ucState  = 0;			// current user choice state
selMp.nomState = 0;			// current nominator state
selMp.neeState = 0;			// current nominee state

function setSelMpState (stateInd, stateVal) {
	debug ("setSelMpState called " + stateInd + stateVal, 1);
	selMp.currState = stateInd;
	switch (stateInd) {
		case ucStateInd  : 
			selMp.ucState = stateVal;
			if (stateVal==stateValNull) {
				debug ("stateVal==0",1);
				selMp (0, 0);
			}
			else if (stateVal==stateValAdd) {
				debug ("stateVal==1",1);
			}
			debug ("setSelMpState - state ucStateInd, val=" + stateVal, 1);
			break;
		case nomStateInd : 
			selMp.nomState = stateVal;
			debug ("setSelMpState - state nomStateInd, val=" + stateVal, 1);
			break;
		case neeStateInd : 
			selMp.neeState = stateVal;
			debug ("setSelMpState - state neeStateInd, val=" + stateVal, 1);
			break;
	} // switch
} // setSelMpState

function clearMpDisplay () {
	// set all Mp classes to default
	for (var i in Mps) 
		// setStyle ("mp"+i, "");
		setStyle (MpEls[i], "");    
	// debug ("MpEls[5]" + MpEls[5],1);
	for (i = 1; i <= NMpTabs; i++)
		MpTabsDone[i] = 0;
	for (i = 1; i <= NMpTabs; i++)
		if (i!=openTab)
			setStyle ("mpTab"+i, "mpHeadUnsel");
// and clear headers
} // clearMpDisplay

function setupSecondGuessDisplay () {
	// set up displays for second guess
	debug ("setupSecondGuessDisplay ");
	for (var i = 1; i <= NMpTabs; i++) {
		MpTabsDone[i] = 0;
	}
	setSelMpState (nomStateInd, stateValNull);
	for (i in userSgA) {		// cycle throuth second guesses
		Mps[i].nominator = 1;		// set nominator
		Mps[i].nominee = userSgA[i].nominee;
		Mps[i].conf = userSgA[i].conf;
		// debug ("Mp " + i + " nominates mp " + Mps[i].nominee,1);
		selMp(i, 0);			// nominators
		// for (var sgTab = 1; i > MpTabsStart[sgTab]; sgTab++) {
		for (var sgTab = 1; Mps[i].surname > MpTabsStart[sgTab]; sgTab++) {
		}
		sgTab--;		// now points to 1-based array
		// debug (" mp " + i + " tab " + sgTab + ", ",1);
		MpTabsDone[sgTab] = 1;
		/*
		MpTabsStart = new Array();		// the starting MP number of each display tab
		MpTabsDone = new Array(0,0,0,0,0,0,0,0,0);
		NMpTabs = 9;							// get from server at some point
				*/ 
		for (i = 1; i <= NMpTabs; i++)
			if (MpTabsDone[i] && (i!=openTab))
				setStyle ("mpTab"+i, "mpHeadUnselActive");
	}
} // setupSecondGuessDisplay

function setupChoiceDisplay() {
	// setup displays for user choice
	setSelMpState (ucStateInd, stateValNull);	
	for (var i = 0; i < 5; i++)
		if (userChA[i] > 0)
			Mps[userChA[i]].choice = i+1;
} // setupChoiceDisplay

function setSecGuessButtons (val) {
	// 0 = all disabled, 1 = update/reset, 2 = update/reset/delete
	// return; *** doesnt work in Firefox
	switch (val) {
		case 0 : getEl ("udsecondGuessSubmit").disabled = true;
						 getEl ("udsecondGuessReset").disabled =  true;
						 getEl ("udsecondGuessDelete").disabled = true;
						 break;
		case 1 : getEl ("udsecondGuessSubmit").disabled = false;
						 getEl ("udsecondGuessReset").disabled = false;
						 getEl ("udsecondGuessDelete").disabled = true;
						 break;
		case 2 : getEl ("udsecondGuessSubmit").disabled = false;
						 getEl ("udsecondGuessReset").disabled = false;
						 getEl ("udsecondGuessDelete").disabled = false;
						 break;
	} // switch
} // setSecGuessButtons

function clearSecGuessFields () {
	// clear second guess fields and disable buttons;
	setElHTML ("sgNominator", "<< double-click an MP name to complete this field >>");
	addStyle ("sgNominator", "mpHoverTextInstruction");
	setElHTML ("sgNominee", "&nbsp;");
	getEl("sgFormConf").value = -1;
	setSecGuessButtons(0);
} // clearSecGuessFields

selMp.currNominator = 0;
selMp.currNominee = 0;
function selMp(n, clicks) {
	// called when selecting an MP
	// clicks=0 for program call, 1 or 2 for event-driven single or double click
	var currStyle;
	var delPrev;
	var prevN;
	// debug ("selMp : " + n + "/" + clicks + ", state " + selMp.currState + '/');
	switch (selMp.currState) {
		case ucStateInd :
					// debug ("" + selMp.ucState, 1);
					if ((clicks==2) && (selMp.ucState == stateValNull)) {
						for (i = 0; i < 5; i++)
							if (userChA[i]==0)
								break;
						if (i < 5) {
							userChA[i] = n;
							fillUserChoices();
							hiliteCurrUc(i+1);
							return;
						}
					}
					else if ((clicks==0) || (selMp.ucState > stateValNull)) {
						currStyle = "mpSelUc";
						delPrev = 1;
						prevN = selMp.ucCurr;
						selMp.ucCurr = n;
					}
					break;
		case nomStateInd :
		case neeStateInd :
					// debug ("" + selMp.nomState, 1);
					currStyle = "mpSelNom"; 
					// debug ("Adding style " + currStyle + " to Mp " + n, 1);
					delPrev = 0;					// more than one nominator
					// debug ("selMp.nomState " + selMp.nomState, 1);
					if ((clicks==0) && (selMp.nomState == stateValNull)) {
						selMp.nomCurr = n;
					}
					else if ((clicks==2) && (selMp.nomState == stateValNull)) {
						setElHTML ("sgNominator", mpSelDisp(n));
						removeStyle ("sgNominator", "mpHoverTextInstruction");
						addStyle ("sgNominee", "mpHoverTextInstruction");
						var s;
						if (addedToRecentMps)
							s = "<< click on an MP, or recently selected nominee,";
						else
							s = "<< single click on an MP name";
						s += " to update this field >>";
						setElHTML ("sgNominee", s);
						selMp.nomState = stateValNeeAdd;
						selMp.currNominator = n;
						setSecGuessButtons (1);		// all but delete
						if ((Mps[n].nominator==1) && (Mps[n].nominee> 0)) {		// preexisting - fill in nominee
							setSecGuessButtons (2);		// include delete
							removeStyle ("sgNominee", "mpHoverTextInstruction");
							setElHTML ("sgNominee", mpSelDisp(Mps[n].nominee));
							getEl("sgFormConf").value = Mps[n].conf;
							selMp.currNominee = Mps[n].nominee;
							getEl("sgFormConf").focus();	// this is where we type!
							// getEl("sgFormConf").select();	// this is where we type!  // not for dropdown
						}
						debug ("2 clicks - selMp.currNominator " + selMp.currNominator + ", selMp.currNominee" + selMp.currNominee, 1);
						return;
					}
					else if ((clicks==1) && (selMp.nomState == stateValNeeAdd)) {
						removeStyle ("sgNominee", "mpHoverTextInstruction");
						setElHTML ("sgNominee", mpSelDisp(n));
						selMp.currNominee = n;
						getEl("sgFormConf").focus();	// this is where we type!
						// getEl("sgFormConf").select();	// this is where we type!  // not for dropdown
						debug ("1 click - selMp.currNominator " + selMp.currNominator + ", selMp.currNominee" + selMp.currNominee, 1);
						return;
					}
					else {
						debug ("Other - selMp.currNominator " + selMp.currNominator + ", selMp.currNominee" + selMp.currNominee, 1);
						return;
						prevN = selMp.nomCurr;
						selMp.nomCurr = n;
					}
					break;
		// case neeStateInd :
					// debug ("" + selMp.neState, 1);
					currStyle = "mpSelNee";
					delPrev = 1;
					prevN = selMp.neeCurr;
					selMp.neeCurr = n;
					break;
		default :
					// debug ("undefined", 1);
					return;
					alert ("selMp.currState is " + selMp.currState + " in selMp");
					break;
	} // switch
	if (delPrev)
		removeStyle("mp"+prevN, currStyle);
	if (n>0) {
		// debug ("Setting style " + currStyle + "for mp"+n, 1);
		setStyle("mp"+n, currStyle);
	}
} // selMp

function setUpOKorCancelButtons(n) {
	setElHTML("ucAddOrDel" + n, "OK");
	setElHTML("ucChangeOrCancel" + n, "cancel");
	dispEl ("ucChangeOrCancel" + n, 1);
	setElHTML("ucChoiceInstructions", "Select your chosen MP from the list above, and click OK, or click Cancel");
} // setUpOKorCancelButtons

hiliteCurrUc.CurrUc = 0;
function hiliteCurrUc(n) {
	// highlight current uc choice n, clear rest
	debug ("hiliteCurrUc (" + n + ")", 1);
	for (var i = 1; i <= 5; i++)
		setStyle ("ucName" + i, "ucEl ucName");
	if (n) {
		setStyle ("ucName" + n, "ucEl ucNameSel");
		setUserChoiceDisableState (false);
	}
	hiliteCurrUc.CurrUc = n;
} // hiliteCurrUc

function setUserChoiceDisableState(disable) {
	//set enable or disable state of user choice buttons
	getEl("udUserChoiceReset").disabled = disable;
	getEl("udUserChoiceSubmit").disabled = disable;
}

addOrDelUc.inAddOrChange = 0;
// addOrDelUc.CurrUc = 0; *** not used
function addOrDelUc(n) {
	// Add/Del/OK button EVENT HANDLER
	debug ("Add/Del/OK button EVENT HANDLER " + n, 1);
	switch (selMp.ucState) {
		case stateValDel : 						// delete if there is someone there already *** code not accessible - check
						debug ("addOrDelUc Del", 1);
						userChA[n-1] = 0;
						fillUserChoices();
						hiliteCurrUc(n);
						break;
		case stateValChange :
		case stateValAdd : 	// confirm add
						debug ("addOrDelUc Change or Add", 1);
						userChA[n-1] = selMp.ucCurr;			// get the value
						debug ("selMp.ucCurr " + selMp.ucCurr, 1);
						setSelMpState (ucStateInd, stateValNull);	// and set state to null
						// addOrDelUc.inAddOrChange = 0;			// and "in add"
						fillUserChoices(); 
						hiliteCurrUc(n);
						break;
		case stateValNull : 												// add - this is called first and last
						// addOrDelUc.inAddOrChange = n
						debug ("addOrDelUc Null", 1);
						if (userChA[n-1]) {									// someone there already, so delete
							userChA[n-1] = 0;									// mark array null
							fillUserChoices();								// redo display
							hiliteCurrUc(n);
						}
						else {															// set up OK/Cancel
							setSelMpState (ucStateInd, stateValAdd);
							setUpOKorCancelButtons(n);
						}
						break;
	} // switch
} // addOrDelUc

function changeOrCancelUc(n) {
	if (addOrDelUc.inAddOrChange && (n==hiliteCurrUc.CurrUc)) {		// reset to 0
		userChA[n] = userChASav[n];
		addOrDelUc.inAddOrChange = 0;		// 
		selMp (0, 0);							// cancel selected MP
		hiliteCurrUc(0);					// .. and highlight
		fillUserChoices();
	}
	else {			// change - as for add
		fillUserChoices();				// in case we have skipped to a different entry
		hiliteCurrUc(n);
		addOrDelUc.inAddOrChange = n;
		setSelMpState (ucStateInd, stateValAdd);
		setUpOKorCancelButtons(n);
	}
} // changeOrCancelUc

function upUc (n) {
	var temp;
	if (n > 1) {
		n--;
		temp = userChA[n-1];
		userChA[n-1] = userChA[n];
		userChA[n] = temp;
		fillUserChoices();
		hiliteCurrUc(n);
	}
} // upUc

function downUc (n) {
	var temp;
	if (n < 5) {
		n--;
		temp = userChA[n+1];
		userChA[n+1] = userChA[n];
		userChA[n] = temp;
		fillUserChoices();
		hiliteCurrUc(n+2);
	}
} // downUc

function resetUserChoice() {
	// reset user choice from userChASav
	for (var i in userChA)
		userChA[i] = userChASav[i];
	fillUserChoices();
	hiliteCurrUc();
	setSelMpState (ucStateInd, stateValNull);
	setUserChoiceDisableState(true);
} // resetUserChoice

function squashChoices() {
	// remove gaps in user choices
	var foundNull = 1;		// force 1st iteration
	var foundVal = 0;
	// debug ("squashChoices", 1);
	while (foundNull) {
		foundNull = 0;
		foundVal = 0;
		for (i = 4; i >= 0; i--) {
			if (userChA[i])
				foundVal = i;
			else if (foundVal) { // null, with lower foundVal
				// debug ("squashChoices - moved " + foundVal + " to " + i, 1);
				userChA[i] = userChA[foundVal];
				userChA[foundVal] = 0;
				foundNull = 1;
			}
		} // for i
	} // while
	hiliteCurrUc();
} // squashChoices

onLinkHover.text = '';			// save existing text - not implemented yet
onLinkHover.lastIn = 0;			// so we don't save twice
function onLinkHover (n) {
	var text, el;
	var elHover = getEl("innerMessageBox");
	var inChoice = (menu.currMen == menuChoices);
	var special = 0;
	
	if ((n > 0) && (el = getEl("mp"+n))) {
		text = '' + mpSelDisp(n) + '';
		
		if (inChoice) {
			var num;
			var choice =  Mps[n].choice;
			if (choice) {
				special = 1;
				switch  (choice) {
					case 1 : num = '1st'; break;
					case 2 : num = '2nd'; break;
					case 3 : num = '3rd'; break;
					case 4 : num = '4th'; break;
					case 5 : num = '5th'; break;
				}
				text = 'MP <div class="mpHoverText">' + text + 
									'</div><br />is your <div class="mpHoverText">' + num + "</div> choice for leader<br />"
			} // if (choice)
		} // if (inChoice)
		else {
			if ((Mps[n].nominator > 0) && (Mps[n].nominee > 0)) {
				special = 1;
				text = 'MP <div class="mpHoverText">' + text + '</div><br />is nominating <div class="mpHoverText">' + mpSelDisp(Mps[n].nominee) + "</div><br />" +
									'with confidence level <div class="mpHoverText">' + Mps[n].conf + '</div>';
			}
		} // if (inChoice) .. else
		if (false && !special)
			text = "&nbsp;<br />" + text + "<br />&nbsp;";
	} // n > 0
	else
		text = "";
	setMessage (text);
} // onLinkHover

function showEvents(s) {
	// debug("showEvents(" + s + ")", 1);
	setElHTML("innerMessageBox", s);
}

function hoverEvents(s, x, y) {
	wid = 800;
	eb = getEl("eventsBox");
	if (s) {
		setElHTML(eb, s);
		dispEl (eb, 1);
		h = xHeight(eb);
		w = xWidth(eb);
		debug ("height="+h+", width="+w, 1);
		if ((x + w) > wid) {
			x = x - w;
			if (x < 0)
				x = 10;
		}
		eb.style.top = (y - 8 - h) + "px";
		eb.style.left = (x) + "px";
	}
	else
		dispEl (eb, 0);
}

function resetMpChoices() {
	// make suere right Mps are tagged
	for (var i in Mps)
		Mps[i].choice = 0;
	for (var i = 0; i < 5; i++)
		if (userChA[i] > 0)
			Mps[userChA[i]].choice = i+1;
	hiliteCurrUc();
} // resetMpChoices

function resetMpGuesses() {
	// make suere right Mps are tagged
	for (var i in Mps)
		Mps[i].nominator = 0;
	setupSecondGuessDisplay();
} // resetMpGuesses

function setError (s) {
	// put message in error box, set style appropriately
	var imb = getEl("innerMessageBox");
	if (s && (s > '')) {
		setStyle (imb, "error");
		setElHTML (imb, s);
	}
	else
		clearMessage();
}

function setMessage (s) {
	var imb = getEl("innerMessageBox");
	removeStyle (imb, "error");
	if (s && (s > '')) {
		setElHTML (imb, s);
	}
	else {
		setElHTML (imb, '');
	}
}

function clearMessage() {
	setMessage ();
}
