function array_unique(arr)
{
  //get sorted array as input and returns the same array without duplicates.
  var result=new Array();
  var lastValue="";
  for (var i=0; i<arr.length; i++)
  {
    var curValue=arr[i];
    if (curValue != lastValue)
    {
      result[result.length] = curValue;
    }
    lastValue=curValue;
  }
  return result;
}

function check_null_values(string_array)
{
  string_array_length = string_array.length;

  for (var i=0; i<string_array_length; i++)
  {
    if (string_array[i] == "")
    {
      return false;
    }
  }
    return true;
}


function check_not_emails(string_array)
{
var check;
  string_array_length = string_array.length;

  for (var i=0; i<string_array_length; i++)
  {
    check = string_array[i].indexOf("@");
    if (check > -1)
    {
      return false;
    }
  }
    return true;

}

function trim_it(the_string)
{
  var string_to_return;
  string_to_return = the_string.replace(/^\s+|\s+$/g,"");

  return string_to_return;
}

function check_email_addresses(email_addr_arr)
{
  email_addr_arr_length = email_addr_arr.length;
  for (var i=0; i<email_addr_arr_length; i++)
  {
     var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

     if (filter.test(email_addr_arr[i]))
     {
       nothing=1;
     }
     else
     {
       return email_addr_arr[i];
     }
  }
  return 'ok';
}

function validate_one()
{

  not_emails_array = new Array()
  emails_array = new Array()
  all_values_array = new Array()

  emails_array[0] = trim_it(document.form2.email.value);

  not_emails_array[0] = trim_it(document.form2.forename.value);
  not_emails_array[1] = trim_it(document.form2.surname.value);
  not_emails_array[2] = trim_it(document.form2.children.value);
  not_emails_array[3] = trim_it(document.form2.ages.value);
  not_emails_array[4] = trim_it(document.form2.comment.value);
  not_emails_array[5] = trim_it(document.form2.better.value);
  not_emails_array[6] = trim_it(document.form2.best.value);


  all_values_array[0] = trim_it(document.form2.forename.value);
  all_values_array[1] = trim_it(document.form2.email.value);



  not_email_bol = check_not_emails(not_emails_array);
  if (not_email_bol === false)
  {
    alert("Sorry, you have entered an email address in a non email field.");
    return false;
  }

  value_check_bol = check_null_values(all_values_array);

  if (value_check_bol === false)
  {
    alert("Sorry, Forename / Email Address are mandatory.");
    return false;
  }

  emails_array.sort();
  emails_array_length = emails_array.length;
  emails_array_no_dupes = new Array()
  emails_array_no_dupes = array_unique(emails_array);

  emails_array_no_dupes_length = emails_array_no_dupes.length;

  //if (emails_array_length != emails_array_no_dupes_length)
  //{
  //  alert("Sorry, you have entered a duplicate email address, please re-enter.");
  //  return false;
  //}

  email_check = check_email_addresses(emails_array);
  if (email_check != 'ok')
  {
    alert("Sorry, email address "+email_check+" is invalid, please re-enter");
    return false;
  }

  //if (!document.form1.checkbox.checked)
  //{
  //  alert("Sorry, you must accept the terms and conditions to enter the competition");
  //  return false;
  //}


  return true;


}
























function validate_compPage() {
  not_emails_array = new Array()
  emails_array = new Array()
  all_values_array = new Array()

  emails_array[0] = trim_it(document.form2.email.value);

  not_emails_array[0] = trim_it(document.form2.name.value);
  not_emails_array[1] = trim_it(document.form2.phone.value);
  not_emails_array[2] = trim_it(document.form2.comment.value);


	if (document.form2.name.value == null || document.form2.name.value == "") {
		alert("You must enter a name.");
		return false;
	}

	if (document.form2.email.value != document.form2.emailconfirm.value) {
		alert("Email addresses do not match.");
		return false;
	}
	if (document.form2.comment.value == null || document.form2.comment.value == "") {
		alert("Please enter an answer.");
		return false;
	}
	
  not_email_bol = check_not_emails(not_emails_array);
  if (not_email_bol === false)
  {
    alert("Sorry, you have entered an email address in a non email field.");
    return false;
  }

  value_check_bol = check_null_values(all_values_array);

  if (value_check_bol === false)
  {
    alert("Sorry, Forename / Email Address are mandatory.");
    return false;
  }

  emails_array.sort();
  emails_array_length = emails_array.length;
  emails_array_no_dupes = new Array()
  emails_array_no_dupes = array_unique(emails_array);

  emails_array_no_dupes_length = emails_array_no_dupes.length;

  //if (emails_array_length != emails_array_no_dupes_length)
  //{
  //  alert("Sorry, you have entered a duplicate email address, please re-enter.");
  //  return false;
  //}

  email_check = check_email_addresses(emails_array);
  if (email_check != 'ok')
  {
    alert("Sorry, email address "+email_check+" is invalid, please re-enter");
    return false;
  }

  //if (!document.form1.checkbox.checked)
  //{
  //  alert("Sorry, you must accept the terms and conditions to enter the competition");
  //  return false;
  //}


  return true;


}






