var URL = window.location.href;
URL = URL.replace('?Code=', '');
var orgId = URL.substr(URL.indexOf('=') + 1);
URL = URL.replace('&OrgId=' + orgId + '', '');
URL = URL.replace(fparams(), '');
$(document).ready(function () {
    LoadingMessage('Checking Details', '<b>Please Wait ...</b>');
    if (fparams() == '' || fparams() == undefined) {
        LoadingMessage('You have not changed password code, we are redirecting to login page', '<b>Please Wait ...</b>');
        window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx?OrgGroupId=' + $('#OrgGroupIdHiddenField').val(); }, 1500);
    }
    else {
        ValidateCodeDetails();
    }
});
function LoadingMessage(msg1, msg2) {
    $('#divLoadingmsg').show();
    $('#msg1').html(msg1);
    $('#msg2').html(msg2);
    $('.hls-bg').show();
}
function CloseHls() {
    $('.hls-bg').hide();
    $('.hls-wrap').hide();
}
function fparams() {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        return pair[1];
    }
}
function ValidateCodeDetails() {
    $.ajax({
        type: "POST",
        url: URL + "/ValidateCodeDetails",
        data: '{"TemporaryCode":"' + fparams() + '"}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.d == 0) {
                LoadingMessage('Your password code is wrong, we are redirecting to login page', '<b>Please Wait ...</b>');
                window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx?OrgGroupId=' + $('#OrgGroupIdHiddenField').val(); }, 1500);
            }
            else {
                window.setTimeout(function () { CloseHls(); }, 500);
            }
        },
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}
function ValidatePasswordDetails() {
    var fields = '';
    var Title = 'Please fill the following details ___________________ \n\n';
    if ($('#txtPassword').val().trim() == '') {
        fields = fields + 'Password \n';
    }
    if ($('#txtConfirmPassword').val().trim() == '') {
        fields = fields + 'Confirm Password \n';
    }
    if ($('#txtPassword').val().trim() != '' && $('#txtConfirmPassword').val().trim() != '') {
        if ($('#txtPassword').val().trim() != $('#txtConfirmPassword').val().trim()) {
            fields = fields + 'Password does not match \n';
        }
    }
    if (fields.length > 0) {
        alert(Title + fields);
        return false;
    }
    else {
        ChangePassword();
    }
}
function ChangePassword() {
    $.ajax({
        type: "POST",
        url: URL + "/ChangePassword",
        data: '{"TemporaryCode":"' + fparams() + '","Password":"' + $('#txtPassword').val().trim() + '","OrgId":"' + orgId + '"}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.d == 1) {
                LoadingMessage('Password changed.', '<b>Please Wait ...</b>');
                $("#lblSuccessCode").html("Password changed.");
                $("#lblSuccessCode").css("display", "");
                window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx?OrgGroupId=' + $('#OrgGroupIdHiddenField').val(); }, 1500);
            }
            else {
                $("#lblSuccessCode").css("display", "none");
                alert(data.d);
                CloseHls();
            }
        },
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}