// IDX uses prototype, so set jQuery to noconflict mode
jQuery.noConflict();
var $j = jQuery;

jQuery.fn.resetForm = function () {
	$j(this).each(function() { 
		this.reset(); 
	});
}

function validateSnapshot(formData, jqForm, options) {
	var form = jqForm[0];
	if (!form.fullname.value || !form.email.value) {
		alert("Please fill out all required fields");
		return false;
	} else {
		$j(form).hide().after('<div class="loader" style="text-align:center;"><img src="http://thurstoncountyhomebuyer.com/dev/images/interface/ajax-loader.gif" /></div>');
	}
}

function validateRequest(formData, jqForm, options) {
	var form = jqForm[0];
	if (!form.fullname.value || !form.email.value) {
		alert("Please fill out all required fields");
		return false;
	} else {
		$j(form).hide().after('<div class="loader" style="text-align:center;"><img src="http://thurstoncountyhomebuyer.com/dev/images/interface/ajax-loader.gif" /></div>');
	}
}

$j(document).ready(function(){
	
	$j('.colorbox').colorbox({
		inline: true
	});
	$j('#snapshotform-ajax').ajaxForm({
		beforeSubmit: validateSnapshot,
		target: '#response',
		success: function() {
			$j(".loader").hide();
			$j(this).html('<p><strong>Your request has been submitted!</strong></p>');
		}
	});
	
	$j(".buyersguideform-ajax, .snapshotform-ajax").ajaxForm({
		beforeSubmit: validateRequest,
		target: '#response',
		resetForm: true,
		success: function() {
			$j(".loader").hide();
			$j(this).html('<p><strong>Your request has been submitted!</strong></p>');
			//$j("#response").html('<p>Your request has been submitted!</p>');
		}
	});
	
	
	$j(".reset").click(function(){
		$j(this).parents("form").resetForm();
		return false;
	});
	
	// auto-clear input fields when clicked, and restore to the default if left blank
	var fieldValues = [];
	$j(".emailform .textinput").each(function(i){
		fieldValues[i] = $j(this).val();
		$j(this).focus(function(){
			if ($j(this).val() == fieldValues[i]) {
				$j(this).val("");
			}
			$j(this).addClass("activefield");
		}).blur(function(){
			if ($j.trim($j(this).val()) == "") {
				$j(this).val(fieldValues[i]);
			}
			$j(this).removeClass("activefield");
		});
	});
	
	// ajax submission for the guide request forms
	// DOES NOT PLAY NICE WITH IDX-SERVED PAGES, REVERT TO STANDARD SUBMISSION
	/*
	$j("input.submit").click(function() {
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var fullnameVal = $j(this).siblings("input[name=fullname]").val();
		var emailVal = $j(this).siblings("input[name=email]").val();
		var formidVal = $j(this).siblings("input[name=formid]").val();
		if (fullnameVal == '' || fullnameVal == 'First & Last Name') {
			hasError = true;
		}
		if (!emailReg.test(emailVal)) {
			hasError = true;
		}
		if (hasError) {
			// one or both of the fields did not validate
			$j.fn.colorbox({
				html:'<h1 style="text-align: center; line-height: 116px;">Please enter your name and email address.</h1>',
				width: 400,
				height: 120
			});
			return false;
		} else {
			// go ahead and submit the form.
			var dataString = 'fullname=' + fullnameVal + '&email=' + emailVal + '&formid=' + formidVal;
			$j.ajax({
				type: "POST",
				url: baseurl + "processform.php",
				data: dataString,
				success: function(){
					$j.fn.colorbox({
						html:'<h1 style="text-align: center; line-height: 116px;">Thank you, your request has been submitted.</h1>',
						width: 400,
						height: 120
					});
				}
			});
		}
		return false;
	});*/
});
