// Polls.js

function radiobuttonClick(thisObj, namespace) {
   var form = document.getElementById("view"+namespace+":form1");
   var firstRadioButtonName = "view"+namespace+":form1:table1:"+0+":radio1";
   var isInDatatable = false;
   var rowId = -1;
   for(i=0; i<form.elements.length; i++){
     if(form.elements[i].name == firstRadioButtonName){
       isInDatatable = true;
     }
     if(isInDatatable == true){
       rowId = rowId+1;
     }
     radioButtonName = "view"+namespace+":form1:table1:"+rowId+":radio1";
     if(form.elements[i].name == radioButtonName){ 
        form.elements[i].checked = false;  
     }      
   }
   thisObj.checked = true;                     
 }
 
/*validates that the user has never voted before for this question and calls submitVote. 
  this is called for the onSubmit of the form in the web page. 	*/
function validateFormSelection(document, namespace, displayMessage, cookieMessage, quesId) {
   var form = document.getElementById("view"+namespace+":form1");

   //check that atleast one checkbox is selected
   //var firstRadioButtonName = "view"+namespace+":form1:table1:"+0+":rowSelect1__hidden_sel";
   var firstRadioButtonName = "view"+namespace+":form1:table1:"+0+":rowSelect1";
   var isInDatatable = false;
   var rowId = -1;
   
   for(i=0; i<form.elements.length; i++)
   {
     if(form.elements[i].name == firstRadioButtonName){
       isInDatatable = true;
     }
     if(isInDatatable == true){
       rowId = rowId+1;
     }
     //after checking for one vote selected, call checkCookie to validate if there is a cookie for this webpoll
     //radioButtonName = "view"+namespace+":form1:table1:"+rowId+":rowSelect1__hidden_sel";
     radioButtonName = "view"+namespace+":form1:table1:"+rowId+":rowSelect1";
     if(form.elements[i].name == radioButtonName)
     {
     	if(form.elements[i].checked==true)
     	{
     	   if(testCookie())
     	   {      	   
     	      if(checkCookie(cookieMessage, quesId))
              { 
                 return submitVote(namespace);
              } 
              else
              { 
                 return false;
              }
     	   }
     	   else
     	   {
     	      alert("Cookies are disabled");
     	      return false;
     	   }
        }
     }      
   }
   
   //check that atleast one radio button is selected
   firstRadioButtonName = "view"+namespace+":form1:table1:"+0+":radio1";
   isInDatatable = false;
   rowId = -1;
   for(i=0; i<form.elements.length; i++){
     if(form.elements[i].name == firstRadioButtonName){isInDatatable = true;}
     if(isInDatatable == true){rowId = rowId+1;}
     radioButtonName = "view"+namespace+":form1:table1:"+rowId+":radio1";
     if(form.elements[i].name == radioButtonName){ 
     	if(form.elements[i].checked==true){if(checkCookie(cookieMessage, quesId)){ return submitVote(namespace);} else return false;}
     }      
   }
   alert(displayMessage);
   return false;
 }
 
 // Tests if cookies are enabled. 
function testCookie() { 
  var expDate = new Date(); //valid one minute 
  expDate.setTime( expDate.getTime() + ( 60 * 1000 ) ); 
  setCookie( "testCookie", "OK", expDate ); 
  testing = getCookie( "testCookie" );

  if ( testing == "OK" ) { 
     return true; 
  }
  else { 
    return false; 
  }
}
 
 //check if there is a cookie for this question
 function checkCookie(cookieMessage, quesId)
 {
	var str=getCookie("QID");
	if(str==quesId)
	{
	   alert(cookieMessage);
	   return false;
	}
	var days=100;
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	setCookie("QID",quesId,date,"/");
	return true;
 }

 //this is called to submit a vote
 function submitVote(namespace)
 {
 	//here we are calling the generated link's onclick function (link24)
 	var linkElem=document.getElementById("view"+namespace+":form1:link24");
 	if(linkElem){ 
 		var func1="function anony1(){"+linkElem.attributes['onclick'].value+"} ";
 		eval(func1); 	
 		openResultsWindow(null);	
 		return anony1(); 		
 	} 
 	else {
 		return false;
 	}
 }

// this function opens a popup window called webpollResults
function openResultsWindow(url){	
	if(!url){
	  url="";
	}
	newwindow = window.open(url,"webpollResults", "width=447,height=420,scrollbars=no,resizable=no,status=no");
	//window.location.reload();
	newwindow.focus();
}

//called by the radio button component on the web page
function func_1(thisObj, thisEvent, namespace) {
	radiobuttonClick(thisObj, namespace);
}