var CONST_CHEKOUT = "pickupLocation";
var CONST_CHEKIN  = "dropoffLocation";
var CONST_CHOOSE = "Select a location / Enter first letters";

Waiting = function(){
	this.id = "waiting";
	this.container = gtObj(this.id);
	this.timer = new Array();
}

Waiting.prototype.start = function() {
	this.timer[this.timer.length] = setInterval("getWaiting().load()", 200);
	this.container.style.display = "block";
	gtObj("submit_button").disabled = true;
	gtObj("pickupLocation").disabled = true;
	gtObj("dropoffLocation").disabled = true;
	gtObj("pickupLocationRange").disabled = true;
	gtObj("dropoffLocationRange").disabled = true;
	gtObj("pickupLocationDetail").style.display = "none";
	gtObj("dropoffLocationDetail").style.display = "none";
}

Waiting.prototype.load = function() {
}

Waiting.prototype.startSubmit = function() {
	this.container.style.display = "block";
	this.timer = setInterval("getWaiting().load()", 200);
}

Waiting.prototype.stop = function(){
	for(var count=0;count<this.timer.length;count++)
		clearInterval(this.timer[count]);
	this.container.style.display = "none";
	gtObj("submit_button").disabled = false;
	gtObj("pickupLocation").disabled = false;
	gtObj("dropoffLocation").disabled = false;
	gtObj("pickupLocationRange").disabled = false;
	gtObj("dropoffLocationRange").disabled = false;
}

var WAITING_INSTANCE = null;
function getWaiting() {
	if(WAITING_INSTANCE==null)
		WAITING_INSTANCE = new Waiting();
	return WAITING_INSTANCE;
}

Stations = function(id){
	if (id == CONST_CHEKIN)
		this.url = "../locations_drop.php";
	else
		this.url = "";
	this.listener = null;
	this.id = id;
	this.stationCode = null;
}

Stations.prototype.receive = function(result){
	gtObj(this.id).parentNode.innerHTML = result;
	if(supportedNavigator())
		setStationSelect(gtObj(this.id), null);	
	getWaiting().stop();	
	if(this.stationCode!=null){
		setValue(this.id, this.stationCode);	
		setStation(this.stationCode,this.id);
	}
	
	gtObj(this.name).options[gtObj(this.name).selectedIndex].value="";
	
	if(currentWindow!="")
	{
		currentWindow.close();
		currentWindow = "";
	}
}

Stations.prototype.getValues = function(stationCode){

	this.stationCode = stationCode;	
	gtObj(this.id+"Range").value = CONST_CHOOSE;
	if(gtObj("pickupCountry").value=="")
		return;
	var parameter = "pkid="+gtObj("pickupCountry").value+"&country_name="+gtObj("refer_country").value+"&component="+this.id+"&loc_id="+gtObj("pickupLocation").value+"&lan="+gtObj("lan").value;
	getWaiting().start();
	sendData(this,HTTP_GET,this.url,parameter);	
}

Stations.prototype.setStation = function(obj){
	if (this.id == "pickupLocation") 
		disable_drop_off();
	if(obj.id.indexOf("dropoff")!=-1)
		return;
	myCheckInStations.getValues();
	gtObj(myCheckInStations.id+"Range").value = getSelectHtml(this.id);
	gtObj("diff_drop").disabled = false;
}

//display the link "see lcoation detail"
var myCheckOutStations = new Stations(CONST_CHEKOUT);
var myCheckInStations = new Stations(CONST_CHEKIN);

function disable_drop_off(){
	gtObj('dropoffDiv').style.display = 'none';
//	gtObj('empty_row').style.display = 'none';
	gtObj("diff_drop").checked = false;
	gtObj("diff_drop").disabled = true;	
}

function setStationSelect(component, text) {	
	var objIframe = null;	
	var options = component.options;	
	var iFrame = gtObj(component.id+"Detail");
	var textLength = (text==null)?0:text.length;

	iFrame = gtObj(component.id + "Detail");
	if(iFrame == undefined || iFrame == null)
		objIframe = document;
	else if (iFrame.contentDocument)
		objIframe = iFrame.contentDocument;
	else if (iFrame.contentWindow)
		objIframe = iFrame.contentWindow.document;
	else if (iFrame.document)
		objIframe = iFrame.document;

	var optionsDisplayed = 0;
	var firstOptionDisplayed = null;

	if(objIframe.getElementById("container") == undefined || objIframe.getElementById("container") == null){
		return;
	}
	var tbody = objIframe.getElementById("container").getElementsByTagName("tbody")[0];
	
	while(tbody.firstChild)	{
		tbody.removeChild(tbody.firstChild);
	}

	for(var count=0;count<options.length;count++){
		var isDisplay = true;
		var textToDisplay = options[count].text;
		var textNode = objIframe.createElement("nobr");
		if(text != null && text.replace(/^\s+|\s+$/g, '')!='') {
			isDisplay = (options[count].text.toUpperCase().indexOf(text.toUpperCase()) != -1);			
			if(isDisplay) {		
				textToDisplay = options[count].text.substring(indexString+textLength);				
				
				var indexString = options[count].text.toUpperCase().indexOf(text.toUpperCase())+1;
				textToDisplay = options[count].text.substring(0, indexString-1);		
				textNode.appendChild(objIframe.createTextNode(textToDisplay));
				
				var strongNode = objIframe.createElement("strong");
				var uNode = objIframe.createElement("u");
				textToDisplay = options[count].text.substring(indexString-1, indexString+textLength);
				uNode.appendChild(objIframe.createTextNode(textToDisplay));
				strongNode.appendChild(uNode);
				textNode.appendChild(strongNode);	
							
				textToDisplay= options[count].text.substring(indexString+textLength);	
				textNode.appendChild(objIframe.createTextNode(textToDisplay));			
			}
		}
		else{
			textNode.appendChild(objIframe.createTextNode(textToDisplay));
		}
		if(isDisplay && textToDisplay != CONST_CHOOSE) {
			var col = objIframe.createElement("TR"); 
			col.onmouseover = function() {
				this.className="stationOver";
			}
			col.onmouseout = function() {
				this.className="stationOut";
			}
			var cell = objIframe.createElement("TD"); 
			cell.onclick = function() {
				window.parent.setStation(options[this.firstChild.value].value, component.id);
			};
			

			var indexNODE = objIframe.createElement("input");
			indexNODE.type = "hidden";
			indexNODE.value = count;
			cell.appendChild(indexNODE);
			cell.appendChild(textNode);
			col.appendChild(cell);
			tbody.appendChild(col);
			optionsDisplayed++;
		}
	}
	var frameSet = (component.id.indexOf("pickup")!=-1)?window.frames.pickupLocationDetail:window.frames.dropoffLocationDetail;	

	frameSet.clearData();

	if(navigator.appName == "Netscape"){
		iFrame.style.height = 300;
		iFrame.scrolling = "yes";
	}
	else{

		if(optionsDisplayed == 0) {
			iFrame.style.display = "none";
			iFrame.scrolling = "no";
		}
		else if(optionsDisplayed<10 ) {
			iFrame.style.height = (optionsDisplayed * 15)+5;
			iFrame.scrolling = "no";
		}
		else {
			iFrame.style.height = (10 * 15) + 5;
			iFrame.scrolling = "yes";
		}
	}
}

function loadFrameCheckout(){	
	if(supportedNavigator()) 
		setStationSelect(gtObj("pickupLocation"),null);
	else
	{
		gtObj("pickupLocation").parentNode.style.display = "block";
		gtObj("dropoffLocation").parentNode.style.display = "block";
	}
	
	if(gtObj("pickupLocation").options.length>1)  {
		gtObj("pickupLocationRange").value = getSelectHtml("pickupLocation");
	}
}

function loadFrameCheckin(){		
	if(supportedNavigator()) 
		setStationSelect(gtObj("dropoffLocation"),null);
	else
	{
		gtObj("pickupLocationRange").style.display = "none";
		gtObj("dropoffLocationRange").style.display = "none";
	}
	if(gtObj("dropoffLocation").options.length>1)  {
		gtObj("dropoffLocationRange").value = getSelectHtml("dropoffLocation");
	}
}

function setValue (id, val){
	for(var i=0;i<gtObj(id).options.length;i++)
	{		
		if(gtObj(id).options[i].value == val)
		{
			gtObj(id).selectedIndex = i;			
			break;
		}
	}
}

function setStation(code, id) {
	setValue(id,code);
	gtObj(id+"Range").value = getSelectHtml(id);
	gtObj(id+"Range").title = getSelectHtml(id);
	gtObj(id+"Detail").style.display = "none";
	if(id.indexOf("pickup")!=-1)
		myCheckOutStations.setStation(gtObj(id));
	else
		myCheckInStations.setStation(gtObj(id));	
}

var isPassedByTextRange = false;
function setTextRange(obj,e) {

	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	
	
	var frameSet = (obj.id.indexOf("pickup")!=-1)?window.frames.pickupLocationDetail:window.frames.dropoffLocationDetail;	
	var id = obj.id.replace("Range", "");
	if(code==27) //arrow down
	{
		gtObj(id+"Detail").style.display = "none";
		return;
	}
	if(code==40) //arrow down
	{
		frameSet.setNextSibling();
		return;
	}	

	if(code==38) //arrow down
	{
		frameSet.setPreviousSibling();
		return;
	}	
	
	if(code==13) //enter
	{
		frameSet.selectCurrentNode();
		return;
	}		
	isPassedByTextRange = true;
	gtObj(id+"Detail").style.display = "block";
	setStationSelect(gtObj(id), obj.value);
}

function displayLocationDetail(obj) {
	
	obj.value = "";
	var id = obj.id.replace("Range", "Detail");
	if(document.getElementById("pickupCountry").selectedIndex==0){
//	if(document.getElementById("pickupCountry").value=='0'){
		gtObj(id).style.display = "none";
	}
	else{
		gtObj(id).style.display = "block";
		if(isPassedByTextRange) {
			isPassedByTextRange = false;
			setStationSelect(gtObj(id.replace("Detail","")), null);
		}
	}
}