$(function(){
	//Form Processing
	$('#nominationsForm').validate({
		rules: {
			name: {
				required: true,
				minlength: 3
			},
			organisation: {
				required: true,
				minlength: 3
			},
			email: {
				required: true,
			},
			categories: {
				required: true
			},
			reason: {
				required: true,
				minlength: 5
			}
		},
		messages: {
		    name: {
				required: "Please enter your nominee's name",
		    	minlength: "Please enter 3 or more characters"
			},
			organisation: {
		    	minlength: "Please enter 2 or more characters"
			},
			number: {
				required: "Please enter your nominee's email address",
		    	email: "Please enter a valid email address"
			},
			categories: {
				required: "Please select an awards"
			},
			reason: {
				required: "Please enter your email",
				minlength: "Please enter more than 5 chacters"
			}
		},
		submitHandler: function() {
			$('#nominationsForm').ajaxSubmit({
				url: 'http://www.hamara.org.uk/scripts/nominationsProcess.php',
				success: function(response) {
                    $('#response').html(response);
                } 	
           	});
		}
	});
});

//CHARACTER COUNTER
window.onload = init;
var maxedOutText = " - You have reached the character limit!";
function init() {
	document.getElementById('countArea').innerHTML = 400-document.getElementById('reason').value.length;
	if(document.getElementById('reason').value.length >= 400) {
		document.getElementById('warning').innerHTML = maxedOutText;
	}
	else {
		document.getElementById('warning').innerHTML = "";
	}	
	colorCheck();
} 

function updateCounter(field,maxlength,id,counter) {
	var totalLength = field.value.length;
	if(totalLength >= maxlength) {
		field.value = field.value.substring(0, maxlength);
		document.getElementById('warning').innerHTML = maxedOutText;
	}
	else {
		document.getElementById('warning').innerHTML = "";
	}	
	document.getElementById(counter).innerHTML = maxlength-field.value.length;
	colorCheck();
} 

function colorCheck() {
	var textfield = document.getElementById('reason');
	var countfield = document.getElementById('countArea');
	if(textfield.value.length < 300) {
		countfield.style.color = "#84ce3a";	// color for good
	}
	else if(textfield.value.length > 350) {
		countfield.style.color = "#a52b2b"; // color for bad
	}
	else {
		countfield.style.color = "#dfa01a"; // color for okay
	}	
}