var transparentDiv
var newimg
var name;

function browserSize()
{
	var bodyWidth = document.documentElement.clientWidth;
    var bodyHeight = document.documentElement.clientHeight;
    	
	var bodyWidth, bodyHeight; 
	
	// all except Explorer 
	if (self.innerHeight)
	{ 
		bodyWidth = self.innerWidth; 
		bodyHeight = self.innerHeight; 
	} 
	   // Explorer 6 Strict Mode 		 
	else if (document.documentElement && document.documentElement.clientHeight) 
	{
		bodyWidth = document.documentElement.clientWidth; 
		bodyHeight = document.documentElement.clientHeight; 
	} 
	// other Explorers 		 
	else if (document.body) 
	{
		bodyWidth = document.body.clientWidth; 
		bodyHeight = document.body.clientHeight; 
	} 
		
	return [bodyWidth,bodyHeight];		
}

function showSaveDialog(e,loginId,paperId,paperType,actionPage,checkDetailSignup)
{

	if(loginId=="")
	{
		var error="Please login to use this feature.";
		location.href="/Documents/intermidate_page.php?error_data="+error+"&action_page="+actionPage;
	}
	else if(checkDetailSignup=="")
	{
		location.href="/User/registration_page.php?error_type=1&show_page=2";
	}
	else
	{
		var bodyWidth = document.body.scrollWidth;
		var bodyHeight = document.body.scrollHeight;
		
		if(document.body.clientHeight > bodyHeight)
		{
			bodyHeight=document.body.clientHeight;
		}
		
		if(document.body.clientWidth > bodyWidth)
		{
			bodyWidth=document.body.clientWidth;
		}

		var dialog = document.getElementById("saveDialog");
		var fullpage = document.getElementById("fullpage");
		
		dialog.style.visibility=""
		fullpage.style.visibility=""

		fullpage.style.width=bodyWidth+"px"
		fullpage.style.height=bodyHeight+"px"

		fullpage.style.zIndex="100"
		dialog.style.zIndex="200"

		if(e.pageX!=null)
		{
			if(e.pageX > 1000)
			{
				dialog.style.left=e.pageX - 240 + "px";
				dialog.style.top=e.pageY + 10 + "px";
			}
			else
			{
				dialog.style.left=e.pageX + 10 + "px";
				dialog.style.top=e.pageY + 10 + "px";
			}

			dialog.style.height="120px"
		}
		else
		{
			var xcoord = e.clientX;
			var ycoord = e.clientY;
		
			if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
			{
				xcoord += document.body.scrollLeft;
				ycoord += document.body.scrollTop;
			} 
			else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
			{
				xcoord += document.documentElement.scrollLeft;
				ycoord += document.documentElement.scrollTop;
			}
			
			if(xcoord > 1000)
			{
				dialog.style.left= xcoord - 305  + "px";
				dialog.style.top= ycoord + 10 + "px";
			}
			else
			{
				dialog.style.left= xcoord + 10 + "px";
				dialog.style.top= ycoord + 10 + "px";
			}
		}

		xmlHttp=GetXmlHttpObject();
			
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		} 

		xmlHttp.onreadystatechange=function ()
		{ 
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
			{ 
				document.getElementById('saveDialog').innerHTML =xmlHttp.responseText;
			} 
		}
		xmlHttp.open("GET","/Paper/show_save_dialog.php?login_id="+loginId+"&paper_id="+paperId+"&paper_type="+paperType+"&sid="+Math.random(),true);
			
		xmlHttp.send(null);
	}
}

function hideDialog()
{
	var dialog = document.getElementById("saveDialog");
	var fullpage = document.getElementById("fullpage");
	document.getElementById("fullpage").visibility="hidden"
	fullpage.style.visibility="hidden"
	dialog.style.visibility="hidden"

	name="";
	
}

function validate(saveName)
{
	name=saveName.value;
	alert(name);
}

function saveInfo(loginId,paperId,flag,type,saveName,category)
{
	name=saveName.value;
	categoryName=category.value;
	
	categoryName=encodeURIComponent(categoryName);

	if(flag=="save")
	{
		var errorDisplay = document.getElementById("errorDisplay");
		var errorCombo = document.getElementById("errorCombo");
	
		var str=errorDisplay.innerHTML;
		
		//if you want to bring back the empty error.
		//if(!str.match("empty") && str!="")
		//name="";
	}	

	if(name=="" && flag=="save")
	{
		errorDisplay.innerHTML="<font size='2px' color='red'>Name cannot be empty</font>";
		return false;
	}
	else
	{
		xmlHttp=GetXmlHttpObject();
		
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		} 

		xmlHttp.onreadystatechange=function ()
		{ 
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
			{ 
				document.getElementById('saveDialog').innerHTML =xmlHttp.responseText;
			} 
		}
		xmlHttp.open("GET","/Paper/save_to_library.php?login_id="+loginId+"&paper_id="+paperId+"&flag="+flag+"&type="+type+"&save_name="+escape(name)+"&category="+categoryName+"&sid="+Math.random(),true);
		
		xmlHttp.send(null);
	}
}

/**********************************************************Combo Box***********************************************/

function selectIt(segment,combo)
{
	var test=segment.innerHTML;
	test=test.replace("&amp;","&");
	combo.value=test;
	closeList();
}

function closeList()
{
	var listBox=document.getElementById("list");
	if(listBox)
	listBox.style.visibility="hidden"
}
	
function highlight(segment)
{
	if(segment.style.backgroundColor!="skyblue")
	{
		segment.style.backgroundColor="skyblue";
	}
	else
	{
		segment.style.backgroundColor="#ffffff";
	}	
}

function showOptions(textBox)
{
	var word=textBox.value;
	var listBox=document.getElementById("list");
	
	xmlHttp=GetXmlHttpObject();
			
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	xmlHttp.onreadystatechange=function ()
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			listBox.innerHTML =xmlHttp.responseText;
			if(listBox.innerHTML=="")
				listBox.style.visibility="hidden"
			else
			{
				listBox.style.visibility=""
				listBox.style.zIndex="300"
			}
		}
	}
	xmlHttp.open("GET","/Paper/list_options.php?word="+word+"&sid="+Math.random(),true);
			
	xmlHttp.send(null);
}

/********************************************************************************************************************************/

function GetXmlHttpObject()
{
	var xmlHttp=null;

	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}