$(function() {
	//Menu
	if ($.browser.msie) {
		$('#nav li').gt(0).prepend('&nbsp;|&nbsp;');
	}

	//Thickbox initialise
	tb_init('#galSingle a');
	
	//Offer
	//console.log($('#offer a').eq(0).clone().addClass('overLink'));
	$('#offer a').eq(0).clone().addClass('overLink').appendTo('#offer');
	$('#additional > li').each(function(i){
		$(this).find('a:first').eq(0).clone().addClass('overLink').appendTo($(this));
		//console.log($(this).children('a').eq(0));
	})	
		.mouseover(function(){	
			$(this).css('backgroundColor','#2152E9');
		})
		.mouseout(function(){	
			$(this).css('backgroundColor','');
		});
	
	//Gallery
	if ($('#galList li.active').size() == 0) $('#galList li').eq(0).addClass('active');
	
	$('#galList li a').click(function(){
		var href = $(this).attr("title");
		var src = $(this).attr("href");
		var txt = $(this).attr("name");
		$("#imgTxt").text(txt);
		
		var file = new Image();
		file.src = src;
		
		var imgwidth = '';
		var imgheight= '';
		if(file.height>=file.width){
			var dif = (file.height/300);
			var imgwidth = 'width="'+parseInt(file.width/dif)+'"';
			var imgheight = 'height="300px"';
		}else if(file.width>file.height){
			var dif = (file.width/275);
			var imgwidth = 'width="275px"';
			var imgheight= 'height="'+parseInt(file.height/dif)+'"';
		}
		
		$('<a href="'+href+'" style="position: absolute; top: 0; left: 0; display: none;"><img src="'+src+'" alt="" '+imgwidth+' '+imgheight+' /></a>').appendTo('#galSingle');
		$('#galSingle a.thickbox').fadeOut("normal",function(){
			$(this).remove();
			$('#galSingle a').addClass('thickbox');
		});
		$('#galSingle a').not('.thickbox').fadeIn("normal");
		tb_init('#galSingle a');
		
		$('#galList li.active').removeClass('active');
		$(this).parent('li').addClass('active');
		return false;
	});
	
	$('#galNav a').click(function(){
		if ($(this).parent('li').is('.next')) {
			var temp = $('#galList li.active a').parent('li');
			if (temp.next().is('li')) {
				var img = temp.next('li').children('a').eq(0);
			} else {
				return false;
			}
		} else {
			var temp = $('#galList li.active a').parent('li');
			if (temp.prev().is('li')) {
				var img = temp.prev('li').children('a').eq(0);
			} else {
				return false;
			}
		}
		var href = img.attr("title");
		var src = img.attr("href");
		var file = new Image();
		var txt = img.attr("name");
		$("#imgTxt").text(txt);
		file.src = src;
		
		var imgwidth = '';
		var imgheight= '';
		if(file.height>=file.width){
			var dif = (file.height/300);
			var imgwidth = 'width="'+parseInt(file.width/dif)+'"';
			var imgheight = 'height="300px"';
		}else if(file.width>file.height){
			var dif = (file.width/275);
			var imgwidth = 'width="275px"';
			var imgheight= 'height="'+parseInt(file.height/dif)+'"';
		}
		
		
		$('<a href="'+href+'" style="position: absolute; top: 0; left: 0; display: none;"><img src="'+src+'" '+imgwidth+' '+imgheight+' alt="" /></a>').appendTo('#galSingle');
		$('#galSingle a.thickbox').fadeOut("normal",function(){
			$(this).remove();
			$('#galSingle a').addClass('thickbox');
		});
		$('#galSingle a').not('.thickbox').fadeIn("normal");
		tb_init('#galSingle a');
		
		$('#galList li.active').removeClass('active');
		img.parent('li').addClass('active');
		return false;
	});
	
	//Tables
	$('table.toggleTable tr.overview > td:first-child').each(function(i) {
		if (($(this).parents('tr.overview').next().is('tr')) && (!$(this).parents('tr.overview').next().is('.overview'))) {
			$(this).prepend('<i></i>').children('i').click(function(){
				hideTableRows($(this));
			});
		};
	});
	
	$('table.toggleTable tbody tr').not('.overview').css("display","none").end().filter('.overview').children('i').toggleClass('minus');
});

function displayExtra(){
	$('table.toggleTable tbody tr').not('.overview').css("display","").end().filter('.overview').children('i').toggleClass('minus');
}

function displayExtra2(){
	$('table.toggleTable tbody tr').not('.overview').css("display","").end().filter('.overview').children('i').toggleClass('minus');
	windowPrint();
}

function windowPrint(){
	window.print();
}

function displaySmall(){
	$('table.toggleTable tbody tr').not('.overview').css("display","none").end().filter('.overview').children('i').toggleClass('minus');
}

function hideTableRows(id) {
	var p = id.parents('tr.overview').next();
		var k = 0;
		if (!p.is('tr.overview') && p.size() != 0) {
			id.toggleClass('minus');
			do {
				if(p.css('display') == 'none') p.css('display', '')
				else p.css('display', 'none');
				p = p.next('tr');
				//console.log(k, p, p.size())
				k++;
			} while ((!p.is('tr.overview')) && (p.size() != 0));
		}
}

function ChangeMaterial(uid) {
	var url = '/intranet/pot.html?tx_estimation_pi1[showUid]='+uid;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	document.location.href=url+"&material="+material;
}
function ChangeCategory(uid) {
	var url = '/intranet/pot.html?tx_estimation_pi1[showUid]='+uid;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	document.location.href=url+"&material="+material+"&category="+category;
}
function ChangeProduct(uid) {
	var url = '/intranet/pot.html?tx_estimation_pi1[showUid]='+uid;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	var prod = document.getElementById("m3").options[document.getElementById("m3").selectedIndex].value;
	document.location.href=url+"&material="+material+"&category="+category+"&product="+prod;
}
function ChangeDimension(uid) {
	var url = '/intranet/pot.html?tx_estimation_pi1[showUid]='+uid;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	var prod = document.getElementById("m3").options[document.getElementById("m3").selectedIndex].value;
	var dimensions = document.getElementById("m4").options[document.getElementById("m4").selectedIndex].value;
	document.location.href=url+"&material="+material+"&category="+category+"&product="+prod+"&dimension="+dimensions;
}

function ChangeMaterialEdit(uid,part) {
	var url = '/intranet/pot.html?tx_estimation_pi1[showUid]='+uid+'&edit=1&estimationPartUid='+part;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	document.location.href=url+"&material="+material;
}
function ChangeCategoryEdit(uid,part) {
	var url = '/intranet/pot.html?tx_estimation_pi1[showUid]='+uid+'&edit=1&estimationPartUid='+part;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	document.location.href=url+"&material="+material+"&category="+category;
}
function ChangeProductEdit(uid,part) {
	var url = '/intranet/pot.html?tx_estimation_pi1[showUid]='+uid+'&edit=1&estimationPartUid='+part;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	var prod = document.getElementById("m3").options[document.getElementById("m3").selectedIndex].value;
	document.location.href=url+"&material="+material+"&category="+category+"&product="+prod;
}
function ChangeDimensionEdit(uid,part) {
	var url = '/intranet/pot.html?tx_estimation_pi1[showUid]='+uid+'&edit=1&estimationPartUid='+part;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	var prod = document.getElementById("m3").options[document.getElementById("m3").selectedIndex].value;
	var dimensions = document.getElementById("m4").options[document.getElementById("m4").selectedIndex].value;
	document.location.href=url+"&material="+material+"&category="+category+"&product="+prod+"&dimension="+dimensions;
}



function ChangePlantType(uid) {
	var url = '/intranet/plant.html?tx_estimation_pi1[showUid]='+uid;
	var type = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	document.location.href=url+"&ptype="+type+"&category="+type;
}
function ChangePlantCategory(uid) {
	var url = '/intranet/plant.html?tx_estimation_pi1[showUid]='+uid;
	var type = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	//var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	document.location.href=url+"&ptype="+type+"&category="+type;
}
function ChangePlantProduct(uid) {
	var url = '/intranet/plant.html?tx_estimation_pi1[showUid]='+uid;
	var type = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	//var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	var prod = document.getElementById("m3").options[document.getElementById("m3").selectedIndex].value;
	document.location.href=url+"&ptype="+type+"&category="+type+"&product="+prod;
}
function ChangePlantTypeEdit(uid,part,nr) {
	var url = '/intranet/plant.html?tx_estimation_pi1[showUid]='+uid+'&edit=1&estimationPartUid='+part+'&nr='+nr;
	var type = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	//var category = document.getElementById("m2").value;
	document.location.href=url+"&ptype="+type+"&category="+type;
}
function ChangePlantCategoryEdit(uid,part,nr) {
	var url = '/intranet/plant.html?tx_estimation_pi1[showUid]='+uid+'&edit=1&estimationPartUid='+part+'&nr='+nr;
	var type = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	//var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	document.location.href=url+"&ptype="+type+"&category="+category;
}
function ChangePlantProductEdit(uid,part,nr) {
	var url = '/intranet/plant.html?tx_estimation_pi1[showUid]='+uid+'&edit=1&estimationPartUid='+part+'&nr='+nr;
	var type = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	//var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	var prod = document.getElementById("m3").options[document.getElementById("m3").selectedIndex].value;
	document.location.href=url+"&ptype="+type+"&category="+type+"&product="+prod;
}


function ChangeCMaterial(uid) {
	var url = '/klanten-intranet/pot.html?tx_estimation_pi1[showUid]='+uid;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	document.location.href=url+"&material="+material;
}
function ChangeCCategory(uid) {
	var url = '/klanten-intranet/pot.html?tx_estimation_pi1[showUid]='+uid;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	document.location.href=url+"&material="+material+"&category="+category;
}
function ChangeCProduct(uid) {
	var url = '/klanten-intranet/pot.html?tx_estimation_pi1[showUid]='+uid;
	var material = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	var prod = document.getElementById("m3").options[document.getElementById("m3").selectedIndex].value;
	document.location.href=url+"&material="+material+"&category="+category+"&product="+prod;
}

function ChangeCPlantType(uid) {
	var url = '/klanten-intranet/plant.html?tx_estimation_pi1[showUid]='+uid;
	var type = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	document.location.href=url+"&ptype="+type;
}
function ChangeCPlantCategory(uid) {
	var url = '/klanten-intranet/plant.html?tx_estimation_pi1[showUid]='+uid;
	var type = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	document.location.href=url+"&ptype="+type+"&category="+category;
}
function ChangeCPlantProduct(uid) {
	var url = '/klanten-intranet/plant.html?tx_estimation_pi1[showUid]='+uid;
	var type = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var category = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	var prod = document.getElementById("m3").options[document.getElementById("m3").selectedIndex].value;
	document.location.href=url+"&ptype="+type+"&category="+category+"&product="+prod;
}

/* VERIFICATIONS */

function check_email(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){ return (false); }
	} 
	
	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) { return (-1); }
	}
}

function SaveClient() {
	var customer_company = document.getElementById("customer_company").value;
	var customer_name = document.getElementById("customer_name").value;
	var customer_address = document.getElementById("customer_address").value;
	var customer_phone = document.getElementById("customer_phone").value;
	var customer_email = document.getElementById("customer_email").value;
	var customer_username = document.getElementById("customer_username").value;
	
	var er = ''; var k=0;
	if (customer_company=="") { k=1; er=er+"Vul een bedrijfsnaam in! \n"; }
	if (customer_name=="") { k=1; er=er+"Vul de klant naam in! \n"; }
	if (customer_address=="") { k=1; er=er+"Vul het adres in! \n"; }
	if (customer_phone=="") { k=1; er=er+"Vul het telefoonnummer in! \n"; }
	if (customer_email=="") { k=1; er=er+"Vul het email adres in! \n"; }
	if (customer_username=="") { k=1; er=er+"Vul de gewenste (unieke) gebruikersnaam in! \n"; }
	if (k==1) {
		alert(er);
	} else {
		if (!check_email(customer_email)) alert("Ongeldig email adres"); 
		else document.addcustomerform.submit();
	}
}

function EstimationSave() {
	var tx_estimation_title = document.getElementById("tx_estimation_title").value;
	var tx_estimation_cfactor = document.getElementById("tx_estimation_cfactor").value;
	var tx_estimation_discount = document.getElementById("tx_estimation_discount").value;
	
	var er = ''; var k=0;
	if (tx_estimation_title=="") { k=1; er=er+"Please type estimation title! \n"; }
	if (tx_estimation_cfactor=="") { k=1; er=er+"Please type correctie factor! \n"; }
	if (tx_estimation_discount=="") { k=1; er=er+"Please type kortingspercentage! \n"; }
	if (k==1) {
		alert(er);
	} else {
		document.estimationform.submit();
	}
}

function CheckStep3Estimation() {
	var val = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	if (val==0) alert("Please select a value!");
	else document.estimationstep3form.submit();
}

function CheckStep1Estimation(val) {
	var m1 = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var m2 = document.getElementById("m2").options[document.getElementById("m2").selectedIndex].value;
	var m3 = document.getElementById("m3").options[document.getElementById("m3").selectedIndex].value;
	var m4 = document.getElementById("m4").options[document.getElementById("m4").selectedIndex].value;
	var m5 = document.getElementById("m5").value;
	var m6 = document.getElementById("m6").value;
	
	var er = ''; var k=0;
	if (m1==0) { 
		alert("Selecteer een materiaal! \n");
	} else {
		if (m2==0) { 
			alert("Selecteer een categorie! \n");
		} else {
			if (m3==0) {
				alert("Selecteer een  product! \n");
			} else {
				if (m4==0) {
					alert("Selecteer een optie! \n");
				} else {
					if (m5=="") { k=1; er=er+"Geef het aantal potten aan! \n"; }
					//if (m6=="") { k=1; er=er+"Geef de locatie aan! \n"; }
					if (k==1) {
						alert(er);
					} else {
						document.getElementById('next_id').value=val;
						document.estimationstep1form.submit();
					}
				}
			}
		}
	}
}


function CheckStep2Estimation(val) {
	var m1 = document.getElementById("m1").options[document.getElementById("m1").selectedIndex].value;
	var m2 = document.getElementById("m2").value;
	var m3 = document.getElementById("m3").options[document.getElementById("m3").selectedIndex].value;
	var m4 = document.getElementById("m4").value;
	
	var er = ''; var k=0;
	if (m1==0) { 
		alert("Selecteer een type! \n");
	} else {
		if (m2==0) { 
			alert("Selecteer een categorie! \n");
		} else {
			if (m3==0) {
				alert("Selecteer een product! \n");
			} else {
				if (m4=="") { k=1; er=er+"Geef het aantal planten aan! \n"; }
				if (k==1) {
					alert(er);
				} else {
					document.getElementById('next_id').value=val;
					document.estimationstep2form.submit();
				}
			}
		}
	}
}

function VerifyDelete(estimation, part) {
	if (confirm("Weet u zeker dat u deze regel wilt verwijderen?")){
		window.location.href = "/intranet/offertes-facturen.html?tx_estimation_pi1[showUid]="+estimation+"&delete=1&estimationPartUid="+part;
	}
}

function VerifyClientDelete(estimation, part) {
	if (confirm("Weet u zeker dat u deze regel wilt verwijderen?")){
		window.location.href = "/klanten-intranet/offertes-facturen.html?tx_estimation_pi1[showUid]="+estimation+"&delete=1&estimationPartUid="+part;
	}
}

function PrintLink() {
	var url = document.location.href;  
	var newurl = url;
	if ( url.indexOf("?") > -1 ) newurl = newurl+'&typeP=1';
	else  newurl = newurl+'?typeP=1';
	window.open(newurl,'PrintPage','menubar=1,scrollbars=1,width=800,height=680');
}

function PrintLink2() {
	var url = document.location.href;  
	var newurl = url;
	if ( url.indexOf("?") > -1 ) newurl = newurl+'&typeP=1&typeL=1';
	else  newurl = newurl+'?typeP=1&typeL=1';
	window.open(newurl,'PrintPage','menubar=1,scrollbars=1,width=800,height=680');
}
	
