var browser=navigator.appName;
if (browser=="Microsoft Internet Explorer") {
   document.onkeydown = MicrosoftEventHandler_KeyDown;
 } else {
   window.captureEvents(Event.KEYDOWN);
   window.onkeydown = NetscapeEventHandler_KeyDown;
}

function NetscapeEventHandler_KeyDown(e) {
  if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit' && e.target.type != 'button') { return false; }
  return true;
}

function MicrosoftEventHandler_KeyDown() {
  if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit' && event.srcElement.type != 'button')
    return false;
  return true;
}

function giveAcesTo(acesForm, acesToGive, giveToUsername, level)
{
	if ((level != 0 && acesForm.amount.value <= 0) || !isNumeric(acesForm.amount.value)) {
		alert("You have entered an invalid Aces amount.\nPlease try another amount.");
		return;
	}

	if (acesForm.amount.value <= acesToGive) {
		var answer = prompt("You are about to give " + acesForm.amount.value + " Aces to " + giveToUsername + "!\nDo you wish to continue?", "Enter reason here (max 50 chars)")
		if (answer != '' && answer != null) {
			acesForm.reason.value = answer;
			acesForm.submit();
		}
	} else {
		alert("You do not have enough Aces to give this amount.\nPlease try another amount.");
	}
}

function giveAcesToComment(acesForm, acesToGive, giveToUsername, commentID, amount)
{
	acesForm.amount.value = amount;
	if (acesForm.amount.value <= acesToGive) {
		acesForm.reason.value = "Cheers for your insight!";
		if (isAjaxEnabled()) {
			giveAcesAjax(acesForm, commentID);
		} else {
			acesForm.submit();
		}
	} else {
		alert("You do not have enough Aces to give this amount.\nPlease try another amount.");
	}
}

function giveAcesAjax(form, commentID) {

	// build up url to send to ajax
	var url = "";
	// url = "notes_ajaxTest.php";
	url = "notesAces_ajax.php";
	
	var param = "";
	
	/*
	// noteTournamentID
	param = param + "noteTournamentID=" + form.tournamentID.value;
	// notePlayer1
	param = param + "&notePlayer1=" + form.player_1ID.value;
	// notePlayer2
	param = param + "&notePlayer2=" + form.player_2ID.value;
	// filter
	param = param + "&filter=" + form.filter.value;
	// negfilter
	param = param + "&negfilter=" + form.negfilter.value;
	// applyNeg
	param = param + "&applyNeg=" + form.applyNeg.value;
	// matchID
	param = param + "&matchID=" + form.matchID.value;
	// maxCount
	param = param + "&maxCount=" + form.maxCount.value;
	*/
	// rate note
		
	// tip
	param = param + "rateCommentID=" + commentID;
	// receiver_userID
	param = param + "&rateUserID=" + form.receiver_userID.value;
	// reason
	param = param + "&rateReason=" + form.reason.value;
	// amount
	param = param + "&rateAmount=" + form.amount.value;
	// addNote
	param = param + "&rateNote=true";

	// alert(param);
	// alert($url);

	// ajaxPost(url, param, "notes");
	ajaxPost(url, param, "commentAces" + commentID);

}

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 swapLayers(whichLayer)
{
  var lyr = getElemRefs(whichLayer);
	// alert(lyr.style.display);
  if (lyr.style.display == "none") {
		lyr.style.display = "block";
	} else {
		lyr.style.display = "none";
	}
}

function getElemRefs(id) {
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? getLyrRef(id,document): null;
	if (el) el.css = (el.style)? el.style: el;
	return el;
}

// get reference to nested layer for ns4
// from old dhtmllib.js by Mike Hall of www.brainjar.com
function getLyrRef(lyr,doc) {
	if (document.layers) {
		var theLyr;
		for (var i=0; i<doc.layers.length; i++) {
	  	theLyr = doc.layers[i];
			if (theLyr.name == lyr) return theLyr;
			else if (theLyr.document.layers.length > 0) 
	    	if ((theLyr = getLyrRef(lyr,theLyr.document)) != null)
					return theLyr;
	  }
		return null;
  }
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}

function submitNote(form) {

	var tipGiven = false;
	
	if (form.tipSelected) {
		if (form.tipSelected.value != "") {
			
			var answer = confirm("You are about to give a Tip for this match.\nEach Member is allowed only one Tip for each Match and once submitted it can not be undone.\nDo you wish to continue?")
			if (!answer){
				return;
			}
			tipGiven = true;
		}
	}
	
	if (!tipGiven) {
		if (trim(form.text.value) == "") {
			alert("You have entered an empty comment.\nPlease put your comments in the box provided and try again.");
			return;
		}
	}

	if (tipGiven) {
		if (form.betOfTheDay.checked) {
			var answer = confirm("You have checked this tip as your Bet Of The Day.\nThis means that all your Stakes will be doubled.\nYou can only do this once per Calendar (GMT) Day.\nDo you wish to continue?");
			if (!answer){
				return;
			}
		}
	}
	
	if (isAjaxEnabled()) {
		submitNote_ajax(form, tipGiven);
		return;
	}

	form.submit();
}

function str_replace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

function submitNote2() {
	submitNote(submitNoteForm);
}

function submitNote_ajax(form, tipGiven) {
	
	
	// build up url to send to ajax
	var url = "";
	url = "notes_ajax.php";
	
	var param = "";
	// noteTournamentID
	param = param + "noteTournamentID=" + form.tournamentID.value;
	// notePlayer1
	param = param + "&notePlayer1=" + form.player_1ID.value;
	// notePlayer2
	param = param + "&notePlayer2=" + form.player_2ID.value;
	// filter
	param = param + "&filter=" + form.filter.value;
	// negfilter
	param = param + "&negfilter=" + form.negfilter.value;
	// applyNeg
	param = param + "&applyNeg=" + form.applyNeg.value;
	// matchID
	param = param + "&matchID=" + form.matchID.value;
	// maxCount
	param = param + "&maxCount=" + form.maxCount.value;
	
	// add note
	// 
	// text
	// param = param + "&text=" + escape(form.text.value);
	textStr = form.text.value;
	textStr = str_replace(textStr, "&", "%26");
	textStr = str_replace(textStr, "+", "%2B");
	textStr = str_replace(textStr, "?", "%3F");
	param = param + "&text=" + textStr;
	
	if (tipGiven) {
		// tip
		param = param + "&tip=" + form.tipSelected.value;
		// odds1
		param = param + "&odds1=" + form.odds1.value;
		// wage1
		param = param + "&wage1=" + form.wage1.options[form.wage1.selectedIndex].value;
		// bookmaker1
		param = param + "&bookmaker1=" + form.bookmaker1.options[form.bookmaker1.selectedIndex].value;
		// tipOdds
		param = param + "&tip_odds=" + form.tipOdds.value;
	}
	
	// bet of the day
	if (tipGiven) {
		if (form.betOfTheDay.checked) {
			param = param + "&betOfTheDay=" + form.betOfTheDay.value;
		}
	}

	
	// addNote
	param = param + "&addNote=true";

	// alert(param);
	// alert($url);
	
	// getElemRefs("notes").innerHTML = $url;
	
	// ajaxGet(url + "?" + param, "notes");

	ajaxPost(url, param, "notes");

}

function refreshNotes(param) {

	if (isAjaxEnabled()) {
		refreshNotes_ajax(param);
		return;
	}
	
	location.reload(true);
}

function refreshNotes_ajax(param) {
	
	// build up url to send to ajax
	var url = "";
	url = "notes_ajax.php";
	
	ajaxGet(url + "?" + param, "notes");
}

function changeClass(id,className)
{
	// alert("changeClass");
	getElemRefs(id).className = className;
}

function filter_activity()
{
	// alert("hi");
	
	// formFilter.action = 'match_preview_activity.php?match_id=<? echo $matchID ?>&player=<? echo $player ?>&activity=1';
	// formFilter.activity.option[formFilter.activity.selectedIndex].value;

	// alert(formFilter.action);

	document.formFilter.submit();
}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}

function open_popup(URL,h) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=" + h + ",left = 262,top = 134');");
}

function open_popup2(URL,w,h) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=" + w + ",height=" + h + ",left = 262,top = 134');");
}

function getMatchStat(form) {
	if (isAjaxEnabled()) {
		getMatchStat_ajax(form);
		return;
	}

	form.submit();
}

function getMatchStat_ajax(form) {
	
	// build up url to send to ajax
	var url = "";
	url = "match_stats_ajax.php";
	
	var param = "";
	// duration
	param = param + "duration=" + form.duration.value;
	// surface
	param = param + "&surface=" + form.surface.value;
	// draw
	param = param + "&draw=" + form.draw.value;
	// case
	param = param + "&case=" + form.caser.value;
	// tour
	param = param + "&tour=" + form.tour.value;

	param = param + "&player_id=" + form.player_id.value;
	param = param + "&player_id2=" + form.player_id2.value;
	param = param + "&playerName=" + form.playerName.value;
	param = param + "&playerName2=" + form.playerName2.value;
	param = param + "&tournamentID=" + form.tournamentID.value;
	param = param + "&tournamentName=" + form.tournamentName.value;
	
	// alert(form.surface.value);

	ajaxGet(url + "?" + param, "match_stats");

}

function deleteNote(form, info) {
	
	if (isAjaxEnabled()) {
		deleteNote_ajax(form, info);
		return;
	}
	
	var answer = confirm("Are you sure you wish to delete this note?\n\"" + info + "\"")
	if (answer){
		form.submit();
	}
}

function deleteNote_ajax(form, info) {
	var answer = confirm("Are you sure you wish to delete this note?\n\"" + info + "\"")
	if (answer){
		// build up url to send to ajax
		var url = "";
		url = "notes_ajax.php";
		
		var param = "";
		// noteTournamentID
		param = param + "noteTournamentID=" + form.tournamentID.value;
		// notePlayer1
		param = param + "&notePlayer1=" + form.player_1ID.value;
		// notePlayer2
		param = param + "&notePlayer2=" + form.player_2ID.value;
		// filter
		param = param + "&filter=" + form.filter.value;
		// negfilter
		param = param + "&negfilter=" + form.negfilter.value;
		// applyNeg
		param = param + "&applyNeg=" + form.applyNeg.value;
		// matchID
		param = param + "&matchID=" + form.matchID.value;
		// maxCount
		param = param + "&maxCount=" + form.maxCount.value;
		
		// del note
		// 
			
			
			
		// text
		param = param + "&deleteUserID=" + form.deleteUserID.value;
		// tip
		param = param + "&deleteCommentID=" + form.deleteCommentID.value;
		// addNote
		param = param + "&delNote=true";
	
		// alert(param);
		// alert($url);
		
		// getElemRefs("notes").innerHTML = $url;
		
		// ajaxGet(url + "?" + param, "notes");
	
		ajaxPost(url, param, "notes");
	}
}

function makePopup2(url) 
{
	popper = window.open(url,"matchstats","width=400,height=485,status=0,toolbar=0");
}

function makePopup(url) 
{
	popper = window.open(url,"matchstats","width=380,height=500,status=0,toolbar=0,scrollbars=1");
}

function showLayer(whichLayer)
{
  var lyr = getElemRefs(whichLayer);
	// alert(lyr.style.display);
		lyr.style.display = "block";
}
function hideLayer(whichLayer)
{
  var lyr = getElemRefs(whichLayer);
	// alert(lyr.style.display);
		lyr.style.display = "none";
}
function getElemRefs(id) {
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? getLyrRef(id,document): null;
	if (el) el.css = (el.style)? el.style: el;
	return el;
}
function updateScore(nameA, nameB, Sets_Best_of) {
	
	// make sure it is a valid score
	var valid = true;
	if (document.scoreForm.set1A.value == "") {
		if (document.scoreForm.set1B.value != "") {
			valid = false;
		}
	} else {
		if (document.scoreForm.set1B.value == "") {
			valid = false;
		}
	}
	if (document.scoreForm.set2A.value == "") {
		if (document.scoreForm.set2B.value != "") {
			valid = false;
		}
	} else {
		if (document.scoreForm.set2B.value == "") {
			valid = false;
		}
	}
	if (document.scoreForm.set3A.value == "") {
		if (document.scoreForm.set3B.value != "") {
			valid = false;
		}
	} else {
		if (document.scoreForm.set3B.value == "") {
			valid = false;
		}
	}
	if (Sets_Best_of == 5) {
		if (document.scoreForm.set4A.value == "") {
			if (document.scoreForm.set4B.value != "") {
				valid = false;
			}
		} else {
			if (document.scoreForm.set4B.value == "") {
				valid = false;
			}
		}
		if (document.scoreForm.set5A.value == "") {
			if (document.scoreForm.set5B.value != "") {
				valid = false;
			}
		} else {
			if (document.scoreForm.set5B.value == "") {
				valid = false;
			}
		}
	}
	if (!valid) {
		alert("You have eneterd an invalid score. Please correct and try again.");
		return;
	}
	
	var setA = 0;
	var setB = 0;
	if (document.scoreForm.retiredA.options[document.scoreForm.retiredA.selectedIndex].value != "") {
		setA = 0;
		setB = 1;
	} else if (document.scoreForm.retiredB.options[document.scoreForm.retiredB.selectedIndex].value != "") {
		setA = 1;
		setB = 0;
	} else {
		if (document.scoreForm.set1A.value > document.scoreForm.set1B.value) {
			setA++;
		} else if (document.scoreForm.set1B.value > document.scoreForm.set1A.value) {
			setB++;
		}
		if (document.scoreForm.set2A.value > document.scoreForm.set2B.value) {
			setA++;
		} else if (document.scoreForm.set2B.value > document.scoreForm.set2A.value) {
			setB++;
		}
		if (document.scoreForm.set3A.value > document.scoreForm.set3B.value) {
			setA++;
		} else if (document.scoreForm.set3B.value > document.scoreForm.set3A.value) {
			setB++;
		}
		if (Sets_Best_of == 5) {
			if (document.scoreForm.set4A.value > document.scoreForm.set4B.value) {
				setA++;
			} else if (document.scoreForm.set4B.value > document.scoreForm.set4A.value) {
				setB++;
			}
			if (document.scoreForm.set5A.value > document.scoreForm.set5B.value) {
				setA++;
			} else if (document.scoreForm.set5B.value > document.scoreForm.set5A.value) {
				setB++;
			}
		}
	}
	var score = "";
	var names = "";
	if (setA > setB) {
		names = nameA + " d. " + nameB;
		if (document.scoreForm.set1A.value != "") {
			score = score + " " + document.scoreForm.set1A.value + "-" + document.scoreForm.set1B.value
		}
		if (document.scoreForm.set2A.value != "") {
			score = score + " " + document.scoreForm.set2A.value + "-" + document.scoreForm.set2B.value
		}
		if (document.scoreForm.set3A.value != "") {
			score = score + " " + document.scoreForm.set3A.value + "-" + document.scoreForm.set3B.value
		}
		if (Sets_Best_of == 5) {
			if (document.scoreForm.set4A.value != "") {
				score = score + " " + document.scoreForm.set4A.value + "-" + document.scoreForm.set4B.value
			}
			if (document.scoreForm.set5A.value != "") {
				score = score + " " + document.scoreForm.set5A.value + "-" + document.scoreForm.set5B.value
			}
		}
		if (document.scoreForm.retiredA.options[document.scoreForm.retiredA.selectedIndex].value) {
			score = score + " " + document.scoreForm.retiredA.value;
		}
		if (document.scoreForm.retiredB.options[document.scoreForm.retiredB.selectedIndex].value) {
			score = score + " " + document.scoreForm.retiredB.value;
		}
	} else {
		names = nameB + " d. " + nameA;
		if (document.scoreForm.set1A.value != "") {
			score = score + " " + document.scoreForm.set1B.value + "-" + document.scoreForm.set1A.value
		}
		if (document.scoreForm.set2A.value != "") {
			score = score + " " + document.scoreForm.set2B.value + "-" + document.scoreForm.set2A.value
		}
		if (document.scoreForm.set3A.value != "") {
			score = score + " " + document.scoreForm.set3B.value + "-" + document.scoreForm.set3A.value
		}
		if (Sets_Best_of == 5) {
			if (document.scoreForm.set4A.value != "") {
				score = score + " " + document.scoreForm.set4B.value + "-" + document.scoreForm.set4A.value
			}
			if (document.scoreForm.set5A.value != "") {
				score = score + " " + document.scoreForm.set5B.value + "-" + document.scoreForm.set5A.value
			}
		}
		if (document.scoreForm.retiredA.options[document.scoreForm.retiredA.selectedIndex].value) {
			score = score + " " + document.scoreForm.retiredA.value;
		}
		if (document.scoreForm.retiredB.options[document.scoreForm.retiredB.selectedIndex].value) {
			score = score + " " + document.scoreForm.retiredB.value;
		}	
	}
	if (score == "") {
		alert("You have eneterd an invalid score. Please correct and try again.");
		return;		
	}
	var warningMessage = "You have entered the following result:\n";
	warningMessage = warningMessage + names + score + "\n";;
	warningMessage = warningMessage + "Are you sure this is the correct score for this match?\n";;
	var answer = confirm(warningMessage);
	if (answer){
		document.scoreForm.submit();
	}


}

function updateMatchStats() {

	var valid = true;
	
	if (document.matchStatsForm.No_Aces.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.Opp_No_Aces.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.No_DFs.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.Opp_No_DFs.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.No_1st_Serves_W.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.Opp_No_1st_Serves_W.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.No_1st_Serves_P.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.Opp_No_1st_Serves_P.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.No_2nd_Serves_W.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.Opp_No_2nd_Serves_W.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.No_2nd_Serves_P.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.Opp_No_2nd_Serves_P.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.BP_Saved.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.Opp_BP_Saved.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.BP_Faced.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.Opp_BP_Faced.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.No_Gms_Served.value == "") {
		valid = false;
	}
	if (document.matchStatsForm.Opp_No_Gms_Served.value == "") {
		valid = false;
	}

	if (!valid) {
		alert("Match stats are incomplete or invalid. Please correct and try again.");
		return;
	}

	document.matchStatsForm.submit();
}

function ajaxGet(url, outputID) {

  center("loading", 100, 100);

var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
	if(xmlHttp.readyState==4)
      {
			hideLayer("loading");
      		getElemRefs(outputID).innerHTML = xmlHttp.responseText;
      }
    }

  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
  
}

function ajaxPost(url, params, outputID) {

  center("loading", 100, 100);

var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
		
		if(xmlHttp.readyState==4)
      {
		    hideLayer("loading");
			if (outputID) {
      			getElemRefs(outputID).innerHTML = xmlHttp.responseText;
			}
      }
    }

  xmlHttp.open("POST",url,true);
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  // xmlHttp.setRequestHeader("Content-length", params.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(params);
  
}

function center(element, elementWidth, elementHeight){
    try{
        element = getElemRefs(element);
    }catch(e){
        return;
    }

    var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' ){
        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    }else if ( document.documentElement &&
             ( document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ){
        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body &&
            ( document.body.clientWidth || document.body.clientHeight ) ){
        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }
	
	my_height = my_height - 100;

    element.style.position = 'absolute';
    element.style.zIndex   = 99;

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }
	
    // var setX = ( my_width  - elementDimensions.width  ) / 2;
	var setX = ( my_width  - elementWidth  ) / 2;
    // var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;
	var setY = ( my_height - elementHeight ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

    element.style.display  = 'block';
}

function isAjaxEnabled() {

var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      // alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

  return true;
  
}

function chooseSub(name, amount, subID) {
	getElemRefs("item_name").value = name;
	getElemRefs("amount").value = amount;
	getElemRefs("subID").value = subID;
}

var submitNoteFormName = "";
var submitNoteForm = "";

function setSubmitNoteFormName(name) {
	submitNoteFormName = name;
	submitNoteForm = getElemRefs(name);
	// alert(submitNoteForm);
}

function tipSelection(selection, odds) {
	submitNoteForm.tipSelected.value = selection.value;
	var oddsText = "";
	if (odds < 0) {
		oddsText = '';
	} else if (odds > 0) {
		oddsText = '@ ' + odds + ' (used in earnings)';
	} else {
		oddsText = '(opening odds used in earnings)';
	}
	getElemRefs("currentOdds").innerHTML = oddsText;
	submitNoteForm.tipOdds.value = odds;
}

function challenge(form, userA, userB, userATip, userBTip, acesRemain) {

/*
	if (isAjaxEnabled()) {
		challenge_ajax(form, info);
		return;
	}
*/
	var infoA = "Enter the amount " + userA  + " receives if " + userATip + " wins";
	var infoB = "Enter the amount you receive if " + userBTip + " wins";

	var b = prompt("You are about to make an Ace challenge\n"+infoB, "Type the amount you will receive here (max 50)");
	b = parseInt(b);
	if (!isNaN(b)) {
   		if (b <= 0) {
      		alert("Invalid amount");
      	} else if (b > 50) {
      		alert("The amount is too big. Maxiumum challenge is 50 Aces");
      	} else {
			
			if (acesRemain < b) {
      			alert("You do not have enough Aces remaining to make this challenge");
				return;
			}

			var a = prompt("You are about to make an Ace challenge\n"+infoA, "Type the amount you are offering here (max 50)");
			a = parseInt(a);
			if (!isNaN(a)) {
				if (a <= 0) {
					alert("Invalid amount");
				} else if (a > 50) {
					alert("The amount is too big. Maxiumum challenge is 50 Aces");
				} else {
					form.amountB.value = b;
					form.amountA.value = a;
					form.submit();
				}
			}
      	}
   	}

}

function challenge_ajax(form, info) {
	var answer = confirm("Are you sure you wish to delete this note?\n\"" + info + "\"")
	if (answer){
		// build up url to send to ajax
		var url = "";
		url = "notes_ajax.php";
		
		var param = "";
		// noteTournamentID
		param = param + "noteTournamentID=" + form.tournamentID.value;
		// notePlayer1
		param = param + "&notePlayer1=" + form.player_1ID.value;
		// notePlayer2
		param = param + "&notePlayer2=" + form.player_2ID.value;
		// filter
		param = param + "&filter=" + form.filter.value;
		// negfilter
		param = param + "&negfilter=" + form.negfilter.value;
		// applyNeg
		param = param + "&applyNeg=" + form.applyNeg.value;
		// matchID
		param = param + "&matchID=" + form.matchID.value;
		// maxCount
		param = param + "&maxCount=" + form.maxCount.value;
		
		// del note
		// 
			
			
			
		// text
		param = param + "&deleteUserID=" + form.deleteUserID.value;
		// tip
		param = param + "&deleteCommentID=" + form.deleteCommentID.value;
		// addNote
		param = param + "&delNote=true";
	
		// alert(param);
		// alert($url);
		
		// getElemRefs("notes").innerHTML = $url;
		
		// ajaxGet(url + "?" + param, "notes");
	
		ajaxPost(url, param, "notes");
	}
}

function challengeDecision(form,userA,userB,userATip,userBTip,acesRemain) {

	var challengeDetails = userB + " on " + userBTip + " " + form.amountB.value + " Aces | " + userA + " on " + userATip + " " + form.amountA.value + " Aces";

	var decision = "accept";
	var decisionValue = "1";
	
	if (acesRemain < form.amountA.value) {
		var answer = confirm("You do not have enough Aces remaining to Accept this challenge (" + challengeDetails + ")\nDo you wish to decline the challenge?");
		if (answer){
			decision = "decline";
			decisionValue = "0";
		} else {
			return;
		}
	}
	
	var accepted = confirm("Do you wish to Accept the following challenge (" + challengeDetails + ")?");
	if (!accepted) {
		var answer = confirm("Do you wish to Decline the following challenge (" + challengeDetails + ")?");
		if (answer){
			decision = "decline";
			decisionValue = "0";
		} else {
			return;
		}
	} 

	form.challengeDecision.value = decisionValue;
	form.submit();

}

function updateView(matchID, userID, status, comment) {

	if (comment == "") {
		comment = "Enter comment here (max 50 chars)";
	}
	
	if (status == 2) {
		var answer = prompt("You are about to commit to providing a preview for this match. Enter a comment and press OK to continue", comment);
	} else if (status == 4) {
		var answer = prompt("You are indicating that you have completed a preview of this match. Update the comment and press OK to continue", comment);
	} else if (status == 6) {
		var answer = prompt("You are about to commit to providing a report for this match. Enter a comment and press OK to continue", comment);
	} else if (status == 8) {
		var answer = prompt("You are indicating that you have completed a preview of this match. Update the comment and press OK to continue", comment);
	}
	if (answer != '' && answer != null){
		// build up url to send to ajax
		var url = "";
		url = "view_ajax.php";
		
		var param = "";
		// matchID
		param = param + "matchID=" + matchID;
		// user
		param = param + "&userID=" + userID;
		// status
		param = param + "&status=" + status;
		// comment
		param = param + "&comment=" + answer;
	
		// alert(param);
		// alert(url);
		
		if (status <= 4) {
			refreshDiv = "preview" + matchID;
		} else {
			refreshDiv = "report" + matchID;
		}
		
		ajaxPost(url, param, refreshDiv);
	}

}