var coupons = jQuery.extend({
	login: function() {
		try {
			// call the real function trapping all errors
			return this.loginFunc();
		} catch (e) {
			if (console) {
				console.error(e);
			}
			return this.showError(e.message);
		}
	},

	loginFunc: function() {
		// case prev login failed
		$('#coupons-msgs').css('display', 'none');

		var username = $('#coupons-username').val();
		var password = $('#coupons-password').val();
		var sess = this.getSessionData();
		if (!sess) {
			// username/password both should be filled
			if (!password || !username) {
				return this.showError('not_filled');
			}
			// try to sign in!
			$('#coupons-ajax_loading').css('display', 'block');
			var res = this.Authenticate(username, password);
			$('#coupons-ajax_loading').css('display', 'none');
			if (!res) {
				return this.showError('wrong_pw');
			}
			return true;
		}
		// ok we had session from js side, check is it valid?
		var res = this.Ping();
		if (!res) {
			return this.showError('cookie_notset');
		}

		// all passed. go and post!
		return true;
	},

	showError: function(code) {
		$('#coupons-msgs').css('display', 'block');
		var msg = this.msgs[code] ? this.msgs[code] : this.msgs['error'];
		$('#coupons-msgs').html(msg);
		// for return value propagation
		return false;
	}
}, cbsc);
