// JavaScript Document
function init() {
  imgOriginSrc = null;
  var imgarr = document.getElementsByTagName( 'img' );
  for ( var i=0; i<imgarr.length; i++ )
	{
    if (imgarr[i].getAttribute( 'hsrc' ) )
		{
			tmpImg = new Image();
			tmpImg.src = imgarr[i].hsrc;
			imgarr[i].onmouseover = function()
			{
				imgOriginSrc = this.getAttribute( 'src' );
				this.setAttribute( 'src', this.getAttribute( 'hsrc' ) )
			}
			
			imgarr[i].onmouseout = function()
			{
				if ( imgOriginSrc != null )
				{
					this.setAttribute( 'src', imgOriginSrc )
				}
      }
    }
  }
	show();
}

function windowGame()
{
	var game = window.open( "", "windowGame", "width=300, height=300, top=200, left=200, scrollbars=yes" );
	game.focus();
}

function windowMember()
{
	var member = window.open( "", "windowMember", "width=500, height=220, top=200, left=200, scrollbars=no" );
	member.focus();
}

function windowTable()
{
	var url;
	var val = document.forms["teamSelection"].txtLiga.value;
	if( isFinite( val ) )
	{
		url = "spo_tables_1l.php?group="+val;
	}
	else
	{
		url = 'http://www.gsgladmin.ch/asp/tabelle_anzeige.asp';
	}
	document.forms["teamSelection"].action = url;
	var table = window.open( url, "windowTable", "width=635, height=305, top=200, left=200, scrollbars=yes" );
	table.focus();
}

function windowAdmin()
{
	var admin = window.open( "admin/login.php", "windowAdmin", "width=800, height=700, top=200, left=200, scrollbars=yes" );
	admin.focus();
}

function windowForm( formIdent )
{
	var form = window.open( "forms/form.php?form="+formIdent, "formAdmin", "width=550, height=400, top=200, left=200, scrollbars=yes" );
	form.focus();
}

function getDateAt( offset, target )
{
	var now = new Date();
	var tmpDate = new Date();
	tmpDate.setTime( now.getTime() + getDateAt.arguments[0] * 24 * 60 * 60 * 1000 );
	var tmpMonth = tmpDate.getMonth();
	var month = new Array( "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" );
	var today = ( tmpDate.getDate() < 10 ? "0" : "" ) + tmpDate.getDate() +  "." + month[tmpMonth] + "." + tmpDate.getFullYear();
	var targetField = document.getElementsByName( getDateAt.arguments[1] );
	targetField[0].value = today;
}

function setFocus( name, element )
{
	var elements = document.getElementsByName( setFocus.arguments[0] );
	elements[setFocus.arguments[1]].checked = true;
}

function addTeam( formName )
{
	var newTeams = document.getElementsByName( "memberTeams[]" )[0];
	var actForm = document.getElementsByName( addTeam.arguments[0] )[0];
	var numElements = newTeams.length;
	var teamIndex = actForm.teamID.selectedIndex;
	var newText = actForm.teamID.options[teamIndex].text + " (" + actForm.teamPos.value + ", #" + actForm.teamNum.value + ")";
	var newValue = actForm.teamID.options[teamIndex].value + "|" + actForm.teamPos.value + "|" + actForm.teamNum.value;
	var newEntry = new Option( newText, newValue );
	newTeams.options[numElements] = newEntry;
	newTeams.size++;
}

function remTeam()
{
	var newTeams = document.getElementsByName( "memberTeams[]" )[0];
	var numElements = newTeams.length;
	var remTeamsIndex = newTeams.selectedIndex;
	newTeams.options[remTeamsIndex] = null;
	newTeams.size--;
	if( remTeamsIndex > 0 )
	{
		newTeams.options[remTeamsIndex-1].selected = true;
	}
}

function addFunction( formName )
{
	var newFunctions = document.getElementsByName( "memberFunctions[]" )[0];
	var actForm = document.getElementsByName( addFunction.arguments[0] )[0];
	var numElements = newFunctions.length;
	var functionIndex = actForm.functionID.selectedIndex;
	var newText = actForm.functionID.options[functionIndex].text;
	var newValue = actForm.functionID.options[functionIndex].value;
	// 3 => Schiri, 4 => Schiri (suspendiert)
	if( newValue == 3 || newValue == 4 )
	{
		var refGradeIndex = actForm.functionRefereeGrade.selectedIndex;
		newText += " (" + actForm.functionRefereeGrade.options[refGradeIndex].text + ")";
		newValue += "|" + actForm.functionRefereeGrade.options[refGradeIndex].value;
	}
	// 5 => Trainer(in), 6 => Teamverantwortliche(r)
	if( newValue == 5 || newValue == 6 )
	{
		var teamIndex = actForm.functionTeamID.selectedIndex;
		newText += " (" + actForm.functionTeamID.options[teamIndex].text + ")";
		newValue += "|" + actForm.functionTeamID.options[teamIndex].value;
	}
	var newEntry = new Option( newText, newValue );
	newFunctions.options[numElements] = newEntry;
	newFunctions.size++;
}

function remFunction()
{
	var newFunctions = document.getElementsByName( "memberFunctions[]" )[0];
	var numElements = newFunctions.length;
	var remFunctionsIndex = newFunctions.selectedIndex;
	newFunctions.options[remFunctionsIndex] = null;
	newFunctions.size--;
	if( remFunctionsIndex > 0 )
	{
		newFunctions.options[remFunctionsIndex-1].selected = true;
	}
}

function checkActive( formName )
{
	var actForm = document.getElementsByName( checkActive.arguments[0] )[0];
	var functionIndex = actForm.functionID.selectedIndex;
	var newValue = actForm.functionID.options[functionIndex].value;
	actForm.functionRefereeGrade.disabled = true;
	actForm.functionTeamID.disabled = true;
	// 3 => Schiri, 4 => Schiri (suspendiert)
	if( newValue == 3 || newValue == 4 )
	{
		actForm.functionRefereeGrade.disabled = false;
		actForm.functionTeamID.disabled = true;
	}
	// 5 => Trainer(in), 6 => Teamverantwortliche(r)
	if( newValue == 5 || newValue == 6 )
	{
		actForm.functionRefereeGrade.disabled = true;
		actForm.functionTeamID.disabled = false;
	}
}

function selectAll()
{
	var newFunctions = document.getElementsByName( "memberFunctions[]" )[0];
	var numElementsFunctions = newFunctions.length;
	var newTeams = document.getElementsByName( "memberTeams[]" )[0];
	var numElementsTeams = newTeams.length;
	for( var i=0; i<numElementsFunctions; i++ )
	{
		newFunctions.options[i].selected = true;
	}
	for( var j=0; j<numElementsTeams; j++ )
	{
		newTeams.options[j].selected = true;
	}
}

function show( id )
{
	var d = document.getElementById( id );
	for( var i = 1; i<=10; i++ )
	{
		if( document.getElementById( 'submenu_'+i ) )
		{
			document.getElementById( 'submenu_'+i ).style.display = 'none';
		}
	}
	if( d )
	{
		d.style.display = 'block';
	}
}

function confirmDel( subject )
{
	if( !confirm( confirmDel.arguments[0] + " wirklich löschen?" ) )
	{
		return false;
	}
  return true;
}

function validateForm( formIdent, args )
{
	var valid = true;
	for( var i = 1; i<arguments.length; i++ )
	{
		elem = arguments[i];
		if( document.forms[formIdent].elements[elem] == null )
		{
			alert( 'Fehler im Formular!' );
			return false;
		}
		field = document.forms[formIdent].elements[elem];
		msgElem = document.getElementById( 'msg_' + elem );
		if( ( field.length > 0 && field[0].type == 'checkbox' ) || field.type == 'checkbox' )
		{
			var chk = false;
			for( var j=0; j<field.length; j++ )
			{
					if( field[j].checked )
					{
						chk = true;
					}
			}
			if( field.type == 'checkbox' )
			{
				chk = field.checked;
			}
			if( chk )
			{
				msgElem.style.display = "none";
			}
			else
			{
				msgElem.style.display = "block";
				valid = false;
			}
		}
		else if( field.value == '' )
		{
			msgElem.style.display = "block";
			valid = false;
		}
		else
		{
			msgElem.style.display = "none";
		}
	}
	return valid;
}