//AJAX FUNCTIONS

// Returns a Browser Specific AJAX calling object
function getAjaxObject()
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

//END AJAX FUNCTIONS

function showHideDiv(div)
{
	var imageId = 'img_'+div.id;
	if (div.style.display != 'block')
	{
		div.style.display = 'block';
		document.getElementById(imageId).src = './images/collapse.png';
	}
	else
	{
		div.style.display = 'none';
		document.getElementById(imageId).src = './images/expand.png';
	}
}

// Sets the URL of the full screen link to the current view in the message board.
function GetIFrameUrl()
{
    if (!document.getElementById)
    {
        return;
    }

    var link = document.getElementById("board_link");

    if (link == null)
    {
       return;
    }

    link.href = frames["message_board"].location.href
}

function submitVote(pollId)
{
	// Validate submission
	var voted = RadioCheck();
	if (voted == false)
	{
		alert('You must select an option to submit a vote! (DO somethin.)');
		return;
	}

	var resultsDiv = document.getElementById('poll');
	resultsDiv.innerHTML = '<img src="./images/poll.jpg" /><br/>';
	var xmlHttp = getAjaxObject(); // Does the posting
	var params = 'ajax=1&operation=submitVote&pollId='+pollId+'&voteOption='+voted;
	xmlHttp.open('POST','index.php',true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	xmlHttp.onreadystatechange = function() //On state change, set the resultsDiv
	{
		if (xmlHttp.readyState == 4)
		{
		  	 if (xmlHttp.status == 200)
		  	 {
			 	resultsDiv.innerHTML += xmlHttp.responseText;
		  	 }
		  	 else
		  	 {
		  	 	resultsDiv.innerHTML = '<em>A problem occurred while attempting to deactivate the poll. Try again later.</em>';
		  	 }
		}
	}
}

function RadioCheck()
{
	var selection = document.vote.pollselect;

	for (i=0; i<selection.length; i++)
	{
  		if (selection[i].checked == true)
  		{
  			return selection[i].value;
  		}
	}
	return false;
}


function showResults(pollId)
{
	var resultsDiv = document.getElementById('poll');
	resultsDiv.innerHTML = '<img src="./images/poll.jpg" /><br/>';
	var xmlHttp = getAjaxObject(); // Does the posting
	var params = 'ajax=1&operation=showResults&pollId='+pollId;
	xmlHttp.open('POST','index.php',true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	xmlHttp.onreadystatechange = function() //On state change, set the resultsDiv
	{
		if (xmlHttp.readyState == 4)
		{
		  	 if (xmlHttp.status == 200)
		  	 {
			 	resultsDiv.innerHTML += xmlHttp.responseText;
		  	 }
		  	 else
		  	 {
		  	 	resultsDiv.innerHTML = '<em>A problem occurred while attempting to deactivate the poll. Try again later.</em>';
		  	 }
		}
	}
}

function showPollVotingForm()
{
	var resultsDiv = document.getElementById('poll');
	resultsDiv.innerHTML = '<img src="./images/poll.jpg" /><br/>';
	var xmlHttp = getAjaxObject(); // Does the posting
	var params = 'ajax=1&operation=resetPoll';
	xmlHttp.open('POST','index.php',true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	xmlHttp.onreadystatechange = function() //On state change, set the resultsDiv
	{
		if (xmlHttp.readyState == 4)
		{
		  	 if (xmlHttp.status == 200)
		  	 {
			 	resultsDiv.innerHTML += xmlHttp.responseText;
		  	 }
		  	 else
		  	 {
		  	 	resultsDiv.innerHTML = '<em>A problem occurred while attempting to show the poll. Try again later.</em>';
		  	 }
		}
	}
}

function resizeFrame(f) {
//f.style.height= '1px';
f.style.height = (f.contentWindow.document.body.scrollHeight + 10) + "px";
}