dojo.declare("cMainJavascript", null, {
	constructor : function(args) {
		this.kos=[];
	},
	doKosiku : function(a) {//eow
		var check=a.attr('checked');
		var val=a.attr('value');
		var id = a.attr('id');
		if(check){
			this.kos.push({id:id,val:val});
		}else{
			for(var i=0;i<this.kos.length;i++){
				if(this.kos[i].id==id){
					this.kos.splice(i,1);
				}           d
			}
		}
		var arr=[];
		for(var i=0;i<this.kos.length;i++){
			arr.push(this.kos[i].val);
		}		
		dojo.attr('inputkurzy','value',arr.toString());
		return
	},
	regme : function(){
		$o=dojo.formToObject("registerform");
		var errstr="";
		if($o.jmeno=="") errstr="jméno\n";
		if($o.prijmeni=="") errstr+="příjmení\n";
		if($o.adresa=="") errstr+="adresa\n";
		if($o.mesto=="") errstr+="město\n";
		if($o.psc=="") errstr+="PSČ\n";
		if($o.mobil=="") errstr+="mobil\n";
		var email=dijit.byId('email');
		if(!email.isValid()) errstr+="email\n";
		if($o.unamereg=="") errstr+="uživatelské jméno\n";
		if($o.upassreg=="") errstr+="heslo\n";
		if($o.upassreg!=$o.upass2reg) errstr+="zadané hesla nejsou stejná\n";
		if(errstr!=""){
			errstr="Doplňte následující položky:\n"+errstr;
			alert(errstr);
			return;
		}else{
		    dojo.xhrPost({
			      url: webhost+"/index/register",
			      postData:dojo.formToJson("registerform"),
			      load: function(response){
		    		if(response=="ok"){
		    			window.location=webhost;
		    		}else{
		    			alert(response);
		    		}
			        return response;
			      },
			      error: function(response, ioArgs){
			        alert("An error occurred, with response: " + response);
			        return response;
			      }
			});
		}
	},
	regmeAndSubscribe : function(){
		$o=dojo.formToObject("subscribeform");
		var errstr="";
		if($o.jmeno=="") errstr="jméno\n";
		if($o.prijmeni=="") errstr+="příjmení\n";
		if($o.adresa=="") errstr+="adresa\n";
		if($o.mesto=="") errstr+="město\n";
		if($o.psc=="") errstr+="PSČ\n";
		if($o.mobil=="") errstr+="mobil\n";
		var email=dijit.byId('email');
		if(!email.isValid()) errstr+="email\n";
		if($o.unamereg=="") errstr+="uživatelské jméno\n";
		if($o.upassreg=="") errstr+="heslo\n";
		if($o.upassreg!=$o.upass2reg) errstr+="zadané hesla nejsou stejná\n";
		if($o.prihlasenekurzy=="") errstr+="nevybrali jste žádný kurz\n";
		if(errstr!=""){
			errstr="Doplňte následující položky:\n"+errstr;
			alert(errstr);
			return;
		}else{
			$a={
			jmeno:$o.jmeno,prijmeni:$o.prijmeni,borndate:$o.borndate,adresa:$o.adresa,mesto:$o.mesto,psc:$o.psc,mobil:$o.mobil,email:$o.email,country:$o.country,unamereg:$o.unamereg,upassreg:$o.upassreg,prihlasenekurzy:$o.prihlasenekurzy
			};
		    dojo.xhrPost({
			      url: webhost+"/index/registerandsubscribe",
			      postData:dojo.toJson($a),
			      load: function(response){
		    		if(response=="ok"){
		    			alert("Vaše přihláška byla přijata")
		    			window.location=webhost;
		    		}else{
		    			alert(response);
		    		}
			        return response;
			      },
			      error: function(response, ioArgs){
			        alert("An error occurred, with response: " + response);
			        return response;
			      }
			});
		}
	},
	extsubmit: function(){
		$o=dojo.formToObject("extsubmitform");
		if($o.uname!="" && $o.upass!=""){
		    dojo.xhrPost({
			      url: webhost+"/index/loginajax",
			      postData:dojo.formToJson("extsubmitform"),
			      load: function(response){
		    		if(response=="ok"){
		    			dojo.byId("extsubmitform").submit();
		    		}else{
		    			alert(response);
		    		}
			        return response;
			      },
			      error: function(response, ioArgs){
			        alert("An error occurred, with response: " + response);
			        return response;
			      }
			});
		}else{
			var errstr="";
			if($o.jmeno=="") errstr="jméno\n";
			if($o.prijmeni=="") errstr+="příjmení\n";
			if($o.adresa=="") errstr+="adresa\n";
			if($o.mesto=="") errstr+="město\n";
			if($o.psc=="") errstr+="PSČ\n";
			if($o.mobil=="") errstr+="mobil\n";
			if($o.email=="") errstr+="email\n";
			if($o.unamereg=="") errstr+="uživatelské jméno\n";
			if($o.upassreg=="") errstr+="heslo\n";
			if($o.upassreg!=$o.upass2reg) errstr+="zadané hesla nejsou stejná\n";
			if(errstr!=""){
				errstr="Doplňte následující položky:\n"+errstr;
				alert(errstr);
				return;
			}else{
			    dojo.xhrPost({
				      url: webhost+"/index/register",
				      postData:dojo.formToJson("extsubmitform"),
				      load: function(response){
			    		if(response=="ok"){
			    			dojo.byId("extsubmitform").submit();
			    		}else{
			    			alert(response);
			    		}
				        return response;
				      },
				      error: function(response, ioArgs){
				        alert("An error occurred, with response: " + response);
				        return response;
				      }
				});
			}
		}
	},
	sendBulk : function(){
	    dojo.xhrPost({
		      url: webhost+"/kurzy/sendmailitem",
		      postData:"test",
		      load: function(response){
	    		var a=Number(response);
	    		if(a<0){
	    			window.location=webhost+"/kurzy";
	    		}else{
	    			dijit.byId("prBar").update({'progress':maxitems-a});
	    			mj.sendBulk();
	    		}
		        return response;
		      },
		      error: function(response, ioArgs){
		        alert("An error occurred, with response: " + response);
		        return response;
		      }
		});
	},
	saveme : function(){
		$o=dojo.formToObject("registerform");
		var errstr="";
		if($o.jmeno=="") errstr="jméno\n";
		if($o.prijmeni=="") errstr+="příjmení\n";
		if($o.adresa=="") errstr+="adresa\n";
		if($o.mesto=="") errstr+="město\n";
		if($o.psc=="") errstr+="PSČ\n";
		if($o.mobil=="") errstr+="mobil\n";
		if($o.email=="") errstr+="email\n";
		if($o.upassreg!=$o.upass2reg) errstr+="zadané hesla nejsou stejná\n";
		if(errstr!=""){
			errstr="Doplňte následující položky:\n"+errstr;
			alert(errstr);
			return;
		}else{
		    dojo.xhrPost({
			      url: webhost+"/index/modifyprofile",
			      postData:dojo.formToJson("registerform"),
			      load: function(response){
		    		if(response=="ok"){
		    			alert("Váš profil byl úspěšně uložen");
		    			window.location=webhost;
		    		}else{
		    			alert(response);
		    		}
			        return response;
			      },
			      error: function(response, ioArgs){
			        alert("An error occurred, with response: " + response);
			        return response;
			      }
			});
		}
	},
	sendpass : function(){
		$o=dojo.formToObject("passform");
		if($o.username==""&&$o.email==""){
			alert("Vyplňte prosím email nebo uživatelské jméno");
			return;
		}else{
		    dojo.xhrPost({
			      url: webhost+"/index/sendpass",
			      postData:dojo.formToJson("passform"),
			      load: function(response){
		    		if(response=="ok"){
		    			alert("Heslo bylo úspěšně odesláno");
		    			window.location=webhost;
		    		}else{
		    			alert(response);
		    		}
			        return response;
			      },
			      error: function(response, ioArgs){
			        alert("An error occurred, with response: " + response);
			        return response;
			      }
			});
		}
	},
	showActionDetails : function(aid){
		if(dlgDetail == ""){
			dlgDetail = new dijit.Dialog({
				title: "Detaily kurzu",
				style: "width: 650px;"
		    });
		}
		dlgDetail.onBlur=function(){
			dlgDetail.hide();
		}
		dlgDetail.show();
		dlgDetail.attr("href",webhost+"/index/getactiondetail/id/"+aid);
	}
});

function startup(){
	if(dojo.byId("loginbutton")){
		dojo.connect(dojo.byId("loginbutton"),'onclick',null, function(e){
			e.preventDefault();
	        e.stopPropagation();
	        if(dijit.byId("uname").attr("value")!="" && dijit.byId("upass").attr("value")!=""){
	        	dojo.byId("lform").submit();
	        }else{
	        	alert("vyplňte přihlašovací údaje");
	        }
		});
	}
	dlgDetail="";
	mj=new cMainJavascript();
	if(dojo.byId("prBar")){
		maxitems=Number(dijit.byId("prBar").attr('maximum'));
		mj.sendBulk();
	}
}

dojo.addOnLoad(startup);