$(document).ready(function() {
	//Fix little Firefox Issue
	var agent = navigator.userAgent.toLowerCase();
	
	if(agent.indexOf('firefox') != -1){
	
		$('#divinputfile').css('background-position', '100% 4px');
		$('#divinputfile').css('height', '30px');
	}
	//----------------------------------------------------------------------------------------------
	// Form Validation
	//----------------------------------------------------------------------------------------------
	//Turn off default messages from the validator plugin (Note: this does not turn off the error for
	//invalid emails - so I directly changed this error message to "" within the jquery.validate.js file)
	jQuery.validator.messages.required = "";

	//Initalise the validator plugin
	$('form').validate({
		//Rules corresponds to the field ids
		rules: {
			name: { 
				required: true
			}, 
			email: {
				required: true,
				email: true,
				//Messages to be reported (if they were turned on) depending on different fail conditions-
				//Feature not fully tested.
				messages: {
					required: "Required",
					email: "email"
				}
			},
			//filepc: { 
			fakefilepc:{
				required: true
			},
			agree: { 
				required: true
			}
		},
		//Place to put all the error messages (which I've hidden with CSS)
		//errorLabelContainer: "#messageBox",
		//wrapper: "span",
		
		//Function to handle when there are errors when validating the form:
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				//$('ul#messageBox').css('display', 'none');
				
				var message = errors == 1
				? 'Please fill out the highlighted area below before submitting your details'
				: 'Please fill out the ' + errors + ' highlighted areas below before submitting your details';
				$("div#main_error").html(message);
				$("div#main_error").show();
				
					//if($('#agree').hasClass('error')){
					//$('label').attr('for', 'agree').css('border', '1px solid');
					//$('label').attr('for', 'agree').css('color', '#5cc3e4');
					//$('label').attr('for', 'agree').css('font-size', '55px');
					//alert('hello');
				//}
					if ($('#agree:checked').val() == null) {
						$('label#label_agree').css('color', '#5cc3e4');

					}
					else{
					$('label#label_agree').css('color', '#919191');
					}
					
				
			} 
			else {
				$("div#main_error").hide();
		
			}
		}
		
	
	});

	
	//----------------------------------------------------------------------------------------------
	// Input labels
	//----------------------------------------------------------------------------------------------
	
	$("input").keyup(function(){
	
		var id= $(this).attr('id');
		var str_length = $(this).val().length;
	
		if(str_length >= 1){
			$(this).css('background-image', 'url(images/blank_bg.jpg)');
		}
		else{
			$(this).css('background-image', 'url(images/'+ id +'_bg.jpg)');
		}
	});

	$('#divinputfile').focusin(function(){
		$("input#fakefilepc").css('background-image', 'url(images/blank_bg.jpg)');
	});
	
});

