errors = new Array();
formFields = null;
errorField = null;
thankYouField = null;

$(document).ready(function(){
						 
	
		formFields = $('#awardForm :text, #awardForm textarea');
		errorField = $('#errorField');
		thankYouField = $('#thankYouField');
		
		initErrors();
		
		//console.log(errors);
		
		formInit();

		var loader = $('#loader');
		
		$('#awardForm').bind('submit', function(e)
		{
			e.preventDefault();
			
			formFields.trigger('blur');
						
			if(errors.length > 0 )
			{
				//if there are errors on the page
				displayErrorField("There are errors on the form.", 3000);
	
			} else {
				
				//so there is no errors on the page, now we can attempt form submission
				
				loader.fadeIn(300);
				errorField.hide();
				
				$.post('award1.php', {ajax: true, sName: $('#sName').val(), sEmail: $('#sEmail').val(), nominee: $('#nominee').val(), sMessage: $('#sMessage').val() }, function(data){
					
						if(data.result == 'success')	
						{
							clearForm();
							displayThankYouField("", 5000);						
						} else {							
							displayErrorField("Could not submit form. Please try again.", 7000);
						}
						
						loader.hide();
												
				}, "json");
			}						
		});
	});
	
// setup field validation on blur event
function formInit()
{
	formFields.blur(function()
	{
		hideThankYouField(500);
		var field = $(this);
		validate(field);
	});
}

function validate(field)
{
	
	var error = field.siblings('span.error');
	var value = $.trim(field.val());
	var req = field.hasClass('req');
	var p = $(error.parent().get(0));
		
	switch(field.attr('rel'))
	{
		case 'email':
			
			field.siblings('span.validationcheck').hide();
			
			if( req && (value == ""))
			{
				p.addClass('erroneous');
				error.text("Please enter your email address").hide().fadeIn("slow");
				trackErrors(field, true);
			}else if (!(/^([a-zA-Z0-9_-]+\.?)+@([a-zA-Z0-9_-]+\.)+[a-zA-Z]{2,4}$/.test(value))){
				p.addClass('erroneous');
				error.text("Should look like an email address.");
				error.hide().fadeIn("slow");
				trackErrors(field, true);
			} else {
				error.fadeOut("slow", function(){
					p.removeClass('erroneous');
					field.siblings('span.validationcheck').show();
				});
				
				trackErrors(field, false);
			}
			break;
			
		case "name":
			
			field.siblings('span.validationcheck').hide();
			
			if( req && (value == ""))
			{
				p.addClass('erroneous');
				error.text("Please enter name").hide().fadeIn("slow");
				trackErrors(field, true);
			} else {
				error.fadeOut("slow", function(){
					p.removeClass('erroneous');
					field.siblings('span.validationcheck').show();						   
				});
				
				trackErrors(field, false);
			}
			break;
			
		case "fullname":
		
			field.siblings('span.validationcheck').hide();
			
		
			if( req && (value == ""))
			{
				p.addClass('erroneous');
				error.text("Please enter your full name").hide().fadeIn("slow");
				trackErrors(field, true);
			} else {
				error.fadeOut("slow", function(){
					p.removeClass('erroneous');
					field.siblings('span.validationcheck').show();						   
				});
				
				trackErrors(field, false);
			}
			break;
			
		case "message":
			
			field.siblings('span.validationcheck').hide();
						
			if( req && (value == ""))
			{
				p.addClass('erroneous');
				error.text("Message cannot be blank").hide().fadeIn("slow");
				trackErrors(field, true);
			} else {
				error.fadeOut("slow", function(){
					p.removeClass('erroneous');	
					field.siblings('span.validationcheck').show();					   
				});
				
				trackErrors(field, false);
			}
			break;
			
		default:
			
			field.siblings('span.validationcheck').hide();
			
			if( req && (value == ""))
			{
				p.addClass('erroneous');	
				error.text("This is a reqiured field").hide().fadeIn("slow");
				trackErrors(field, true);
			} else {
				error.fadeOut("slow", function(){
					p.removeClass('erroneous');	
					field.siblings('span.validationcheck').show();					   
				});
				
				trackErrors(field, false);
			}
	}
}

function displayErrorField(txt, delay)
{
	if ( txt == undefined ) txt = "There was an error.";
	if ( delay == undefined ) delay = 3000;
	
	errorField.text(txt).fadeIn('slow', function(){
	  //timeout auto fadeout
	  setTimeout( function(){ errorField.fadeOut(1500)}, delay );
	});
}

function displayThankYouField(txt, delay)
{
	 if( $.trim(thankYouField.children('span').text()) == "" ) 
	 { 
	 	if ( txt == undefined ) txt = "Thank you for your feedback!"; 
	 } else {
		txt = $.trim(thankYouField.children('span').text());
	 }
	
	if ( delay == undefined ) delay = 3000;
	
	thankYouField.children('span').text(txt);
	thankYouField.fadeIn('slow', function(){
	  //timeout auto fadeout
	  setTimeout( function(){ thankYouField.fadeOut(1500); }, delay );
	});
}

function hideThankYouField(delay)
{
	if(delay == undefined) delay = 500;
	thankYouField.fadeOut(delay);
}

function trackErrors(field, isError)
{
	//errors array already contains this field and this field no longer has an error
	var index = $.inArray(field.attr('name'), errors);
	
	if( index  >= 0 )
	{
		if(!isError)
		{
			//so we remove it from errors array
			errors.splice(index, 1);
		}
	} else {
		
		if(isError)
		{
			var name = field.attr('name');
			errors.push(name);	
		}
	}
}

function clearForm()
{
	formFields.val('').siblings('span.validationcheck').hide();	
	errors = new Array();
	initErrors();
}

function initErrors()
{
	formFields.each(function()
	{
		errors.push($(this).attr('name'));
	});	
}