$(document).ready(function(){
	//homepage images
	$('.logosBox img').hover(function() {
		var src=$(this).attr('src');
		var src=src.replace("-2.gif", "-1.gif");
		$(this).attr('src', src);
	}, function() {
		var src=$(this).attr('src');
		var src=src.replace("-1.gif", "-2.gif");
		$(this).attr('src', src);
	});

	var teller=1;
	$('.logosBox img').each(function() {
		var object=$(this);
		var src=$(this).attr('src');

		function change() {
			$(object).attr('src', src.replace("-2.gif", "-1.gif"));
			setTimeout(change_back, teller*200);
		};

		function change_back() {
			$(object).attr('src', src);
		};

		setTimeout(change, teller*200);
		teller++;
	});

	//perspakket
	$('a.perspakket').live("click", function() {
		window.location.href=$(this).attr('href');
		return true;
	});

	//colorbox
	$("a[rel='b-to-b']").colorbox({rel:'nofollow'});
	$('a[rel=productimg]').live('click', function() {
		url=this.href;
		$.fn.colorbox({href: url});
		return false;
	});

	//b-to-b images
	$('.imagesBox img').hover(function() {
		var src=$(this).attr('src');
		var src=src.replace("z.jpg", "k.jpg");
		$(this).attr('src', src);
	}, function() {
		var src=$(this).attr('src');
		var src=src.replace("k.jpg", "z.jpg");
		$(this).attr('src', src);
	});

	//contact
	var contact = function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setContactForm&id='+this.id+'&'+$('form#contact').serialize(),

			success: function(result) {
				$('#centerCntr').html(result);
			}
		});
		return false;
	};

	//setContactForm
	$('.contactBox li').live("click", contact);
	$('.contactBox input#verzend').live("click", contact);
	if($('.contactBox').length!=0) contact();
	
	//showDetails
	$('.images a.details').live("click", function() {
		var pid=this.id;
		var pid=pid.replace('show', '');
		var src=$(this).attr('innerHTML');

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showDetails&pid='+pid,

			success: function(result) {
				$('#details'+pid).toggle();

				if(src=="Details") {
					$('#details'+pid).html(result);
					$('a#show'+pid).css('background', 'url(\'/images/product-minius.jpg\') right center no-repeat');
					$('a#show'+pid).html('Details ');
				}else{
					$('a#show'+pid).css('background', 'url(\'/images/detail.jpg\') right center no-repeat');
					$('a#show'+pid).html('Details');
				}
			}
		});
		return false;
	});

	//showOrder
	var showOrder = function() {
		var oid=this.id;
		var src=$(this).attr('innerHTML');
		var src=$(src).attr('src');

		if(src=='/images/to-be-close.gif') {
			$('#order'+oid).hide();
			$('#'+oid).html('<img src="/images/to-be-open.gif" alt="" />');
			doHeight();
		}else{
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=showOrder&oid='+oid,
	
				success: function(result) {
					$('#order'+oid).html(result);
					$('#'+oid).html('<img src="/images/to-be-close.gif" alt="" />');
					$('#order'+oid).show();
					doHeight();
				}
			});
		}
		return false;
	};

	//$('.tab a.details').click(showOrder);
	$('.tab a.details').live("click", showOrder);

	//click value but not submit
	$('form.product').live("click", function() {
		return false;
	});

	//showBasket
	var showBasket = function() {
		//pid
		var pid=$(this).attr('id');
	
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showBasket&'+$('#product'+pid).serialize(),

			success: function(result) {
				$('#basket').html(result);
			}
		});
		return false;
	};

	//do basket??
	if(window.location.pathname!="/dealer/wijzigen.html" && window.location.pathname!="/dealer/afronden.html") {
		if(document.getElementById('basket')) {
			//bestel
			$('.product .button').live("click", showBasket);
			//load
			$(document).ready(showBasket);
		}
	}else $('.linkBox .last').hide();

	//showBasket
	var showWijzigen = function() {
		//actie
		var pid=$(this).attr('id');
		var actie=$(this).attr('class');
		var thisclass=$(this).attr('class');
		var curvalue=$('input[name$="value"]').val();

		if(window.location.pathname=="/dealer/wijzigen.html") {
			var cmd='showWijzigen';
		}

		if(window.location.pathname=="/dealer/lopende-order-wijzigen.html") {
			var cmd='showLopendeOrderWijzigen';
		}

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd='+cmd+'&actie='+actie+'&class='+thisclass+'&curvalue='+curvalue+'&'+$('form#form'+pid).serialize(),

			success: function(result) {
				$('#centerCntr').html(result);
				//showBasket();
				doHeight();
			}
		});
		return false;
	};

	//edit uw bestelling
	$('.factuuradresBox .wijzigen').live("change", showWijzigen);
	$('.factuuradresBox .minus').live("click", showWijzigen);
	$('.factuuradresBox .plus').live("click", showWijzigen);
	$('.factuuradresBox .closeTab').live("click", showWijzigen);

	//setAantal
	var setAantal = function() {
		//actie
		var pid=$(this).attr('id');
		var actie=$(this).attr('class');

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setAantal&actie='+actie+'&'+$('form#product'+pid).serialize(),

			success: function(result) {
				$('#value'+pid).val(result);
			}
		});
		return false;
	};

	//set aantal
	$('.productBox .minus').live("click", setAantal);
	$('.productBox .plus').live("click", setAantal);
	
	//perspakket
	$('.clicktrue').live("click", function() {
		window.location.href=$(this).attr('href');
		return true;
	});
	
	//set besteller
	$('#besteller').live("keyup", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setBesteller&besteller='+document.getElementById('besteller').value
		});
		return false;
	});

	//set reden
	$('#reden').live("keyup", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setReden&reden='+document.getElementById('reden').value
		});
		return false;
	});

	//search dealer
	$('#searchDealer').keyup(function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=searchDealer&query='+document.getElementById('searchDealer').value+'&thisclass='+$(this).attr('class'),
			
			success: function(result) {
				$('#searchResults').html(result);
				doHeight();
			}
		});
		return false;
	});

	//search verkoper
	$('#searchVerkoper').keyup(function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=searchVerkoper&query='+document.getElementById('searchVerkoper').value,
			
			success: function(result) {
				$('#searchResults').html(result);
			}
		});
		return false;
	});
	
	function showVerkoper(id) {
		if(id!='[object Object]') {
			var did=id;
		}else{
			var did=$(this).attr('id');
			if(!did) var did=document.getElementById('did').value;
		}

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showVerkoper&did='+did,

			success: function(result) {
				$('#centerCntr').html(result);
				doHeight();
			}
		});
		return false;
	}

	//showVerkoper
	$('.showVerkoper').live("click", showVerkoper);

	//save verkoper
	$('#saveverkoper').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showVerkoper&save=yes'+'&'+$('form#verkoper').serialize(),

			success: function(result) {
				$('#centerCntr').html(result);
			}
		});
		return false;
	});

	//search persaccount
	$('#searchPersaccount').keyup(function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=searchPersaccount&query='+document.getElementById('searchPersaccount').value,
			
			success: function(result) {
				$('#searchResults').html(result);
			}
		});
		return false;
	});

	//nieuweverkoper
	$('#nieuweverkoper').live("click", function() {
		if(confirm('Let op: u staat op het punt een nieuwe verkoper aan te maken. Klik op OK om verder te gaan.')) {
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=nieuweVerkoper',
	
				success: function(result) {
					showVerkoper(result);
					$('a#nieuweverkoper').addClass('selected');
				}
			});
		}
		return false;
	});

	//nieuwepersaccount
	$('#nieuwepersaccount').live("click", function() {
		if(confirm('Let op: u staat op het punt een nieuwe persaccount aan te maken. Klik op OK om verder te gaan.')) {
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=nieuwePersaccount',
	
				success: function(result) {
					showVerkoper(result);
					$('a#nieuwepersaccount').addClass('selected');
				}
			});
		}
		return false;
	});

	//setAantal
	var doDealerLogin = function() {
		if(confirm('Let op: u staat op het punt de huidige login te verlaten. Klik op OK om verder te gaan.')) {
			var did=$(this).attr('id');
	
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=doDealerLogin&did='+did,

				success: function(result) {
					$('#centerCntr').html(result);
				}
			});
		}
		return false;
	};

	//doDealerLogin
	$('.showDealer').live("click", doDealerLogin);
	$('.doDealerLogin').click(doDealerLogin);

	//search product
	$('#searchProduct').keyup(function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=searchProduct&query='+document.getElementById('searchProduct').value,
			
			success: function(result) {
				$('#searchResults').html(result);
				doHeight();
			}
		});
		return false;
	});

	//search products
	$('#searchProducts').click(function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=searchProducts&query='+document.getElementById('q').value,
			
			success: function(result) {
				$('#centerCntr').html(result);
				$('#contentCntr').addClass('productBg');
				$('#maintitle').removeClass('head');
				$('#maintitle').addClass('style');
				$('.style h2').html('Producten zoeken');
			}
		});
		return false;
	});

	function showProduct(id) {
		if(id!='[object Object]') {
			var pid=id;
		}else{
			var pid=$(this).attr('id');
		}
		
		//check pid
		if(!pid) var pid=document.getElementById('pid').value;

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showProduct&pid='+pid,

			success: function(result) {
				$('#centerCntr').html(result);
				uploadify();
				uploadify_pers();
				doHeight();

				if(id) {
					if(id!='[object Object]') setCollectie();
				}
			}
		});
		return false;
	}

	//showProduct
	$('.showProduct').live("click", showProduct);

	//setCollectie
	var setCollectie = function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setCollectie&mid='+document.getElementById('mid').value,

			success: function(data) {
				data = eval(data);
				var options = '';
				for (i=0; i<data.length; i++) {
					for (key in data[i]) {
						options += '<option value="' + key + '">' + data[i][key] + '</option>';
					}
				}
				$("#cid").html(options);
			}
		});
		return false;
	};
	
	$('#listingBox #mid').live("change", setCollectie);

	//save product
	$('#saveproduct').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showProduct&save=yes'+'&'+$('form#product').serialize(),

			success: function(result) {
				$('#centerCntr').html(result);
				uploadify();
				uploadify_pers();
				doHeight();
			}
		});
		return false;
	});

	function handlefile(event,queueID,fileObj,response,data) {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=handleFile&pid='+document.getElementById('pid').value+'&file='+fileObj.name,

			success: function(result) {
				showProduct();
			}
		});
	}

	function uploadify() {
		$("#uploadify").uploadify({
			'uploader'      : '/javascripts/uploadify/uploadify.swf',
			'script'        : '/javascripts/uploadify/uploadify.php',
			'folder'        : '/images/uploads/',
			'wmode'         : 'transparent',
			'buttonImg'     : '/images/addimg.gif',
			'width'         : 260,
			'height'        : 22,
			'auto'          : true,
			'multi'         : false,
			'fileDesc'      : 'Extensies: *.jpg',
			'fileExt'       : '*.jpg',
			'onComplete'    : handlefile,
			'onSelectOnce'  : doHeight,
			'sizeLimit'     : 2097152
		});
	}

	function handlefilepers(event,queueID,fileObj,response,data) {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=handleFilePers&pid='+document.getElementById('pid').value+'&file='+fileObj.name,

			success: function(result) {
				showProduct();
			}
		});
	}
	
	function uploadify_pers() {
		$("#uploadify_pers").uploadify({
			'uploader'      : '/javascripts/uploadify/uploadify.swf',
			'script'        : '/javascripts/uploadify/uploadify.php',
			'folder'        : '/images/uploads/',
			'wmode'         : 'transparent',
			'buttonImg'     : '/images/addpers.gif',
			'width'         : 260,
			'height'        : 22,
			'auto'          : true,
			'multi'         : false,
			'fileDesc'      : 'Extensies: *.zip',
			'fileExt'       : '*.zip',
			'onComplete'    : handlefilepers,
			'onSelectOnce'  : doHeight,
			'sizeLimit'     : 33554432
		});
	}
	
	//nieuwproduct
	$('#nieuwproduct').live("click", function() {
		if(confirm('Let op: u staat op het punt een nieuw product aan te maken. Klik op OK om verder te gaan.')) {
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=nieuwProduct',
	
				success: function(result) {
					showProduct(result);
					$('a#nieuwproduct').addClass('selected');
				}
			});
		}
		return false;
	});

	//show omzet
	$('form#omzet').submit(function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=getOmzet&'+$(this).serialize(),

			success: function(result) {
				$('#output').html(result);
				$('p#output').addClass('margin');
				doHeight();
			}
		});
		return false;
	});

	//search dealer
	$('#dealerName').keyup(function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=searchDealerOverzicht&query='+document.getElementById('dealerName').value,
			
			success: function(result) {
				if(result) {
					$('#overlayDealer').html(result);
					$('#overlayDealer').show();
				}
			}
		});
		return false;
	});

	//insertDealer
	$('a.insertDealer').live("click", function() {
		var did=$(this).attr('id');
		var name=$(this).attr('innerHTML');
		
		$('#dealerName').val(name);
		$('#did').val(did);
		
		$('#overlayDealer').hide();
		return false;
	});
	
	//generate_pass
	$('#generate_pass').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=generate_pass',

			success: function(result) {
				$('#ww1').val(result);
				$('#ww2').val(result);
			}
		});
		return false;
	});

	//show verkoperomzet
	$('form#verkoperomzet').submit(function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=verkoperOmzet&'+$(this).serialize(),

			success: function(result) {
				$('#verkoperoutput').html(result);
				$('p#verkoperoutput').addClass('margin');
				doHeight();
			}
		});
		return false;
	});

	//search verkoper
	$('#verkoperName').keyup(function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=searchVerkoperOverzicht&query='+document.getElementById('verkoperName').value,

			success: function(result) {
				if(result) {
					$('#overlayVerkoper').html(result);
					$('#overlayVerkoper').show();
				}
			}
		});
		return false;
	});

	//insertVerkoper
	$('a.insertVerkoper').live("click", function() {
		var vid=$(this).attr('id');
		var name=$(this).attr('innerHTML');

		$('#verkoperName').val(name);
		$('#vid').val(vid);

		$('#overlayVerkoper').hide();
		return false;
	});
	
	$('#listingBox').click(closeDiv);
	$('input#dealerName').live("click", closeDiv);
	$('input#verkoperName').live("click", closeDiv);
	
	function closeDiv() {
		$('#overlayDealer').hide();
		$('#overlayVerkoper').hide();
	}

	//checkValues
	$('a#checkValues').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=checkValues',

			success: function(result) {
				if(result==false) {
					alert('U dient een naam en reden op te geven');
					return false;
				}else{
					window.location.href=result;
					return true;
				}
			}
		});
	});
	
	//order afronden
	$('a.afronden').live("click", function() {
		if(confirm('Let op: u staat op het punt deze order af te ronden.\n\nU dient uw pop-up blocker uit te schakelen of een regel voor deze pagina in te stellen.\n\nKlik op OK om verder te gaan.')) {
			var oid=$(this).attr('id');
											   
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=orderAfronden&oid='+oid,
	
				success: function(result) {
					if(result!='false') {
						window.open(result);
						alert('De geselecteerde order is zojuist afgerond.\n\nDe factuur is geopend in een nieuw venster.');
						window.location.reload();
					}else alert('Er is een fout opgetreden bij het verwerken van de order. Probeer het opnieuw.');
				}
			});
		}
		return false;
	});

	//toon factuur
	$('a.toonfactuur').live("click", function() {
		if(confirm('Let op: de factuur die u krijgt te zien, is een proforma factuur.\n\nDe echte factuur wordt gemaakt en getoond wanneer u de order af rond.\n\nU dient uw pop-up blocker uit te schakelen of een regel voor deze pagina in te stellen.\n\nKlik op OK om verder te gaan.')) {
			var oid=$(this).attr('id');
											   
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=toonFactuur&oid='+oid,
	
				success: function(result) {
					if(result=='false') {
						alert('Deze factuur bestaat niet');
					}else window.open(result);
				}
			});
			return false;
		}
	});

	//wijzigen akkoord
	$('a.akkoord').live("click", function() {
		if(confirm('Let op: u staat op het punt deze wijziging goed te keuren.\n\nDe nieuwe order zal zichtbaar worden onder "Lopende orders".\n\nU dient uw pop-up blocker uit te schakelen of een regel voor deze pagina in te stellen.\n\nKlik op OK om verder te gaan.')) {
			var oid=$(this).attr('id');
											   
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=wijzigingAkkoord&oid='+oid,
	
				success: function(result) {
					alert('De geselecteerde wijziging is zojuist goedgekeurd.');
					window.location.reload();
				}
			});
		}
		return false;
	});

	//wijzigen nietakkoord
	$('a.nietakkoord').live("click", function() {
		if(confirm('Let op: u staat op het punt deze wijziging af te keuren.\n\nDe nieuwe order zal worden verwijderd en de oorspronkelijke (gewijzigde) order zal worden hersteld.\n\nKlik op OK om verder te gaan.')) {
			var oid=$(this).attr('id');
											   
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=wijzigingNietAkkoord&oid='+oid,
	
				success: function(result) {
					alert('De geselecteerde wijziging is zojuist afgekeurd.');
					window.location.reload();
				}
			});
		}
		return false;
	});

	//annuleren
	$('a.annuleren').live("click", function() {
		if(confirm('Let op: u staat op het punt deze order te annuleren.\n\nKlik op OK om verder te gaan.')) {
			var oid=$(this).attr('id');

			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=annuleren&oid='+oid,
	
				success: function(result) {
					alert('De geselecteerde order is zojuist geannuleerd.');
					window.location.reload();
				}
			});
		}
		return false;
	});

	//productsperpage
	$('#productsperpage').change(function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=productsperpage&aantal='+document.getElementById('productsperpage').value,

			success: function(result) {
				window.location.reload();
			}
		});
		return false;
	});	

	//doHeight
	var doHeight = function() {
		//do??
		if(document.getElementById('factuuradresBox') || document.getElementById('historieBox') || document.getElementById('listingBox') || document.getElementById('searchProduct') || document.getElementById('searchDealer') || window.location.pathname=="/dealer/wijzigen.html" || window.location.pathname=="/beheer/nieuweaccounts.html" || window.location.pathname=="/beheer/bezoekrapporten.html" || window.location.pathname=="/beheer/bezoekrapport_nieuw.html" || window.location.pathname=="/beheer/bezoekrapporten_zoeken.html") {
			//get
			var height=$('#centerCntr').height()+30;
			if(height<480) var height=480;
			//set
			$('#leftCntr, #rightCntr').css('height',height);
		}
	};

	//setHeight
	doHeight();
		
	if(window.location.pathname=="/dealer/wijzigen.html" || window.location.pathname=="/dealer/lopende-order-wijzigen.html") {
		showWijzigen();
	}
	
	//checkBesteller
	$('a#checkBesteller').live("click", function() {
		if(confirm('Controleer uw gegevens en klik op OK om uw bestelling te voltooien.')) {
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=checkBesteller',
	
				success: function(result) {
					if(result==false) {
						alert('U dient het veld "Uw volledige naam" in te vullen');
					}else{
						if($('input[name=akkoord_voorwaarden]').is(':checked')==false) {
							alert('U dient akkoord te gaan met de voorwaarden');
						}else window.location.href=result;
					}
				}
			});
		}
		return false;
	});

	//setOlidAantal
	$('.aantal').live("change", function() {
		var id=$(this).attr('id');
		var id=id.split("_");

		var oid=id[0];
		var olid=id[1];
		var aantal=$(this).attr('value');

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setOlidAantal&oid='+oid+'&olid='+olid+'&aantal='+aantal,

			success: function(result) {
				$('#order'+oid).html(result);
			}
		});
		return false;
	});
	
	//change factuurland
	$('#land').live("change", function() {
		$('#factuurland option[value='+$('#land').val()+']').attr('selected', 'selected');
		return false;
	});

	//autoResize
	var autoResize = function() {
		$('#reden, #afspraken, #acties, #opmerkingen').autoResizable({
			animate: true,
			animateDuration: 300,
			maxHeight: 1000,
			onBeforeResize: function(currentHeight, newHeight) { },
			onAfterResize: function() {
				doHeight();
			},
			padding: 0,
			paste: true,
			pasteInterval: 100
		});
	};
	
	autoResize();
	
	//calender
	$('#datumvolgendebezoek').datepicker({
		showWeek: true,
		dateFormat: 'yy-mm-dd',
		minDate: 1,
		maxDate: '+1y'
	});
	
	//calender
	$('#datumfrom, #datumto').datepicker({
		showWeek: true,
		dateFormat: 'yy-mm-dd',
		maxDate: '+0d'
	});
	
	//set "lopend"
	$('#acties').live("keydown", function() {
		$('input:radio').each(function() {
			$(this).attr('checked', false);
			if($(this).attr('value')==2) $(this).attr('checked', true);
		});
	});

	//uploadSubmit
	$('#checkInput').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=checkInput&'+$('#bezoekrapport').serialize(),

			success: function(result) {
				if(result) {
					alert(result);
				}else document.getElementById('bezoekrapport').submit();
			}
		});
		return false;
	});

	//readonly
	if($('#checkDid').size()!=0) {
		if($('#checkDid').attr('value')!='') {
			//readonly
			$('.section1 input, .section1 select').each(function() {
				$(this).attr('disabled', 'disabled');
			});

			//replace *
			$('.section1 label').each(function() {
				var label=$(this).html().replace('*', '');
				$(this).html(label);
			});
		}
	}

	//doSearchBezoekrapporten
	$('#doSearchBezoekrapporten').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=searchBezoekrapporten&'+$('#searchBezoekrapporten').serialize(),

			success: function(result) {
				$('#searchResult').html(result);

				$('textarea').each(function() {
					$(this).autoResizable({
						animate: true,
						animateDuration: 300,
						maxHeight: 1000,
						onBeforeResize: function(currentHeight, newHeight) { },
						onAfterResize: function() {
							doHeight();
						},
						padding: 0,
						paste: true,
						pasteInterval: 100
					});
					
					$(this).trigger('focus');
				});

				//readonly
				$('form.rapport input:text, form.rapport select, form.rapport textarea').each(function() {
					if(this.id!='opmerkingen') $(this).attr('disabled', 'disabled');
				});
				
				doHeight();
			}
		});
		return false;
	});

	//showRapport
	$('.showRapport').live("click", function() {
		var brid=this.id;
		$('#rapport'+brid).toggle();
		
		//plus/min
		if($(this).hasClass('labminus')) {
			$(this).removeClass('labminus');
		}else $(this).addClass('labminus');

		$("a[rel='uploads']").colorbox({rel:'nofollow'});

		doHeight();
		return false;
	});

	//showAdresgegevens
	$('.showAdresgegevens').live("click", function() {
		var brid=this.id;
		$('#adresgegevens'+brid).toggle();

		//plus/min
		if($(this).hasClass('labminus')) {
			$(this).removeClass('labminus');
		}else $(this).addClass('labminus');

		doHeight();
		return false;
	});

	//saveRapport
	$('.saveRapport').live("click", function() {
		var brid=this.id;

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=saveRapport&'+$('#form'+brid).serialize(),

			success: function(result) {
				alert(result);
				//$('#rapport'+brid).toggle();
			}
		});
		return false;
	});

	//set upload id
	$('object#file1Uploader, object#file2Uploader, object#file2Uploader').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setUploadId&id='+this.id,

			success: function(result) {

			}
		});
		return false;
	});

	function handleFileBezoekrapport(event,queueID,fileObj,response,data) {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=handleFileBezoekrapport&file='+fileObj.name,
			dataType: 'json',

			success: function(result) {
				$('#'+result.setUploadId+'Blaat').html(result.html);
				$("a[rel='uploads']").colorbox({rel:'nofollow'});
			}
		});
	}

	function uploadify_bezoekrapport() {
		$('#file1, #file2, #file3').uploadify({
			'uploader'      : '/javascripts/uploadify/uploadify.swf',
			'script'        : '/javascripts/uploadify/uploadify.php',
			'folder'        : '/images/uploads/',
			'wmode'         : 'transparent',
			'buttonImg'     : '/images/bladeren.jpg',
			'width'         : 73,
			'height'        : 18,
			'auto'          : true,
			'multi'         : false,
			'fileDesc'      : 'Extensies: *.jpg',
			'fileExt'       : '*.jpg',
			'onComplete'    : handleFileBezoekrapport,
			'onSelectOnce'  : doHeight,
			'sizeLimit'     : 5242880
		});
	}
	
	if($('#bezoekrapport').size()!=0) uploadify_bezoekrapport();
});
