/* ------------------------------------------------------
------------------Global & Date Variables----------------
-------------------------------------------------------*/
var dtToday = new Date();
var dtCh= "/";
var minYear=((dtToday.getFullYear())-121);
var maxYear=((dtToday.getFullYear())-21);

/*-----------------------------------------------------------
------------Good Sport / SL Eval / Persistent Offender-------
------------------Validation Function------------------------
-----------------------------------------------------------*/

function checkPOffender(formName) {
	var badfields = 0;
   		badfields += checkComments(document.forms[formName.name].comments, "comments", "*Please Provide A Detailed Description Below.");
	  	//badfields += checkZipCode(document.forms[formName.name].divteam, "divteam", "Division/Team");
		badfields += checkNameField(document.forms[formName.name].player_name, "pname", "*Enter A Player Name.");
		badfields += checkZipCode(document.forms[formName.name].player_no, "pno", "Player Number");
	if (badfields != 0){
		if (badfields == 1) {document.getElementById("alertMsg").innerHTML="<p class='footer'><font color='red'><b>*You have 1 field that is not completed correctly.</b>";alert("You have 1 field that is not completed correctly.\nThere will be a red error message below that field.\nPlease correct the field and re-submit.")}
		else if (badfields > 1) {document.getElementById("alertMsg").innerHTML="<p class='footer'><font color='red'><b>*You have " + badfields + " fields above that are not completed correctly.</b>"; alert("You have " + badfields + " fields that are not completed correctly.\nThere will be a red error message below each of these fields.\nPlease correct these fields and re-submit.")}
		return false
		}
		return true;
}

function checkGoodSport(formName) {
	var badfields = 0;
   		badfields += checkComments(document.forms[formName.name].comments, "comments", "*Please Provide A Detailed Description Below.");
	  	//badfields += checkZipCode(document.forms[formName.name].divteam, "divteam", "Division/Team");
		badfields += checkNameField(document.forms[formName.name].player_name, "pname", "*Enter A Player Name.");
		badfields += checkZipCode(document.forms[formName.name].player_no, "pno", "Player Number");
	if (badfields != 0){
		if (badfields == 1) {document.getElementById("alertMsg").innerHTML="<p class='footer'><font color='red'><b>*You have 1 field that is not completed correctly.</b>";alert("You have 1 field that is not completed correctly.\nThere will be a red error message below that field.\nPlease correct the field and re-submit.")}
		else if (badfields > 1) {document.getElementById("alertMsg").innerHTML="<p class='footer'><font color='red'><b>*You have " + badfields + " fields above that are not completed correctly.</b>"; alert("You have " + badfields + " fields that are not completed correctly.\nThere will be a red error message below each of these fields.\nPlease correct these fields and re-submit.")}
		return false
		}
		return true;
}

function checkSLEval(formName) {
	var badfields = 0;
		badfields += checkPullDown(document.forms[formName.name].disposition, "dispo", "*Enter A Disposition.");
		badfields += checkPullDown(document.forms[formName.name].slltp, "slwp", "*Enter Players Skill Level When You Played Them.");
		badfields += checkPullDown(document.forms[formName.name].slsuggest, "sugsl", "*Enter Your Suggested Skill Level For This player.");
   		badfields += checkComments(document.forms[formName.name].comments, "comments", "*Please Provide A Detailed Description Below.");
	  	//badfields += checkZipCode(document.forms[formName.name].divteam, "divteam", "Division/Team");
		badfields += checkNameField(document.forms[formName.name].player_name, "pname", "*Enter A Player Name.");
		badfields += checkZipCode(document.forms[formName.name].player_no, "pno", "Player Number");
	if (badfields != 0){
		if (badfields == 1) {document.getElementById("alertMsg").innerHTML="<p class='footer'><font color='red'><b>*You have 1 field that is not completed correctly.</b>";alert("You have 1 field that is not completed correctly.\nThere will be a red error message below that field.\nPlease correct the field and re-submit.")}
		else if (badfields > 1) {document.getElementById("alertMsg").innerHTML="<p class='footer'><font color='red'><b>*You have " + badfields + " fields above that are not completed correctly.</b>"; alert("You have " + badfields + " fields that are not completed correctly.\nThere will be a red error message below each of these fields.\nPlease correct these fields and re-submit.")}
		return false;
		}
		return true;
}

/*----------------------------------------------------------------------------------------------------------------------------
------------------------------------------More Info Master Function-----------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------*/

	function showHideOtherLeagues(Field, ID){
		var OtherLGS = document.getElementById("OtherLeague");
		var OtherAPA = document.getElementById("formerAPAleague");
			if (Field.value == "No"){
				var ID = document.getElementById(ID);
				ID.style.display = 'none';
			}
			if (Field.value == "Yes" ){
				var ID = document.getElementById(ID);
				ID.style.display = 'inline'
			}			
	}
	
function checkWholeForm(MoreInfo) {
	var badfields = 0;
	var form = document.MoreInfo;

		badfields += checkOptionalPhone(form, form.Work_Cell_Tele, form.Work_Cell_Is, "STele");
		badfields += checkTelephone(form.Home_Tele, "HTele", "*Your Home Telephone ");
		badfields += checkEmail(form.Email, form.Email_Confirm, "Email");
		badfields += checkDOB(form.DOB, "DOB", "*Your ");
		//badfields += checkZipCode(form.Zip, "Zip", "Zip Code");
		badfields += checkPullDown(form.State_ID, "State", "*Enter Your State Please.");
		badfields += checkNameField(form.City, "City", "*Enter Your City/Province Please.");
		badfields += checkNameField(form.Address, "Addy", "*Enter Your Street Address Please.");
		badfields += checkNameField(form.Full_Name, "FullName", "*Enter Your Full Name Please.");

	if (badfields != 0){
		if (badfields == 1) {document.getElementById("alertMsg").innerHTML="<p class='footer'><font color='red'><b>*You have 1 field that is not completed correctly.</b>";alert("You have 1 field above that is not completed correctly.\nThere will be a red error message below that field.\nPlease correct the field and re-submit.")}
		else if (badfields > 1) {document.getElementById("alertMsg").innerHTML="<p class='footer'><font color='red'><b>*You have " + badfields + " fields above that are not completed correctly.</b>"; alert("You have " + badfields + " fields that are not completed correctly.\nThere will be a red error message below each of these fields.\nPlease correct these fields and re-submit.")}
		return false
		}
		form.validated.value = "true";
		return true;
}


/*-----------------------------------------------------------
---------------Team Split Form------------------------------
--------------Validation Functions----------------------------
-----------------------------------------------------------*/

function showTeamDetails(yourTeams) {
		var form = document.yourTeams;
	if (form.splitTeams.value == "default") {alert("You must select a team to continue!"); return false;}
	else {form.showForm.value = "true"; return true;}
}
function processMembers(teamMembers) {
	var form = document.teamMembers;
 	form.bolComMembers.value = "false";
	var badfields = 0;
	var t1players = 0;
	var t2players = 0;
	var err = ""
		for (i=0, n=form.team1.length; i<n; i++){
	       	if (form.team1[i].checked){
	           t1players ++;
	   		} 
	   	}
		for (i=0, n=form.team2.length; i<n; i++){
	       	if (form.team2[i].checked){
	           t2players ++;
	   		} 
	   	}
			if (t1players < 3) {err += "Team #1 must have at least 3 original members.\n";}
			if (t2players < 3) {err += "Team #2 must have at least 3 original members.\n";}
			if (t1players >= 3 && t2players >= 3) {
				for (i=0, n=form.team1.length; i<n; i++){
					if (form.team1[i].checked && form.team2[i].checked){
						form.bolComMembers.value = "true";
					}
				}
			}
		badfields += checkPullDown(form.t1_tavern_state, "t1_state", "*Enter Team #1's Tavern State.");
		badfields += checkPullDown(form.t2_tavern_state, "t2_state", "*Enter Team #2's Tavern State.");
		badfields += checkNameField(form.t1_tavern_city, "t1_city", "*Enter Team #1's Tavern City/Town.");
		badfields += checkNameField(form.t2_tavern_city, "t2_city", "*Enter Team #2's Tavern City/Town.");
		badfields += checkNameField(form.t1_tavern_name, "t1_tavernname", "* Enter Team #1's Tavern Name.");
		badfields += checkNameField(form.t2_tavern_name, "t2_tavernname", "* Enter Team #2's Tavern Name.");
		badfields += checkPullDown(form.t1_tavern_tables, "t1_NoTables", "*Tell Us How Many Tables This Tavern Has.");
		badfields += checkPullDown(form.t2_tavern_tables, "t2_NoTables", "*Tell Us How Many Tables This Tavern Has.");
		//badfields += checkPullDown(form.t1_night_of_play, "t1_NOP", "*Select Team #1's Night Of Play.");
		//badfields += checkPullDown(form.t2_night_of_play, "t2_NOP", "*Select Team #2's Night Of Play.");
		//badfields += checkPullDown(form.t1_game_format, "t1_format", "*Select Team #1's League Format.");
		//badfields += checkPullDown(form.t2_game_format, "t2_format", "*Select Team #2's League Format.");
	if (badfields != 0){
		if (badfields == 1) {document.getElementById("alertMsg").innerHTML="<p><font color='red'><b>*You have 1 field that is not completed correctly.</b></font></p>"; alert("You have 1 field that is not completed correctly.\nThere will be a red error message below that field.\nPlease correct the field and re-submit.")}
		else if (badfields > 1) {document.getElementById("alertMsg").innerHTML="<p><font color='red'><b>*You have " + badfields + " fields above that are not completed correctly.</b></font></p>"; alert("You have " + badfields + " fields that are not completed correctly.\nThere will be a red error message below each of these fields.\nPlease correct these fields and re-submit.")}
		return false;
 	}

			if (form.bolComMembers.value == "true"){
				err += "Members MAY NOT play on both teams because the NEW Teams will play in the same division on the same night. Please go back and re-assign your current roster to one team or the other.";
			}
			if (err != ""){alert(err); return false;}
			else {form.showForm2.value = "true"; return true;}
}

function  completeRegistration(newTeams){
	var t1capt_picked = false;
	var t1cocapt_picked = false;
	var t2capt_picked = false;
	var t2cocapt_picked = false;
	var form = document.newTeams;
	err = "";

	for (i=0, n=7; i<n; i++){
		if (form.t1_capt[i].checked) {t1capt_picked = true;}		
		if (form.t1_cocapt[i].checked) {t1cocapt_picked = true;}		
		if (form.t2_capt[i].checked) {t2capt_picked = true;}		
		if (form.t2_cocapt[i].checked) {t2cocapt_picked = true;}		
	}
	
	if (!(t1capt_picked)){err += "Team #1: You must pick a team captain.\n"}
	if (!(t1cocapt_picked)){err += "Team #1: You must pick a team co-captain.\n"}
	if (!(t2capt_picked)){err += "Team #2: You must pick a team captain.\n"}
	if (!(t2cocapt_picked)){err += "Team #2: You must pick a team co-captain.\n"}

	for (i=0, n=7; i<n; i++){
		if (form.t1_capt[i].checked && form.t1_cocapt[i].checked) {
			err += "Team #1: You must select DIFFERENT MEMBERS for Captain & Co-Captain\n";
		break;
		}
	}
	for (i=0, n=7; i<n; i++){
		if (form.t2_capt[i].checked && form.t2_cocapt[i].checked) {
			err += "Team #2: You must select DIFFERENT MEMBERS for Captain & Co-Captain\n";
			break;
		}
	}
	if(err != ""){
		alert (err);
		return false;
	}
	form.validated.value = "true";
	return true;
}


/*-----------------------------------------------------------
------------Team Registration Form---------------------------
--------------Validation Function----------------------------
-----------------------------------------------------------

	function checkTeamReg(TeamReg) {
		var form = document.TeamReg;
  		var badfields = 0;
		var NewMembers = 0;
		for (n=0; n<form.new_members.length; n++){
			if (form.new_members[n].checked){NewMembers++;}
		}
	    if ((form.game_format.value != "Masters") && (form.game_format.value != "8-Ball (3 Person Team)") && (form.game_format.value != "9-Ball (3 Person Team)")) {
	    	badfields += checkNameField(form.plyr5_name, "P5", "*Enter A Name For Player 5.");
	  		badfields += checkNameField(form.plyr4_name, "P4", "*Enter A Name For Player 4.");
		}else{	  	
			normalDisplay(form.plyr5_name,"P5");
			normalDisplay(form.plyr4_name,"P4");
		}
		badfields += checkNameField(form.plyr3_name, "P3", "*Enter A Name For Player 3.");
		badfields += checkNameField(form.cocapt_name, "cocaptname", "*Enter Co-Captain's Name.");
		badfields += checkOptionalPhone(form, form.capt_secondtele, form.capt_secondtele_is, "captSecondtele");
		badfields += checkTelephone(form.capt_hometele, "captHtele", "*Captain's Home Phone ");
		badfields += checkDOB(form.capt_DOB, "captDOB", "Captain's ");
		badfields += checkEmail(form.capt_email, form.capt_email_confirm, "captemail");
		badfields += checkNameField(form.capt_name, "captname", "*Please Enter Captain's Name.");
		badfields += checkZipCode(form.tavern_zip, "zip", "Zip Code"); 
		badfields += checkPullDown(form.tavern_state, "state", "*Enter Tavern's State Please.");
		badfields += checkNameField(form.tavern_city, "city", "*Enter Tavern's City/Town Please.");
		badfields += checkNameField(form.tavern_name, "tavernname", "*Please Enter Tavern's Name.");
		badfields += checkPullDown(form.tavern_tables, "NoTables", "*Please Tell Us How Many Tables This Tavern Has.");
		badfields += checkPullDown(form.night_of_play, "NOP", "*Select Your Night Of Play Please.");
		badfields += checkPullDown(form.game_format, "format", "*Select A League Format.");
		badfields += checkPullDown(form.registration, "reg_type", "*We Need To Know If Your Team Played Last Session.");		
	
	if (badfields != 0){
		if (badfields == 1) {document.getElementById("alertMsg").innerHTML="<p><font color='red'><b>*You have 1 field that is not completed correctly.</b></font></p>"; alert("You have 1 field that is not completed correctly.\nThere will be a red error message below that field.\nPlease correct the field and re-submit.")}
		else if (badfields > 1) {document.getElementById("alertMsg").innerHTML="<p><font color='red'><b>*You have " + badfields + " fields above that are not completed correctly.</b></font></p>"; alert("You have " + badfields + " fields that are not completed correctly.\nThere will be a red error message below each of these fields.\nPlease correct these fields and re-submit.")}
		return false;
 	}
	form.new_memb_total.value = NewMembers;
	form.validated.value = "true";
	return true;
}
*/
/*-----------------------------------------------------------
------------Sportsmanship Form_---------------------------
--------------Validation Function----------------------------
-----------------------------------------------------------*/

	function checkSportsVio(Sportsmanship){
		var form = document.Sportsmanship;
		var badfields = 0;

	   	badfields += checkComments(form.details, "details","*Please Enter A Detailed Description Of The Event Below.");
	/*	for (i=0, n=form.complaints.length; i<n; i++){
	       	if (form.complaints[i].checked){
	           	var ComplaintsChecked = form.complaints[i].value;
	           	break;
	   		} 
	   	}
		badfields += checkRadios(ComplaintsChecked, "complaint" ,"*Please Check One Complaint Form The List Below.");		
		for (i=0, n=form.they_had_bylaws.length; i<n; i++){
	       	if (form.they_had_bylaws[i].checked){
	           	var TheyBylawsChecked = form.they_had_bylaws[i].value;
	           	break;
	   		} 
	   	}
		badfields += checkRadios(TheyBylawsChecked, "theybylaws" ,"*Did The Other Team Have A Copy Of The Bylaws With Them.");		
		for (i=0, n=form.we_had_bylaws.length; i<n; i++){
	       	if (form.we_had_bylaws[i].checked){
	           	var WeBylawsChecked = form.we_had_bylaws[i].value;
	           	break;
	   		} 
	   	}
		badfields += checkRadios(WeBylawsChecked, "webylaws" ,"*Did your team have a copy of the Bylaws with you.");		
	   	*/
		badfields += checkPullDown(form.disposition, "dispo", "*Enter Other Captain's Attitude towards this issue.");
		badfields += checkNameField(form.problem_P1No, "ProbP1No","*Please enter problem Player's Number.");
		badfields += checkNameField(form.problem_P1Name, "ProbP1Name","*Please enter Problem Player's Name.");
		badfields += checkNameField(form.div_team, "divteam", "*Please tell us the team name or Div/team #.");
		badfields += checkPullDown(form.my_team_was, "teamwas", "*Was your team home or away?");
		badfields += checkNameField(form.tavern, "tavern","*Please enter a Tavern Name.");
		badfields += checkPullDown(form.match, "match", "*Pick A Match.");
		badfields += checkPullDown(form.week, "week", "*Pick A Week.");
		badfields += checkPullDown(form.session, "session", "*Pick A Session.");
		badfields += checkDate(form.incident_date, "DOI", "Date of incident ");

	if (badfields != 0){
		if (badfields == 1) {document.getElementById("alertMsg").innerHTML="<p><b><font color='red'>*You have 1 field above that is not completed correctly.</font></b></p>"; alert("You have 1 field that is not completed correctly.\nThere will be a red error message below that field.\nPlease correct the field and re-submit.")}
		else if (badfields > 1) {document.getElementById("alertMsg").innerHTML="<p><b><font color='red'>*You have " + badfields + " fields above that are not completed correctly.</font></b></p>";alert("You have " + badfields + " fields that are not completed correctly.\nThere will be a red error message below each of these fields.\nPlease correct these fields and re-submit.")}
		return false;
	}
	form.validated.value = "true";
	return true;
	}

/*----------------------------------------------------------------------------------------------------------------------------
------------------------------------Date Of Birth Validation for all Forms----------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------*/

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;
    }
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++){ // Search through string's characters one by one. If character is not in bag, append to returnString.
        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, variable){
	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){
		dobError = "*DOB Should Be Formatted: MM/DD/YYYY.";
		dateError = "*Date Should Be Formatted: MM/DD/YYYY.";
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		dobError = "*DOB Contains An Invalid Month";
		dateError = "*Date Contains An Invalid Month.";
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		dobError = "*DOB Contains An Invalid Day.";
		dateError = "*Date Contains An Invalid Day.";
		return false
	}
	if (variable == "DOB") {	
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
			dobError = "*DOB Contains An Invalid Year.";
			return false
		}
	}else if (strYear.length != 4 || year==0 || year > dtToday.getFullYear() || (year < ((dtToday.getFullYear()) -1))){
		dateError = "*Date Contains An Invalid Year.";
		return false;
	}	

	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		dobError = "*DOB Contains Invalid Characters.";
		dateError = "*Date Contains Invalid Characters.";
		return false
	}
	return true
}

function checkDOB(field, reasonVariable, ID){
	var dt = field;
	var error = 0;
	if (isDate(dt.value, "DOB")==false){error = 1;highlightErrors(field,reasonVariable, dobError);}
	else{normalDisplay(field,reasonVariable);}
  	return error;
}

function checkDate(field, reasonVariable, ID){
	var dt = field;
	var error = 0;
	if (isDate(dt.value, "DATE")==false){error = 1;highlightErrors(field,reasonVariable, dateError);}
	else{normalDisplay(field,reasonVariable);}
   	return error;
}

/*------------------------------------------------------
 --------Blank Field/Name or Comments Supplied Validation--------------------
------------------------------------------------------*/

function isInteger2(s, reasonVariable, errorMsg){
	var error = 0;
	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"))) {error=1;highlightErrors(s,reasonVariable, errorMsg);}
		break;
    }
	if (error == 0) {normalDisplay(field,reasonVariable);}
    return error;
}

function checkNameField(field, reasonVariable, errorMsg){	
		var error = 0;
		if (field.value == null || field.value.length == 0) {error=1;highlightErrors(field,reasonVariable, errorMsg);}
		else{
			var c = "";
			for (i = 0; i < field.value.length; i++){
				if (i == 0) {c += field.value.charAt(i).toUpperCase();}
				else if ((field.value.charAt(i-1) == " " || field.value.charAt(i-1) == "." || field.value.charAt(i-1) == "'" || field.value.charAt(i-1) == "-")) {c += field.value.charAt(i).toUpperCase();}
				else {c += field.value.charAt(i).toLowerCase();}
			}
			field.value = c;
		normalDisplay(field,reasonVariable);}
		return error;
}

function checkComments(field, reasonVariable, errorMsg){
		var error = 0;
		if ((field.value == null || field.value.length == 0) || field.value.length < 20) {error=1;highlightErrors(field, reasonVariable, errorMsg);}
		if (error == 0) {normalDisplay(field,reasonVariable);}
    	return error;
	}

/*------------------------------------------------------
 --------Email Validation-------------------------------
------------------------------------------------------*/

function checkEmail (field, confirm, reasonVariable) {
	var error = 0;
	var emailFilter=/^.+@.+\..{2,3}$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
	if (field.value == null || field.value.length == 0) {error = 1; highlightErrors(field,reasonVariable, "*Please Enter Your Email Address.");}
	else if (!(emailFilter.test(field.value))) {error = 1; highlightErrors(field,reasonVariable, "*Email Address Is Invalid.");}
	else if (field.value.match(illegalChars)) {error = 1; highlightErrors(field,reasonVariable, "*Email Address Contains Illegal Characters.");}
	else{		
		for (i=0; i < field.value.length; i++){
			if (field.value.charAt(i) != confirm.value.charAt(i)){error = 1; highlightErrors(field,reasonVariable, "*Email Addresses Do Not Match.");break;} 
		}
	}
	if (error == 0) {normalDisplay(field,reasonVariable);field.value=field.value.toLowerCase();confirm.value=confirm.value.toLowerCase();}
return error;
}
/*------------------------------------------------------
 --------Telephone Validation---------------------------
------------------------------------------------------*/

function checkTelephone(field, reasonVariable, errorMsg) {
		var error = 0;
		var valid = "0123456789";
		var stripped = field.value.replace(/[\(\)\.\-\ ]/g, ''); //strips out non-numeric
			if (field.value == null || field.value.length == 0) {error = 1; highlightErrors(field,reasonVariable, errorMsg + " Must Be Provided.");}
			else if (!(stripped.length == 10)) {error = 1; highlightErrors(field,reasonVariable, errorMsg +  " Must Be 10 Digits.");}
		for (var i=0; i < stripped.length; i++) {
			temp = "" + stripped.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {error = 1; highlightErrors(field,reasonVariable, errorMsg +  " Contains Illigal Characters.");}
		}
		if (error == 0) {normalDisplay(field,reasonVariable);field.value = reformatPhone(stripped);}
		return error;
	}

function checkOptionalPhone(form, field, option, reasonVariable) {
		var error = 0;
		var valid = "0123456789";
		var stripped = field.value.replace(/[\(\)\.\-\ ]/g, ''); //strips out non-numeric
		if (field.value == null || field.value.length == 0) {error = 0;  normalDisplay(field,reasonVariable); return error;}
		else if (!(stripped.length == 10)) {error = 1; highlightErrors(field,reasonVariable, "*Your Work/Cell Must Be 10 Digits.");}
		for (var i=0; i < stripped.length; i++) {
			temp = "" + stripped.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {error = 1; highlightErrors(field,reasonVariable, "*Your Work/Cell Contains Illigal Characters.");}
		}
		if (error == 0) {
			field.value = reformatPhone(stripped);
			for (i=0, n=option.length; i<n; i++){
       			if (option[i].checked){
           			var WorkCellChecked = option[i].value;
           			break;
   				} 
   			}
   			if (!(WorkCellChecked)) {error = 1;highlightErrors(field, reasonVariable, "*Is This Your Work Or Cell Number?");}
			if (error == 0) {normalDisplay(field,reasonVariable);field.value = reformatPhone(stripped);}
		}
		return error;
	}
	
	
/*------------------------------------------------------
---Zip & Postal Code Validation / Player numbers also---
------------------------------------------------------*/

function checkZipCode(field, reasonVariable, errMsg){
		var error = 0;
		var valid = "0123456789";
		if (field.value.length != 5) {error=1; highlightErrors(field,reasonVariable, "*Enter A " + errMsg + ".");}
		for (var i=0; i < field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {error = 1; highlightErrors(field,reasonVariable, "*Invalid " + errMsg + ".");}
		}			
		if (error == 0) {normalDisplay(field,reasonVariable);}
		return error;
}

function checkPostalCode(field, reasonVariable, errMsg){ 
		var error = 0;
			
		if (field.length < 7) {error=1; highlightErrors(field,reasonVariable, "*Enter A 6 digit Postal Code Including A Space.");}
		
		if(field.length == 7) {
			field=field.toUpperCase();   // in case of lowercase characters
			field.value = field; // correct this for the searcher
			// Check for legal characters in string - note index starts at zero
			if('ABCEGHJKLMNPRSTVXY'.indexOf(field.charAt(0))<0) {error = 1; highlightErrors(field,reasonVariable, "*First Postal Code Character Is Invalid.");}
			if('0123456789'.indexOf(field.charAt(1))<0) {error = 1; highlightErrors(field,reasonVariable, "Second Postal Code Character Is Invalid.");}
			if('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(field.charAt(2))<0) {error = 1; highlightErrors(field,reasonVariable, "Third Postal Code Character Is Invalid.");}
			if(" ".indexOf(field.charAt(3)) <0) {error = 1; highlightErrors(field,reasonVariable, "Forth Postal Code Character Must Be A Space.");}
			if('0123456789'.indexOf(field.charAt(4))<0) {error = 1; highlightErrors(field,reasonVariable, "Fifth Postal Code Character Is Invalid.");}
			if('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(field.charAt(5))<0) {error = 1; highlightErrors(field,reasonVariable, "Sixth Postal Code Character Is Invalid.");}
			if('0123456789'.indexOf(field.charAt(6))<0) {error = 1; highlightErrors(field,reasonVariable, "Seventh Postal Code Character Is Invalid.");}
		}
		if (error == 0) {normalDisplay(field,reasonVariable);}
		return error;
}


/*------------------------------------------------------
 --------Radio Button Validation------------------------
------------------------------------------------------*/

function checkCompRadio(checkcomplaints, reasonVariable, errMsg){
	var error = 0;
	if (!(checkcomplaints)){error = 1;document.getElementById(reasonVariable + "reason").innerHTML = errMsg;}
	else {document.getElementById(reasonVariable + "reason").innerHTML = "";}
return error;    
}
function checkRadios(Radios, reasonVariable, errMsg){
	var error = 0;
	if (!(Radios)){error = 1;document.getElementById(reasonVariable + "reason").innerHTML = "<p class='highlight'><span class='small'>" + errMsg + "</span></p>";}
	else {document.getElementById(reasonVariable + "reason").innerHTML = "";}
return error;    
}
/*------------------------------------------------------
 --------Pull Down Validation------------------------
------------------------------------------------------*/

function checkPullDown(field, reasonVariable, errMsg){
	var error = 0;
	if (field.value == "default" || field.value == "" || field.value.length == 0 || field.value == null) {error = 1; highlightErrors(field,reasonVariable, errMsg);}
	else {normalDisplay(field,reasonVariable);}
	return error;
}


/*--------------------------------------------------------
-----------------Form Reset-------------------------------
--------------------------------------------------------*/
function pageReset(){
	var resetform = confirm('Are you sure you want to reset this form?\n\nThis will clear all information \nand cannot be undone!')
	if (resetform) window.location.reload();
}

/*----------------------------------------------------------
------------Player Finder Functions ------------------------
----------------------------------------------------------*/
function setfields(playerinfo){
	var playerinfoarray = playerinfo.split("|");
	document.forms[0].player_no.value = playerinfoarray[0]
	document.forms[0].player_name.value = playerinfoarray[1]
}
function searchbynumber(playerno){
	var desturl = "player_finder.asp?playerno=" + playerno;
	var MainWindow = window.open (desturl, "","toolbar=no,location=no,menubar=no,width=250,height=250,top=200,left=200,resizeable=no,status=no");
}
function searchbyname(){
	var desturl = "pop_members.asp";
	var MainWindow = window.open (desturl, "","toolbar=no,location=no,menubar=no,width=225,height=625,top=50,left=200,resizeable=no,status=no");
}

/*----------------------------------------------------------
--------Reformat Form Fileds after Validation---------------
----------------------------------------------------------*/
function reformat(s){
	var arg;
	var sPos = 0;
	var result = "";
	for (i = 1; i < reformat.arguments.length; i ++){
		arg = reformat.arguments[i];
		if (i % 2 == 1) result += arg;
		else {
			result += s.substring(sPos, sPos + arg);
			sPos += arg;
		}
	}
	return result;
}


function reformatPhone(USPhone){ return (reformat (USPhone, "(",3,") ",3, "-", 4));}

/*----------------------------------------------------------
--------------Show Form Errors To Users------------------------
----------------------------------------------------------*/
function highlightErrors(field, reasonVariable, errorMsg){
		document.getElementById(reasonVariable + "reason").innerHTML = "<p class='errormsg small'>" + errorMsg;field.focus();
		//document.getElementById(reasonVariable).setAttribute('className', 'required');
		//document.getElementById(reasonVariable).setAttribute('class', 'required');
		field.setAttribute('className', 'required');
		field.setAttribute('class', 'required');
}
function normalDisplay(field, reasonVariable){
			document.getElementById(reasonVariable + "reason").innerHTML = "";
		//document.getElementById(reasonVariable).setAttribute('className', 'normal');
		//document.getElementById(reasonVariable).setAttribute('class', 'normal');
			field.setAttribute('className', 'normal');
			field.setAttribute('class', 'normal');
}


