function BrowserCheck() {
var b = navigator.appName;
if (b == "Netscape") this.b = "NS";
else if (b == "Microsoft Internet Explorer") this.b = "IE";
else this.b = b;
this.v = parseInt(navigator.appVersion);
this.NS = (this.b == "NS" && this.v>=4);
this.NS4 = (this.b == "NS" && this.v == 4);
this.NS5 = (this.b == "NS" && this.v == 5);
this.IE = (this.b == "IE" && this.v>=4);
this.IE4 = (navigator.userAgent.indexOf('MSIE 4')>0);
this.IE5 = (navigator.userAgent.indexOf('MSIE 5')>0);
if (this.IE5 || this.NS5) this.VER5 = true;
if (this.IE4 || this.NS4) this.VER4 = true;
this.OLD = (! this.VER5 && ! this.VER4) ? true : false;
this.min = (this.NS||this.IE);
}

function makeAlertBox() {
	is = new BrowserCheck();
	alertBox = (is.VER5) ? document.getElementById("alertLayer").style
	: (is.NS) ? document.layers["alertLayer"]
	: document.all["alertLayer"].style;
	return alertBox;
}

function hideLoginAlert() {
	document.getElementById('userId').value = ""; 
	document.getElementById('password').value = ""; 
	document.getElementById("userId").disabled=false;
	document.getElementById("password").disabled=false;
	document.getElementById("submit").disabled=false;
	document.getElementById("userId").focus();
}
function hideAlert(){
	alertBox.visibility = "hidden";
}

function whcAlert(title, content, buttonLabel) {
	ModalPopups.Alert("whcAlert1",
        title,
        content, 
        {
            okButtonText: buttonLabel,
            onOk: "history.go(-1)",
            width: 300,
            height: 100,
            shadow: false,
            borderColor: "#FF6600",
            titleBackColor: "#000000",
            titleFontColor: "#FF6600",            
            popupFontColor: "#FF6600",            
            popupBackColor: "#000000",
            footerBackColor: "#000000",
            footerFontColor: "#FFFFFF"            
        }
	
    );

}

function whcPopup(message, buttonLabel, width, height, left, top){
	alertBox = makeAlertBox();
	document.all.alertLayer.innerHTML = "<table border=0 width='100%' height='100%'>" +
	"<tr height='5'><td width='5'></td></tr>" +
	"<tr class='alertMessage'><td width='5'></td><td align='left'>" + message + "<br></td><td width='5'></td></tr>" + 
	"<tr height='5'><td width='5'></td></tr>" +
	"<tr class=><td width=5></td><td colspan=2 align=center><input id='okButton' type=button style='font-weight:bold; color:0000BB' value='" + 
	buttonLabel + "' onClick='hideAlert()' class='okButton'></input><BR></td><td width='5'></td></tr>" +
	"<tr class='alertMessage' height='5'><td width='5'></td></tr></table>";

	alertBox.width = width;
	alertBox.height = height;
	alertBox.left = left;
	alertBox.top = top;

	alertBox.visibility = "visible";
}

function whcQuery(title, message, button1Label, button2Label, width, height, left, top){
	alertBox = makeAlertBox();
	document.all.alertLayer.innerHTML = "<table border=0 width=100% height=100%>" +
	"<tr height=5><td colspan=4 class=alertTitle>" + " " + title + "</td></tr>" +
	"<tr height=5><td width=5></td></tr>" +
	"<tr><td width=5></td><td align=center class=alertMessage>" + message + "<BR></td><td width=5></td></tr>" + 
	"<tr height=5><td width=5></td></tr>" +
	"<tr>" +
	"<td width=5></td><td colspan=1 align=center><input id='okButton' type=button style='font-weight:bold; color:0000BB' value='" + 
	button1Label + "' onClick='hideAlert()' class=okButton><BR></td><td width=5></td>" +
	"<td width=5></td><td colspan=1 align=center><input id='okButton' type=button style='font-weight:bold; color:0000BB' value='" + 
	button2Label + "' onClick='hideAlert()' class=okButton><BR></td><td width=5></td>" +
	"</tr>" +
	"<tr height=5><td width=5></td></tr></table>";

	alertBox.width = width;
	alertBox.height = height;
	alertBox.left = left;
	alertBox.top = top;

	alertBox.visibility = "visible";
}

function whcBigAlert(title, message, buttonLabel) {
	whcPopup(message, buttonLabel, 350, 200, 350, 225);
}

function closeMe() {
	window.close();
}


function validRoleStrings(str) {
	var arr = new Array();
	arr = str.split(':');
	for (var i in arr)
	{
		if (!validRoleString(arr[i])) {
			return false;
		}
	} 
	return true;
}


function validRoleString(str) {
	var arr = new Array();
	arr = str.split(';');
	var role = arr[0]; 
	var ageGroup = arr[1];
	var team = arr[2];

	if (role == "Coach") {
		if ((ageGroup != "") && (team != "")) {
			return true;
		}
		else {
			return false;
		}
	}
	else if (role == "AGC") {
		if ((ageGroup != "") && (team == "")) {
			return true;
		}
		else {
			return false;
		}
	}
	else if ((role == "Commissioner") || (role == "Assistant Commissioner") || (role == "Board Member") || (role == "Referee Coordinator")) {
		if ((ageGroup == "") && (team == "")) {
			return true;
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
}


function scoreEntryPopup(ageGroup, homeTeam, visitingTeam, gameDate, gameTime, gameDateTime, siteName) {
	grayOut(true, {'opacity':'40'});
	ModalPopups.Custom("scoreEntryPopup",  
						"<table class='scoreEntryTable' width='100%' align=\"center\" border='0' cellspacing='0' cellpadding='4' align=\"center\">" +
						"<tbody>" +
						"<tr bgcolor=\"#FF6600\">" +
						"<td colspan='2' align='center' <span class='popup-heading'>Score Entry</span></td>" + 
						"<tr bgcolor=\"#000000\">" +
						"<td colspan='2' align='center' width=\"100%\" valign='bottom'><span id=\"ageGroup\" class=\"loginTable\" value=\"ageGroup\">" + ageGroup + "</span></td>" +
						"</tr>" +
						"<tr bgcolor=\"#000000\">" +
						"<td colspan='2' align='center' width=\"100%\" valign='bottom'><span id='siteName' class=\"loginTable\">" + siteName + "</span></td>" +
						"</tr>" +
						"<tr bgcolor=\"#000000\">" +
						"<td colspan='2' align='center' width=\"100%\" valign='bottom'><span class=\"loginTable\">" + gameDateTime + " " + "</span></td>" + 
						"</tr>" + 
						"<tr bgcolor=\"#000000\">" + 
						"<td align='right' width=\"50%\" valign='bottom'><span id='homeTeam' class=\"loginTable\">" + homeTeam + "</span></td>" + 
						"<td align='left' width=\"50%\" valign='bottom'><input type='text' size=\"3\" maxlength=\"3\" class=\"loginInput\"  id='homeTeamScore'></input></td>" + 
						"</tr>" +	
						"<tr bgcolor=\"#000000\">" + 
						"<td align='right' width='50%' valign='bottom'><span id='visitingTeam' class=\"loginTable\">" + visitingTeam + "</span></td>" + 
						"<td align='left' width='50%' valign='bottom'><input type='text' size=\"3\" maxlength=\"3\" class=\"loginInput\" id='visitingTeamScore'></input></td>" + 
						"</tr>" + 
						"<tr bgcolor=\"#000000\">" + 
						"<td align='center' bgcolor=\"#000000\">" + 
						"<button type='submit' style='color:0000FF; font-weight:bold; height:25px; width:70px' onClick='ModalPopups.Close(\"scoreEntryPopup\"); grayOut(false)' name='scoreCancel' id='scoreCancel'>Cancel</button></td>" + 
						"<td align='center' bgcolor=\"#000000\">" + 
						"<button type='submit' style='color:0000FF; font-weight:bold; height:25px; width:70px' " +
							"onClick='submitScore(\"" + ageGroup + "\", " +  
												  "\"" + siteName + "\", " + 
												  "\"" + gameDate + "\", " +
												  "\"" + gameTime + "\", " +
												  "\"" + homeTeam + "\", " +
												  "\"" + visitingTeam + "\"); " +
												  "return false;'" + 
												  "name='scoreSubmit' id=\"scoreSubmit\">Submit</button></td>" + 
						"</tr>" +
						"</tbody>" +
						"</table>",
						{  
							width: 400, 
						    popupBackColor: "#0000FF"
						}  
		);  
	document.getElementById("scoreEntryPopup_popupBody").style.zIndex = 60;
	ModalPopups.GetCustomControl("homeTeamScore").focus();   
}  							

function submitScore(ageGroup, siteName, date, time, homeTeam, visitingTeam) {
	var homeTeamScore = trim(document.getElementById('homeTeamScore').value);
	var visitingTeamScore = trim(document.getElementById('visitingTeamScore').value);
	error = validScoreEntry(homeTeamScore);
	
	if (error) {
		//ModalPopups.Cancel("scoreEntryPopup");
		grayOut(false);
		whcPopup(error, "Close", 300, 150, 400, 400);
		return;
	}
	error = validScoreEntry(visitingTeamScore);
	if (error) {
		//ModalPopups.Cancel("scoreEntryPopup");
		grayOut(false);
		whcPopup(error, "Close", 300, 150, 400, 400);
		return;
	}
	
	$.post("scoreUpdate.php", {'ageGroup' : ageGroup, 'siteName' : siteName, 'date': date, 
		   'time' : time, 'homeTeam' : homeTeam, 'visitingTeam' : visitingTeam, 
		   'homeTeamScore' : homeTeamScore, 'visitingTeamScore' : visitingTeamScore}, 
		   function(data) {
	              if (data == 'success') {
										  ModalPopups.Cancel("scoreEntryPopup");
						            	  history.go(0);
						              }
						              else 
						              {
						            	  ModalPopups.Cancel("scoreEntryPopup");
						            	  alert("An error occurred while updating your game score. Please try again. If the problem persists, please report the incident to the site administrator at website@whcybasketball.com.");
						            	  grayOut(false);
						              }
							   }
			);
	return false;
}


function notifyRescheduledGame(gameId, ageGroup, date, time, site, homeTeam, visitingTeam)  {
	var answer = confirm ("Email coaches about schedule change?")
	if (!answer) {
		return;
	}
	$.post("rescheduleNotification.php", {'gameId': gameId, 'ageGroup' : ageGroup, 'date' : date, 'time' : time, 'site' : site, 'homeTeam' : homeTeam, 'visitingTeam' : visitingTeam},
			function(data) {
		if (data == 'success') {
			alert("Parties notified.");
			history.go(0);
		}
		else {
			alert(data);
		}
	}
	);
	return false;
}
	

function grayOut(vis, options) {
	  // Pass true to gray out screen, false to ungray
	  // options are optional.  This is a JSON object with the following (optional) properties
	  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
	  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
	  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
	  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
	  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
	  // in any order.  Pass only the properties you need to set.
	  var options = options || {}; 
	  var zindex = options.zindex || 50;
	  var opacity = options.opacity || 70;
	  var opaque = (opacity / 100);
	  var bgcolor = options.bgcolor || '#000000';
	  var dark=document.getElementById('darkenScreenObject');
	  if (!dark) {
	    // The dark layer doesn't exist, it's never been created.  So we'll
	    // create it here and apply some basic styles.
	    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
	    var tbody = document.getElementsByTagName("body")[0];
	    var tnode = document.createElement('div');           // Create the layer.
	        tnode.style.position='absolute';                 // Position absolutely
	        tnode.style.top='0px';                           // In the top
	        tnode.style.left='0px';                          // Left corner of the page
	        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
	        tnode.style.display='none';                      // Start out Hidden
	        tnode.id='darkenScreenObject';                   // Name it so we can find it later
	    tbody.appendChild(tnode);                            // Add it to the web page
	    dark=document.getElementById('darkenScreenObject');  // Get the object.
	  }
	  if (vis) {
	    // Calculate the page width and height 
	    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
	        var pageWidth = document.body.scrollWidth+'px';
	        var pageHeight = document.body.scrollHeight+'px';
	    } else if( document.body.offsetWidth ) {
	      var pageWidth = document.body.offsetWidth+'px';
	      var pageHeight = document.body.offsetHeight+'px';
	    } else {
	       var pageWidth='100%';
	       var pageHeight='100%';
	    }   
	    //set the shader to cover the entire page and make it visible.
	    dark.style.opacity=opaque;                      
	    dark.style.MozOpacity=opaque;                   
	    dark.style.filter='alpha(opacity='+opacity+')'; 
	    dark.style.zIndex=zindex;        
	    dark.style.backgroundColor=bgcolor;  
	    dark.style.width= pageWidth;
	    dark.style.height= pageHeight;
	    dark.style.display='block';                          
	  } else {
	     dark.style.display='none';
	  }
	}

function validScoreEntry(x) {
	if (x == "") {
		return "";
	}
	if (!isNumeric(x)) {
		return "Score entries must be numbers. Please try again.";
	}
	return ("");
}


function hideLoginErrorMessage() {
	$("td.loginErrorMessage").hide();
}

function showLoginErrorMessage() {
	$("td.loginErrorMessage").show();
}

function isNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

function trim(s) {
	return s.replace(/^\s+|\s+$/g,"");
}


