var register = {
	verify: function() {
		if( $("input[name='password']").val() == $("input[name='confirm']").val() ) { return 1; } else { alert("Passwords do not match."); return false; }
	},
	
	random: function() {
		var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var string_length = 8;
		var randomstring = '';
		for (var i=0; i<string_length; i++) {
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum,rnum+1);
		}
		$("#randPassword").html( randomstring );
		$("input[name='password'],input[name='confirm']").val( randomstring );
	},
	
	clear: function() {
		$("input").val( "" );
	}
}

var account = {
	init: function() {
		account.bind();
		jQuery("input").blur();
		scroll( 0, 0 );
		$("input[name='birthday']:not(.ignore)").datepicker({ changeYear: true, yearRange: "-99:+99", dateFormat: "yy-mm-dd", maxDate: "+0d", minDate: "-99y" });
	},
	
	edit: function(e) {
		var f = $(e).attr("id").replace( /edit/, "" );
		$("#editText" + f).hide();
		$("#field" + f).show();
	},
	
	save: function() { $("#account").html( "Saving changes..." ); return true; },
	
	filter: function() {
		$("#results").html( "Filtering users..." );
		ajax.link( "", "", "#results", "ajax/filter.php", "", "" );
	},
	
	printer: function() {
		$("#results").html( "Loading..." );
		ajax.link( '', 'printer=yes', '#results', 'ajax/filter.php', '', '' );
	},
	
	downloadUsers: function() {
		$("#printer").attr( "src", "ajax/download.php" );
	},
	
	addFamily: function() { if( $("input[name='addFamily']").val() == "" ) { alert( "Family name field is empty." ); return false; } else { return true; } },

	bind: function() {
		jQuery(".fieldToEdit > input").focus().bind( "keyup", function(e) {
			var c = ( e.keyCode ? e.keyCode : e.which );
			if( c == 13 ) { ajax.link( '', 'uid=' + $("#uid").val(), '#account', 'ajax/save.php', 'account.save()', '' ); }
		});
		
		$(".login").focus().bind( "keyup", function(e) {
			var c = ( e.keyCode ? e.keyCode : e.which );
			if( c == 13 ) { ajax.link( '', '', '#loginResults', 'ajax/login.php', '', '' ); }
		});
		
		$(".attr > input, .attrS > input, .attrL > input").keyup( function() { account.filter(); });
		$(".attr > select, .attrS > select, .attrL > select, .admin > select").change( function() { account.filter(); });
		$(".editField:not(.ignore)").click( function() { account.edit(this); });
		$("select[name='member']").change( function() { if( $(this).val() == 4 ) { $("#family").show(); } else { $("#family").hide(); } });
		
		//Ajax upload stuff
		if( $("#newImage").length > 0 ) {
			new AjaxUpload('#newImage', {
				action: 'ajax/newPhoto.php?id=' + $("#uid").val(),
				name: 'photo',
				autoSubmit: true,
				onSubmit: function(file, extension) { $("#image").html( "Updating photo." ); },
				onComplete: function(file, response) { $("#image").html( response ); }
			});
		}
	}
}

var user = {
	confirmDelete: function( i ) {
		if( i != 1 ) {
			var r = confirm("Are you sure you want to delete this user?");
			if( r === true ) {
				$("#results").html( "Deleting user..." );
				ajax.link( "", "delete=yes&deleteId=" + i, "#results", "ajax/filter.php", "", "" );
			}
		} else { alert( "Can't delete admin account!" ); }
	},
	
	getTo: function() { return ajax.safe( $("#sendTo").html() ); },
	
	confirmSend: function() {
		var r = confirm("Send mass email?");
		if( r === true ) {
			ajax.link( "", "sendEmail=yes", "#results", "ajax/filter.php", "user.loadingSend()", "" );
		}
	},
	
	loadingSend: function() {
		$("#results").html( "Sending mass email..." );
		return true;
	},
	
	showEmail: function() {
		$("#massEmail").show();
	},
	
	hideEmail: function() {
		$("#massEmail").hide();
	}
}

$(document).ready( function() { account.init(); });