// javascript common functions


//  sub-window code for presenting example documents - version date Dec 2003
//  Some example links: <a href="yo.html" onclick="return openSubWin(this.href)">link</a> 
//  <a href="some.html" onclick="return openSubWin(this.href, 'win2', null, null, 800, 600, 'resizable')">link</a>
function openSubWin(url, nm, x, y, w, h, atts) 
{
  nm = nm || "subwindow";
  //atts = atts || "menubar=no,status=no,titlebar=no,toolbar=no,location=no,directories=no,copyhistory=no";
  //atts = atts || "resizable,scrollbars";
  atts += "menubar=no,status=no,titlebar=no,toolbar=no,location=no,fullscreen=no,directories=no,copyhistory=no,dialog=yes,close=no,border=no";

  w = w || 500; h = h || 500;
  x = (typeof x=="number")? x: window.opera? 100: Math.round( (screen.availWidth - w)/2 );
  y = (typeof y=="number")? y: window.opera? 20: Math.round( (screen.availHeight - h)/2 );
  atts += ',width='+w+',height='+h+',left='+x+',top='+y;
  var win = window.open(url, nm, atts);
  if (win)
  {
    if (!win.closed) { win.resizeTo(w,h); win.moveTo(x,y); win.focus(); return false; }
  } 
  return true;
}

function MM_reloadPage(init) 
{  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

MM_reloadPage(true);

function Cancel(page)
{
    if (confirm('Are you sure you want to cancel?'))
    {
      document.location.href=page;
      return true;
    }
    return false;
}

function SubmitAction(sFormName)
{
	document.forms[sFormName].submit();
}

function SubmitActionWithInput(sFormName, name, value)
{
	document.getElementById(name).value = value;
	document.forms[sFormName].submit();
}

function SubmitLogin(sFormName)
{
    // get the username and password
    sUserName = document.forms[sFormName].un.value;
    sPassword = document.forms[sFormName].pwd.value;
    
    // hash the password cause its hashed in the database
    sPassword = hex_md5(sPassword);
    
    // calculate the md5 hash form the username and password
    sHash = hex_md5(sUserName + sPassword);

    // stick the hash value back in to the password field
    document.forms[sFormName].pwd.value = sHash;
    
    //alert(sUserName + ' ' + document.forms[sFormName].pwd.value);
    
    // submit the form
	document.forms[sFormName].submit();
}

function ShowHide(sFieldName)
{
    if (document.getElementById(sFieldName).className == "Hide")
        document.getElementById(sFieldName).className = "Show";
    else
        document.getElementById(sFieldName).className = "Hide";
}

function SetLoginFocus()
{
    document.forms['login'].un.focus();
}

function ShowWarning(name)
{
	if (confirm("You are about to Delete " + name + ", Are you sure?")) 
		return true;
	return false;
}

var hiddenInput = 3;
function AddPhotoInput()
{
	if (document.all)
	{
	obj = eval("document.all.fileinput" + hiddenInput);
	}
	else
	{
	obj = document.getElementById('fileinput' + hiddenInput);
	}
	obj.style.display = (document.all)?"block":"block";

	if (document.all)
	{
	objt = eval("document.all.fileinputtext" + hiddenInput);
	}
	else
	{
	objt = document.getElementById('fileinputtext' + hiddenInput);
	}
	objt.style.display = (document.all)?"block":"block";

	hiddenInput++;
}

// DEPRECATED
function ShowReport(game, value) 
{
	if (document.all)
	{
	obj = eval("document.all.reportrow" + game);
	}
	else
	{
	obj = document.getElementById("reportrow" + game);
	}

	if (value == '0')
	{
	if (!obj.hasChildNodes())
		return;
		
	if (document.all)
	{
		nod = eval("document.all.reportcol" + game);
	}else{
		nod = document.getElementById("reportcol" + game);
	}
	obj.removeChild(nod);

	if (document.all)
	{
		mrow = eval("document.all.matchrow" + game);
	}
	else
	{
		mrow = document.getElementById("matchrow" + game);
	}
	//mrow.style.backgroundColor = \"#FFFFFF\";

	return;
	}

	if (obj.hasChildNodes()) 
	return;

	var nod = document.createElement("td");
	nod.setAttribute("className", "text");
	nod.setAttribute("class", "text");
	//nod.style.setAttribute(\"cssText\", \"border:1px solid #000000;\");
	//nod.setAttribute(\"style\", \"border:1px solid #000000;\");
	nod.style.cssText = "border:1px solid #000000;";
	nod.setAttribute("colSpan", "9");
	nod.setAttribute("id", "reportcol" + game);
	obj.appendChild(nod);

	if (document.all)
	{
		mrow = eval("document.all.matchrow" + game);
	}
	else
	{
		mrow = document.getElementById("matchrow" + game);
	}
	//mrow.style.backgroundColor = \"#FEF6D1\";

	if (value == "")
	{
		nod.innerHTML = "<b>no match report</b>";
	}
	else
	{
		nod.innerHTML = value;
	}
	//alert(nod.getAttribute('style'));
}

function AddPlayer() 
{
	list = document.forms['upd_teamplayers'].all_players;
	var s = document.forms['upd_teamplayers'].elements['team_players'];
	var cap = document.forms['upd_teamplayers'].elements['team_captain'];
	var coa = document.forms['upd_teamplayers'].elements['team_coach'];
	var man = document.forms['upd_teamplayers'].elements['team_manager'];

	if (list.selectedIndex < 0)
		return;

	var selValue = list.options[list.selectedIndex].value;
	var selText = list.options[list.selectedIndex].text;

	//add new player to team_players list and to select boxes
	var opt = new Option(selText, selValue);
	s.options[s.options.length] = opt;
	var opt = new Option(selText, selValue);
	cap.options[cap.options.length] = opt;
	var opt = new Option(selText, selValue);
	coa.options[coa.options.length] = opt;
	var opt = new Option(selText, selValue);
	man.options[man.options.length] = opt;

	//remove player from all_players list
	list.remove(list.selectedIndex);

	//add new hidden input node
	var newnode = document.createElement('input');
	newnode.setAttribute('type', 'hidden');
	newnode.setAttribute('name', 'team_player'+selValue);
	newnode.setAttribute('id', selValue);
	newnode.setAttribute('value', selValue);

	if (document.getElementById)
	{
		document.getElementById('players').appendChild(newnode);
	}
	else if (document.all)
	{
		document.all.players.appendChild(newnode);
	}

	// update list counters
	if (document.getElementById)
	{
		document.getElementById('total_members').innerText = parseInt(document.getElementById('total_members').innerText) - 1;
		document.getElementById('total_players').innerText = parseInt(document.getElementById('total_players').innerText) + 1;
	}
}
	
function RemovePlayer() 
{
	list = document.forms['upd_teamplayers'].team_players;
	var cap = document.forms['upd_teamplayers'].team_captain;
	var coa = document.forms['upd_teamplayers'].team_coach;
	var man = document.forms['upd_teamplayers'].team_manager;

	if (list.selectedIndex < 0)
		return;

	var selValue = list.options[list.selectedIndex].value;
	var selText = list.options[list.selectedIndex].text;

	//remove player from team_players list and from select boxes
	list.remove(list.selectedIndex);
	var i = 0;
	for(i = 0; i < cap.length;i++)
	{
		if (cap.options[i].value==selValue)
		{
			cap.remove(i); break;
		}
	}
	for(i = 0; i < coa.length;i++)
	{
		if (coa.options[i].value==selValue)
		{
			coa.remove(i); break;
		}
	}
	for(i = 0; i < man.length;i++)
	{
		if (man.options[i].value==selValue)
		{
			man.remove(i); break;
		}
	}

	//add player back to all_players list
	var s = document.forms['upd_teamplayers'].elements['all_players'];
	var opt = new Option(selText, selValue);
	s.options[s.options.length] = opt;

	//remove input node
	if (document.getElementById)
	{
		var inputnode = document.getElementById(selValue);
		document.getElementById('players').removeChild(inputnode);
	}
	else if (document.all)
	{
		var inputnode = document.all.selValue;
		document.all.players.removeChild(inputnode);
	}

	// update list counters
	if (document.getElementById)
	{
		document.getElementById('total_members').innerText = parseInt(document.getElementById('total_members').innerText) + 1;
		document.getElementById('total_players').innerText = parseInt(document.getElementById('total_players').innerText) - 1;
	}
}

function KeyPressEvent(evt, sInputFieldName)
{
	var evt = (evt) ? evt : (window.event) ? event : null;

	if (evt)
	{
		var charCode = (evt.charCode) ? evt.charCode :
						((evt.keyCode) ? evt.keyCode :
						((evt.which) ? evt.which : 0));

		//var sText = evt.srcElement.innerHTML;
		var sText = '';

		if (charCode == '13')
		{
			evt.returnValue = false;

			sText = document.getElementById('HTML_' + sInputFieldName).innerHTML;
			sText = sText + '<BR/>';
			//sendSpace(sInputFieldName);
		}
		else
		{
			evt.returnValue = true;

			sText = document.getElementById('HTML_' + sInputFieldName).innerHTML;
			sText = sText.replace('<P>', '');
			sText = sText.replace('</P>', '');
		}
		document.getElementById('HTML_' + sInputFieldName).innerHTML = sText;
		document.getElementById(sInputFieldName).value = sText;
	}
}

function SendSpace(sInputFieldName)
{
	var evtObj = document.createEventObject();
	evtObj.keyCode = '32';
	document.getElementById('HTML_' + sInputFieldName).fireEvent("onkeydown", evtObj);
	event.cancelBubble = true;
}

function SendShiftEnter()
{
	var evtObj = document.createEventObject();
	evtObj.keyCode = '13';
	evtObj.shiftKey = true;
	document.all.HTML_team_notice.fireEvent("onkeydown", evtObj);
	event.cancelBubble = true;
}

function HTMLEdit_DoFormat(sCommand, sInputFieldName)
{
	var edit = document.getElementById('HTML_' + sInputFieldName).document.selection.createRange();
	if (edit != null && edit.text != null && edit.text.length > 0)
	{
		edit.execCommand(sCommand, false);
		document.getElementById(sInputFieldName).value = document.getElementById('HTML_' + sInputFieldName).innerHTML;
		//alert(document.getElementById(sInputFieldName).value);
		edit.select();
		document.getElementById('HTML_' + sInputFieldName).focus();
	}
}

function SubmitRTEForm(sRTEElementName)
{
	if (sRTEElementName != null)
		updateRTE(sRTEElementName);
	else
		updateRTEs();
	return true;
}



//preload images
/*
imgCancel = new Image();
imgCancel.src = 'images/buttons/s_cancel_d.gif';
imgCancelOn = new Image();
imgCancelOn.src = 'images/buttons/s_cancel_u.gif';

imgRegister = new Image();
imgRegister.src = 'images/buttons/s_register_d.gif';
imgRegisterOn = new Image();
imgRegisterOn.src = 'images/buttons/s_register_u.gif';

imgLogin = new Image();
imgLogin.src = 'images/buttons/login.gif';
imgLoginOn = new Image();
imgLoginOn.src = 'images/buttons/login.gif';

onmouseup=\"this.src=imgCancelOn.src\" onmouseout=\"this.src=imgCancelOn.src\" onmousedown=\"this.src=imgCancel.src\"
*/
