var diggTitle;
var diggURL;

function load()
{
}

function gradeQuiz()
{
	var complete = true;

	var votes = new Array(files.length);
	for (var i=0; i<votes.length; i++)
		votes[i] = 0;

	for (var i=0; i<numQuestions; i++)
	{
		var x = key[i].length;
		var qcomplete = false;
		for (var j=0; j<x; j++)
		{
			var r = document.getElementById("q" + i + "_r" + j);
			if ( r.checked )
			{
				qcomplete = true;
				votes [ key[i][j] ]++;
				continue;
			}
		}
		
		var qt = document.getElementById("qt" + i);
		if ( qcomplete )
			qt.style.color = 'black';
		else
		{
			qt.style.color = 'red';
			complete = false;
		}
	}

	if ( !complete )
	{
		alert("You didn't answer all the questions.  Complete the ones in red.");
		return;
	}

	var maxVotes = 0;
	for (var i=0; i<votes.length; i++)
		if ( votes[i] > maxVotes ) 
			maxVotes = votes[i];


	var winners = new Array();
	for (var i=0; i<votes.length; i++)
		if ( votes[i] == maxVotes ) 
			winners.push(i);

	var choice = winners[ Math.floor(Math.random() * winners.length ) ];

	var y = "?np=" + votes.length;
	for (var i=0; i<votes.length; i++)
	{
		y += "&p" + i + "=" + votes[i];
	}

	document.location.href = files[choice] + y;
}


function gradeMCQuiz()
{
	var complete = true;
	var r;

	for (var i=0; i<numQuestions; i++)
	{
		var qcomplete = false;
		var x = document.forms["quiz"].elements["q" + i].length;
		for (var j=0; j<x; j++)
		{
			r = document.getElementById("q" + i + "_r" + j);
			if ( r.checked )
			{
				qcomplete = true;
				continue;
			}
		}
		
		var qt = document.getElementById("qt" + i);
		if ( qcomplete )
			qt.style.color = 'black';
		else
		{
			qt.style.color = 'red';
			complete = false;
		}
	}

	if ( !complete )
	{
		alert("You didn't answer all the questions.  Complete the ones in red.");
		return;
	}

	for (var i=0; i<numQuestions; i++)
	{
		var x = document.forms["quiz"].elements["q" + i].length;
		var answer = 0;
		for (var j=0; j<x; j++)
		{
			r = document.getElementById("q" + i + "_r" + j);
			if ( r.checked )
			{
				answer = j;
				break;
			}
		}

		r = document.getElementById("q" + i + "_a" + answers[i]);
		r.style.color = "blue";

		if ( answer == answers[i] )
		{

			r = document.getElementById("grade" + i);
			r.innerHTML = "Correct!";
			r.style.color = "green";

		}
		else
		{
			r = document.getElementById("grade" + i);
			r.innerHTML = "Incorrect!";
			r.style.color = "red";

		}
		

	}
}


function codeboxClicked(x)
{
	x.focus();
  	x.select();
}

/********* GENERAL FUNCTIONALITY ***************/

var http = createRequestObject();

function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}


function friendClicked()
{
	var x = document.getElementById("friend");
	if ( x.value == "friend's e-mail address" )
	{
		x.value = "";
		x.style.color = "#000000";
	}
}

function nameClicked()
{
	var x = document.getElementById("yourName");
	if ( x.value == "Your Name" )
	{
		x.value = "";
		x.style.color = "#000000";
	}
}

function bookmarkThis() 
{
	var url = document.location.href;
	var title = document.title;

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}

function getDiggURL()
{
	if ( diggURL == undefined )
		return document.location.href;
	else
		return diggURL;
}

function getDiggTitle()
{
	if ( diggTitle == undefined )
		return document.title;
	else
		return diggTitle;
}

function delicious() 
{
	var url = "http://del.icio.us/post?url=" + escape(document.location.href) + "&title=" + escape(document.title);
	document.location.href = url;
}

function reddit()
{
	var url = "http://reddit.com/submit?title=" + escape( getDiggTitle() ) + "&url=" + escape( getDiggURL() );
	document.location.href = url;
}

function digg()
{
	var url = "http://digg.com/submit?phase=2&url=" +  escape( getDiggURL() ) + "&title=" + escape( getDiggTitle() );
	document.location.href = url;
}

function sendEmail()
{
	var x = document.getElementById("friend").value;
	var y = document.getElementById("yourName").value;
	x = trim(x);
	y = trim(y);
	if ( x == "friend's e-mail address" || x == "" )
	{
		alert("Please enter your friend's e-mail address to mail the article to.");
		return;
	}
	if ( y == "Your Name" || y == "" )
	{
		alert("Please enter your name, so your friend will know who sent the article.")
		return;
	}

	url = "send-email.php?email=" + escape(x) + "&name=" + escape(y) + "&num=" + articleNum;
	
	alert( url );
	http.open("get", url );
	http.send(null);

	alert("Thank you! E-mail sent to " +  document.getElementById("friend").value );
}

function trim(s)
{
	s = s.replace(/^\s+/, '');
	return s.replace(/\s+$/, '');
}

function changecat()
{
	document.location.href = document.getElementById("catpulldown").value;
}

