$(document).ready(function(){

});


function update_account(url,target) {
    var update = "";
    var newval = "";
    if($("input[name$=nu_"+target+"]").val()=="") {
        
    } else {
        switch(target) {
            case 'first_name': update = 'firstname';
                newval = $("input[name$=nu_first_name]").val();
                break;
            case 'last_name': update = 'lastname';
                newval = $("input[name$=nu_last_name]").val();
                break;
        }
        $(":input").attr("disabled","disabled");
        $("#edit_msg").show();
        $("#edit_msg").attr("innerHTML","Updating Account...");
        $.post(url+"/edit_account/update_data",{
            field: update,
            value: newval
        },function(data){
            if(data=="ok") {
                $("#"+target).attr("innerHTML",$("input[name$=nu_"+target+"]").val());
                $("#edit_msg").attr("innerHTML","Field updated.");
            }
            else if(data=="not ok") {
                $("#edit_msg").attr("innerHTML","Unable to update field. Please try again.");
            } else {
                $("#edit_frm").attr("innerHTML","It seems you have been away for a while. Please log-in again. Redirecting...");
                window.location.replace(url);
            }
            $(":input").removeAttr("disabled");
            $(":text").val("");
            $("#edit_msg").fadeOut(3000);
        });
    }
}
function update_password(url) {
    if($("input[name$=cur_pass]").val()=="" || $("input[name$=new_pass]").val()=="") {
        $("#edit_msg").attr("innerHTML","Please enter your current password and new password");
    } else {
        $(":input").attr("disabled","disabled");
        $("#edit_msg").show();
        $("#edit_msg").attr("innerHTML","Updating Account...");
        $.post(url+"/edit_account/change_password",{
            cur_pass: $("input[name$=cur_pass]").val(),
            new_pass: $("input[name$=new_pass]").val()
        },function(data){
            if(data=='password not match') {
                $("#edit_msg").attr("innerHTML","Current Password incorrect.");
            }
            else if(data=='ok') {
                $("#edit_msg").attr("innerHTML","Password changed.");
            } else {
                $("#edit_frm").attr("innerHTML","It seems you have been away for a while. Please log-in again. Redirecting...");
                window.location.replace(url);
            }
            $(":input").removeAttr("disabled");
            $(":password").val("");
            $("#edit_msg").fadeOut(3000);
        });
    }
}
