// for auto jump to next field 
function autoTab(currfield, inputlength, nextfield)
{
  if (currfield.value.length == inputlength) {
    o = document.getElementById(nextfield);
    o.select();
  }
}

function validate_email(str) {
  var at = "@";
  var dot = ".";
  var lat = str.indexOf(at);
  var lstr = str.length;
  var ldot = str.indexOf(dot);
  
  if (str.indexOf(at) == -1) {
    return false;
  }
  
  if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
    return false;
  }
  
  if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr - 1) {
    return false;
  }
  
  if (str.indexOf(at, (lat + 1))!= -1) {
    return false;
  }
  
  if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
    return false;
  }
  
  if (str.indexOf(dot, (lat + 2)) == -1) {
    return false;
  }
  
  if (str.indexOf(" ") != -1) {
    return false;
  }
  
  return true;
}

var dtCh = "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s) {
  var i;
  for (i = 0; i < s.length; i++) {
    // Check that current character is number.
    var c = s.charAt(i);
    if ((c < "0") || (c > "9"))
      return false;
  }
  // All characters are numbers.
  return true;
}

function daysInFebruary (year){
  // February has 29 days in any year evenly divisible by four,
  // EXCEPT for centurial years which are not also divisible by 400.
  return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
  for (var i = 1; i <= n; i++) {
    this[i] = 31;
    if (i == 4 || i == 6 || i == 9 || i == 11) {
      this[i] = 30;
    }
    if (i == 2) {
      this[i] = 29;
    }
  }
  return this;
}

function validateDate(month, day, year) {
  if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) { // is leap year
    var daysInMonth = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  } else {
    var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  }
  
  if (day > daysInMonth[month - 1]) {
    return false;
  } else {
    return true;
  }
}

function validateAge(month, day, year) {
  var currentTime = new Date();
  var age13Time = new Date();
	age13Time.setFullYear(parseInt(year) + 13, parseInt(month) - 1, parseInt(day));
  
	if (age13Time > currentTime) {
    return false;
	}
  return true;
}

function ValidateWelcomeForm() {
  alert_msg = '';
  
  if (document.getElementById('title').value == 'sel') {
    alert_msg += 'Please choose your title\n';
  }
  if (document.getElementById('firstname').value == '') {
    alert_msg += 'Please fill in your firstname\n';
  }
  if (document.getElementById('lastname').value == '') {
    alert_msg += 'Please fill in your lastname\n';
  }
  if (validate_email(document.getElementById('email').value) == false) {
    alert_msg += 'Please fill in a valid email address\n';
  }
  if (document.getElementById('address1').value == '') {
    alert_msg += 'Please fill in your address\n';
  }
  if (document.getElementById('city').value == '') {
    alert_msg += 'Please fill in your city\n';
  }
  if (document.getElementById('state').value == 'sel') {
    alert_msg += 'Please choose your state\n';
  }
  if ((document.getElementById('zipcode').value == null) || (document.getElementById('zipcode').value == '') || (document.getElementById('zipcode').value % 1 != 0) || (document.getElementById('zipcode').value.length < 5)) {
    alert_msg += 'Please fill in a valid zip code\n';
  } else if (!isInteger(document.getElementById('zipcode').value)) {
    alert_msg += 'Please fill in the correct zip code\n';
  }
  if (document.getElementById('phone1').value == '' || document.getElementById('phone2').value == '' || document.getElementById('phone3').value == '') {
    alert_msg += 'Please fill in your phone number\n';
  } else if (document.getElementById('phone1').value.length != 3 || document.getElementById('phone2').value.length != 3 || document.getElementById('phone3').value .length != 4) {
    alert_msg += 'Please fill in a 10 digit phone number\n';
  } else if (!isInteger(document.getElementById('phone1').value) || !isInteger(document.getElementById('phone2').value) || !isInteger(document.getElementById('phone3').value)) {
    alert_msg += 'Please fill in the correct phone number\n';
  }
  if (document.getElementById('birthday_month').value == 'sel' || document.getElementById('birthday_day').value == 'sel' || document.getElementById('birthday_year').value == 'sel') {
    alert_msg += 'Please fill in your birthday\n';
  } else if (!validateDate(document.getElementById('birthday_month').value, document.getElementById('birthday_day').value, document.getElementById('birthday_year').value)) {
    alert_msg += 'Please fill in the correct birthday\n';
  } else if (!validateAge(document.getElementById('birthday_month').value, document.getElementById('birthday_day').value, document.getElementById('birthday_year').value)) {
    alert_msg += 'You must be at least 13 years old.\n';
  }
  
  if (alert_msg == '') {
    return true;
  } else {
    alert(alert_msg);
    return false;
  }
}