// JavaScript Document

var counts;
var nItems;
var nButtons;

function getParam(name)
{
 	var start = location.search.indexOf("?" + name + "=");
	if (start < 0) 
	  	start = location.search.indexOf("&" + name + "=");
	if (start < 0)
  		return '';

	start += name.length + 2;
	var end = location.search.indexOf("&", start) - 1;
	if (end < 0)
		end = location.search.length;
		
	var result='';
	for (var i = start; i <= end; i++) 
	{
	    var c = location.search.charAt(i);
		result = result + (c == '+' ? ' ' : c);
	}
	return unescape(result);
}

function setNumberOfItems()
{
	var parm;

	parm = getParam("items");
	if (parm == "")
   		parm = "10";
	return parm;
}

function buildTable(dyn)
{
	var row;
	var rowButton;
						
	// parse input parameter for number of items					
	nItems = setNumberOfItems();
	
	// build the instructions
	document.write("<table width='720'>");
    document.write("<tr><td class='instructions' style='font-weight:bold;'>");
	document.write("<a href='http://www.beverlyryle.com/prioritizing-grid'>Back</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com'>Home</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/credentials'>Credentials</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/media'>Media</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/services'>Services</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/career-retreats'>Retreats</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/book'>Book</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/newsletter-blog'>Blog</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/contact'>Contact</a>");
	document.write("</td></tr>");
	document.write("<tr class='noprint'><td>");
	if (dyn)
	{
		document.write("<p class='headings' id='div1' onclick='expand(" + '"text1"' + "," + "this);'>PRIORITIZING GRID INSTRUCTIONS (click to show)</p>");
		document.write("<p class='instructions' id='text1' style='display:none'>");
	}
	else
	{
		document.write("<p class='headings' id='div1' onclick='expand(" + '"text1"' + "," + "this);'>PRIORITIZING GRID INSTRUCTIONS (click to hide)</p>");
		document.write("<p class='instructions' id='text1'>");
	}
	document.write("First, enter items to be prioritized in the numbered slots below.<br/><br/>");
	document.write("Next, use the radio buttons to indicate your preferences among the items by considering them in pairs, one pair at a time.<br/><br/>");
	document.write("As you place the mouse pointer over each box with a pair of radio buttons, you will see the associated items highlighted. Click the button that indicates ");
	document.write("the one you prefer and the highlighting will change to indicate your preference. Use the first row of radio button boxes to indicate your preference between ");
	document.write("items 1 and 2, the second row to indicate your preferences between items 1 and 3, and 2 and 3, and so forth until you reach the end of the form. Then click the ");
	document.write("PRIORITIZE button to generate the list of PRIORITIZED ITEMS on the right. You may not skip any of the button boxes, but must make a choice between all item pairs.<br/><br/>");
	document.write("If a row of the PRIORITIZED ITEMS list is highlighted, it means you have a tie, i.e., two or more items have received an equal number of votes.<br/><br/>");
	document.write("There are two ways to break the tie. The easiest is to click the highlighted row and a new window will open with a grid which contains just the items that are tied. ");
	document.write("For this to work, however, your browser must allow popups.<br/><br/>");
	document.write("If your browser does not allow popups, find the preferred item in the highlighted row in the list of UNPRIORITIZED ITEMS and click the <strong>+</strong> button next ");
	document.write("to it to increase its ranking.");
	document.write("For example: in a prioritized list of beverages, COFFEE and TEA both appear in the same highlighted row, which means they both got the same ");
	document.write("number of votes. To break the tie, you click the <strong>+</strong> button next to COFFEE in the UNPRIORITIZED ITEMS list, since you prefer coffee to tea.");
	document.write("In case of a three-way tie, click the button beside your highest preference TWICE, and then click the button beside your second highest preference ");
	document.write("ONCE.<br/><br/>If you make a mistake or get confused during tie-breaking, you can always click the PRIORITIZE button to start over.<br/><br/>");
	document.write("When you are done, you can use the PRINT RESULTS button below to print out the items in the prioritized list followed by the unprioritized list, or, if you want to print the entire form, you can use your browser's print preview dialog to format and print your results.<br/><br/>");

	if (!dyn)
	{
		document.write("Before you begin you might want to work with a couple of samples. Click the DESSERTS button below to generate a sample list of desserts to prioritize, or the ");
		document.write("WORK VALUES button to generate a sample list of work values.<br/><br/>");
		document.write("<input type='button' value='Desserts' onClick='desserts()' class='prioritizebutton' title='Click to populate the grid with a list of sample desserts you can use for ");
		document.write("practice.'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		document.write("<input type='button' value='Work Values' onClick='workvalues()' class='prioritizebutton' title='Click to populate the grid with a list of sample work values you can ");
		document.write("use for practice.'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	}
	
	document.write("</td></tr>");
	document.write("<tr><td>");
	document.write("<p class='copyright'>Prioritizing Grid © 1989 by Richard N. Bolles. Used by permission.<br/>HTML/Javascript version by Michael Ryle, © 2009 by the Center for ");
	document.write("Career and Business Development.");
	document.write("</td></tr>");
	document.write("</table>");

	// build the headings and the first possibility field
	document.write("<table><tr><td>");
	document.write("<form name='grid'>");	
	document.write("<table>");
	document.write("<tr><td colspan='3'><p class='instructions' style='font-weight:bold'>UNPRIORITIZED LIST<br/><span style='font-weight:normal'>Enter items and<br/>indicate preferences</span></p><textarea class='unorderedlist' wrap='soft' name='possibility1' id='p1'></textarea></td>");
	document.write("<td class='cell' valign='bottom'>1<br/><input type='button' value='+' onClick='moveup(1)' class='movebutton'/></td></tr>");

	// build the radio button rows
	nButtons = 0;
	for (row = 1; row < nItems; row++)
	{
		document.write("<tr>");
		for (rowButton = 0; rowButton < row; rowButton++)
		{
			nButtons++;
			document.write("<td class='radiocell' id='rcell" + nButtons + "' onMouseover='highlight(" + nButtons + "," + (row+1) + "," + (rowButton+1) + ");' onMouseout='unhighlight(" + (row+1) + ", " + (rowButton+1) + ");'>");
			document.write("<input class='radioinput' onClick='highlight(" + nButtons + "," + (row+1) + "," + (rowButton+1) + ");' type='radio' id='r" + nButtons + "a' name='r" + nButtons + "' value='" + (rowButton+1) + "'>" + (rowButton+1));
			document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
			document.write("<input class='radioinput' onClick='highlight(" + nButtons + "," + (row+1) + "," + (rowButton+1) + ");' type='radio' id='r" + nButtons + "b' name='r" + nButtons + "' value='"+ (row+1) + "'>" + (row+1) + "</td>");
		}
		document.write("<td class='cell' colspan='3'><textarea class='unorderedlist' wrap='soft' name='possibility" + (row+1) + "' id='p" + (row+1) + "'></textarea></td>");
		document.write("<td class='cell' valign='bottom'>" + (row+1) + "<br/><input type='button' value='+' onClick='moveup(" + (row+1) + ")' class='movebutton'/></td>");
		document.write("</tr>");
	}
	
	// prioritize, clear and print buttons 
	document.write("<tr class='noprint'><td colspan='12'><br/>");
	document.write("<input type='button' value='Prioritize' onClick='prioritize(0)' class='prioritizebutton' title='Click to run prioritization and create an ordered list of prioritized items.'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	document.write("<input type='button' value='Blank Form' onClick='clearForm()' class='prioritizebutton'  title='Click to create a blank form.'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	document.write("<input type='button' value='Print Results' onClick='printResults()' class='prioritizebutton'  title='Click to print results.'/>");
	document.write("</td></tr>");
	
	// close the left side inner table
	document.write("</table></form></td>");
	
	// build the ordered list, close the right side inner and outer tables
	document.write("<td valign='top'><p class='instructions' style='font-weight:bold'>PRIORITIZED LIST<br/><span style='font-weight:normal'>Click highlighted rows to break ties<br/> or use '+' button to increase ranking</span></p>");
	document.write("<form name='orderedlist'><table><div id='olist'>");
	for (row = 0; row < nItems; row++)
	{
		document.write("<tr><td><textarea class='orderedlist' wrap='soft' name='p" + (row+1) + "' id='o" + (row+1) + "' readonly onclick='tiebreak(this)'></textarea></td></tr>");
	}
	document.write("</div></table></form></td></tr></table>")

 	// build the hidden tiebreak result field
	document.write("<table><tr><td><form name='tbresult'>");
	document.write("<input type='hidden' id='tblist' name='tbstrings'></input>");
	document.write("</form></td></tr></table>")

	// build the email table
	document.write("<table width='216' class='noprint'><tr><td>");
	document.write("<p class='instructions'>IF YOU ARE A CLIENT OF BEVERLY RYLE and you have been asked to email your results to her, fill in the form below and click the button.<br/><br/>PLEASE NOTE: YOU CAN'T USE THIS FORM TO EMAIL YOUR RESULTS TO YOURSELF. I wish you could. It would be a nice feature to have, but the server doesn't allow web forms to assign the recipient email address dynamically, nor does it allow cc addresses. If it did, spammers would hijack the form to send unwanted email. It's just another example of how spammers spoil things for everybody.</p>");
	document.write("<form name='results' id='results' method='post' action='/cgi-sys/formmail.pl'>");
	document.write("<input type='hidden' id='recipient' name='recipient' value='info@successonyourownterms.com'/>");
	document.write("<input type='hidden' name='redirect' value='http://www.beverlyryle.com/grid-results-sent'/>");
	document.write("<input type='hidden' name='subject' value='Prioritizing Grid Results'/>");
	document.write("<input type='hidden' name='required' value='name'/>");
	document.write("<p class='instructions'>");
	document.write("Name (required)<br/><input type='text' id='mailtoname' name='name' class='clientname'><br/><br/>");
	document.write("Notes<br/><textarea class='clientnotes' wrap='soft' name='notes'></textarea><br/><br/>");
	for (row = 0; row < nItems; row++)
		document.write("<input type='hidden' name='Item_0" + (row+1) + "' id='eo" + (row+1) + "' value=''/>");
	document.write("<input class='sendemailbutton' type='button' onclick='sendToBev();' value='Email Results to Beverly Ryle'/>");
	document.write("</form>");
	document.write("</td></tr>");
    document.write("<tr><td class='instructions' style='font-weight:bold;'><br/>");
	document.write("<a href='http://www.beverlyryle.com/prioritizing-grid'>Back</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com'>Home</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/credentials'>Credentials</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/media'>Media</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/services'>Services</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/career-retreats'>Retreats</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/book'>Book</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/newsletter-blog'>Blog</a>&nbsp;&nbsp;|&nbsp;&nbsp;");
	document.write("<a href='http://www.beverlyryle.com/contact'>Contact</a>");
	document.write("</td></tr>");
	document.write("</table>");
	
	// initialize the array of item counts
	counts = new Array(nItems);
}

function sendToBev()
{
	document.results.submit();
}

function printResults()
{
    var wo;
    var optstr;
    var i;

//	if (navigator.appName == "Opera")
//	{
//		alert("Unfortunately, because the Opera browser executes the window print function asynchronously, this won't work here. Try a different browser.");
//      return;
//	}

	optstr = "left=100,top=100,width=350,height=350,toolbars=no,scrollbars=no,status=no,resizable=no,titlebar=no";
	wo = window.open("", "PrioritizingGridResults", optstr);

    wo.document.write("<p>PRIORITIZED LIST</p>");
	for (i = 0; i < nItems; i++)
        wo.document.write("<p>" + (i+1) + ". " + document.getElementById("o" + (i+1)).value + "</p>");

    wo.document.write("<p>&nbsp;</p>");

    wo.document.write("<p>UNPRIORITIZED LIST</p>");
	for (i = 0; i < nItems; i++)
        wo.document.write("<p>" + (i+1) + ". " + document.getElementById("p" + (i+1)).value + "</p>");

	if (navigator.appName == "Opera")
	{
	    wo.document.close();
	    wo.focus();
        wo.onload = wo.print;
    }
    else
    {
        wo.document.close();
        wo.focus();
        wo.print();
        wo.close();
    }
}

function adjustRanking()
{
	var tblist;
	var splitResult;
	var p;
	var i;
	var j;

	// reference the hidden tiebreak result field
	tblist = document.getElementById("tblist");
	
	// parse data into separate strings
	splitResult = tblist.value.split("\n");
	
	// find each parsed string (except for the last) in the possibility list and call moveup() the requisite number of times to adjust the ranking upward and break the tie
	for (i = 0; i < splitResult.length - 1; i++)
	{
		for (j = 0; j < nItems; j++)
		{
			p = document.getElementById("p" + (j+1));
			if (trim(p.value) == trim(splitResult[i]))
			{
				for (k = 0; k < (splitResult.length - i - 1); k++)
					moveup(j+1);				
			}
		}
	}
}

function buildTiebreakTable()
{
	var row;
	var rowButton;
	var i;
	var parm;
	var p;
	var w;
	var nTD;
	
	// parse input parameter for number of items					
	nItems = setNumberOfItems();
	
	// Instructions
	document.write("<table><tr><td class='headings'>Re-prioritize items and click the ACCEPT button to break the tie.");

	// build the structure and the first possibility field
	document.write("<table><tr><td>");
	document.write("<table>");
	document.write("<tr>");
	nTD = (nItems - 1) + 4;
	for (i = 0; i < nTD; i++)
		document.write("<td class='filler'>&nbsp;</td>");
	document.write("</tr>");

	// build the radio button rows
	document.write("<tr><td colspan='3'><textarea class='unorderedlist' wrap='soft' name='possibility1' id='p1'></textarea></td><td width='52'></td>");

	nButtons = 0;
	for (row = 1; row < nItems; row++)
	{
		document.write("<tr>");
		for (rowButton = 0; rowButton < row; rowButton++)
		{
			nButtons++;
			document.write("<td class='radiocell' id='rcell" + nButtons + "' onMouseover='highlight(" + nButtons + "," + (row+1) + "," + (rowButton+1) + ");' onMouseout='unhighlight(" + (row+1) + ", " + (rowButton+1) + ");'>");
			document.write("<input class='radioinput' checked onClick='highlight(" + nButtons + "," + (row+1) + "," + (rowButton+1) + ");prioritize(0);' type='radio' id='r" + nButtons + "a' name='r" + nButtons + "' value='" + (rowButton+1) + "'>");
			document.write("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
			document.write("<input class='radioinput' onClick='highlight(" + nButtons + "," + (row+1) + "," + (rowButton+1) + ");prioritize(0);' type='radio' id='r" + nButtons + "b' name='r" + nButtons + "' value='"+ (row+1) + "'></td>");
		}
		document.write("<td class='cell' colspan='3'><textarea class='unorderedlist' wrap='soft' name='possibility" + (row+1) + "' id='p" + (row+1) + "'></textarea></td><td width='52'></td>");
	}
		
	// close the left side inner table
	document.write("</table></td>");
	
	// create the ordered list, close the right side inner and outer tables
	document.write("<td><table>");
	document.write("<td class='filler'>&nbsp;</td>");
	for (row = 0; row < nItems; row++)
	{
		document.write("<tr><td><textarea class='orderedlist' wrap='soft' name='p" + (row+1) + "' id='o" + (row+1) + "' readonly'></textarea></td></tr>");
	}
	document.write("</table></table>")
	
	// create the accept button 
	document.write("<table><tr><td><br/>");
	document.write("<input type='button' value='Accept' onClick='acceptTiebreak();' class='prioritizebutton' title='Accept tiebreak.'/>");
	document.write("</td></tr></table>");

	
	// lock possibilities for editing
	for (i=0; i < nItems; i++)
	{
		parm = getParam("item" + (i+1));
		p = document.getElementById("p" + (i+1));
		p.value = parm;
		p.readOnly = true;
	}
	
	// create the array of item counts and prioritize
	counts = new Array(nItems);
	
	prioritize(0);
}

function tiebreak(item)
{
	var ordered;
	var splitResult;
	var loc;
	var i;
	var tbpopup;
	var optstr;
	var w;
	var h;
	var l;
	var t;
	var tblist;
	
	// parse the data in the selected row; if there is only one item, there is no tie, so return
	ordered = document.getElementById(item.id);
	splitResult = ordered.value.split("\n");
	if (splitResult.length < 2)
		return;
	
	// build the popup URL and parm string
	loc = "prioritizing_grid_tb.htm" + "?items=" + splitResult.length + "&";
	for (i = 0; i < splitResult.length; i++)
		loc = loc + "?item" + (i+1) + "=" + splitResult[i] + "&";
	
	// build the option string
	optstr = "toolbar=0, directories=0, location=0, status=0, scrollbars=0, menubar=0, resizable=1";
	h = 200 + ((splitResult.length) * 52);
	w = 300 + ((splitResult.length + 3) * 52);
	l = (window.screen.width/2) - (w/2);
	t = (window.screen.height/2) - (h/2);
	optstr = optstr + ", width=" + w + ", height=" + h + ", left=" + l + ", top=" + t;
	
	// call the popup window to create the tiebreak grid
	tbpopup = window.open(loc, "tbpopup", optstr);
}

function acceptTiebreak()
{
	var i;
	var ordered;
	var splitResult;
	var neworder;
			
	neworder = "";
	
	// make sure the tiebreak hasn't created a tie
	for (i = 0; i < nItems; i++)
	{
		ordered = document.getElementById("o" + (i+1));
		splitResult = ordered.value.split("\n");		
		if (splitResult.length != 1)
		{
			alert("Invalid tiebreak");
			return;
		}
		neworder = neworder + ordered.value;
		if (i != (nItems - 1))
			neworder = neworder + "\n";
	}
			
	// save the data in the parent window
	window.opener.document.tbresult.tbstrings.value = neworder;
	window.opener.adjustRanking();
	
	// close the popup
	self.close();
}

function highlight(n, p1, p2)
{
	var p;
	var buttonA;
	var buttonB;
	var rcell;
	
	buttonA = document.getElementById("r" + n + "a");
	buttonB = document.getElementById("r" + n + "b");
	
//	rcell = document.getElementById("rcell" + n);
//	recell.style.backgroundColor = "#FFFF33";

	if (buttonA.checked)
	{
		// Least favored
		p = document.getElementById("p" + p1);
		p.style.backgroundColor = "#CCCCCC";
		p.style.fontWeight = "bold";
		p.style.color = "#336699";

		// Most favored
		p = document.getElementById("p" + p2);
		p.style.backgroundColor = "#336699";
		p.style.fontWeight = "bold";
		p.style.color = "#FFFFFF";
	}
	else if (buttonB.checked)
	{
		p = document.getElementById("p" + p1);
		p.style.backgroundColor = "#336699";
		p.style.fontWeight = "bold";
		p.style.color = "#FFFFFF";

		p = document.getElementById("p" + p2);
		p.style.backgroundColor = "#CCCCCC";
		p.style.fontWeight = "bold";
		p.style.color = "#336699";
	}
	else
	{
		p = document.getElementById("p" + p1);
		p.style.backgroundColor = "#CCCCCC";
		p.style.fontWeight = "bold";
		p.style.color = "#336699";

		p = document.getElementById("p" + p2);
		p.style.backgroundColor = "#CCCCCC";
		p.style.fontWeight = "bold";
		p.style.color = "#336699";
	}
}

function unhighlight(p1, p2)
{
	var p;

	p = document.getElementById("p" + p1);
	p.style.backgroundColor = "#FFFFFF";
	p.style.fontWeight = "normal";
	p.style.color = "#336699";

	p = document.getElementById("p" + p2);
	p.style.backgroundColor = "#FFFFFF";
	p.style.fontWeight = "normal";
	p.style.color = "#336699";
}

function prioritize(redo)
{
	var radioa;
	var radiob;
	var possibility;
	var ordered;
	var i;
	var j;
	var toMove;
	var moveCount;
	var moveval;
	var doNumber;
	var slot;
	var ord;
	var eord;
	var buttonA;
	var buttonB;
	var rcell;

	// parse input parameter for number of items
	nItems = setNumberOfItems();
	
	// insure all boxes have been checked
	for (i = 0; i < nButtons; i++)
	{
		buttonA = document.getElementById("r" + (i+1) + "a");
		buttonB = document.getElementById("r" + (i+1) + "b");
		rcell = document.getElementById("rcell" + (i+1));
		rcell.style.backgroundColor = "#FFFF33";
	    if (!buttonA.checked && !buttonB.checked)
		{
			rcell = document.getElementById("rcell" + (i+1));
			rcell.style.backgroundColor = "#FF9999";
			alert("You must select one of the two items in the highlighted box.");
			return;
		}
	}

	// clear the ordered list
	for (i = 0; i < nItems; i++)
	{
		ordered = document.getElementById("o" + (i+1));
		ordered.value = "";
		ordered.style.backgroundColor = "#FFFFFF";
	}

	// if we find a blank entry in the uprioritized list it means the end of the list; adjust nItems & nButtons accordingly
	for (i = 0; i < nItems; i++)
	{
		possibility = document.getElementById("p" + (i + 1));
		if (possibility.value == "")
		{
			nItems = i;
			for (nButtons = 0, j = 1; j < nItems; j++)
				nButtons += j;
			break;
		}
	}

	// create the array of items to be moved to the prioritized list
	toMove = new Array(nItems);
	for (i = 0; i < nItems; i++)
		toMove[i] = 0;
		
	if (redo == 0)
	{
		// if this is not a redo, clear the array of counts and loop through the radio buttons, adding 1 to the appropriate hit count for each one checked
		for (i = 0; i < nItems; i++)
			counts[i] = 0;
			
		for (i = 0; i < nButtons; i++)
		{
			radioa=document.getElementById("r" + (i+1) + "a");
			radiob=document.getElementById("r" + (i+1) + "b");
			if (radioa.checked == true)
				counts[radioa.value - 1] += 10; // 1.0 must be expressed as 10 because of Javascript's handling of floating point numbers
			else				
				counts[radiob.value - 1] += 10;
		}
	}
		
	// build the ordered list
	for (doNumber = (nItems * 10) + 9, slot = 0; doNumber >= 0; doNumber--)
	{
		// find the number we're looking for in the array of hit counts and save a list of its instances in the toMove array
		for (i = 0, j = 0, moveCount = 0; i < nItems; i++)
		{
			if (counts[i] == doNumber)
			{
				toMove[j] = i;
				j++;
				moveCount = j;
			}
		}
		// We didn't find doNumber, so look for the next one down
		if (moveCount == 0)
			continue;
	
		// fill the next slot in the ordered list with one or more items from the unordered list
		slot++;
		for (i = 0; i < moveCount; i++)
		{
			possibility = document.getElementById("p" + (toMove[i] + 1));

			ordered = document.getElementById("o" + slot);
			ordered.style.backgroundColor = "#FFFFFF";
			ordered.style.fontWeight = "normal";
			
			moveval = possibility.value;
			
			if (ordered.value == "")
			{
				ordered.value = moveval;
			}
			else
			{
				ordered.value = ordered.value + "\n" + moveval;
				ordered.style.backgroundColor = "#FF9999";
				ordered.style.fontWeight = "bold";
			}
		}
	}	
	
	// prepare the ordered list for emailing to a client
	for (i = 0; i < nItems; i++)
	{
		ord = document.getElementById("o" + (i+1));
		eord = document.getElementById("eo" + (i+1));		
		eord.value = ord.value;
	}	
}

function moveup(idx)
{	
	if (counts[idx - 1] == ((idx * 10) + 9))
		return;
	counts[idx - 1] += 1;
	prioritize(1);
}

function desserts()
{
	var radioa;
	var radiob;
	
	for (var i = 0; i < 45; i++)
	{
		radioa=document.getElementById("r" + (i+1) + "a");
		radiob=document.getElementById("r" + (i+1) + "b");
		radioa.checked = false;
		radiob.checked = false;
	}
	
	document.grid.p1.value = "Apple pie";
	document.grid.p2.value = "Biscotti";
	document.grid.p3.value = "Brownies";
	document.grid.p4.value = "Cheesecake";
	document.grid.p5.value = "Fresh fruit";
	document.grid.p6.value = "Ice cream";
	document.grid.p7.value = "Key lime pie";
	document.grid.p8.value = "Sorbet";
	document.grid.p9.value = "Strawberry shortcake";
	document.grid.p10.value = "Tiramisu";
	
	document.orderedlist.p1.value = "";
	document.orderedlist.p2.value = "";
	document.orderedlist.p3.value = "";
	document.orderedlist.p4.value = "";
	document.orderedlist.p5.value = "";
	document.orderedlist.p6.value = "";
	document.orderedlist.p7.value = "";
	document.orderedlist.p8.value = "";
	document.orderedlist.p9.value = "";
	document.orderedlist.p10.value = "";
}

function workvalues()
{
	for (var i = 0; i < 45; i++)
	{
		radioa=document.getElementById("r" + (i+1) + "a");
		radiob=document.getElementById("r" + (i+1) + "b");
		radioa.checked = false;
		radiob.checked = false;
	}

	document.grid.p1.value = "Work as an activity";
	document.grid.p2.value = "Work as a community";
	document.grid.p3.value = "Work as competence";
	document.grid.p4.value = "Work as competition";
	document.grid.p5.value = "Work as a contribution";
	document.grid.p6.value = "Work as a home base";
	document.grid.p7.value = "Work as income";
	document.grid.p8.value = "Work as pleasure";
	document.grid.p9.value = "Work as self-actualization";
	document.grid.p10.value = "Work as structure";

	document.orderedlist.p1.value = "";
	document.orderedlist.p2.value = "";
	document.orderedlist.p3.value = "";
	document.orderedlist.p4.value = "";
	document.orderedlist.p5.value = "";
	document.orderedlist.p6.value = "";
	document.orderedlist.p7.value = "";
	document.orderedlist.p8.value = "";
	document.orderedlist.p9.value = "";
	document.orderedlist.p10.value = "";
}

function clearForm()
{
	var i;
	var ordered;
	var unordered;
	
	for (i = 0; i < nButtons; i++)
	{
		radioa=document.getElementById("r" + (i+1) + "a");
		radiob=document.getElementById("r" + (i+1) + "b");
		radioa.checked = false;
		radiob.checked = false;
	}

	for (i = 0; i < nItems; i++)
	{
		unordered = document.getElementById("p" + (i+1));
		unordered.value = "";
		unordered.style.backgroundColor = "#FFFFFF";
	}

	for (i = 0; i < nItems; i++)
	{
		ordered = document.getElementById("o" + (i+1));
		ordered.value = "";
		ordered.style.backgroundColor = "#FFFFFF";
	}

	for (i = 0; i < nItems; i++)
		counts[i] = 0;
}

function expand(thistag, tag)
{
	styleObj=document.getElementById(thistag).style;
	if (styleObj.display=="none")
	{
		styleObj.display="";
		tag.innerHTML = "PRIORITIZING GRID INSTRUCTIONS (click to hide)";
	}
	else 
	{
		styleObj.display="none";
		tag.innerHTML = "PRIORITIZING GRID INSTRUCTIONS (click to show)";
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
