$j(document).ready( init );


function init()
{
	// NOTE: IE and Opera don't support border-radius,
	// so we use jquery-round-corners-canvas plugin
	// from http://jrc.meerbox.nl/?page_id=4
	if ( $j.browser.msie || $j.browser.opera )
	{
		$j('#header').corner('4px');
		$j('#header #mainmenu.nav').corner('top 4px');
		$j('#header #navPrimary.nav').corner('top 4px');
		$j('#header #mainmenu.nav li').corner('bottom 4px');
		$j('#header #navPrimary.nav li').corner('bottom 4px');
		$j('#company-blocks ul li').corner('4px');
	}


	if ( $j('#contact-form').length > 0 )
	{
		$j('#contact-form').submit( validateContactForm );
	}

	if ( $j('#cv-form').length > 0 )
	{
		$j('#cv-form').submit( validateCvForm );
	}
}



function validateContactForm()
{

	var Errors = new Array();

	if ( $j(':checkbox[name=company]:checked').length == 0 )
	{
		Errors.push( { field:'company' , msg:'Please select at least one company you would like information about.' } );
	}

	$j('input:text,textarea,select')
		.each
		( function()
			{
				if ( $j(this).parent().is('.required') && $j(this).val() == '' )
				{
					var ActionText = $j(this).is('select') ? 'select' : 'enter' ;
					var LabelText = $j(this).prev('label').text().toLowerCase().replace(/^your |:$/g,'');

					Errors.push( { field:this.id , msg:'Please '+ActionText+' your '+LabelText+'.' } );
				}
			}
		);


	if ( $j('#email').val() != '' && ! $j('#email').val().match(/[^@]+@[^@]+/) )
	{
		Errors.push( { field:'email' , msg:'Please check you have entered a valid email address.' } );
	}

	if ( $j('#message').val().length > 250 )
	{
		Errors.push( { field:'message' , msg:'Please shorten your message. Maximum length '+$j('#message').attr('maxlength')+' characters.' } );
	}


	if (Errors.length == 0)
	{
		return true;
	}
	else
	{
		displayErrors(Errors);
		return false;
	}
}




function validateCvForm()
{
	var Errors = new Array();


	$j('input:text,textarea,select')
		.each
		( function()
			{
				if ( $j(this).parent().is('.required') && $j(this).val() == '' )
				{
					var ActionText = $j(this).is('select') ? 'select' : 'enter' ;
					var LabelText = $j(this).prev('label').text().toLowerCase().replace(/^your |\*?:$/g,'');

					Errors.push( { field:this.id , msg:'Please '+ActionText+' '+LabelText+'.' } );
				}
			}
		);


	if ( $j('#email').val() != '' && ! $j('#email').val().match(/[^@]+@[^@]+/) )
	{
		Errors.push( { field:'email' , msg:'Please check you have entered a valid email address.' } );
	}


	if (Errors.length == 0)
	{
		return true;
	}
	else
	{
		displayErrors(Errors);
		return false;
	}
}




function displayErrors(Errors)
{
	/*
	  INFO: Create or clear any existing errors.
	 */
	if ( $j('#errors').length == 0 )
	{
		$j('<ul id="errors"></ul>').insertBefore('fieldset.controls');
	}
	else
	{
		$j('#errors').empty();
	}

	// TODO: Consider doing highlightField( Errors[i].field ) on hover
	/*
	 INFO: Appends each error message.
	 */
	for ( i = 0; i < Errors.length; i++ )
	{
		$j('#errors').append('<li>'+Errors[i].msg+'</li>');
	}
}
