var rightNow=new Date();
var rf=false;
var dtime=ddate=rtime=rdate="";

function parseJourney(sterm) {
	if (sterm) {
		sterm=sterm.toLowerCase();
		var points=sterm.split(" ");
		if (points[0]=="from") points=points.slice(1);
		if (points.length<2 || (points.length==2 && points[1]=="to"))
			alert("No destination!");
		else {
			var url="http://www.traintimes.org.uk/";
			if (points[0].charAt(0)=="\"")
				points=quoted(points,0);
			url+=points[0]+"/";
			points=points.slice(1);
			if (points[0]=="to")
				points=points.slice(1);
			if (points[0].charAt(0)=="\"")
				points=quoted(points,0);
			url+=points[0]+"/";
			points=points.slice(1);
			var datetime=parseDateTime(points);
			dtime=datetime[0];
			ddate=datetime[1];
			if (points.length) {
				datetime=parseDateTime(points);
				rtime=datetime[0];
				rdate=datetime[1];
			}
			if (dtime=="" && ddate!="")
				dtime=padZero(rightNow.getHours())+":"+padZero(rightNow.getMinutes());
			if (rtime=="" && rdate!="")
				rtime=dtime;
			if (rtime!="" && dtime=="")
				dtime=padZero(rightNow.getHours())+":"+padZero(rightNow.getMinutes());
			if (rtime!="" && ddate=="")
				ddate="today";
			if (rf && rtime=="") {
				rtime=dtime;
				if (rtime=="")
					rtime=dtime=padZero(rightNow.getHours())+":"+padZero(rightNow.getMinutes());
				if (ddate=="")
					ddate="today";
			}
			if (dtime) url+=dtime+"/";
			if (ddate) url+=ddate+"/";
			if (rtime) url+=rtime+"/";
			if (rdate) url+=rdate+"/";
			document.location=url;
		}
	}
}
	
function padZero(num) {
	if (num<10) return "0"+num;
	else return num.toString();
}

function quoted(points,start) {
	points[start]=points[start].substr(1);
	var k=start+1;
	while (start<points.length-1) {
		points[start]+=" "+points[k];
		if (points[k].charAt(points[k].length-1)=="\"") {
			points[start]=points[start].substr(0,points[start].length-1);
			start=points.length;
		}
		points.splice(k,1);
	}
	return points;
}

function parseDate(pieces) {
	var theDate="";
	if (!pieces.length)
		return "";
	if (pieces[0]=="next" || pieces[0]=="next-") {
		pieces.splice(0,1);
		if (!pieces.length)
			return "";
		theDate="next-"+pieces[0];
		pieces.splice(0,1);
		return theDate;
	}
	if (pieces[0].substr(0,5)=="next-") {
		theDate="next-"+pieces[0].substr(5);
		pieces.splice(0,1);
		return theDate;
	}
	if (pieces[0].substr(0,4)=="next") {
		theDate="next-"+pieces[0].substr(4);
		pieces.splice(0,1);
		return theDate;
	}
	if (pieces[0].match(/^[0-9]{1,2}[/-][0-9]{1,2}([-/]([0-9]{2}|[0-9]{4}))?$/) ) {
		if (!pieces[0].match(/^([0-9]{1,2}[-/]){2}([0-9]{2}|[0-9]{4})$/)) {
			pieces[0]+="/"+rightNow.getFullYear();
		}
		var dp=pieces[0].split(/[/-]/);
		if (dp[2].length==2) dp[2]="20"+dp[2];
		chckDate=new Date();
		chckDate.setDate(dp[0]);
		chckDate.setMonth(dp[1]-1);
		chckDate.setFullYear(dp[2]);
		if (chckDate<rightNow)
			dp[2]=rightNow.getFullYear()+1;
		theDate=dp[2]+"-"+padSZero(dp[1])+"-"+padSZero(dp[0]);
		pieces.splice(0,1);
		return theDate;
	}
	if (pieces[0].match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)) {
		theDate=pieces[0];
		pieces.splice(0,1);
		return theDate;
	}
	if (checkMonth(pieces[0])) {
		theDate=pieces[1];
		pieces[1]=pieces[0];
		pieces[0]=theDate;
		return "";
	}
	theDate=pieces[0];
	pieces.splice(0,1);
	return theDate;
}

function padSZero(num){
	if (num.length<2)
		return "0"+num;
	else
		return num;
}

function parseFuture(pieces, theTime) {
	var theTime = (theTime == null) ? new Date() : theTime;
	if (isNaN(parseInt(pieces[0])) || pieces.length<2)
		return null;
	switch (pieces[1].toLowerCase()) {
	case "minutes":
	case "minute":
	case "mins":
	case "min":
	case "m":
		theTime.setMinutes(theTime.getMinutes()+parseInt(pieces[0]));
		pieces.splice(0,2);
		break;
	case "hours":
	case "hour":
	case "hrs":
	case "hr":
	case "h":
		theTime.setHours(theTime.getHours()+parseInt(pieces[0]));
		pieces.splice(0,2);
		break;
	case "days":
	case "day":
	case "ds":
	case "d":
		theTime.setHours(theTime.getHours()+parseInt(pieces[0])*24);
		pieces.splice(0,2);
		break;
	case "weeks":
	case "week":
	case "wks":
	case "wk":
	case "ws":
	case "w":
		theTime.setHours(theTime.getHours()+parseInt(pieces[0])*24*7);
		pieces.splice(0,2);
		break;
	case "months":
	case "month":
	case "mnths":
	case "mnth":
	case "mns":
	case "mn":
		theTime.setMonth(theTime.getMonth()+parseInt(pieces[0]));
		pieces.splice(0,2);
		break;
	case "years":
	case "year":
	case "yrs":
	case "yr":
	case "ys":
	case "y":
		theTime.setYear(theTime.getYear()+parseInt(pieces[0]));
		pieces.splice(0,2);
		break;
	default:
		return null;
		break;
	}
	
	if (pieces.length){
		var theNextTime=new Date();
		if (theNextTime=parseFuture(pieces,theTime))
			return theNextTime;
		else
			return theTime;
	} else 
		return theTime;
}
function parseTime(pieces){
	if (!pieces.length)
		return null;
	if (!pieces[0].match(/^[0-9]{1,2}(:[0-9]{2})?$/))
		return null;
	var timebits=pieces[0].split(":");
	if (timebits.length==1)
		timebits.push("00");
	if (pieces[1]=="pm" && parseInt(timebits[0])<12) {
		timebits[0]=parseInt(timebits[0])+12;
		timebits[0]=timebits[0].toString();
	}
	if (pieces[1]=="am" && parseInt(timebits[0])==12)
		timebits[0]="00";
	if (pieces[1]=="am" || pieces[1]=="pm")
		pieces.splice(1,1);
	pieces.splice(0,1);
	return padSZero(timebits[0])+":"+padSZero(timebits[1]);
}

function parseDateTime(points){
	var otimedate=["",""];
	while (points.length && (otimedate[0]=="" || otimedate[1]=="")) {
		if (points[0]=="return" || points[0]=="returning" || points[0]=="ret") {
			break;
		}
		if (points[0]=="in") {
			points.splice(0,1);
			if (!points.length)
				break;
			var future=parseFuture(points);
			if (future) {
				otimedate[0]=padZero(future.getHours())+":"+padZero(future.getMinutes()) ;
				otimedate[1]=future.getFullYear()+"-"+padZero(future.getMonth()+1)+"-"+padZero(future.getDate());
				continue;
			} else {
				alert("In what now!?");
				return null;
			}
		}
		if (isNaN(parseInt(points[0].charAt(0))) && points[0]!="at" && points[0]!="@" && points[0]!="now") {
			if (points[0]=="on") {
				points.splice(0,1);
				continue;
			}
			otimedate[1]=parseDate(points);
			continue;
		}
		if (points[0]=="at" || points[0]=="@")
			points.splice(0,1);
		if (!points.length)
			break;
		if (points[0]=="now") {
			otimedate[0]=padZero(rightNow.getHours())+":"+padZero(rightNow.getMinutes());
			points.splice(0,1);
			continue;
		}
		if (points[0].match(/(am|pm)$/)) {
			points.splice(1,0,points[0].substr(-2));
			points[0]=points[0].substr(0,points[0].length-2);
			continue;
		}
		var aMonth=checkMonth(points[1]);
		if (aMonth) {
			points[0]+="/"+padZero(aMonth);
			points.splice(1,1);
			if (points[1] && points[1].match(/^[0-9]{2}|[0-9]{4}$/) && (!points[2] || !points[2].match(/^(a|p)m$/))) {
				points[0]+="/"+points[1];
				points.splice(1,1);
			}
			continue;
		}
		var aTime=parseTime(points);
		if (aTime) {
			otimedate[0]=aTime;
			continue;
		}
		otimedate[1]=parseDate(points);
	}
	if (points[0]=="return" || points[0]=="returning" || points[0]=="ret") {
		points.splice(0,1);
		rf=true;
	}
	return otimedate;
}

function checkMonth(chk){
	if (!chk)
		return null;
	var theMnths=new Array("january","february","march","april","may","june","july","august", "september","october","november","december");
	var theShrtMnths=new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov", "dec");
	for (var i=0;i<12;i++)
		if (theMnths[i]==chk || theShrtMnths[i]==chk)
			return i+1;
	return null;
}
