$( function()
{
	// Execute the function after defining it
	fixFields('form');
	fixFields('solform');
	
	// Locatie switch
	$('input[type="radio"][name=locatie]').bind( 'click', function()
	{
		// Get current selection
		var sel = $(':radio[name=locatie]:checked').val();
		
		// Show correct div
		$('#locatie_thuis_form, #locatie_showroom_form, #aanvraag_informatie_form, #aanvraag_offerte_form').hide();
		$('#'+sel+'_form').show();
	});
	
	
	// Type aanvraag switch
	$('input[type="radio"][name=aanvraag]').bind( 'click', function()
	{
		// Get current selection
		var sel = $(':radio[name=aanvraag]:checked').val();
		
		// Show aanvraag informatie form
		$('#aanvraag_form').show();
		
		// Show correct text
		$('#aanvraag_informatie_tekst, #aanvraag_offerte_tekst').hide();
		$('#'+sel+'_tekst').show();
	});
	
	
	// Submit action
	$('.submit').click( function() {
		$('form[name=form]').submit();
		$('form[name=solform]').submit();
		return false;
	});
	
	// Catch form submition
	$('form[name=form]').submit( function()
	{
		// Set status
		var state = true;
		
		// Set error msg
		var errorMsg = 'De volgende velden zijn nog niet ingevuld:\n';
		
		// Empty fields that are similiar to their rel attributes
		$(':input', this).each( function() {
			if ( $(this).val() == $(this).attr('rel') )
				$(this).val ( '' );
		});
		
		// Check required fields
		$(':input.required').each( function()
		{
			// Set state to false if empty and add the field name
			if ( $(this).attr('id') == 'telefoon' && /\d/.test($(this).val()) == false )
			{
				// Get id
				var id = $(this).attr('id')
				
				// Get friendly fieldname
				var fname = $('label[for="'+id+'"]').text() || $(this).attr('rel');
				
				// Set error msg
				errorMsg = errorMsg+'- '+fname+'\n';
				
				// Set state to false
				state = false;
			}
			else if ( $(this).val() == '' || $(this).val() == $(this).attr('rel') )
			{
				// Get id
				var id = $(this).attr('id')
				
				// Get friendly fieldname
				var fname = $('label[for="'+id+'"]').text() || $(this).attr('rel');
				
				// Set error msg
				errorMsg = errorMsg+'- '+fname+'\n';
				
				// Set state to false
				state = false;
			}
		});
		
		// Do correct actions on false state
		if ( ! state )
		{
			alert(errorMsg);
			fixFields('form');
		}
		else
			// Disable submit button
			$('.submit')
				.attr( 'disabled', 'disabled' )
				.text( 'Een ogenblik geduld a.u.b.' );
		
		return state;	
	});
	
	// Catch form submition
	$('form[name=solform]').submit( function()
	{
		// Set status
		var state = true;
		
		// Set error msg
		var errorMsg = 'De volgende velden zijn nog niet ingevuld:\n';
		
		var radios = new Array();
		
		// Empty fields that are similiar to their rel attributes
		$(':input', this).each( function() {
			if ( $(this).val() == $(this).attr('rel') )
				$(this).val ( '' );
		});
		
		// Check required fields
		$('.required').each( function()
		{
			// Set state to false if empty and add the field name
			if ( $(this).attr('id') == 'telefoon' && /\d/.test($(this).val()) == false )
			{
				// Get id
				var id = $(this).attr('id')
				
				// Get friendly fieldname
				var fname = $('label[for="'+id+'"]').text() || $(this).attr('rel');
				
				// Set error msg
				errorMsg = errorMsg+'- '+fname+'\n';
				
				// Set state to false
				state = false;
			}
			else if ( $(this).is(':radio') )
			{
				// Get name of the current radio range
				var cname = $(this).attr('name');
				
				// Check if one is checked
				if ( !radios.toString().indexOf(cname) && $(':radio[name="'+cname+'"]:checked').length < 1 )
				{
					var fname = cname;
					
					// Set error msg
					errorMsg = errorMsg+'- '+fname+'\n';
				
					// Set state to false
					state = false;
				}
				
				// Add the current name
				radios.push(cname);
			}
			else if ( $(this).val() == '' || $(this).val() == $(this).attr('rel') )
			{
				// Get id
				var id = $(this).attr('id')
				
				// Get friendly fieldname
				var fname = $('label[for="'+id+'"]').text() || $(this).attr('rel');
				
				// Maybe get the fname from the first option?
				if (typeof fname == 'undefined' && $(this).hasClass('isaselect'))
				{
					fname = $('option:first', this).text();
				}
				
				// Set error msg
				errorMsg = errorMsg+'- '+fname+'\n';
				
				// Set state to false
				state = false;
			}
		});
		
		// Do correct actions on false state
		if ( ! state )
		{
			alert(errorMsg);
			fixFields('solform');
		}
		else
			// Disable submit button
			$('.submit')
				.attr( 'disabled', 'disabled' )
				.text( 'Een ogenblik geduld a.u.b.' );
		
		return state;	
	});
	
	// Loop through the hidden fields
	$('form[name=solform] input[type="hidden"]').each(function()
	{
		// Only continue if the current field has a value
		if ($(this).val() != '')
		{
			var fieldvalue = $(this).val();
			
			// Get the name of the current field
			var fieldname = $(this).attr('name');
			fieldname = fieldname.substr(3, fieldname.length);
		
			// Get the field to fill
			var field = $(':input[name="'+fieldname+'"]');
		
			// Select correct field
			if ( !field.is(':radio') )
				$('*[value="'+fieldvalue+'"]', field).attr('selected', 'selected');
			else
				$(':radio[value="'+fieldvalue+'"]').attr('checked', 'checked');
		}
	});
	
	// Fix fields which are filled in
	$('form[name=solform] input[type=text], form[name=solform] textarea').each( function() {
		if ($(this).val() != '') {
			var id = $(this).attr('id');
			$('label[for="'+id+'"]').addClass('has-text');
		}
	});
	
});

function fixFields(form)
{
	// Correct function for input fields
	$('form[name='+form+'] input[type=text], form[name='+form+'] textarea').each( function()
	{
		// Set alias
		var t = $(this);
		
		// Fix all the standard texts
		if ( t.val() == '' )
			t.addClass('greyed').val( t.attr('rel') );
			
		// Do correct action on focus and blur
		t.focus( function() {
			if ( $(this).hasClass('greyed') )
				$(this).removeClass('greyed').val('');
		});
		t.blur( function() {
			if ( $(this).val() == '' )
				$(this).addClass('greyed').val( $(this).attr('rel') );
		});
	});
}
