
function guess(type,e)
{
	if (type=='Date')
	{
		guessDate(e);
	}

	if (type=='Time')
	{
		guessTime(e);
	}

	fillFields();
}

function time2seconds(duration)
{
	var durationMatch=/(\d+):(\d+)/;
	durationMatch.exec(duration);
	minutes=RegExp.$1*60+RegExp.$2;
	seconds=minutes*60;
	return seconds;
}

function de2mysql(deDate)
{
	var deDateMatch=/(\d+)\.(\d+)\.(\d+)/;
	deDateMatch.exec(deDate);
	//alert (RegExp.$3 + '-' + RegExp.$2 + '-' + RegExp.$1);
	return (RegExp.$3 + '-' + RegExp.$2 + '-' + RegExp.$1);
}


function guessTime(s)
{
	var toGuess=s;
	var now=new Date();
	now.setMinutes(0);
	now.setSeconds(0);
	now.setDate(1);
	now.setMonth(1);
	now.setYear(1970);
	
	//convert , to .
	toGuess=toGuess.replace(/,/g,'.');
	
	//if contains . convert to "minutes"
	if (toGuess.indexOf('.')>=0)
	{
		//convert 1.5 to 90
		if (toGuess==parseFloat(toGuess))
		{
			toGuess=Math.round(toGuess*60,0);
		}
	}
	//alert(now);
	
	//assume numbers 1-23 to mean hours, not minutes
	if (toGuess<24)
	{
		now.setHours(toGuess);
		//now.setMinutes(0); //done above
	} else if (toGuess==parseInt(toGuess,10)) 
		//if a plain number >=24
	{
		//alert(toGuess);
		//convert 45 to 0:45
		//convert 75 to 1:15
		now.setHours(0);
		now.setMinutes(toGuess);
		//alert(now);
	} else if (toGuess.substring(0,1)==':')
		//convert :45 to 0:45
	{
		//alert(toGuess.substring(1,2));
		now.setHours(0);
		now.setMinutes(toGuess.substring(1,3));
	} else {

		//assume, it's fine here. So split and reformat to make sure
		now.setHours(parseInt(toGuess,10));
		//if (isNaN(hours)) hours=0;
		//use the + to cast integer/number
		now.setMinutes(parseInt(+toGuess.substr(toGuess.lastIndexOf(':')+1),10));
		//if (isNaN(minutes)) minutes=0;
	}

	//alert(now);
	if ((isNaN(now.getHours())) || (isNaN(now.getMinutes())))
	{
	  alert('Die Eingabe "' + s + '" konnte nicht einwandfrei als Uhrzeit interpretiert werden');
	  return '00:00';
	} else {
	  return zero(now.getHours()) + ':' + zero(now.getMinutes());
	}
}

function guessDate(e)
{
	var toGuess=e.value;
	var now=new Date();
	now.setHours(0);
	now.setMinutes(0);
	now.setSeconds(0);

	if (parseInt(toGuess,10)==toGuess)
	{
		//assume simple numbers mean day of current month
		now.setDate(toGuess)
	} else {
		//1.3 or 31.12
		dot=toGuess.indexOf('.');
		if (dot>=0) {
			dot2=toGuess.lastIndexOf('.');
			//alert(toGuess.substring(0,dot));
			now.setDate(toGuess.substring(0,dot));
			//alert(toGuess.substring(dot+1));
			now.setMonth(parseInt(toGuess.substring(dot+1),10)-1);

			//check if date given
			if (dot!=dot2)
			{
				year=toGuess.substring(dot2+1);
				if (year<20)
				{
					year=parseInt(year,10)+2000;
				}
				now.setYear(year);
			}
		}
	}
	
	e.value=now.getDate() + '.' + ((now.getMonth())+1) + '.' + now.getFullYear();
}

  function zero(i)
  {
    return i >9 ? i : '0' + i;
  }
