var MooTooltips=new Class({Implements:[Options],options:{container:null,hovered:null,extra:null,ToolTipClass:'ToolTips',toolTipPosition:1,showDelay:0,sticky:false,fromTop:-30,fromLeft:100,duration:100,fadeDistance:20},initialize:function(options){this.setOptions(options||null);if(!this.options.hovered&&!this.options.extra)return;if(this.options.hovered)
this.elements=$(this.options.container||document.body).getElements(this.options.hovered);if(!$defined(this.elements))this.elements=new Array();var e=new Hash(this.options.extra);e.each(function(el){if($(el.id)!=null){$(el.id).set('rel',JSON.encode(el));this.elements.include($(el.id));}},this);this.currentElement=null;},add:function(id){if(!$chk($(id))){return;}
var arg={'id':id,'ajax':'wnk/tt/'+id,'ajax_message':'Loading... please wait.','position':1,'sticky':false};$(id).set('rel',JSON.encode(arg))
this.elements.include($(id));},attach:function(){this.elements.each(function(elem,key){var t=new Hash(JSON.decode(elem.getProperty('rel')));t.include('visible',0);var tooltip=this.createContainer(t.sticky||this.options.sticky);if(t.content)
tooltip.message.set({'html':$(t.content).get('html')});else if(t.text)
tooltip.message.set({'html':t.text});else if(t.ajax){tooltip.message.set({'html':t.ajax_message||'Loading... please wait.'});new Element('div',{'class':'loading'}).injectInside(tooltip.message);}
if(!t.position)t.position=this.options.toolTipPosition;if(t.position==-1){tooltip.header.set({'class':'dockTopHeader'});tooltip.footer.set({'class':'dockTopFooter'});}
tooltip.container.store('properties',t);elem.store('tip',tooltip.container);$(document.body).adopt(tooltip.container);elem.removeProperties('title','rel');var over=this.enter.bindWithEvent(this,elem);var out=this.leave.bindWithEvent(this,elem);elem.addEvent('mouseover',over);if(t.sticky||this.options.sticky){tooltip.close.addEvent('click',this.hide.pass(tooltip.container).bind(this));}
elem.addEvent('mouseleave',out.pass(tooltip.container));},this);},enter:function(event,element){var tip=element.retrieve('tip');var elProperties=tip.retrieve('properties');if(elProperties.visible==1)return;if(elProperties.ajax&&!elProperties.loaded){new Request.HTML({url:elProperties.ajax,update:tip.getElement('.message'),onFailure:function(){elProperties.set('loaded',0);}}).get();elProperties.set('loaded',1);}
var showAfter=elProperties.target?$(elProperties.target):element;var elSize=showAfter.getCoordinates();var tipSize=tip.getCoordinates();this.fromTop=0;if(elProperties.position==-1)
this.fromTop=elSize.top-this.options.fromTop-tipSize.height;else
this.fromTop=elSize.top+this.options.fromTop+elSize.height;var top_dist=this.fromTop+(elProperties.position||this.options.toolTipPosition)*this.options.fadeDistance;tip.setStyles({'top':top_dist,'left':elSize.left+this.options.fromLeft,'z-index':'110000'});elProperties.set('leave',top_dist);this.currentElement=tip;this.timer=$clear(this.timer);this.timer=this.show.delay(this.options.showDelay,this);},leave:function(element){var elProperties=element.retrieve('properties');if((elProperties.sticky||this.options.sticky)&&elProperties.visible){return;}
this.hide(element);},hide:function(element){this.timer=$clear(this.timer);var elProperties=element.retrieve('properties');element.morph({'opacity':0,'top':elProperties.leave});elProperties.visible=0;},show:function(){this.currentElement.setStyles({'display':'block','opacity':0,'z-index':100000});this.currentElement.morph({'opacity':1,'top':this.fromTop});this.setVisible();},setVisible:function(){var elProperties=this.currentElement.retrieve('properties');elProperties.visible=1;},createContainer:function(sticky){var container=new Element('div').set({'class':this.options.ToolTipClass,'styles':{'position':'absolute','top':0,'left':0,'opacity':0,'z-index':'100000'},'morph':{duration:this.options.duration,wait:false,transition:Fx.Transitions.Sine.easeOut}});var header=new Element('div',{'class':'dockBottomHeader'});if(sticky){var closeBtn=new Element('div',{'class':'sticky_close'}).injectInside(header);}
var message=new Element('div',{'class':'message'});var footer=new Element('div',{'class':'dockBottomFooter'});container.adopt(header,message,footer);return{'container':container,'header':header,'message':message,'footer':footer,'close':closeBtn||null};}});

var JSONP = {
	parse : function (text) {
		return $try(function () {
					return eval('('+text+')');
				});
	}
};

var Lang = {
	get : function (key) {
		return ln[key];
	}
};

var Livescript = {
	newReqProd : function () {

		var prod = $('jsProduct').getSelected().get('value');
		var aantal = $('jsAantal').get('value');

		if(aantal < 0) {
			return;
		}

		var url = 'loader=normaal&id='+prod+'&aantal='+aantal;
		this.request(url);
	},
	newReqAntiv : function () {

		var prod 		= $('jsProduct').getSelected().get('value');
		var afnemer 	= $('jsAfnemer')
		if(afnemer != null) {
			afnemer = afnemer.getSelected().get('value');
		}
		var aantal 		= $('jsAantal').getSelected().get('value');
		
		if(aantal < 0) {
			return;
		}

		var licentie 	= $('jsLictype');
		if(licentie != null) {
			licentie = licentie.getSelected().get('value');
		}
		var jaren 		= $('jsJaar').getSelected().get('value');

		var url = 'loader='+$('jsTypeForm').get('value')+'&afnemer='+afnemer+'&aantal='+aantal+'&jaren='+jaren+'&lictype='+licentie+'&product='+prod;
		this.request(url);
	},
	handle : function (text) {
		var obj = JSONP.parse(text);

		if(obj == null) {
			return false;
		}

		if(obj.error == 0) {
			$('jsEx').set('html',obj.ex);
			$('jsInc').set('html',obj.inc);
			$('jsBtw').set('html',obj.btw);

			if($chk($('jsPc'))) {
				$('jsPc').set('html',obj.pc);
			}
		}
	},
	request : function (url) {
		var req = new Request({
					url:'wnk/loader',
					method:'post',
					onRequest : function () {

					},
					onSuccess : function (resText) {
						Livescript.handle(resText);
					}});

		req.setHeader('X_HANDLER','PRODUCTLOADERv1.0');

		// Versturen!
		req.send(url);
	},
	newTussenReq : function () {
		
		var req = new Request({
			url:'wnk/getprod/' + $('jsAfnemer').getSelected().get('value'),
			method:'post',
			onRequest : function () {
	
			},
			onSuccess : function (resText) {
				Switcher.tussen(resText);
			}});

		req.setHeader('X_HANDLER','PRODUCTLOADERv1.0');
		
		// Versturen!
		req.send();
		
	}
	
};

var Switcher = {
	
	validator : null,
		
	prop : "jsAantal",		
	dataZakelijk : ['5 - 10','11 - 24','25 - 49','50 - 99','100 - 249','250 - 499','500 - 999','1000 - 1999','2000 - 4999','5000 - 9999','10000 - 24999','25000 - 49999','50000 of meer'],
	dataConsument : ['1 computer','2 computers','3 computers','4 computers', '5 computers', '6 computers', '7 computers', '8 computers', '9 computers', '10 computers'],
	
	dataGdataZakelijk : ['4 - 9','10 - 24','25 - 49','50 - 99','100 - 249','250 - 499','500 - 999','1000 - 2499'],
	dataGdataML	: ['3 - 24','25 - 50'],
	
	start : function (el) {
	
		if(el.get('id') == 'jsAfnemer') {
			// Consument
			var t = el.getSelected().get('value');
		
			switch(t/1) {
				case 1:
					var data = this.dataConsument;	
				break;
				case 2:
					var data = this.dataZakelijk;
					
				break;
				
				case 3:
				case 6:
				case 7:
				case 10:
					var data = ['1 computer','3 computers'];
					this.parse('jsJaar',['1 '+Lang.get('jaar'),'2 '+Lang.get('jaar')]);
				break;
				case 9:
					var data = ['1 computer','3 computers'];
					this.parse('jsJaar',['1 '+Lang.get('jaar')]);
				break;
				case 4:
					var data = this.dataGdataZakelijk;
					this.parse('jsJaar',['1 '+Lang.get('jaar'),'2 '+Lang.get('jaar'),'3 '+Lang.get('jaar')]);
				break;
				
				default:
					var data = [1,2,3,4,5];
					this.parse('jsJaar',['1 '+Lang.get('jaar'),'2 '+Lang.get('jaar'),'3 '+Lang.get('jaar')]);
				break;
			}

			
			
			this.parse('jsAantal',data);
			
			switch (t/1) {
				case 7:
				case 10:
				case 4: 
				case 2: 
					
					if(land != 'nl') {
						var btw_nr = new Element('input',{
							'type':'text',
							'name':'btw_nr',
							'class':'fValidate[\'required\'] left',
							'id':'btw_nr_veld'
						});
						this.rule('* '+Lang.get('btw_nr')+':',btw_nr,'tt_btw_nr','extraAfnemer');
						
						Switcher.validator.register('btw_nr_veld',Switcher.validator.options['required']);
						
						var bedr = new Element('input',{
							'type':'text',
							'name':'bedrijfsnaam',
							'class':'fValidate[\'required\'] left',
							'id':'bedrijfsnaam_veld'
						});
						this.rule('* '+Lang.get('bedrijfsnaam')+':',bedr,false,'extraAfnemer');
						Switcher.validator.register('bedrijfsnaam_veld',Switcher.validator.options['required']);
						
						
						var a = new Element('a',{'class':'tt','href':'#','id':'btw_intracommunautair'}).set('html','<img src="data/img/documentinfo.png" alt="Info" />');
						$('btwding').set('html','<span class="left">'+Lang.get('btw_verlegd')+'</span>');
						a.inject($('btwding'));
						MT.addAndStart('btw_intracommunautair');
						
						$$('.bedragen tr:first-child td:first-child strong').set('html',Lang.get('prijs_per_pc')+':');
						$$('.exclusief').setStyle('display','none');
						$('jsEx').set('html','');
						$$('.inclusief td:first-child').set('html',Lang.get('prijs_totaal_voldoen')+':');
						$$('.secondbtw').setStyle('display','none');
						
					}
				break;
				default:
					
					$('extraAfnemer').empty();
					$('btwding').set('html',Lang.get('prijs_btw')+':');
					
					$$('.bedragen tr:first-child td:first-child strong').set('html',Lang.get('prijs_pc')+':');
					$$('.secondbtw td:first-child').set('html',Lang.get('prijs_totaal_inc')+':');
					
					
					if(!Browser.Engine.trident) {
						$$('.exclusief').setStyle('display','table-row');					
						$$('.secondbtw').setStyle('display','table-cell');
					}
					
				break;
			}
			
			
		} else if(el.get('id') == 'jsProduct') {
			
			var af = $('jsAfnemer').getSelected().get('value');
			
			// Gdata en consument, en product notebook sec
			if(af == 3 || af == 6 || af == 9 || af == 12) {
				var product = el.getSelected().get('value');
				
				if(product == 10 || product == 63 || product == 42) {
					this.parse(this.prop,['1 computer']);
				} else {
					if($(this.prop).getElements('option').length == 1) {
						this.parse(this.prop,['1 computer','3 computers']);
					}
				}
			}
			
			if(af != 4) {
				return;
			}
			
			if($chk(this.ml[el.getSelected().get('value')])) {
				this.parse(this.prop,this.dataGdataML);
			} else  {
				this.parse(this.prop,this.dataGdataZakelijk);
			}
		}
	},
	
	parse : function (el,data) {
		$(el).empty();
		$each(data,function (item, index) {
			var x = new Element('option',{
				'value' : index+1
			}).set('html',item);
			x.inject($(el));
		},this);
		
	},
	
	tussen : function (res) {
		var obj = JSONP.parse(res);
		if(obj == null) {
			return;
		}
		
		$('jsProduct').empty();
		this.ml = [];
		
		$each(obj.result, function (item) {
			var el = new Element('option',{'value':item.id}).set('html',item.naam);
			el.inject($('jsProduct'));
			
			if(item.ml == 1) {
				this.ml[item.id] = 1;
			}
		},this);
	},
	
	ml : [],
	
	verlenging : function (el,type) {
		
		if(el.getSelected().get('value') == 0) {
			
			var geb = new Element('input',{
				'type':'text',
				'name':'lic_geb',
				'class':'fValidate[\'required\'] left',
				'id':'lic_geb_veld'
			});
			
			
			switch(type) {
				case "gdata": 
					this.rule('* '+Lang.get('gebruiker')+':',geb,'tt_geb','extraLic');
				break;
				case "antiv": 
					this.rule('* '+Lang.get('gebruiker')+':',geb,'tt_geb','extraLic');
					
					var ww = new Element('input',{
						'type':'text',
						'name':'lic_ww',
						'class':'fValidate[\'required\'] left',
						'id':'lic_ww_veld'
					});
					
					
					this.rule('* '+Lang.get('wachtwoord')+':',ww,'tt_ww','extraLic');
					
					Switcher.validator.register('lic_ww_veld',Switcher.validator.options['required']);
				break;	
			}
			Switcher.validator.register('lic_geb_veld',Switcher.validator.options['required']);
			
		} else {
			$('extraLic').empty();
		}
	
	},
	
	rule : function (tekst,el,tt,input) {
		var p = new Element('p').setStyle('height','23px');
		var label = new Element('label',{'class':'floated'}).set('html', tekst);
		label.inject(p);
		
		el.inject(p);
		
		// Tooltip?
		if(tt != false) {
			var a = new Element('a',{'class':'tt','href':'#','id':tt}).set('html','<img src="data/img/documentinfo.png" alt="Info" />');
			a.inject(p);
			p.inject($(input));
			MT.addAndStart(tt);
		} else {
			p.inject($(input));
		}
		
		
	}
		
};

var MT = {
	
	init : null,

	start : function () {
	
		this.init =	new MooTooltips({
			hovered:'.tipper',		// the element that when hovered shows the tip
			extra:{},
			ToolTipClass:'ToolTips',	// tooltip display class
			toolTipPosition:1, // -1 top; 1: bottom - set this as a default position value if none is set on the element
			showDelay: 100,
			sticky:false,		// remove tooltip if closed
			fromTop: 0,		// distance from mouse or object
			fromLeft: 5,	// distance from left
			duration: 300,		// fade effect transition duration
			fadeDistance: 20    // the distance the tooltip starts the morph
		});
	
	},
	add : function (id) {
		this.init.add(id);
	},
	finish : function () {
		this.init.attach();
	},
	addAndStart : function (id) {
		
		var t = new MooTooltips({
			hovered:'.tipper',		// the element that when hovered shows the tip
			extra:{},
			ToolTipClass:'ToolTips',	// tooltip display class
			toolTipPosition:1, // -1 top; 1: bottom - set this as a default position value if none is set on the element
			showDelay: 100,
			sticky:false,		// remove tooltip if closed
			fromTop: 0,		// distance from mouse or object
			fromLeft: 5,	// distance from left
			duration: 300,		// fade effect transition duration
			fadeDistance: 20    // the distance the tooltip starts the morph
		});
		
		t.add(id);
		t.attach();
	}
};

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

	// Form validator
	if($chk($('formAjax'))) {
		
		var tl;
		if(language == 'Engels') {
			tl = 'en';
		} else {
			tl = 'nl';
		}
		
		Switcher.validator = new fValidator('upperdiv','formAjax', {language: tl});

		if($chk($('jsTypeForm'))) {
		var type = $('jsTypeForm').get('value');

			switch(type) {
			
			case "ps":
				$('jsProduct').addEvent('change', function () {
					Switcher.start(this);
					Livescript.newReqAntiv();
				});
				
				$$('#jsAantal,#jsJaar').each(function (el) {
					el.addEvent('change', function (e) {
						Livescript.newReqAntiv();
					});
				});
			break;
			
				case "gdata": 
					$('jsAfnemer').addEvent('change',function () {
						Switcher.start(this);
						Livescript.newTussenReq();
						(function () { Livescript.newReqAntiv(); }).delay(500);
					});
					$('jsProduct').addEvent('change', function () {
						Switcher.start(this);
						Livescript.newReqAntiv();
					});
					
					$$('#jsAantal,#jsJaar').each(function (el) {
						el.addEvent('change', function (e) {
							Livescript.newReqAntiv();
						});
					});
					$('jsLictype').addEvent('change', function (e) {
						Switcher.verlenging(this,type);
						Livescript.newReqAntiv();
					});
				break;
				case "antiv":
					$$('#jsProduct,#jsAantal,#jsAfnemer,#jsJaar').each(function (el) {
						el.addEvent('change', function (e) {
							Switcher.start(this);
							Livescript.newReqAntiv();
						});
					});
					$('jsLictype').addEvent('change', function (e) {
						Switcher.verlenging(this,type);
						Livescript.newReqAntiv();
					});
				break;
				

				case "normaal":

					$$('#jsAantal,#jsProduct').each(function (el) {
						el.addEvent('change', function () {
							Livescript.newReqProd();
						});
					});
				break;
			}
		}
	}

	$$('p a.tt').each(function (el) {
		el.getParent().setStyle('height','24px');
	});
	
	// MootoolsToolTip
	MT.start();
	MT.add('lic_type_tt');
	MT.add('product_gdata');
	MT.add('product_antiv');
	MT.finish();
});

