$(document).ready(function(){

	$("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;
			}
		});	
	});/**/
	
	
	if ($("#qoute_form #postalcode").length > 0) {
		$("#qoute_form #postalcode").blur(checkpostvalues);
		$("#qoute_form #housenumber").blur(checkpostvalues);
	}
	
});
/* --------------------------------------------------------------------------- */

function checkpostvalues() {
	if ($("#qoute_form #postalcode").val() != "" && $("#qoute_form #housenumber").val() != "") {
		$.ajax({
			type: "GET",
			url: "postcode/start.php",
			data: "postcode=" + $("#qoute_form #postalcode").val() + $("#qoute_form #housenumber").val(),
			success: function(msg){
				if (msg.getElementsByTagName('straatnaam')[0].firstChild) {
					$("#straatnaam").val(msg.getElementsByTagName('straatnaam')[0].firstChild.nodeValue);
					$("#woonplaats").val(msg.getElementsByTagName('woonplaats')[0].firstChild.nodeValue);
					//document.create_account.city.value = xmldoc.getElementsByTagName('woonplaats')[0].firstChild.nodeValue;
					//document.create_account.city.value = xmldoc.getElementsByTagName('woonplaats')[0].firstChild.nodeValue;
				}			
			}
		});
	}
}

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;
	}
}
