var form = "";
var submitted = false;
var error = false;
var error_message = "";

function check_input(field_name, field_size, message) {
  if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
    var field_value = form.elements[field_name].value;

    if (field_value == '' || field_value.length < field_size) {
      error_message = error_message + "* " + message + "\n";
      error = true;
    }
  }
}


function Validate_String(string, return_invalid_chars) {
  valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  invalid_chars = '';
  if(string == null || string == '')
     return(true);

  //For every character on the string.   
  for(index = 0; index < string.length; index++) {
    char = string.substr(index, 1);                        
     
    //Is it a valid character?
    if(valid_chars.indexOf(char) == -1) {
      //If not, is it already on the list of invalid characters?
      if(invalid_chars.indexOf(char) == -1) {
        //If it's not, add it.
        if(invalid_chars == '')
          invalid_chars += char;
        else
          invalid_chars += ', ' + char;
      }
    }
  }
            
  //If the string does not contain invalid characters, the function will return true.
  //If it does, it will either return false or a list of the invalid characters used
  //in the string, depending on the value of the second parameter.
  if(return_invalid_chars == true && invalid_chars != '') {
    last_comma = invalid_chars.lastIndexOf(',');
    if(last_comma != -1)
      invalid_chars = invalid_chars.substr(0, $last_comma) + 
      ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
    return(invalid_chars);
    }
  else
    return(invalid_chars == ''); 
}



function Validate_Email_Address(email_address) {
  //Assumes that valid email addresses consist of user_name@domain.tld
//  at = email_address.indexOf('@');
//  dot = email_address.indexOf('.');

//  if(at == -1 || 
//    dot == -1 || 
//    dot <= at + 1 ||
//    dot == 0 || 
//    dot == email_address.length - 1)
//    return(false);
     
//  user_name = email_address.substr(0, at);
//  domain_name = email_address.substr(at + 1, email_address.length);                  

//  if(Validate_String(user_name) === false || 
//    Validate_String(domain_name) === false)
//    return(false);                     

//  return(true);

if(email_address=="")   {return(false);}
else { if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email_address))  return(true); else {  return(false); }   }
  

}




function check_email(field_name, message) {
  if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
    var field_value = form.elements[field_name].value;

    if (!Validate_Email_Address(field_value)) {
      error_message = error_message + "* " + message + "\n";
      error = true;
    }
  }
}


function check_radio(field_name, message) {
  var isChecked = false;

  if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
    var radio = form.elements[field_name];

    for (var i=0; i<radio.length; i++) {
      if (radio[i].checked == true) {
        isChecked = true;
        break;
      }
    }

    if (isChecked == false) {
      error_message = error_message + "* " + message + "\n";
      error = true;
    }
  }
}

function check_select(field_name, field_default, message) {
  if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
    var field_value = form.elements[field_name].value;

    if (field_value == field_default) {
      error_message = error_message + "* " + message + "\n";
      error = true;
    }
  }
}


function check_form(form_name) {
  if (submitted == true) {
    alert("This form has already been submitted. Please press Ok and wait for this process to be completed.");
    return false;
  }




  error = false;
  form = form_name;
  error_message = "Errors have occured during the process of your form.\nPlease make the following corrections:\n\n";

  check_input("city", 2, "Please enter a city.");
  check_input("tokenID", 4, "Your token serial must contain at least 4 characters.");
 // check_input("tokenCode", 2, "Your token security code must conatin at least 2 characters.");

  check_radio("type", "Please select whether you gave or received the token.");

  check_email("email", "Please enter a valid e-mail address.");

  check_input("txtsearch", 1, "Please enter a token serial");
  
  check_input("code", 5, "Please enter the CAPTCHA code");	

  if (error == true) {
    alert(error_message);
    return false;
  } else {
    submitted = true;
    return true;
  }
}