/*
	Resources:
	1. The code to validate the email address [function validateEmail()] was borrowed from the book: 
	PHP and MySQL Web Development Second Edition by Luke Welling and Laura Thompson
*/

//function to check if email is correct
function validateEmail(addr,db) 
{
	if (addr == "") 
	{
  		return false;
	}
	
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) 
	{
   		if (addr.indexOf(invalidChars.charAt(i),0) > -1) 
   		{
      		if (db) 
      			return false;
   		}
	}
	for (i=0; i<addr.length; i++) 
	{
   		if (addr.charCodeAt(i)>127) 
   		{
      		if (db) 
      			return false;
   		}
	}
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) 
	{
   		if (db) 
   			return false;
	}
	if (atPos == 0) 
	{
   		if (db) 
   			return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) 
	{
   		if (db) 
   			return false;
	}
	if (addr.indexOf('.', atPos) == -1) 
	{
   		if (db)
   			return false;
	}
	if (addr.indexOf('@.',0) != -1) 
	{
   		if (db) 
   			return false;
	}
	if (addr.indexOf('.@',0) != -1)
	{
   		if (db) 
   			return false;
	}
	if (addr.indexOf('..',0) != -1) 
	{
   		if (db) 
   			return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != "com" && suffix != "net" && suffix != "org" && suffix != "edu" && suffix != "int" && suffix != "mil" && suffix != "gov" & suffix != "arpa" && suffix != "biz" && suffix != "aero" && suffix != "name" && suffix != "coop" && suffix != "info" && suffix != "pro" && suffix != "museum") 
	{
   		if (db) 
   			return false;
	}
return true;
}

// funtion to validate the volunteer website form
function ValidateVolunteerForm(volunteerform) 
{ 
  var fieldsmissing = "";
  var maxallowed = "";

  if (volunteerform.volunteerName.value == "") { fieldsmissing += "Name\n"; }
  if (volunteerform.volunteerHelp.value == "") { fieldsmissing += "How can you contribute to MSUG\n"; }

  var yourEmail = volunteerform.volunteerEmail.value;
  if (!validateEmail(yourEmail,1)) 
  {
	fieldsmissing += "Email Address\n";
  } 

  //check the maximum date imput lenght allowed
  if(volunteerform.volunteerName.value.length > 80) { maxallowed +="Name\n"; }
  if(volunteerform.volunteerHelp.value.length > 500) { maxallowed +="How can you contribute to MSUG\n"; }
  if(volunteerform.volunteerEmail.value.length > 55) { maxallowed +="Email Address\n"; }

  if (fieldsmissing != "") 
  {
    // open the alert window to display fields which are missing  
	alert("You have either missed the following fields or you have typed incorrect values:\n"
       + fieldsmissing
       + "Please complete and resubmit.")
    return false
  } 
  if (maxallowed != "") 
  {
    // open the alert window to display fields which are missing  
	alert("You have reached the max characters allowed in the following fields:\n"
       + maxallowed
       + "Please complete and resubmit.")
    return false
  } 
  if(confirm("Following is the information provided by you\n\n" +
			"Name = " + volunteerform.volunteerName.value + "\n" + 
			"How can yo contribute to MSUG  = " + volunteerform.volunteerHelp.value + "\n" +
	        "Email address = " + volunteerform.volunteerEmail.value + "\n" +
		    "\n\n Continue?"))
  {
	return true;	
  }
  else
  {
	return false;
  }
}

// funtion to validate the contact us form
function ValidateContactForm(contactform) 
{ 
  var fieldsmissing = "";
  var maxallowed = "";

  if (contactform.contactName.value == "") { fieldsmissing += "Name\n"; }
  if (contactform.contactMessage.value == "") { fieldsmissing += "Message\n"; }

  var yourEmail = contactform.contactEmail.value;
  if (!validateEmail(yourEmail,1)) 
  {
	fieldsmissing += "Email Address\n";
  } 

  //check the maximum date imput lenght allowed
  if(contactform.contactName.value.length > 80) { maxallowed +="Name\n"; }
  if(contactform.contactMessage.value.length > 500) { maxallowed +="Message\n"; }
  if(contactform.contactEmail.value.length > 55) { maxallowed +="Email Address\n"; }

  if (fieldsmissing != "") 
  {
    // open the alert window to display fields which are missing  
	alert("You have either missed the following fields or you have typed incorrect values:\n"
       + fieldsmissing
       + "Please complete and resubmit.")
    return false
  } 
  if (maxallowed != "") 
  {
    // open the alert window to display fields which are missing  
	alert("You have reached the max characters allowed in the following fields:\n"
       + maxallowed
       + "Please complete and resubmit.")
    return false
  } 
  if(confirm("Following is the information provided by you\n\n" +
			"Name = " + contactform.contactName.value + "\n" + 
			"Message  = " + contactform.contactMessage.value + "\n" +
	        "Email address = " + contactform.contactEmail.value + "\n" +
		    "\n\n Continue?"))
  {
	return true;	
  }
  else
  {
	return false;
  }
}

// funtion to validate the recomment website form
function ValidateRecommendForm(recform) 
{ 
  var fieldsmissing = "";
  var maxallowed = "";

  if (recform.recommendName.value == "") { fieldsmissing += "Your Name\n"; }
  if (recform.recName.value == "") { fieldsmissing += "Recipients Name\n"; }

  var yourEmail = recform.recommendEmail.value;
  if (!validateEmail(yourEmail,1)) 
  {
	fieldsmissing += "Your Email Address\n";
  } 

  var recEmail = recform.recEmail.value;
  if (!validateEmail(recEmail,1)) 
  {
	fieldsmissing += "Recipients Email Address\n";
  } 

  //check the maximum date imput lenght allowed
  if(recform.recommendName.value.length > 80) { maxallowed +="Your Name\n"; }
  if(recform.recName.value.length > 80) { maxallowed +="Recipients Name\n"; }
  if(recform.recommendEmail.value.length > 55) { maxallowed +="Your Email Address\n"; }
  if(recform.recEmail.value.length > 55) { maxallowed +="Recipients Email Address\n"; }

  if (fieldsmissing != "") 
  {
    // open the alert window to display fields which are missing  
	alert("You have either missed the following fields or you have typed incorrect values:\n"
       + fieldsmissing
       + "Please complete and resubmit.")
    return false
  } 
  if (maxallowed != "") 
  {
    // open the alert window to display fields which are missing  
	alert("You have reached the max characters allowed in the following fields:\n"
       + maxallowed
       + "Please complete and resubmit.")
    return false
  }
  if(confirm("Following is the information provided by you\n\n" +
			"Your Name = " + recform.recommendName.value + "\n" + 
			"Recipients Name = " + recform.recName.value + "\n" +
			"Your Email address = " + recform.recommendEmail.value + "\n" +
			"Recipients Email address = " + recform.recEmail.value + "\n" +
		    "\n\n Continue?"))
  {
	return true;	
  }
  else
  {
	return false;
  }
}

// funtion to validate the new member form
function ValidateNewMemberForm(form) 
{ 
  var fieldsmissing = "";
  var maxallowed = ""
  var invalid_char_quote = "";
  var char_missing = "";

  var my_forename = form.registerForename.value;
  var my_surname = form.registerSurname.value;
  var my_email = form.registerEmail.value;
  var my_gender  = form.registerGender.selectedIndex;
  var my_gender_text = form.registerGender.options[my_gender].value;
  var my_home_tel = form.registgerTelephone.value;
  var my_mob_tel = form.registerMobile.value;
  var my_post_code = form.registerPostcode.value;
  var my_userid = form.registerUserID.value;
  var my_password = form.registerPassword.value;

  //check whether the following information is provided or not
  if (my_forename == "") { fieldsmissing += "Forename\n"; }
  if (my_surname == "") { fieldsmissing += "Surname\n"; }
  if (my_gender == 0) { fieldsmissing += "Gender\n"; }
  if (!validateEmail(my_email,1)) { fieldsmissing += "Email Address\n"; }
  if (my_userid == "") { fieldsmissing += "User ID\n"; }
  if (my_password == "") { fieldsmissing += "Password\n"; }

  if(my_forename.indexOf("'") != -1)
  {
	invalid_char_quote += "Forename\n";
  }
  if(my_surname.indexOf("'") != -1)
  {
    invalid_char_quote += "Surname\n";
  }
  if(my_email.indexOf("'") != -1)
  {
    invalid_char_quote += "Email\n";
  }
  if(my_userid.indexOf("'") != -1)
  {
    invalid_char_quote += "User id\n";
  }
  if(my_password.indexOf("'") != -1)
  {
    invalid_char_quote += "Password\n";
  }
  if(my_home_tel.indexOf("'") != -1)
  {
    invalid_char_quote += "Home phone\n";
  }
  if(my_mob_tel.indexOf("'") != -1)
  {
    invalid_char_quote += "Mobile phone\n";
  }
  if(my_post_code.indexOf("'") != -1)
  {
    invalid_char_quote += "Post code\n";
  }

  //check the maximum date imput lenght allowed
  if(my_forename.length > 50) maxallowed +="Forename\n"
  if(my_surname.length > 50) maxallowed +="Surname\n"
  if(my_userid.length > 5) maxallowed +="User ID\n"
  if(my_password.length > 50) maxallowed +="Password\n"
  if(my_email.length > 55) maxallowed +="Email\n"
  if(my_home_tel.length > 25) { maxallowed += "Home Phone\n"; }
  if(my_mob_tel.length > 15) { maxallowed += "Mobile Phone\n"; }
  if(my_post_code.length > 15) { maxallowed += "Post code\n"; }

  if (fieldsmissing != "") 
  {
    // open the alert window to display fields which are missing  
	alert("You have either missed the following fields or you have typed incorrect values:\n"
       + fieldsmissing
       + "Please complete and resubmit.")
    return false
  } 
  if (maxallowed != "")
    {
      alert("Maximum characters allowed extended in the following field/s - \n"
            + maxallowed
            + "Please try again.")
       return false
    }
	if(invalid_char_quote != "")
	{
      alert("Please remove the single quote i.e. ' from the following field/s\n" +
		    invalid_char_quote + "\nPlease try again.");
	  return false
	}
	if(char_missing != "")
	{
      alert("" + char_missing);

	  return false;
	}
  if(confirm("Following is the information provided by you\n\n" +
			"Forename = " + my_forename + "\n" + 
	        "Surname = " + my_surname + "\n" +
	        "Gender = " + my_gender_text + "\n" + 
	        "Email address = " + my_email + "\n" +
		    "Home phone no = " + my_home_tel + "\n" +
		    "Mobile no = " + my_mob_tel + "\n" +
		    "Post code = " + my_post_code + "\n" +
		    "Language/s = " + selected_langs + "\n" +
            "User id = " + my_userid + "\n" +
	        "Password = " + my_password +
		    "\n\n Continue?"))
  {
	return true;	
  }
  else
  {
	return false;
  }
}

//function to change the drop down menu
function OnChange(dropdown)
{
	document.forms[0].submit();    
	return true; 
}

//function to go to the previous page
function previousPage()
{
	window.history.back();
}

//function to validate the academic details form
function ValidateAcademicDetailsForm()
{
	var missing = "";
	var my_fees_status  = document.forms[0].fees_status.selectedIndex;
	var my_cur_year = document.forms[0].current_year.selectedIndex;
	var my_start_year = document.forms[0].Year_start.selectedIndex;
	var my_end_year = document.forms[0].Year_end.selectedIndex;	

	//check whether the following information is provided or not
	if (my_fees_status == "" || my_fees_status == 0)
	{ 
	  missing += "Fees status\n";
	}
	if (my_cur_year == "" || my_cur_year == 0)
	{ 
	  missing += "Current year of programme\n";
	}
	if(my_start_year == "" || my_start_year == 0)
	{
	  missing += "Year programme started\n";
	}
	if(my_end_year == "" || my_end_year == 0)
	{
	  missing += "Year programme expected to finish\n";
	}
	if (missing != "")
	{
	   alert("Please provide the following information\n"
			 + missing
			 + "and then try again")
	   return false
	}
}

//function to go back
function back()
{    
	history.go(-1);
}

//function to validate the login form
function ValidateLogin(form)
{
    var fieldsmissing = ""
    var maxallowed = ""
	var invalid_char_quote = "";

	var my_userid = form.loginusername.value;
	var my_password = form.loginpassword.value;

    //check whether the following information is provided or not 
    if(my_userid == "") { fieldsmissing += "User ID\n"; }
	if(my_password == "") { fieldsmissing += "Password\n"; }
	
	//check for invalid character quotes
	if(my_userid.indexOf("'") != -1) { invalid_char_quote += "User id\n"; }
	if(my_password.indexOf("'") != -1) { invalid_char_quote += "Password\n"; }
			
    if (fieldsmissing != "")
    {
       alert("You have missed the following information:\n"
             + fieldsmissing
             + "Please complete and try to login again.")
       return false;
    }
	if (maxallowed != "")
    {
      alert("Maximum characters allowed extended in the following field/s - \n"
            + maxallowed
            + "Please try again.")
       return false;
    }
}

// funtion to validate the retrieve password form
function ValidateRetrievePassword(retrivepassform) 
{ 
  var fieldsmissing = "";
  var maxallowed = "";

  if(retrivepassform.retrievepass.value == "") { fieldsmissing += "Username\n"; }
  if(retrivepassform.retrievepass.value.length > 5) { maxallowed +="Username\n"; }

  if (fieldsmissing != "") 
  {
    // open the alert window to display fields which are missing  
	alert("You have either missed the following fields or you have typed incorrect values:\n"
       + fieldsmissing
       + "Please complete and resubmit.")
    return false
  } 
  if (maxallowed != "") 
  {
    // open the alert window to display fields which are missing  
	alert("You have reached the max characters allowed in the following fields:\n"
       + maxallowed
       + "Please complete and resubmit.")
    return false
  } 
  if(confirm("Following is the information provided by you\n\n" +
			"Username = " + retrivepassform.retrievepass.value + "\n" + 
		    "\n\n Continue?"))
  {
	return true;	
  }
  else
  {
	return false;
  }
}