﻿var timeoutVar;

addLoadEvent(LoginPageInit);


function LoginPageInit() {
	setProperFieldFocus();
	document.onkeydown = function(evt) {
		var e = (evt) ? evt : ((event) ? event : null);
		if (e != null) {
			var c = (e.target) ? e.target : ((e.srcElement) ? e.srcElement : null);
			if ((e.keyCode == 27) && (c.id == 'resetPasswordUser')) {
				resetCancelClicked();
				return false;
			}
		}
	}
}

function loginSumbitClicked() {
	postVars = getFormPostVars('LoginForm');
	if (postVars.length){
		postVars = 'fn=UserLogin&' + postVars;
		var pwVal = document.getElementById('loginPassword').value;
		if (pwVal.length) postVars += '&shaud=' + sha1(pwVal);
		timeoutVar = setTimeout('dimControls(\'LoginForm\',timeoutVar)',420);
		submitAjaxPostRequest('/Login/index.php', loginSumbitClickedCallBack, postVars);
	}
	return false;
}

function loginSumbitClickedCallBack(responseText) {
	undimControls('LoginForm', timeoutVar);
	var respTxt = responseText.substring(2);

	if (responseText.charCodeAt(0) == 48) {
		updateErrorText('LoginErrorList', '');
		showCurrentUserInfo(respTxt);
		var redirectCtrl = document.getElementById('loginReturnURL');
		if (redirectCtrl != null) window.location = redirectCtrl.value;
	} else {
		document.getElementById('loginPassword').value = '';
		updateErrorText('LoginErrorList', respTxt);
		setProperFieldFocus();
	}
}

function resetSubmitClicked() {
	var postVars = "fn=ResetPassword&" + getFormPostVars('ResetForm');
	timeoutVar=setTimeout('dimControls(\'ResetForm\',timeoutVar)',420);
	submitAjaxPostRequest('/Login/index.php', resetSubmitClickedCallBack, postVars);
	return false;
}

function resetSubmitClickedCallBack(responseText) {
	undimControls('ResetForm', timeoutVar);
	var respTxt = (responseText.length > 2) ? responseText.substring(2) : '';
	if (respTxt.length == 0){
		document.getElementById('LoginTable').style.display = 'none';
		document.getElementById('ResetTable').style.display = 'none';
		document.getElementById('ResetComp').style.display = 'table';
	}
	else
		document.getElementById('resetPasswordUser').focus();
	updateErrorText('ResetErrorList', respTxt);
}

function loginResetClicked() {
	var rpu = document.getElementById('resetPasswordUser');
	var lun = document.getElementById('loginUserName');
	rpu.value = lun.value;
	document.getElementById('LoginTable').style.display = 'none';
	document.getElementById('ResetTable').style.display = 'table';
	rpu.focus();
}

function resetCancelClicked() {
	document.getElementById('LoginTable').style.display = 'table';
	document.getElementById('ResetTable').style.display = 'none';
	setProperFieldFocus();
}

function resetCompleteClicked() {
	window.location = '/';
}


function updateErrorText(errorCtl, errorText) {
	var loginErrorListCtrl = document.getElementById(errorCtl);
	if (loginErrorListCtrl) {
		loginErrorListCtrl.innerHTML = errorText;
		loginErrorListCtrl.style.display = (errorText == '') ? 'none' : 'block';
	}
}

function setProperFieldFocus() {
	var lun = document.getElementById('loginUserName');
	if (lun != null) {
		var lpw = document.getElementById('loginPassword');
		if ((lun.value != null) && (lun.value.length > 0) && (lpw != null))
			lpw.focus();
		else
			lun.focus();
	}
}


