﻿/**
 * @author m5it
 */

var Anketa = Class.create();
Anketa.prototype = {
	initialize: function(auid) {
	//alert("auid: "+auid);
	
	   this.uid       = "";
	   this.nameCookie='cekira_glasuj';
	   
	   if(myCO.get(this.nameCookie)==-1)
	       this.uid = auid;
	   else {
	       var tmpuid = myCO.get(this.nameCookie);
	       if(tmpuid != auid) {
	           myCO.set(this.nameCookie, "", -1);
		   this.uid = auid;
	       }
	       else this.uid = auid;
	       //var cvar = myCO.get(this.nameCookie);
	       //var atmp = cvar.split("\x10");
	       
	       //this.uid = atmp[0];
	   }
	},
	
	ResponseStat: function(text) {
	    $('anketa').innerHTML = text;
	},	

	Glasuj: function(spol) {
	    var ank_nvpr = 0;
	    var ank_nv   =-1;
	    ank_nvpr = $('ank_nvpr').value;
	    for(i=0;i<ank_nvpr;i++) {
	        if($('anketa'+i).checked) {
	            ank_nv=i;
	        }
	    }
	    
	    if(ank_nv>-1) {
	        jrUrl  ='sc/js-request.php';
	        jrData ="?anketa=1&ank_nvpr="+ank_nv+"&ank_spol="+spol+"&ank_uid="+this.uid;
	        myJR.ajxRequest(jrUrl, jrData, this.Response);
	    }
	    else {
	        alert("Napaka!: Izbrati morate odgovor!");
	    }
	},
	
	Response: function(text) {
	    if(text>=0) {
		    myCO.set(myAN.nameCookie, myAN.uid, 120);
	        
	        jrUrl  ='sc/js-request.php';
	        jrData ="?anketaStat=1";
	        myJR.ajxRequest(jrUrl, jrData, myAN.ResponseStat);
	    }
	    else {
	        if(text==-1) {
	            alert("Napaka. Si že glasoval!");
	            document.location.href=document.location;
	        }
	        else if(text==-2) {
	            alert("Napaka. Napačni argumenti pri zagonu ajxRequest() !");
	            document.location.href=document.location;
	        }
	        else {
	            alert("Napaka. Neznana! : \n\n"+text);
	            document.location.href=document.location;
	        }
	    }
	}
}
