document.writeln('<script type="text/javascript" src="/javascript/Cookie.js"></script>');

function PR_Shop_Review(id,
						shop_id,
						user_id,
						user_fullname,
						text_good,
						text_bad,
						text_overall,
						count_helpful,
						count_unhelpful,
						rating,
						insert_datestr) {
	this.var_name		= '';
	this.HTMLElement_id	= '';
	
	this.id						= id;
	this.shop_id				= shop_id;
	this.user_id				= user_id;
	this.user_fullname			= user_fullname;
	this.text_good				= text_good;
	this.text_bad				= text_bad;
	this.text_overall			= text_overall;
	this.count_helpful			= parseInt(count_helpful);
	this.count_unhelpful		= parseInt(count_unhelpful);
	this.rating					= rating;
	this.insert_datestr			= insert_datestr;
}

PR_Shop_Review.prototype.set_var_name			= function (var_name){
	this.var_name				= var_name;
}

PR_Shop_Review.prototype.set_HTMLElement_id		= function (HTMLElement_id){
	this.HTMLElement_id			= HTMLElement_id;
}

PR_Shop_Review.prototype.increment_count_helpful	= function () {
	this.count_helpful++;
	
	var xml_http;
	
	if (window.XMLHttpRequest) {
		xml_http = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xml_http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xml_http != null) {
		xml_http.open('GET', 'update_shop_review_votes_action.php?shop_review_id=' + this.id + '&count_helpful=' + this.count_helpful + '&count_unhelpful=' + this.count_unhelpful, false);
		xml_http.send(null);
		//alert(xml_http.responseText);
	} else {
		alert("Your browser does not support XMLHTTP.")
	}
}

PR_Shop_Review.prototype.increment_count_unhelpful	= function () {
	this.count_unhelpful++;
	
	var xml_http;
	
	if (window.XMLHttpRequest) {
		xml_http = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xml_http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xml_http != null) {
		xml_http.open('GET', 'update_shop_review_votes_action.php?shop_review_id=' + this.id + '&count_helpful=' + this.count_helpful + '&count_unhelpful=' + this.count_unhelpful, false);
		xml_http.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.")
	}
}

PR_Shop_Review.prototype.set_voted					= function(is_voted) {
	var pr_shop_review_data	= new Cookie(document, 'pr_shop_review_' + this.id, 240);
	if (is_voted) {
		pr_shop_review_data.is_voted = true;
		pr_shop_review_data.store();
	} else {
		pr_shop_review_data.remove();
	}
}

PR_Shop_Review.prototype.is_voted					= function() {
	var pr_shop_review_data	= new Cookie(document, 'pr_shop_review_' + this.id, 240);
	return pr_shop_review_data.load();
}

PR_Shop_Review.prototype.refresh_vote					= function() {
	if(document.getElementById){
		  document.getElementById(this.HTMLElement_id + '_votes').innerHTML = this.get_votes_HTML();
	} else if (document.all){
		  document.all[this.HTMLElement_id + '_votes'].innerHTML=this.get_votes_HTML();
	} else if (document.layers){
		  with(document.layers[this.HTMLElement_id + '_votes'].document){
				open();
				write(this.get_votes_HTML());
				close();
		  }
	}
}

PR_Shop_Review.prototype.get_votes_HTML = function() {
	var total_votes				= this.count_helpful + this.count_unhelpful;
	var percentage_helpful		= (this.count_helpful / total_votes) * 100;
	var percentage_helpful		= Math.round(percentage_helpful*100)/100;					// round "percentage_helpful" to 2 decimals
	
	var ret_val					= '';
	if (total_votes > 0){
		ret_val					+= percentage_helpful + '% of People (' + this.count_helpful + '/' + total_votes+ ') found this review helpful. ';
	}
	
	if (! this.is_voted()) {
		ret_val					+= 'Is this review <a href="javascript: ' + this.var_name + '.increment_count_helpful(); ' + this.var_name + '.set_voted(true); ' + this.var_name + '.refresh_vote()">helpful</a> or <a href="javascript: ' + this.var_name + '.increment_count_unhelpful(); ' + this.var_name + '.set_voted(true); ' + this.var_name + '.refresh_vote(); ">unhelpful</a> for you?';
	}
	
	return ret_val;
}