$(document).ready(function()
{
	jQuery(".dialog").dialog({
	  modal: true, height: 370, width: 350, autoOpen: false
	});
	if (typeof categories != "undefined")
	{
		loadMemberPage();
	}
});
function btnRegisterClicked() {
	jQuery(".dialog").dialog('open');
}
function submitRegistration()
{
	var error = "";
	if ($("#txtRegisterUsername").val() == "")
	{
		error += "<p>Username Required.</p>";
	}
	if ($("#txtRegisterPassword").val() == "")
	{
		error += "<p>Password Required.</p>";
	}
	if ($("#txtRegisterRepeatPassword").val() == "")
	{
		error += "<p>Please Confirm Password.</p>";
	}
	if ($("#txtRegisterPassword").val() != $("#txtRegisterRepeatPassword").val())
	{
		error += "<p>Password and Confirmation Password Must Match</p>";
	}
	if ($("#txtRegisterFirstName").val() == "")
	{
		error += "<p>Please provide first name.  We don't care if its real, we just want something to call you.</p>";
	}
	if ($("#txtRegisterLastName").val() == "")
	{
		error += "<p>Please provide last name.  We don't care if its real, we just want something to call you.</p>";
	}
	if ($('#txtRegisterEMail').val() == "")
	{
		error += "<p>Please provide valid email.  This will be used to verify account.</p>";
	}
	if (error == "")
	{
	$.ajax({
		url: "ajax.index.php",
		type: "POST",
		data: {
			action: "register",
			username: $("#txtRegisterUsername").val(),
			password: $("#txtRegisterPassword").val(),
			repeat: $("#txtRegisterRepeatPassword").val(),
			firstName: $("#txtRegisterFirstName").val(),
			lastName: $("#txtRegisterLastName").val(),
			email: $("#txtRegisterEMail").val()			
		},
		dataType: "json",
		success: function(selector) {
			console.dir(selector);
			switch (selector.results)
			{
				case "EXISTS":
					$('#errors').html("<p>This username is already in use.</p>");
					break;
				case "register":
					jQuery(".dialog").html("An email has been sent to verify this account.");
					//jQuery(".dialog").dialog('close');
					break;
				default:
					break;
			}
		},
		error: function(results) {
		
		}
	});
	}
	else
	{
		$('#errors').html(error);
	}
}
function login()
{
	$.ajax({
		url: "ajax.index.php",
		type: "POST",
		data: {
			action: "authenticate",
			username: $("#txtUsername").val(),
			password: $("#txtPassword").val()
		},
		dataType: "json",
		success: function(json) {
			if (json.results == "Valid")
			{
				location.href = "member.php";
			}
			else if(json.results == "Invalid")
			{
				$("#txtLoginError").html("<p>Invalid username/password combination.</p>");
			}
			else if(json.results = "Inactive")
			{
				$("#txtLoginError").html("<p>This account is currently inactive.  If you just registered, please check your e-mail and follow the instructions provided.  If you don't see the e-mail, check your junk e-mail account.</p>");
			}
		},
		error: function(error) {
		
		}
	});
	
}
function loadMemberPage()
{
	// May want to use this combo box instead of spinners... http://www.fairwaytech.com/Technology/FlexBox.aspx
	var catData = [];
	jQuery.each(categories, function(i, val) {
		catData[catData.length] = val.name;
		
	});

	var options = { 
		lookup: catData,
		onSelect: categorySpinnerChanged
	};
	$('#categorySpinner').autocomplete(options);
	
	options = {
		serviceUrl: 'ajax.index.php',
		params: {action: 'getUserNames'}
	};
	$('#txtEventName').autocomplete(options);
	//$('#eventDate').datepicker();
	$('#eventDate').datetime({
		userLang	: 'en',
		americanMode: true
	});
	$('#statusDate').datetime({
		userLang : 'en',
		americanMode: true
	});
	var options = {autoHeight: true};
	$('#mainAccordion').accordion();
	
}
function categorySpinnerChanged(value, data)
{
	var category = $('#categorySpinner').val();
	var div = $('#eventFields');
	
	jQuery.each(categories, function(i, val) {
		if (val.name == category)
		{
			var html = '';
			jQuery.each(val.fields, function(k, v) {
				html += '<tr><td>' + v + '</td><td><input class="roundedInputsEvent" type="text" style="width: auto;"/></td></tr>';
			});
			div.html(html);
		}
	});
	
}
function submitEvent()
{
	// Process fields.
	var fields = [];
	var div = $('#eventFields');
	jQuery.each(div[0].rows, function(i, val) {
		if (!val.cells[0].childNodes[0].tagName)
		{
			fields[fields.length] = {name: val.cells[0].innerHTML, value: val.cells[1].children[0].value};
		}
		else
		{
			fields[fields.length] = {name: val.cells[0].childNodes[0].value, value: val.cells[1].children[0].value};
		}
	});
	console.dir(fields);
	$.ajax({
		url: "ajax.index.php",
		type: "POST",
		data: {
			action: "submitEvent",
			dateTime: $('#eventDate').val(),
			category: $('#categorySpinner').val(),
			eventName: $('#txtEventName').val(),
			notes: $('#taNotes').val(),
			tags: $('#taTags').val(),
			fields: JSON.stringify(fields)
		},
		dataType: "json",
		success: function(json) {
			console.dir(json);
		},
		error: function(error) {
		
		}
	});
}
function submitStatus()
{
	console.log('here');
	$.ajax({
		url: "ajax.index.php",
		type: "POST",
		data: {
			action: "submitStatus",
			dateTime: $('#statusDate').val(),
			name: $('#txtStatusName').val(),
			value: $('#txtStatusValue').val(),
			scale: $('#txtStatusScale').val()
		},
		dataType: "json",
		success: function(json) {
			console.dir(json);
		},
		error: function(error) {
		
		}
	});
	
}
function addNewField()
{
	var div = $('#eventFields');
	var html = div.html() + '<tr><td><input type="text" /></td><td><input class="roundedInputsEvent" type="text" style="width: auto;"/></td></tr>';
	div.html(html);
}
function showGraphCalories()
{
	var days = $('#txtDays').val();
	$.ajax({
		url: "ajax.index.php",
		type: "POST",
		data: {
			action: "graphCalories",
			days: days
		},
		dataType: "json",
		success: function(json) {
			var div = $('#graphArea');
			div.empty();
			div.append($('<img />').attr('src', json.results));

		},
		error: function(error) {
		
		}
	});
	
}
function showGraphSleep()
{
	var days = $('#txtDays').val();
	$.ajax({
		url: "ajax.index.php",
		type: "POST",
		data: {
			action: "graphSleep",
			days: days
		},
		dataType: "json",
		success: function(json) {
			var div = $('#graphArea');
			div.empty();
			div.append($('<img />').attr('src', json.results));

		},
		error: function(error) {
		
		}
	});
	
}
function showGraphWeight()
{
	var days = $('#txtDays').val();
	$.ajax({
		url: "ajax.index.php",
		type: "POST",
		data: {
			action: "graphWeight",
			days: days
		},
		dataType: "json",
		success: function(json) {
			var div = $('#graphArea');
			div.empty();
			div.append($('<img />').attr('src', json.results));

		},
		error: function(error) {
		
		}
	});
	
}