/**
*/
var SRV_CONFIG_VAT_RATE = 0;
var SRV_CONFIG_CURRENCY = '';
var SRV_CONFIG_USER_PRICE = '';



/**
*/
function cConfigurator(server_key, phpsid, order_type){
	this.server_key = server_key;
	this.phpsid = phpsid;
	this.order_type = order_type;
}

cConfigurator.prototype = {
	
	Options: {},
	Bonds: {},
	sections: new Array,
	VAT_RATE: 0.19,
	
	ajax_httpRequest: null,
	
	/** ____________________________________________________________________________________________________________________ /
		#
	*/
	ItemTables_Init: function(ConfiguratorTablesId){
		
		this.Options = new cOptions();
		
		var ConfiguratorTables = $(ConfiguratorTablesId);
		var ItemTable = ConfiguratorTables.firstChild;
		while(ItemTable){
			if(ItemTable.tagName=='UL'){
				
				var ItemTableInfo = /ConfigItems:([a-z]+)/.exec(ItemTable.id);
				this.sections.push(ItemTableInfo[1]);
				// 
				this._ItemTable_Traverse(ItemTable);
				
			}// if(ItemTable.tagName=='UL')
			ItemTable = ItemTable.nextSibling;
		}// while(ItemTable)
		
	},// Init: function
	
	
	
	
	/** ____________________________________________________________________________________________________________________ /
		# 
	*/
	_ItemTable_Traverse: function(ul_ItemTable){
		var li = ul_ItemTable.firstChild;
		while(li){
			if(li.tagName=="LI" && jscss.isClassName(li, "ItemRow")){
				
				li.onmouseover = function(e){
					var e = e||event;
					e.cancelBubble = true;
					var li = this.parentNode.firstChild;
					while(li){
						if(li.tagName=='LI'){
							li.style.zIndex = (li.id==this.id?1:0);
						}// if(li.tagName=='LI')
						li = li.nextSibling;
					}// while(li)
				}// li.onmouseover = function()
				
				//li.onmouseout = function(){
					//this.style.backgroundColor = '#fafafa';
				//}
								
				/* znaleźć opcje alternatywne dla opcji wybranej
				*/
				var that = this;
				var ul2 = li.firstChild;
				while(ul2){
					if(ul2.tagName=='UL'){
						var li2 = ul2.firstChild;
						while(li2){
							if(li2.tagName=='LI'){
								this.GetOptionFromItemHTML(li2);
								li2.onclick = function(event){
									that.ItemOption_onClick(event, this);
								}
								li2.onmouseover = function(event){
									that.ItemOption_onMouseOver(event, this);
								}
								li2.onmouseout = function(event){
									that.ItemOption_onMouseOut(event, this);
								}
								// znaleźć subopcje
								var ul3 = li2.firstChild;
								while(ul3){
									if(ul3.tagName=="UL"){
										var li3 = ul3.firstChild;
										while(li3){
											if(li3.tagName=='LI'){
												this.GetOptionFromItemHTML(li3);
												li3.onclick = function(event){
													that.ItemOption_onClick(event, this);
												}
											}// if(li3.tagName=='LI')
											li3 = li3.nextSibling;
										}// while(li3)
										//alert(ul3.innerHTML);
									}// if(ul3.tagName=="UL")
									ul3 = ul3.nextSibling;
								}// while(ul3)
							}// if(li2.tagName=='LI')
							li2 = li2.nextSibling;
						}// while(li2)
					}// if(ul2.tagName=='UL')
					ul2 = ul2.nextSibling;
				}// while(ulAlternativeList)
				
				
			
			}// if(li.tagName=='LI')
			li = li.nextSibling;
		}// while(li)
		
		
		// sprawdzić required
		var li = ul_ItemTable.firstChild;
		while(li){
			if(li.tagName=="LI" && jscss.isClassName(li, "ItemRow")){
				var liIdSplit = /ItemRow:([0-9]{1,})/.exec(li.id);
				if(liIdSplit){
					var parents_input = $('parents:' + liIdSplit[1]);
					if(parents_input.value!=''){
						var parents = parents_input.value.split(",");
						for(var i=0; i<parents.length; i++){
							var item = this.Options.getByKey(parents[i]);
							item.childs.push(liIdSplit[1]);
							var ItemsInCategory = this.Options.getFromCategory(liIdSplit[1]);
							for(var iic=0; iic<ItemsInCategory.length; iic++){
								ItemsInCategory[iic].visible = false;
							}
						}// for(var i=0; i<parents.length; i++)
						li.style.display = 'none';
					}// if(parents_input.value!='')
				}// if(liIdSplit)
			}// if(li.tagName=="LI" && jscss.isClassName(li, "ItemRow"))
			li = li.nextSibling;
		}// while(li)
		
		for(var itm=0; itm<this.Options.Items.length; itm++){
			var Item = this.Options.Items[itm];
			if(Item.marked && Item.visible){
				for(var i=0; i<Item.childs.length; i++){
					var li = $('ItemRow:' + Item.childs[i]);
					li.style.display = 'block';
					var items = this.Options.getFromCategory(Item.childs[i]);
					for(var it=0; it<items.length; it++){
						items[it].visible = true;
						if(items[it].marked){
							if(items[it].childs.length>0){
								for(var i2=0; i2<items[it].childs.length; i2++){
									var li2 = $('ItemRow:' + items[it].childs[i2]);
									li2.style.display = 'block';
									var items2 = this.Options.getFromCategory(items[it].childs[i2]);
									for(var it2=0; it2<items2.length; it2++){
										items2[it2].visible = true;
									}
								}
							}
						}
					}
				}// for(var i=0 i<Item.childs.length; i++)
			}// if(Item.marked && Item.visible)
		}// for(var itm=0; itm<this.Options.Items; itm++)
			
	},// _ItemTable_Traverse: function
	
	
	
	
	/** ____________________________________________________________________________________________________________________ /
		# 
	*/
	ItemOption_onClick: function(e, ItemOption){
		var e = e||event;
		e.cancelBubble = true;
		
		var ItemIdSplit = /ItemOption:([0-9]{1,}):([0-9]{1,})/.exec(ItemOption.id);
		if(ItemIdSplit!=null){
			
			var category = ItemIdSplit[1];
			var key = ItemIdSplit[2]
			var oItem = this.Options.getByKey(key);
			
			if(oItem.marked==false){
				
				var input_category = $('category:' + category);
				var key_old = input_category.value;
				var oItemOld = this.Options.getByKey(key_old);
				
				// usunąć kategorie
				for(var i=0; i<oItemOld.childs.length; i++){
					var li = $('ItemRow:' + oItemOld.childs[i]);
					li.style.display = 'none';
					var items = this.Options.getFromCategory(oItemOld.childs[i]);
					for(var it=0; it<items.length; it++){
						items[it].visible = false;
						if(items[it].childs.length>0){
							for(var i2=0; i2<items[it].childs.length; i2++){
								var li2 = $('ItemRow:' + items[it].childs[i2]);
								li2.style.display = 'none';
								var items2 = this.Options.getFromCategory(items[it].childs[i2]);
								for(var it2=0; it2<items2.length; it2++){
									items2[it2].visible = false;
								}
							}
						}
					}
				}// for(var i in oItemOld.childs)
				
				// dodać kategorie
				for(var i=0; i<oItem.childs.length; i++){
					var li = $('ItemRow:' + oItem.childs[i]);
					li.style.display = 'block';
					var items = this.Options.getFromCategory(oItem.childs[i]);
					for(var it=0; it<items.length; it++){
						items[it].visible = true;
						//
						if(items[it].marked && items[it].childs.length>0){
							for(var i2=0; i2<items[it].childs.length; i2++){
								var li2 = $('ItemRow:' + items[it].childs[i2]);
								li2.style.display = 'block';
								var items2 = this.Options.getFromCategory(items[it].childs[i2]);
								for(var it2=0; it2<items2.length; it2++){
									items2[it2].visible = true;
								}
							}
						}
						//
					}
				}// for(var i in oItem.childs)
				
				
				// wysyłamy to co user kliknął w opcjach
				if(this._ajax_httpRequest_Create()){
					var that = this;
					this.ajax_httpRequest.onreadystatechange = function() { that._ajax_Response_UserServerOptions(); };
					var url = '/utils/js/ajax.php?client_ssid=' + this.phpsid + '&script='+this.order_type+'&action=config&params=' + this.server_key + ',' + category + ',' + key;
					this.ajax_httpRequest.open('GET', url, true);
					this.ajax_httpRequest.send(null);
				}// if(this._ajax_httpRequest_Create())
				
				// ustawić marker na wybranej pozycji i zmienną marked w obiekcie
				var allItemsInGroup = this.Options.getFromCategory(category);
				for(var i=0; i<allItemsInGroup.length; i++){
					var li = $('ItemOption:' + allItemsInGroup[i].category + ':' + allItemsInGroup[i].key);
					if(allItemsInGroup[i].key==key){
						jscss.add(li, 'ItemOptionSelected');
						allItemsInGroup[i].marked = true;
					}else{
						jscss.remove(li, 'ItemOptionSelected');
						allItemsInGroup[i].marked = false;
					}// if(allItemsInGroup[i].key==key)
				}// for(var i=0
				
				// wpisać wybraną pozycję do div.ItemOptionSelected
				var ItemRow = $('ItemRow:' + category);
				var divItOptSelected = ItemRow.firstChild;
				while(divItOptSelected){
					if(divItOptSelected.tagName=='DIV' && jscss.isClassName(divItOptSelected, 'ItemOptionSelected')){
						var divs = divItOptSelected.getElementsByTagName('DIV');
						divs[0].innerHTML = this.Options.getNameFromHTML(category, key);
						var spans = divs[1].getElementsByTagName('SPAN');
						spans[0].innerHTML = oItem.bond.strPrice(SRV_CONFIG_USER_PRICE);
						spans[1].innerHTML = SRV_CONFIG_CURRENCY;
						divs[1].style.display = oItem.bond.netto==0 ? 'none' : 'block';
						break;
					}
					divItOptSelected = divItOptSelected.nextSibling;
				}// while(divItOptSelected)
				
				// wpisać klucz option do inputa kategorii
				input_category.value = key;
					
				this.Options.Calculate();
				this.Bonds.Calculate(this.Options.total_bond.netto, this.Options.total_setup.netto);
				this.DisplayPrices();
				
				//alert('total_bond netto:' + this.Options.total_bond.netto + ' brutto:' + this.Options.total_bond.brutto);
				
			}// if(oItem.marked==false)
			
		}// if(ItemIdSplit!=null)
		
	},// ItemOption_onClick: function
	
	
	
	/** ____________________________________________________________________________________________________________________ /
		# 
	*/
	ItemOption_onMouseOver: function(e, ItemOption){
		var e = e||event;
		e.cancelBubble = true;
		
		var uls = ItemOption.getElementsByTagName('UL');
		if(uls.length>0){
			uls[0].style.display = 'block';
		}
		
	},// ItemOption_onClick: function
	
	
	/** ____________________________________________________________________________________________________________________ /
		# 
	*/
	ItemOption_onMouseOut: function(e, ItemOption){
		var e = e||event;
		e.cancelBubble = true;
		
		var uls = ItemOption.getElementsByTagName('UL');
		if(uls.length>0){
			uls[0].style.display = 'none';
		}
		
	},// ItemOption_onClick: function
	
	
	/** 
		 GetOptionFromItemHTML
			@param  funkcja tworzy Options.Item na podstawie HTML, w argumencie przekazywany jest LI
			@return
	*/
	GetOptionFromItemHTML: function(ItemOption){
		
		var ItemIdSplit = /ItemOption:([0-9]{1,}):([0-9]{1,})/.exec(ItemOption.id);
		if(ItemIdSplit){
			
			var input_category = $('category:' + ItemIdSplit[1]);
			var marked = (input_category.value==ItemIdSplit[2]);
				
			var divs = ItemOption.getElementsByTagName('DIV');
			var spans = divs[1].getElementsByTagName('SPAN');
			var bond = spans[0].innerHTML;
			var setup = spans[1].innerHTML;
			
			this.Options.Add(ItemIdSplit[1], ItemIdSplit[2], bond, setup, marked);
				
		}// if(ItemInfo!=null)
		
	},// GetOptionFromItemHTML
	
	
	/** Inicjacja opcji usera: Osobowość prawna, Siedziba, Waluta.
			funkcja pobiera z input[hidden] wartości i pokazuje w output
	*/
	UserCoreOptions_Init: function(OptionBoxId){
		this.UserCoreOptionBox = $(OptionBoxId);
		if(!this.UserCoreOptionBox) return;
		this.UserCoreOptionBox.addClassName('ucoBox_Disabled');
		//
		var div = this.UserCoreOptionBox.firstChild;
		while(div){
			if(div.tagName=="DIV"){
				var UserCoreOption_IdSplit = /^uscopt_([a-z]+)$/.exec(div.id);
				if(UserCoreOption_IdSplit){
					var _ucoptId = UserCoreOption_IdSplit[1];
					var _val = $(_ucoptId).value;
					var _output = $(_ucoptId + ':output');
					_output.innerHTML = $(_ucoptId + ':' + _val).innerHTML;
					//_output.update($(_ucoptId + ':' + _val).innerHTML);
				}// if(UserCoreOption_IdSplit)
			}// if(div.tagName=="DIV")
			div = div.nextSibling;
		}// while(div)
	}, // UserCoreOptions_Init: function
	
	
	
	/** ____________________________________________________________________________________________________________________ /
		# SetLowStatute
	*/
	UserCoreOptions_Change: function(optionType, newValue){
		
		var input_hidden = $(optionType);
		var isChanged = input_hidden.value!=newValue;
		
		if(isChanged){
			
			switch(optionType){
				case 'lowstatute':
						
					break;
				case 'country':
						
					break;
				case 'currency':
						
					break;
			}// switch(optionType)
			
			input_hidden.value = newValue;
			var _output = $(optionType + ':output');
			_output.innerHTML = $(optionType + ':' + newValue).innerHTML;
			
			this.SendUserCoreOptions();
			
		}// if(isChanged)
		
	}, // UserCoreOptions_Change: function
	
	
	/** ____________________________________________________________________________________________________________________ /
		# SendNewBond
		funkcja wysyła poprzez AJAX wybór usera
	*/
	SendNewBond: function(){
		
		if(this._ajax_httpRequest_Create()){
			
			var that = this;
			this.ajax_httpRequest.onreadystatechange = function() { that._ajax_Response_SendNewBond(); };
			var url = '/utils/js/ajax.php?client_ssid=' + this.phpsid + '&script=server&action=bond&params=' + this.server_key + ',' + this.Bonds.months;
			//alert(url); return;
			this.ajax_httpRequest.open('GET', url, true);
			this.ajax_httpRequest.send(null);
			
		}// if(this._ajax_httpRequest_Create())
		
	}, // SendNewBond: function
	
	
	/**
		funkcja wysyła poprzez AJAX wybór usera i zwraca nową tabelę
	*/
	SendUserCoreOptions: function(){
		
		// ustawić optionbox na disabled
		//var UserCoreOptionBox = $('UserCoreOptionBox');
		//jscss.add(UserCoreOptionBox, 'ucoBox_Disabled');
		
		this.UserCoreOptionBox.addClassName('ucoBox_Disabled');
		
		// pokazać preloaderki
		for(var i=0; i<this.sections.length; i++) this._ajax_Preloader_Display('ConfigItems:' + this.sections[i], 'Preloader:' + this.sections[i], true);
		this._ajax_Preloader_Display('BondContent', 'Preloader:bonds', true);
		this._bondsBttEnabled(false);
		
		var user_core_options_value = Array();
		var input_hidden = this.UserCoreOptionBox.getElementsByTagName('INPUT');
		for(var i=0; i<input_hidden.length; i++){
			user_core_options_value.push(input_hidden[i].value);
		}
		
		if(this._ajax_httpRequest_Create()){
			var that = this;
			this.ajax_httpRequest.onreadystatechange = function() { that._ajax_Response_UserCoreOptions(); };
			var url = '/utils/js/ajax.php?client_ssid=' + this.phpsid + '&script=server&action=client&params=' + this.server_key + ',' + user_core_options_value.join(',');
			this.ajax_httpRequest.open('GET', url, true);
			this.ajax_httpRequest.send(null);
		}// if(this._ajax_httpRequest_Create())
		
	}, // SendUserCoreOptions: function
	
	
	/**
	*/
	_ajax_Response_UserServerOptions: function() {
		if(this.ajax_httpRequest.readyState == 4){
			if(this.ajax_httpRequest.status == 200){
				var xmldoc = this.ajax_httpRequest.responseXML;
				var nodeRoot = xmldoc.getElementsByTagName('SERVER_CONFIG').item(0);
				var result = nodeRoot.getAttributeNode('result').value;
				if(result==0){
					// error
				}else{
				
				}
			}
		}
	}, // _ajax_Response_UserServerOptions: function
	
	
	
	/** ____________________________________________________________________________________________________________________ /
		# _ajax_Response_UserCoreOptions
	*/
	_ajax_Response_UserCoreOptions: function() {
		if(this.ajax_httpRequest.readyState == 4){
			if(this.ajax_httpRequest.status == 200){
				var xmldoc = this.ajax_httpRequest.responseXML;
				var nodeRoot = xmldoc.getElementsByTagName('SERVER_CONFIG').item(0);
				var result = nodeRoot.getAttributeNode('result').value;
				if(result==0){
					
					// ustawić optionbox na enabled
					//var UserCoreOptionBox = $('UserCoreOptionBox');
					this.UserCoreOptionBox.removeClassName('ucoBox_Disabled');
					//jscss.remove(UserCoreOptionBox, 'ucoBox_Disabled');
					
					// ukryć preloaderki
					for(var i=0; i<this.sections.length; i++) this._ajax_Preloader_Display('ConfigItems:' + this.sections[i], 'Preloader:' + this.sections[i], false);
					this._ajax_Preloader_Display('BondContent', 'Preloader:bonds', false);
					this._bondsBttEnabled(true);
					
				}else{
					document.location = location.href;
				}
			}
		}
	}, // _ajax_Response_UserCoreOptions: function
	 
	 
	 /**
	*/
	_ajax_Response_SendNewBond: function() {
		if(this.ajax_httpRequest.readyState == 4){
			if(this.ajax_httpRequest.status == 200){
				var xmldoc = this.ajax_httpRequest.responseXML;
				var nodeRoot = xmldoc.getElementsByTagName('SERVER_CONFIG').item(0);
				if(nodeRoot){
					var result = nodeRoot.getAttributeNode('result').value;
					if(result==0){
						alert('error #' + result);
					}else{
						this._ajax_Preloader_Display('BondContent', 'Preloader:bonds', false);
						this._bondsBttEnabled(true);
					}
				}else{
					alert('nodeRoot is null');
				}
			}
		}
	}, // _ajax_Response_UserServerOptions: function
	 
	 
	
	/** ____________________________________________________________________________________________________________________ /
		# _ajax_httpRequest_Create
	*/
	_ajax_httpRequest_Create: function(){
		
		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
				this.ajax_httpRequest = new XMLHttpRequest();
				if (this.ajax_httpRequest.overrideMimeType) {
						this.ajax_httpRequest.overrideMimeType('text/xml');
						// See note below about this line
				}
		}else if(window.ActiveXObject) { // IE
				try {
						this.ajax_httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} 
				catch (e) {
						try {
								this.ajax_httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
						} 
						catch (e) {}
				}
		}
		
		if (!this.ajax_httpRequest) {
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
		}else{
			return true;
		}
		
	}, // _ajax_httpRequest_Create: function

	
	/** 
	*/
	BondSummary_Init: function(){
		
		this.Bonds = new cServerBonds($('bonds').value);
		
		var that = this;
		var BondButtons = $('BondButtons');
		var li = BondButtons.firstChild;
		while(li){
			if(li.tagName=='LI'){
				//
				var btt_IdSplit = /^BondButton:([0-9]{1,})$/.exec(li.id);
				if(btt_IdSplit){
					//
					this.Bonds.months==btt_IdSplit[1] ? jscss.add(li,'tabStripBttSelected') : jscss.remove(li,'tabStripBttSelected');
					/**
					*/
					li.onclick = function(){
						var btt_IdSplit = /^BondButton:([0-9]{1,})$/.exec(this.id);
						if(btt_IdSplit){
							if(newBondScope!=that.Bonds.months && !jscss.isClassName(this,'tabStripBttDisabled') ){
								that._bondsBttEnabled(false);
								that._ajax_Preloader_Display('BondContent', 'Preloader:bonds', true);
								var newBondScope = btt_IdSplit[1];
								var ul = this.parentNode;
								var li = ul.firstChild;
								while(li){
									if(li.tagName=='LI'){
										li==this ? jscss.add(li,'tabStripBttSelected') : jscss.remove(li,'tabStripBttSelected');
									}// if(li.tagName=='LI')
									li = li.nextSibling;
								}// while(li)
								that.Options.Calculate();
								document.getElementById('bonds').value = newBondScope;
								that.Bonds.months = $('bonds').value;
								that.Bonds.PromotionInfoDisplay();
								that.Bonds.Calculate(that.Options.total_bond.netto, that.Options.total_setup.netto);
								that.DisplayPrices();
								// wysłać AJAXEM nowy bond
								that.SendNewBond();
							}// if(newBondScope!=that.Bonds.months)
						}// if(btt_IdSplit)
					}// li.onclick = function
					/**
					*/
				}// if(btt_IdSplit)
				//
			}// if(li.tagName=='LI')
			li = li.nextSibling;
		}// while(li)
		
	}, // BondSummary_Init: function()
	
	
	/** funkcja odpalana tylko raz po załadowaniu danych
	*/
	DisplayStart: function(){
		
		this.Options.Calculate();
		this.Bonds.PromotionInfoDisplay();
		this.Bonds.Calculate(this.Options.total_bond.netto, this.Options.total_setup.netto);
		this.DisplayPrices();
		
		if(this.UserCoreOptionBox) this.UserCoreOptionBox.removeClassName('ucoBox_Disabled');
		
		SRV_CONFIG_USER_PRICE=='netto' ? $('disclaimer:netto').show() : $('disclaimer:netto').hide();
		SRV_CONFIG_USER_PRICE=='brutto' ? $('disclaimer:brutto').show() : $('disclaimer:brutto').hide();
		
		for(var i=0; i<this.sections.length; i++) this._ajax_Preloader_Display('ConfigItems:' + this.sections[i], 'Preloader:' + this.sections[i], false);
		this._ajax_Preloader_Display('BondContent', 'Preloader:bonds', false);
		this._bondsBttEnabled(true);
		
	}, // DisplayStart: function
	
	
	/** pokazuje wszystkie ceny w tabelce na dole
	*/
	DisplayPrices: function(){
		
		
		var price_table = $('BondSummary:setup');
		var spans = price_table.getElementsByTagName('SPAN');
		spans[0].innerHTML = this.Bonds.Selected().total_setup.strPrice(SRV_CONFIG_USER_PRICE);
		spans[1].innerHTML = SRV_CONFIG_CURRENCY;
		
		var price_table = $('BondSummary:bond');
		var spans = price_table.getElementsByTagName('SPAN');
		spans[0].innerHTML = this.Bonds.Selected().bond.strPrice(SRV_CONFIG_USER_PRICE);
		spans[1].innerHTML = SRV_CONFIG_CURRENCY;
		
		var price_table = $('BondSummary:options');
		var spans = price_table.getElementsByTagName('SPAN');
		spans[0].innerHTML = this.Options.total_bond.strPrice(SRV_CONFIG_USER_PRICE);
		spans[1].innerHTML = SRV_CONFIG_CURRENCY;
		
		var price_table = $('BondSummary:total');
		var spans = price_table.getElementsByTagName('SPAN');
		spans[0].innerHTML = this.Bonds.Selected().total.strPrice(SRV_CONFIG_USER_PRICE);
		spans[1].innerHTML = SRV_CONFIG_CURRENCY;
		
	},// DisplayPrices: function()
	
	
	
	/** pokazuje lub ukrywa preloader w miejscu opcji serwera
		@arguments
	*/
	_ajax_Preloader_Display: function(table, preloader, switchonoff){
		
		var _table = $(table);
		var _preloader = $(preloader);
		
		//(typeof table == 'string') ? (_table=$(table)) : (table);
		//(typeof preloader == 'string') ? (_preloader=$(preloader)) : (preloader);
		
		
		if(_table && _preloader){
			if(switchonoff){
				_preloader.style.height = (_table.offsetHeight-0) + 'px';
				_table.style.display = 'none';
				_preloader.style.display = 'block';
			}else{
				_table.style.display = 'block';
				_preloader.style.display = 'none';
				//_table.show();
				//_preloader.hide();
			}
		}// if(_table && _preloader)
				
	},// _ajax_Preloader_Display: function
	
	
	/** ustawia bttTabStrip na enabled | disabled
	*/
	_bondsBttEnabled: function(isEnabled){
		
		var ul = $('BondButtons');
		var li = ul.firstChild;
		while(li){
			if(li.tagName=='LI'){
				isEnabled ? jscss.remove(li,'tabStripBttDisabled') : jscss.add(li,'tabStripBttDisabled');
			}// if(li.tagName=='LI')
			li = li.nextSibling;
		}// while(li)
		
	}// _bondsBttDisabled: function
	
	
}// cConfigurator.prototype










function cPriceScheme(netto){
	
	// zamiana notacji polskiej na ISO
	var _netto = netto.toString();
	_netto = _netto.replace(/ /g,'');
	_netto = _netto.replace(/,/g,'.');
	_netto = Number(_netto);
	if(isNaN(_netto)) _netto=0;
	
	this.netto_default = _netto;
	this.netto = this.netto_default;
	this.brutto = this.netto + this.netto * SRV_CONFIG_VAT_RATE;
}
cPriceScheme.prototype = {
	
	Clear: function(){
		this.netto = this.netto_default;
		this.brutto = this.netto + this.netto * SRV_CONFIG_VAT_RATE;
	},
	
	SetNew: function(netto){
		// zamiana notacji polskiej na ISO: 1 234,56 ---> 1234.56
		var _netto = netto.toString();
		_netto = _netto.replace(/ /g,'');
		_netto = _netto.replace(/,/g,'.');
		_netto = Number(_netto);
		if(isNaN(_netto)) _netto=0;
	
		this.netto_default = _netto;
		this.netto = this.netto_default;
		this.brutto = this.netto + this.netto * SRV_CONFIG_VAT_RATE;
	},
	
	/** zamienia wartość liczbową na notację polską
	*/
	strPrice: function(pricetype){
		var _price;
		switch(pricetype){
			case 'brutto':
					_price = (Math.floor((this.netto + this.netto * SRV_CONFIG_VAT_RATE) * 10000000) / 10000000).toString();
				break;
			default:
				_price = (Math.floor(this.netto * 10000000) / 10000000).toString();
		}// switch(pricetype)
		//
		var a = _price.split('.');
		if(a[0].length>3) a[0] = a[0].substr(0, a[0].length-3) + ' ' + a[0].substr(a[0].length-3);
		
		if(a.length==1)a[1]='0';
		switch(a[1].length){
			case 1:
					a[1]+='0'
				break;
			case 2:
					
				break;
			default:
				if(Number(a[1].substr(2,1))>=5){
					a[1] = a[1].substr(0,1) + (Number(a[1].substr(1,1))+1).toString();
				}else{
					a[1] = a[1].substr(0,2);
				}
				
		}//switch(a[1].length)
		//
		_price = a[0] + ',' + a[1];
		//
		return _price;
	}// strPrice: function
	
}

function cOptionItem(category, key, bond, setup, marked){
	this.category = category;
	this.key = key;
	this.bond = new cPriceScheme(bond);
	this.setup = new cPriceScheme(setup);
	this.marked = marked;
	this.name = '';
	this.li = null;
	this.childs = Array();
	this.visible = true;
	this.getObjectItemOptionHTML();
}
cOptionItem.prototype = {
	
	/**
		* pobiera obiekt LI i przypisuje go do zmiennej this.li
	*/
	getObjectItemOptionHTML: function(){
		
		this.li = document.getElementById('ItemOption:' + this.category + ':' + this.key);
		
		if(this.marked){
			jscss.add(this.li, 'ItemOptionSelected');
		}else{
			jscss.remove(this.li, 'ItemOptionSelected');
		}
		
		this.PriceDisplay();
		
	},// getObjectItemOptionHTML: function()
	
	
	
	/** pokazuje cenę w LI z uwzględnieniem netto|brutto
	*/
	PriceDisplay: function(){
		
		var optionName = '';
		var div = this.li.firstChild;
		while(div){
			if(div.tagName=='DIV'){
				switch(div.className){
					case 'ioName':
							optionName = div.innerHTML;
						break;
					case 'ioPrice':
							var spans = div.getElementsByTagName('SPAN');
							spans[0].innerHTML = this.bond.strPrice(SRV_CONFIG_USER_PRICE);
							spans[1].innerHTML = this.setup.strPrice(SRV_CONFIG_USER_PRICE);
							spans[2].innerHTML = SRV_CONFIG_CURRENCY;
							div.style.display = this.bond.netto==0 ? 'none' : 'block';
						break;
				}// switch(div.className)
				
			}// if(div.tagName=='DIV')
			div = div.nextSibling;
		}// while(div)
		
		// jeżeli pozycja jest wybrana, wpisać nazwę i cenę do ItemOptionSelected
		if(this.marked){
			// wpisać wybraną pozycję do div.ItemOptionSelected
			var ItemRow = document.getElementById('ItemRow:' + this.category);
			var divItOptSelected = ItemRow.firstChild;
			while(divItOptSelected){
				if(divItOptSelected.tagName=='DIV' && jscss.isClassName(divItOptSelected, 'ItemOptionSelected')){
					var divs = divItOptSelected.getElementsByTagName('DIV');
					divs[0].innerHTML = optionName;
					var spans = divs[1].getElementsByTagName('SPAN');
					spans[0].innerHTML = this.bond.strPrice(SRV_CONFIG_USER_PRICE);
					spans[1].innerHTML = SRV_CONFIG_CURRENCY;
					divs[1].style.display = this.bond.netto==0 ? 'none' : 'block';
					break;
				}
				divItOptSelected = divItOptSelected.nextSibling;
			}// while(divItOptSelected)
		}// if(this.marked)
		
	}// PriceDisplay: function
	
}// cOptionItem.prototype



/**
*/
function cOptions(){
	this.Init();
}
cOptions.prototype = {
	
	Items: new Array(),
	total_bond: {},
	total_setup: {},
	
	/**
	*/
	Init: function(){
		this.total_bond = new cPriceScheme(0);
		this.total_setup = new cPriceScheme(0);
	},
	
	/**
	*/
	Add: function(category, key, bond, setup, marked){
		this.Items.push(new cOptionItem(category, key, bond, setup, marked));
	}, // Add: function
	
	/** zwraca item na podstawie klucza sfo_pkey
	*/
	getByKey: function(search_key){
		var returnItem = null;
		for(var i=0; i<this.Items.length; i++){
			if(this.Items[i].key==search_key){
				returnItem = this.Items[i];
				break;
			}
		}
		return returnItem;
	},// getByKey: function
	
	/** pobiera wszystkie obiekty Items z wybranej kategorii
	*/
	getFromCategory: function(category){
		var _items = new Array();
		for(var i=0; i<this.Items.length; i++){
			if(this.Items[i].category == category){
				_items.push(this.Items[i]);
			}
		}
		return _items;
	},// getFromCategory: function
	
	
	/** przelicza ponownie sumę cen wybranych opcji
	*/
	Calculate: function(){
		this.total_bond.Clear();
		this.total_setup.Clear();
		for(var i=0; i<this.Items.length; i++){
			if(this.Items[i].marked && this.Items[i].visible){
				this.total_bond.netto += this.Items[i].bond.netto;
				this.total_bond.brutto += this.Items[i].bond.brutto;
				this.total_setup.netto += this.Items[i].setup.netto;
				this.total_setup.brutto += this.Items[i].setup.brutto;
			}
		}
	},// Calculate: function
	
	
	/** pobiera nazwę z node LI
	*/
	getNameFromHTML: function(category, key){
		var optionName = '';
		var li = document.getElementById('ItemOption:' + category + ':' + key);
		var div = li.firstChild;
		while(div){
			if(div.tagName=='DIV' && div.className=='ioName'){
				optionName = div.innerHTML;
				break;
			}
			div = div.nextSibling;
		}
		return optionName;
	}, // getNameFromHTML: function(category, key)
	
	/**
	*/
	ResetCategory: function(category){
		var _items = this.getFromCategory(category);
		for(var i in _items){
			//alert(i + '. ' + this.getNameFromHTML(category, _items[i].key));
		}
	}
	
	
}// cOptions.prototype



/**
*/
function cServerBond(months, bond, setup, promotion_months, promotion_price){
	this.months = months;
	this.bond = new cPriceScheme(bond);
	this.setup = new cPriceScheme(setup);
	this.promotion_months = promotion_months;
	this.promotion_price = new cPriceScheme(promotion_price);
	this.calculate_total_base_bond();
}
cServerBond.prototype = {
	
	total_base_bond: {},	///
	total_bond: {},				///
	total_setup: {},			///
	total: {},						/// suma abonamentu i usług
	
	
	/** oblicza abonament za wybrany okres z uwzględnieniem promocji
	*/
	calculate_total_base_bond: function(){
		var total_base_bond = this.months * this.bond.netto - this.promotion_months * this.bond.netto + this.promotion_months * this.promotion_price.netto;
		this.total_base_bond = new cPriceScheme(total_base_bond);
		this.total_bond = new cPriceScheme(total_base_bond);
		
		this.total_setup = new cPriceScheme(this.setup.netto);
		
		this.total = new cPriceScheme(this.setup.netto + this.total_bond.netto);
		
	}, // calculate_total_base_bond: function
	
	
	/** oblicza cenę za wybrany okres: suma abonamentu serwera i opcji
	*/
	Calculate: function(options_bond, options_setup){
		this.total_bond.SetNew( options_bond*this.months + this.total_base_bond.netto );
		this.total_setup.SetNew( options_setup + this.setup.netto );
		this.total.SetNew( this.total_bond.netto + this.total_setup.netto );
	}// Calculate: function
	
}// cServerBond.prototype


/**
*/
function cServerBonds(SelectedIndex){
	this.Init(SelectedIndex);
}
cServerBonds.prototype = {
	
	bonds: new Array(),
	SelectedIndex: 0,
	months: 0,
	
	
	/**
	*/
	Init: function(months){
		this.months = months;
	}, // Init: function
	
	
	/**
	*/
	Add: function(months, bond, setup, promotion_months, promotion_price){
		this.bonds.push( new cServerBond(months, bond, setup, promotion_months, promotion_price) );
	},// Add: function
	
	
	/**
	*/
	Calculate: function(options_bond, options_setup){
		
		for(var i=0; i<this.bonds.length; i++){
			this.bonds[i].Calculate(options_bond, options_setup);
		}
		
	}, // Calculate: function
	
	
	/**
	*/
	Selected: function(){
		var _bond = {};
		for(var i=0; i<this.bonds.length; i++){
			if(this.bonds[i].months==this.months){
				_bond = this.bonds[i];
				break;
			}
		}
		return _bond;
	}, // Selected: function()
	
	
	/**
	*/
	PromotionInfoDisplay: function(){
		// pokazać promocje
		var PromotionInfo = document.getElementById('PromotionInfo'); // tagName=UL
		if(PromotionInfo){
			var liPromotionInfo = PromotionInfo.getElementsByTagName('LI');
			for(var ili in liPromotionInfo){
				if(liPromotionInfo[ili].tagName=="LI"){
					liPromotionInfo[ili].style.display = 'none';
				}
			}
		}
		if(this.Selected().promotion_months>0){
			var liPromotionInfo = document.getElementById('PromotionInfo:' + SRV_CONFIG_USER_PRICE + ':' + this.Selected().months);
			if(liPromotionInfo) liPromotionInfo.style.display = 'block';
		}// if(this.Bonds.Selected().promotion_months>0)
	}// PromotionInfoDisplay: function
	
	
}// cServerBonds.prototype




/**
*/
function in_array(arr, val){
	for(var i in arr){
		if(arr[i]==val) return true;
	}
	return false;
}

