
Now = new Date();
NowYear = Now.getYear();
NowMon = Now.getMonth();
if (NowMon < 4) NowYear--;
if (NowYear < 2000) NowYear += 1900;

//function to write date options
function WriteDateOptions(from, to)
{
  line = "";
  for (i = from; i <= to; i++)
  {
    line += "<OPTION value=\"" +  i + "\">";
    line += i;
    line += "</OPTION>";
  }
  return line;
}

function validateEmail(strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/

var objRegExp = /(\w+[\w|\.|-]*\w+)(@\w+[\w|\.|-]*\w+\.\w{2,3})/i;

  //check for valid email
  return objRegExp.test(strValue);
}

function validateUSPhone(strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains valid
  US phone pattern.
  Ex. (999) 999-9999 or (999)999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/

  var objRegExp  = /^[2-9]\d{2}\-\d{3}\-\d{4}$/;
//  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

// check for valid us phone with or without space between
// area code
  return objRegExp.test(strValue);
}

function isblank(s)
{
  for (var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if ( (c != ' ') && (c != '\n') && (c != '\t') ) return false;
  }
  return true;
}

function verify(f)
{
  var msg;
  var msg2 = "";
  var empty_fields = "";
  var errors = "";
  var invalid = "";
  var undefined;
  var em;

  for ( var i = 0; i < f.length; i++) {
    var e = f.elements[i];

    if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
      if ((e.value == null) || (e.value == "") || isblank(e.value)) {
        if (!empty_fields) {
          empty_fields = e;
        }
        continue;
      }

      if (e.numeric || (e.min != null) || (e.max != null)) {
        var v = parseFloat(e.value);
        if (isNaN(v) || ((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max))) {
          if (!errors) {
            errors = e;
          }
        }
      }
    }
  }


  if (!validateEmail(f.Email.value)) {
    if (!invalid) {
      invalid = f.Email;
	}
    msg2 += "\n- Please Enter A Valid EMail Address.\n";
  }

  if (!validateUSPhone(f.CTelNum.value)) {
    if (!invalid) {
      invalid = f.CTelNum;
	}
    msg2 += "\n- Please Enter A Valid Telephone Number.\n";
  }

  if (!validateUSPhone(f.P1TelNum.value)) {
    if (!invalid) {
      invalid = f.P1TelNum;
	}
    msg2 += "\n- Please Enter A Valid Telephone Number.\n";
  }

  if (f.P2TelNum.value && !validateUSPhone(f.P2TelNum.value)) {
    if (!invalid) {
      invalid = f.P2TelNum;
	}
    msg2 += "\n- Please Enter A Valid Telephone Number.\n";
  }

  if (!validateUSPhone(f.ECTelNum.value)) {
    if (!invalid) {
      invalid = f.ECTelNum;
	}
    msg2 += "\n- Please Enter A Valid Telephone Number.\n";
  }

  var dt = f.bmon.value + "/" + f.bday.value + "/" + f.byear.value;
  if ( !isDate(dt) ) {
    if (!invalid) {
      invalid = f.bmon;
    }
    msg2 += "\n- Please Enter A Valid Birth Date.\n";
  }

  var ag = f.currAgeGrp.value;
  if (ag == "---") {
    invalid = f.currAgeGrp;
    msg2 += "\n- Please Choose Current Age Group.\n";
  }

  var ag = f.toAgeGrp.value;
  if (ag == "---") {
    invalid = f.toAgeGrp;
    msg2 += "\n- Please Choose Tryout Age Group.\n";
  }

  var age = HowOld(f.bday.value, f.bmon.value, f.byear.value, 31, 7, 2010);
  f.age.value = age;

  if (!empty_fields && !errors && !invalid) return true;

  msg =  " _______________________________________________________\n\n";
  msg += " The form was not submitted because of the following error(s).\n";
  msg += " Please correct these error(s) and re-submit.\n";
  msg += " _______________________________________________________\n\n";


  if (empty_fields) {
    msg += "- One or more required fields are empty.\n";
  }

  if (errors) {
    msg += "\n- One or more fields contain and invalid value.\n";
    errors.focus();
  }

  if (invalid) {
    msg += msg2;
    invalid.focus();
  }

  if (empty_fields) {
    empty_fields.focus();
  }

  alert(msg);

  return false;
}

function setCurrAgeGrp(f)
{
  var age = HowOld(f.bday.value, f.bmon.value, f.byear.value, 31, 7, 2010);
  var a = age - 5;
  if (a > 11) { a = 12; }
  f.currAgeGrp.selectedIndex = a;
  setToAgeGrp(f, f.currAgeGrp);
}

function setToAgeGrp(f, o)
{
  var a = o.selectedIndex;
//  if (a == 10) { a = 9; }
  if (a > 10) { a = 11; }
  f.toAgeGrp.selectedIndex = a;
}

function copyAddrP1(f, o)
{
  if (o.checked) {
    f.P1Lastname.value = f.CLastname.value;
    f.P1Street.value = f.CStreet.value;
    f.P1Town.value = f.CTown.value;
    f.P1State.value = f.CState.value;
    f.P1ZipCode.value = f.CZipCode.value;
    f.P1TelNum.value = f.CTelNum.value;
  } else {
    f.P1Lastname.value = "";
    f.P1Firstname.value = "";
    f.P1Street.value = "";
    f.P1Town.value = "";
    f.P1State.value = "";
    f.P1ZipCode.value = "";
    f.P1TelNum.value = "";
  }
}

function copyAddrP2(f, o)
{
  var clr = "Yellow";
  var dis = false;

  if (f.P2Rel.value == "None") {
    dis = true;
    clr = "Khaki";
  };
  
  f.P2Lastname.disabled = dis;
  f.P2Firstname.disabled = dis;
  f.P2Street.disabled = dis;
  f.P2Town.disabled = dis;
  f.P2State.disabled = dis;
  f.P2ZipCode.disabled = dis;
  f.P2TelNum.disabled = dis;
  
  f.P2Lastname.style.backgroundColor = clr;
  f.P2Firstname.style.backgroundColor = clr;
  f.P2Street.style.backgroundColor = clr;
  f.P2Town.style.backgroundColor = clr;
  f.P2State.style.backgroundColor = clr;
  f.P2ZipCode.style.backgroundColor = clr;
  f.P2TelNum.style.backgroundColor = clr;
  
   if ( (f.P2Rel.value != "None") && o.checked) {
    f.P2Lastname.value = f.CLastname.value;
    f.P2Street.value = f.CStreet.value;
    f.P2Town.value = f.CTown.value;
    f.P2State.value = f.CState.value;
    f.P2ZipCode.value = f.CZipCode.value;
    f.P2TelNum.value = f.CTelNum.value;
  } else {
    f.P2Lastname.value = "";
    f.P2Firstname.value = "";
    f.P2Street.value = "";
    f.P2Town.value = "";
    f.P2State.value = "";
    f.P2ZipCode.value = "";
    f.P2TelNum.value = "";
  }
}

function phoneFilter(f)
{
  var ipt = f.value;
  var fmt = "###-###-####";

  if (ipt.length > 0) {
    //do not perform if empty input
	var nums = ""; //store all the numbers here

	//process to remove non-numbers and spaces
	for (var i = 0; i < ipt.length; i++) {
      var c = ipt.charAt(i);
		if (!(isNaN(c) || c == " ")) nums += c;
	}

	//remove country code, if any
	var opt = ""; //assign numbers here

	//assign numbers to chosen format
	var n = 0, i = 0;
	while (i < fmt.length && n < nums.length) {
	  var c = fmt.charAt(i);
      if (c == "#") {
		opt += nums.charAt(n++)
	  } else {
		opt += c;
	  }
	  i++;
	}

	//give alert if length is less than 10.
	if (nums.length < 10) {
	  alert("Please Enter a 10 Digit Phone Number.");
	  f.select();
	}

  f.value = opt; //output to form
  }
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
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 stripCharsInBag(s, bag) {
  var i;
  var returnString = "";
  // Search through string's characters one by one.
  // If character is not in bag, append to returnString.
  for (i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

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 isDate(dtStr) {

  var daysInMonth = DaysArray(12);
  var pos1 = dtStr.indexOf(dtCh);
  var pos2 = dtStr.indexOf(dtCh,pos1 + 1);
  var strMonth = dtStr.substring(0, pos1);
  var strDay = dtStr.substring(pos1 + 1, pos2);
  var strYear = dtStr.substring(pos2 + 1);
  strYr = strYear;
  if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1);
  if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1);
  for (var i = 1; i <= 3; i++) {
    if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1);
  }
  month = parseInt(strMonth);
  day = parseInt(strDay);
  year = parseInt(strYr);
  if (pos1 == -1 || pos2 == -1) return false;
  if (strMonth.length < 1 || month < 1 || month > 12) return false;
  if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) return false;
  if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) return false;
  if (dtStr.indexOf(dtCh,pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) return false;
  return true;
}

function HowOld(day, month, year, thisDay, thisMonth, thisYear) {

  var yearsold = thisYear - year, monthsold = 0, daysold = 0;
  
  if ( (day == 0) || (month == 0) || (year == 0) ) return 0;
  
  if (thisMonth >= month)
    monthsold = thisMonth - month;
  else {
    yearsold--;
    monthsold = thisMonth + 12 - month;
  }
  
  if (thisDay >= day)
    daysold = thisDay - day;
  else {
    if (monthsold > 0)
      monthsold--;
    else {
      yearsold--;
      monthsold += 11;
    }
    daysold = thisDay + 31 - day;
  }
  
  if (yearsold < 3) return 0;
  
  return yearsold;
}