/**************************************************************************************************************************
 * 
 * slyCMS
 * 
 * Written by Andy Loeber <andreas.loeber@slysolutions.ch>
 * (c) 2010-2011 slySOLUTIONS GmbH, Zurich, Switzerland. All rights reserved.
 * 
 * YOU MAY NOT REMOVE THIS COPYRIGHT NOTICE - SEE WWW.SLYSOLUTIONS.CH FOR LICENSE DETAILS
 *************************************************************************************************************************/

function slyLoginDialog(object) {
	this.object = object;
	this.base = slyDialog;
	var _this = this;
	this.base(
			{title:'slyCMS', msg:'msg', buttons:[
			          {label:'Abbrechen', event:function() {
			        	  _this.close();
			          }},
			          {label:'Anmelden', event:function() {
			        	  _this.login();
			          }}
			 ]}
			);
	document.getElementById('slyLoginDialog_login').focus();
}

slyLoginDialog.prototype = new slyDialog();

slyLoginDialog.prototype.setDialogContent = function(parentNode) {
	var html = '<table>';
	html += '<tr><td>Benutzer:</td><td><input id="slyLoginDialog_login" type="text" onkeyup="if (event.keyCode == 13 && this.value != \'\') document.getElementById(\'slyLoginDialog_password\').focus()" /></td></tr>';
	html += '<tr><td>Passwort:</td><td><input id="slyLoginDialog_password" type="password" onkeyup="if (event.keyCode == 13 && this.value != \'\') sly.dialog.login()" /></td></tr>';
	html += '</table>';
	parentNode.innerHTML = html;
};

slyLoginDialog.prototype.login = function() {
	var _this = this;
	sly.call('login', {login:document.getElementById('slyLoginDialog_login').value, password:hex_md5(hex_md5(document.getElementById('slyLoginDialog_password').value) + 'slyCMS')}, function(data) {
		if (data.statusCode == 200) {
			_this.close();
			sly.user = data.res;
		} else {
			new slyTooltip(data.statusMessage, document.getElementById('slyLoginDialog_login'));
			document.getElementById('slyLoginDialog_password').value = '';
			if (document.getElementById('slyLoginDialog_login').value == '') document.getElementById('slyLoginDialog_login').focus();
			else document.getElementById('slyLoginDialog_password').focus();
		}
	});
};
