var url_helper = {
	
	qs:		false,
	
	get:	function(key) {
		var qs = this.qs;
		if(qs === false) {
			qs = {};
			q = window.location+'';
			q = q.split('?');
			if(q[1]) {
				$.each(q[1].split('&'), function(i, value) {
					v = value.split('=');
					qs[v[0]] = v[1];
				});
				this.qs = qs;
			}
		}
		
		if(qs[key]) return qs[key];
		return false;
	}
	
};

Number.prototype.formatMoney = function(c, d, t){
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

jQuery.fn.hover = function(attr) {
	var el = $(this);
	var old_attr = {};
	$.each(attr, function(i) {
		old_attr[i] = el.css(i);
	});
	
	el.mouseenter(function() {
		$(this).css(attr);
	}).mouseleave(function() {
		$(this).css(old_attr);
	});
	
}

function switch_tab(eq) {
	$('#meniu_c a').removeClass('active');
	$('div#meniu_c a:eq(' + eq + ')').addClass('active');
	$('#tabs_content > p.titlu1').slideUp('fast');
	$('#tabs_content > div.tab_content').slideUp('fast');
	$('#tabs_content > p.titlu1:eq(' + eq + ')').slideDown('fast');
	$('#tabs_content > div.tab_content:eq(' + eq + ')').slideDown('fast');
}

function autofill(model, fields) {
	$(model).keyup(function() {
		var val = $(this).val();
		$.each(fields, function(i, field) {
			field = $(field);
			if(!field.data('autofill_empty')) field.data('autofill_empty', (field.val().length < 1 ? 'yes' : 'no'));
			
			if(field.data('autofill_empty') == 'yes') field.val(val);
		});
	}); 
}
function overmenen2(nu,id) {
	//autofill('#bez_zipp', ['#post_zipp', '#fact_zipp', '#direct_zipp', '#contact_zipp']);
	//autofill('#bez_number', ['#post_number', '#fact_number', '#direct_number', '#contact_number']);
	//autofill('#bez_number_add', ['#post_number_add', '#fact_number_add', '#direct_number_add', '#contact_number_add']);
	//autofill('#bez_address', ['#post_address', '#fact_address', '#direct_address', '#contact_address']);
	//autofill('#bez_city', ['#post_city', '#fact_city', '#direct_city', '#contact_city']);
	
	if(nu!="" && id!="") {
		copyinput2(nu+"_zipp",id+"_zipp");
		copyinput2(nu+"_number",id+"_number");
		copyinput2(nu+"_number_add",id+"_number_add");
		copyinput2(nu+"_address",id+"_address");
		copyinput2(nu+"_city",id+"_city");
	} else {
		//alert("?");	
	}
}

function copyinput2(from,to) {
	if(document.getElementById(from)) {
		fv=document.getElementById(from).value;
		//alert("go:"+fv);
		if(document.getElementById(to)) {
			document.getElementById(to).value=fv;
			//alert("ok");
		}
	} else {
		//alert("failed ("+from+","+to+")");
	}
}
function quick_login() {
	
	var dialog_html = ['<div id="dialog" title="Login">',
		'<p><strong>Inloggen</strong></p>',
		'<form>',
			'<div class="form-row">',
				'<label for="ql_email">Email</label>',
				'<input type="text" name="email" id="ql_email" value="" class="s75" />',
			'</div>',
			'<div class="form-row">',
				'<label for="ql_password">Wachtwoord</label>',
				'<input type="password" name="password" id="ql_password" value="" class="s75" />',
			'</div>',
			'<div class="form-row">',
				'<label>&nbsp;</label>',
				'<input type="submit" class="button" id="ql_submit" value="Inloggen" /> &nbsp; <a href="#" id="ql_cancel">Annuleren</a>',
			'</div>',
		'</form>',
	'</div>'].join('');
	
	$(document.body).append(dialog_html);
	
	var email = $("#ql_email"), password = $("#ql_password");
	var dialog = $('#dialog');
	
	dialog.find('#ql_cancel').click(function() {
		dialog.remove();
		return false;
	});
	
	dialog.find('#ql_submit').click(function() {
		$.post('/ajax?action=login', {
			username:	email.val(),
			password:	password.val()
		}, function(re) {
			if(re.auth == true) {
				window.location = window.location;
			} else {
				alert(re.error);
			}
		}, 'json');
		return false;
	});
	
	return false;
}

$(document).ready(function(){
	
	$('.quick_login').click(quick_login);
	
	/* external links */
	$('a.ext').attr('target', '_blank');
	
	/* MENU (IE6) */
	if($.browser.msie && $.browser.version == "6.0")
	$('.menu li').mouseenter(function() {
		$(this).find('ul.sub').css({
			left:			0,
			top:			25,
			right:			2,
			padding:		'3px 0',
			'white-space':	'nowrap',
			width:			200,
			height:			'auto'
		});
	}).mouseleave(function() {
		$(this).find('ul.sub').css({
			left:	-9999,
			top:	-9999
		});
	});
	
	/* END MENU */
	/*
	$('input[name="new_account_type"]').change(function() {
		if($(this).val() == 'Deelnemer') {
			$('#new_acc_poer_more_info').hide();
			$('#nr_deelnemers, #go_to_next_step_company').show();
		} else {
			$('#new_acc_poer_more_info').show();
			$('#nr_deelnemers, #go_to_next_step_company').hide();
		}
	});
	*/
	
	$('#new_account_type_deelnemer, #new_account_type_deelnemerz').click(function() {
		if($(this).attr('checked')) {
			$('#new_acc_poer_more_info').hide();
			$('#nr_deelnemers, #go_to_next_step_company').show();
		}
	});
	
	$('#new_account_type_poer, #new_account_type_poerz').click(function() {
		if($(this).attr('checked')) {
			$('#new_acc_poer_more_info').show();
			$('#nr_deelnemers, #go_to_next_step_company').hide();
		}
	});
	//autofill values for organisations
	//autofill('#bez_zipp', ['#post_zipp', '#fact_zipp', '#direct_zipp', '#contact_zipp']);
	//autofill('#bez_number', ['#post_number', '#fact_number', '#direct_number', '#contact_number']);
	//autofill('#bez_number_add', ['#post_number_add', '#fact_number_add', '#direct_number_add', '#contact_number_add']);
	//autofill('#bez_address', ['#post_address', '#fact_address', '#direct_address', '#contact_address']);
	//autofill('#bez_city', ['#post_city', '#fact_city', '#direct_city', '#contact_city']);
	
	if(url_helper.get('p') == 'mijn-traject' && url_helper.get('id')) {
		alert('Deze training komt al voor in uw traject');
	}
	
	var banner = $('#banner_bg');
	var images_nr = 13;
	if(banner) {
		if(banner.attr('class')) {
			var cr_image = parseInt(banner.attr('class').replace('bg', ''), 10);
		}
	}

	setInterval(function() {
		
		if($.browser.msie && $.browser.version == '6.0') {
			banner.attr('class', 'bg' + cr_image);
		} else {
			banner.animate({
				opacity: 0.1
			}, 400, 'swing', function() {
				cr_image++;
				if(cr_image > images_nr) cr_image = 1;
				banner.attr('class', 'bg' + cr_image).animate({
					opacity: 1
				}, 400);
			});
		}
		
	}, 15000);
	
	set_sizes();
	
	var selected_tab = url_helper.get('tab') || 0;
	
	$('#tabs_content > p.titlu1, #tabs_content > div.tab_content').hide();
	$('#tabs_content > p.titlu1:eq(' + selected_tab + '), #tabs_content > div.tab_content:eq(' + selected_tab + ')').show();
	$('#meniu_c ul li a[rel=' + selected_tab + ']').addClass('active');
	
	$('#meniu_c a[rel]').click(function() {
		
		switch_tab($(this).attr('rel'));
		return false;
	});
	
	$('.go2inschr_from_data').click(function() {
		var date = $(this).attr('rel');
		switch_tab(5);
		$('#datum12').children().each(function() {
			if($(this).val() == date) {
				$(this).attr('selected', true);
			}
		});
		$('#datum22').children().each(function() {
			if($(this).val() == date) {
				$(this).attr('selected', true);
			}
		});
		return false;
	});
	
	$('.show_widget').click(function() {
		rel = $(this).attr('rel');
		if(rel && rel.length > 0) {
			horizon_widget.show(rel);
		} else {
			horizon_widget.show();
		}
		return false;
	});
	
	$('input[name=type]').click(function() {
	    if($('input[name=type]:checked').val() == 'deelnemer') {
	         $('#reg_education').show();
	    } else {
	         $('#reg_education').hide(); 
	    }
	});
	
	$('.getaddress').blur(function() {
		getaddress($(this).attr('rel'));
	});
	
	$('.hideParent').click(function() {
		
		$(this).parent().hide(300);
		
		return false;
	});
	
	//Inschrijven form
	$('#more_deelnemer').click(function() {
		var cl = $('#the_deelnemer').clone(true).appendTo($('#nr_deelnemers'));
		var nr = $('#nr_deelnemers').find('table').length;
		
		var row = cl.find('tr');
		var col = $('<td/>').css('text-align', 'right').html('<span>X</span>').appendTo(row[0]);
		col.find('span').addClass('deelnemers_remove').mouseenter(function() {
			$(this).parent().parent().parent().css('background-color', '#ffecfc');
		}).mouseleave(function() {
			$(this).parent().parent().parent().css('background-color', '#ffffff');
		}).click(function() {
			$(this).parent().parent().parent().remove();
		});
		
		cl.find('input[type=text]').val('');
		
		cl.find('*').each(function() {
			id = $(this).attr('id');
			if(id)
			if(id.indexOf('deelnemer_1') != -1) {
				id = id + '';
				id = id.replace('deelnemer_1', 'deelnemer_' + nr);
				$(this).attr('id', id);
			}
			
			rel = $(this).attr('rel');
			if(rel)
			if(rel.indexOf('deelnemer_1') != -1) {
				rel = rel + '';
				rel = rel.replace('deelnemer_1', 'deelnemer_' + nr);
				$(this).attr('rel', rel);
			}
		});
		
		$('#no_of_deelnemers').val(nr);
		return false;
	});
	
	$('#go_step_234').click(function() {
		
		source = 'optie';
		training_id = $('#inscr_sl_tr_id').val();
		
		url = 'http://' + window.location.host + '/direct-inschrijven?save_tr=ja&c=' + training_id + '&source=' + source + '&datum1=' + $('#datum12').val() + '&datum2=' + $('#datum22').val();
		window.location = url;
		
		/*$('#ins_step_1').hide();
		next = $('#user_type').val().toLowerCase() == 'deelnemer' ? '3' : '2';
		$('#ins_step_' + next).show();
		*/
		return false;
	});
	
	$('#go_step_2').click(function() {
		
		$('#ins_step_1').hide();
		next = $('#user_type').val().toLowerCase() == 'deelnemer' ? '3' : '2';
		$('#ins_step_' + next).show();
		
		return false;
	});
	
	$('#go_step_3').click(function() {
		
		var inp = $('#ins_step_2').find('input[rel=req]').filter(function() {
			return $(this).val().length < 2;
		});
		
		inp.css('border','1px solid pink');
		inp.focus(function() {
			$(this).css('border', '1px solid #5285b8');
		});
		
		if(inp.length > 0) {
			nr = (inp.length == 1 ? '1 required field is not filled' : inp.length + ' required fields are not filled! You need to fill the forms listed below:') + "\n";
			
			inp.each(function() {
				text = $(this).parent().parent().find('label:first').text();
				nr += text + "\n";
			});
			
			alert(nr);
		} else {
			$('#ins_step_2').hide();
			$('#ins_step_3').show();
		}
		return false;
	});
	$('#go_step_4').click(function() {
		$('#ins_step_3').hide();
		$('#ins_step_4').show();
		return false;
	});
	//form aanmelden
	$('#more_deelnemer2').click(function() {
		var cl = $('#the_deelnemer2').clone().insertAfter($('#the_deelnemer2'));
		cl.find('input[type=text]').val('');
		return false;
	});
	
	$('#go2_step_2').click(function() {
		
		source = $(this).attr('rel');
		training_id = $('#inscr_sl_tr_id').val();
		
		url = 'http://' + window.location.host + '/direct-inschrijven?save_tr=ja&c=' + training_id + '&source=' + source + '&datum1=' + $('#datum12').val() + '&datum2=' + $('#datum22').val();
		window.location = url;
		
		/*$('#ins2_step_1').hide();
		next = $('#user_type').val().toLowerCase() == 'deelnemer' ? '3' : '2';
		$('#ins2_step_' + next).show();
		*/
		return false;
	});
	$('#go2_step_3').click(function() {
		
		var inp = $('#ins2_step_2').find('input[rel=req]').filter(function() {
			return $(this).val().length < 2;
		});
		
		inp.css('border','1px solid pink');
		inp.focus(function() {
			$(this).css('border', '1px solid #5285b8');
		});
		
		if(inp.length > 0) {
			nr = (inp.length == 1 ? '1 required field is not filled' : inp.length + ' required fields are not filled! You need to fill the forms listed below:') + "\n";
			
			inp.each(function() {
				text = $(this).parent().parent().find('label:first').text();
				nr += text + "\n";
			});
			
			alert(nr);
		} else {
			$('#ins2_step_2').hide();
			$('#ins2_step_3').show();
		}
		return false;
	});
	$('#go2_step_4').click(function() {
		$('#ins2_step_3').hide();
		$('#ins2_step_4').show();
		return false;
	});
	
	if($('#calc-discount').length > 0) {
		calc_discount();
		$('#to_inscr').click(function() {
			
			ids = [];
			
			$('#calc-discount li').each(function() {
				info = $(this).attr('rel')+'';
				info = info.split('|');
				ids.push(info[0]);
			});
			
			window.location = 'http://' + window.location.host + '/direct-inschrijven/?c=' + ids.join(',');
			
			return false;
		});
	}
	
	if($('.mijn-traject-discount').length > 0) {
		calc_discount2();
		$('.mijn-traject-discount').find('input').click(calc_discount2);
	}
	
	$('#mijn-traject-to-direct-inscr').click(function() {
		selected = $('.mijn-traject-discount').find('input:checked');
		if(selected.length < 1) {
			alert('U dient minimaal één training in uw traject te plaatsen.');
		} else {
			ids = [];
			dates = [];
			selected.each(function() {
				ids.push($(this).attr('value'));
				date = $(this).parent().next().children('select');
				dates.push($(date[0]).val() + '-' + $(date[1]).val());
			});
			
			url = 'http://' + window.location.host + '/direct-inschrijven?nw=tr&from_s=opgeslagen&c=' + ids.join(',') + '&date=' + dates.join(',');
			window.location = url;
		}
	});

	$('#mijn-traject-to-optie').click(function() {
		selected = $('.mijn-traject-optie').find('input:checked');
		if(selected.length < 1) {
			alert('U dient minimaal één training in uw traject te plaatsen.');
		} else {
			ids = [];
			dates = [];
			selected.each(function() {
				ids.push($(this).attr('value'));
				date = $(this).parent().next().children('select');
				dates.push($(date[0]).val() + '-' + $(date[1]).val());
			});
			url = 'http://' + window.location.host + '/direct-inschrijven?nw=tr&from_s=opties&c=' + ids.join(',') + '&date=' + dates.join(',');
			window.location = url;
		}
	});

	$('#mijn-traject-optie-to-inscr').click(function() {
		selected = $('.mijn-traject-optie').find('input:checked');
		if(selected.length < 1) {
			alert('U dient minimaal één training in uw traject te plaatsen.');
		} else {
			ids = [];
			selected.each(function() {
				ids.push($(this).attr('value'));
			});
			url = 'http://' + window.location.host + '/mijn-horizon/?p=mijn-traject&tab=verzonden-opties&add=' + ids.join(',');
			window.location = url;
		}
	});
	
});


function calc_discount2() {
	
	var training = 0;
	var module = 0;
	var discount = 0;
	price = 0;
	
	var cont = $('#show_discount').empty();
	
	$('.mijn-traject-discount').each(function() {
		
		if($(this).find('input:checked').length) {
			type = $(this).attr('rel');
			
			price2 = $(this).find('span.prijs').text();
			price2 = price2.replace(/[^0-9,]/g,'');
			price2 = price2.replace(',', '.');
			price2 = parseFloat(price2);
			if(price2 < 0) price2 = 0;
			price = price + price2;
			
			if(type == '1') training++;
			if(type == '2') module++;
		}
	});
	
	
	
	if(training > 0) {
		if(module == 1) discount = 2;
		if(module == 2) discount = 4;
		if(module == 3) discount = 6;
		if(module == 4) discount = 8;
		if(module > 4) discount = 10;
	}
	
	if($('#user_account_type').length && $('#user_account_type').attr('rel') == 'PO-er') {
		discount = 0;
	}
	
	discount2=0;
	if(document.getElementById('mijn-traject-calc-discount-org')) {
		discount2=document.getElementById('mijn-traject-calc-discount-org').innerHTML;
	}
	p_discount=0;
	p_discount2=0;
	discountb=0;
	p_discount2b=0;
	p_discount2p=0;
	if(discount2>discount) {
		discountb=discount;
		p_discount2p=discount2;
		if(discountb>0) {
			p_discount2b = price * (discountb / 100);
		}
		discount=0;
		if(discount2>0) {
			p_discount2 = price * (discount2 / 100);
		}
	} else {
		p_discount2p=discount;
		discountb=discount2;
		discount2=0;
		if(discountb>0) {
			p_discountb = price * (discountb / 100);
		}
		if(discount>0) {
			p_discount = price * (discount / 100);
		}
	}
	
	p_discount = Math.round(p_discount*100)/100;
	p_discount2 = Math.round(p_discount2*100)/100;
	
	$('#mijn-traject-calc-discount').text(discount);
	
	
	if(document.getElementById('mijn-traject-calc-discount-org')) {
		$('#mijn-traject-calc-discount-org').text(discount2);
	}
	$('#mijn-traject-calc-subtotal').text(price.formatMoney(2, ',', '.'));
	$('#mijn-traject-calc-discount2').text(p_discount.formatMoney(2, ',', '.'));
	//alert(discount2+"/"+discount+"|"+price+"="+p_discount+"="+p_discount2+"|"+p_discount2b);
	if(document.getElementById('mijn-traject-calc-discount2-org')) {
		$('#mijn-traject-calc-discount2-org').text(p_discount2.formatMoney(2, ',', '.'));
	}
	
	var vre_price = price - p_discount - p_discount2;
	
	$('#mijn-traject-calc-total').text(parseFloat(vre_price).formatMoney(2, ',', '.'));
	
	var code = '';
	
	if(document.getElementById('trajectcode')) {
		code=document.getElementById('trajectcode').value;
	}
	korting(code,p_discount2p);
}

function korting(code,hoeveel) {
	//alert(code);
	var dat=new Array();
	file="/postcode/org.php?code="+code+"&korting="+hoeveel+"";
	if((hoeveel!='')) {
		//alert(file);
		xmlophalen( ""+file, 'test',korting2,dat,hoeveel);
	}
	return false;
}

function korting2(responseText,dat,extravars) {
	
}

function calc_discount() {
	
	var training = 0;
	var module = 0;
	var discount = 0;
	price = 0;
	
	var cont = $('#show_discount').empty();
	
	$('#calc-discount li').each(function() {
		info = $(this).attr('rel')+'';
		info = info.split('|');
		price2 = info[2].replace(/[^0-9,]/g,'');
		alert(price2);
		price2 = parseFloat(price2);
		if(price2 < 0) price2 = 0;
		price = price + price2;
		if(info[1] == '1') training++;
		if(info[1] == '2') module++;
	});
	
	if(training > 0) {
		if(module == 1) discount = 2;
		if(module == 2) discount = 4;
		if(module == 3) discount = 6;
		if(module == 4) discount = 8;
		if(module > 4) discount = 10;
	}
	
	p_discount = price * (discount / 100);
	p_discount = Math.round(p_discount*100)/100;
	$('<p/>').html('&nbsp;&nbsp;&nbsp;&nbsp;<b style="display:inline-block;width:150px">Subtotaal</b> &euro; ' + price + '<br />').appendTo(cont);
	$('<p/>').html('&nbsp;&nbsp;&nbsp;&nbsp;<b style="display:inline-block;width:150px">Korting ' + discount + '%</b> &euro; ' + p_discount + '<br />').appendTo(cont);
	$('<p/>').html('&nbsp;&nbsp;&nbsp;&nbsp;<b style="display:inline-block;width:150px">Total</b> &euro; ' + (price - p_discount) + '<br />').appendTo(cont);
	
}

function set_sizes() {
	var sidebar = $('#right_sidebar').height() + $('#right_sidebar_down').height();
	var content = $('#content').height();
	style = ($.browser.msie && $.browser.version == '6.0') ? 'height' : 'min-height';
	$('#content').css(style, (content > sidebar ? content : sidebar) + 15);
	
	if($.browser.msie && $.browser.version == '6.0') {
		$('#right, #menu_left2, #menu_left').css('height', (content > sidebar ? content : sidebar) + 15);
	}	
	
}