// This file contains several functions used in the registration process to validate information on the client-side

function register() // This puts some information into cookies and makes an AJAX call to registerme.php
{
	var inputs = [];
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + 1);


	// We go through the inputs on the page, save them as cookies, and put them into an array to use for the AJAX call
	
	$(":input",document.forms[0]).each(function() {
		// First we put the information into an array in the format of "fieldname=fieldvalue"

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


		// Then we make cookies for each of the pieces of data

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


	// And now we make an AJAX call using jQuery's .ajax() function, which is a handy little function you can read about on jquery.com or
	// in happenedpagefunctions.js/plannedpagefunctions.js
	// If the string returned to us contains "failed" in it, we set a timer and reload the page (filling in the fields from our cookies),
	// and if it doesn't, we direct the user to the homepage

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


function instvalidate(str) // Here we validate the institution name
{
	var spans = document.forms[0].getElementsByTagName("span");

	if (str != "" && !wordvalidate(str))
	{
		valid(spans,0);
	}
	else
	{
		invalid(spans,0);
		spans[0].innerHTML = "Invalid! Please use only alphanumeric characters!";
	}
}


function namevalidate(str) // Validating the name field
{
	var spans = document.forms[0].getElementsByTagName("span");

	if (str != "" && !wordvalidate(str))
	{
		valid(spans,1);
	}
	else
	{
		invalid(spans,1);
		spans[1].innerHTML = "Invalid! Please use only alphanumeric characters!";
	}
}


function emailvalidate(str) // Making sure the email field is formatted properly
{
	var spans = document.forms[0].getElementsByTagName("span");

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


function phonevalidate(str) // Checking the phone number
{
	var spans = document.forms[0].getElementsByTagName("span");

	var i;
	var newstr = "";


	// Here we go through the string, putting every character that isn't (, ), ., or - into a new string

	for (i = 0; i <= str.length; i++)
	{
		if (str.charAt(i) != "(" && str.charAt(i) != ")" && str.charAt(i) != "-" && str.charAt(i) != ".")
		{
			newstr += str.charAt(i);
		}
	}


	// That new string is put into the phone field

	document.forms[0].phone.value = newstr;


	// And then we check if it is a number and validate

	if (!isNaN(newstr))
	{
		valid(spans,3);
	}
	else
	{
		invalid(spans,3);
		spans[3].innerHTML = "Invalid! Please input a real phone number!";
	}
}


function pwdvalidate(str) // Checks to see if the password is the right length
{
	var spans = document.forms[0].getElementsByTagName("span");
	var pwdspan = 4;

	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() // Goes through each of the fields and enables the submit button if they are all valid
{
	var spans = document.forms[0].getElementsByTagName("span");

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

