// JavaScript Document

function search_update()
{
	town = document.getElementById("browse_select").value;
	other_field = document.getElementById("other_town");
	if (town!="other")
	{
		other_field.style.visibility = "hidden";
	}
	else
	{
		other_field.style.fontStyle = "italic";
		other_field.style.color = "#666666";
		other_field.value = "enter location here";
		other_field.style.visibility = "visible";
	}
}

function browse_update()
{
	town = document.getElementById("browse_select").value;
	other_field = document.getElementById("other_town");
	if (town!="other")
	{
		window.location = "index.php?module=venue&cmd=browse&town="+town;
		other_field.style.visibility = "hidden";
	}
	else
	{
		other_field.style.fontStyle = "italic";
		other_field.style.color = "#666666";
		other_field.value = "enter location here";
		other_field.style.visibility = "visible";
	}
}

function update_browse_action()
{

}

function make_comment(venue)
{
	var oXmlHttp = new createXMLHttp();
	var comment = document.getElementById("comment").value;
	if (comment.length>0 && comment != "Enter your comment here")
	{
		oXmlHttp.open("get", "AJAX/venue_addComment.php?venue=" + venue+'&comment='+comment, true);
		oXmlHttp.onreadystatechange = function () 
		{
			if (oXmlHttp.readyState == 4) {
				comment_added(oXmlHttp.responseText, comment);
			}
		};
		oXmlHttp.send(null);
	}
	else
	{
		var message_para = document.getElementById("commentFeedbackContainer");
		message_para.innerHTML = "<p class='error'><img src='./images/symbol_error.gif' alt='error' />Please enter a comment</p>";
	}
	
	
	
}

function comment_added(status,comment)
{
		var message_para = document.getElementById("commentFeedbackContainer");
		
		//get comments list:
		//if successful add comment:
		if (status=="success")
		{
			var username = document.getElementById("username").value;
			var comment_list = document.getElementById("comment_list");
			//check if comment already exists:
			var new_comment = '<li><span class="comment_date">Just Added</span><h4>'+username+'</h4><p class="comment_para">'+comment+'</p></li>';
			if (comment_list.innerHTML.indexOf("No comments available")>=0)
			{
				comment_list.innerHTML = new_comment;
			}
			else
			{			
				comment_list.innerHTML = new_comment+comment_list.innerHTML; 
			}

			
			message_para.innerHTML = "<p class='message'><img src='./images/symbol_message.gif' alt='message' />Your comment has been added successfully</p>";
		}
		else
		{
			message_para.innerHTML = status;
		}
}

function request_block(comment)
{
	var oXmlHttp = new createXMLHttp();


	oXmlHttp.open("get", 'AJAX/venue_requestBlock.php?comment='+comment, true);
	

	
	oXmlHttp.onreadystatechange = function () 
	{
		if (oXmlHttp.readyState == 4) {
			var message_para = document.getElementById("commentFeedbackContainer");
			message_para.innerHTML = oXmlHttp.responseText;
		}
	};
	
	oXmlHttp.send(null);
}

function hide_block(link_handle)
{
	link_handle.style.visibility = "hidden";
}

function refresh_rating_image(image_number)
{
	var image_element = document.getElementById('user_rating_image');
	 
	if (image_number=='original')
	{
		image_element.src = "./images/rating_"+original_image+"_small.gif";
	}
	else
	{
		image_element.src = "./images/rating_"+image_number+"_small.gif";
		current_rating = image_number;
	}
}
	
function store_rating()
{
	if (parseInt(current_rating)>=1 && parseInt(current_rating)<=5)
	{
		original_image = current_rating;

		var oXmlHttp = new createXMLHttp();
		
		oXmlHttp.open("get", "AJAX/venue_rate.php?venue=" + base_id+'&rating='+current_rating, true);
	
		
		oXmlHttp.onreadystatechange = function () 
		{
			if (oXmlHttp.readyState == 4) {
				update_rating(oXmlHttp.responseText);
				
			}
		};
		
		oXmlHttp.send(null);
	}

	
}

function update_rating(rating_data)
{
	average_update = document.getElementById("averageRatingImage");
	numberOfVotes = document.getElementById("numberOfVotesContainer");
	
	var data = rating_data.split(";");
	
	if (data.length==2)
	{
		averageRatingImage.src = 'images/rating_'+data[0]+'_small.gif';
		averageRatingImage.alt = data[0]+'/5 stars';
		numberOfVotes.innerHTML = parseInt(data[1]);
		updateComplete("<p class='message'><img src='./images/symbol_message.gif' alt='message' />Your rating has been updated.</p>");
	}
	else
		updateComplete(rating_data);
}


function clear_town_all()
{
	all_checkbox = document.getElementById("towns_all");
	all_checkbox.checked = false;
	
}

function clear_towns()
{
	browse_form = document.getElementById("browse_venues");
	all_checkbox = document.getElementById("towns_all");
	all_is_checked = all_checkbox.checked;
	form_elements = browse_form.elements;
	for (var i=0;i<form_elements.length;i++)
	{
		if (form_elements[i].name.indexOf("town")>-1)
			form_elements[i].checked = false;
	}
	all_checkbox.checked = all_is_checked;
}

function clear_types()
{
	browse_form = document.getElementById("browse_venues");
	all_checkbox = document.getElementById("types_all");
	all_is_checked = all_checkbox.checked;
	form_elements = browse_form.elements;
	for (var i=0;i<form_elements.length;i++)
	{
		if (form_elements[i].name.indexOf("type")>-1)
			form_elements[i].checked = false;
	}
	all_checkbox.checked = all_is_checked;
}

function clear_type_all()
{
	all_checkbox = document.getElementById("types_all");
	all_checkbox.checked = false;
	
}
