// only checks to make sure required fields are filled in
//   -- fields that don't validate will still be sent through
function validate_contact_form(form) {
	
	// get the required fields
	var fields = $(form).find('.required_field');

	// assume it's valid
	var valid = true;
	
	// test each field
	fields.each( function () {
	
		// put cell back to normal color
		$(this).removeClass('required');
		
		// check if the field is empty
		if ( $.trim($(this).val()).length == 0 ) {
			$(this).val(''); // get rid of the spaces
			valid = false; // mark invalid state
			$(this).addClass('required'); // highlight field
		}
	});
	
	// display fix message if not valid
	if ( !valid ) {
		$('#please_fix').show();
	}
	
	return valid;
	
}

$(document).ready( function () {

	$('[name="tbName"], [name="tbCompany"]').blur( function () {
		var last_td = $(this).parent().parent().find('td:last');
		var name = "";
		if ( $(this).attr('name') == 'tbName' )
			name = 'name';
		else
			name = 'company';
		if ( this.value.length == 0 ) {
			last_td.find('div.invalid').remove();
			last_td.append($("<div class=\"invalid\">Please enter " + name + "</div>"));
		} else
			last_td.find('div.invalid').remove();
	});

	$('[name="tbEmail"]').blur( function () {
		var last_td = $(this).parent().parent().find('td:last');
		if (!this.value.match(/^[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-_]+\.)+[A-Za-z0-9]{2,4}$/)) {
			if ( this.value.length == 0 ) {
				last_td.find('div.invalid').remove();
				last_td.append($("<div class=\"invalid\">Please enter e-mail</div>"));
				return;
			}
			last_td.find('div.invalid').remove();
			last_td.append($("<div class=\"invalid\">Invalid format</div>"));
		} else {
			last_td.find('div.invalid').remove();
		}
	});
	
	$('[name="tbPhone"], [name="tbFax"]').blur( function () {
		var last_td = $(this).parent().parent().find('td:last');
		var val = this.value;
		val = $.trim(val);
		if ( val.length == 0 ) {
			last_td.find('div.invalid').remove();
			if ( $(this).attr('name') == 'tbPhone')
				last_td.append($("<div class=\"invalid\">Please enter phone</div>"));
			return;
		}

		// wowza!
		if (!val.match(/^((\+){0,1}\d{1,4}\s*[.-]?)?\(?([0-9]{1,4})\)?\s*[\.-]?\s*([0-9]{3})\s*[\. -]?\s*([0-9]{4})\s?((ext|x)\s*\.?:?\s*([0-9]+))?$/)) {
			last_td.find('div.invalid').remove();
			last_td.append($("<div class=\"invalid\">Invalid format</div>"));
		} else {
			last_td.find('div.invalid').remove();
			
			// remove excess spaces
			val = val.replace(/(\s)+/g, ' ');
			
			// if 'ext' or 'x' is surrounded by whitespace, remove it and replace with 'x'
			if (val.match(/(\s*ext\s*|\s*x\s*)/))
				val = val.replace(/(\s*ext\s*|\s*x\s*)/, 'x');
			
			// split extension off
			var phone_array = val.split('x');
			var ext = '';
			
			if ( phone_array.length > 1 )
				ext = "x" + phone_array[1];
				
			val = phone_array[0];
			
			// do formatting, working from right to left
			
			// format last 7 digits
			if (val.match(/[0-9]{3}(\s*[.-]?\s*)[0-9]{4}$/))
				val = val.replace(/([0-9]{3})(\s*[.-]?\s*)([0-9]{4})$/, '$1-$3');
			
			// format last 7 + area code
			if (val.match(/[0-9]{1,4}(\s*[\.-]?\s*)?[0-9]{3}-[0-9]{4}$/)) {
				// if 4-3-4, likely meant to be (last-digit)-(area)-(prefix)-(last4), not (4-digit-area)-(prefix)-(last4)
				if (val.match(/^[0-9]{4}(\s*[\.-]?\s*)?[0-9]{3}-[0-9]{4}$/))
					val = val.replace(/^([0-9]{1})([0-9]{3})(\s*[\.-]?\s*)([0-9]{3}-[0-9]{4})$/, '$1 ($2) $4');
				else
					val = val.replace(/([0-9]{1,4})(\s*[\.-]?\s*)([0-9]{3}-[0-9]{4})$/, '($1) $3');
			}
			
			// format country code if it exists and rest of number
			if (val.match(/[(]/)) {
				val = val.replace(/^([+]{0,1})(\d+)(\s*[\.-]?\s*)([(].*)$/, '$1$2 $4');
			}
			
			// make sure we start with a plus, if a country code is given
			if (val.match(/^(\d+)\s*([(].*)$/)) {
				val = '+' + val;
			}
			
			$(this).val(val + ext);
		}
	});
	
	// force numericality and length
	$('[name="tbZip"], [name="tbZip2"]').blur( function () {
		var last_td = $(this).parent().parent().find('td:last');
		
		var zip = $('[name="tbZip"]').val();
		var zip2 = $('[name="tbZip2"]').val();

		if ( zip.length == 0 && zip2.length == 0 ) { // neither have content
			last_td.find('div.invalid').remove();
		} else if ( zip.length == 0 && zip2.length > 0 ) { // zip2 has content only
			last_td.find('div.invalid').remove();
			last_td.append($("<div class=\"invalid\">Invalid format</div>"));
		} else if ( zip.length > 0 && zip2.length == 0 ) { // zip has content only
			if ( zip.match(/[^0-9]/) ) {
				last_td.find('div.invalid').remove();
				last_td.append($("<div class=\"invalid\">Numbers only</div>"));
			} else if ( !zip.match(/[0-9]{5}/) ) {
				last_td.find('div.invalid').remove();
				last_td.append($("<div class=\"invalid\">Must be 5 digits</div>"));
			} else
				last_td.find('div.invalid').remove();
		} else { // both have content
			if ( zip.match(/[^0-9]/) || zip2.match(/[^0-9]/) ) {
				last_td.find('div.invalid').remove();
				last_td.append($("<div class=\"invalid\">Numbers only</div>"));
			} else if ( !zip.match(/[0-9]{5}/) || !zip2.match(/[0-9]{4}/) ) {
				last_td.find('div.invalid').remove();
				last_td.append($("<div class=\"invalid\">5 digits - 4 digits</div>"));
			} else
				last_td.find('div.invalid').remove();
		}
	});

	// force numericality
	$('[name="tbYears"], [name="tbEmployees"]').blur( function () {
		var last_td = $(this).parent().parent().find('td:last');
		if (!this.value.match(/[0-9]+/) && !this.value.length == 0) {
			last_td.find('div.invalid').remove();
			last_td.append($("<div class=\"invalid\">Numbers only</div>"));
		} else
			last_td.find('div.invalid').remove();
	});
	
	// make sure website starts with http:// or https://
	$('[name="tbWebsite"]').blur( function () {
		if (!this.value.match(/^https?:\/\//) && !this.value.length == 0)
			$(this).val("http://" + $(this).val());
	});

});