﻿var rUrl = "";
var email = "";
var reset = false;

$(document).ready(function() {
    validateChangePwd();
    rUrl = getKeyFromQueryStr("r");
    email = getKeyFromQueryStr("e");
    reset = getKeyFromQueryStr("reset") == 1;
    if (email.length > 0)
        $('#cpwd_userName').val(email);
    if (reset)
        $('#old_pwd').html('Temporary Password');
})
function validateChangePwd() {
    $('#form_cpwd').validate({
        messages: {
            cpwd_new_password2: {
                required: " ",
                equalTo: "Please enter the same password as above"
            }
        },
        rules: {
            cpwd_new_password: {
                required: true,
                minlength: 4
            }
        },
        hightlight: function(e) {
            $(e).addClass("error");
        },
        unhighlight: function(e) {
            $(e).removeClass("error");
        },
        onkeyup: false,
        submitHandler: function() {
            changePwd();
        }
    });
}
function changePwd() {
    var email = $('#cpwd_userName').val();
    var oldPwd = $('#cpwd_old_password').val();
    var newPwd = $('#cpwd_new_password').val();
    $.ajax({
        type: "POST",
        url: ws_auth + "/changePwd",
        data: '{ "email":"' + email + '","oldpwd": "' + oldPwd + '","newpwd": "' + newPwd + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $('#msg').html(msg.d.message);
            if (msg.d.message.indexOf("successfully") > 0) {
                $('#msg').css("color", "#264409");
                if (rUrl.length > 0)
                    window.location.href = rUrl;
            }
            $('#msg').show();
        }
    });
}