
var lastDiv = "";
function showDiv(divName) {
	// hide last div
	if (lastDiv) 
	{
		document.getElementById(lastDiv).className = "hiddenDiv";
	}
	//if value of the box is not nothing and an object with that name exists, then change the class
	if (divName && document.getElementById(divName)) 
	{
		document.getElementById(divName).className = "visibleDiv";
		lastDiv = divName;
	}
}

function submitPollForm(form )
{
	i = getCheckedRadioButton(form.answer);
    if (i < 0)
	{
		alert('Sorry, you forgot to vote!');
		form.elements["voteaction"].value = 'ShowQuestion';
		return false;
	}
	
	//form.elements["voteaction"].value = 'Vote';	
    //form.submit();
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	var url="/devenv/dept/displaypoll.asp";
	url=url+"?voteaction=Vote&pollid="+form.pollid.value+"&answer="+form.answer[i].value+"&sid="+Math.random();	
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("homepolldiv").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function validateAddPollForm(form)
{
	if (isBlank(form.question.value))
	{
		alert("Please enter your poll question.");
		form.question.focus();
		return false;
   }

	if (isBlank(form.answer1.value))
	{
		alert("Please enter your poll answer 1.");
		form.answer1.focus();
		return false;
   }

	if (isBlank(form.answer2.value))
	{
		alert("Please enter your poll answer 2.");
		form.answer2.focus();
		return false;
   }
   
	return true;   
}

function validateUpdatePollForm(form)
{
	if (isBlank(form.count1.value))
		form.count1.value = "0";	
	if (!isPositiveInteger(form.count1.value))
	{
		alert("Please enter a valid vote count.");
		form.count1.focus();
		return false;
   }

	if (isBlank(form.count2.value))
		form.count2.value = "0";	
	if (!isPositiveInteger(form.count2.value))
	{
		alert("Please enter a valid vote count.");
		form.count2.focus();
		return false;
   }
   
	if (isBlank(form.count3.value))
		form.count3.value = "0";	
	if (!isPositiveInteger(form.count3.value))
	{
		alert("Please enter a valid vote count.");
		form.count3.focus();
		return false;
   }
   
	if (isBlank(form.count4.value))
		form.count4.value = "0";	
	if (!isPositiveInteger(form.count4.value))
	{
		alert("Please enter a valid vote count.");
		form.count4.focus();
		return false;
   }
   
	return true;   
}

function validateManagePollForm(form)
{	
	i = getCheckedRadioButton(form.pollid)
	if ( i<0)
	{
		alert("Please select a poll first.");		
		return false;
	}
	pollid = form.pollid[i].value;
	pollstatus = form.elements["pollstatus"+pollid].value;
	pollaction = form.elements["action"].value;
	//alert("You select poll: " + pollid + " status: " + pollstatus + " action: " + pollaction);
	if (pollaction.toLowerCase()=="archive" && pollstatus.toLowerCase()=="archived")
	{
		alert("This poll is already archived!");
		return false;
	}
	else if (pollaction.toLowerCase()=="publish" && pollstatus.toLowerCase()=="published")
	{
		alert("This poll is already published!");
		return false;
	}
	else if (pollaction.toLowerCase()=="preview")
	{
		window.open ("http://www.stonybrookphysicians.com/default.asp?pollid="+pollid,"mywindow");
		return false;
	}
	
	return true;
}

function submitManagePollForm(form, action)
{
	form.elements["action"].value = action;
	//if (form.onsubmit())
	if (validateManagePollForm(form))
		form.submit();
}
