$(document).ready(function(){
	$('#switcher').tabs();
	$('#contactform').hide();
	$('#contactus a').click(function() {
		$('#contactform').slideToggle();
		return false;
	});
	// Contact Form

	
	
	
	$("#form5").ajaxSubmit();
});


$.fn.ajaxSubmit = function(e) {
	/* Change a form's submission type to ajax */
	this.submit(function(){
		var params = {};

		$(this).find("select").each(function() {
			params[this.name] = this.options[this.selectedIndex].text;
		});

		$(this)
		.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], textarea")
		.filter(":enabled")
		.each(function() {
			params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
		});
		$.post("process.php" + "?call=ajax", params, function(wufoo){
			// $("#response").append(wufoo); // DEBUG
/* 
Success look like: {"wufoo_submit":[{"success":"true", "entry_id":"50", "redirect_url":"", "confirmation_message":"Thanks for contacting us. You will hear from us soon.", "merchant":"", }]} 
	Failure look like: {"wufoo_submit":[{"success":"false", "error":"", "field_errors":[{"field_id":"field0", "error_code":"0", "error_message":"This field is required. Please enter a value."},{"field_id":"field1", "error_code":"0", "error_message":"This field is required. Please enter a value."},{"field_id":"field4", "error_code":"0", "error_message":"This field is required. Please enter a value."}]}]}
*/
			eval("obj = "+wufoo);
			if (obj['wufoo_submit'] != undefined) {
			  for (var i in obj.wufoo_submit) {
				if (obj.wufoo_submit[i].success == 'true') {
/*                     alert("We have success"); */
/*                     alert("What is? " + typeof obj.wufoo_submit[i].confirmation_message + " holding -> " + obj.wufoo_submit[i].confirmation_message); */
                    // Clear the form
                    // Display message of success
                    					$("#response").append("<p class=\"success\">"+obj.wufoo_submit[i].confirmation_message+"</p>");

                    $("#contactform").slideToggle();
                    $("#form5").each(function() {
                    
						if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType))
						this.reset();
						});
				} else if (obj.wufoo_submit[i].success == 'false') {
					for (var key in obj.wufoo_submit[i]) {
					  // alert(obj.wufoo_submit[i]); // DEBUG
					  for (var j in obj.wufoo_submit[i][key]) {
						if (obj.wufoo_submit[i][key][j].field_id != undefined) {
							li = $("."+obj.wufoo_submit[i][key][j]['field_id']);
							li.addClass("error");
							li.append("<p class=\"error\">"+obj.wufoo_submit[i][key][j].error_message+"</p>");
						}
					  }
					}/* End of for loop key traversal */
				}
			  } /* End of for loop index traversal */
			} /* End of if wufoo_submit defined */
		 });
		
		
/*
		 $("#response").load('process.php',params,function(){
		 	alert();
		 	
		 });
*/
		
		return false;
	});
	return this;
}
