$(document).ready(function() {
    $("#listallpoll").bremote("pollIndeks-",funcServPoll);
});
function getCookieVal (offset) 
  {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
  }

//------------------- ambil data cookie ------------------------
function getCookie(NameOfCookie)
{
	 if (document.cookie.length > 0) 
		{ 
			begin = document.cookie.indexOf(NameOfCookie+"="); 
				if (begin != -1) 
				{ 
					begin += NameOfCookie.length+1; 
					end = document.cookie.indexOf(";", begin);
					if (end == -1) end = document.cookie.length;
					return unescape(document.cookie.substring(begin, end)); 
				} 
		}
return null; 
}
//------------------- ambil data cookie ------------------------
function setCookie(NameOfCookie, value, expiredays) 
{
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24* 3600));
  	document.cookie = NameOfCookie + "=" + escape(value) + 
  	((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}
function choose(cookstr) 
{
    var ilang= $.cookie('pbahasa');	
    var radioGrp = document.poll.choice;
    var sel = false;
	var pars=cookstr;
    	for (i=0; i<radioGrp.length; i++) 
    	{
      		if (document.poll.choice[i].checked == true)
      			{
        				sel = true;
	       			var idpoll =document.poll.choice[i].value;
	       			break;
      			} 
    	}
  	
    	if (sel == false) 
    		{
			if (ilang=='EN') {
				alert("Silahkan pilih salah satu !!");

			}
			else {
      				alert("Silahkan pilih salah satu !!");

			}
      			return false;
    		}
   		vote=getCookie(pars);

		if (vote!=pars && vote==null) 
		{	
			var polling =document.poll.poll_id.value;
			setCookie(pars,pars,365)
			alert("Terima kasih atas partisipasi Anda !");
			var nametitle=document.poll.nametitle.value;
			poll_modules(polling,idpoll,"","","","","","","",nametitle);
			return document.location.href='#';
		}

		else if (vote!=null) 
		{
			if (ilang=='EN') {
			alert("Anda sudah memilih polling ini, dan tidak boleh lebih dari satu kali!!");
						}
			else {
			alert("Anda sudah memilih polling ini, dan tidak boleh lebih dari satu kali!!");
						}
			return false;
		}
}
function result() 
  {	
	   var svr="s03";
	   var idreg="poll";
  	   var idlist =document.poll.poll_id.value;
	   var nametitle=document.poll.nametitle.value;
	   polllist_modules(svr,idreg,idlist,"","","","","","",nametitle);
	   return document.location.href='#';
  }
//-------------------- modul jajak (Ajax) -------------------------------------------------------------------------------------
function funcServPoll (el) {
    if (el.clientX) {el = $(this).get(0); }
	var svr = $(el).attr("svr");
	var idreg = $(el).attr("idreg");
	var idpolling = $(el).attr("idpolling");
	var key = $(el).attr("key");
	var ctgr = $(el).attr("ctgr");
	var idchoice = $(el).attr("idchoice");
	var currentPage = $(el).attr("currentPage");
	var sdate = $(el).attr("sdate");
	var edate = $(el).attr("edate");
	var jumpPage = $(el).attr("jumpPage");
	var totPage = $(el).attr("totalPages");
	var input = $(el).siblings("@name=pageNum").val();
	var nametitle = $(el).attr("nametitle");
	var idBUMN = $(el).attr("idBUMN");

	if(jumpPage == '1')
	{
		var currentPage = input;
		if (currentPage > totPage) {
			alert(message("halamanTerakhir")+ totPage); }		
		else {
				polllist_modules(svr,idreg,idpolling,idchoice,key,ctgr,currentPage,sdate,edate,nametitle,idBUMN);	}	
	}
	else {
			polllist_modules(svr,idreg,idpolling,idchoice,key,ctgr,currentPage,sdate,edate,nametitle,idBUMN);	
	}
}

function poll_modules (polling,idpoll,nametitle) {
		var idreg='poll';
		var svr='s01';		
		var url = 'modules/?';
		var pars = 'id='+idreg+'&svr='+svr;
			$.get(url,pars, function (data) {
			   $("#pollModule").html(data);
			   $("#listallpoll").bremote("pollIndeks-",funcServPoll);
				});
					var finds="";
					var idmod="";
					var polling =document.poll.poll_id.value;
				   	polllist_modules("s03","poll",polling,idpoll,"","","","","",nametitle);
}

function polllist_modules (svr,idreg,idpolling,idchoice,key,ctgr,currentPage,sdate,edate,nametitle,idBUMN) {
var fopts ="";
var showsfilters="";
var judul =nametitle; 
//alert ("2");
showsfilters=true;  
fopts = { id: "poll" };			
		  var url  = "modules/";	
		  var pctgr = function() {
			  $("a[@class=page_poll]").bind("click",funcServPoll);
			  $("a.listpoll").bremote("pollDetail-",funcServPoll);
			  $("a.listpollctgr").bremote("pollDetail",funcServPoll);
			  $("input[@class=searchButton]").bind("click",funcServPoll);	
			  $("a[@class=newPage]").bind("click",funcServPoll);  
		  }
		  var options = {title : judul,url: "modules/",filterOptions : fopts, id : idreg , postCtgr : pctgr, postKey : pctgr, postPrd : pctgr, showfilter: true};
		  var pars = {idpolling: idpolling, key: key , ctgr: ctgr, filterOptions : fopts, id: idreg, svr: svr, idchoice: idchoice, currentPage: currentPage, sdate: sdate, edate: edate, idBUMN: idBUMN};
		  Container.displayOnView(options,pars,function( ) {		  
 			$("a.list").bremote("pollDetail",funcServPoll);
 			$("a.detail").bremote("pollDeatail-",funcServPoll);
			$("a[@class=page_poll]").bind("click",funcServPoll);
			$("a.indeks_all").bremote("pollIndeks",funcServPoll);
			$("a.listpoll").bremote("pollDetail",funcServPoll);	
		    $("a.listpollctgr").bremote("pollDetail",funcServPoll);
			$("input[@class=searchButton]").bind("click",funcServPoll);	
			$("a[@class=newPage]").bind("click",funcServPoll);
			$("a[@class=printdata]").bind("click",PrintPoll);
			$("#formTanggapan3").ajaxForm(ct_options3);  
	});
	//alert("test");
}
function PrintPoll(event)
{
	var idmod = $(this).attr("idmod");
	var svr = $(this).attr("svr");
	var idreg = $(this).attr("idreg");	
	var idBUMN = $(this).attr("idBUMN");	
	var url = 'modules/';
	swin = window.open(url+"?idmod="+idmod+"&svr="+svr+"&id="+idreg+"&idBUMN="+idBUMN,'win','scrollbars,width=750,height=600,top=80,left=140,status=yes,toolbar=no,menubar=yes,location=no');
	swin.focus();
}

function MenuPollServices()
{
var url='modules/?id=poll&svr=s05';$.getScript(url);return
}