/* login page functions */

function enterSubmit(thisForm, e) {
	if (window.event && window.event.keyCode == 13) {
		loginAttempt();
		return false;
	} else if (e && e.which == 13) {
		loginAttempt();
		return false;
	} else {
		return true;
	}
}

function updateLoginStatus(msg, msgType) {
	var statusHeader = (msgType == "statuserror" ? ilcTrans(5504,"Error Message") : ilcTrans(5619,"Status Message"));

	if($("#error").hasClass(msgType)) {
		$("#error")
			.html("<h3>"+statusHeader+"</h3><ul><li>"+msg+"</li>");
	} else {
		$("#error")
			.toggleClass("statuserror").toggleClass("statusconfirm")
			.html("<h3>"+statusHeader+"</h3><ul><li>"+msg+"</li>");
	}

	if ($("#error").css("display") == "none") {
		$("#error").show("fast")
	}
}

function loginAttempt() {
	var form = document.login;

	updateLoginStatus(ilcTrans(5649,"Validating log-in information..."), 'statusconfirm');

	// remove 8.6 and 9.0 cookies
	var domainAry = location.host.split(".");
	if (domainAry[domainAry.length-1].match(/\D+/) && form.hosted.value == '1') { // ensure that the domain is not an IP address
		var delDomain = domainAry[domainAry.length-2] + "." + domainAry[domainAry.length-1];
		document.cookie = "vendor=; path=/; domain=." + delDomain + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "auth=; path=/; domain=." + delDomain + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}

	var ajaxObj = new ilincAjax('loginAttempt', 'loginAttemptReturn');
	ajaxObj.addParam('user_name', form.user_name.value);
	ajaxObj.addParam('password', form.password.value);
	ajaxObj.addParam('username_label', form.username_label.value);
	ajaxObj.addParam('password_label', form.password_label.value);
	ajaxObj.send();
}
function loginAttemptReturn(retObj) {
	var status = '';
	var statusType = 'statuserror';

	if (retObj) {
		if (retObj.error_id == 0) {
			status = ilcTrans(5558,"Login validation succeeded");
			statusType = 'statusconfirm';
			updateLoginStatus(status, statusType);
			$("#login").submit();
		} else {
			status = retObj.errorMsg;
			statusType = 'statuserror';

			// clear password field on error
			var form = document.login;
			form.password.value = '';
		}
	} else {
		status = ilcTrans(5445,"An unknown error occured.");
		statusType = 'statuserror';
	}

	updateLoginStatus(status, statusType);
}


