$(function () {

	$('form#contact_form input.input').bind('focus', function() {
			$(this).parents('.input-outer').css('background-position', '-500px top');
			$(this).next('span.input-right').css('background-position', '-11px top');
	}).bind('blur', function() {
			$(this).parents('.input-outer').css('background-position', 'left top');
			$(this).next('span.input-right').css('background-position', 'left top');
	})

	$('form#contact_form textarea').bind('focus', function() {
			$(this).css('background', '#fff');
	}).bind('blur', function() {
			$(this).css('background', '#F9F14D');
	})


	function check_captcha() {
		var captcha = $('input#Captcha').val();
		if (captcha == '') return false;
		
		var check = $.ajax({
			type: "POST",
			url: "/index.php",
			data: {ACT: ACT,
					type: "check_captcha",
					captcha: captcha
			},
			async: false
		}).responseText;
		
		$("#captchaSpan span").remove;
		
		if(check == "correct") {
			return true;
		} else {
			$('span#captchaImg').html(check);
			return false;
		}
		return false;
	}
	
	
	function remove_captcha() {
		var captcha = $('input#Captcha').val();
		var captcha_img = $('span#captchaImg').attr('src');
		$.ajax({
			type: "POST",
			url: "/index.php",
			data: {
				ACT: ACT,
				type: "remove_captcha",
				captcha: captcha,
				captcha_img: captcha_img
			},
			async: false
		});
		return;
	
	}
	
	function refresh_captcha() {
		//remove_captcha();
		$('#captchaSpan img').remove();
		var cimg = $.ajax({
			type: "POST",
			url: "/index.php",
			data: {ACT: CACT,
					type: "generate_captcha"
			},
			async: false
		}).responseText;
		$('span#captchaImg').html(cimg);
	}
	
	jQuery.validator.addMethod("captcha_validate", function(value, element, params) { 
	 return check_captcha();
	}, "Your text does not match the image. Please try again.");
	
	
	$('input#Captcha').val('');
	refresh_captcha();	
	
	$('form#contact_form').validate({
		errorElement: "em",
		rules: {
			name: "required",
			from: {
				required: true,
				email: true
			},
			subject: "required",
			message: "required",
			captcha: {
				required: true,
				captcha_validate: true
			}
		},
		messages: {
			name: "Please enter your name.",
			from: {
				required: "We need your email address to contact you.",
				email: "Your email address must be in the format of name@domain.com."
			},
			subject: "Please enter a subject.",
			message: "Please enter a message.",
			captcha: "Your text does not match the image. Please try again."
		},
		success: function(label) {
			if (label.attr('for') == 'Captcha')
				label.addClass("ok").text("Correct.");
			else 
				label.removeClass('error');
		},
		onkeyup: false,
		focusCleanup: true,
		errorPlacement: function(error, element) {
     		error.appendTo( element.parents('p.field') );
   		}

	});
	
});