// whitespace characters
var whitespace = " \t\n\r";

// isDate returns true if str is a valid date in the format mm/dd/yyyy
function isDate(str) {
  if (str.length != 10) { return false }

  for (j=0; j<str.length; j++) {
    if ((j == 2) || (j == 5)) {
      if (str.charAt(j) != "/") { return false }
    } else {
      if ((str.charAt(j) < "0") || (str.charAt(j) > "9")) { return false }
    }
  }

  var month = str.charAt(0) == "0" ? parseInt(str.substring(1,2)) :
parseInt(str.substring(0,2));
  var day = str.charAt(3) == "0" ? parseInt(str.substring(4,5)) :
parseInt(str.substring(3,5));
  var begin = str.charAt(6) == "0" ? (str.charAt(7) == "0" ? (str.charAt(8) ==
"0" ? 9 : 8) : 7) : 6;
  var year = parseInt(str.substring(begin, 10));

  if (day == 0) { return false }
  if (month == 0 || month > 12) { return false }
  if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
month == 10 || month == 12) {
    if (day > 31) { return false }
  } else {
    if (month == 4 || month == 6 || month == 9 || month == 11) {
      if (day > 30) { return false }
    } else {
      if (year%4 != 0) {
        if (day > 28) { return false }
      } else {
        if (day > 29) { return false }
      }
    }
  }
  return true;
};



// isEmpty returns true if s is empty or null
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// isWhitespace returns true if string s is empty or whitespace characters only.
function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Returns true if string s contains any whitespace characters 
function containsWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
    // Check to see if the current character is whitespace.
    var c = s.charAt(i);
    if (whitespace.indexOf(c) != -1) return true;
    };

    // No characters are whitespace.
    return false;
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return true;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace or does it contain whitespace?
    if (isWhitespace(s) || containsWhitespace(s)) return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++  }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++  }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function validateInputFreeTrial()
{
var blnValidInput = true;
if (document.OrganizationEntryForm.OrganizationName.value == "")
   {
   window.alert("Please enter an Organization Name");
   document.OrganizationEntryForm.OrganizationName.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.ContactName.value == "")
   {
   window.alert("Please enter a contact name");
   document.OrganizationEntryForm.ContactName.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.ResponsibilityLevel.value == "")
   {
   window.alert("Please select your organizational role.");
   document.OrganizationEntryForm.ResponsibilityLevel.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.ReferralMethod.value == "")
   {
   window.alert("Please select how you found out about Churchteams");
   document.OrganizationEntryForm.ReferralMethod.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.EmailAddress.value == "")
   {
   window.alert("Please enter the contact E-mail Address");
   document.OrganizationEntryForm.EmailAddress.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.EmailAddress.value != "")
   {
   if (!isEmail(document.OrganizationEntryForm.EmailAddress.value))
      {
      window.alert("Invalid email address, please re-enter.");
      document.OrganizationEntryForm.EmailAddress.focus();
      blnValidInput = false;
	  };
   };

return blnValidInput;
};

function validateInput()
{
var blnValidInput = true;
if (document.OrganizationEntryForm.OrganizationName.value == "")
   {
   window.alert("Please enter an Organization Name");
   document.OrganizationEntryForm.OrganizationName.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.MemberCount.value == "")
   {
   window.alert("Please enter the estimated size of your GroupFinder database.");
   document.OrganizationEntryForm.MemberCount.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.PaymentFrequency.value == "")
   {
   window.alert("Please select the payment frequency.");
   document.OrganizationEntryForm.PaymentFrequency.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.SmallGroupAdminPassword.value == "")
   {
   window.alert("Please enter an Admin Password");
   document.OrganizationEntryForm.SmallGroupAdminPassword.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.CommunityLeaderPassword.value == "")
   {
   window.alert("Please enter a Community Leader Password");
   document.OrganizationEntryForm.CommunityLeaderPassword.focus();   
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.CoachPassword.value == "")
   {
   window.alert("Please enter a Coach Password");
   document.OrganizationEntryForm.CoachPassword.focus();   
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.GroupLeaderPassword.value == "")
   {
   window.alert("Please enter a Group Leader Password");
   document.OrganizationEntryForm.GroupLeaderPassword.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.MemberPassword.value == "")
   {
   window.alert("Please enter a Member Password");
   document.OrganizationEntryForm.MemberPassword.focus();
   blnValidInput = false;
   }
else if ((document.OrganizationEntryForm.MemberPassword.value == document.OrganizationEntryForm.GroupLeaderPassword.value)
   || (document.OrganizationEntryForm.MemberPassword.value == document.OrganizationEntryForm.CoachPassword.value)
   || (document.OrganizationEntryForm.MemberPassword.value == document.OrganizationEntryForm.CommunityLeaderPassword.value)
   || (document.OrganizationEntryForm.MemberPassword.value == document.OrganizationEntryForm.SmallGroupAdminPassword.value)
   || (document.OrganizationEntryForm.GroupLeaderPassword.value == document.OrganizationEntryForm.CoachPassword.value)
   || (document.OrganizationEntryForm.GroupLeaderPassword.value == document.OrganizationEntryForm.CommunityLeaderPassword.value)
   || (document.OrganizationEntryForm.GroupLeaderPassword.value == document.OrganizationEntryForm.SmallGroupAdminPassword.value)
   || (document.OrganizationEntryForm.CoachPassword.value == document.OrganizationEntryForm.CommunityLeaderPassword.value)
   || (document.OrganizationEntryForm.CoachPassword.value == document.OrganizationEntryForm.SmallGroupAdminPassword.value)
   || (document.OrganizationEntryForm.CommunityLeaderPassword.value == document.OrganizationEntryForm.SmallGroupAdminPassword.value))
   {
   window.alert("Your small group administrator, community leader, coach, leader, and member passwords must all be different.  Please re-enter them.");
   document.OrganizationEntryForm.SmallGroupAdminPassword.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.ContactName.value == "")
   {
   window.alert("Please enter the contact name");
   document.OrganizationEntryForm.ContactName.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.ResponsibilityLevel.value == "")
   {
   window.alert("Please select your organizational role.");
   document.OrganizationEntryForm.ResponsibilityLevel.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.EmailAddress.value == "")
   {
   window.alert("Please enter the contact E-mail Address");
   document.OrganizationEntryForm.EmailAddress.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.Website.value == "")
   {
   window.alert("Please enter your website URL");
   document.OrganizationEntryForm.Website.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.Address.value == "")
   {
   window.alert("Please enter your address");
   document.OrganizationEntryForm.Address.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.ReferralMethod.value == "")
   {
   window.alert("Please select how you found out about Churchteams");
   document.OrganizationEntryForm.ReferralMethod.focus();
   blnValidInput = false;
   }
else if (document.OrganizationEntryForm.EmailAddress.value != "")
   {
   if (!isEmail(document.OrganizationEntryForm.EmailAddress.value))
      {
      window.alert("Invalid email address, please re-enter.");
      document.OrganizationEntryForm.EmailAddress.focus();
      blnValidInput = false;
	  };
   };
return blnValidInput;
}


function linkWarning(strChurchName, strLink)
{
window.alert(strChurchName + ' has been kind enough to allow Churchteams to use their site as an example so please respect their contribution and do not enter any profiles or other data in their database.  Thank You.');
location.href=strLink;
};
