$(document).ready(function(){
	$("#log form .log_box").each(function() {
		$(this).attr("oldval", $(this).val());
		$(this).click(function(){
			$(this).val("");
		});
		$(this).blur(function(){
			if ($(this).val() == "") {
				$(this).val($(this).attr("oldval"));
			}
		});
	});
	
	$("#log form .log_button").mouseover(function(){$(this).css("background", "transparent url(assets/images/zoek_hover.gif) no-repeat top left");})
	$("#log form .log_button").mouseout(function(){$(this).css("background", "transparent url(assets/images/zoek.gif) no-repeat top left");})	

	$("form[name=contactus]").each(function(){		
		$(this).find("input").each(function(){
			$(this)
				.val( $(this).attr('alt') )
				.click(function(){
					if($(this).val() == $(this).attr('alt')) {
						$(this).val('');
					}
				})
				.blur(function(){
					if($(this).val() == '') {
						$(this).val($(this).attr('alt'));
					}
				});
		});
		$(this).find("textarea").each(function(){
			$(this)
				.val( $(this).attr('label') )
				.click(function(){
					if($(this).val() == $(this).attr('label')) {
						$(this).val('');
					}
				})
				.blur(function(){
					if($(this).val() == '') {
						$(this).val($(this).attr('label'));
					}
				});
		});		
		$(this).submit(function(){			
			var err = false;

			$(this).find("input[title=required]").each(function(){
				if(change_border($(this))) err = true;
			});

			$(this).find("textarea[title=required]").each(function(){								
				if(change_border($(this))) err = true;
			});
			
		
			if(err) {
				return false;
			}
			else {
				return true;
			}
		});	
	});/**/
	
	getImage();
	
	if ($("form.validate").length > 0) startValidation();
});
/* --------------------------------------------------------------------------- */

function change_border(el) {
	if( ((el.val() == "") || (el.val() == el.attr('alt'))) && (el.attr('type') == 'text') ) {
		el.css("border-bottom", "1px dotted #B11C0F");
		return true;

	} else if( ((el.val() == "") || (el.val() == el.attr('label'))) && (el.attr('label') != '') ) {
		el.css("border-bottom", "1px dotted #B11C0F");
		return true;

	} else {
		if(el.attr('name') == 'email') {
			if(validateemail(el)) {
				el.css("border-bottom", "none");				
				return false;
			}
			else {
				el.css("border-bottom", "1px dotted #B11C0F");
				return true;
			}
		}
		else {
			el.css("border-bottom", "none");
			return false;
		}
	}
}

function validateemail(el) {
	var matchArray = el.val().match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);
	
	if (matchArray == null) {
		return false;
	} else {
		return true;
	}
}

function startValidation() {
	$("form.validate").each(
		function (i, val) {			
			$(this).find(":text").each(
				 function () {
					if ($(this).val() != "") $(this).attr("oldval", $(this).val());
					
					$(this).blur(function() {
						if ($(this).hasClass("required")) {
							if ($(this).hasClass("valid")) $(this).removeClass("valid");
							if ($(this).hasClass("invalid")) $(this).removeClass("invalid");
							
							if ($(this).val() == "") { 
								$(this).addClass("invalid");
							} else if ($(this).hasClass("emailadd")) {
								var tEmail = new REgExp("\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$");
								if (!tEmail.test($(this).text)) $(this).addClass("invalid");
							} else {
								$(this).addClass("valid");
							}
						}
						
						if ($(this).attr("oldval") != "") {
							if ($(this).val() == "") {
								$(this).val($(this).attr("oldval"));
							}
						}
					});
					
					$(this).focus(function() {
						if ($(this).val() == $(this).attr("oldval")) {
							$(this).val("");
						}
					});					
				}
			);
			
			myform = $(this);
	
			$(this).submit(function() { 
				if ($(this).hasClass("callback")) {
					if (validateChildren(this)) {
						frmurl = $(this).attr("action");
						frmdata = $(this).serialize();
						$.ajax({
							type: "POST",
							url: frmurl,
							data: frmdata,
							success: function(msg) {
								if (msg == "success") {
									myform.fadeOut("normal", function(){$("#sentmessage").html("Thank you!<br/>Message Sent!").fadeIn();});
								} else {
									myform.fadeOut("normal", function(){
										$("#sentmessage").html("Error sending! <a href=\"#\">Try again</a>.").fadeIn();
										$("#sentmessage a").click(function(){
											$("#sentmessage").fadeOut("normal", function(){myform.fadeIn("normal");});
											return false;
										});
									});									
								}
							}
						});
					} 
					return false;
				} else {				
					return validateChildren(this);
				}
			});		
		}
	);
}

function validateChildren(myForm) {
	if ($(myForm).find(".required").length > 0) {
		if ($(myForm).find(".valid").length == $(myForm).find(".required").length) {
			return true;
		} else {
			$(myForm).find(".required").each(
				function() {
					if ($(this).val() == "") $(this).addClass("invalid");
				}
			);
			return false;
		}
	} else {
		retval = true;
		$(myForm).find("input[type='text']").each(function(){
			if ($(this).val() == $(this).attr("oldval")) retval = false;			
		});		
		return retval;
	}
}
