$(document).ready(function () {
$('#chkTermsAndConditions').attr('checked', false);
// Below method will prevent the copy and paste activity for password field.
$("#txtPassword").keydown(function (event) {
if (event.ctrlKey && (event.which === 99 || event.which === 67)) {
event.preventDefault();
}
});
// Below method will prevents mouse right click event for password field.
$("#txtPassword").on("contextmenu", function (e) {
e.preventDefault();
});
$("#txtUserName").focus(function () {
$("#txtUserName").val("");
}).blur(function () {
if ($("#txtUserName").val().length == 0) {
$("#txtUserName").val(loginPage.UserNameText);
}
});
$("#txtUserName").keyup(checkPasswordMatch);
function checkPasswordMatch() {
if ($("#txtUserName").val() === loginPage.defaultUserName) {
$("#txtPassword").val("");
$("#passwordPlaceholder").val("Enter password");
$("#passwordPlaceholder").attr('readonly', true);
$("#txtPassword").attr('readonly', true);
}
else {
$("#passwordPlaceholder").attr('readonly', false);
$("#txtPassword").attr('readonly', false);
}
}
var passwordField = $('input[name=password]');
// add a password placeholder field to the html
passwordField.after('');
var passwordPlaceholder = $('#passwordPlaceholder');
// show the placeholder with the prompt text and hide the actual password field
passwordPlaceholder.show();
passwordField.hide();
// when focus is placed on the placeholder hide the placeholder and show the actual password field
passwordPlaceholder.focus(function () {
if ($("#txtUserName").val() === loginPage.defaultUserName) {
$("#txtPassword").val("");
$("#passwordPlaceholder").val("Enter password");
$("#passwordPlaceholder").attr('readonly', true);
$("#txtPassword").attr('readonly', true);
}
else {
$("#passwordPlaceholder").attr('readonly', false);
$("#txtPassword").attr('readonly', false);
passwordPlaceholder.hide();
passwordField.show();
passwordField.focus();
}
});
// when focus goes to and moves away from the email field, reset it to blank or restore the default depending if a value is entered
passwordField.blur(function () {
if (passwordField.val() == '') {
passwordPlaceholder.show();
passwordField.hide();
}
});
var loginPage = {
username: null,
password: null,
waitText: "--",
noResponseText: "Unknown Error",
UserNameText: null,
PasswordText: null,
InvalidCredentialErrorMsg: null,
defaultUserName: null,
doAjax: function (p) {
loginPage.showSpinner();
$.ajax({
type: p.type,
url: p.url,
data: p.data,
dataType: p.dataType,
success: p.callbackMethod,
error: p.errorMethod
});
},
showSpinner: function () {
$('#dialogContainer').dialog({
autoOpen: false,
resizable: false,
modal: true
});
$(".ui-dialog-titlebar").hide();
$(".ui-dialog-buttonpane").hide();
$('#dialogContainer').html(
'
' + loginPage.waitText + '
'
).dialog("open");
},
onError: function (data, textStatus, jqXHR) {
$('#dialogContainer').dialog("close");
var jsonData = jQuery.parseJSON(data.responseText);
if (jsonData.u === "") {
ShowErrorMsg(loginPage.InvalidCredentialErrorMsg);
}
if (jsonData.u != "") {
window.location.href = jsonData.u;
}
},
callbackGotStrings: function (data, textStatus, jqXHR) {
loginPage.waitText = getResourceValue(data.IDS_PLEASEWAIT);
sessionStorage.setItem('WaitText', loginPage.waitText);
loginPage.noResponseText = getResourceValue(data.IDS_NORESPONSE1);
loginPage.username = getResourceValue(data.IDS_USERNAME);
$("#usernameLabel").html(loginPage.username);
loginPage.password = getResourceValue(data.IDS_PASSWORD);
$("#passwordLabel").html(loginPage.password);
$("#teesAndCeesLabel").html(getResourceValue(data.IDS_LOGIN_TERMS_CONDITIONS));
$("#btnLogin").html(getResourceValue(data.IDS_LOGIN));
var loginPageTitle = getResourceValue(data.IDS_LOGINWELCOMEMESSAGE);
$(document).prop('title', loginPageTitle);
$("#txtUserName").val(getResourceValue(data.IDS_LOGINUSERNAMETEXT));
loginPage.UserNameText = getResourceValue(data.IDS_LOGINUSERNAMETEXT);
loginPage.PasswordText = getResourceValue(data.IDS_LOGINPASSWORDTEXT);
$("#passwordPlaceholder").val(loginPage.PasswordText);
loginPage.InvalidCredentialErrorMsg = getResourceValue(data.IDS_INCORRECT_PASSWORD);
loginPage.defaultUserName = getResourceValue(data.IDS_DEFAULT);
$('#dialogContainer').dialog("close");
},
hmacSHA1: function (key) {
var hasher = new sjcl.misc.hmac(key, sjcl.hash.sha1);
this.encrypt = function () {
return hasher.encrypt.apply(hasher, arguments);
};
},
RemoveByIndex: function (arr, index, numberOfItemsToRemove) {
arr.splice(index, numberOfItemsToRemove);
},
callbackGetLoginParameters: function (data, textStatus, jqXHR) {
if (data.r != true) {
$('#dialogContainer').dialog("close");
$("#loginFormMsg").html(data.m).show();
}
else {
sessionStorage.setItem('loggedInUserName', loginPage.username);
var hashKeyBytes = null;
var hashKeyBytesAsHexString = null;
var secondaryTextToHash = null;
var saltBytes = null;
var secondaryHashBytes = null;
var secondaryHashBytesConverted = null;
// In case of Up grade scenario, need to use CRYPTOJS algorithm.
if (data.a === 0) {
hashKeyBytes = CryptoJS.MD5(data.s1 + loginPage.password);
// 1. Convert the original password hash to hex string
hashKeyBytesAsHexString = CryptoJS.enc.Hex.stringify(hashKeyBytes);
}
else {
// Convert the original password hash to hex string
saltBytes = sjcl.codec.hex.toBits(data.s1);
//Generate keys from passwords using PBKDF2-HMAC-SHA256.
hashKeyBytes = sjcl.misc.pbkdf2(loginPage.password, saltBytes, data.i1, 256, loginPage.hmacSHA1);
loginPage.RemoveByIndex(hashKeyBytes, 5,3);
//Convert from a bitArray to a hex string.
hashKeyBytesAsHexString = sjcl.codec.hex.fromBits(hashKeyBytes);
if (hashKeyBytesAsHexString.length > 16) {
hashKeyBytesAsHexString = hashKeyBytesAsHexString.substr(0, 16);
}
}
// Add the random text
secondaryTextToHash = hashKeyBytesAsHexString.toUpperCase() + data.n;
//Convert from a hex string to a bitArray.
saltBytes = sjcl.codec.hex.toBits(data.s2);
//Generate keys from passwords using PBKDF2-HMAC-SHA256.
secondaryHashBytes = sjcl.misc.pbkdf2(secondaryTextToHash.toUpperCase(), saltBytes, data.i2, 256, loginPage.hmacSHA1);
loginPage.RemoveByIndex(secondaryHashBytes, 5);
//Convert from a bitArray to a hex string.
secondaryHashBytesConverted = sjcl.codec.hex.fromBits(secondaryHashBytes);
secondaryHashBytesConverted = secondaryHashBytesConverted.substr(0, 16);
loginPage.doAjax({
"url": '/BEGINSESSION',
"requestType": 'GET',
"dataType": 'json',
"callbackMethod": loginPage.callbackDoLogin,
"errorMethod": loginPage.onError,
"data": {
"LSID": data.g,
"PWH": secondaryHashBytesConverted.toUpperCase()
}
});
}
},
callbackDoLogin: function (data, textStatus, jqXHR) {
$('#dialogContainer').dialog("close");
if (data.u != "") {
document.location.href = data.u;
}
}
};
loginPage.doAjax({
"url": '/REQUESTLOGINSTRINGS',
"requestType": 'GET',
"dataType": 'json',
"callbackMethod": loginPage.callbackGotStrings,
"data": {
}
});
$("#btnLogin").prop('disabled', true);
$("#chkTermsAndConditions").on("click", function () {
($('#chkTermsAndConditions').is(":checked")) ? $("#btnLogin").prop('disabled', false) : $("#btnLogin").prop('disabled', true);
});
$("#btnLogin").on("click", function (e) {
$("#loginFormMsg").hide();
if ($("#txtUserName").val() === loginPage.defaultUserName) {
$("#txtPassword").val("");
$("#passwordPlaceholder").val("Enter password");
$("#passwordPlaceholder").attr('readonly', true);
$("#txtPassword").attr('readonly', true);
}
else {
$("#passwordPlaceholder").attr('readonly', false);
$("#txtPassword").attr('readonly', false);
}
loginPage.username = $("#txtUserName").val();
loginPage.password = $("#txtPassword").val();
// IE8 won't support trim function, Hence we are using below mentioned alternate solution.
$.trim(loginPage.username);
$.trim(loginPage.password);
if ($("#txtUserName").val() === loginPage.defaultUserName) {
loginPage.doAjax({
"url": '/REQUESTLOGINPARAMETERS',
"requestType": 'GET',
"dataType": 'json',
"callbackMethod": loginPage.callbackDoLogin,
"errorMethod": loginPage.onError,
"data": {
"USR": loginPage.username
}
});
}
else if ((loginPage.username.length) && (loginPage.password.length)) {
loginPage.doAjax({
"url": '/REQUESTLOGINPARAMETERS',
"requestType": 'GET',
"dataType": 'json',
"callbackMethod": loginPage.callbackGetLoginParameters,
"errorMethod": loginPage.onError,
"data": {
"USR": loginPage.username
}
});
}
e.preventDefault();
});
});
function ShowErrorMsg(errorMsg) {
$("#loginFormMsg").show();
$("#errorMsg").remove();
$("#loginFormMsg").append("" + errorMsg + "");
}
//Below method will check whether the resource contains string or not.
//In case of failure this function will return "Resource not available" string.
function getResourceValue(resource) {
if ((resource == null) || (resource === undefined)) {
return "Resource not found";
}
else
return resource;
}