$(function(){
	
	$('#quoteform').submit(function(e){
		var requiredElements = $('.requiredField',this)
		if(!checkRequired(requiredElements)) {
			
			if($(this).hasClass('popupErrors')) {
				 var popupErrorString = 'The following REQUIRED fields have been left empty:' +"\n\n";
				 var fieldCount = 0;
			     $('li.error',this).each(function() {
				 	if ($('input', this).hasClass('requiredField')) {
					   var fieldName = $('label', this).html();
					   fieldCount++;
					   popupErrorString += "\n" + fieldName;
					   //$(this).removeClass('error');
					}
				 });
				 
				 if(fieldCount > 0) {
				    alert(popupErrorString);	
				
				 }
				 
			}
			
            return false;
        }
		
		if(!checkValid($('input[type="text"]',this),'info')) {
			return false;
		}
		return true;
	});
	
	/*$('input[type="text"]','#quoteform').blur(function(e){
		var formType = 'info';
		checkValid($(this),formType);
	});*/
	
	
	// ie6 doesn't understand :focus in the css, so we'll do it here
	if($.browser.msie){
		$('input[type="text"]').focus(function(){
			$(this).addClass('focus');
		});
		$('input[type="text"]').blur(function(){
			$(this).removeClass('focus');
		});
	}
	
	function checkValid(element,formType) {
		if (checkRequired(element)) {
			var arrRegex = {
				'info': {
					'email': "^([a-zA-Z0-9_'+*$%\^&!\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,4})+$",
					'postcode': "^(([A-PR-UWXYZ]{1,2}[0-9]{1,2}[ABEHJMNPRVWXY]?)\\s?([0-9][ABD-HJLNP-UW-Z]{2})|(GIR)\\s?(0AA))$",
					'firstname': "^([a-z\\s\\-']+)$",
					'fullname': "^([a-z\\s\\-']+)$",
					'surname': "^([a-z\\s\\-']+)$",
					'housenum': "^[a-z0-9\\-\\s']+$",
					'phone': "^[0-9\\-\\s]{1,}$"
				
				},
				'call': {
					'email': "^([a-zA-Z0-9_'+*$%\^&!\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,4})+$",
					'name': "^([a-z\\s\\-']+)$",
					'fullname': "^([a-z\\s\\-']+)$",
					'moreInfo': "^[a-z0-9\\-\\s']+$",
					'telephone': "^[0-9\\-\\s]{1,}$"
				}
			}
			
			
			var valid = true;
			
			$(element,'#quoteform').each(function() {
				var parent = $(this).parents('li:nth(0)');
				var field = $(this).attr('name');
				var fieldValue = $(this).val();
				if (fieldValue) {
					var objRegex = new RegExp(arrRegex[formType][field], 'i');
					var matched = objRegex.exec(fieldValue);
					if (matched) {
						$(parent).removeClass('error');
						if(!$('.success', parent).length) {
							$(parent).append('<span class="success"><img src="/images/greentick.png" width="20" /></span>');
						} else {
							$('.success', parent).show();
						}
					} else {
						$(parent).addClass('error');
						if(!($('.fail').length)) {
							  $('.success', parent).hide();
						      valid = false;
						      $(parent).append('<span class="fail"><img src="/images/fail.png" width="20" /></span>');
                        } else {
							$('.fail', parent).show();
						}
					}
				}
			});
		} else {
			valid = false;
		}
		return valid;
	}
	
	function checkRequired(element) {
		var valid = false;
		$(element).each(function() {
			if ($(this).hasClass('requiredField')) {
				var parent = $(this).parents('li:nth(0)');
				if ($(this).val() != '') {
					valid = true;
					$(parent).removeClass('error');
					if ($('.errors').length && !$('.error').length) {
						$('.errors').remove();
					}
				}
				else {
					valid = false
					var currentForm = $(element).parents('ul');
					$(parent).addClass('error');
					if (!$('.errors').length) {
						$(currentForm).prepend('<li class="errors">Please complete these required fields:</li>');
					}
				//return false;
				}
			} else {
				valid = true;
			}
		},valid);
		return valid;
	}
	
});

