// JavaScript Document
$(document).ready(function(){
	var valid = true;

	$("div.gallery ul.thumbs_list:not(:eq(0))").hide();
   	$("div.gallery h3")
		.css({"cursor":"pointer"})
		.click(function(){
			$(this).next().toggle("slow");
   		})
		.hover(function(){
			$(this).css("color","blue");
		},function(){
			$(this).css("color","#333333");
		})
	.end();

	$(".gallery:odd").css({background: "#EFEFEF"});	
	
	if (document.getElementById("contact_form")){
		
		$(document.getElementById("contact_form")).submit(function(){
			
			$.getScript("http://www.kraakbv.nl/javascript/validate.js");
			
			setTimeout(function(){
							
				$(":input",document.getElementById("contact_form")).each( function(){
								
					if (!validateField(this))
						valid = false;
							
				});
								
				if (!valid) {
					//Send form via ajax
				}	
				
			},300);
			
			// If javascript is enabled then don't send form
			return false;
		});
	
	}
});

function validateField(elem) {
    var errors = [];

    // Go through all the possible validation techniques
    for ( var name in errMsg ) {
        // See if the field has the class specified by the error type
        var re = new RegExp("(^|\\s)" + name + "(\\s|$)");

        // Check to see if  the element has the class and that it passes the
        // validatino test
        if ( re.test( elem.className ) && !errMsg[name].test( elem ) )
            // If it fails the validation, add the error message to list
            errors[errors.length] = errMsg[name].msg;
    }

    // Show the error messages, if they exist
    if ( errors.length )
        showErrors(elem, errors);

    // Return false if the field fails any of the validation routines
    return errors.length > 0;					
}

function showErrors(elem, errors){

	for (var i = 0; i < errors.length; i++) {
		alert(errors[i]);	
	}
	
}

