/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	//global vars
	var form = $("#review_form");
	var title = $("#title");
	var titleInfo = $("#titleInfo");
	var rating = $("#rating");   
	var ratingInfo = $("#ratingInfo");  
	var text = $("#text");   
	var textInfo = $("#textInfo"); 
	var captcha = $("#captchaCode");  
	var captchaInfo = $("#captchaInfo");  
			
	//On blur
	title.blur(validateTitle);
	//rating.blur(validaterating);  
	rating.click(validaterating);  
	text.blur(validateComment);  
	captcha.blur(validateCaptcha);
	//On key press
	title.keyup(validateTitle);
	//txtphone.keyup(validatePhone);
		
	form.submit(function(){
			
		if((validateTitle()==true) & (validaterating()==true) & (validateComment()==true) & (validateCaptcha()==true))
		{
			review();
			return false;
		}
		else
		{
			return false;
		}
	});
	
	//validation functions
	
		function validaterating(){
		//if it's NOT valid
		var b = $('input[name=rating]').val();
		if(b == 0){
			rating.addClass("error");
			ratingInfo.text("Please select a rating");
			ratingInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			rating.removeClass("error");
			rating.text("");
			ratingInfo.removeClass("error");
			return true;
		}
	}


	function validateTitle(){
		//if it's NOT valid
		if(title.val().length < 1){
			title.addClass("error");
			titleInfo.text("Please enter title");
			titleInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			title.removeClass("error");
			titleInfo.text("");
			titleInfo.removeClass("error");
			return true;
		}
	}

	function validateComment(){
		//if it's NOT valid
		
		//alert(txtcomments.val().length);
		if(text.val().length < 1){
			text.addClass("error");
			textInfo.text("Please enter your comments");
			textInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			text.removeClass("error");
			textInfo.text("");
			textInfo.removeClass("error");
			return true;
		}
	}

	function validateCaptcha(){
		//if it's NOT valid
		if(captcha.val().length < 1){
			captcha.addClass("error");
			captchaInfo.text("Please enter code shown in box");
			captchaInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			captcha.removeClass("error");
			captchaInfo.text("");
			captchaInfo.removeClass("error");
			return true;
		}
	}
	
});
