function ParamHandler(){

	this.setPrice = function(){
		var price = this.calculatePrice(0,true); //==getting price with VAT !
		//document.getElementById('total_price').innerHTML = format_it(price);
		if(this.isSet)
			calculateSetPrice();
		//document.getElementById('total_price_vat').innerHTML = format_it(price*1.20);
		var DPH = 1.20;
		var priceVat =  this.basePriceVat + price;
		//var priceVat =  (this.basePrice+price);
		var priceOld =  Math.round(this.oldPrice*DPH) + price;
		
		this.totalPriceVat = priceVat;
				
		document.getElementById('total_price_vat').innerHTML = format_it(priceVat);
		document.getElementById('total_old_vat').innerHTML = format_it(priceOld);
		try{
			document.getElementById('total_price_vat2').innerHTML = format_it(priceVat);
			document.getElementById('total_old_vat2').innerHTML = format_it(priceOld);
		}catch(e){
		
		}	
	}
  this.setIsSet = function(jeSet){
		this.isSet = jeSet;
	
	}


	// =========== do not fuck wit this !!!  ====================
	this.data = new Array();
	this.items = new Array();
	this.basePrice = 0;
	this.basePriceVat = 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,vat){
		var price=0;
		var priceVat=0;
		if(!vat)vat = false;
		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],vat);	
					priceVat+=this.calculatePrice(this.data[parent][i],vat);
				}		
			}                                                     
		}else{
			item = this.items[parent];
			if(!item)return false;//set without variants
			value=false;
			
			var validValue = false;
			
			el = document.getElementById('opt_'+item.id_skuvar+'_'+item.id_var+'');
			if(!el)return false;//set without variants		
			if(el.tagName=='INPUT' && (el.type=='checkbox' || el.type=='radio')){
			   	if(el.checked)validValue = true;
			}else if(el.tagName=='OPTION'){
				if(el.parentNode.value == item.id_skuvar)validValue = true;
			}
			
			if(validValue){
				price+=item.price*1;
				priceVat+=Math.round(item.price*1.2);
			}
	
		}
		if(vat)
			return priceVat;
		else		
			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.basePriceVat = price*1.20	
	}
	this.setOldPrice = function(price){
		this.oldPrice = 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 && el.tagName=='INPUT' && (el.type=='checkbox' || el.type=='radio')){
			   	if(el.checked)value=item.id_skuvar;
			}else if(el && 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;

}



