var qlRrequest = null;
function createMPRequest()
{
	try
	{
		MPRequest = new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			MPRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				MPRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (failed) 
			{
				MPRequest = null;
			}
		}
	}
	if (MPRequest == null)
		alert("Error creating request object!");
}

function AddWatch(webUrl, lcid, itemID)
{
	hideButton("AddWatch");
	showButton("Progress");
	createMPRequest();
	//    the following line is a hack
	lcid = "MarketPlace";
	var url = webUrl + "/_layouts/" + lcid + "/MarketPlace.aspx?op=AddWatch&ItemID=" + itemID + "&dummy=" + new Date().getTime();
	MPRequest.open("GET",url, true);
	MPRequest.onreadystatechange = WatchAdded;
	MPRequest.send(null);
}
function WatchAdded()
{
	if (MPRequest.readyState == 4)
	{
		if(MPRequest.responseText != "-1")
		{
			hideButton("Progress");
			showButton("DeleteWatch");
			watchID = MPRequest.responseText;
		}
		else
		{
			alert('An error occurred!');
		}
	}
}

function DeleteWatch(webUrl, lcid, itemID)
{
	hideButton("DeleteWatch");
	showButton("Progress");
	createMPRequest();
	//    the following line is a hack
	lcid = "MarketPlace";
	var url = webUrl + "/_layouts/" + lcid + "/MarketPlace.aspx?op=DeleteWatch&ItemID=" + itemID + "&dummy=" + new Date().getTime();
	MPRequest.open("GET",url, true);
	MPRequest.onreadystatechange = WatchDeleted;
	MPRequest.send(null);
}
function WatchDeleted()
{
	if (MPRequest.readyState == 4)
	{
			
		if(MPRequest.responseText == "0")
		{
			hideButton("Progress");
			showButton("AddWatch");
		}
		else
		{
			alert('An error occurred!');
		}
	}
}

function FindWatch(webUrl, lcid, itemID)
{
    createMPRequest();
    //    the following line is a hack
    lcid = "MarketPlace";
	var url = webUrl + "/_layouts/" + lcid + "/MarketPlace.aspx?op=FindWatch&ItemID=" + itemID + "&dummy=" + new Date().getTime();
	if(document.getElementById("ModifyPageLink") == null) url += "&ANONFLAG=true";
	MPRequest.open("GET",url, true);
	MPRequest.onreadystatechange = WatchFound;
	MPRequest.send(null);
}

var watchID = '0';
function WatchFound()
{
	if (MPRequest.readyState == 4)
	{
		
			hideButton("Progress");
		
			if(MPRequest.responseText == "-2")
			{
				showButton("EditItem");
				showButton("DeleteItem");
				return;
			}
			if(MPRequest.responseText == "-1")
			{
				showButton("Contact");
				return;
			}
			if(MPRequest.responseText == "0")
			{
				showButton("Contact");
				showButton("AddWatch");
				return;
			}
			else
			{
				showButton("Contact");
				showButton("DeleteWatch");
				watchID = MPRequest.responseText;
				return;
			}
			
	}
}

var theItemID = null;
function DeleteWatchesThenItem(webUrl, lcid, itemID)
{
//    the following line is a hack
    lcid = "MarketPlace";
	theItemID = itemID;
	createMPRequest();
	var url = webUrl + "/_layouts/" + lcid + "/MarketPlace.aspx?op=DeleteWatches&ItemID=" + itemID + "&dummy=" + new Date().getTime();
	MPRequest.open("GET",url, true);
	MPRequest.onreadystatechange = WatchesDeleted;
	MPRequest.send(null);
}

function WatchesDeleted()
{
	if (MPRequest.readyState == 4)
	{
		currentItemID = theItemID;
		DeleteItemFinal();
	}
}

function hideButton(btnName)
{
	btn = document.getElementById("btn" + btnName);
	if(null != btn)
	{
		btn.style.display = 'none';
		btn.nextSibling.style.display = 'none';
	}
}

function showButton(btnName)
{
	btn = document.getElementById("btn" + btnName);
	if(null != btn)
	{
		btn.style.display = 'inline';
		btn.nextSibling.style.display = 'inline';
	}
}

var picWin;
function showPicture(img)
{
	//alert(browseris.ie55up);
	//if(browseris.ie55up)
	//{
		var oFullSizeImg = document.createElement("IMG");
  		oFullSizeImg.src = img.src;
  		
  		var iWidth = oFullSizeImg.width + 50;
  		var iHeight = oFullSizeImg.height + 50;
  		
  		var sWidth = window.screen.availWidth;
  		var sHeight = window.screen.availHeight;
  		
  		var wWidth = (iWidth >= sWidth) ? sWidth - 50 : iWidth;
  		var wHeight = (iHeight >= sHeight) ? sHeight - 50 : iHeight;

		var iX = Math.round((sWidth - wWidth) / 2);
		var iY = Math.round((sHeight - wHeight) / 2);
  		//alert("Image: " + iWidth + " x " + iHeight + 
		// "\r\nWindow: " + wWidth + " x " + wHeight + 
		// "\r\nScreen: " + sWidth + " x " + sHeight +
		// "\r\nLeft: " + iX + " Top: " + iY);
		
		//if(iWidth >= sWidth) iWidth = sWidth - 75;
  		//if(iHeight >= sHeight) iHeight = sHeight - 75;
  		
  		//iWidth = (oFullSizeImg.width >= window.screen.availWidth - 50) ? window.screen.availWidth - 50 : oFullSizeImg.width;
  		//iHeight = (oFullSizeImg.height >= window.screen.availHeight - 50) ? window.screen.availHeight - 50 : oFullSizeImg.Height;

  		picWin = window.open(oFullSizeImg.src,"picWin",
  		    "location=no,menubar=no,status=no,toolbar=no,resizable=yes");
  		picWin.resizeTo(wWidth,wHeight);
		picWin.moveTo(iX, iY);
  		picWin.focus(); 
  		//oPopup.show(iX, iY, iWidth, iHeight);
  	//} 
}

function SizeImage()
{
	var oImg = document.getElementById("webPreview_");
	if(oImg.width > 640) oImg.width=640;
} 		
