
var lastrel = '';

$(document).ready(function() {
	$('form select').change(function() {
									 
		var selected = $(this).val();
		
		var rel = '';
	
		$(this).children('option').each(function() {
			if($(this).val() == selected) {
				var attr = $(this).attr('rel');
				if (typeof attr !== 'undefined' && attr !== false) {
					rel = attr;
				}
			}
		});
		
		if (rel != '') {
			$('#'+rel).attr('disabled', 'disabled');
			lastrel = rel;
		} else if (lastrel != '') {
			$('#'+lastrel).attr('disabled', '');	
		}
		//$('.tempdisplay').html(rel+'!!!!!');
	
	});
	
	$('form').live('submit', function() 
	{
		if (!validation($(this)))
		{
			return false;
		}
		else
		{
			return;	
		}
	});
});

function validation(formname) {

	var error = "";
	var focusThis = "";
	
	formname.find('.validate').css('border-color', '#666666').each(function() {
		if ($(this).is('select')) {
			if ($(this).attr('disabled') === false) {
				if ($(this).attr("selectedIndex") < 1) {
					error += "Select an option in the '"+$(this).attr('Name')+"' drop down.\n";
					$(this).css('border-color', '#CC0000');
				}
			}
		} else {
			if ($(this).attr('name').toLowerCase() == 'email') {
				if (IsEmail($(this).val())) {
				} else {
					if ($(this).val() === '') {
						error += 'You must enter a valid email address\n';
						$(this).css('border-color', '#CC0000');
					} else {
						error += "The email address you entered is not valid\n";
						$(this).css('border-color', '#CC0000');
					}
				}
			} else if ($(this).attr('name').toLowerCase() == 'phone') {
				if ($(this).val().replace(/ /g,'').length	 < 8) {
					error += "The '"+$(this).attr('name')+"' field must contain at least 8 numbers.\n";	
					$(this).css('border-color', '#CC0000');
				}
			} else {
				if ($(this).val() === '') {
					error += "The '"+$(this).attr('name')+"' field cannot be left blank.\n";	
					$(this).css('border-color', '#CC0000');
				} /**/
			}
		}
	});
	
	if (error != "") { alert("Please fix the following errors:\n\n"+error); return false; } 
	
	return true;
	
}

function IsEmail(email) {
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return regex.test(email);
}
