$(document).ready(function() { 

	var options = {
		clearForm: true,
		beforeSubmit: validate,
		success: function() {
			$('#formTable').attr("class","sent").html("<tr><td><h1>Thank you!</h1>Your message has been sent.</td></tr>").oneTime(3000, function() { 
																																 $(this).removeClass("sent").html($("#storeTable").html());
																																 $("span.errormsgmain").attr("class","errormsgmain hidden red");
																																 });
			}
	};
	
	var side_options = {
		clearForm: true,
		beforeSubmit: side_validate,
		success: function() {
			$('#sidecontact').attr("class","sent").html("<table><tr><td><h1>Thank you!</h1>Your message has been sent.</td></tr></table>").oneTime(3000, function() { 
																																 $(this).removeClass("sent").html($("#storesidecontact").html());
																																 $("span.errormsg").attr("class","errormsg hidden red");
																																 });
			}
	};	
	
	var pages = [
	{ text: "Contact Us", url: "contact.php" },
	{ text: "Mailing Address", url: "contact.php" },
	{ text: "Office Address", url: "contact.php" },
	{ text: "Phone Number", url: "contact.php" },
	{ text: "Email", url: "contact.php" },
	{ text: "Facebook", url: "http://www.facebook.com/janice.perlman" },
	{ text: "LinkedIn", url: "http://www.linkedin.com/in/janiceperlman" },
	{ text: "Author", url: "biography.php" },
	{ text: "Biography", url: "biography.php" },
	{ text: "About", url: "board.php" },
	{ text: "Janice Perlman", url: "biography.php" },
	{ text: "Blog", url: "blog/" },
	{ text: "News", url: "blog/" },
	{ text: "Favela: Four Decades", url: "favela.php" },
	{ text: "Myth of Marginality", url: "myth.php" },
	{ text: "Donate", url: "donate.php" },
	{ text: "Publications", url: "pub_1.php" },
	{ text: "Innovation Transfers", url: "trans_1.php" },
	{ text: "New York City", url: "trans_2.php" },
	{ text: "Los Angeles", url: "trans_2.php" },
	{ text: "Curitiba", url: "trans_1.php" },
	{ text: "Sao Paulo", url: "trans_1.php" },
	{ text: "Bangkok", url: "trans_1.php" },
	{ text: "Rio de Janeiro", url: "trans_1.php" },
	{ text: "Cairo", url: "trans_1.php" },
	{ text: "Manila", url: "trans_1.php" },
	{ text: "Bombay", url: "trans_1.php" },
	{ text: "Favela Study Publications", url: "pub_1.php" },
	{ text: "Environmental Justice Publications", url: "pub_2.php" },
	{ text: "International Transfers Publications", url: "pub_3.php" },
	{ text: "Urban Leadership Publications", url: "pub_4.php" },
	{ text: "Conceptual/Analytical Publications", url: "pub_5.php" },
	{ text: "Order Book Online", url: "favela.php" },
	{ text: "Board of Directors", url: "board.php" },
	{ text: "Former Directors", url: "board.php" },
	{ text: "Project Coordinators", url: "board.php" },
	{ text: "Global Advisory Board", url: "board.php" },
	{ text: "Funding Sources", url: "board.php" }
	];

		
	$('#contactForm').ajaxForm(options); 
	$('#sidebarContactForm').ajaxForm(side_options); 
	
	$('input:text').click(function() {
		$(this).val('');
	});
	
	$('textarea').click(function() {
		$(this).val('');
	});
		
	$("#search").autocomplete(pages, {
	  width: 200,	
	  formatItem: function(item) {
		return item.text;
	  }
	}).result(function(event, item) {
	  location.href = item.url;
	});
	
}); 

function validate(formData, jqForm, options) {  
    var form = jqForm[0]; 
    if (!form.name.value || !form.email.value || !form.comments.value) { 
        $("span.errormsgmain").text("Please enter all required fields.").attr("class","errormsgmain red");
        return false; 
	} else if (!form.email.value.match(/@/)) {
        $("span.errormsgmain").text("Please enter a valid email address.").attr("class","errormsgmain red");
		return false;
	}	
}

function side_validate(formData, jqForm, options) {  
    var form = jqForm[0]; 
	if (!form.email.value.match(/@/)) {
        $("span.errormsg").text("Please enter a valid email address.").attr("class","errormsg red");
		return false;
	}	
}

