// ==UserScript==
// @name            Fast TRA script, v1.0
// @namespace       http://dolphinwing.pixnet.net/blog
// @description     2009/06/11
// @include         http://railway.hinet.net/ct*
// @include         http://railway.hinet.net/cf*
// @include         http://railway.hinet.net/cc*
// @include         http://railway.hinet.net/coq*
// @include         http://210.71.*/cs*
// modified log:    
// 	v1.0: 2009-06-11, first release
// author: dolphin

var PERSON_ID="your personal id";//Personal ID
var FROM_STATION="106";//starting station code
var TO_STATION="175";//destination station code

var pageState = location.href.split('/')[3];//alert(pageState);
window.addEventListener('load', function()
{
	var person_id=null,from_station=null,to_station=null;
	var dtime_end=null,dtime_end2=null,train_type=null,train_type2=null;
	var i=0;
	var inputText=document.getElementsByTagName("input");
	for(i=0;i<inputText.length;i++){
		if(inputText[i].name=="person_id"||inputText[i].name=="personId"){
			person_id=inputText[i];break;
		}
	}
	var selectOpt=document.getElementsByTagName("select");
	for(i=0;i<selectOpt.length;i++){
		if(selectOpt[i].name=="from_station")from_station=selectOpt[i];
		if(selectOpt[i].name=="to_station")to_station=selectOpt[i];
		if(selectOpt[i].name=="getin_end_dtime")dtime_end=selectOpt[i];
		if(selectOpt[i].name=="getin_end_dtime2")dtime_end2=selectOpt[i];
		if(selectOpt[i].name=="train_type")train_type=selectOpt[i];
		if(selectOpt[i].name=="train_type2")train_type2=selectOpt[i];
	}

	var bt_style="width:100px;font-size:16px;margin: 0 10px 0 10px;font-family:Arial;";
	var pd_style="padding:10px;background:#ffc;border:solid 3px #fcc;width:250px;text-align:center;";
	var pd_style_p1="position:absolute;top:100px;left:400px;";
	var pd_style_p2="margin-top:-175px;margin-left:50px;";
	var pd_style_p3="margin-left:50px;";
				
    window.setTimeout(function()
    {
		if(person_id==null)return;
		var mydiv = document.createElement('div');
		mydiv.style.cssText=pd_style+pd_style_p1;
		if(pageState=="csearch.htm")mydiv.style.cssText=pd_style+pd_style_p2;
		else if(pageState=="ccancel.htm")mydiv.style.cssText=pd_style+pd_style_p3;
		document.body.appendChild(mydiv);

		var mybt = document.createElement('input');
		mybt.type="button";
		mybt.style.cssText=bt_style;
		mybt.value="Fast Home";
		mybt.addEventListener('click', function(){
				person_id.value=PERSON_ID;
				if(from_station!=null)from_station.value=FROM_STATION;
				if(to_station!=null)to_station.value=TO_STATION;
			}, false);
		mydiv.appendChild(mybt);
		
		mybt = document.createElement('input');
		mybt.type="button";
		mybt.style.cssText=bt_style;
		mybt.value="Fast Back";
		mybt.addEventListener('click', function(){
				person_id.value=PERSON_ID;
				if(from_station!=null)from_station.value=TO_STATION;
				if(to_station!=null)to_station.value=FROM_STATION;
			}, false);
		mydiv.appendChild(mybt);
		
		if(dtime_end!=null)dtime_end.value="23:59";
		if(dtime_end2!=null)dtime_end2.value="23:59";
		if(train_type!=null)train_type.value="*1";
		if(train_type2!=null)train_type2.value="*1";
		
    }, 500);
	
}, false);