//Author: Mario Walker / Version 1.1 / Date: 19.01.2009 / Filetype: Javascript

//validates the date format
function checkForValidDate(sDate) {
	// regular expression for validating date. Format: <day 2digits>.<month 2digits>.<year 4digits>
	var regex = /^[0-9][0-9]?\.[0-9][0-9]?\.[0-9][0-9][0-9][0-9]$/i;
	return regex.test(sDate);
}

//many helper functions that validate if the supplied date does exist
//do not worry too much about the logic, I have taken this stuff somewhere from the internet
function alphaOf( s)
{
	var i = s.search( /[^A-Za-zÄÖÜäöüß]/); /* /[^\w]/ */
	if (i < 0)
	{
		return (s);
	}
	else
	{
		return (s.substring( 0, i));
	}
}

function digitsOf( s)
{
	var i = s.search( /[^0-9]/);
	if (i < 0)
	{
		return (s);
	}
	else
	{
		return (s.substring (0, i));
	}
}

function digitsStripLd0( s)
{
	var i = s.search( /[1-9]|[0]$/);
	if (i < 0)
	{
		return ("");
	}
	else
	{
		return (digitsOf( s.substring( i, s.length)));
	}
}

function trimString( s)
{
	var i = 0;
	var l = s.length;
	while
	(
		i < l
		&&
		s.charAt( i) == ' '
		||
		s.charAt( i) == '\n'
		||
		s.charAt( i) == '\r'
		||
		s.charAt( i) == '\t'
		||
		s.charAt( i) == '\f'
	)
	{
		i++;
	}
	if (i >= l)
	{
		return ("");
	}
	do
	{
		l--;
	}
	while
	(
		s.charAt( l) == ' '
		||
		s.charAt( l) == '\n'
		||
		s.charAt( l) == '\r'
		||
		s.charAt( l) == '\t'
		||
		s.charAt( l) == '\f'
	);
	return (s.substring( i, l + 1));
}

function todayMillis()
{
	var d = new Date();
	d.setHours( 0);
	d.setMinutes( 0);
	d.setSeconds( 0);
	d.setMilliseconds( 0);
	return (d.getTime());
}

function parseMonth( s)
{
	var m = -1;
	var ms = alphaOf( trimString( s)).toLowerCase();
	if (ms.length > 0)
	{
		if (ms.indexOf( "ja") == 0) { m = 0; }
		else if (ms.indexOf( "f") == 0) { m = 1;}
		else if (ms.indexOf( "m") == 0 && (ms.indexOf( "ä") > 0 || ms.indexOf( "r") > 0)) { m = 2; }
		else if (ms.indexOf( "a") == 0 && (ms.indexOf( "p") > 0 || ms.indexOf( "r") > 0)) { m = 3; }
		else if (ms.indexOf( "mai") == 0 || ms.indexOf( "may") == 0) { m = 4; }
		else if (ms.indexOf( "ju") == 0 && ms.indexOf( "n") > 0) { m = 5; }
		else if (ms.indexOf( "ju") == 0 && ms.indexOf( "l") > 0) { m = 6; }
		else if (ms.indexOf( "au") == 0) { m = 7; }
		else if (ms.indexOf( "s") == 0) { m = 8; }
		else if (ms.indexOf( "o") == 0) { m = 9; }
		else if (ms.indexOf( "n") == 0) { m = 10; }
		else if (ms.indexOf( "d") == 0) { m = 11; }
	}
	else
	{
		ms = digitsOf( trimString( s));
		if (ms.length > 0)
			m = parseInt( digitsStripLd0( ms));
	}
	return (m);
}

function dateStringToMillis( s, txto)
{
	var val;
	var dmy = new Array(3);
	var i;
	var j;
	var dmys = s.split( /[^0-9]+/);
	var dmysl = dmys.length;
	if (dmysl < 3)
	{
		if (dmysl == 2)
		{
			var month = -1;
			var matches = s.match( /[\s][^0-9]+[\s]/);
			if (matches != null)
			{
				month = parseMonth( matches[0]);
			}
			if (month >= 0)
			{
				dmys = new Array( dmys[0], "" + (month + 1), dmys[1]);
				dmysl = 3;
			}
			else
			{
				return (Number.NaN);
			}
		}
		else
		{
			return (Number.NaN);
		}
	}
	j = 0;
	for (i = 0; i < dmysl; i++)
	{
		if (dmys[i].length > 0)
		{
			dmy[j++] = parseInt( digitsStripLd0( dmys[i]));
			if (j == 3)
				break;
		}
	}
	if (dmy[2] < 25)
			dmy[2] += 2000;
		else if (dmy[2] < 100)
		dmy[2] += 1900;
	if
	(
		dmy[1] >= 1 && dmy[1] <= 12 && dmy[0] >= 1
		&&
		dmy[0] <=
		(
			(dmy[1] == 2)
			?
			(((dmy[2] %4 == 0) && ((dmy[2] % 100 != 0) || (dmy[2] % 400 == 0))) ? 29 : 28)
			:
			(30 + (((dmy[1] < 8) ? dmy[1] : (dmy[1] + 1)) % 2))
		)
	)
	{
		val = (new Date( dmy[2], dmy[1] - 1, dmy[0])).getTime();
		if (txto != null)
			txto.value = ((dmy[0] < 10) ? ("0" + dmy[0]) : dmy[0]) + ((dmy[1] < 10) ? ".0" : ".") + dmy[1] + "." + dmy[2];
	}
	else
	{
		val = Number.NaN;
	}
	return (val);
}


//validates the resultat
function checkForValidResultat(sResultat) {
	// regular expression for validating resultat. Format: <digits>:<digits> (<digits>:<digits>)
	var regex = /^[0-9][0-9]*:[0-9][0-9]*( [(][0-9][0-9]*:[0-9][0-9]*[)])?$/i;
	return regex.test(sResultat);
}

//validates the minute and name
function checkForValidMinuteName(sMinuteName) {
	// regular expression for validating minute plus name. Format: <max 3 digits>\.<name name-name ...>(, repeat like beginning)
	var regex = /^[0-9]+\. [A-Z a-zÄÖÜäöüßêñéçìÈùøíàè\-]+(, ([0-9]+\. [A-Z a-zÄÖÜäöüßêñéçìÈùøíàè\-]+))*$/i;
	return regex.test(sMinuteName);
}

//validates the name
function checkForValidName(sName) {
	// regular expression for validating name. Must provide at least 2 names, separated by space
	var regex = /^[A-Za-zÄÖÜäöüßêñéçìÈùøíàè\-]+\s[A-Za-zÄÖÜäöüßêñéçìÈùøíàè\-]+$/i;
	return regex.test(sName);
}

//validates that all fields correspond to the appropriate syntax. Semantics often cannot be checked.
function validateInput() {
	var form_datum=document.getElementById("datum").value;
	var form_geg_mannschaft=document.getElementById("geg_mannschaft").value;
	var form_titel=document.getElementById("titel").value;
	var form_resultat=document.getElementById("resultat").value;
	var form_txt=document.getElementById("txt").value;
	var form_tor=document.getElementById("tor").value;
	var form_auswechslung=document.getElementById("auswechslung").value;
	var form_verwarnung=document.getElementById("verwarnung").value;
	var form_platzverweis=document.getElementById("platzverweis").value;
	var form_nameverfasser=document.getElementById("nameverfasser").value;

	allok=true;
	
	if (form_datum=="" || !checkForValidDate(form_datum)) { allok=false; window.alert("Datum: entspricht nicht dem geforderten Format."); return false; }
	// We parse the date and check if it is valid
  var millis = dateStringToMillis(form_datum);
  if (isNaN(millis)) {
  	// not a valid date. We must inform the user!
  	allok=false; window.alert("Datum: dieses Datum existiert nicht."); return false;
  }
	
	if (form_geg_mannschaft=="") { allok=false; window.alert("Bitte geben Sie eine gegnerische Mannschaft an."); return false; }
	if (form_titel=="") { allok=false; window.alert("Bitte geben Sie einen Titel an."); return false; }
	if (form_resultat=="" || !checkForValidResultat(form_resultat)) { allok=false; window.alert("Resultat: entspricht nicht dem geforderten Format."); return false; }
	if (form_txt=="") { allok=false; window.alert("Bitte geben Sie einen Spieltext an."); return false; }
	if (form_tor!="" && !checkForValidMinuteName(form_tor)) { allok=false; window.alert("Tore: entspricht nicht dem geforderten Format."); return false; }
	if (form_auswechslung!="" && !checkForValidMinuteName(form_auswechslung)) { allok=false; window.alert("Auswechslung: entspricht nicht dem geforderten Format."); return false; }
	if (form_verwarnung!="" && !checkForValidMinuteName(form_verwarnung)) { allok=false; window.alert("Verwarnung: entspricht nicht dem geforderten Format."); return false; }
	if (form_platzverweis!="" && !checkForValidMinuteName(form_platzverweis)) { allok=false; window.alert("Platzverweis: entspricht nicht dem geforderten Format."); return false; }
	if (form_nameverfasser=="" && !checkForValidName(form_nameverfasser)) { allok=false; window.alert("Bitte geben Sie Vornamen und Nachnamen des Verfassers an."); return false; }

	if (allok) 
    return true;
}

//asks the user if he really want's to delete the Spielbericht
function askForBerichtDeletion() {
  return window.confirm("Wollen Sie diesen Spielbericht wirklich loeschen? Es werden auch alle dazugehörigen Bilder entfernt!");
}

//asks the user if he really want's to delete all pictures associated to this Spielbericht
function askForBildDeletion() {
  return window.confirm("Wollen Sie alle dazugehörigen Bilder wirklich löschen? Diese müssen dann neu hochgeladen werden!");
}

