$(document).ready(function() {

	/***********************************************/
	/* Text Box
	/***********************************************/
	$('.text').focus(function () {
		if ($(this).hasClass("text")) {
			$(this).removeClass("text");
			$(this).addClass("text_active");
		}
	});
	
	$('.text').blur(function () {
		if ($(this).val().length > 0) {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("text_good");
		} else {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("text");
		}
	});
	
	$('.text').keyup(function () {
		if ($(this).val().length > 0) {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("text_good");
		} else {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("text_active");
		}
	});
	
	/***********************************************/
	/* Email
	/***********************************************/
	$('.email').focus(function () {
		if ($(this).hasClass("email")) {
			$(this).removeClass("email");
			$(this).addClass("email_active");
		}
	});
	
	$('.email').blur(function () {
		if($(this).val().length > 0) {
			if (validateEmail($(this).val())) {
				$(this).removeClass($(this).attr("class"));
				$(this).addClass("email_good");
			} else {
				$(this).removeClass($(this).attr("class"));
				$(this).addClass("email");
			}
		}
		else {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("email");
		}
	});
	
	$('.email').keyup(function () {
		if($(this).val().length > 0) {
			if (validateEmail($(this).val())) {
				$(this).removeClass($(this).attr("class"));
				$(this).addClass("email_good");
			} else {
				$(this).removeClass($(this).attr("class"));
				$(this).addClass("text_active");
			}
		}
		else {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("text_active");
		}
	});
	
	/***********************************************/
	/* Phone
	/***********************************************/
	$('.phone').focus(function () {
		if ($(this).hasClass("phone")) {
			$(this).removeClass("phone");
			$(this).addClass("phone_active");
		}
	});
	
	$('.phone').blur(function () {
		if($(this).val().length > 0) {
			if (validatePhone($(this).val())) {
				$(this).removeClass($(this).attr("class"));
				$(this).addClass("phone_good");
			} else {
				$(this).removeClass($(this).attr("class"));
				$(this).addClass("phone");
			}
		}
		else {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("phone");
		}
	});
	
	$('.phone').keyup(function () {
		if ($(this).val().length > 0) {
			if (validatePhone($('#form-phone').val())) {
				$(this).removeClass($(this).attr("class"));
				$(this).addClass("phone_good");
			} else {
				$(this).removeClass($(this).attr("class"));
				$(this).addClass("phone_active");
			}
		}
		else {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("phone_active");
		}
	});
	
	
	/***********************************************/
	/* Text Area
	/***********************************************/
	$('.textarea').focus(function () {
		if ($(this).hasClass("textarea")) {
			$(this).removeClass("textarea");
			$(this).addClass("textarea_active");
		}
	});
	
	$('.textarea').blur(function () {
		if ($(this).hasClass("textarea_active")) {
			$(this).removeClass("textarea_active");
			$(this).addClass("textarea");
		}
	});
	
	$('.textarea').keyup(function () {
		if ($(this).val().length > 1) {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("textarea_good");
		} else {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("textarea_active");
		}
	});
	
	
	/***********************************************/
	/* Form Submit
	/***********************************************/
	$('#form-contact').submit(function() {
		if (!validate_form()) {
			
			//if phone field is not entered, it should be set to a value
			if($('#form-phone').val() == '' ){
				$('#form-phone').val(' ');
			}
			
			var data = { "name-field": $('#form-name').val(), "email-field": $('#form-email').val(), "phone-field": $('#form-phone').val(), "comments-field": $('#form-comments').val(), "address-field": $('#form-address').val()};
			$.post('/actions/ajax-form-submit.php', data, function(xml_data) {
				switch($("response", xml_data).text()) {
					case 'Success!':
						$("#contact-form").fadeOut("fast", function () {
								$('#contact-success').fadeIn("fast");
						});
						 break;
					case 'Email error':
						$("#contact-form").fadeOut("fast", function () {
								$('#contact-fail').fadeIn("fast");
						});
					 	break;
					case 'Random error':
						$("#contact-form").fadeOut("fast", function () {
								$('#contact-fail').fadeIn("fast");
						});
						break;
				}
				resetForm();
			}, "xml");
			return false;
		} else {
			display_errors();
			return false;
		}
	});
	
	/***********************************************/
	/* Reset Form
	/***********************************************/
	$('.return-to-form').click(function() {
		resetForm();
		$('#contact-success').fadeOut("fast", function () {
			$("#contact-form").fadeIn("fast");
		});
		return false;
	});
});

function display_errors() {
	
	$('.text,.text_active,.text_good,.text_bad').each(function() {
		if ($(this).val().length == 0) {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("text_bad");
		}
	});
	if (!validateEmail($('#form-email').val())) {
			$('#form-email').removeClass($('#form-email').attr("class"));
			$('#form-email').addClass("email_bad");
	}
	if (!validatePhone($('#form-phone').val())) {
			$('#form-phone').removeClass($('#form-phone').attr("class"));
			$('#form-phone').addClass("phone_bad");
	}
	$('.textarea, .textarea_active, .textarea_good, .textarea_bad').each(function() {
		if ($(this).val().length < 2) {
			$(this).removeClass($(this).attr("class"));
			$(this).addClass("textarea_bad");
		}
	});
}

function validate_form() {

	var error = false;
	$('.text,.text_active,.text_good,.text_bad').each(function() {
		if ($(this).val().length == 0) {
			error = true;
		}
	});
	if (!validateEmail($('#form-email').val())) {
		error = true; 
	}
	if (!validatePhone($('#form-phone').val())) {
		error = true;
	}
	
	$('.textarea, .textarea_active, .textarea_good, .textarea_bad').each(function() {
		if ($(this).val().length < 2) {
			error = true;
		}
	});
	return error;
}

function resetForm() {

	$('#form-name, #form-email, #form-phone, #form-comments').val('');

	$('#form-name').removeClass($('#form-name').attr("class"));
	$('#form-name').addClass("text");

	$('#form-email').removeClass($('#form-email').attr("class"));
	$('#form-email').addClass("email");
	
	$('#form-phone').removeClass($('#form-phone').attr("class"));
	$('#form-phone').addClass("phone");
	
	$('#form-comments').removeClass($('#form-comments').attr("class"));
	$('#form-comments').addClass("textarea");

}
