// This provides several functions used on the Log In page

function login() // This function saves cookies and then sends an AJAX call out to logmein.php
{
	var inputs = [];
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + 1);


	// Loop through the inputs, set them to cookies and put them into an array so we can send them to logmein.php
	
	$(":input",document.forms[0]).each(function() {
		// First we put them into the array as a pair, like "email=email@email.com"

		inputs.push(this.name + "=" + escape(this.value));


		// Then we toss the email into a cookie, so we can rewrite if the page needs to get reloaded

		if (this.type != "password" && this.name != "submit") document.cookie = this.name + "=" + escape(this.value) + "; expires=" + exdate.toGMTString() + "; path=/";
	});


	// And then we make an AJAX call with jQuery's .ajax() function, which you can read more about at jquery.com or in plannedpagefunctions.js/happenedpagefunctions.js
	// If the string we're returned contains "failed", we set a timer to reload the page, otherwise we redirect the user to the homepage, logged in

	$.ajax({
		data: inputs.join('&'),
		type: "POST",
		url: "pages/logmein.php",
		success: function(data) { document.forms[0].innerHTML = data; if (data.search("failed") > -1) setTimeout("window.location.reload(); cookies();",3000); else setTimeout("window.location.assign('index.php');", 3000); }});
}


function emailvalidate(str) // This checks the email string for email formatting
{
	var spans = document.forms[0].getElementsByTagName("span");

	if (str.length > 6  && !wordvalidate(str) && isemail(str) && str != "")
	{
		valid(spans,0);
	}
	else
	{
		invalid(spans,0);
		spans[0].innerHTML = "Invalid! Please input a real e-mail address!";
	}
}


function pwdvalidate(str) // This checks the length of the password
{
	var spans = document.forms[0].getElementsByTagName("span");
	var pwdspan = 1;

	if (str.length >= 8 && str.length <= 16)
	{
		valid(spans,pwdspan);
	}
	else
	{
		invalid(spans,pwdspan);
		spans[pwdspan].innerHTML = "Invalid! Please input an alphanumeric password between 8 and 16 characters long!";
	}
}

function checkpage() // This goes through all two fields, checking if they are valid, and enables/disables the submit button appropriately
{
	var spans = document.forms[0].getElementsByTagName("span");

	if ($(spans[0]).hasClass("valid") && $(spans[1]).hasClass("valid")) document.forms[0].submit.disabled = false;
	else document.forms[0].submit.disabled = true;
}

