$(function()	{

	//functie voor het opvangen van het formulier
	//bepaald welke velden verzonden moeten worden
	$("#sendWebshop").click(function()	{

		$(".productField").each(function()	{

			var fill = $(this).val();

			if (fill != 'undefined' && fill != '0' && fill != '')
			{
				//bovenliggende hidden en onderliggende aanzetten
				$(this).prev("input[type=hidden]").removeAttr("disabled");
				//$(this).next("input[type=hidden]").removeAttr("disabled");

			}else	{

				$(this).prev("input[type=hidden]").attr("disabled","disabled");
				//$(this).next("input[type=hidden]").attr("disabled","disabled");
				$(this).attr("disabled","disabled");

			}

		});

		//formulier submit
		document.webshopForm.submit();

	});

});

$(function()	{
	//functie voor het in en uit klappen van de contact en login box
	$("a.contact_header").toggle(function()	{
		$("#product_formulier").animate({height:"310px"},400,'', function(){
			//show the footer
			$("#formulier_footer").show();
			$(".productdetail_contactheader, #productdetail_contactform").fadeIn('fast');
		});

	},
	function()	{
		$(".productdetail_contactheader, #productdetail_contactform").fadeOut('fast',function()	{
			$("#formulier_footer").hide();
			$("#product_formulier").animate({height:"36px"},400);
		});
	});
});

function handleLoginDrop()
{
	//login box
	$("a.login_header_knop").toggle(function()	{
		$("#login_status").animate({
			height: "160px",
			bottom: "124px"
		},400,'', function(){
			$("#login_footer").show();
			$(".box_wrap").fadeIn('fast');
		});
	},
	function()	{
		$(".box_wrap").fadeOut('fast',function()	{
			$("#login_footer").hide();
			$("#login_status").animate({
				height: "36px",
				bottom: "248px"
			},400);
		});
	});
}

function checkWebshopButton()
{

	//functie die controleert of het extra menu item moet worden toegvoegd
	//request
	$.ajax({
		url: "/javascript/ajaxRequest/checkWebshopButton.php",
		type: "GET",
		dataType: "html",
		//cache: false,
		success: function(data) {
			if (data != 'not')
			{
				$("#hoofdmenu").append(data);
			}
		}
	});

}

function getOfferteForm()
{
	$.ajax({
		url: "/javascript/ajaxRequest/getFilledContactForm.php",
		type: "GET",
		dataType: "html",
		//cache: false,
		success: function(data) {
			$("#contactform_geheel_webshop").html(data);

		}
	});
}

//info uitklap
$(document).ready(function()	{

	// Uitklap voor mikos
	$("div.projectoverzicht_omschrijving").each(function(){
		$(this).attr("rel", $(this).height() );
		$(this).height("17px");
	});

	if ($("div.projectoverzicht").length > 0)	{

		$("div.projectoverzicht_header").click(function(event)	{
			// slideDown
			var textDiv = $(this).next("div.projectoverzicht_tekst")
			var omschrijvingDiv = textDiv.children("div.projectoverzicht_omschrijving");
			var maxHeight = omschrijvingDiv.attr("rel");

			if (omschrijvingDiv.height() == maxHeight) {
				// slideUp
				omschrijvingDiv.next("div.read_more").show();
				omschrijvingDiv.animate({height: "17px"},400)
			} else {
				// slideDOwn
				omschrijvingDiv.next("div.read_more").hide();
				omschrijvingDiv.animate({height: maxHeight},400);
			}
		});

		$("div.read_more").click(function(event) {

			// slideDOwn
			var omschrijvingDiv = $(this).prev("div.projectoverzicht_omschrijving");
			var maxHeight = omschrijvingDiv.attr("rel");
			$(this).hide();
			omschrijvingDiv.animate({height: maxHeight},400);

		});

	}

	//focus
	if ($("input.specialistinput").length > 0)	{
		$("input.specialistinput").focus();
	}

	if ($("div#contactform_geheel_monster").length > 0)	{
		//check form
		checkMonsterForm();
	}
	
	//check for activate the actieform
	if ($("div.actieForm").length > 0)	{
		handleActieForm();
	}

});

function handleActieForm()
{
	//catch the enter for search
	$("input[name=actiecode]").keypress(function(event) {
		if (event.which == '13') {
			event.preventDefault();
		    $("input.sendCode").trigger("click");
		}
	});
	
	//check the submit
	$("input.sendCode").click(function()	{
		
		//reset the errors
		$("span.wrongCode").hide();
		$("span.oldCode").hide();
		$("img.loadCode").hide();
		$("div.actieFormResult").html('');
		
		//get the value
		var curCode = $("input[name=actiecode]").val();
		if (curCode != 'undefined' && curCode != '')	{
			
			//oke, make the request
			$("img.loadCode").show();
			
			$.ajax({
				url: "/javascript/ajaxRequest/handleCode.php",
				type: "GET",
				dataType: "html",
				data: 'action=checkCode&code='+curCode,	
				complete: function(XMLHttpRequest, textStatus) {
					var data = XMLHttpRequest.responseText;
					$("img.loadCode").hide();
					//check for errors
					if (data == 'e' || data == 'nocode' || data == 'wrongCode')	{
						$("span.wrongCode").show();
					}else if (data == 'oldCode')	{
						$("span.oldCode").show();
					}else	{
						//no error, show the form
						$("div.actieFormResult").html(data);
						checkFormHandler();	
						activateCufon();				
					}
				}
			});
			
			
		}//empty, stop
		
	});
}

function checkFormHandler()
{
	//check all formfields on blur (leaving field)
	if ($("form").length > 0) {
		$("form input, form select").blur(function() {
			$.checkField($(this));
		});

		//check form on submit
		$('form').submit(function() {
			return $.checkForm($(this));
		});
	}

}


$.checkField = function(field)
{
	if (field.hasClass('isReq') && field.val() == '') {
		field.css("border","1px solid red");
		$('#lb_'+field.attr('id')).css('color','red');
		return false;
	} else if ((field.hasClass('email') || field.attr('name') == 'email') && !$.emailValidation(field.val())) {
		field.css("border","1px solid red");
		$('#lb_'+field.attr('id')).css('color','red');
		return false;
	} else {
		field.css("border","1px solid #E6E6E6");
		$('#lb_'+field.attr('id')).css('color','#404040');
	}
	return true;
};

$.emailValidation = function(value) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(value) == false) {
		return false;
	}
	return true;
};


$.checkForm = function(currentForm)
{
	var errorFree = true;
	currentForm.find('textarea, input, select').each(function()	{
		if (!$.checkField($(this))) {
			errorFree = false;
		}
	});
	return errorFree;
};

function activateCufon()
{
	Cufon.replace("span.title_nieuws, div.beurs_item h2, span.huur, span.huur_aanbieding", { fontFamily: "Helvetica Neue LT Std" });
	Cufon.replace("span.huur_aanbieding, span.kortingstekst", { fontFamily: "Helvetica Neue LT Std bold" });
}

function hideError()
{
	if ($("#klantLoginFoutMeldingen").length > 0)	{
		$("#klantLoginFoutMeldingen").hide();
	}
}

function checkMonsterForm()
{
	$("form#mForm").submit(function()	{

		var submitForm = true;
		var errorRow = '<p>';

		//contactpersoon
		if ($("input#contactpersoon").length == 0 || $("input#contactpersoon").val() == '')	{
			var errorRow = errorRow + '- Vul de contactpersoon in<br>';
			$("input#contactpersoon").css("border","1px solid red");
			var submitForm = false;
		}else	{
			$("input#contactpersoon").css("border","1px solid #DDDDDD");
		}

		//adres
		if ($("input#adres").length == 0 || $("input#adres").val() == '')	{
			var errorRow = errorRow + '- Vul uw adresgegevens in<br>';
			$("input#adres").css("border","1px solid red");
			var submitForm = false;
		}else	{
			$("input#adres").css("border","1px solid #DDDDDD");
		}

		//postcode
		if ($("input#postcode").length == 0 || $("input#postcode").val() == '')	{
			var errorRow = errorRow + '- Vul uw postcode in<br>';
			$("input#postcode").css("border","1px solid red");
			var submitForm = false;
		}else	{
			$("input#postcode").css("border","1px solid #DDDDDD");
		}

		//plaats
		if ($("input#plaats").length == 0 || $("input#plaats").val() == '')	{
			var errorRow = errorRow + '- Vul een plaatsnaam in<br>';
			$("input#plaats").css("border","1px solid red");
			var submitForm = false;
		}else	{
			$("input#plaats").css("border","1px solid #DDDDDD");
		}

		//telefoon
		if ($("input#telefoon").length == 0 || $("input#telefoon").val() == '')	{
			var errorRow = errorRow + '- Vul uw telefoonnummer in<br>';
			$("input#telefoon").css("border","1px solid red");
			var submitForm = false;
		}else	{
			$("input#telefoon").css("border","1px solid #DDDDDD");
		}

		//opleiding
		if ($("input#opleiding").length == 0 || $("input#opleiding").val() == '')	{
			var errorRow = errorRow + '- Geef uw opleiding aan<br>';
			$("input#opleiding").css("border","1px solid red");
			var submitForm = false;
		}else	{
			$("input#opleiding").css("border","1px solid #DDDDDD");
		}


		var errorRow = errorRow + '</p>';

		if (submitForm)	{
			return true;
		}else	{
			$("div.errorHolder").html(errorRow);
			return false;
		}

	})

}
