/**
 *
 */
function alertIfEmpty(value, message)
{
  if (value=="") {
    alert(message);
    return true;
  }
  else
    return false;
}

function validateApplyForm (self)
{
  var f = document.signupForm;
 
  var alertedForMissingField = 
    alertIfEmpty (f.firstName.value, "Please enter your first name") ||
    alertIfEmpty (f.lastName.value, "Please enter your last name") ||
    alertIfEmpty (f.email.value, "Please enter your email address") ||
    alertIfEmpty (f.phone.value, "Please enter your phone number") ||
    alertIfEmpty (f.country.value, "Please enter your country") ||
    alertIfEmpty (f.citizenship.value, "Please enter your citizenship") ||
    alertIfEmpty (f.occupation.value, "Please enter your occupation") ||
    alertIfEmpty (f.password.value, "Please choose a password") ||
    alertIfEmpty (f.confirmPassword.value, "Please confirm your password") ||
    false;

  // Note: city and prov are optional

  if (alertedForMissingField)
    return false;

  // --- Validate email address
  var atIndex = f.email.value.indexOf('@');
  var lastDotIndex = f.email.value.lastIndexOf('.');
  if (atIndex == -1 || lastDotIndex == -1 || atIndex > lastDotIndex) {
    alert("Email address is invalid - please re-enter");
    return false;
  }

  // --- Passwords must match
  if (f.password.value != f.confirmPassword.value) {
    alert("The two passwords do not match - please re-enter.");
    return false;
  }

  return true;
}

function logout () {
  eraseCookie ("signupId");
  eraseCookie ("lastApply");
  window.location = "/";
}


// Cookie functions from: http://www.quirksmode.org/js/cookies.html

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

