//

User = new function(){

	this.checkLogin = function(f){
		$.post('/', { 
			action: 'login',
			login: f.login.value,
			pass: f.pass.value,
		},
			function (reply) {
				//alert(reply)
				var replyData = XN.fromJSON(reply, 'login');
				if(replyData != 0){
					document.location.reload();
					//Jemplate.process('auth.tmpl', replyData, '#auth');
				}
			}
		);
		return false;
	}

	this.logout = function(){
		$.post('/', { 
			action: 'logout'
		},
			function (reply) {
				document.location.reload();
			}
		);
		return false;
	}

}

Feedback = new function(){

	this.send = function(f){
		$.post('/', { 
			action: 'feedback',
			text: f.text.value,
		},
			function (reply) {
				var replyData = XN.fromJSON(reply, 'feedback');
				if(replyData != 0){ alert('OK!'); }
			}
		);
		return false;
	}
}


XN = new function(){

	this.fromJSON = function(replyText, action){
		var replyData = JSON.parse(replyText);
		return XN.checkErr(replyData, action) || 0;
	}

	this.checkErr = function(replyData, action){
		if(replyData[action].ok == 1) return replyData;
		
		XN.showErr(replyData);
		return 0;
	}

	this.showErr = function(replyData){
		var errText = 'There is errors on action: ';
		for(var block in replyData){
			for(var j = 0; j < replyData[block].errors.length; j++){
				errText += replyData[block].errors[j] + ' ';
			}
		}
		document.getElementById('errors').innerHTML = errText;
		document.getElementById('errors').style.display = '';
		setTimeout("document.getElementById('errors').style.display = 'none'", 3000);
	}
}

Util = new function(){
	
	
}

Template = new function(){
	this.version = '1.0';
}

Template.prototype = Jemplate;