var updateInProgress = false;
var rootDivs = null;
var divIndex = -1;

var ajaxReq = null;
var ajaxDoc = null;
var ajaxDiv = null;

// Process the different pollmini div tags
function processAjax()
{
	if( ( ajaxReq.readyState == 4 ) && ( ajaxReq.status == 200 ) )
	{
		ajaxDoc = ajaxReq.responseText;
		ajaxDiv.innerHTML = ajaxDoc;
		divIndex += 1;
		processPollMini();
	}
} // processAjax()

// find the information about the pollmini (the URL)
function processPollMini()
{
	if( divIndex < rootDivs.length )
	{
		try
		{
			ajaxDiv = rootDivs[divIndex].getElementsByTagName('div')[0];
			var pollMiniFrm = rootDivs[divIndex].getElementsByTagName('form')[0];
			var pollminiURL = pollMiniFrm.url.value;
			ajaxDiv.innerHTML = '<img src="/images/ajax-loader.gif" border="0" alt="Loading, please wait..." />';
			ajaxReq = createRequestObject();
			ajaxReq.onreadystatechange = processAjax;
			ajaxReq.open("GET", pollminiURL, true);
			ajaxReq.send(null);
		}
		catch(e)
		{
			alert(e);
		}
	}
	else
	{
		updateInProgress = false;
	}

} // processPollMini()


// update all pollmini tags
function updatePollMini()
{
	if( !updateInProgress )
	{
		updateInProgress = true;
		rootDivs = document.getElementsByName("pollMini");
		if( rootDivs.length == 0 )
		{
			rootDivs = new Array;
			var divs = document.getElementsByTagName('div');
			for( var i = 0; i < divs.length; i++ )
			{
				if( divs[i].name == "pollMini" )
				{
					rootDivs.push(divs[i]);
				}
			}
		}

		if( rootDivs.length > 0 )
		{
			divIndex = 0;
			bHasPoll = true;
			processPollMini();
		}
		else
		{
			updateInProgress = false;
		}
	}
} // updatePollMini()


// when submitting a pollmini form
function processAjaxPollResult()
{
	if( ajaxReq.readyState == 4 )
	{
		ajaxDoc = ajaxReq.responseText;
		ajaxDiv.innerHTML = ajaxDoc;
	}
} // processAjaxPollResult()

// post the vote to the dll
function processPollMiniResult( aURL, aDivTag, parameters )
{
	try
	{
		ajaxDiv = aDivTag;
		ajaxReq = createRequestObject();
		ajaxReq.onreadystatechange = processAjaxPollResult;
		ajaxReq.open("POST", aURL, true);
		ajaxReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxReq.setRequestHeader("Content-length", parameters.length);
		ajaxReq.send(parameters);
	}
	catch(e)
	{
		alert(e);
	}
}  // processPollMiniResult()

// Check the pollmini form before sending the information to the dll
function checkForm(aFormId, aDivTagId)
{
	formErrors = true;
	aForm = document.getElementById(aFormId);
	aDivTagToUpdate = document.getElementById(aDivTagId);
	for( i = aForm.q1.length-1; i > -1; i--)
	{
		if(aForm.q1[i].checked)
		{
			formErrors = false;
			break;
		}
  	}

	if( formErrors )
	{
		alert("You must choose an answer");
	}
	else
	{
		var getstr = '';

		for( i=0; i < aForm.elements.length; i++ )
		{
			if( aForm.elements[i].tagName == "INPUT" )
			{
				if( aForm.elements[i].type == "text" )
				{
					getstr += aForm.elements[i].name + "=" + aForm.elements[i].value + "&";
				}

				if (aForm.elements[i].type == "checkbox" )
				{
					if (aForm.elements[i].checked )
					{
						getstr += aForm.elements[i].name + "=" + aForm.elements[i].value + "&";
					}
					else
					{
						getstr += aForm.elements[i].name + "=&";
					}
				}

				if( aForm.elements[i].type == "radio" )
				{
					if( aForm.elements[i].checked)
					{
						getstr += aForm.elements[i].name + "=" + aForm.elements[i].value + "&";
					}
				}
			}

			if( aForm.elements[i].tagName == "SELECT" )
			{
				var sel = aForm.elements[i];
				getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
			}

			if( aForm.elements[i].type == "hidden" )
			{
				getstr += aForm.elements[i].name + "=" + aForm.elements[i].value + "&";
			}
		}

		var postUrlValue = aForm.PostUrl.value;
		aDivTagToUpdate.innerHTML  = '<img src="/images/ajax-loader.gif" border="0" alt="Loading, please wait..." />';

		// call the dll to show the pollmini graph
		processPollMiniResult(postUrlValue, aDivTagToUpdate, getstr);
	}
}  // checkForm


//add to the body 'onload' execution chain
globalOnloads.push( updatePollMini );

