/**
 *  author:		Timothy Groves - http://www.brandspankingnew.net
 *	version:	1.2 - 2006-11-17
 *              1.3 - 2006-12-04
 *              2.0 - 2007-02-07
 *              2.1.1 - 2007-04-13
 *              2.1.2 - 2007-07-07
 *              2.1.3 - 2007-07-19
 *
 */


if (typeof(bsn) == "undefined")
	_b = bsn = {};


if (typeof(_b.Autosuggest) == "undefined")
	_b.Autosuggest = {};
else
	alert("Autosuggest is already set!");












_b.AutoSuggest = function (id, param)
{
	// no DOM - give up!
	//
	if (!document.getElementById)
		return 0;
	
	
	
	
	// get field via DOM
	//
	this.fld = _b.DOM.gE(id);

	if (!this.fld)
		return 0;
	
	
	
	
	// init variables
	//
	this.sInp 	= "";
	this.nInpC 	= 0;
	this.aSug 	= [];
	this.iHigh 	= 0;
	this.aSugSt = [];
	this.req_xml = null;
	this.req_results = null;
	this.req_indexes = null;
	
	
	// parameters object
	//
	this.oP = param ? param : {};
	
	// defaults	
	//
	var k, def = {minchars:2, meth:"get", varname:"input", className:"autosuggest", timeout:2500, delay:500, offsety:-5, shownoresults: false, noresults: "No results!", maxheight: 250, cache: false, maxentries: 10};
	for (k in def)
	{
		if (typeof(this.oP[k]) != typeof(def[k]))
			this.oP[k] = def[k];
	}
	
	
	// set keyup handler for field
	// and prevent autocomplete from client
	//
	var p = this;
	
	// NOTE: not using addEventListener because UpArrow fired twice in Safari
	//_b.DOM.addEvent( this.fld, 'keyup', function(ev){ return pointer.onKeyPress(ev); } );
	
	this.fld.onkeypress 	= function(ev){ return p.onKeyPress(ev); };
	this.fld.onkeyup 		= function(ev){ return p.onKeyUp(ev); };
	
	this.fld.setAttribute("autocomplete","off");
};





_b.AutoSuggest.prototype.onKeyPress = function(ev)
{

	var key = (window.event) ? window.event.keyCode : ev.keyCode;



	// set responses to keydown events in the field
	// this allows the user to use the arrow keys to scroll through the results
	// ESCAPE clears the list
	// TAB sets the current highlighted value
	//
	var RETURN = 13;
	var TAB = 9;
	var ESC = 27;
	
	var bubble = 1;

	switch(key)
	{
		case RETURN:
			this.setHighlightedValue();
			this.setSelectedId();
			bubble = 0;
			break;

		case TAB:
			this.setHighlightedValue();
			this.setSelectedId();
			bubble = 0;
			break;

		case ESC:
			this.clearSuggestions();
			break;
	}

	return bubble;
};



_b.AutoSuggest.prototype.onKeyUp = function(ev)
{
	var key = (window.event) ? window.event.keyCode : ev.keyCode;
	


	// set responses to keydown events in the field
	// this allows the user to use the arrow keys to scroll through the results
	// ESCAPE clears the list
	// TAB sets the current highlighted value
	//

	var ARRUP = 38;
	var ARRDN = 40;
	var TAB = 9;
	var bubble = 1;

	switch(key)
	{


		case ARRUP:
			this.changeHighlight(key);
			bubble = 0;
			break;


		case ARRDN:
			this.changeHighlight(key);
			bubble = 0;
			break;
		
		case TAB:
		    this.changeHighlight(key);
			bubble = 0;
			break;
		
		
		default:
			this.getSuggestions(this.fld.value);
	}

	return bubble;
	

};








_b.AutoSuggest.prototype.getSuggestions = function (val)
{

	
	// if input stays the same, do nothing
	//
	if (val == this.sInp)
		return 0;
	
	
	// kill list
	//
	_b.DOM.remE(this.idAs);
	
	
	this.sInp = val;
	
	
	// input length is less than the min required to trigger a request
	// do nothing
	//
	if (val.length < this.oP.minchars)
	{
		this.aSug = [];
		this.nInpC = val.length;
		return 0;
	}
	
	
	var ol = this.nInpC; // old length
	this.nInpC = val.length ? val.length : 0;
	
	// if caching enabled, and user is typing (ie. length of input is increasing)
	// filter results out of aSuggestions from last request
	//
	var l = this.aSug.length;

	if (this.nInpC > ol && l && l<this.oP.maxentries && this.oP.cache) {
	
		//alert("cache");
		var arr = [];
		for (var i=0;i<l;i++)
		{
		    if (this.aSug[i].value.substr(0,val.length).toLowerCase() == val.toLowerCase()) {
			
			// add region
			arr.push( this.aSug[i] );


			//add all stations of region
			var stations = this.aSug[i].stations;

			for (var k=0; k < stations.length;k++ ) {

			    arr.push( stations[k] );

			}
			
		    }
		}
	this.aSug = arr;
		
		this.createList(this.aSug);
		
		
		
	return false;

} else {
    // do new request
    //

	    //alert("new request");
		var pointer = this;
		var input = this.sInp;
		clearTimeout(this.ajID);
		this.ajID = setTimeout( function() { pointer.doAjaxRequest(input) }, this.oP.delay );
	}

	return false;
};





_b.AutoSuggest.prototype.doAjaxRequest = function (input)
{
	// check that saved input is still the value of the field
	//
	if (input != this.fld.value)
		return false;
		
	
	
	var pointer = this;
	
	
	// create ajax request
	//
	if (typeof(this.oP.script) == "function")
		var url = this.oP.script(encodeURIComponent(this.sInp));
	else
		var url = this.oP.script+this.oP.varname+"="+encodeURIComponent(this.sInp)+"&locale="+ac_locale;
	
	if (!url)
		return false;
	
	var meth = this.oP.meth;
	var input = this.sInp;
	
	var onSuccessFunc = function (req) { pointer.setSuggestions(req, input) };
	var onErrorFunc = function (status) { alert("AJAX error: "+status); };

	if ( !this.req_xml) { 	
	
	    var myAjax = new _b.Ajax();
	    myAjax.makeRequest( url, meth, onSuccessFunc, onErrorFunc );

	} else {
		
	    pointer.setSuggestions(null, input);

	}
};


_b.AutoSuggest.prototype.setSelectedId = function(){
	
    //alert(" country_id = " + this.aSug[this.iHigh - 1].id + " state_id = " + this.aSug[this.iHigh - 1].states_id);
    //alert("country_id"+country_id);
    //alert("state_id"+state_id);
    //alert("region_id"+region_id);
	
    document.getElementById("ac_country_id").value = this.aSug[this.iHigh - 1].id
    document.getElementById("ac_state_id").value = this.aSug[this.iHigh - 1].states_id;
    document.getElementById("ac_region_id").value = this.aSug[this.iHigh - 1].region_id;
    document.getElementById("cr_from_station_id").value = this.aSug[this.iHigh - 1].station_id;
    document.getElementById("cr_from_foreigen_id").value = this.aSug[this.iHigh - 1].foreigen_id;

	cr_autosuggest_init_to("",this.aSug[this.iHigh - 1].states_id);
}




_b.AutoSuggest.prototype.initSatesuggest = function (geographic_id){

    if (this.oP.backref != null) {
	
	// seting hidden vars to -1
	recursive_reset(this.oP.backref);

	this.oP.backref.script = this.oP.backref.script_base + geographic_id + '&';
	
	this.oP.backref.sugObj = new bsn.AutoSuggest(this.oP.backref.varname, this.oP.backref);

    } else {

	/* If this.oP.backref is null this we are called by a region suggestion no new suggestions have to be created and no hidden vars have to be reset*/
	
	
    }

    document.getElementById(this.oP.varname_to_update).value = geographic_id; 

	
}


_b.AutoSuggest.prototype.setSuggestions = function (req, input)
{
	// if field input no longer matches what was passed to the request
	// don't show the suggestions
	//k
	if (input != this.fld.value)
		return false;
	
	
	this.aSug = [];
	
	
	if (this.oP.json)
	{
		var jsondata = eval('(' + req.responseText + ')');
		
		for (var i=0;i<jsondata.results.length;i++)
		{
			this.aSug.push(  { 'id':jsondata.results[i].id, 'value':jsondata.results[i].value, 'info':jsondata.results[i].info }  );
		}
	}else{

		if ( !this.req_xml) { 

		    this.req_xml = req.responseXML;
		    this.req_results = this.req_xml.getElementsByTagName('rs');
		    this.req_indexes = this.req_xml.getElementsByTagName('index');

		}
		    this.req_results = this.req_xml.getElementsByTagName('rs');
		    this.req_indexes = this.req_xml.getElementsByTagName('indexes')[0].childNodes;
			//this.req_indexes = this.req_xml.getElementsByTagName('index');

		//var xml = req.responseXML;
		var xml = this.req_xml;
		var results = this.req_results;
		var indexes = this.req_indexes;
		
		// traverse xml
		//
		
		var maxResults = this.oP.maxentries;
		var start_index;
		var end_index;
		
		for (var i=0;i<indexes.length;i++){
			
			if(indexes[i].hasChildNodes()){
			
				var	val = indexes[i].childNodes[0].nodeValue;
				var st=  this.sInp;
				var pattern = new RegExp("^"+val , "i");
				
				if(st.match(pattern)){
					
					start_index = parseInt(indexes[i].getAttribute('start'));
					end_index = parseInt(indexes[i].getAttribute('end'));
					
				}
			}
		}
		
		
		var jj = 0;
		//for (var i=0;i<results.length;i++)
		var st=  this.sInp;
		var pattern = new RegExp("^"+st , "i");

		for (var i=(start_index - 1);i<end_index;i++)
		{
			

				if (results[i].hasChildNodes()) {
					

					
					 // author js@navelar.de
					 // since 2008/11/06
					 // check if exact match and only push this results
				    
				    var txtToCompare = '';

				    if (results[i].getAttribute('region_id') == "-1") {				    

					 txtToCompare = results[i].childNodes[0].nodeValue;

				    } else {


					 txtToCompare = results[i].getAttribute('region_name');

				    }

				    //alert("to = " + txtToCompare);
				    if (txtToCompare.match(pattern)) {
						
					
					if(jj < 30) {
							
					    if (results[i].getAttribute('region_id') == "-1") {				    

						this.aSug.push({
						    'id': results[i].getAttribute('id'),
							'value': results[i].childNodes[0].nodeValue,
							'info': results[i].getAttribute('info'),
							'states_id': results[i].getAttribute('states_id'),
							'state_names': results[i].getAttribute('state_names'),
							'region_id': results[i].getAttribute('region_id'),
							'tabindex': jj,
                                                        'station_id': '-1',
                                                        'foreigen_id': '-1',
							});
					    
						jj++;

					    } else {



						this.aSug.push(new cr_suggestion(results[i].getAttribute('id'), results[i].getAttribute('region_name'), results[i].getAttribute('state_names'), results[i].getAttribute('states_id'), "", results[i].getAttribute('region_id'), jj, '-1', '-1'));
					    
						jj++;
						
						//cr_suggestion = function (id, value, info, state_id, state_names, region_id, tabindex, station_id, foreigen_id, stations )
						var stations_arr = new Array();
						var station_nodes = results[i].getElementsByTagName('station');
								
						for (var k=0;k < station_nodes.length; k++) {

						    if (station_nodes[k].hasChildNodes()) {


							this.aSug.push(new cr_suggestion(station_nodes[k].getAttribute('sid'), station_nodes[k].childNodes[0].nodeValue, "", '-1', '', '-1', jj, station_nodes[k].getAttribute('sid'), station_nodes[k].getAttribute('fid') , null) );
									
							jj++;

						    }

						}
						
					    
						//results[i].getAttribute('region_name'),

						
					    }
   

					}  else {

					    break;

					}
							
				    }
				    
				}
				
		}
				
	}
	

	
	this.idAs = "as_"+this.fld.id;
	
	
	this.createList(this.aSug);

};














_b.AutoSuggest.prototype.createList = function(arr,more)
{
	
	
	var pointer = this;
	
	
	
	
	// get rid of old list
	// and clear the list removal timeout
	//
	_b.DOM.remE(this.idAs);
	this.killTimeout();
	
	
	// if no results, and shownoresults is false, do nothing
	//
	if (arr.length == 0 && !this.oP.shownoresults)
		return false;
	
	
	// create holding div
	//
	var div = _b.DOM.cE("div", {id:this.idAs, className:this.oP.className});	
	
	var hcorner = _b.DOM.cE("div", {className:"as_corner"});
	var hbar = _b.DOM.cE("div", {className:"as_bar"});
	var header = _b.DOM.cE("div", {className:"as_header"});
	header.appendChild(hcorner);
	header.appendChild(hbar);
	div.appendChild(header);
	
	
	
	
	// create and populate ul
	//
	var ul = _b.DOM.cE("ul", {id:"as_ul"});
	
	
	
	
	// loop throught arr of suggestions
	// creating an LI element for each suggestion
	//
	if(more){//flag to show the next results
		
				for (var i=15;(i<arr.length && i<30); i++)
			//for (var i=0;i<arr.length;i++)
			{
				// format output with the input enclosed in a EM element
				// (as HTML, not DOM)
				//
				
				var val = arr[i].value;
				var id = arr[i].id;
				var tab = arr[i].tabindex;
				//alert("arr[i].states_id;"+arr[i].states_id);
				
				var states_id = arr[i].states_id;
				var region_id = arr[i].region_id;
				var state_names = arr[i].state_names;
				
				
				var st = val.toLowerCase().indexOf( this.sInp.toLowerCase() );
				var output = val.substring(0,st) + "<em>" + val.substring(st, st+this.sInp.length) + "</em>" + val.substring(st+this.sInp.length);
				
				var span 		= _b.DOM.cE("span", {}, output, true);
				if (arr[i].info != "")
				{
					var br			= _b.DOM.cE("br", {});
					span.appendChild(br);
					var small		= _b.DOM.cE("small", {}, arr[i].info);
					span.appendChild(small);
				}
				
				if (arr[i].state_names != "")
				{
					var br			= _b.DOM.cE("br", {});
					span.appendChild(br);
					var small		= _b.DOM.cE("small", {}, arr[i].state_names);
					span.appendChild(small);
				}
				
				
				
				
				var a 			= _b.DOM.cE("a", { href:"#", alt:arr[i].id });
				
				var tl 		= _b.DOM.cE("span", {className:"tl"}, " ");
				var tr 		= _b.DOM.cE("span", {className:"tr"}, " ");
				
				a.appendChild(tl);
				a.appendChild(tr);
				a.appendChild(span);
		
				
				a.id=id;
				a.name = i+1;
				a.tabIndex = tab+1;
				//alert(a.tabindex);
				a.onclick = function () { pointer.setHighlightedValue();pointer.setSelectedId()/*pointer.initSatesuggest(this.id)*/; return false; };
				a.onmouseover = function () { pointer.setHighlight(this.name); };
				//pointer.aSug[pointer.iHigh].id, as_xml.aSug.states_id, as_xml.aSug.region_id
				
				var li = _b.DOM.cE(  "li", {}, a  );
				
				/**
				 * show results only if exact match
				 * js@navelar.de
				 * 2008/06/10
				 */
				
				if (st != -1) {
					ul.appendChild(li);
				}
		
			}
		
		
		
	}else{
		
				for (var i=0;(i<arr.length && i<15);i++)
			//for (var i=0;i<arr.length;i++)
			{
				// format output with the input enclosed in a EM element
				// (as HTML, not DOM)
				//
				
				var val = arr[i].value;
				var id = arr[i].id;
				var tab = arr[i].tabindex;
				//alert("arr[i].states_id;"+arr[i].states_id);
				
				var states_id = arr[i].states_id;
				var region_id = arr[i].region_id;
				var state_names = arr[i].state_names;
				var station_id = arr[i].station_id;
				var foreigen_id = arr[i].foreigen_id;

				var st = val.toLowerCase().indexOf( this.sInp.toLowerCase() );
				
				if (station_id == -1) {
				    //var output = val.substring(0,st) + "<em>" + val.substring(st, st+this.sInp.length) + "</em>" + val.substring(st+this.sInp.length);

				    var output = val.substring(0,st) + "<em>" + val.substring(st, st+this.sInp.length) +  "</em>" + val.substring(st+this.sInp.length);
				    var span   = _b.DOM.cE("p", {}, output, true);
				    var li_stations = _b.DOM.cE(  "li", {} );

				} else {

				     var output = "&nbsp;&nbsp;&nbsp;" + val.substring(0,st) +  val.substring(st, st+this.sInp.length) +  val.substring(st+this.sInp.length);;
				     var span 	= _b.DOM.cE("span", {}, output, true);

				     
				}


				if (arr[i].info != "")
				{
					var br			= _b.DOM.cE("br", {});
					span.appendChild(br);
					var small		= _b.DOM.cE("small", {}, arr[i].info);
					span.appendChild(small);
				}
				
				if (arr[i].state_names != "")
				{
					var br			= _b.DOM.cE("br", {});
					span.appendChild(br);
					var small		= _b.DOM.cE("small", {}, arr[i].state_names);
					span.appendChild(small);
				}
				
				
				if (station_id != -1) {				
				
				var a 			= _b.DOM.cE("a", { href:"#", alt:arr[i].id });
				
				
				//a.appendChild(tl);
				//a.appendChild(tr);
				a.appendChild(span);
				
				
				a.id=id;
				a.name = i+1;
				a.tabIndex = tab+1;
				//alert(a.tabindex);
				a.onclick = function () { pointer.setHighlightedValue();pointer.setSelectedId()/*pointer.initSatesuggest(this.id)*/; return false; };
				a.onmouseover = function () { pointer.setHighlight(this.name); };
				//pointer.aSug[pointer.iHigh].id, as_xml.aSug.states_id, as_xml.aSug.region_id
				
				var li = _b.DOM.cE(  "li", {}, a );
				//li_stations.appendChild( _b.DOM.cE(  "br", {} ));
				//li_stations.appendChild(a ,{});
				} else {


				var tl 		= _b.DOM.cE("span", {className:"tl"}, " ");
				var tr 		= _b.DOM.cE("span", {className:"tr"}, " ");

				//var li = _b.DOM.cE(  "li", {}, tl  );
				// li = _b.DOM.cE(  "li", {}, tr  );
				 var li = _b.DOM.cE(  "li", {}, span  );

				}
				/**
				 * show results only if exact match
				 * js@navelar.de
				 * 2008/06/10
				 */
				

				if ( station_id == -1 ) {
				    
				    ul.appendChild(li);
				} else {

				    ul.appendChild(li);
				}
			
			}
		
	}
	
	
	//MORE RESULTS ===================================================================================================================
	
		var a_more = _b.DOM.cE("a", { href:"#", alt:-1 });
		var tl_more 		= _b.DOM.cE("span", {className:"tl"}, " ");
		var tr_more 		= _b.DOM.cE("span", {className:"tr"}, " ");
		if(!more){
			var output_more = "<em>" + "more results..." + "</em>";	
		}else{
			var output_more = "<em>" + "previous results..." + "</em>";
		}
		
		var span_more 		= _b.DOM.cE("span", {}, output_more, true);
			
		a_more.appendChild(tl_more);
		a_more.appendChild(tr_more);
		a_more.appendChild(span_more);
		
		a_more.id=30;
		//a_more.name = "more_results";
		a_more.name = 30;
		
			//alert(a.tabindex);
		if (!more) {
			a_more.onclick = function () { pointer.setHighlightedValue();pointer.createList(arr,true);; return false; };
		}else{
			a_more.onclick = function () { pointer.setHighlightedValue();pointer.createList(arr);; return false; };
		}
		
		a_more.onmouseover = function () { pointer.setHighlight(this.name); };
		
		var li_more = _b.DOM.cE(  "li", {}, a_more  );
		if(arr.length >= 15){//only show more options if length of countries is >=15
			ul.appendChild(li_more);	
		}
		
			
		
		
	// 	END MORE RESULTS	============================================================================================================
	
	
	// no results
	//
	if (arr.length == 0 && this.oP.shownoresults)
	{
		var li = _b.DOM.cE(  "li", {className:"as_warning"}, this.oP.noresults  );
		ul.appendChild( li );
	}
	
	
	div.appendChild( ul );
	
	
	var fcorner = _b.DOM.cE("div", {className:"as_corner"});
	var fbar = _b.DOM.cE("div", {className:"as_bar"});
	var footer = _b.DOM.cE("div", {className:"as_footer"});
	footer.appendChild(fcorner);
	footer.appendChild(fbar);
	div.appendChild(footer);
	
	
	
	// get position of target textfield
	// position holding div below it
	// set width of holding div to width of field
	//
	var pos = _b.DOM.getPos(this.fld);
	
	div.style.left 		= pos.x + "px";
	div.style.top 		= ( pos.y + this.fld.offsetHeight + this.oP.offsety ) + "px";
	div.style.width 	= this.fld.offsetWidth + "px";
	
	
	
	// set mouseover functions for div
	// when mouse pointer leaves div, set a timeout to remove the list after an interval
	// when mouse enters div, kill the timeout so the list won't be removed
	//
	div.onmouseover 	= function(){ pointer.killTimeout() };
	div.onmouseout 		= function(){ pointer.resetTimeout() };


	// add DIV to document
	//
	document.getElementsByTagName("body")[0].appendChild(div);
	
	
	
	// currently no item is highlighted
	//
	this.iHigh = 0;
	
	
	
	
	
	
	// remove list after an interval
	//
	var pointer = this;
	this.toID = setTimeout(function () { pointer.clearSuggestions() }, this.oP.timeout);
};















_b.AutoSuggest.prototype.changeHighlight = function(key)
{	
	var list = _b.DOM.gE("as_ul");
	if (!list)
		return false;
	
	var n;

	if (key == 40)
		n = this.iHigh + 1;
	else if (key == 38)
		n = this.iHigh - 1;
	else if(key == 9)
		n = this.iHigh + 1;
	
	if (n > list.childNodes.length)
		n = list.childNodes.length;
	if (n < 1)
		n = 1;
	
	
	this.setHighlight(n);
};



_b.AutoSuggest.prototype.setHighlight = function(n)
{
	
	
	var list = _b.DOM.gE("as_ul");
	if (!list)
		return false;
	
	if (this.iHigh > 0)
		this.clearHighlight();
	
	this.iHigh = Number(n);
	if(this.iHigh == 30){
		document.getElementById(30).className = "as_highlight";//more results item
	}else{
		list.childNodes[this.iHigh-1].className = "as_highlight";	
	}
	


	this.killTimeout();
};


_b.AutoSuggest.prototype.clearHighlight = function()
{
	var list = _b.DOM.gE("as_ul");
	if (!list)
		return false;
	
	if (this.iHigh > 0 && this.iHigh < 30)
	{
		list.childNodes[this.iHigh-1].className = "";
		this.iHigh = 0;
	}else{
		document.getElementById(30).className = "";//more results item
	}
};


_b.AutoSuggest.prototype.setHighlightedValue = function ()
{
	if (this.iHigh)
	{
		this.sInp = this.fld.value = this.aSug[ this.iHigh-1 ].value;
		
		// move cursor to end of input (safari)
		//
		this.fld.focus();
		if (this.fld.selectionStart)
			this.fld.setSelectionRange(this.sInp.length, this.sInp.length);
		

		this.clearSuggestions();
		
		// pass selected object to callback function, if exists
		//
		if (typeof(this.oP.callback) == "function")
			this.oP.callback( this.aSug[this.iHigh-1] );
	}
};













_b.AutoSuggest.prototype.killTimeout = function()
{
	clearTimeout(this.toID);
};

_b.AutoSuggest.prototype.resetTimeout = function()
{
	clearTimeout(this.toID);
	var pointer = this;
	this.toID = setTimeout(function () { pointer.clearSuggestions() }, 120000);
};







_b.AutoSuggest.prototype.clearSuggestions = function ()
{
	
	this.killTimeout();
	
	var ele = _b.DOM.gE(this.idAs);
	var pointer = this;
	if (ele)
	{
		var fade = new _b.Fader(ele,1,0,250,function () { _b.DOM.remE(pointer.idAs) });
	}
};










// AJAX PROTOTYPE _____________________________________________


if (typeof(_b.Ajax) == "undefined")
	_b.Ajax = {};



_b.Ajax = function ()
{
	this.req = {};
	this.isIE = false;
};



_b.Ajax.prototype.makeRequest = function (url, meth, onComp, onErr)
{
	
	if (meth != "POST")
		meth = "GET";
	
	this.onComplete = onComp;
	this.onError = onErr;
	
	var pointer = this;
	
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest)
	{
		this.req = new XMLHttpRequest();
		this.req.onreadystatechange = function () { pointer.processReqChange() };
		this.req.open("GET", url, true); //
		this.req.send(null);
	// branch for IE/Windows ActiveX version
	}
	else if (window.ActiveXObject)
	{
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
		if (this.req)
		{
			this.req.onreadystatechange = function () { pointer.processReqChange() };
			this.req.open(meth, url, true);
			this.req.send();
		}
	}
};


_b.Ajax.prototype.processReqChange = function()
{
	
	// only if req shows "loaded"
	if (this.req.readyState == 4) {
		// only if "OK"
		if (this.req.status == 200)
		{
			this.onComplete( this.req );
		} else {
			this.onError( this.req.status );
		}
	}
};










// DOM PROTOTYPE _____________________________________________


if (typeof(_b.DOM) == "undefined")
	_b.DOM = {};



/* create element */
_b.DOM.cE = function ( type, attr, cont, html )
{
	var ne = document.createElement( type );
	if (!ne)
		return 0;
		
	for (var a in attr)
		ne[a] = attr[a];
	
	var t = typeof(cont);
	
	if (t == "string" && !html)
		ne.appendChild( document.createTextNode(cont) );
	else if (t == "string" && html)
		ne.innerHTML = cont;
	else if (t == "object")
		ne.appendChild( cont );

	return ne;
};



/* get element */
_b.DOM.gE = function ( e )
{
	var t=typeof(e);
	if (t == "undefined")
		return 0;
	else if (t == "string")
	{
		var re = document.getElementById( e );
		if (!re)
			return 0;
		else if (typeof(re.appendChild) != "undefined" )
			return re;
		else
			return 0;
	}
	else if (typeof(e.appendChild) != "undefined")
		return e;
	else
		return 0;
};



/* remove element */
_b.DOM.remE = function ( ele )
{
	var e = this.gE(ele);
	
	if (!e)
		return 0;
	else if (e.parentNode.removeChild(e))
		return true;
	else
		return 0;
};



/* get position */
_b.DOM.getPos = function ( e )
{
	var e = this.gE(e);

	var obj = e;

	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	
	var obj = e;
	
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;

	return {x:curleft, y:curtop};
};










// FADER PROTOTYPE _____________________________________________



if (typeof(_b.Fader) == "undefined")
	_b.Fader = {};





_b.Fader = function (ele, from, to, fadetime, callback)
{	
	if (!ele)
		return 0;
	
	this.e = ele;
	
	this.from = from;
	this.to = to;
	
	this.cb = callback;
	
	this.nDur = fadetime;
		
	this.nInt = 50;
	this.nTime = 0;
	
	var p = this;
	this.nID = setInterval(function() { p._fade() }, this.nInt);
};




_b.Fader.prototype._fade = function()
{
	this.nTime += this.nInt;
	
	var ieop = Math.round( this._tween(this.nTime, this.from, this.to, this.nDur) * 100 );
	var op = ieop / 100;
	
	if (this.e.filters) // internet explorer
	{
		try
		{
			this.e.filters.item("DXImageTransform.Microsoft.Alpha").opacity = ieop;
		} catch (e) { 
			// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
			this.e.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+ieop+')';
		}
	}
	else // other browsers
	{
		this.e.style.opacity = op;
	}
	
	
	if (this.nTime == this.nDur)
	{
		clearInterval( this.nID );
		if (this.cb != undefined)
			this.cb();
	}
};



_b.Fader.prototype._tween = function(t,b,c,d)
{
	return b + ( (c-b) * (t/d) );
};



cr_suggestion = function (id, value, info, state_id, state_names, region_id, tabindex, station_id, foreigen_id, stations ) {

    this.id = id;
    this.value = value;
    this.info = info;
    this.states_id = state_id;
    this.state_names = state_names;
    this.region_id = region_id;
    this.tabindex = tabindex;
    this.station_id = station_id;
    this.foreigen_id = foreigen_id;
    this.stations = stations;

};
