function open_category(id) {
	window.location.href = 'index.php?p=product_list&cat=' + id;
}

function ajaxPost(url, postdata) {
	new Ajax.Request(url,
	{
		method:'postdata',
		postBody: postdata,
		onSuccess: function(transport) {
			if(transport.responseText.indexOf('--finished--') > 0)
				window.location.href = 'index.php?p=product_details&id=' + document.getElementById('product_id_no').value;
			else
				document.getElementById('product_reviews').innerHTML = transport.responseText;
		},
		onFailure: function()	{
			fillElement.innerHTML = 'Error loading reviews..';
		}   
	});
}

function ajaxGet(url) {
	new Ajax.Request(url,
	{
		method:'get',
		onSuccess: function(transport) {
			document.getElementById('product_reviews').innerHTML = transport.responseText;
		},
		onFailure: function()	{
			fillElement.innerHTML = 'Error loading reviews..';
		}   
	});
}

function write_review(id) {
	document.getElementById('product_reviews').innerHTML = '' +
        	'<table class="product_review_box">' +
            ' 	<tr height=100% valign=center>' +
            '    	<td width=100% align=center>' +
            '        	<img src="product_loading.gif" />' +
            '        </td>' +
            '    </tr>' +
            '</table>';
	var new_page = '';
	for(var i = 1; i <= document.getElementById('product_rating_page_count').value; i++)
		new_page = new_page + '<a href="javascript:review_page(\'' + id + '\', \'' + i + '\');">' + i + '</a> ';
	document.getElementById('product_review_page_bar').innerHTML = new_page;
	var rand = 0;
	rand = String(Math.floor((Math.random() * 1000)));
	ajaxGet('product_write_review.php?id=' + id + '&rand=' + rand);
}

function review_page(id, page) {
	document.getElementById('product_reviews').innerHTML = '' +
        	'<table class="product_review_box">' +
            ' 	<tr height=100% valign=center>' +
            '    	<td width=100% align=center>' +
            '        	<img src="product_loading.gif" />' +
            '        </td>' +
            '    </tr>' +
            '</table>';
	var new_page = '';
	for(var i = 1; i <= document.getElementById('product_rating_page_count').value; i++) {
		if(i == page)
			new_page = new_page + i + ' ';
		else
			new_page = new_page + '<a href="javascript:review_page(\'' + id + '\', \'' + i + '\');">' + i + '</a> ';
	}
	document.getElementById('product_review_page_bar').innerHTML = new_page;
	var rand = 0;
	rand = String(Math.floor((Math.random() * 1000)));
	ajaxGet('product_reviews.php?id=' + id + '&p=' + page + '&rand=' + rand);
}

function upload_review() {
	var product_id = document.getElementById('review_product_id').value;
	var check = document.getElementById('review_check').checked;
	var comments = document.getElementById('review_comments').value;
	
	if(check) {
		if(comments.length < 15 || comments.length > 500) {
			alert('Your comments must be between 15 and 500 characters in length');
			return;
		}
	} else if(comments.length > 0)
		if(!confirm('If you wish to leave comments, you must tick the Leave Comments box, are you sure you want to continue?'))
			return;
	
	var rating1 = document.getElementById('star_value_rating1').value;
	var rating2 = document.getElementById('star_value_rating2').value;
	var rating3 = document.getElementById('star_value_rating3').value;
	var rating4 = document.getElementById('star_value_rating4').value;
	var rating5 = document.getElementById('star_value_rating5').value;
	
	if(rating1 == 0 && rating2 == 0 && rating3 == 0 && rating4 == 0 && rating5 == 0)
		if(!confirm('You have given 0 stars to every point, are you sure?'))
			return;
	ajaxPost('product_write_review.php?id=' + product_id, 'rating1=' + rating1 + '&rating2=' + rating2 + '&rating3=' + rating3 + '&rating4=' + rating4 + '&rating5=' + rating5 + '&comments=' + escape(comments));
	
	
	document.getElementById('product_reviews').innerHTML = '' +
        	'<table class="product_review_box">' +
            ' 	<tr height=100% valign=center>' +
            '    	<td width=100% align=center>' +
            '        	<img src="product_loading.gif" />' +
            '        </td>' +
            '    </tr>' +
            '</table>';	
}

function star_mouseout(evt, id) {
	var rating = document.getElementById('star_value_' + id).value;
	document.getElementById('star_value_' + id).value = rating;
	document.getElementById('star_div_' + id).style.width = (rating * 16) + 'px';
	if(rating > 0)
		document.getElementById('star_div_' + id).style.visibility = 'visible';
	else
		document.getElementById('star_div_' + id).style.visibility = 'hidden';
	document.getElementById('star_div_' + id).style.background = 'url(\'/product_star1.gif\')';
}

function star_mousemove(evt, id) {
	xy = getEventOffsetXY( evt );
	x = xy[0];
	var percentage = (100 / 80) * x;
	var rating = Math.round((percentage / 20) + 0.45);
	document.getElementById('star_div_' + id).style.width = (rating * 16) + 'px';
	if(rating > 0)
		document.getElementById('star_div_' + id).style.visibility = 'visible';
	else
		document.getElementById('star_div_' + id).style.visibility = 'hidden';
	document.getElementById('star_div_' + id).style.background = 'url(\'/product_star2.gif\')';
}

function getEventOffsetXY( evt )
{
	if ( evt.offsetX != null )
		return [ evt.offsetX , evt.offsetY ];

    var obj = evt.target || evt.srcElement;
   	setPageTopLeft( obj );
    return [ ( evt.clientX - obj.pageLeft ) , ( evt.clientY - obj.pageTop ) ];
}

function setPageTopLeft( o )
{
    var top = 0, left = 0, obj = o;

    while ( o.offsetParent )
     {
         left += o.offsetLeft ;
         top += o.offsetTop ;
         o = o.offsetParent ;
    };

    obj.pageTop = top;
    obj.pageLeft = left;

}

function star_click(evt, id) {
	xy = getEventOffsetXY( evt );
	x = xy[0];
	var percentage = (100 / 80) * x;
	var rating = Math.round((percentage / 20) + 0.45);
	if(rating == document.getElementById('star_value_' + id).value)
		rating = 0;
	document.getElementById('star_value_' + id).value = rating;
	document.getElementById('star_div_' + id).style.width = (rating * 16) + 'px';
	if(rating > 0)
		document.getElementById('star_div_' + id).style.visibility = 'visible';
	else
		document.getElementById('star_div_' + id).style.visibility = 'hidden';
		
	document.getElementById('star_div_' + id).style.background = 'url(\'/product_star1.gif\')';
}

function recalculate_chars() {
	var com_length = document.getElementById('review_comments').value.length;
	if(com_length > 500)
		com_length = 500;
	document.getElementById('review_comments').value = document.getElementById('review_comments').value.substr(0, 500);
	var remaining = 500 - com_length;
	document.getElementById('review_chars').innerHTML = '<font size=-1 color="#CCCCCC">' + remaining + ' characters remaining</font>';
	if(com_length > 500)
		return false;
	return true;
}
