String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

// 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 isPostalCode(s) {
   rePostalCode = new RegExp(/^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$/);
   if (!rePostalCode.test(s)) {
      return false;
   }
   return true;
}

function isPhoneNumber(s)
{
   // Check for correct phone number
   rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);

   if (!rePhoneNumber.test(s)) {
      return false;
   }
   return true;
}

function validateEmailSimple(oObj) {
   var sEmail = new String(oObj.value).trim();
   oObj.value = sEmail;
   var rx = /^[\w\.-]+@[\w\.-]+\.\w+$/i;
   return (!rx.test(sEmail));
}

function validateEmail(oFrm) {
   var sEmail1 = new String(oFrm.txtEMail.value).trim();
   var sEmail2 = new String(oFrm.txtEMail2.value).trim();
   oFrm.txtEMail.value = sEmail1;
   oFrm.txtEMail2.value = sEmail2;

   var sErrorMsgs = "";

   if (sEmail1=='') {
      sErrorMsgs += "Please supply an e-mail address.<br/>";
   }
   if (sEmail1 != sEmail2) {
      sErrorMsgs += "Your e-mail addresses don't match.<br/>";
   }
   else {
      var rx = /^[\w\.-]+@[\w\.-]+\.\w+$/i;
      if (!rx.test(sEmail1)) {
         sErrorMsgs += "Your e-mail addresses isn't valid.<br/>";
      }
   }
   return sErrorMsgs;
}
function validateUserName(oFrm) {
   var sUName = new String(oFrm.txtUserName.value).trim();
   var sUName2 = new String(oFrm.txtUserName2.value).trim();
   oFrm.txtUserName.value = sUName;
   oFrm.txtUserName2.value = sUName2;

   var sErrorMsgs = "";

   if (sUName=='') {
      sErrorMsgs += "You must supply a User Name.<br/>";
   }
   if (sUName != sUName2) {
      sErrorMsgs += "Your User Names don't match.<br/>";
   }
   return sErrorMsgs;
}

function validatePwds(oFrm) {
   var sPwd1 = new String(oFrm.txtPwd.value).trim();
   var sPwd2 = new String(oFrm.txtPwd2.value).trim();
   oFrm.txtPwd.value = sPwd1;
   oFrm.txtPwd2.value = sPwd2;

   var sErrorMsgs = "";

   if (sPwd1=='') {
      sErrorMsgs += "You must supply a password.<br/>";
   }
   if (sPwd1 != sPwd2) {
      sErrorMsgs += "Your passwords don't match.<br/>";
   }
   return sErrorMsgs;
}
function validateNames(oFrm) {
   var sFirstName = new String(oFrm.txtFirstName.value).trim();
   var sLastName = new String(oFrm.txtLastName.value).trim();
   oFrm.txtFirstName.value = sFirstName;
   oFrm.txtLastName.value = sLastName;

   var sErrorMsgs = "";

   if (sFirstName=='') {
      sErrorMsgs += "Please enter your First Name.<br/>";
   }
   if (sLastName=='') {
      sErrorMsgs += "Please enter your Last Name.<br/>";
   }
   return sErrorMsgs;
}
function validateGender(oFrm) {
   var bSelected=false;
   var sErrorMsgs = "";

   for (i=0;i<=oFrm.optGender.length - 1;i++) {
      if (oFrm.optGender[i].checked) {
         bSelected=true;
         break;
      }
   }
   if (!bSelected) {
      sErrorMsgs += "Please specify your Gender.<br/>";
   }
   return sErrorMsgs;
}
function validateDOB(oFrm) {
   var sErrorMsgs = "";
   if (oFrm.optDayOfBirth.selectedIndex == 0 || oFrm.optMonthOfBirth.selectedIndex == 0 || oFrm.optYearOfBirth.selectedIndex == 0) {
      sErrorMsgs += "Please specify a Date of Birth.<br/>";
   }
   else {
      strDate = oFrm.optMonthOfBirth[oFrm.optMonthOfBirth.selectedIndex].value + "/" + oFrm.optDayOfBirth[oFrm.optDayOfBirth.selectedIndex].value + "/" + oFrm.optYearOfBirth[oFrm.optYearOfBirth.selectedIndex].value;
      if (!isDate(strDate)) {
         sErrorMsgs += "Please specify a valid Date of Birth.<br/>";
      }
   }
   return sErrorMsgs;
}
function validatePhone(oFrm) {
   var sErrorMsgs = "";
   var sDayPhone = new String(oFrm.txtDayPhone.value).trim();
   var sNightPhone = new String(oFrm.txtNightPhone.value).trim();
   oFrm.txtDayPhone.value = sDayPhone;
   oFrm.txtNightPhone.value = sNightPhone;
   if (sDayPhone=='' && sNightPhone=='') {
      sErrorMsgs += "You must specify either a Daytime or a Nighttime Contact Number.<br/>";
   }
   else {
      if (sDayPhone!='') {
         if (!isPhoneNumber(sDayPhone)) {
            sErrorMsgs += "Invalid Day Phone format. Please use the following format: (555) 555-1234<br/>";
         }
      }
      if (sNightPhone!='') {
         if (!isPhoneNumber(sNightPhone)) {
            sErrorMsgs += "Invalid Night Phone format. Please use the following format: (555) 555-1234<br/>";
         }
      }
   }
   return sErrorMsgs;
}
function validateEInfo(oFrm) {
   var sErrorMsgs = "";
   var sEName = new String(oFrm.txtEContactName.value).trim();
   var sEPhone = new String(oFrm.txtEPhoneNumber.value).trim();
   oFrm.txtEContactName.value = sEName;
   oFrm.txtEPhoneNumber.value = sEPhone;

   if (sEName=='' || sEPhone=='') {
      sErrorMsgs += "You must specify Emergency Contact Information.<br/>";
   }
   else {
      if (!isPhoneNumber(sEPhone)) {
         sErrorMsgs += "Invalid Emergency Phone format. Please use the following format: (555) 555-1234<br/>";
      }
   }
   return sErrorMsgs;
}

function validateAddress1(oFrm) {
   var sErrorMsgs = "";
   var sAddress = new String(oFrm.txtAddress1.value).trim();
   
   if (sAddress == '') {
      sErrorMsgs += "You must specify Address Line 1.<br/>";
   }
   return sErrorMsgs;
}

function validateCity(oFrm) {
   var sErrorMsgs = "";
   var sCity = new String(oFrm.txtCity.value).trim();
   
   if (sCity == '') {
      sErrorMsgs += "You must specify a City.<br/>";
   }
   return sErrorMsgs;
}

function validateProvince(oFrm) {
   var sErrorMsgs = "";
   
   if (oFrm.optProvince.selectedIndex == 0) {
      sErrorMsgs += "You must select a Province.<br/>";
   }
   return sErrorMsgs;
}

function validatePostalCode(oFrm) {
   var sErrorMsgs = "";
   
   if (!isPostalCode(new String(oFrm.txtPostalCode.value).trim())) {
      sErrorMsgs += "You must enter a valid Postal Code.<br/>";
   }
   
   return sErrorMsgs;
}

function validate(oFrm, bUserName, bEmail, bPassword) {
   var sErrorMsgs = "";
   if (bUserName) {
      sErrorMsgs += validateUserName(oFrm);
   }
   if (bEmail) {
      sErrorMsgs += validateEmail(oFrm);
   }
   if (bPassword) {
      sErrorMsgs += validatePwds(oFrm);
   }
   sErrorMsgs += validateNames(oFrm);
   sErrorMsgs += validateGender(oFrm);
   sErrorMsgs += validateDOB(oFrm);
   sErrorMsgs += validatePhone(oFrm);
   sErrorMsgs += validateAddress1(oFrm);
   sErrorMsgs += validateCity(oFrm);
   sErrorMsgs += validateProvince(oFrm);
   sErrorMsgs += validatePostalCode(oFrm);
   //sErrorMsgs += validateEInfo(oFrm);
   return sErrorMsgs;
}
function validateTeamName(oFrm) {
   var sTeamName = new String(oFrm.txtTeamName.value).trim();
   oFrm.txtTeamName.value = sTeamName;

   var sErrorMsgs = "";

   if (sTeamName=='') {
      sErrorMsgs += "You must enter your Team Name.<br/>";
   }
   return sErrorMsgs;
}
function validateContactName(oFrm) {
   var sContactName = new String(oFrm.txtContactName.value).trim();
   oFrm.txtContactName.value = sContactName;

   var sErrorMsgs = "";

   if (sContactName=='') {
      sErrorMsgs += "You must enter your Team Contacts Name.<br/>";
   }
   return sErrorMsgs;
}
function validateSimplePhone(oFrm) {
   var sSimplePhone = new String(oFrm.txtDayPhone.value).trim();
   oFrm.txtDayPhone.value = sSimplePhone;

   var sErrorMsgs = "";

   if (sSimplePhone=='') {
      sErrorMsgs += "You must enter your Team Contacts Telephone.<br/>";
   }
   return sErrorMsgs;
}
function validateTeamForm(oFrm) {
   var sErrorMsgs = "";
   sErrorMsgs += validateTeamName(oFrm); //txtTeamName

   sErrorMsgs += (oFrm.optDivision.selectedIndex == 0) ? "You must select a Division.<br/>" : "";

   if (typeof(oFrm.optContactType) != "undefined") {
      // User cannot be both Primary and Secondary
      sErrorMsgs += ((oFrm.optContactType[0].checked) && (oFrm.txtPrimaryEmail.value == oFrm.txtSecondaryEmail.value)) ? "You cannot have the same person as the Primary and Secondary Contact<br/>" : "";

      if (oFrm.optContactType[2].checked) {
         sErrorMsgs += (oFrm.txtPrimaryEmail.value == oFrm.hdnUserEmail.value) ? "You cannot be both the Primary and Secondary Contacts of a Team.<br/>" : "";
      }

      if (oFrm.optContactType[1].checked) {
         sErrorMsgs += (oFrm.txtSecondaryEmail.value == oFrm.hdnUserEmail.value) ? "You cannot be both the Primary and Secondary Contacts of a Team.<br/>" : "";
      }

      // Primary Check
      sErrorMsgs += ((oFrm.optContactType[0].checked || oFrm.optContactType[2].checked) && validateEmailSimple(oFrm.txtPrimaryEmail)) ? "You must enter a valid Primary Contact e-mail address.<br/>" : "";

      // Secondary Check
      sErrorMsgs += ((oFrm.optContactType[0].checked || oFrm.optContactType[1].checked) && validateEmailSimple(oFrm.txtSecondaryEmail)) ? "You must enter a valid Secondary Contact e-mail address.<br/>" : "";
   }

   //sErrorMsgs += " ";
   return sErrorMsgs;
}

function validateTournyRegisterForm(oFrm) {
   var sErrorMsgs = "";
   sErrorMsgs += validateTeamName(oFrm);
   if( !oFrm.isLeagueTeam[0].checked && !oFrm.isLeagueTeam[1].checked ) {
      sErrorMsgs += "You must select Yes or No if you are a SLOPITCH.com league team.<br/>";
   } 
   if( oFrm.requestDiv.value == "" ) {
      sErrorMsgs += "You must request a division to participate in.<br/>";
   } 
   sErrorMsgs += validateContactName(oFrm);  
   sErrorMsgs += validateSimplePhone(oFrm);  
   if (validateEmailSimple(oFrm.txtContactEmail)) {
      sErrorMsgs += "You must enter a valid Team Contacts Email.<br/>";
   }  
   sErrorMsgs += validateCaptcha(oFrm);   

   return sErrorMsgs;      
}

function validateCaptcha(oFrm) {
   var sErrorMsgs = "";
   var challengeField = oFrm.recaptcha_challenge_field.value;
   var responseField = oFrm.recaptcha_response_field.value;

   if( responseField == "" ) {
      sErrorMsgs += "You must attempt the challange question.<br/>";
   }
   
   var html = $.ajax({
      type: "POST",
      url: "includes/codebase/ajax_check_recaptcha.php",
      data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
      async: false
   }).responseText;

   if (html.replace(/^\s+|\s+$/, '') != "success") {
      sErrorMsgs += "The text you entered does not match the challange question. <br/>";
   }
   
   return sErrorMsgs;
}
