//alert(MooTools.version);

// Copyright 2008; this code may not be used without Read & View B.V.'s authorization.
//
// Note: depends on Mootools v1.11

var ViewBSSet = new Class({
    name: 'ViewBSSet'
});

 

ViewBSSet.implement({

	submit: function () {	
		
		// delete the elements from a previous submit
		
		var old = $$('input[id$=offerids]');
		old.each(
			function(item, index) {
				if (item.type == 'hidden') {
					$('formbsset').removeChild(item);
				}
			}
		);
		
		// create new elements to post
			
		var bsset = $('bsset');
		var length = bsset.options.length;
		
		for (var i = 0; i < length; i++) {
			var input = document.createElement('input');
			input.setAttribute('type', 'hidden');
			input.setAttribute('name', 'offerids[]');
			input.setAttribute('id', 'offerids[' + i + ']');	
			input.value = bsset.options[i].value;
			$('formbsset').insertBefore(input, $('formbsset').firstChild);
		}		
		
   		$('method').value = 'save';
   		$('formbsset').submit();
	return false;
    },



	toLeft: function(el) {
   		el.addEvent('click', function(e) {
   		    var left  = $('offers');
			var right = $('bsset');
			
			if (right.selectedIndex >= 0) {
				var theId = right.id.replace('bsset[', '');
   		    	var theId = theId.substring(0, theId.length-1);	    	
   		    	var option = document.createElement('option');
   		    	
   		    	option.text = right.options[right.selectedIndex].text;
  				option.value = right.options[right.selectedIndex].value; //theId;
  					
  				try {
    				left.add(option, left.options[left.options.length] ); // standards compliant; doesn't work in IE
  				} catch(ex) {
    				left.add(option, left.options.length); // IE only
    			}
				
				right.removeChild(right.options[right.selectedIndex]);
			}
			
			viewbsset.calculateAverageMargin();
			
   		    return false;
   		});
    },



	toRight: function(el) {
   		el.addEvent('click', function(e) {
   		    var left  = $('offers');
			var right = $('bsset');
   		    
   		    if (left.selectedIndex >= 0) {
   		    	var theId = left.id.replace('offers[', '');
   		    	var theId = theId.substring(0, theId.length-1);	    	
   		    	var option = document.createElement('option');
   		    	
   		    	option.text = left.options[left.selectedIndex].text;
  				option.value = left.options[left.selectedIndex].value; //theId;
  					
  				try {
    				right.add(option, right.options[right.options.length] ); // standards compliant; doesn't work in IE
  				} catch(ex) {
    				right.add(option, right.options.length); // IE only
    			}
    			
    			left.removeChild(left.options[left.selectedIndex]);	
   		    }
   		    
   		    viewbsset.calculateAverageMargin();
   		    
   		    return false;
   		});
    },
    
    

    
	packetNumber: function(el) {
   		
		el.addEvent('keyup', function(e) {
			viewbsset.calculateAveragePrice();
			
			this.style.background = "red";
			this.style.color = "white";
   		});
   		
   		el.addEvent('blur', function(e) {
			this.value = isNaN(parseInt(this.value)) ? 0 : parseInt(this.value);
			
			this.style.background = "white";
			this.style.color = "black";
   		});
   		
   		el.addEvent('change', function(e) {
			viewbsset.updateMargin();
   		});
    },
    
    
    
    packetPrice: function(el) {
   		
		el.addEvent('keyup', function(e) {
			viewbsset.calculateAveragePrice(el);
			
			this.style.background = "red";
			this.style.color = "white";
   		});
   		
   		el.addEvent('blur', function(e) {
			this.value = isNaN(parseFloat(this.value.replace(",", "."))) ? 0 : parseFloat(this.value.replace(",", ".")).toFixed(2).replace(".", ",");

			this.style.background = "white";
			this.style.color = "black";
   		});
   		
   		el.addEvent('change', function(e) {
			viewbsset.updateMargin();
   		});
    },
    
    
    
	calculateAverageMargin: function() {

		var averageMargin = 0.00;
		var sumMargin = 0.00;
		
		selection = $('bsset');
		
		for (i = 0;  i < selection.length; i++ ) {
			offerText = selection.options.item(i).innerHTML;
			tmpMargin = offerText.match(/M: € [-]{0,1}[0-9]{1,},[0-9]{2}/); // match example: "M: € 12,34)"
			
			tmpMargin += ""; // cast to string otherwise .replace() doesn't work.
			tmpMargin = parseFloat(tmpMargin.replace("M: € ", "").replace(",", "."));
			sumMargin += tmpMargin;
		}
		
		averageMargin = selection.length > 0 ? sumMargin / selection.length : 0;
		
		$('averageMargin').innerHTML = "&euro; " + averageMargin.toFixed(2).replace(".00", ",-").replace(".", ",");
    },

    
    
    calculateAveragePrice: function() {
		number = parseInt($('packetNumber').value);
		number = isNaN(number) ? 0 : number;
		
		if (number > 0) {
			price = parseFloat($('packetPrice').value.replace(",", "."));
			price = isNaN(price) ? 0 : price;
			
			average  = parseFloat(price / number);
		} else {
			average = 0.00;
		}
		
		$('averagePrice').innerHTML = "&euro; " + average.toFixed(2).replace(".00", ",-").replace(".", ",");
		
		return average;
    },
    
    
    
    updateMargin: function() {
		
		var margin = viewbsset.margins;

		var selections = new Array();
		selections[0] = "bsset";
		selections[1] = "offers";

		var averageOfferPrice = parseFloat(viewbsset.calculateAveragePrice().toFixed(2));		
		
		for (s = 0; s < selections.length; s++) {
			
			selection = $(selections[s]);
			
			for (i = 0;  i < selection.length; i++ ) {
				offerText = selection.options.item(i).innerHTML;
				offerId = selection.options.item(i).value;
				offerMargin = parseFloat((parseFloat(margin[offerId]) + averageOfferPrice)).toFixed(2);
				
				old_offerMargin = offerText.match(/M: € [-]{0,1}[0-9]{1,},[0-9]{2}/); // match example: "M: € 12,34)"
				offerText = offerText.replace(old_offerMargin, ("M: € " + offerMargin).replace(".", ","));
				selection.options.item(i).label = offerText;
				selection.options.item(i).innerHTML = offerText;
			}
		}
		
		viewbsset.calculateAverageMargin();
    }
    
});



var viewbsset = new ViewBSSet();


window.addEvent('domready', function() {

	if ($('offer_link_left')) {
		viewbsset.toLeft($('offer_link_left'));
	}
	
	if ($('offer_link_right')) {
		viewbsset.toRight($('offer_link_right'));
	}
	
	if ($('packetNumber')) {
		viewbsset.packetNumber($('packetNumber'));
	}
	
	if ($('packetPrice')) {
		viewbsset.packetPrice($('packetPrice'));
	}
	
});


