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 show_error(error_message)
{
 //showDialog('Error','<center>'+error_message+'</center>','error');
 alert(error_message);
}

function validate_one()
{

  not_emails_array = new Array()
  emails_array = new Array()
  all_values_array = new Array()

  emails_array[0] = trim_it(document.form1.i_email.value);

  not_emails_array[0] = trim_it(document.form1.i_name.value);
  
  all_values_array[0] = trim_it(document.form1.i_name.value);
  all_values_array[1] = trim_it(document.form1.i_email.value);


  not_email_bol = check_not_emails(not_emails_array);
  if (not_email_bol === false)
  {
    show_error("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)
  {
    show_error("Sorry, Name / 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')
  {
    show_error("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;


}

