$(document).ready(function() {

	$('form#adduserform').submit(function() {
		var status = addUserValidate($('#adduserform'));
		return status;
	});

	if ($('form').attr('name') == 'seasonpredictionsform') {
		$('form').submit(function() {
			var status = addSeasonPredictionsValidate();
			return status;
		});
	}

	//javascript to enable second level menus
	$("#nav a").hover( function() { 
		var menuitem = this.id;
		var secondlevelmenuitem = $("#nav-2 #"+menuitem);
		var sec = document.getElementById('nav-2');
		$("#nav-2 div").hide();
		secondlevelmenuitem.show();
		$('#logout').show();
	});

});

function addUserValidate() {
	var errorstring = '';
	var status = true;
	if ($('#name').val() == '')
	{
		errorstring += 'You must enter your name. <br />';
		status = false;
	}

	if ($('#email').val() == '')
	{
		errorstring += 'You must enter an email address. <br />';
		status = false;
	}

	if ($('#password').val() == '')
	{
		errorstring += 'You must enter a password. <br />';
		status = false;
	}

	if ($('#teamsupported').val() == '')
	{
		errorstring += 'You must enter a team. If you do not support anyone, put N/A.<br />';
		status = false;
	}
	
	if (status == true)
	{
		var email = $('#email').val();
		var url = 'ajaxfunctions.php?email='+email;

		var XMLresponse = $.ajax({
				type: "GET",
				url: unescape(url),
				async: false
				}).responseText;

		if (XMLresponse == 0)
		{
			return true;
		} else {
			errorstring += 'This email address is already registered in the database.';
		}
	}
	$('#errortext').html(errorstring);
	return false;
}

function addSeasonPredictionsValidate() {
	var errorstring = '';
	var status = true;
	if ($('#champions').val() == '')
	{
		errorstring += 'You must enter your prediction for who will be champions. <br />';
		status = false;
	}

	if ($('#runnerup').val() == '')
	{
		errorstring += 'You must enter your prediction for who will be the premiership runner up. <br />';
		status = false;
	}

	if ($('#facup').val() == '')
	{
		errorstring += 'You must enter your prediction for who will win the FA Cup. <br />';
		status = false;
	}

	if ($('#leaguecup').val() == '')
	{
		errorstring += 'You must enter your prediction for who will win the Carling Cup. <br />';
		status = false;
	}

	if ($('#topscorer').val() == '')
	{
		errorstring += 'You must enter your prediction for who be top scorer in the league. <br />';
		status = false;
	}

	if ($('#bottom').val() == '')
	{
		errorstring += 'You must enter your prediction for who will come bottom of the premiership. <br />';
		status = false;
	}

	if ($('#nineteenth').val() == '')
	{
		errorstring += 'You must enter your prediction for who will come 19th and get relegated. <br />';
		status = false;
	}

	if ($('#eighteenth').val() == '')
	{
		errorstring += 'You must enter your prediction for who will come 18th and get relegated. <br />';
		status = false;
	}

	if (status == true)
	{
		return true;
	} else {
		$('#errortext').html(errorstring);
		return false;
	}
}


function addFixtureFields() {
	var noofgames = $('#numberofgames').val();
	i = 1;
	while (i <= noofgames)
	{
		var label =  '<label for="fixture'+i+'">Fixture '+i+': </label>';
		var gameinput = '<input type="text" size="30" id="fixture'+i+'" name="fixture'+i+'" /><br />';
		if (i == 1)
		{
			$(gameinput).insertAfter('#numberofgames');
		} else {
			$(gameinput).insertAfter(previousgameinput);
		}
		var previousgameinput = '#fixture'+i;
		$(label).insertBefore(previousgameinput);
		i++;
	}
}

function logout() {
	url = 'logout.php';
	
	var XMLresponse = $.ajax({
				type: "POST",
				url: unescape(url),
				async: false
				}).responseText;

	window.location.reload();
	var loginbox = $('#loginbox');
	$('#loginbox').html(XMLresponse);
	
}