empty_string_re = /^\s*$/;
blank_re = /^\s*$/;
digits_re = /^[0-9]+$/;
phone_re = /^\d{10}$/;
email_re = /^[^ ]+@[^ ]+\.[^ ]+$/;
postal_code_re = /^[a-zA-Z]\d[a-zA-Z] ?\d[a-zA-Z]\d$/;
zip_code_re = /^\d{5}(\-\d{4})?$/;
website_re = /^(http:\/\/)?[^ ]*\.?[^. ]+\.[^. ]+$/;
function $(id) {
  return document.getElementById(id);
}

function required_text_field_empty(id, alert_text) {
  if (empty_string_re.exec($(id).value)) {
    $(id).focus();
    alert(alert_text);
    return true;
  } else {
    return false;
  }
}

function invalid_email_entered(id) {
  if (!empty_string_re.exec($(id).value) && !email_re.exec($(id).value)) {
    $(id).focus();
    alert('Please enter a valid email address.');
    return true;
  } else {
    return false;
  }
}

function invalid_phone_entered(id) {
  if (!empty_string_re.exec($(id).value) && !phone_re.exec($(id).value)) {
    $(id).focus();
    alert('Please enter a valid phone.');
    return true;
  } else {
    return false;
  }
}

function instant_invalid_phone_entered(id) {
  if (!empty_string_re.exec($(id).value) && !phone_re.exec($(id).value)) {
    alert('Please enter a valid phone.');
    return true;
  } else { 
    return false;
  }
}

function invalid_postal_code_entered(id) {
  if (!empty_string_re.exec($(id).value) && !postal_code_re.exec($(id).value)) {
    $(id).focus();
    alert('Please enter a valid postal code.');
    return true;
  } else {
    return false;
  }
}

function invalid_zip_code_entered(id) {
  if (!empty_string_re.exec($(id).value) && !zip_code_re.exec($(id).value)) {
    $(id).focus();
    alert('Please enter a valid postal code.');
    return true;
  } else {
    return false;
  }
}

function invalid_zip_nor_postal_code_entered(id) {
  if (!empty_string_re.exec($(id).value) && !zip_code_re.exec($(id).value) && !postal_code_re.exec($(id).value)) {
    $(id).focus();
    alert('Please enter a valid postal or zip code.');
    return true;
  } else {
    return false;
  }
}

function invalid_website_url_entered(id) {
  if(!blank_re.exec($(id).value) && !website_re.exec($(id).value)){
    alert('Please enter a valid website address, or leave it blank.');
    $(id).focus();
    return true;
  } else {
    return false;
  }
}

function instant_invalid_credit_card_number_entered(id, cc_type) {
  var valid_cc_types = ['visa', 'mastercard', 'amex', 'discover'];
  invalid_cc_type = true;
  for(i = 0; i < valid_cc_types.length; i++) {
    if (cc_type == valid_cc_types[i]) {
      invalid_cc_type = false;
      break;
    }
  }
  if (invalid_cc_type) {
    alert("Credit card type must be one of 'visa', 'mastercard', 'amex' or 'discover'.");
    return false;
  }
  if (cc_type == 'amex') {
    if ($(id).value.length == 15 && valid_according_to_luhn($(id).value)) {
      return false;
    } else {
      alert('Credit card number invalid. Please re-check.');
      return true;
    }
  } else {
    if ($(id).value.length == 16 && valid_according_to_luhn($(id).value)) {
      return false;
    } else {
      alert('Credit card number invalid. Please re-check.');
      return true;
    }
  }
}

function invalid_credit_card_number_entered(id, cc_type) {
  var valid_cc_types = ['visa', 'mastercard', 'amex', 'discover'];
  invalid_cc_type = true;
  for(i = 0; i < valid_cc_types.length; i++) {
    if (cc_type == valid_cc_types[i]) {
      invalid_cc_type = false;
      break;
    }
  }
  if (invalid_cc_type) {
    alert("Credit card type must be one of 'visa', 'mastercard', 'amex' or 'discover'.");
    return false;
  }
  if (cc_type == 'amex') {
    if ($(id).value.length == 15 && valid_according_to_luhn($(id).value)) {
      return false;
    } else {
      $(id).focus();
      alert('Credit card number invalid. Please re-check.');
      return true;
    }
  } else {
    if ($(id).value.length == 16 && valid_according_to_luhn($(id).value)) {
      return false;
    } else {
      $(id).focus();
      alert('Credit card number invalid. Please re-check.');
      return true;
    }
  }

}

function valid_according_to_luhn(number) {

  var number=number.replace(/\D/g, '');
 
  // Set the string length and parity
  var number_length=number.length;
  var parity=number_length % 2;
 
  // Loop through each digit and do the maths
  var total=0;
  for (i=0; i < number_length; i++) {
    var digit=number.charAt(i);
    // Multiply alternate digits by two
    if (i % 2 == parity) {
      digit=digit * 2;
      // If the sum is two digits, add them together (in effect)
      if (digit > 9) {
        digit=digit - 9;
      }
    }
    // Total up the digits
    total = total + parseInt(digit);
  }

  // If the total mod 10 equals 0, the number is valid
  if (total % 10 == 0) {
    return true;
  } else {
    return false;
  }
}

function invalid_credit_card_id_entered(id, cc_type) {
  var valid_cc_types = ['visa', 'mastercard', 'amex', 'discover'];
  invalid_cc_type = true;
  for(i = 0; i < valid_cc_types.length; i++) {
    if (cc_type == valid_cc_types[i]) {
      invalid_cc_type = false;
      break;
    }
  }
  if (invalid_cc_type) {
    alert("Credit card type must be one of 'visa', 'mastercard', 'amex' or 'discover'.");
    return false;
  }

  if (cc_type == 'amex') {
    if ($(id).value.length == 4) {
      return false;
    } else {
      $(id).focus();
      alert('The American Express credit card id has to be 4 digits long.');
      return true;
    }
  } else {
    if ($(id).value.length == 3) {
      return false;
    } else {
      $(id).focus();
      alert('The credit card id has to be 3 digits long.');
      return true;
    }
  }
}

function required_radio_field_empty(name, alert_text) {
  var f = document.forms[0];
  if (f[name] && f[name].length) {
    for (var i = 0; i < f[name].length; i++) {
      if (f[name][i].checked) {
        return false;
      }
    }
    f[name][0].focus();
  } else if (f[name]){
    if (f[name].checked) {
      return false;
    }
    f[name].focus();
  }
  alert(alert_text);
  return true;
}

function get_value_of_radio_field(name) {
  var f = document.forms[0];
  var value = null;
  for (var i = 0; i < f[name].length; i++) {
    if (f[name][i].checked) {
      value = f[name][i].value;
    }
  }
  return value;
}

function phoneticize(text, display_target, is_email) {
  var phonetic_alphabet = {
    "a": "Alpha", "b": "Bravo", "c": "Charlie",
    "d": "Delta", "e": "Echo", "f": "Foxtrot",
    "g": "Golf", "h": "Hotel", "i": "India",
    "j": "Juliet", "k": "Kilo", "l": "Lima",
    "m": "Mike", "n": "November", "o": "Oscar",
    "p": "Papa", "q": "Quebec", "r": "Romeo", "s": "Sierra",
    "t": "Tango", "u": "Unicorn", "v": "Victor",
    "w": "Whiskey", "x": "X-Ray", "y": "Yankee", "z": "Zulu",
    "0": "Zero", "1": "One", "2": "Two", "3": "Three", "4": "Four",
    "5": "Five", "6": "Six", "7": "Seven", "8": "Eight", "9": "Nine", 
    "@": "AT", ".": "DOT", "-": "DASH", "+": "PLUS", " ": "SPACE",
    "/": "SLASH", ":": "COLON", "?": "QUESTION MARK", "&": "AMPERSAND",
    "=": "EQUALS"
  };
  var phonetic_version = '';
  if (is_email && !email_re.exec(text)) {
    phonetic_version = 'Invalid email.';
  } else {
    for (var i=0; i < text.length; i++) {
      phonetic_version += phonetic_alphabet[text.charAt(i).toLowerCase()] + " ";
    }
  }
  $(display_target).innerHTML = phonetic_version;
}

function noLetters(e) {
  var keynum
  var keychar
  var numcheck

  if(window.event)  { // IE
    keynum = e.keyCode
  }
  else if(e.which) { // Netscape/Firefox/Opera 
    keynum = e.which
  }
    // For backspace(8), tab(9), arrow keys(37-40) http://www.geekpedia.com/tutorial138_Get-key-press-event-using-JavaScript.html.
  if (keynum == 8 || keynum == 16 || keynum == 17 || (keynum < 34 && keynum > 41) || keynum == 46 || keynum == 116 || keynum == 9 || (keynum > 95 && keynum < 106) ) { 
    return true;   
  } else {
    keychar = String.fromCharCode(keynum)
    numcheck = /\d/
    return numcheck.test(keychar);
  }
}

