



var contract_make ={
	
	defaults :{
		ticket_types : [],
		discounts: [],
		dd_types: '#contractform-ticket_type',
		dd_discounts: '#contractform-id_discount',
	},	
	init: function(o){
		contract_make.defaults = $.extend(contract_make.defaults, o);
		contract_make.update();
		contract_make.getDDTypes().change(contract_make.update);
		contract_make.getDDDiscounts().change(contract_make.update);
	},
	getDDTypes: function(){
		return $(contract_make.defaults.dd_types);
	},
	
	getDDDiscounts: function(){
		return $(contract_make.defaults.dd_discounts);
	},
	findType: function(id){
		var type ;
		var i;
		type = null;
		for ( i = 0;  i<contract_make.defaults.ticket_types.length; i++ ){
			if ( contract_make.defaults.ticket_types[i]['id_ticket_type'] == id ){
				type = contract_make.defaults.ticket_types[i];
				break;
			}
		}
		return type;
	},
	findDiscount: function(id){
		var type ;
		var i;
		type = null;
		if ( id  && !isNaN(id) ){
			for ( i = 0;  i < contract_make.defaults.discounts.length; i++ ){
				if ( contract_make.defaults.discounts[i]['id_discount'] == id ){
					type = contract_make.defaults.discounts[i];
					break;
				}
			}
		}
		return type;
	},
	update: function(){
		var type = contract_make.findType( +contract_make.getDDTypes().val());
		var discount = contract_make.findDiscount( +contract_make.getDDDiscounts().val() );
		var total = contract_make.calcPriceBrutto(type,discount);
		$('.ticket-price').html(type.price_brutto);
		$('.ticket-total').html(total);
	},
	calcPriceBrutto: function(type,discount){
		var result;
		result = type.price_brutto;
		if ( discount!= null ){
			var d = result* discount.value /100;
			d =  Math.floor( d );
			result = result - d;
		}
		return result;
	}
	
}