function clearError(id, error, variable){
	$(id).focus(function(){
   		$(error).html('');
		$(this).css('background-color', '#666666');
		variable = false;
    });
}

function nan(id, error, test){
	var x = $(id).val();
	x = parseFloat(x);
	
	if(isNaN(x) == true){
		$(id).val(0)
		$(error).html('Please only insert<br />numeric values.');
		$(id).css('background-color', '#484848');
		test = false;
	} else {
		test = true;
	}
}

function resetStyle(id, error){
	$(error).html('');	  
	$(id).css('background-color', '#FFFFFF');
}

$(document).ready(function(){	
		   
	/*					   
	$('#nameInput').focus(function(){
		$('#nameError').html('');
		$(this).css('background-color', '#666666');
		nameValid = false;
    });
	*/
						   
						   
						   
//MAILER VALIDATION------------------------------------------------
	//initialise---------------------------------------------------
	$('#submitDiv').html('<span id="submit">Submit</span>');
	
	$('#submit').mouseover(function(){
			this.style.cursor='pointer';
	});
	
	$('#submit').mouseup(function(){									
		//name validation------------------------------------------						  
		var name = $('#nameInput').val();
		
		if(name == ''){
			$('#nameError').html('Required field missing');
			$('#nameInput').css('background-color', '#484848');
		}
		else {
			var nameValid = true;
		}
		
		//clear error when focused
		//clearError('#nameInput', '#nameError', nameValid);	
		
		//email validation-----------------------------------------
		var email = $('#emailInput').val();
		var atSymbol = email.indexOf('@');
		var dot = email.indexOf('.');
		var lastDot = email.lastIndexOf('.');
		var length = (email.length)-1;
		var secondAt = email.indexOf('@', (atSymbol+1));
		
		if(email == ''){
			$('#emailError').html('Required field missing');
			$('#emailInput').css('background-color', '#484848');
		} 
		else if (atSymbol < 0){
			$('#emailError').html('Error: no @ character');
			$('#emailInput').css('background-color', '#484848');
		}
		else if (atSymbol == 0){
			$('#emailError').html('Error: cannot begin with @');
			$('#emailInput').css('background-color', '#484848');
		}
		else if (dot < 0){
			$('#emailError').html('Error: no . character');
			$('#emailInput').css('background-color', '#484848');
		}
		else if (lastDot < atSymbol){
			$('#emailError').html('Error: . is before @');
			$('#emailInput').css('background-color', '#484848');
		}
		else if (lastDot >= length){
			$('#emailError').html('Error: no text after .');
			$('#emailInput').css('background-color', '#484848');
		}
		else if (secondAt > 0){
			$('#emailError').html('Error: more than one @ character');
			$('#emailInput').css('background-color', '#484848');
		}
		else {
			var emailValid = true;
		}
		
		//comments validation-------------------------------------------------
		var comment = $('#commentInput').val();
		
		if(comment == ''){
			$('#commentError').html('Required field missing');
			$('#commentInput').css('background-color', '#484848');
		}
		else {
			var commentValid = true;
		}
		
		//clear styles on focus-------------------------------------------------
		clearError('#nameInput', '#nameError', nameValid);
		clearError('#emailInput', '#emailError', emailValid);
		clearError('#commentInput', '#commentError', commentValid);
		
		//submit call--------------------------------------------------------
		if((nameValid == true) && (emailValid == true) && (commentValid == true)){
			document.mailForm.submit();
		}
		
	});	
});

