// JavaScript Document
var oXmlHttp;

function createXMLHttp() 
{
	if (typeof XMLHttpRequest != "undefined") {
		return new XMLHttpRequest();
	} 
	else if (window.ActiveXObject)
	{
		var aVersions = [ "MSXML2.XMLHttp.5.0",
		"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
		"MSXML2.XMLHttp","Microsoft.XMLHttp"
		];
		for (var i = 0; i < aVersions.length; i++) 
		{
			try {
				var oXmlHttp = new ActiveXObject(aVersions[i]);
				return oXmlHttp;
			} 
			catch (oError) {}
		}	
	}
	throw new Error("XMLHttp object could be created.");
}

function add_to_favourites(type,id)
{
	
	var oXmlHttp = new createXMLHttp();
	
	oXmlHttp.open("get", "AJAX/"+type+"_addToFavourites.php?"+type+"=" + id, true);
	
	oXmlHttp.onreadystatechange = function () 
	{
		if (oXmlHttp.readyState == 4) {
			updateComplete(oXmlHttp.responseText);
			
			if (oXmlHttp.responseText.indexOf('message')>0)
			{				
				favourite_link = document.getElementById("favourite_"+id);
				
				favourite_link.href="javascript:remove_from_favourites('"+type+"',"+id+")";
favourite_link.innerHTML = '<img src="images/symbol_favourite_remove.gif" alt="Remove From Favourites"/>';
				if (document.URL.indexOf("view")>0)
				{
					favourite_link.innerHTML +=" Remove From Favourites"
				}

			}
		}
	};
	
	oXmlHttp.send(null);
}

function remove_from_favourites(type,id)
{
	var oXmlHttp = new createXMLHttp();
	
	oXmlHttp.open("get", "AJAX/"+type+"_removeFromFavourites.php?"+type+"=" + id, true);
	
	oXmlHttp.onreadystatechange = function () 
	{
		if (oXmlHttp.readyState == 4) {
			updateComplete(oXmlHttp.responseText);
			if (oXmlHttp.responseText.indexOf('message')>0)
			{
				favourite_link = document.getElementById("favourite_"+id);
				favourite_link.href="javascript:add_to_favourites('"+type+"',"+id+")";
				favourite_link.innerHTML = '<img src="images/symbol_favourite_add.gif" alt="Add to Favourites"/>';
				if (document.URL.indexOf("view")>0)
				{
					favourite_link.innerHTML +=" Add to Favourites"
				}
			}
		}
	};
	
	oXmlHttp.send(null);
}

function updateComplete(status)
{
		var message_para = document.getElementById("feedbackContainer");
		message_para.innerHTML = status;
}


function select_text(text_area){
		if ((text_area.value.toLowerCase()).indexOf("enter")>-1)
		{
			text_area.value = '';
			text_area.style.fontStyle = "normal";
			text_area.style.color = "#000000";
		}
}

function create_popup(command, params, width, height)
{
	newWindow = window.open( "index.php?module=popup&cmd="+command+"&"+params , "pubipediapopup","height="+height+",width="+width+",toolbar=yes,menubar=yes,location=yes,status=yes,left=100,top=100");
	return newWindow;
}


var qs_text_showing;

function check_qs_town()
{
	var town_dd = document.getElementById("qs_town");
	var option =  town_dd.options[town_dd.selectedIndex];
	if (option.value=="-2")
	{
		qs_text_showing = true;
		updateComplete("<p class='message'><img src='images/symbol_message.gif' alt='message'>Please add the town name of where you are looking to the search box with the other details you have entered.</p>");
	}
	else
	{
		if (qs_text_showing)	
			updateComplete("");
	}
}

function update_page(module, command)
{
	town_select = document.getElementById("update_town");
	window.location = "index.php?module="+module+"&cmd="+command+"&town="+town_select.value;
}