
function registerPlayer1(theForm)
{
	var firstName = theForm.playerFirst.value;
	/* 	var firstName = document.addPlayerForm.playerFirst.value; */
	alert(firstName);
}


function registerPlayer(theForm,bid)
{
	/* check for all the mandatory fields... */
	var reason = "";
	/* alert('entered function'); */
	reason += validatePlayerEmpty(theForm.playerFirst, "First Name");	
	reason += validatePlayerEmpty(theForm.playerLast, "Last Name");		
	reason += validateChoice(theForm.dobm,"DOB Month");
	reason += validateChoice(theForm.dobd, "DOB Day");
	reason += validateChoice(theForm.doby, "DOB Year");
	reason += validateChoice(theForm.grade, "Grade");
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
	}
	else
	{
		alert("Recording new player data for " + theForm.playerFirst.value + " " + theForm.playerLast.value + ". Click OK to continue.");
			var recdata = "";
			recdata += "playerFirst=" + theForm.playerFirst.value + ":";
			recdata += "playerLast=" + theForm.playerLast.value + ":";
			recdata += "dobm=" + theForm.dobm.value + ":";
			recdata += "dobd=" + theForm.dobd.value + ":";
			recdata += "doby=" + theForm.doby.value + ":";
			recdata += "school=" + theForm.school.value + ":";
			recdata += "grade=" + theForm.grade.value + ":";
			recdata += "stNum=" + theForm.stNum.value + ":";
			recdata += "street=" + theForm.street.value + ":";
			recdata += "city=" + theForm.city.value + ":";
			recdata += "county=" + theForm.county.value + ":";
			recdata += "state=" + theForm.state.value + ":";
			recdata += "zip=" + theForm.zip.value + ":";
			recdata += "phone=" + theForm.phone.value + ":";
			recdata += "PID=" + bid + ":";
			/* alert (recdata); */
			var url = "src/CreatePlayer.php";
			request.open("POST", url, true);
			request.onreadystatechange = CreatePlayerResults;
			request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			request.send("dataset=" + escape(recdata));	
	}	
}

function CreatePlayerResults()
{
  if (request.readyState == 4) {
    if (request.status == 200) {
      /* Get the response from the server */
      var results = request.responseText;
      if(results != "") {
      	
				window.location = '/viewPlayers';
	  		
	  	} else {
	  		var message = "Try Again" + results;
	  		alert(message);
	  	}
    } else {
      alert("Error! Request status is " + request.status);
		}
  }
}

function TestCreatePlayerResults()
{
  if (request.readyState == 4) {
    if (request.status == 200) {
      /* Get the response from the server */
      var results = request.responseText;
      if(results != "") {
      	
      	document.getElementById('APS').innertext = results;
      	alert(results);
	  		
	  } else {
	  	alert("The server is busy at the moment, please try again.");
	  }
	  
    } else {
      alert("Error! Request status is " + request.status);
	}
  }
}


function doEditMyPlayer(theForm, pid){
	var recdata = pid + ":";
	recdata += theForm.playerFirst.value + ":";
	recdata += theForm.playerLast.value + ":";
	var msel = theForm.dobm.selectedIndex;
	recdata += theForm.dobm[msel].value + ":";
	var dsel = theForm.dobd.selectedIndex;
	recdata += theForm.dobd[dsel].value + ":";
	var ysel = theForm.doby.selectedIndex;
	recdata += theForm.doby[ysel].value + ":";
	recdata += theForm.stNum.value + ":";
	recdata += theForm.street.value + ":";
	recdata += theForm.city.value + ":";
	recdata += theForm.county.value + ":";
	recdata += theForm.state.value + ":";
	recdata += theForm.zip.value + ":";
	recdata += theForm.phone.value + ":";
	recdata += theForm.school.value + ":";
	recdata += theForm.grade.value + ":";
	recdata += theForm.urlopts.value;

	/* alert (recdata); */
	var url = "src/UpdatePlayerData.php";
	request.open("POST", url, true);
	request.onreadystatechange = UpdatePlayerDataResults;
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send("dataset=" + escape(recdata));	

}

function UpdatePlayerDataResults()
{
	if (request.readyState == 4) {
		if (request.status == 200) {
			/* Get the response from the server */
			var results = request.responseText;
			if(results != "") {
				/* alert(results); */
				if(results == "booster") {
					window.location = '/viewPlayers';
				} else {
					alert('You have updated the players record, and will be returned to the refreshed report.');
					window.location = '/src/RegistrationReport2.php' + results;
				}
	  	} else {
	  		var message = "Try Again" + results;
				alert(message);
			}
    } else {
      alert("Error! Request status is " + request.status);
		}
	}
}


function validateChoice(fld, name) {
    var error = "";
 
    if (fld.value == 0) {
        fld.style.background = 'Yellow'; 
        error = "Please select a value for this field: " + name + ".\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateSport(fld, frm) {
    var error = "";
    var cnt = -1;
    for (var i=fld.length-1; i > -1; i--) {
			if (fld[i].checked) {cnt = i; i = -1; var sval = fld[cnt].value;}
    }
 		
 		if (cnt > -1) {
 			/* fld.style.background = 'White'; */
    } else {
      /* fld.style.background = 'Yellow'; */
		  error = "You didn't select a sport to play - check either Fastpitch Softball or Baseball.\n";
    }
		
    if (sval == "bb") {
    	var agegroup = frm.bbgroup.value;
    	if (agegroup == "0") {
    		/* alert(agegroup); */
				frm.bbgroup.style.background = 'Yellow'; 
				frm.sbgroup.style.background = 'white'; 
				error = "You didn't select an age group for Baseball.\n";
			}
    }
    
    if (sval == "sb") {
    	var agegroup = frm.sbgroup.value;
    	if (agegroup == "0") {
    		/* alert(agegroup); */
				frm.sbgroup.style.background = 'Yellow'; 
				frm.bbgroup.style.background = 'White'; 
				error = "You didn't select an age group for Fastpitch Softball.\n";
			}
    }
 

    return error;
}

function validatePlayerEmpty(fld, name) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in: " + name + "\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateAgree(fld) {
    var error = "";
    var cnt = -1;
    for (var i=fld.length-1; i > -1; i--) {
			if (fld[i].checked) {cnt = i; i = -1;}
    }
 
 		if (cnt > -1) {
 			/* fld.style.background = 'White'; */
    } else {
      /* fld.style.background = 'Yellow'; */
		  error = "Please confirm the accuracy of your information by checking the Certification of Acuuracy checkbox.\n";
    }
    return error;
}
function validateCharacters(fld, name) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a value in the " + name +" field.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 40)) {
        fld.style.background = 'Yellow'; 
        error = "Please use between 2 and 40 characters.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The value you entered contains illegal characters like quotes or apostrophes. Please re-enter the information with letters, numbers or underscore chacters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}