
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 += validateName(theForm.playerFirst, "First Name");	
	reason += validateName(theForm.playerLast, "Last Name");		
	reason += validateChoice(theForm.dobm,"DOB Month");
	reason += validateChoice(theForm.dobd, "DOB Day");
	reason += validateChoice(theForm.doby, "DOB Year");
	reason += validateIllegal(theForm.school, "School Name");
	reason += validateChoice(theForm.grade, "Current 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.");

			for (i = 0; i < 2; i++) {
				var checked = theForm.gender[i].checked;
				if (checked) {
					var myGender = theForm.gender[i].value;
				}
			}

			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 + ":";
			recdata += "gender=" + myGender + ":";
			
			/* 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 != "") {
      	/* alert(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){

/* check for all the mandatory fields... */
	var reason = "";
	var myGender = "";
	var theone = "";
	
	/* alert('entered function'); */
	reason += validateName(theForm.playerFirst, "First Name");	
	reason += validateName(theForm.playerLast, "Last Name");		
	reason += validateChoice(theForm.dobm,"DOB Month");
	reason += validateChoice(theForm.dobd, "DOB Day");
	reason += validateChoice(theForm.doby, "DOB Year");
	reason += validateIllegal(theForm.school, "School Name");
	reason += validateChoice(theForm.grade, "Current Grade");
	
	reason += validateStNum(theForm.stNum);
	reason += validateIllegal(theForm.street, 'street name');
	reason += validateIllegal(theForm.city, 'city');
	reason += validateIllegal(theForm.county, 'county name');
	reason += validateEmpty(theForm.state);
	reason += validateIllegal(theForm.zip, 'zipcode.');
	
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
	}
	else
	{
	
		for (i = 0; i < 2; i++) {
			if (theForm.gender[i].checked==true) {
				theone=i;
			}
		}
		if (theone == "0") {
			myGender = "m";
		}
		if (theone == "1") {
			myGender = "f";
		}

		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 + ":";
		recdata += myGender;

		/* 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 validateName(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 " + name + ".\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 30)) {
        fld.style.background = 'Yellow'; 
        error = "Please use between 2 and 30 characters for your name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The information you entered for "+fld+" contains illegal characters. Please try again.\n";
    } else {
        fld.style.background = 'White';
    }
    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 for " + name + " 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;
}
function validateSchool(fld, fldName) {
    alert("testing");
    var error = "";
    var illegalChars=/^\d{1,2}\/\d{1,2}\/\d{4}$/ ;
    if (fld == "") {
	    alert(fld+" is empty");
	    fld.style.background = 'Yellow'; 
        error = "You didn't enter a value in the field for " + fldName + ".\n";
    } else if (illegalChars.test(fld.value)) {
        error = "A field contains illegal characters. Please review each section and remove any characters that are not letters or numbers.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    
   return error;
}

function validateStNum(fld) {
    var error = "";
    var onlynumbers = "/[0-9]/g";    
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a street number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The street number contains illegal characters. Be sure to only put numbers in this field.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}



function validatePhoneCheck(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}
function validateEmpty(fld) {
    var error = "";
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateIllegal(fld, fldName) {
	var error = "";
	var illegalChars=/[\(\)\<\>\,\;\:\\\/\"\'\[\]]/;
	
	if (illegalChars.test(fld.value)) {
	   error = "The " + fldName + " contains illegal characters.\n";
	   fld.style.background = 'Yellow';
	}
	
	return error;
}
