$(function(){
	$("#WebToLeadForm").submit(function(event){
		event.preventDefault(); var form = $(this);
		
		// fix submit button:
		form.find(":submit").removeClass("clicked");
		
		//validate email
		if(validateEmail() == false)
			return false;

		$.post(this.action, 
			   form.serialize(),
			   function(){
					form.hide();
					$("#success-msg").show();
			   }
			);
	});
	
	// make the button pretty
	$("#WebToLeadForm :submit").mousedown(function(){
		this.className="clicked";
	});

	// email validation && focus/blur events
	$("#email").change(function(){
		validateEmail();	
	}).focus(function(){
		if(this.value =="email address")
			this.value = "";
	}).blur(function(){
		if(this.value.length == 0)
			this.value = "email address";
	});
	
	$("#city").focus(function(){
		if(this.value =="city")
			this.value = "";
	}).blur(function(){
		if(this.value.length == 0)
			this.value = "city";
	});
	$("#state").focus(function(){
		if(this.value =="state")
			this.value = "";
	}).blur(function(){
		if(this.value.length == 0)
			this.value = "state";
	});
	
	//login form
	$("#username").focus(function(){
		if(this.value == "Username")
			this.value = "";
	}).blur(function(){
			if(this.value.length == 0)
				this.value = "Username";
	});
	
	addPasswordEvents();
	
	$("#login-form").submit(function(event){
		var username = $(this).find("#username"),
			password = $(this).find("#password");
	
		//sanitize
		username.val(username.val().replace("Username",""));
		password.val(password.val().replace("Password",""));


		//if(form.validateForm() == false){
			
		if(username.val().length === 0 && password.val().length === 0){
			event.preventDefault();

			username.val("Username");
			password.val("Password");
			alert("please enter a username and password");
		} else {
			if(username.val().length === 0){
				event.preventDefault();
				username.val("Username");
				alert("please enter a username");
			}
			if(password.val().length === 0){
				event.preventDefault();
			
				password.val("Password");
				alert("please enter a password");
			}
		}
		//}
		
	});
});

function validateEmail(){
	if(document.getElementById('email').value.length >0) {
		if(document.getElementById('email').value.match(/^\w+(['\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == null){
			alert('Please enter a valid email address');
	  		return false;
		} else
			return true;
		
	} else {
		alert('Please enter a valid email address');
  		return false;
	}
}


function addPasswordEvents(){
	$("#password").focus(function(){
		if(this.type == "text"){
			$('<input type="password" name="password" id="password" value="" class="validate-req" tabindex="2" />').insertAfter(this).focus();
			$(this).remove();
			addPasswordEvents();
		}
	}).blur(function(){
		if(this.type == "password"){
			if(this.value.length === 0){
				$('<input type="text" name="password" id="password" value="Password" class="validate-req" tabindex="2" />').insertAfter(this);
				$(this).remove();
				addPasswordEvents();
			}
		}
	});
}