var dtCh= "/";
var minYear=2008;
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 strDay=dtStr.substring(0,pos1)
	var strMonth=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){
		/*alert("The date format should be : mm/dd/yyyy")*/
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		/*alert("Please enter a valid month")*/
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		/*alert("Please enter a valid day")*/
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		/*alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)*/
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		/*alert("Please enter a valid date")*/
		return false
	}
return true
}



function validate_login()
{
 
 if( document.login_form.username.value == "" ) {
   document.login_form.username.focus();
   alert("You must supply a username");
   return false;
   }

 
 if( document.login_form.password.value == "" ) {
   document.login_form.password.focus();
   alert("You must supply a password");
   return false;
   }

 return true;
}

function validate_register()
{
 
 if( document.register_form.username.value == "" ) {
   alert("You must supply a username");
   document.register_form.username.focus();
   return false;
   }
 
 if( document.register_form.surname.value == "" ) {
   alert("You must supply a surname");
   document.register_form.surname.focus();
   return false;
   }
 
 if( document.register_form.forename.value == "" ) {
   alert("You must supply a forename");
   document.register_form.forename.focus();
   return false;
   }
 
 if( document.register_form.email.value == "" ) {
   alert("You must enter a valid email address");
   document.register_form.email.focus();
   return false;
   }
   
 if( document.register_form.retypedemail.value == "" ) {
   alert("You must enter your email address twice");
   document.register_form.retypedemail.focus();
   return false;
   }
   
 if( document.register_form.email.value != document.register_form.retypedemail.value ) {
   document.register_form.retypedemail.value = "";
   document.register_form.email.value =  "";
   document.register_form.email.focus();
   alert("Your email addresses don't match, please retype");
   return false;
   }
   
 /*if( document.register_form.password.value == "" ) {
   alert("You must enter a password");
   document.register_form.password.focus();
   return false;
   }
   
 if( document.register_form.repassword.value == "" ) {
   alert("You must confirm your password by entering it twice");
   document.register_form.repassword.focus();
   return false;
   }
   
 if( document.register_form.repassword.value != document.register_form.password.value ) {
   document.register_form.repassword.value = "";
   document.register_form.password.value =  "";
   document.register_form.password.focus();
   alert("Your passwords don't match, please retype");
   return false;
   }*/
   
 return true;  
}

function val_create_comp () {
 valid = true;
 team_count="16";
 var DateStr;
 
 if( document.create_comp.compname.value == "" ) {
   document.create_comp.compname.focus();
   alert("You must provide a competition name");
   return false;
   }
 
 if( document.create_comp.type.value == "L" && document.create_comp.teamcount.value == "" ) {
  document.create_comp.teamcount.focus();
  alert("You must provide the number of teams in a League");
  return false;
  }

/* Validate the date */
/* Create a string from the date, month and year fields using whatever */
/* is in the global variable dtCh as the separator                     */

 DateStr = document.create_comp.start_date.value;
 DateStr += dtCh;
 DateStr += document.create_comp.start_month.value;
 DateStr += dtCh;
 DateStr += document.create_comp.start_year.value;
 
 if (isDate(DateStr)==false){
   document.create_comp.start_date.focus();
   alert("You must provide a valid competition start date");
   return false;
   }
 
 return valid;
}

function val_team_creation_L() {
 for( x=0; x < (document.L_teams.elements.length - 1); x++ ) { 
   if( document.L_teams.elements[x].value == "" ) {
	 document.L_teams.elements[x].focus();
	 alert("All fields must be completed");
	 return false;
     }
  }

 return true;
}

function val_team_creation_C() {
 for( x=0; x < (document.C_teams.elements.length - 1); x++ ) { 
   if( document.C_teams.elements[x].value == "" ) {
	 document.C_teams.elements[x].focus();
	 alert("All fields must be completed");
	 return false;
     }
  }

 return true;
}

function val_team_creation_W() {
 for( x=0; x < (document.W_teams.elements.length - 1); x++ ) { 
   if( document.W_teams.elements[x].value == "" ) {
	 document.W_teams.elements[x].focus();
	 alert("All fields must be completed");
	 return false;
     }
  }

 return true;
}

function val_c_fixture_update() {
/* Just need to check that the winners of a group are not also recorded as runners up */
 
 if( document.c_results_form.winner_group_a.value != "0" ) {
	if( document.c_results_form.winner_group_a.value == document.c_results_form.runner_up_group_a.value ) {
	  alert("Winner of Group A cannot be the same as Runner Up Group A!");
	  return false;
	}
 }

 return true;
}

function val_w_fixture_update() {
/* Just need to check that the winners of a group are not also recorded as runners up */
 
 if( document.w_results_form.winner_group_a.value != "0" ) {
	if( document.w_results_form.winner_group_a.value == document.w_results_form.runner_up_group_a.value ) {
	  alert("Winner of Group A cannot be the same as Runner Up Group A!");
	  return false;
	}
 }

 if( document.w_results_form.winner_group_b.value != "0" ) {
	if( document.w_results_form.winner_group_b.value == document.w_results_form.runner_up_group_b.value ) {
	  alert("Winner of Group B cannot be the same as Runner Up Group B!");
	  return false;
	}
 }

 if( document.w_results_form.winner_group_c.value != "0" ) {
	if( document.w_results_form.winner_group_c.value == document.w_results_form.runner_up_group_c.value ) {
	  alert("Winner of Group C cannot be the same as Runner Up Group C!");
	  return false;
	}
 }
 
  if( document.w_results_form.winner_group_d.value != "0" ) {
	if( document.w_results_form.winner_group_d.value == document.w_results_form.runner_up_group_d.value ) {
	  alert("Winner of Group D cannot be the same as Runner Up Group D!");
	  return false;
	}
 }
 
  if( document.w_results_form.winner_group_e.value != "0" ) {
	if( document.w_results_form.winner_group_e.value == document.w_results_form.runner_up_group_e.value ) {
	  alert("Winner of Group E cannot be the same as Runner Up Group E!");
	  return false;
	}
 }
 
  if( document.w_results_form.winner_group_f.value != "0" ) {
	if( document.w_results_form.winner_group_f.value == document.w_results_form.runner_up_group_f.value ) {
	  alert("Winner of Group F cannot be the same as Runner Up Group F!");
	  return false;
	}
 }
 
  if( document.w_results_form.winner_group_g.value != "0" ) {
	if( document.w_results_form.winner_group_g.value == document.w_results_form.runner_up_group_g.value ) {
	  alert("Winner of Group G cannot be the same as Runner Up Group G!");
	  return false;
	}
 }
 
  if( document.w_results_form.winner_group_h.value != "0" ) {
	if( document.w_results_form.winner_group_h.value == document.w_results_form.runner_up_group_h.value ) {
	  alert("Winner of Group H cannot be the same as Runner Up Group H!");
	  return false;
	}
 }
 
 return true;
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false;
	}
    return true;
 }

function val_c_entry() {

/* this bit checks every input field contains a number           */
/* the x+=3 makes sure the next loop starts at the first field   */
/* in a row.                                                     */

for( x=0; x < 96; x++ ) { 
  if( document.c_entry_form.elements[x+1].value == "" ) {
	document.c_entry_form.elements[x+1].focus();
	alert("All scores must be completed");
	return false;
    }
  if( document.c_entry_form.elements[x+1].value < "0" || document.c_entry_form.elements[x+1].value > "9" ) {
	document.c_entry_form.elements[x+1].focus();
	document.c_entry_form.elements[x+1].select();
	alert("Numbers only");
	return false;
    }
  if( document.c_entry_form.elements[x+3].value == "" ) {
	document.c_entry_form.elements[x+3].focus();
	alert("All scores must be completed");
	return false;
    }
  if( document.c_entry_form.elements[x+3].value < "0" || document.c_entry_form.elements[x+3].value > "9" ) {
	document.c_entry_form.elements[x+3].focus();
	document.c_entry_form.elements[x+3].select();
	alert("Numbers only");
	return false;
    }
  x+=3;
  }
 
 return true;
}

function val_w_entry() {

/* this bit checks every input field contains a number           */
/* the x+=3 makes sure the next loop starts at the first field   */
/* in a row.                                                     */

for( x=0; x < 192; x++ ) { 
  if( document.w_entry_form.elements[x+1].value == "" ) {
	document.w_entry_form.elements[x+1].focus();
	alert("All scores must be completed");
	return false;
    }
  if( document.w_entry_form.elements[x+1].value < "0" || document.w_entry_form.elements[x+1].value > "9" ) {
	document.w_entry_form.elements[x+1].focus();
	document.w_entry_form.elements[x+1].select();
	alert("Numbers only");
	return false;
    }
  if( document.w_entry_form.elements[x+3].value == "" ) {
	document.w_entry_form.elements[x+3].focus();
	alert("All scores must be completed");
	return false;
    }
  if( document.w_entry_form.elements[x+3].value < "0" || document.w_entry_form.elements[x+3].value > "9" ) {
	document.w_entry_form.elements[x+3].focus();
	document.w_entry_form.elements[x+3].select();
	alert("Numbers only");
	return false;
    }
  x+=3;
  }
 
 return true;
}


function validate_member()
{
 
 if( document.new_member_form.username.value == "" ) {
   alert("You must supply a username");
   document.new_member_form.username.focus();
   return false;
   }
 
 if( document.new_member_form.surname.value == "" ) {
   alert("You must supply a surname");
   document.new_member_form.surname.focus();
   return false;
   }
 
 if( document.new_member_form.forename.value == "" ) {
   alert("You must supply a forename");
   document.new_member_form.forename.focus();
   return false;
   }
 
 if( document.new_member_form.email.value == "" ) {
   alert("You must enter a valid email address");
   document.new_member_form.email.focus();
   return false;
   }
   
 if( document.new_member_form.retypedemail.value == "" ) {
   alert("You must enter your email address twice");
   document.new_member_form.retypedemail.focus();
   return false;
   }
   
 if( document.new_member_form.email.value != document.new_member_form.retypedemail.value ) {
   document.new_member_form.retypedemail.value = "";
   document.new_member_form.email.value =  "";
   document.new_member_form.email.focus();
   alert("Your email addresses don't match, please retype");
   return false;
   }
   
 if( document.new_member_form.password.value == "" ) {
   alert("You must enter a password");
   document.new_member_form.password.focus();
   return false;
   }
   
 if( document.new_member_form.repassword.value == "" ) {
   alert("You must confirm your password by entering it twice");
   document.new_member_form.repassword.focus();
   return false;
   }
   
 if( document.new_member_form.repassword.value != document.new_member_form.password.value ) {
   document.new_member_form.repassword.value = "";
   document.new_member_form.password.value =  "";
   document.new_member_form.password.focus();
   alert("Your passwords don't match, please retype");
   return false;
   }
   
 return true;  
}

function validate_update_member()
{
 
 if( document.update_member_form.username.value == "" ) {
   alert("You must supply a username");
   document.update_member_form.username.focus();
   return false;
   }
 
 if( document.update_member_form.surname.value == "" ) {
   alert("You must supply a surname");
   document.update_member_form.surname.focus();
   return false;
   }
 
 if( document.update_member_form.forename.value == "" ) {
   alert("You must supply a forename");
   document.update_member_form.forename.focus();
   return false;
   }
 
 if( document.update_member_form.email.value == "" ) {
   alert("You must enter a valid email address");
   document.update_member_form.email.focus();
   return false;
   }
   
 if( document.update_member_form.retypedemail.value == "" ) {
   alert("You must enter your email address twice");
   document.update_member_form.retypedemail.focus();
   return false;
   }
   
 if( document.update_member_form.email.value != document.update_member_form.retypedemail.value ) {
   document.update_member_form.retypedemail.value = "";
   document.update_member_form.email.value =  "";
   document.update_member_form.email.focus();
   alert("Your email addresses don't match, please retype");
   return false;
   }
   
 if( document.update_member_form.password.value == "" ) {
   alert("You must enter a password");
   document.update_member_form.password.focus();
   return false;
   }
   
 if( document.update_member_form.repassword.value == "" ) {
   alert("You must confirm your password by entering it twice");
   document.update_member_form.repassword.focus();
   return false;
   }
   
 if( document.update_member_form.repassword.value != document.update_member_form.password.value ) {
   document.update_member_form.repassword.value = "";
   document.update_member_form.password.value =  "";
   document.update_member_form.password.focus();
   alert("Your passwords don't match, please retype");
   return false;
   }
   
 return true;  
}

function val_team_positions(){
	
    return true;
 }
