var productRating = document.getElementById('product_rating');

function hoverStar(n)
{
  var nmax = 5;

  for (var i = 1; i <= nmax; i++)
  {
    var id = 'star_' + i;

    if (i <= n)
    {
      document.getElementById(id).className = 'star_icon_hover';
    }
    else
    {
      document.getElementById(id).className = 'star_icon';
    }
  }
}

function resetStars()
{
  var nmax = 5;

  for (var i = 1; i <= nmax; i++)
  {
    var id = 'star_' + i;

    if (i <= productRating.value)
    {
      document.getElementById(id).className = 'star_icon_hover';
    }
    else
    {
      document.getElementById(id).className = 'star_icon';
    }
  }
}

var jr = jQuery.noConflict();

var productRatingID = 'product_rating';

var reviewStar1 = 'star_1';
var reviewStar2 = 'star_2';
var reviewStar3 = 'star_3';
var reviewStar4 = 'star_4';
var reviewStar5 = 'star_5';

jr('#star_1').click(function() {
  document.getElementById(productRatingID).value = 1
});

jr('#star_2').click(function() {
  document.getElementById(productRatingID).value = 2
});

jr('#star_3').click(function() {
  document.getElementById(productRatingID).value = 3
});

jr('#star_4').click(function() {
  document.getElementById(productRatingID).value = 4
});

jr('#star_5').click(function() {
  document.getElementById(productRatingID).value = 5
});

var jrt = jQuery.noConflict();

jrt(document).ready(function() {
	
	//if submit button is clicked
	jrt('#add_review_button').click(function () {		
			
		//Get the data from all the fields
		var user_name = jrt('input[name=user_name]');
		var user_location = jrt('input[name=user_location]');
		var product_id = jrt('input[name=product_id]');
		var rating = jrt('input[name=rating]');
		var comment = jrt('textarea[name=comment]');

		if (user_name.val()=='Your Name...') {
			user_name.addClass('hightlight');
			return false;
		} else user_name.removeClass('hightlight');
		
		if (user_location.val()=='Your Location...') {
			user_location.addClass('hightlight');
			return false;
		} else user_location.removeClass('hightlight');
		
		if (comment.val()=='Your review goes here!') {
			comment.addClass('hightlight');
			return false;
		} else comment.removeClass('hightlight');

		//organize the data properly
		var data = 'user_name=' + user_name.val() + '&user_location=' + user_location.val() + '&product_id=' + product_id.val() + '&rating=' + 
		rating.val() + '&comment='  + encodeURIComponent(comment.val());
		
		//disabled all the text fields
		jrt('.text').attr('disabled','true');
		
		//show the loading sign
		jrt('.loading').show();
		
		//start the ajax
		jrt.ajax({
			//this is the php file that processes the data
			url: "./process_reviews.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				//if process_reviews script returned 1/true (send success)
				if (html==1) {					
					//hide the form					
					jrt('.form').fadeOut('slow');					
					
					document.getElementById('addReview').style.display = 'none';
					//show the success message
					jrt('.done').fadeIn('fast');
					
				//if process_reviews.php returned 0/false (send failed)
				} else { 
					//hide the loading sign
					jrt('.loading').hide();
					alert('Sorry, unexpected error. Please try again later.'); 				
				}
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
});	
