// vote.js

// newFunction
var showComments = false;
function validateVote(voteId) 
{
	if( GetVoteCookieValue("voteId"+voteId) )
		return ;
	var arrAllAnswers = document.getElementsByName("answer" + voteId ) ;
	var answerIsChecked 	 = false ;
	var language = "" ;

	for(var i = 0 ; i < arrAllAnswers.length ; i++ )
	{
		if( arrAllAnswers[i].checked )
		{
			answerIsChecked = true ;
			break ;
		}
	}
	if( !answerIsChecked)
	{
		if( document.getElementById('language') )	
			language = document.getElementById('language').value ;
		if( language == "english" )	
			alert("Please select your choice ");
		else
			alert("الرجاء اختيار أحد الخيارات الموجودة");
		return ;
	}
	//openVoteWindow() ;
	if( document.getElementById("voteId") )
	{
		document.getElementById("voteId").value = voteId ;
	}
	if( document.getElementById("actionVote") )
	{
		document.getElementById("actionVote").value = "voteParticipate" ;
	}
	
	disableAllAnswers(voteId);	
	if( document.getElementById("voteParticipateForm") )
	{
		//SetCookie("vote"+voteId,voteId);
		SetCookie("voteId"+voteId,voteId);
		showComments = true ;
		openVoteWindow() ;
		showComments = false ;
		document.getElementById("voteParticipateForm").submit();
	}	
	
}
function openVoteWindow()
{
	var height = 390 ;
	
	//alert(showComments);
	
	if ( typeof answerCounts == "number" )
	{
		switch(answerCounts)
		{
			case 1:
			case 2:
			case 3:
			{
				height = 350;
			};break;
			default:
			{
				height = (answerCounts * 90) + 20 ;				
			}
		}
	}
	
	if( showComments )
	{
		height +=250;
	}	
	
	var winVote = window.open("","voteWindow","height=" + height + ",width=620,status=no,toolbar=no,menubar=no,location=no,scrollbars=auto");	
	winVote.focus();
	
}
function getVoteAnswers(voteId,openNewWindow)
{
	if( openNewWindow == "true" )
		openVoteWindow() ;

	
	if( document.getElementById("voteId") )
	{
		document.getElementById("voteId").value = voteId ;
	}
	if( document.getElementById("actionVote") )
	{
		document.getElementById("actionVote").value = "getVoteResults" ;
	}	
	if( document.getElementById("voteParticipateForm") )
	{
		if( openNewWindow == "false")
			document.getElementById("voteParticipateForm").target = "" ;
			
		document.getElementById("voteParticipateForm").submit();
	}	
}
function GetVoteCookieValue(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");  	
  
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");      
	if (sName == aCrumb[0]) 
	{  
	   return unescape(aCrumb[1]);     
	}      
      
  }

  // a cookie with the requested name does not exist
  return null;
}
function CheckCookie(voteId)
{
	//return ;
	var voteId = GetVoteCookieValue("voteId"+voteId);

	if( voteId )
	{
		disableAllAnswers(voteId);
	}
	if (!(navigator.cookieEnabled))
	{
		//alert ("cookies is not enabled") ;
		disableAllAnswers(voteId);
	}	
	//alert(voteId) ;
}
function disableAllAnswers(voteId)
{
	var coll = document.all.item("answer" + voteId);
	
	if (coll)
	{
		for (var i=0; i<coll.length; i++)
		{
			coll(i).disabled = true;
		}
	}	
	
	if( document.getElementById("voteBtn" + voteId ) )
	{
		document.getElementById("voteBtn" + voteId ).disabled = true ;
	}	
}
function SetCookie(sName,sValue )
{
	expDate = new Date('1/1/2020');	
	//expDate = new Date(sExpiration) ;
	//expDate.setYear(expDate.getYear()+1);
	
	expiration = ";expires=" + expDate.toUTCString() +";" ;
	
	var domain	= ";domain=" + "http://www.moi.gov.qa" ; 
	var path	= ";path=" +  "/" ; 
	
	try
	{		
		document.cookie = sName + "=" + escape(sValue) + expiration + path  ;//+ domain + path ;		
		//alert(sName + "=" + escape(sValue) + expiration ) ;
	}
	catch(e)
	{
		//alert(e.description) ;		
		return false ;
	}
	return true ;
}
function getVoteArchive(depId)
{
	var winVote = window.open("","voteWindow","height=600,width=620,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");	
	winVote.focus();
	
	if( document.getElementById("depId") )
	{
		document.getElementById("depId").value = depId ;
	}
			
	if( document.getElementById("actionVote") )
	{
		document.getElementById("actionVote").value = "getArchivedVotePage" ;
	}	
	if( document.getElementById("voteParticipateForm") )
	{
		document.getElementById("voteParticipateForm").submit();
	}		
}
