var review_rating_set = false;
var $j = jQuery.noConflict();
$j(function(){
	var tip = $j('#star-rating #tip');
	var tip_data = tip.html();
	$j('#star-rating input.star1').rating({
		required: true, 
		focus: function(value, link){
			var tip = $j('#star-rating #tip');
			tip[0].data = tip[0].data || tip.html();
			tip.html(link.title || 'value: '+value);
		},
		blur: function(value, link){
			var tip = $j('#star-rating #tip');
			$j('#star-rating #tip').html(tip[0].data || '');
		},
		callback: function(value, link){
			review_rating_set = true;
		}
	});
	tip.html(tip_data);
});


jQuery.fn.fadeToggle = function(s, fn){
    return (this.is(":visible"))
        ? this.fadeOut(s, fn)
        : this.fadeIn(s, fn);
};

jQuery(function($j){
    $j("#click").click(function(){
		$j("#click img").toggle();
        $j("#product_review").slideToggle("slow");
    });
	//$j("#product_review").draggable();
});

$j(document).ready(function() { 

	$j('#star-rating-form').submit(function() {
	
	
	$j("#review_title_error").empty().hide();
	$j("#review_name_error").empty().hide();
	$j("#email_address_error").empty().hide();
	$j("#review_comment_error").empty().hide();
	$j("#security_code_error").empty().hide();
	$j("#rating_error").empty().hide();
	
	
	
	
function validateForm()
{
    var reviewer_name = $j("#review_name").val(); 
    var review_title = $j("#review_title").val();
	var email_address = $j("#email_address").val(); 
	var comment = $j("#review_comment").val(); 
	var security_code = $j("#security_code").val();
	var rating = $j("input[@name='rating']").val();
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  
    var errors = 0; 
	
    if (reviewer_name == null || reviewer_name == '') 
    {
        $j("#review_name_error").show().html("Reviewer name is required"); 
        errors++; 
    }
	 
 	if (review_title == null || review_title == '') 
    { 
        $j("#review_title_error").show().html("Review title is required"); 
        errors++; 
    }
	
	if (email_address == null || email_address == '') 
    { 
        $j("#email_address_error").show().html("Email is required"); 
        errors++; 
    }
	
	if (reg.test(email_address) == false)
	{
		$j("#email_address_error").show().html("The Email address is not valid"); 
        errors++; 
	}
	
	if (comment == null || comment == '') 
    { 
        $j("#review_comment_error").show().html("Comment is required"); 
        errors++; 
    }
	
	if (security_code == null || security_code == '') 
    { 
        $j("#security_code_error").show().html("Security code is required"); 
        errors++; 
    } 
	
	
	if (rating < 1 || rating > 5) 
    { 
        $j("#rating_error").show().html("Rating is required"); 
        errors++; 
    } 
	
    if (errors > 0) 
    { 
        //alert ("Errors were found on the form"); 
        return false; 
    } 
 
}         
	validateForm();
	
	// prepare Options Object 
	var options = { 
		target:     '#message',  
		success:    function(msg) { 
			if(msg == 'Thank you for reviewing this product.') {
				var rating = $j("input[@name='rating']").val();

				$j('#product_review').fadeOut();
				$j('.reviewtitle2').remove();
				$j('#click').remove();

				if(review_rating_set) {	
					var disabled_stars = "<div id=\"average-star-rating-ro\">";
					for(i=0; i<5; i++) {
						disabled_stars += "<input class=\"average-star\" type=\"radio\" name=\"average-rating-ro\"";
						if(i < rating) disabled_stars += " checked=\"checked\"";
						disabled_stars += " />";
					}
					disabled_stars += "<span id=\"average-tip\" style=\"margin:0 0 0 5px; font-size: 8px;\"><strong>Thanks for rating!</strong></span></div>";
					
					$j('#average-star-rating').remove();
					$j('#ratingstars').html(disabled_stars);
					
					// triggered change event to delay converting input boxes to ratings
					// when the html for #ratingsstars is changed. Fixes problem where conversion
					// of input boxes to ratings is run before html is updated, by adding a small delay
					$j('#ratingstars').bind('change', function(){
						$j('#average-star-rating-ro input').rating({readOnly: true});
					});
					
					$j('#ratingstars').trigger('change');
					$j('#ratingstars').unbind();
					// end
				}
			}
		}
	};
	
	// submit the form 
	$j(this).ajaxSubmit(options);
	//$j('#product_review').fadeOut();
	//$j('#click').fadeOut();
	//$j('#message').fadeIn();
	
	var d = new Date();
	var cb = d.getSeconds() + "" + d.getMilliseconds();
	var captcha_img = '<img src="CaptchaSecurityImages.php?width=100&height=40&characters=5&cb=' + cb + '" />';
	$j('#captcha').html(captcha_img);
	 
	// return false to prevent normal browser submit and page navigation 
	return false; 
	}); 
});