function ParamHandler(){
	this.vat = 1;
	this.setVat = function(vat){
		this.vat = vat;
	}

	this.setPrice = function(){
		//if(!detailLoaded)return false;
		var price = this.calculatePrice();
		document.getElementById('total_price').innerHTML = format_it(price);
		document.getElementById('total_price_vat').innerHTML = format_it(price*this.vat);			
	}

	// =========== do not fuck wit this !!!  ====================
	this.data = new Array();
	this.items = new Array();
	this.basePrice = 0;
	this.isValid = function(){
		return false;
	}


	this.add = function(id_skuvar,id_var,parent,typeSelect,mustFill,price){
		if(!this.data[parent])this.data[parent] = new Array;	
		this.data[parent][this.data[parent].length]=id_skuvar;		
		this.items[id_skuvar] = new ParamHandlerItem(id_skuvar,id_var,parent,typeSelect,mustFill,price); 
	}
	
	this.calculatePrice = function(parent){
		var price=0;
		
		if(!parent){
			parent=0;
			price=this.basePrice;
		}
		
		
		if(this.data[parent] && this.data[parent].length){
			var goDeep = false;
			var item;
			try{
				item = this.items[parent];
			}catch(e){
				alert(e+parent)
			}
			if(item)el = document.getElementById('opt_'+item.id_skuvar+'_'+item.id_var+'');
			
			if(!item || !el){
			 	goDeep=true;			
			}else if(el.tagName=='INPUT' && (el.type=='checkbox' || el.type=='radio')){
				if(el.checked)goDeep=true
			}
			
			if(goDeep){ 			
				for(var i=0;i<this.data[parent].length;i++){
					price+=this.calculatePrice(this.data[parent][i]);	
				}		
			}                                                     
		}else{
			item = this.items[parent];
			if(item){
				value=false;
				el = document.getElementById('opt_'+item.id_skuvar+'_'+item.id_var+'');
				if(el){		
					if(el.tagName=='INPUT' && (el.type=='checkbox' || el.type=='radio')){
					   	if(el.checked)price+=item.price*1;
					}else if(el.tagName=='OPTION'){
						if(el.parentNode.value == item.id_skuvar)price+=item.price*1;
					}
				}
			}
	
		}
		return price;		
	}
	
	this.onSubmit = function(){
		//if(!this.data.length)return true;
		if(!this.isValid()){
			alert('prosim zvolte nekterou z moznosti');
			return false;
		}
		addItemDetail(this.idPro,this.getValue());
		//alert('val:'+this.getValue());
	}
	this.setBasePrice = function(price){
		this.basePrice = price*1;	
	}
	this.setProdId = function(id){
		this.idPro = id;	
	}
	
	this.isValid = function(parent){
		if(!this.data.length)return true;	
		if(!parent)parent=0;
		var item = this.items[parent];
		if(item){
			//= check 'must fill at least one' item
			if(item.mustFill){
				var els = document.getElementsByClassName('opt_'+item.id_skuvar+'_'+item.id_var+'');
				if(els.length){
					var checked = false;
					for(var i=0;i<els.length;i++){
						if(els[i].checked){
						   checked = true;
						   break;				
						}
					}
					if(!checked)return false;				
				}

			}	
		}	                                                              

		if(!this.data[parent] || this.data[parent].length==0)return true;
		
		//check children
		for(var i=0;i<this.data[parent].length;i++){
			if(!this.isValid(this.data[parent][i]))return false;	
		}		
		return true;			
	}
	
	this.getValue = function(parent){	
		if(!this.data.length)return '';
		if(!parent)parent=0;
		
		
		ret='';
		if(this.data[parent] && this.data[parent].length){
			var goDeep = false;
			var item = this.items[parent];
			if(item)el = document.getElementById('opt_'+item.id_skuvar+'_'+item.id_var+'');
			
			if(!item || !el){
			 	goDeep=true;			
			}else if(el.tagName=='INPUT' && (el.type=='checkbox' || el.type=='radio')){
				if(el.checked)goDeep=true
			}
			
			if(goDeep){ 			
				for(var i=0;i<this.data[parent].length;i++){
					ret+=this.getValue(this.data[parent][i]);	
				}		
			}
		}else{
			item = this.items[parent];
			value=false;
			el = document.getElementById('opt_'+item.id_skuvar+'_'+item.id_var+'');
			if(el){		
				if(el.tagName=='INPUT' && (el.type=='checkbox' || el.type=='radio')){
				   	if(el.checked)value=item.id_skuvar;
				}else if(el.tagName=='OPTION'){
					if(el.parentNode.value == item.id_skuvar)value=item.id_skuvar;
				}
				if(value){							
					ret+='&opt[]='+value;
				}
			}	
		}
		return ret;			
	}	
	
}

function ParamHandlerItem(id_skuvar,id_var,parent,typeSelect,mustFill,price){
	this.id_skuvar = id_skuvar;
	this.id_var = id_var;
	this.parent = parent;
	this.typeSelect = typeSelect;
	this.mustFill = mustFill;
	this.price = price;

}




