Array.prototype.push = function(value) 
{
	this[this.length]=value;
	return this;
};
Array.prototype.joinList = function(startList,endList,startItem,endItem,sep)
{
	var S="";
	for (var i=0; i<this.length; i++) S += (i ? sep : startList) + startItem+this[i]+endItem;
	if (this.length) S += endList;
	return S;
};
String.prototype.test = function(re)
{
	if (re instanceof RegExp) return re.test(this);
}
String.prototype.htmlEncode = function()
{
	var s = this;
	s = s.replace( /&/g , '&amp;');
	s = s.replace( /</g , '&lt;');
	s = s.replace( />/g , '&gt;');
	s = s.replace( /"/g , '&quot;');
	s = s.replace( /'/g , '&#039;');
	return s;
}
String.prototype.htmlDecode = function()
{
	var s = this;
	s = s.replace( /&lt;/g , '<');
	s = s.replace( /&gt;/g , '>');
	s = s.replace( /&quot;/g , '"');
	s = s.replace( /&#039;/g , "'");
	s = s.replace( /&amp;/g , '&');
	return s;                
}
String.prototype.splitLines = function()
{
	var s=this;
	s = s.replace( /\r/g , "\n");
	s = s.replace( /\n\n/g , "\n");
	return s.split("\n");
}
Array.prototype.joinLines = function()
{
	var R="";
	for (var i=0; i<this.length; i++) {
		if (this[i]) R += (i ? "\n" : "") + this[i].replace( /[\r\n]/g , " ");
	}
	return R;
}

function getE(id) { return document.getElementById(id); }

var TaxiOrder = 
{
	Pages    : null,
	CurPage  : 0,
	BtnBack  : null,
	BtnNext  : null,
	BtnSave  : null,
	_InitPage: 0,
	OnRegion : false,
	_lng     : {},
	FieldNext: 
	{
		_ignore  : false,
		_div     : null,
		_lng     : {},
		SetLang  : function(lng) {
			this._lng['add']  = (lng=='ru') ? "Добавить" : "Add";
			this._lng['del']  = (lng=='ru') ? "Удалить" : "Delete";
		},
		Items    : null,
		_newItem : function(index,value)
		{
			return '<input class="txt_next" type="text" id="txt_next_'+index+'" value="'+value.toString().htmlEncode()+'" />'
			     + '<a href="javascript:void(0);" onclick="TaxiOrder.FieldNext.Add();" title="'+this._lng['add']+'" class="add_button"></a>'
			     + '<a href="javascript:void(0);" onclick="TaxiOrder.FieldNext.Delete('+index+');" title="'+this._lng['del']+'" class="del_button"></a>';
		},
		_newDiv  : function(index,content)
		{
			return '<div id="div_next_'+index+'">'+content+'</div>';
		},
		Init     : function()
		{
			this._div=getE('dst_next_area');
			var o=getE('id_dst_next');
			if (this._ignore=!o) return;
			
			this.Items=o.value.splitLines();
			
			var i=this.Items.length;
			var R=this._newDiv(i, "");
			while (--i >= 0) R=this._newDiv(i, this._newItem(i, this.Items[i]) + R);
			this._div.innerHTML=R;
		},
		Add      : function() {
			if (this._ignore) return true;
			var i=this.Items.length;
			this.Items.push("");
			var o=getE('div_next_'+i);
			if (o) o.innerHTML=this._newItem(i, "")+this._newDiv(i+1, "");
		},
		Delete   : function(index)
		{
			if (this._ignore) return true;
			if (index<0 || index>=this.Items.length || this.Items.length==1) return;
			var o1,o2;
			for (var i=index; i<this.Items.length-1; i++) {
				o1=getE('txt_next_'+i);
				o2=getE('txt_next_'+(i+1));
				o1.value=o2.value;
				this.Items[i]=this.Items[i+1];
			}
			this.Items=this.Items.slice(0, -1);
			getE('div_next_' + this.Items.length).innerHTML="";
		},
		Verify   : function()
		{
			if (this._ignore) return true;
			var R=[], o;
			for (var i=0; i<this.Items.length; i++) {
				o=getE('txt_next_'+i);
				if (o && o.value) R.push(o.value);
			}
			if (o=getE('id_dst_next')) o.value=R.joinLines();
			return true;
		}
	},
	SetLang  : function(lng, isFav)
	{
		this._lng['send']  = isFav ? ((lng=='ru') ? "Сохранить" : "Save") : ((lng=='ru') ? "Отправить" : "Send");
		this._lng['back']  = (lng=='ru') ? "<< Назад" : "<< Back";
		this._lng['next']  = (lng=='ru') ? "Далее >>" : "Next >>";
		this._lng['save']  = (lng=='ru') ? "Сохранить в списке маршрутов" : "Save in favorite orders";
		this._lng['save_h']= (lng=='ru') ? "Заказ сохранится в Вашем списке маршрутов в разделе «Личный кабинет»" : "The order will be saved in your favorite orders list in «Личный кабинет»";
		this._lng['save_ask'] = (lng=='ru') ? "Укажите будущее название маршрута" : "Type a title for the order";
		this._lng['save_ask_v'] = (lng=='ru') ? "Новый маршрут" : "New order";
		this._lng['page']  = (lng=='ru') ? "Стр." : "Page";
		this._lng['pageName'] = ['', "R", "N", "D"];
		this._lng['service']= (lng=='ru') ? "Выберите услугу!" : "Select a service!";
		this._lng['payment']= (lng=='ru') ? "Выберите споcоб оплаты!" : "Select payment type!";
		this._lng['car_class']= (lng=='ru') ? "Выберите класс автомобиля!" : "Select car class!";
		this._lng['radio']  = (lng=='ru') ? "Выберите радиостанцию!" : "Select a radio!";
		this._lng['phone']  = (lng=='ru') ? "Укажите номер телефона!" : "Input phone number!";
		this._lng['metro'] = (lng=='ru') ? "Выберите станцию метро!" : "Select a metro station!";
		this._lng['street'] = (lng=='ru') ? "Укажите улицу!" : "Input street!";
		this._lng['city']   = (lng=='ru') ? "Укажите город!" : "Input city!";
		this._lng['region'] = (lng=='ru') ? "Укажите район!" : "Input region!";
		this._lng['airport']= (lng=='ru') ? "Выберите аэропорт!" : "Select an airport!";
		this._lng['rail']   = (lng=='ru') ? "Выберите вокзал!" : "Select a rail station!";
		this.FieldNext.SetLang(lng);
	},
	_lngPage : function(index)
	{
		return "<b>" + this._lng['page'] + index.toString() + " - " + this._lng['pageName'][index] + "</b>: ";
	},
	Init     : function(start,pages)
	{
		this._InitPage = start;
		this.Pages = pages.slice(0,-1);
		var d;
		if (d=getE('label_total_pages')) d.innerHTML = this.Pages.length.toString();
		this.BtnBack = getE('btnBack');
		this.BtnNext = getE('idbtnSubmit');
		this.BtnSave = getE('idbtnFavSave');
		this.FieldNext.Init();
		this.BtnBack.value = this._lng['back'];
		if (this.BtnSave) {
			this.BtnSave.value = this._lng['save'];
			this.BtnSave.title = this._lng['save_h'];
		}
		else {
			this.FavSave = null;
		};
		this._ShowPage(this._InitPage, true);
	},
	_ShowPage : function(index, show)
	{
		if (! this.Pages[index]) return false;
		var f = getE(this.Pages[index]);
		if (!f) return false;
		f.style.display = show ? 'block' : 'none';
		if (f = getE('tab_'+this.Pages[index])) {
			f.style.borderColor = show ? '#999' : '#fff';
		}
		if (show) {
			this.CurPage = index;
			index++;
			this.BtnBack.style.visibility = this.IsFirst() ? 'hidden' : 'visible';
			this.BtnNext.value = this.IsLast() ? this._lng['send'] : this._lng['next'];
			if (this.BtnSave) this.BtnSave.style.display = this.IsLast() ? '' : 'none';
			OrderStatus.Switch(index+1);
		}
		return true;
	},
	_GoPage   : function(offset)
	{
		var new_page = this.CurPage + (offset>=0 ? 1 : -1);
		if (new_page<0 || new_page>=this.Pages.length) return false;
		for (var i=0; i<this.Pages.length; i++) this._ShowPage(i, false);
		return this._ShowPage(new_page, true);
	},
	IsFirst  : function()
	{
		return this.CurPage == 0;
	},
	IsLast   : function()
	{
		return this.CurPage == this.Pages.length-1;
	},
	Next     : function()
	{
		if (this.IsLast()) return this.Verify();
		this._GoPage(+1);
		return false;
	},
	Back     : function()
	{
		if (this.IsFirst()) return true;
		this._GoPage(-1);
		return false;
	},
	FavSave  : function() {
		if (this.Verify()) {
			var o = getE('id_fav_title');
			if (o) {
				var s;
				do {
					s = prompt(this._lng['save_ask'], this._lng['save_ask_v']);
					if (s == void 0) return false;
				} while (/^\s*$/.test(s));
				o.value = s;
				o.disabled = false;
			};
			return true;
		}
		else return false;
	},
	Goto     : function(index)
	{
		for (var i=0; i<this.Pages.length; i++) this._ShowPage(i, false);
		this._ShowPage(index, true);
	},
	Reset    : function()
	{
		this.Goto(this._InitPage);
	},
	Verify   : function() 
	{
		var o, r=[], page=0, n;
		// check first page
		
		if (o=getE('id_service')) {	// check service
			if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(1) + this._lng['service']);
		}
		if (o=getE('id_user_phone1')) {	// check phone
			if (TextInline.isEmpty(o.id) || !o.value.test( /^([-+\s\(\)]*\d){10,11}\s*$/ )) r.push(this._lngPage(1) + this._lng['phone']);
		}
		if (o=getE('id_payment')) {	// check payment
			if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(1) + this._lng['payment']);
		}
		if (o=getE('id_car_class')) {	// check car_class
			if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(1) + this._lng['car_class']);
		}
		if (o=getE('id_radio')) {	// check radio
			if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(1) + this._lng['radio']);
		}
		
		// check second page
		if (!r.length) page++;
		
		if ((o=getE('id_source')) && (n=o.value)) {	// determine subform
			if (n=='0') { // on Moscow check metro and street
				/* if (o=getE('id_source_0_src_metro')) {	// check metro
					if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(2) + this._lng['metro']);
				} */
				if ((o=getE('txtAjax_1')) && TextInline.isEmpty(o.id)) r.push(this._lngPage(2) + this._lng['street']);
			}
			else if (n=='1') {
				if ((o=getE('txtAjax_2')) && TextInline.isEmpty(o.id)) r.push(this._lngPage(2) + this._lng['city']);
				if ((o=getE('txtAjax_3')) && TextInline.isEmpty(o.id)) r.push(this._lngPage(2) + this._lng['street']);
			}
			else if (this.OnRegion && n=='2') {
				if ((o=getE('txtAjax_4')) && TextInline.isEmpty(o.id)) r.push(this._lngPage(2) + this._lng['region']);
				if ((o=getE('txtAjax_5')) && TextInline.isEmpty(o.id)) r.push(this._lngPage(2) + this._lng['street']);
			}
			else if (n==( this.OnRegion ? '3' : '2' )) {
				if (o=getE('id_source_'+n+'_src_airport')) {	// check airport
					if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(2) + this._lng['airport']);
				}
			}
			else if (n==( this.OnRegion ? '4' : '3' )) {
				if (o=getE('id_source_'+n+'_src_rail')) {	// check rail
					if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(2) + this._lng['rail']);
				}
			}
			else if (n==( this.OnRegion ? '5' : '4' )) {
				if (o=getE('id_source_'+n+'_src_metro')) {	// check metro
					if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(2) + this._lng['metro']);
				}
			}
		}
		
		if (!this.OnRegion) {
			// check second page
			if (!r.length) page++;
			
			if ((o=getE('id_destination')) && (n=o.value)) {
				if (n=='0') {
					/* if (o=getE('id_destination_0_dst_metro')) {	// check metro
						if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(3) + this._lng['metro']);
					} */
					if ((o=getE('txtAjax_4')) && TextInline.isEmpty(o.id)) r.push(this._lngPage(3) + this._lng['street']);
				}
				else if (n=='1') {
					if ((o=getE('txtAjax_5')) && TextInline.isEmpty(o.id)) r.push(this._lngPage(3) + this._lng['city']);
					if ((o=getE('txtAjax_6')) && TextInline.isEmpty(o.id)) r.push(this._lngPage(3) + this._lng['street']);
				}
				else if (n=='2') {
					if (o=getE('id_destination_2_src_airport')) {	// check airport
						if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(3) + this._lng['airport']);
					}
				}
				else if (n=='3') {
					if (o=getE('id_destination_3_src_rail')) {	// check rail
						if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(3) + this._lng['rail']);
					}
				}
				else if (n=='4') {
					if (o=getE('id_destination_4_src_metro')) {	// check metro
						if (!o.value || o.value.test( /^0?$/ )) r.push(this._lngPage(4) + this._lng['metro']);
					}
				}
			}
		}
		
		// result
		var o=getE('order_paged_report');
		if (r.length) {
			this.Goto(page);
			if (o) o.innerHTML = r.joinList("<ul>","</ul>","<li>","</li>","\n");
			//else alert(r.joinList("","","+ ","","\n"));
		}
		o.style.display = r.length ? "block" : "none";
		return !r.length && this.FieldNext.Verify();
	}
};

var BlinkText =
{
	_items   : {},
	c1       : 'blink1',
	c2       : 'blink2',
	Create   : function(_id, _interval)
	{
		if (this._items[_id]) return false;
		this._items[_id] =
		{
			id : _id,
			interval : _interval,
			span : null,
			on : false
		};
		window.setInterval(function(){ BlinkText.DoBlink(_id); }, _interval);
	},
	DoBlink  : function(_id)
	{
		if (! this._items[_id].span) this._items[_id].span = getE(this._items[_id].id);
		if (this._items[_id].span) {
			this._items[_id].span.className = (this._items[_id].on = !this._items[_id].on) ? BlinkText.c1 : BlinkText.c2;
			// setTimeout('BlinkText.DoBlink("'+_id+'");', this._items[_id].interval);
		}
	}
};

var Tabs =
{
	_items   : {},
	Add      : function(name,items,init)
	{
		var t, p, n = null, o;
		for (var i=0; i<items.length; i++) {
			t = getE('Tab'+items[i]);
			p = getE('Page'+items[i]);
			if (t && p) {
				if (!this._items[name]) this._items[name] = {};
				this._items[name][items[i]] = {Tab : t, Page : p, TabClass : t.className, PageClass : p.className };
			}
		}
		if (!items[init]) init = 0;
		if (items[init]) this.Show(name, items[init]);
	},
	Show     : function(name,id)
	{
		var k;
		if (id && id.substr(0, 3)=='Tab') id = id.substr(3);
		if (this._items[name] && this._items[name][id]) {
			for (k in this._items[name]) this._Select(this._items[name][k], false);
			this._Select(this._items[name][id], true);
		}
		if (name=='FormType') {
			OrderStatus.Switch(id=='FormPaged' ? TaxiOrder.CurPage+2 : 1);
		}
	},
	_Select  : function(obj, sel)
	{
		if (!obj.Tab || !obj.Page) return;
		var k;
		obj.Page.className = obj.PageClass + (obj.PageClass.length ? ' ' : '') + (sel ? "act" : "deact");
		obj.Tab.className = obj.TabClass + (obj.TabClass.length && sel ? ' ' : '') + (sel ? "act" : "");
	}
};

var OrderStatus =
{
	ImgArrow : null,
	ImgGear  : null,
	ImgDone  : null,
	_posted  : false,
	_done    : false,
	Switch   : function(index)
	{
		if (this._posted) index=4;
		if (!this.ImgArrow) this.ImgArrow = getE('order_ind_fuel');
		if (!this.ImgGear) this.ImgGear = getE('order_ind');
		if (this.ImgArrow) this.ImgArrow.className = 'order_arrow bg' + index.toString();
		if (this.ImgGear) this.ImgGear.className = 'order_pages bg' + index.toString();
	},
	Done     : function()
	{
		if (!this.ImgDone) this.ImgDone = getE('gals_taxi_car');
		if (this._done && this.ImgDone) {
			// this.ImgDone.style.backgroundPosition='40% -516px';
			this.ImgDone.className += ' enlight';
		}
	}
};

var TextInline =
{
	_items  : {},
	_clGrey : '#999',
	_clDef  : '#000',
	add     : function(id,text)
	{
		var e = getE(id);
		if (!e || !text) return false;
		var o = { element : e, title : text, empty : !(e.value && e.value.length) };
		if (o.empty) {
			o.element.value = text;
			o.element.style.color = this._clGrey;
		}
		this._items[id] = o;
		return true;
	},
	focus  : function(id)
	{
		var o = this._items[id];
		if (!o) return;
		if (o.empty) {
			o.element.value = '';
			o.element.style.color = this._clDef;
		}
	},
	blur   : function(id)
	{
		var o = this._items[id];
		if (!o) return;
		o.empty = !o.element.value;
		if (o.empty) {
			o.element.value = o.title;
			o.element.style.color = this._clGrey;
		}
	},
	reset  : function()
	{
		var id, o;
		for (id in this._items) {
			o = this._items[id];
			if (o.empty) o.element.value = '';
		}
	},
	isEmpty : function(id)
	{
		var o = this._items[id];
		return o ? o.empty : true;
	}
};

function iPopup(url, args) {
/* width : int
 * height : int
 * left : int
 * top : int
 * location : bool
 * menubar : bool
 * resizable : bool
 * scrollbars : bool
 * status : bool
 * toolbar : bool
 */
	args = args || {};
	args.location = args.location || false;
	args.menubar = args.menubar || false;
	args.resizable = args.resizable || true;
	args.status = args.status || false;
	args.toolbar = args.toolbar || false;
	
	var s = [], v;
	for (var k in args) {
		v = args[k];
		if (typeof v == typeof false) v = v ? 'yes' : 'no';
		s.push(k + '=' + v);
	};
	var r;
	try {
		r = window.open(url, '_blank', s.join(','));
	}
	catch (e) {
		r = false;
	};
	return r;
};


