Scriptaculous.require(Config.Paths.JSLibraryClasses+'class_octethform.js');

var LoginForm = null;

var WindowLogin = {
	DefaultRedirectURL: 'overview.php',
	onFormSubmit: function() {
		WindowLogin.hideFormErrorMessage();
	},
	processAPIResponse: function (APIResponse) {
		if (APIResponse.Success == true) {
			// Page redirection
			window.location.href = ($F('FormValue_RedirectURL') != '' ? $F('FormValue_RedirectURL') : WindowLogin.DefaultRedirectURL);
		} else {
			WindowLogin.hideLoader();
			for (i=0; i<APIResponse.ErrorCode.length; i++) {
				switch (APIResponse.ErrorCode[i]) {
					case 1:
						LoginForm.validateField('Username','Rule1',true);
						$('Form_Login').enable();
						$('FormValue_Username').focus();
						$('captcha-image').src = $('captcha-image').src + '&rnd=' + Math.random();
						break;
					case 2:
						LoginForm.validateField('Password','Rule1',true);
						$('Form_Login').enable();
						$('FormValue_Password').focus();
						$('captcha-image').src = $('captcha-image').src + '&rnd=' + Math.random();
						break;
					case 3:
						WindowLogin.displayFormErrorMessage(Language['0009']);
						$('Form_Login').enable();
						$('Form_Login').focusFirstElement();
						$('captcha-image').src = $('captcha-image').src + '&rnd=' + Math.random();
						break;
					case 4:
						WindowLogin.displayFormErrorMessage(Language['0012']);
						$('Form_Login').enable();
						$('Form_Login').focusFirstElement();
						$('captcha-image').src = $('captcha-image').src + '&rnd=' + Math.random();
						break;
					case 5:
						WindowLogin.displayFormErrorMessage(Language['0013']);
						$('Form_Login').enable();
						$('Form_Login').focusFirstElement();
						$('captcha-image').src = $('captcha-image').src + '&rnd=' + Math.random();
						break;
				}
			}
		}
	},
	displayFormErrorMessage: function (message) {
		$('FormRow_Message').innerHTML = '';
		$('FormRow_Message').insert({bottom:FormFieldValidationMessages.Error(message)});
		$('FormRow_Message').show();
	},
	hideFormErrorMessage: function () {
		$('FormRow_Message').hide();
	},
	showLoader: function() {
		$('login-form').hide();
		$('loader').show();
	},
	hideLoader: function() {
		$('login-form').show();
		$('loader').hide();
	}
};
/* Window onload event - Start */
document.observe("dom:loaded", function() {
	LoginForm = new OctethForm('Form_Login', {
		action				: Config.URLs.API,
		method				: 'post',
		requestParameters	: {
								responseFormat: 'JSON',
								command	: 'User.Login'
		},
		onConnectionSuccess : WindowLogin.processAPIResponse,
		onConnectionFailure : function() { WindowLogin.hideLoader(); WindowLogin.displayFormErrorMessage(Language['9019']); },
		onFormLoading		: function() { WindowLogin.showLoader(); },
		onFormSubmit		: WindowLogin.onFormSubmit,
		validation			: [
								{ fieldName: 'Username', rules: [{ name: 'Rule1', rule:FormValidationRules.Empty, message:[FormFieldValidationMessages.Error, Language['0007']] }]},
								{ fieldName: 'Password', rules: [{ name: 'Rule2', rule:FormValidationRules.Empty, message:[FormFieldValidationMessages.Error, Language['0008']] }]}
		]
	});

	if ($('FormValue_Captcha')) {
		LoginForm.validation.push({ fieldName: 'Captcha', rules: [{ name: 'Rule3', rule:FormValidationRules.Empty, message:[FormFieldValidationMessages.Error, Language['0011']] }]});
	}

	$('Form_Login').focusFirstElement(); // Focus on first element of form
});
/* Window onload event - End */

