$(function() {
	//make sure error messages are hidden
	$('.error').hide();
	
	//now, let's set logic for input feild styles
	$('.text').focus(function(){
		$(this).addClass("onfocus");
		$(this).removeClass("onfail");
	});
	$('.text').blur(function(){
		$(this).removeClass("onfocus");
	});
	
	$(".contact_submit").click(function() {
		// validate and process the form
		// first hide any error messages
		$('.error').hide();
		
		//next, let's set our required feild variables
		var name = $("input#name").val();
		var email = $("input#email").val();
		var store = $("input#store").val();		
		var comments = $("textarea#comments").val();
		
		//we need some extra variables for email validation
		var at = "@";
		var dot = ".";
		if (name == "") {
			$("input#name").addClass("onfail");
		}
		
		if (email == "" || email.indexOf(at)==-1 || email.indexOf(dot)==-1) {
			$("input#email").addClass("onfail");
		}
		if (comments == "") {
			$("textarea#comments").addClass("onfail");
		}
		
		//if something is missing, don't process the form
		if (name == "" || email == "" || email.indexOf(at)==-1 || email.indexOf(dot)==-1 || comments == "") {
			return false;
		}

		//setup store email address
		var sendto;
		switch(store)
		{		
		case 'Beaverton':
			sendto='kw318@kremeworks.com ';
			break;
		case 'Burlington':
			sendto='kw316@kremeworks.com';
			break;
		case 'Clackamas':
			sendto='kw319@kremeworks.com';
			break;
		case 'Clark County':
			sendto='kw326@kremeworks.com ';
			break;
		case 'Delta':
			sendto='kw324@kremeworks.com';
			break;		
		case 'Issaquah':
			sendto='kw311@kremeworks.com';
			break;		
		case 'Kahului, Maui':
			sendto='kw323@kremeworks.com';
			break;		
		case 'Puyallup':
			sendto='kw321@kremeworks.co';
			break;		
		case 'Seattle (North)':
			sendto='kw314@kremeworks.com';
			break;	
		case 'Seattle (SODO)':
			sendto='kw315@kremeworks.com';
			break;	
		case 'Spokane':
			sendto='kw313@kremeworks.com';
			break;	
		case 'Tacoma Mall':
			sendto='kw320@kremeworks.com';	
		default:	
			sendto='msurbycurtin@kremeworks.com';
			break;	
		}
		
		//if all is good, then create a datastring with the values
		var dataString = 
		'name='+ name +
		'&email=' + email + 
		'&store=' + sendto + 
		'&comments=' + comments;		
		//alert (dataString);

		$.ajax({
			type: "POST",
			url: "http://www.kremeworks.com/wp-content/themes/kremeworks/new_contact.php",
			data: dataString,
			success: function() {
				$('#ajaxtext').html('<p style="padding:50px 5px">Thank you for your inquiry!<br/>One of our managers will be<br/>getting back to you shortly.</p>').hide().fadeIn(800, function() {
					$('#ajaxtext');
				});
			}
			
		});

		return false;
		});
});
