window.addEvent('domready', function() {
	// The hover behaviour for each of the menu items
	$$('.item a').addEvent('mouseover', function() {
		// Trace back to the ID of the DIV element holding this anchor element
		var iItemID = this.get('id').replace('a', '');
		
		// Change the background image of this item
		//$('item' + iItemID).setStyle("background-image", "url('../images/menu/but" + iItemID + "_hover.gif')");
		var iHeight = $('item' + iItemID).getStyle("height");
		$('item' + iItemID).setStyle("background-position", "0 -" + iHeight);
	});
	$$('.item a').addEvent('mouseout', function() {
		// Trace back to the ID of the DIV element holding this anchor element
		var iItemID = this.get('id').replace('a', '');
		
		// Change the background image of this item
		//$('item' + iItemID).setStyle("background-image", "url('../images/menu/but" + iItemID + "_static.gif')");
		$('item' + iItemID).setStyle("background-position", "0 0");
	});


	// Add a Twitter badge inside the footer if it has content
	if ($$('#bottom p')[0]) {
		var linkTwitter = new Element("a", {
			"href": "http://www.twitter.com/detectiondogs", 
			"class": "twitterIcon", 
			"title": "Volg ons op twitter"
		});
		var imgTwitter = new Element("img", {
			"src": "../images/twitter.png", 
			"width": "61", 
			"height": "23", 
			"alt": "Volg ons op twitter"
		});
		linkTwitter.injectInside($$('#bottom p')[0]);
		imgTwitter.injectInside(linkTwitter);
	}


	// Only attempt to add these event handlers if a form is on the page
	if ($$('form')) {
		// Event handlers used on an "Online offerte" page, for providing feedback when
		// the user has set or lost focus on an input field of the form
		$$('.textbox').addEvent('focus', function() {
			this.setStyles({
				border: '1px solid #000000',
				background: '#ffffff none no-repeat scroll 0 0'
			});
		});
		$$('.textbox').addEvent('blur', function() {
			this.setStyle('border', '1px solid #bfb791');
		});
	}

	if ($('formOfferte')) {
		// Catch the submit event of the form and call a validation function
		// which will return true or false for allowing submission
		$('formOfferte').addEvent('submit', fValidateForm);
	}
});


// A function which validates an "Online Offerte" form and displays feedback accordingly
function fValidateForm() {
	// Declare and intialize a counter variable which will hold the
	// amount of errors encountered
	var iErrors = 0;

	// Check all of the implicit fields and if any contain errors, display feedback for each
	if ($('txtBedrijfsnaam').value.length < 1) { $('txtBedrijfsnaam_error').removeClass('hide'); iErrors++; } else { $('txtBedrijfsnaam_error').set("class", "error hide"); }
	if (($('txtNaam').value.length < 1) || (!($('formOfferte').rdoGeslacht[0].checked) && !($('formOfferte').rdoGeslacht[1].checked))) { $('txtNaam_error').removeClass('hide'); iErrors++; } else { $('txtNaam_error').set("class", "error hide"); }
	if (($('txtStraat').value.length < 1) || ($('txtHuisnr').value.length < 1)) { $('txtStraat_error').removeClass('hide'); iErrors++; } else { $('txtStraat_error').set("class", "error hide"); }
	if (($('txtPostcode').value.length < 1) || ($('txtPlaats').value.length < 1)) { $('txtPostcode_error').removeClass('hide'); iErrors++; } else { $('txtPostcode_error').set("class", "error hide"); }
	if ($('txtEmail').value.length < 1) { $('txtEmail_error').removeClass('hide'); iErrors++; } else { $('txtEmail_error').set("class", "error hide"); }
	if ($('txtTelnr').value.length < 1) { $('txtTelnr_error').removeClass('hide'); iErrors++; } else { $('txtTelnr_error').set("class", "error hide"); }
	if ($('cmbSrtAanvraag').value == "0") { $('cmbSrtAanvraag_error').removeClass('hide'); iErrors++; } else { $('cmbSrtAanvraag_error').set("class", "error hide"); }

	// Check if there were any errors and if not, allow the form to submit
	if (iErrors == 0) {
		return true;
	} else {
		return false;
	}
}