/*****************************/
/* Global Variables
/*****************************/

var xmlDoc; // holds the xml object
var questionCollection = new Array(); // An array all of the question objects
var minLinksDisplayed = 4; // This minimum amount of links to be displayed
var formPage = "/dtcf/pages/11_Personalize.htm";
var resultsPage = "/dtcf/pages/11_Results.htm";
var contentFile = "/xml/personalize.xml";

/*****************************/
/* This function opens the XML file, reads it, and converts the questions into objects.
/*****************************/
function loadXML()
{
	// Code for IE
	if (window.ActiveXObject){
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	// Code for everything else
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc=document.implementation.createDocument("","",null);
	}

	xmlDoc.async = false;
	xmlDoc.load(contentFile);
}

/*****************************/
/* This function loads the questions from the xml document object into the question array.
/*****************************/
function loadQuestions()
{
	var questionList = xmlDoc.getElementsByTagName("question"); // get the xml list of question to traverse through

	// Since all questions have the same structure in the XML file we don't have to pay attention to
	// what TYPE of question is being parsed
	
	for (var i=0; i < questionList.length; i++) // Traverse through the xml list of questions
	{
		var thisQuestion = questionList[i];
		//document.write(i + " " + thisQuestion.childNodes[0].childNodes[0].nodeValue);
		var type = thisQuestion.childNodes[0].childNodes[0].nodeValue; // set the question type to a variable
		var text = thisQuestion.childNodes[1].childNodes[0].nodeValue; // set the question text to a variable
		
		var ansList = thisQuestion.getElementsByTagName("answer"); // assign questions answers to a variable
		var ansArray = new Array(); // create array to hold question answers

		var linksArray = new Array();
		var linksArrayCounter = 0;
		
		for (var j=0;j<thisQuestion.childNodes[2].childNodes.length;j++) // traverse the question answers
		{
			ansArray[j] = ansList[j].childNodes[0].childNodes[0].nodeValue; // set the question answers to an array variable
			
			var linksList = ansList[j].getElementsByTagName("linkList"); // set this variable to hold the linkList nodes
			
			for (var a=0;a<linksList[0].childNodes.length;a++) // traverse the nodes in the link list
			{
				// Create the values for the link object
				var page = linksList[0].childNodes[a].childNodes[0].childNodes[0].nodeValue;
				var title = linksList[0].childNodes[a].childNodes[1].childNodes[0].nodeValue;
				var linkText = linksList[0].childNodes[a].childNodes[2].childNodes[0].nodeValue;
				var category = linksList[0].childNodes[a].childNodes[3].childNodes[0].nodeValue;
				var weight = linksList[0].childNodes[a].childNodes[4].childNodes[0].nodeValue;
				var ans = j; // This value represents the answer this link applies to   !!!!!!Very Important!!!!!
				
				var newLink = new link(page, category, weight, linkText, title, ans); // Create a new link object with the above values
				
				//document.write(linksList[0].childNodes[a].childNodes.length + "<br />");
				
				if (linksList[0].childNodes[a].childNodes.length == 6)
				{
					var img = linksList[0].childNodes[a].childNodes[5].childNodes[0].nodeValue;
					newLink.img = img;
				}
				
				linksArray[linksArrayCounter] = newLink; //Add a new link object to this temporary array
				linksArrayCounter++; // Increment the array counter
			}
		}

		var newQuestion = new question(type, text, ansArray, linksArray); // create a new question object
		
		questionCollection[i] = newQuestion; // store the question in the overall question array
	}
}

/*****************************/
/* This function will iterate through the array of question objects
/* and display them one at a time.
/* Radio button questions are display different than Checkbox questions
/*****************************/
function displayForm()
{
	
	var tab = 1;
	
	for (var i=0;i<questionCollection.length;i++)
	{
		document.write("<form action='" + resultsPage + "' method='get' name='personform'>"); // the first line of the form, submits to a sepearte page that handles the results
		
		if (i>0)  // add extra line breaks for questions after the first
		{
			document.write("<br/><br/>");
		}
		
		if (questionCollection[i].type=="radio") // Outputs the radio button questions to the webpage
		{
			// Output the label text for the radio question
			document.write("<label class='personalizelabel'>" + (i+1) + ". " + questionCollection[i].text + "</label><br/>");
			
			for( var j=0; j<questionCollection[i].ansList.length;j++) // Use for loop to traverse through the answer list
			{
				// Output the radio buttons and associated labels to the webpage
				document.write("<input type='radio' id='" + (i+1) + "."+ j +"' name='q" + i + "' value='" + i + "." +j + "' tabindex='" + tab + "'><label class='personalizeradio' for='" + (i+1) +"." + j + "'>" + questionCollection[i].ansList[j] + "</label>");
				tab++;
			}
		}
		
		if (questionCollection[i].type=="checkbox") // Outputs the check box questions to the webpage
		{
			// Output the label text for the check box question
			document.write("<label class='personalizelabel'>" + (i+1) + ". " + questionCollection[i].text + "</label><br/>");
			
			for( var j=0; j<questionCollection[i].ansList.length;j++) // Use for loop to traverse through the answer list
			{
				// Output the check box answers and associated labels to the webpage
				document.write("<input type='checkbox' id='" + (i+1) + "." + j + "' name='q" + i + "' value='" + i + "." +j + "' tabindex='" + tab + "'><label class='personalizecheck' for='" + (i+1) + "." + j + "'>" + questionCollection[i].ansList[j] + "</label><br/>");
				tab++;
			}
		}
		
		if (questionCollection[i].type=="dropdown") // Outputs the drop down list to the webpage
		{
			// Output the associated label for the drop down list to the web page
			document.write("<label class='personalizelabel' for='" + (i+1) + "'>" + (i+1) + ". " + questionCollection[i].text + "</label><br/>");
			// Create the opening drop down list tag
			document.write("<select id='" + (i+1) + "." + j + "' name='q" + i  + "' tabindex='" + tab + "'>");
			tab++;
			
			for( var j=0; j<questionCollection[i].ansList.length;j++)
			{
				// Output the values of the drop down list to the web page
				document.write("<option value='" + i +"." +j + "'>" + questionCollection[i].ansList[j] + "</option>");
			}
			document.write("</select>"); // Close the drop down list tag
		}
		
	}
	// Create the submit button for the form
	document.write("<br/><br/><input type='button' value='Personalize My Content Now' class='personalizesubmit' onclick=\"submitForm()\" tabindex='" + tab + "'/>");
	document.write("</form>"); // Close the form tag
}


/*****************************/
/* This function will submit the form.  It will first check the form to make sure at least one question is answered.
/* If at least one question is answered the form will submit.
/* If no questions are answered a popup message will appear informing the user.
/*****************************/
function submitForm()
{
	var elements = document.forms["personform"].elements.length - 1;
	
	var willSubmit;
	
	for (var i=0;i<elements;i++)
	{
		if (document.forms["personform"].elements[i].checked)
		{
			willSubmit = true;
		}
	}
	
	if (willSubmit)
	{
		document.forms["personform"].submit();
	}
	else{
		alert("Please answer at least one question.");
	}
}


/*****************************/
/* This function accepts a url and redirects the current window to that url.
/* @param url - the url the window will be redirected to
/*****************************/
function redirect(url)
{
	window.location = url;
}


/*****************************/
/* This function checks for a cookie.  If there is a valid cookie, it returns a query
/* containing answer values.  If not it returns false.
/*****************************/
function cookieLoad()
{
	var cookie = document.cookie;
	
	var pos = cookie.indexOf("pecookie=");
	
	if (pos!=-1)
	{
		var start = pos + 9;
		var value = cookie.substring(start);
		return value;
	}
	
	return false;
}


/*****************************/
/* This function checks for a cookie.  If one exists and has values, it returns true.
/* Otherwise it returns false.
/*****************************/
function cookieCheck()
{
	var cookie = document.cookie;
	
	var pos = cookie.indexOf("pecookie=");
	
	if (pos == "-1")
	{
		return false;
	}
	
	var start = pos+9;
	var value = cookie.substring(start);
	
	if (value != "")
	{
		return true
	}
	else{
		return false;
	}
	
}


/*****************************/
/* This function creates a cookie and stores the query string in it.
/* @param query - the string of answers the user selected from the form
/*****************************/
function cookieCreate(query)
{
	var cookie;
	
	var name = "pecookie=";
	var value = query + "; ";
	var daysToLive = 5;
	var maxage = "max-age=" + (daysToLive*24*60*60) +  "; ";
	
	cookie = name + value + maxage;
	
	document.cookie = cookie;
}


/*****************************/
/* This function deletes the values in the cookie for this personalization engine.
/*****************************/
function cookieDelete()
{
	var cookie;
	
	var name = "pecookie="
	var value =" ;";
	var daysToLive = -1;
	var maxage = "max-age=" + (daysToLive*24*60*60) +  "; ";
	
	cookie = name + value + maxage;
	
	document.cookie = cookie;
}


/*****************************/
/* This function will delete the cookie values, and then reload the personalization form.
/*****************************/
function reloadForm()
{
	cookieDelete();
	redirect(formPage);
}


/*****************************/
/* This function will check the URL to see if there are argument values in it from a form get.
/* If there are arguments, the function will return true;
/* If there are no arguments, the function will return false;
/*****************************/
function checkURL()
{
	var query = location.search.substring(1);
	
	if (query == "")
	{
		return false;
	}
	else{
		return true;
	}
}


/*****************************/
/* This function parses the URL in the browser and returns the query string (the answers the user selected).
/* @return query - the answers the user selected.
/*****************************/
function parseURL()
{
	var query = location.search.substring(1);
	
	return query;
}


/*****************************/
/* This function accepts a query string and parses the values in it.
/* It then returns an array of those values;
/* @param query - the query string of values
/* @return answerList - the array of values
/*****************************/
function parseArgs(query)
{
	var answerList = new Array();
	if (query == "false")
	{
		reloadForm();
		return null;
	}
	
	if (query == "")
	{
		// Call redirect function?
		reloadForm();
		return null;
	}
	
	var pairs = query.split("&");
	
	for (var i = 0; i<pairs.length; i++)
	{
		var pos = pairs[i].indexOf('=');
		answerList[i] = pairs[i].substring(pos+1);
		var pos2 = answerList[i].indexOf('.');
	}
	
	return answerList;
}

/*****************************/
/* This function will calculate the links to be displayed.
/* This is the core of the application.  Here the links are identified, weighed, and then sorted.
/* @param answerList an array of the answers selected by the user
/* @return 
/*****************************/
function calcLinks(answerList)
{
	// using the questionCollection array...traverse through, finding the links, then obtain the category and weight if there is one
	// use parallel arrays to store link names and weights
	// 2 parallel arrays for each category
	// when adding a link to an array, check to see if it's there first, if it is, do not add, but add to weight in parallel array
	
	// reorder links according to weight
	
	// create totalLinks object with order list of links
	
	// return totalLinks object
	
	var unweighedList = new Array();
	var counter1 = 0;
	var category1 = new Array();
	var cat1counter = 0;
	var category2 = new Array();
	var cat2counter = 0;
	var category3 = new Array();
	var cat3counter = 0;
	var category4 = new Array();
	var cat4counter = 0;
	
	// The answerList array contains the list of answers the user chose on the form.
	// The current format of an answer is question#.answer#, example: 0.2
	// Numbering of both questions and answers begin at zero.
	// The question# and answer# are seperated in the next set of steps.
	// These numbers are used to quickly locate the appropriate links associated to this specific question/answer combination
	
	for( var x=0;x<answerList.length; x++) // Begin adding link objects to the unweighed complete list
	{
		var question = answerList[x].substring(0,1); // get the question number from this answer value
		var pos = answerList[x].indexOf('.');
		var answer = answerList[x].substring(pos+1); // get the answer number from this answer value
		
		for (var y=0;y<questionCollection[question].linkList.length; y++) // Traverse through the question's list of links
		{
			if (questionCollection[question].linkList[y].answer == answer) // If a link in this question is attached to this answer
			{
				unweighedList[counter1] = questionCollection[question].linkList[y]; // Then add that link object to the unweighed complete list
				counter1++;
			}
		}
	}
	
	// We now have associated the user's answers with the appropriate links
	// Duplicate link objects will now be removed from the list.
	// At the same time, when a duplicate link object is found (based upon the actual URL of the link object) 
	// its weight value will be added to the first link object
	// The duplicate link object will then be removed from the array and the array is truncated
	
	for (var j=0;j<unweighedList.length;j++) // traverse through the complete list of unweighed link objects
	{
		for(var k=j+1;k<unweighedList.length;k++) 
		{
			if(unweighedList[j].page == unweighedList[k].page) // if two link objects have the same link
			{
				unweighedList[j].weight = parseFloat(unweighedList[j].weight) + parseFloat(unweighedList[k].weight); // add the weight of the second link object to the first
				
				// If the link object about to be removed contains an image link
				// then add that image link to the remaining link object
				// This is just a precaution. Link objects with the same url should either all contain image links, or not.
				if (unweighedList[k].img)
				{
					unweighedList[j].img = unweighedList[k].img;
				}
				
				unweighedList.splice(k, 1); // remove the second link object from the array
				k=k-1; // decrement the counter because an array item was just removed
			}
		}
	}
	
	
	// The link object array has now been removed of duplicate link objects and link weights have been totaled.
	// The links will now be split up into their seperate categories.
	// additional manipulation based upon weight can be added before the next step (removing links with weight < 1 for example)
	
	for(var i = 0;i<unweighedList.length;i++) // Traverse through the now weighed link object array
	{
		if (unweighedList[i].category == "category1")
		{
			category1[cat1counter] = unweighedList[i];
			cat1counter++;
		}
		
		if (unweighedList[i].category == "category2")
		{
			category2[cat2counter] = unweighedList[i];
			cat2counter++;
		}
		
		if (unweighedList[i].category == "category3")
		{
			category3[cat3counter] = unweighedList[i];
			cat3counter++;
		}
		
		if (unweighedList[i].category == "category4")
		{
			category4[cat4counter] = unweighedList[i];
			cat4counter++;
		}
	}
	
	// The link objects are now split into various arrays based upon their category
	// The link objects also have weight totals
	
	// The next step will be to order the links in the order of highest weight first in the array, to lowest weight last in the array
	// Doing this will make displaying the links much easier
	// A for loop is used to iterate through each category array
	
	for(var a=0;a<category1.length;a++)
	{
		for(var b=a+1;b<category1.length;b++)
		{
			if(parseFloat(category1[b].weight) > parseFloat(category1[a].weight)) // parseFloat is needed to convert to #s for the comparison
			{
				var temp1 = category1[a]; // Set the lower weight link to a temp variable
				category1[a] = category1[b]; // Set the higher weight link closer to the front of the array
				category1[b] = temp1; // Set the lower weight link
			}
		}
	}
	
	// Repeated for category 2 links
	for(var a=0;a<category2.length;a++)
	{
		for(var b=a+1;b<category2.length;b++)
		{
			if(category2[b].weight > category2[a].weight)
			{
				var temp1 = category2[a].weight;
				category2[a].weight = category2[b].weight;
				category2[b].weight = temp1;
			}
		}
	}
	
	// Repeated for category 3 links
	for(var a=0;a<category3.length;a++)
	{
		for(var b=a+1;b<category3.length;b++)
		{
			if(category3[b].weight > category3[a].weight)
			{
				var temp1 = category3[a].weight;
				category3[a].weight = category3[b].weight;
				category3[b].weight = temp1;
			}
		}
	}
	
	// Repeated for category 4 links
	for(var a=0;a<category4.length;a++)
	{
		for(var b=a+1;b<category4.length;b++)
		{
			if(category4[b].weight > category4[a].weight)
			{
				var temp1 = category4[a].weight;
				category4[a].weight = category4[b].weight;
				category4[b].weight = temp1;
			}
		}
	}
	
	// The arrays have now been sorted properly
	// We will place them into a totalLinks object and then return that object
	
	var finishedLinks = new totalLinks(category1, category2, category3, category4);
	
	return finishedLinks;
}

/*****************************/
/* This function will display the links, previously calculated, onto the web page.
/* @param totalLinks - the object that holds an array for each of the link categories
/*****************************/
function displayLinks(totalLinks)
{
	document.write("<h1 class='personh1'>Just For You</h1>");
	
	// *****************************************
	// CATEGORY 1 LINKS
	// This section will output the category 1 links
	// If there are category 1 links, write the header to the screen
	if (totalLinks.links1.length > 0)
	{
		document.write("<h2 class='personh2'>People Are Talking About</h2>");
	}
	
	// If there are more links that the minimum required, write the first links to the screen, and hide the rest in a div 
	if (totalLinks.links1.length > minLinksDisplayed)
	{			
		for (var i=0;i<minLinksDisplayed;i++) // Output links up to the minimum amount to be displayed
		{
			if (totalLinks.links1[i].img) // If the link object contains an image to be displayed
			{
				// Display image and text within a table for easier display control
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links1[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			// Outputs the link and intro text
			document.write("<a href='" + totalLinks.links1[i].page + "' class='perstitle' target='_self'>" + totalLinks.links1[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links1[i].text + " ...</span><br/>");
			
			// Closes up the table created for image display
			if (totalLinks.links1[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />"); // This div is a visual divider for links
		}
		
		// Create a hidden div that holds link results beyond the minimum
		document.write("<div class='show-hide'><a href=\"javascript:showHide('cat1');\" class='showhide'>Show/hide more results...</a><br/><br/></div><div id='cat1'>");
		
		for (var i=minLinksDisplayed;i<totalLinks.links1.length;i++) // Output the remaining links within this hidden div
		{
			if (totalLinks.links1[i].img)
			{
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links1[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			document.write("<a href='" + totalLinks.links1[i].page + "' class='perstitle' target='_self'>" + totalLinks.links1[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links1[i].text + " ...</span><br/>");
			
			if (totalLinks.links1[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />");
		}
		document.write("</div>"); // close the hidden results div
		
	}
	else // If there are less that the minimum amount of links required
	{
		for(var i =0;i<totalLinks.links1.length;i++) // Output those links normally
		{
			if (totalLinks.links1[i].img)
			{
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links1[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			document.write("<a href='" + totalLinks.links1[i].page + "' class='perstitle' target='_self'>" + totalLinks.links1[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links1[i].text + " ...</span><br/>");
			
			if (totalLinks.links1[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />");
		}
	}
	
	// *****************************************
	// CATEGORY 2 LINKS
	// This section outputs the category 2 links
	// If there are category 2 links, write the header to the screen
	if (totalLinks.links2.length > 0)
	{
		document.write("<h2 class='personh2'>Issues of Weight</h2>");
	}
	
	// If there are more links that the minimum required, write the first links to the screen, and hide the rest in a div 
	if (totalLinks.links2.length > minLinksDisplayed)
	{			
		for (var i=0;i<minLinksDisplayed;i++) // Output links up to the minimum amount to be displayed
		{
			if (totalLinks.links2[i].img) // If the link object contains an image to be displayed
			{
				// Display image and text within a table for easier display control
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links2[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			// Outputs the link and intro text
			document.write("<a href='" + totalLinks.links2[i].page + "' class='perstitle' target='_self'>" + totalLinks.links2[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links2[i].text + " ...</span><br/>");
			
			// Closes up the table created for image display
			if (totalLinks.links2[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />"); // This div is a visual divider for links
		}
		
		// Create a hidden div that holds link results beyond the minimum
		document.write("<div class='show-hide'><a href=\"javascript:showHide('cat2');\" class='showhide'>Show/hide more results...</a><br/><br/></div><div id='cat2'>");
		
		for (var i=minLinksDisplayed;i<totalLinks.links2.length;i++) // Output the remaining links within this hidden div
		{
			if (totalLinks.links2[i].img)
			{
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links2[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			document.write("<a href='" + totalLinks.links2[i].page + "' class='perstitle' target='_self'>" + totalLinks.links2[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links2[i].text + " ...</span><br/>");
			
			if (totalLinks.links2[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />");
		}
		document.write("</div>"); // close the hidden results div
		
	}
	else // If there are less that the minimum amount of links required
	{
		for(var i =0;i<totalLinks.links2.length;i++) // Output those links normally
		{
			if (totalLinks.links2[i].img)
			{
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links2[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			document.write("<a href='" + totalLinks.links2[i].page + "' class='perstitle' target='_self'>" + totalLinks.links2[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links2[i].text + " ...</span><br/>");
			
			if (totalLinks.links2[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />");
		}
	}

	// *****************************************
	// CATEGORY 3 LINKS
	// This section outputs the category 3 links
	// If there are category 3 links, write the header to the screen
	if (totalLinks.links3.length > 0)
	{
		document.write("<h2 class='personh2'>Bariatric Surgery and Its Effects on Weight-Related Health Conditions</h2>");
	}
	
	// If there are more links that the minimum required, write the first links to the screen, and hide the rest in a div 
	if (totalLinks.links3.length > minLinksDisplayed)
	{			
		for (var i=0;i<minLinksDisplayed;i++) // Output links up to the minimum amount to be displayed
		{
			if (totalLinks.links3[i].img) // If the link object contains an image to be displayed
			{
				// Display image and text within a table for easier display control
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links3[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			// Outputs the link and intro text
			document.write("<a href='" + totalLinks.links3[i].page + "' class='perstitle' target='_self'>" + totalLinks.links3[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links3[i].text + " ...</span><br/>");
			
			// Closes up the table created for image display
			if (totalLinks.links3[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />"); // This div is a visual divider for links
		}
		
		// Create a hidden div that holds link results beyond the minimum
		document.write("<div class='show-hide'><a href=\"javascript:showHide('cat3');\" class='showhide'>Show/hide more results...</a><br/><br/></div><div id='cat3'>");
		
		for (var i=minLinksDisplayed;i<totalLinks.links3.length;i++) // Output the remaining links within this hidden div
		{
			if (totalLinks.links3[i].img)
			{
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links3[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			document.write("<a href='" + totalLinks.links3[i].page + "' class='perstitle' target='_self'>" + totalLinks.links3[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links3[i].text + " ...</span><br/>");
			
			if (totalLinks.links3[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />");
		}
		document.write("</div>"); // close the hidden results div
		
	}
	else // If there are less that the minimum amount of links required
	{
		for(var i =0;i<totalLinks.links3.length;i++) // Output those links normally
		{
			if (totalLinks.links3[i].img)
			{
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links3[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			document.write("<a href='" + totalLinks.links3[i].page + "' class='perstitle' target='_self'>" + totalLinks.links3[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links3[i].text + " ...</span><br/>");
			
			if (totalLinks.links3[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />");
		}
	}
	
	// *****************************************
	// CATEGORY 4 LINKS
	// This section outputs the category 4 links
	// If there are category 4 links, write the header to the screen
	if (totalLinks.links4.length > 0)
	{
		document.write("<h2 class='personh2'>Bariatric Surgery: Overview of the Procedures</h2>");
	}
	
	// If there are more links that the minimum required, write the first links to the screen, and hide the rest in a div 
	if (totalLinks.links4.length > minLinksDisplayed)
	{			
		for (var i=0;i<minLinksDisplayed;i++) // Output links up to the minimum amount to be displayed
		{
			if (totalLinks.links4[i].img) // If the link object contains an image to be displayed
			{
				// Display image and text within a table for easier display control
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links4[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			// Outputs the link and intro text
			document.write("<a href='" + totalLinks.links4[i].page + "' class='perstitle' target='__self'>" + totalLinks.links4[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links4[i].text + " ...</span><br/>");
			
			// Closes up the table created for image display
			if (totalLinks.links4[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />"); // This div is a visual divider for links
		}
		
		// Create a hidden div that holds link results beyond the minimum
		document.write("<div class='show-hide'><a href=\"javascript:showHide('cat4');\" class='showhide'>Show/hide more results...</a><br/><br/></div><div id='cat4'>");
		
		for (var i=minLinksDisplayed;i<totalLinks.links4.length;i++) // Output the remaining links within this hidden div
		{
			if (totalLinks.links4[i].img)
			{
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links4[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			document.write("<a href='" + totalLinks.links4[i].page + "' class='perstitle' target='_self'>" + totalLinks.links4[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links4[i].text + " ...</span><br/>");
			
			if (totalLinks.links4[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />");
		}
		document.write("</div>"); // close the hidden results div
		
	}
	else // If there are less that the minimum amount of links required
	{
		for(var i =0;i<totalLinks.links4.length;i++) // Output those links normally
		{
			if (totalLinks.links4[i].img)
			{
				document.write("<table cellspacing='0' cellpadding='0' border='0'><tr valign='top'><td><img src='" + totalLinks.links4[i].img + "' class='persimg' alt=''></td><td width='100%'>");
			}
			
			document.write("<a href='" + totalLinks.links4[i].page + "' class='perstitle' target='_self'>" + totalLinks.links4[i].title + "</a><br/>");
			document.write("<span class='persontext'>" + totalLinks.links4[i].text + " ...</span><br/>");
			
			if (totalLinks.links4[i].img)
			{
				document.write("</td></tr></table>");
			}
			
			document.write("<br /><div class='pershr' ></div><br />");
		}
	}
	
	document.write("<input type='button' value='Personalize Again' onclick=\"reloadForm()\" class='pers-btn' />");
}

/*****************************/
/* This function is a simple show/hide function for the divs that will contain
/* extra links.
/*****************************/
function showHide(id)
{
	var div = document.getElementById(id);
	
	//div.style.display = "inline";
//	div.innerHTML = div.style.display;
	
	if(div.style.display == "inline")
	{
		div.style.display = "none";
	}
	else{
		div.style.display = "inline";
	}
}

/*****************************/
/* CLASSES
/*****************************/

/*****************************/
/* This class represents the various questions types.
/* @property type - the type of question (radio, checkbox, drop down list)
/* @property text - the text of the question
/* @property ansList - the text of the radio, checkboxes, or drop down list answers, parallel array to linkList
/* @property linkList - an array of link objects
/*****************************/
function question(type, text, ansList, linkList)
{
	this.type = type;
	this.text = text;
	this.ansList = ansList;
	this.linkList = linkList;
}

/*****************************/
/* This class represents an answer link
/* @property link - the URL of the link
/* @property category - the category the URL falls under
/* @property weight - the weight of the link object
/* @property text - the flavor text for this link
/* @property title - the title of the link (this will carry the hyperlink)
/* @property answer - the question/answer this link is associated with.  important for calculation
/*****************************/
function link(page, category, weight, text, title, answer)
{
	this.page = page;
	this.category = category;
	this.weight = weight;
	this.text = text;
	this.title = title;
	this.answer = answer;
}

/*****************************/
/* This class represents the entire collection of links to be displayed.
/* @property links1 - category 1 link object array
/* @property links2 - category 2 link object array
/* @property links3 - category 3 link object array
/* @property links4 - category 4 link object array
/*****************************/
function totalLinks(links1, links2, links3, links4)
{
	this.links1 = links1;
	this.links2 = links2;
	this.links3 = links3;
	this.links4 = links4;
}