function highlight_field(id, has_error)
{
	var thisid = '#' + id;
	if ( has_error )
	{
		$(thisid).removeClass('goodInput');
		$(thisid).addClass('errorInput');
	}
	
	else
	{
		$(thisid).removeClass('errorInput');
		$(thisid).addClass('goodInput');
	}
}

function validate(type, id, name)
{
	var returntext = '';
	var thisid = '#' + id;
	
	switch (type)
	{
		case 'string':
			if ( $(thisid).val().length < 1 )
			{
				returntext = 'This is a required field.';
				highlight_field(id, true);
			}
			
			else
			{
				highlight_field(id, false);
			}
		break;
		
		case 'price':
			if ( $(thisid).val().length < 1 )
			{
				returntext = 'This is a required field.';
				highlight_field(id, true);
			}
			
			else
			{
				// Not blank but is it numeric?
				var regexp = /^[0-9\.]{1,}$/;
				var text = $(thisid).val();
				
				if ( text.match(regexp) )
				{
					highlight_field(id, false);
				}
				
				else
				{
					returntext = 'Please enter only numbers in this field.';
					highlight_field(id, true);
				}
			}
		break;
		
		case 'numeric':
			if ( $(thisid).val().length < 1 )
			{
				returntext = 'This is a required field.';
				highlight_field(id, true);
			}
			
			else
			{
				// Not blank but is it numeric?
				var regexp = /^[0-9]{1,}$/;
				var text = $(thisid).val();
				
				if ( text.match(regexp) )
				{
					highlight_field(id, false);
				}
				
				else
				{
					returntext = 'Please enter only numbers in this field.';
					highlight_field(id, true);
				}
			}
		break;
		
		case 'creditcard':
			if ( $(thisid).val().length < 1 )
			{
				returntext = 'This is a required field.';
				highlight_field(id, true);
			}
			
			else
			{
				// Not blank but is it numeric?
				var regexp = /^[0-9 ]{1,}$/;
				var text = $(thisid).val();
				
				if ( text.match(regexp) )
				{
					highlight_field(id, false);
				}
				
				else
				{
					returntext = 'Please enter just your credit card number in this field (spaces are allowed).';
					highlight_field(id, true);
				}
			}
		break;
		
		case 'zip':
			var regexp = /^[0-9]{5}$/;
			var text = $(thisid).val();
			
			if ( text.match(regexp) )
			{
				highlight_field(id, false);
			}
			
			else
			{
				returntext = 'Please enter a 5 digit ZIP code.';
				highlight_field(id, true);
			}
		break;
		
		case 'phone':
			var text = $(thisid).val();
			text = text.replace(/[ \(\)\-]/g, '');
			var regexp = /^[0-9]{10}$/;
			
			if ( text.match(regexp) )
			{
				highlight_field(id, false);
			}
			
			else
			{
				returntext = 'Please enter a 10 digit phone number including area code.';
				highlight_field(id, true);
			}
		break;
		
		case 'email':
			var regexp = /^[a-z0-9\.\-_]{1,}@[a-z0-9\.\-_]{2,}\.[a-z0-9\.\-_]{2,}$/;
			var text = $(thisid).val();
			
			if ( text.match(regexp) )
			{
				highlight_field(id, false);
			}
			
			else
			{
				returntext = 'Your email address appears to be invalid.';
				highlight_field(id, true);
			}
		break;
		
		case 'username':
			var regexp = /^[A-Za-z0-9]{8,32}$/
			var text = $(thisid).val();
			
			if ( text.match(regexp) )
			{
				highlight_field(id, false);
			}
			
			else
			{
				returntext = 'Usernames must contain both letters and numbers and be between 8 and 32 characters in length.';
				highlight_field(id, true);
			}
		break;
		
		case 'password':
			if ( $(thisid).val().length < 8 )
			{
				returntext = 'Your password is too short.';
				highlight_field(id, true);
			}
			
			else
			{
				highlight_field(id, false);
			}
		break;
		
		case 'select':			
			if ( $(thisid).val() == '' )
			{
				returntext = 'Please select an option.';
				highlight_field(id, true);
			}
			
			else
			{
				highlight_field(id, false);
			}
		break;
		
		case 'radio':
			// Now just loop through and see if any are checked
			var jqueryname = "input[type='radio'][name='" + name + "']";
			var checked = false;
			
			$(jqueryname).each(function() {
				if ( $(this).attr('checked') )
				{
					checked = true;
				}
			});
			
			if ( checked == true )
			{
				returntext = '';
			}
			
			else
			{
				returntext = 'Please select an option.';
			}
		break;
		
		case 'checkbox':
			// Create a loop to go through all of these checkboxes
			var jqueryname = "input[type='checkbox'][name='" + name + "']";
			var checked = false;
			
			$(jqueryname).each(function() {
				if ( $(this).attr('checked') )
				{
					checked = true;
				}
			});
			
			if ( checked )
			{
				returntext = '';
			}
			
			else
			{
				returntext = 'Please select at least one option.';
			}
		break;
		
		default:
		// do nothing
	}
	
	return returntext;
}

$(document).ready(function(){
	// Loop through all input elements
	$('.required').each( function() {
		var thisname = $(this).attr("name");
		var thisclass = $(this).attr("className");
		var inputtype = $(this).attr("type");
		var classnames = thisclass.split(' ');
		var thisid = $(this).attr("id");
		
		for ( var i in classnames )
		{
			if ( classnames[i] != 'required' )
			{
				if ( inputtype == 'radio' || inputtype == 'checkbox' )
				{
					$(this).blur(function() {
						// Go up one to the p tag then down to the span
						var elm = $(this).closest('p').children('span').get(0);
						$(elm).text( validate(classnames[i], thisid, thisname) );
					});
				}
				
				else
				{
					$(this).blur(function() {
						$(this).next('span').text( validate(classnames[i], thisid, thisname) );
					});
				}
			}
		}
	});
});
