/* FW100.091027 */
/* (c)2009 SugarHill Works LLC - http://www.sugarhillworks.com */

//var submit_btn_txt_default = 'Send Message';
$(function(){
	$.get("lib/token.php",function(txt){
		$("#contact_fm").append('<input type="hidden" name="ts" value="'+txt+'" />');
		$("#contact_fm").append('<input type="hidden" name="serverErr" value="false" />');
		if ($(".input_err:hidden").length) {
			if($(".input_err:visible").length === 0) {
				$("#resale").focus().select(); // this id selects where the cursor appears on page load!
			}
		}
	});	
	
	// one pair of these line for each form field you want to filter and show input errors. also add an 'else if' statement below	
	$("#name_err").click(function(){$(this).fadeOut(250, function(){$("#name").focus().select();});});
	$("#name").focus(function(){$("#name_err").fadeOut(250, function(){$("#name").select();});});
	$("#business_name_err").click(function(){$(this).fadeOut(250, function(){$("#business_name").focus().select();});});
	$("#business_name").focus(function(){$("#business_name_err").fadeOut(250, function(){$("#business_name").select();});});
	$("#email_err").click(function(){$(this).fadeOut(250, function(){$("#email").focus().select();});});
	$("#email").focus(function(){$("#email_err").fadeOut(250, function(){$("#email").select();});});
	$("#phone_err").click(function(){$(this).fadeOut(250, function(){$("#phone").focus().select();});});
	$("#phone").focus(function(){$("#phone_err").fadeOut(250, function(){$("#phone").select();});});
	$("#message_err").click(function(){$(this).fadeOut(250, function(){$("#message").focus().select();});});
	$("#message").focus(function(){$("#message_err").fadeOut(250, function(){$("#message").select();});});
//	$("#message_subject_err").click(function(){$(this).fadeOut(250, function(){$("#message_subject").focus().select();});});
//	$("#message_subject").focus(function(){$("#message_subject_err").fadeOut(250, function(){$("#message_subject").select();});});
});

function validateForm() {
//	$('#submit_btn').attr({'disabled' : 'true', 'value' : 'Sending...' });
	var phone_filter = /\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/;

	if ($("#name").val().search(/\S/) == -1) {
		doFormError("name" ,"Please enter your name.");
	}
	else if ($("#business_name").val().search(/\S/) == -1) {
		doFormError("business_name" ,"Please enter your business name.");
	}
	else if ($("#email").val().search(/\S/) == -1) { 
		doFormError("email", "Please enter your working email address.");
	}
	else if ((($("#phone").length > 0) && ($("#phone").val())) && (!phone_filter.test(jQuery.trim($("#phone").val())))) {
		doFormError("phone", "Please format like: 000-000-0000");
	}
//	else if ($("#message_subject").val().search(/\S/) == -1) { 
//		doFormError("message_subject", "You forgot to type a message subject!");
//	}
	else if ($("#message").val().search(/\S/) == -1) { 
		doFormError("message", "You forgot to type a message!");
	}
	else {
		var sendBtn_txt = 'Sending...';
		var i = 0;
		var updateBtnTxt = setInterval(function(){
			i += 1;
			if (i < 10) {
				sendBtn_txt += '.';
			} else { 
				clearInterval(updateBtnTxt);
			}
			$('#sending_txt').html(sendBtn_txt);
		}, 75);
		$("#contact_fm").submit();
	}
}

function doFormError(err_cntr_id, error_txt) {
//	$('#submit_btn').attr('value', submit_btn_txt_default);
	$("#"+err_cntr_id+"_err").html(error_txt).fadeIn();
}



