global_lock = true;
passlist = []
unlock_key = null;
$(document).ready(function() {
	$("#newrecord").hide();
	$("#passwdlist").hide();
	$("#control").hide();
	update_lock_status();
	get_unlock_key();
	get_acc_types();
})

function flash(msg) {
	$("#flash").html(msg);
}


function get_acc_types() {
	$.getJSON("getacctypes.php", function (data) {
			$.each(data, function (i, item) {
				$("<option>").val(item.id).html(item.acc_type).appendTo("#acctype_sel");
			})
		}
	) 
}

function update_lock_status() {
	var txt = global_lock ? "locked" : "unlocked";
	$("#lockstatus").text(txt)
}

function unlock() {
	if (unlock_key == null) {
		flash("some how i didn't get the unlock key.");
		return;
	}
	if (hex_md5($("#deckeybox").attr("value")) == unlock_key) {
		global_lock = false;
		$("#passwdlist").show();
		$("#control").show();
		$("#deccontrol").hide();
		update_lock_status();
		get_passwd_list();
	} else {
		flash("couldn't unlock");
		global_lock = true;
	}
}

function get_unlock_key() {
	$.getJSON("getunlockkey.php", 
		function(data){
			unlock_key = data
		}
	)	
}

function get_passwd_list() {
	reset_forms();
	$.getJSON("getpwlist.php", 
		function(data){
			passlist = data
			render_pw_list();
		}
	)
}

function render_pw_list() {
	$("#passwdlist").empty()
	$.each(passlist, function(i,item) {

		$("<li class='pwitem'>").html(pw_list_row(item)).appendTo("#passwdlist");
	})
}

function pw_list_row(item) {
	str = "";
	str += "<a href='#' onclick='display_pw("+item.id+")'>"+item.title+"</a><span class='acctype'>"+item.acc_type+"</span>";
	str += "<span>"+item.create_ts+"</span>";
	str += "<div id='passwddetail_"+item.id+"' style='display: none'>"
	str += "<div>username: "+item.username+"</div>"
	str += "<div>password 1: "+decrypt(item.password1)+"</div>"
	str += "<div>password 2: "+decrypt(item.password2)+"</div>"
	str += "<div>notes: "+item.notes+"</div>"
	str += "<input type='button' value='edit' onclick=edit("+item.id+") />"
	str += "</div>";
	
	return str;
}

function display_pw(id) {

	if($("#passwddetail_"+id).css('display') == "none") {
		$("#passwddetail_"+id).show();
	} else {
		$("#passwddetail_"+id).hide();
	}
}

function encrypt(text) {
            if (global_lock) {
                return false ;
            }
            SpiderCrypt.keyReset();
            SpiderCrypt.userKey = $("#deckeybox").attr("value");
            SpiderCrypt.plainText = text
            SpiderCrypt.initSpiderKey();
            SpiderCrypt.initSubKey();
            SpiderCrypt.encrypt();
            SpiderCrypt.formatCipherCode();
            return SpiderCrypt.cipherCodeNL;
}
function decrypt(text) {
            SpiderCrypt.keyReset();
            SpiderCrypt.userKey = $("#deckeybox").attr("value");
            SpiderCrypt.cipherCode= text
            SpiderCrypt.initSpiderKey();
            SpiderCrypt.initSubKey();
            if (SpiderCrypt.decrypt()){
                SpiderCrypt.decodePlain();
                return SpiderCrypt.plainText;
            } else {
                return "Error in decryption";
            }
}

function reset_forms() {
	$(".newacc").val('');
}

function submit_new_account() {
	if (global_lock) {
		flash("Unlock to submit");
		return;
	}
	var passwd1 = encrypt($("#password1").val());
	var passwd2 = encrypt($("#password2").val());
	$.post("newaccount.php", 
		{
			title: $("#title").val(),
			username: $("#username").val(),
			password1: passwd1,
			password2: passwd2,
			acctype: $("#acctype_sel").val(),
			notes: $("#notes").val(),
			edit_id: $("#edit_id").val()
		}, function(data) {
			get_passwd_list();	
			$("#newrecord").hide();
			$("#passwdlist").show();
		}
	);
}


function edit(id) {
	var current_item = null
	for (i = 0; i<passlist.length; i++) {
		if (id == passlist[i].id) {
			current_item = passlist[i];
			break;
		}
 	}
	if (current_item == null) {
		flash("could not find that id to edit!?");
		return;
	}
	$("#title").val(current_item.title);
	$("#username").val(current_item.username);
	$("#password1").val(decrypt(current_item.password1));
	$("#password2").val(decrypt(current_item.password2));
	$("#notes").val(current_item.notes);
	$("#edit_id").val(current_item.id);
	$("#newrecord").show();
	$("#passlist").hide();	
}
