// JavaScript Document

/******** LOGIN FUNCTIONS *********/
function loginfields_onclick(field, value)
{
	if (field.value == value)
	field.value = "";
}

function loginfields_onblur(field, value)
{
	if (field.value.length == 0)
		field.value = value;
}

function loginfields_onsubmit(form)
{
	if (form.Username.value == "Username" || form.Username.value == "" || form.Password.value == "")
		return false;
	return true;
}

/************* REGISTRATION FUNCTIONS ***************/
function register_onsubmit(form)
{
	if (form.reg_name.value.length == 0)
	{
		alert("Please enter your name");
		form.reg_name.focus();
		return false;
	}
	if (form.reg_email.value.length == 0)
	{
		alert("Please enter your email address");
		form.reg_email.focus();
		return false;
	}
	if (!verifyEmail(form.reg_email.value))
	{
		alert("Please enter a valid email address");
		form.reg_email.focus();
		return false;
	}
	if (form.reg_pw.value.length == 0 || form.reg_pw.value.length < 6 || form.reg_pw.value.length > 15)
	{
		alert("Please enter a password of 6-15 characters");
		form.reg_pw.focus();
		return false;
	}
	if (form.reg_repw.value.length == 0)
	{
		alert("Please re-enter your password");
		form.reg_repw.focus();
		return false;
	}
	if (form.reg_pw.value != form.reg_repw.value)
	{
		alert("Please enter the same password");
		form.reg_repw.focus();
		return false;
	}
	if (!form.agreement.checked)
	{
		alert("Please agree to the terms and conditions");
		form.agreement.focus();
		return false;
	}
	
	return true;
	
}

/*************** MISC *****************/
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=590,height=500,left = 345,top = 262');");
}

//Verify that a valid email address is entered
function verifyEmail(email)
{
  //List of known top-level domains that an e-mail address must end with.
  var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

  //The following string represents an atom (basically a series of non-special characters)
  var atom = "\[^\\s" + "\\(\\)><@,;:\\\\\\\"\\.\\[\\]" + "\]" + '+';

  //The following string represents one word in the typical username.
  var word = "(" + atom + "|" + "(\"[^\"]*\")" + ")";

  // The following pattern describes the structure of the user
  var userPattern = new RegExp("^" + word + "(\\." + word + ")*$");

  //The following pattern describes the structure of a normal symbolic domain.
//  , as opposed to ipDomainPat, shown above.
  var domainPatttern = new RegExp("^" + atom + "(\\." + atom +")*$");

  //Break up user@domain.
  var matchArray = email.match(/^(.+)@(.+)$/);

  //Address doesn't fit the general format of a valid e-mail address. eg. user@domain.com
  if (matchArray == null)
    return false;

  var user = matchArray[1];
  var domain = matchArray[2];

  // Start by checking that only basic ASCII characters are in the strings (0-127).
  for (i=0; i<user.length; i++) 
  {
    if (user.charCodeAt(i)>127 || domain.charCodeAt(i)>127) 
      return false;
  }

  //Check if "user" is valid 
  if (user.match(userPattern)==null) 
    return false;

  //Check if "domain" is valid.
  var atomPattern = new RegExp("^" + atom + "$");
  var domArray = domain.split(".");
  var domArrayLen = domArray.length;
  for (i=0 ; i<domArrayLen ; i++) 
  {
    if (domArray[i].search(atomPattern)==-1) 
      return false;
  }

  //Check to confirm domain name ends in a known top-level domain or a two-letter word representing country.
  if (domArray[domArray.length-1].length!=2 && domArray[domArray.length-1].search(knownDomsPat)==-1)
    return false;

  // Make sure there's a host name preceding the domain.
  if (domArrayLen<2)
    return false;

  return true;
}