// Basis functie om dom elementen mee op te halen

function clearText(e){
	if(e) {
		if(e.value == 'Trefwoord') {
			e.value = '';
		}
	}	
}

function fillEmpty(e){
	if(e.value == ''){
		e.value = 'Trefwoord';
	}
}

function bindElements(){
	$('.deleteProduct').click(function(){
		var data = this.id.split('_');
		var variation = data.pop();
		var id = data.pop();
		updateBasket({id:id, variation:variation, amount:0, type:'amount'});
	});

	$('.updateAmount').focus(function(){
		$(this).select();
	});

	$('.updateAmount').keydown(function(e){
		$(this).attr('oldVal', ($(this).val()));
	});
	
	$('.updateAmount').keyup(function(e){
		var val = parseInt($(this).val());
		if(isNaN(val)){
			$(this).val($(this).attr('oldVal'));
		} else if(val+'' != $(this).val()){
			$(this).val(val);
		} else if(val === 0){
			var data = this.id.split('_');
			var variation = data.pop();
			var id = data.pop();
			updateBasket({id:id, variation:variation, amount:this.value, timeout:500, type:'amount'});
		}

	/*
		if(e.which != 9){
			var data = this.id.split('_');
			var variation = data.pop();
			var id = data.pop();
			updateBasket({id:id, variation:variation, amount:this.value, timeout:500, type:'amount'});
		}
	*/
	});
	/*
	$('.updateAmount').blur(function(e){
		var data = this.id.split('_');
		var variation = data.pop();
		var id = data.pop();
		resetBasket({id:'input_'+id+'_'+variation,value:'amount'});
	});

	$('.updateRemark').blur(function(e){
		var data = this.id.split('_');
		var variation = data.pop();
		var id = data.pop();
		updateBasket({id:id, variation:variation, remark:this.value, timeout:500, type:'remark'});
	});
	*/
	$('#finishOffer a.button').click(function(){
		var url = $(this).attr('href');
		var recs = [];
		$('.updateAmount').each(function(){
			var data = this.id.split('_');
			var variation = data.pop();
			var id = data.pop();
			recs[recs.length] = {id:id,variation:variation,val:$(this).val(),remark:$('#remark_'+id+'_'+variation).val()};
		});
		ajax.request({
			url:'customer/plugins/webshop/render/update.php',
			method:'GET',
			params:{
				task:'checkBasket',
				data:ajax.encode(recs)
			},
			success:function(response){
				var result = response.responseText;
				if(result != ''){
					result = eval('('+result+')');
					switch(result.success){
						case true:
							document.location = url;
						break;
						case false:
							alert(result.msg);
						break;
					}
				} else {
					alert('Geen reactie van update.php bij checkBasket');
				}
			},
			failure:function(response){
				alert(response.responseText);
			}
		});
		return false;
	});
}

/*
function bindElements(){
	$('.deleteProduct').click(function(){
		var data = this.id.split('_');
		var variation = data.pop();
		var id = data.pop();
		updateBasket({id:id, variation:variation, amount:0, type:'amount'});
	});

	$('.updateAmount').keyup(function(e){
		if(e.which != 9){
			var data = this.id.split('_');
			var variation = data.pop();
			var id = data.pop();
			updateBasket({id:id, variation:variation, amount:this.value, timeout:500, type:'amount'});
		}
	});

	$('.updateAmount').blur(function(e){
		var data = this.id.split('_');
		var variation = data.pop();
		var id = data.pop();
		resetBasket({id:'input_'+id+'_'+variation,value:'amount'});
	});

	$('.updateRemark').blur(function(e){
		var data = this.id.split('_');
		var variation = data.pop();
		var id = data.pop();
		updateBasket({id:id, variation:variation, remark:this.value, timeout:500, type:'remark'});
	});
}
*/

function calculateSizes() {
	var containerTotal = document.getElementById('containerTotal');
	if(containerTotal) {
		var total = 0;
		for(var i = 1; i < 13; i++) {
			var unitsize = $('#size_unit'+i).html();
			if(unitsize != '') {
				size = unitsize.split(' cm');
				total = total + parseFloat(size[0]);
			}
		}
		if(containerTotal) {
			var response = '';
			if(total != 0) {
				response = total+' cm';
			}
			containerTotal.innerHTML = response;
		}
	}
}

function setBranche(){
	var branche = document.getElementById("branche");
	var functie = document.getElementById("functie");
	if (branche != null && functie != null){
		var name = branche.options[branche.selectedIndex].value;
		functie.options.length = 0;
		var option = null;
		if(name == "Horeca"){
			functie.options.add(createOption("Eigenaar"));
			functie.options.add(createOption("Horecamanager"));
			functie.options.add(createOption("Bedrijfsleider"));
			functie.options.add(createOption("Chef-kok"));
			functie.options.add(createOption("Anders"));
		} else if (name == "Bedrijven"){
			functie.options.add(createOption("Directeur/Eigenaar"));
			functie.options.add(createOption("Facilitair manager"));
			functie.options.add(createOption("Anders"));
		} else if (name == "Zorg"){
			functie.options.add(createOption("Directeur"));
			functie.options.add(createOption("Hoofd zorg"));
			functie.options.add(createOption("Chef-kok"));
			functie.options.add(createOption("Anders"));
		} else if (name == "Onderwijs"){
			functie.options.add(createOption("Directeur"));
			functie.options.add(createOption("Facilitair manager"));
			functie.options.add(createOption("Cateraar"));
			functie.options.add(createOption("Anders"));
		} else if (name == "Catering"){
			functie.options.add(createOption("Directeur / Eigenaar"));
			functie.options.add(createOption("Cateringmanager"));
			functie.options.add(createOption("Anders"));
		}else if (name == "Cafetaria"){
			functie.options.add(createOption("Eigenaar"));
			functie.options.add(createOption("Bedrijfsleider"));
			functie.options.add(createOption("Anders"));
		}  else {
			functie.options.add(createOption("Selecteer een branche"));
		}
	}
}


function createOption(name){
	var option = document.createElement('option');
	option.text = name;
	option.value = name;
	return option;
}

function submitform(form)
	{
		if(form == 'addofferte'){
			 document.addofferte.submit();
		}
		if(form == 'send'){
			 document.send.submit();
		}
		if(form == 'sendofferte'){
			 document.sendofferte.submit();
		}
		if(form == 'informatie'){
			 document.informatie.submit();
		}
	}
	function resetform(form)
	{
		if(form == 'informatie'){
			document.informatie.reset();
		}
	}

// Onready functie
$(function(){
	$('.divbutton').mouseover(function() {
		$(this).toggleClass('divbuttonHover');
	});
	$('.divbutton').mouseout(function() {
		$(this).toggleClass('divbuttonHover');
	});
	$('input.button').mouseover(function() {
		$(this).toggleClass('buttonHover');
	});
	$('input.button').mouseout(function() {
		$(this).toggleClass('buttonHover');
	});

	var arrowUp = $('#arrowUp img');
	var arrowDown = $('#arrowDown img');
	var scroller = $('#projectImageScroller');
	var spi = $('.smallProjectImage');

	arrowUp.css({cursor:'pointer'});
	arrowDown.css({cursor:'pointer'});
	var moving = {};
	arrowDown.click(function(){
		if(moving.pscroller !== true){
			var t = (parseInt(scroller.css('top'))+$('.smallProjectImage').height());
			var time = 800;
			if(t > 0){
				var n = parseInt(scroller.css('top'))*-1;
				time = parseInt(time/(spi.height()/n));
				t = 0;
			}
			moving.pscroller = true;
			scroller.animate({
				top:t
			}, time, 'swing', function(){
				moving.pscroller = false;	
			});
		}
	});
	arrowUp.click(function(){
		if(moving.pscroller !== true){
			var t = (parseInt(scroller.css('top'))-spi.height());
			var time = 800;
			if(t < ($('#projectImageWrap').height()-scroller.height())){
				var n = $('#projectImageWrap').height()-scroller.height()-t;
				n = spi.height()-n;
				time = parseInt(time/(spi.height()/n));
				t = ($('#projectImageWrap').height()-scroller.height());
			}
			moving.pscroller = true;
			scroller.animate({
				top:t
			}, time, 'swing', function(){
				moving.pscroller = false;
			});
		}
	});

	$('.smallProjectImage img').css({
		cursor:'pointer'
	});

	$('.smallProjectImage img').click(function(){
		var src = $(this).attr('large');
		var image = new Image();
		image.src = src;
		$('#largeProjectImage img').attr('src', image.src);
	});

	bindElements();

	if(document.all && navigator.appVersion.indexOf("MSIE")>-1 && navigator.appVersion.indexOf("Windows")>-1)
	{
		var a = document.getElementsByTagName("label");
		for(var i=0,j=a.length;i<j;i++){
			if(a[i].hasChildNodes && a[i].childNodes.item(0).tagName == "IMG")
			{
				a[i].childNodes.item(0).forid = a[i].htmlFor;
				a[i].childNodes.item(0).onclick = function(){
					var e = document.getElementById(this.forid);
					switch(e.type){
						case "radio": e.checked|=1;break;
						case "checkbox": e.checked=!e.checked;break;
						case "text": case "password": case "textarea": e.focus(); break;
					}
				}
			}
		}
	}

	$('.unitselect').change(function() {
		var selected = $("#"+this.id+" option:selected").attr('id');
		var items = selected.split('_');
		ajax.request({
			url:'customer/addsize.php',
			method:'GET',
			params:{
				unit:items[0],
				cm:items[1]
			},
			success:function(response){
				var result = response.responseText;
				if(result != ''){
					result = eval('('+result+')');
					switch(result.success){
						case true:
							$('#size_'+items[0]).html(items[1]+' cm');
							calculateSizes();
						break;
						case false:
							alert(result.msg);
						break;
					}
				} else {
					alert('Geen antwoord van addsize.php bij addSize.');
				}
			}
		});
	});
	
	calculateSizes();

	$('input[name="postcheck"]').css({
		display:'none'
	});
});
