$(function(){
	form = $('#feedbackform');
	if( !focus_field )
		focus_field = "email";
	$('input[name=' + focus_field + ']', form).focus();
	
	form.bind("submit", function(e) {
		//check email...
		var email = $.trim( $('input[name=email]', form).val() );
		if( email == '' ) {
			alert('Please, fill in the email field!');
			$('input[name=email]', form).focus();
			return false;
		}
		if( !is_valid_email(email) ) {
			alert("Invalid email adress!");
			$('input[name=email]', form).focus();
			return false;	
		}
		//check subject...
		if( !$('select[name=subject]', form).val() ) {
			alert('Please choose a subject from the list!!');
			$('select[name=subject]', form).focus();
			return false;
		}
		//message check...
		if( !$.trim($('textarea[name=message]', form).val()) ) {
			alert('Please, fill in the message field!');
			$('textarea[name=message]', form).focus();
			return false;
		}
		
		//check captcha
		var ccode = $.trim( $('input[name=ccode]', form).val() );
		if( ccode == '' ) {
			alert("Please, enter the code from image!");
			$('input[name=ccode]', form).focus();
			return false;	
		}

		return true;
	});
	
	$("#cupdater").bind("click", function(e) {
		var rnd = Math.floor( Math.random()*10000 );
		$("#sequre_img").attr('src', '/get_captcha/?' + rnd);
		$('input[name=ccode]', form).focus();
		return false;
	});
	
	
});


function is_valid_str(str) {
	return ( str.match(/[^a-zA-Z0-9_]/) ) ? false : true;
}

function is_valid_email(str) { 
	return str.match(/^(\w|-|\d|_)+(\.(\w|-|\d|_)+)*@(\w|-|\d)+(\.(\w|-|\d)+)+$/);
}

