/*jQuery.validator.addMethod("phone", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number.");*/

jQuery.validator.setDefaults({
	success: "valid",
	errorElement: "span"
});

function DisplayNextStep(handle) {
	$("fieldset", handle).slideUp("fast");
	$(handle).next().find("fieldset").slideDown("fast");
}
	

function ProcessForm(handle, last)
{
	if ($(handle).valid()) {
		$.ajax({
			type: "POST",
			url: "/process.php",
			data: $(handle).serialize(),
			success: function(msg)
			{
				if (msg == 'ok' && last != true) {
          DisplayNextStep(handle);
          //$('input[name=code]').val(msg);
        }
				else $('#message').html('Information could not be saved, please e-mail us directly.');

				//else $("#confirm fieldset").prepend('<p class="message">E-mail was resent to the indicated address.</p>');
			}
		});
	}

	return false;
}

$(document).ready(function() {

$("li.submit").append('<input type="hidden" name="ajax" value="1" /><span class="loading"><img src="i/ajax-loader.gif" alt="Loading" /></span>');
$(".loading").hide();


/*$("#confirm ul").hide();

$("#showEmail").click(function() { $("#confirm ul").slideDown("fast"); return false; });*/


$("#email").change(function() { $("#resendemail").val($(this).val()); });

$("#contact").validate({

	/*groups: {
		fullPhone: "phone ext"
	},
	
	errorPlacement: function(error, element) {
		if (element.attr("name") == "phone") error.insertAfter("#ext");
		else error.insertAfter(element);
	},*/

	rules: {
		fname: "required",
		phone: { required: true}, /*, phone: true */
		email: { required: true, email: true }
	}
});


$(".submit").ajaxStart(function() {
	$("input:submit", this).attr("disabled", "disabled");
	$(".loading", this).show();
});
$(".submit").ajaxStop(function() {
	$("input:submit", this).removeAttr("disabled");
	$(".loading", this).hide();
});


$("#contact, #project").submit(function() { return ProcessForm(this); });
/*$("#confirm").submit(function() { return ProcessForm(this, true); });



$("#project").validate({ rules: { cname: "required", budget: "required" } });

$("#confirm").validate({ rules: { resendemail: { required: true, email: true } } });*/

});
