
Now = new Date();
NowYear = Now.getYear();
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  = /^[1-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 (f.EventDate.value == "--") {
//    if (!invalid) {
//      invalid = f.EventDate;
//	}
//    msg2 += "\n- Please Enter A Valid Event Date.\n";
//  }

  if (f.TeamAge.value == "--") {
    if (!invalid) {
      invalid = f.TeamAge;
	}
    msg2 += "\n- Please Enter A Valid Age Group.\n";
  }

  if (f.PlayAge.value == "--") {
    if (!invalid) {
      invalid = f.PlayAge;
	}
    msg2 += "\n- Please Enter A Valid Division to Play.\n";
  }

  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 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;
}
