var animate_popup=2;	//This variable cant take 0 or 1; Set value to 0 for no animations and set value to 1 to show animated popup;

document.onmousedown=function()
{
	hide_options();
}

/*
This function will disable the page
*/
function disable_background()
{
	var x=document.createElement('div');	
	x.setAttribute('id','background');
	x.setAttribute('style','display:block');
	document.body.appendChild(x);

	document.getElementById('background').style.position='absolute';
	document.getElementById('background').style.display='block';
	document.getElementById('background').style.top=0;
	document.getElementById('background').style.left=0;
	document.getElementById('background').style.zIndex=2;

	if(document.body.scrollHeight>screen.height)
	{
		document.getElementById('background').style.height=document.body.scrollHeight+"px";
	}
	else
	{
		document.getElementById('background').style.height=screen.height+"px";
	}
	//document.getElementById('background').style.width=document.body.scrollWidth;	
	document.getElementById('background').style.width=document.body.clientWidth+"px";	
	document.getElementById('background').style.MozOpacity=.50;
}

/*
This function will show popup window.
It needs two parameters
	1) URL of the page which is to be opened in pop up box.
	2) Title to be shown on Pop Up box.
*/
function show_popup(url,window_title,method,params)
{
	var popup=document.createElement('div');	
	popup.setAttribute('id','popup');
	document.body.appendChild(popup);

	var title=document.createElement('div');
	title.setAttribute('id','title');
	document.getElementById('popup').appendChild(title);

	var body=document.createElement('div');	
	body.setAttribute('id','body');
	document.getElementById('popup').appendChild(body);

	document.getElementById('popup').style.position='absolute';
	document.getElementById('popup').style.border='2px solid black';
	//document.getElementById('popup').style.display="block";
		
	document.getElementById('title').innerHTML="<a style='color:white;text-decoration:none' href='#' onClick='hide_popup()'><img src='images/close.gif' border='0' width='20'></a><font style='padding:5px;font-weight:bold' face='verdana' size='3'>" + window_title + "</font>";
	document.getElementById('title').style.border='1px solid black';	
			
	disable_background();
	if(method=="POST")
	{
		ajax_post(url,'body',params);
	}
	else
	{
		ajax(url,'body');
	}

	document.getElementById('popup').style.top="-500px";
	document.getElementById('popup').style.left="-500px";
	if(animate_popup==1)
	{
		animate(10);
	}
	else if(animate_popup==2)
	{
		animate1(10);
	}
	else
	{
		no_animation(10);
	}
}

/*
Show Popup with animation
*/
function animate(ctr)
{
	if(ctr<(document.body.clientHeight-document.getElementById('popup').clientHeight)/2)	
	{
		document.getElementById('popup').style.top=ctr+"px";
		document.getElementById('popup').style.left=(screen.width-document.getElementById('popup').clientWidth)/2+"px";
		ctr=ctr+5;
		setTimeout("animate("+ ctr +")",1);
	}
}

function animate1(ctr)
{
	if(ctr<document.getElementById('popup').clientHeight)
	{
		document.getElementById('popup').style.top=(-document.getElementById('popup').clientHeight+ctr)+"px";
		document.getElementById('popup').style.left="0px";
		document.getElementById('popup').style.left=(screen.width-document.getElementById('popup').clientWidth)/2+"px";
		//document.getElementById('popup').style.left="100px";
		ctr=ctr+10;
		setTimeout("animate1("+ ctr +")",50);
	}
}


/*
Show Popup without animation
*/
function no_animation(ctr)
{
	document.getElementById('popup').style.top=(document.body.clientHeight-document.getElementById('popup').clientHeight)/2+"px";
	document.getElementById('popup').style.left=(screen.width-document.getElementById('popup').clientWidth)/2+"px";
	setTimeout("no_animation("+ ctr +")",1);
}

/*
This function will hide popup box
*/
function hide_popup()
{
	document.body.removeChild(document.getElementById('popup'));
	document.body.removeChild(document.getElementById('background'));
	//document.getElementById('popup').style.display="none";
}

/*
This function will show option menu like right click.
It takes two parameters:
	1) Event Object
	2) URL of page which will contain options to be shown.
*/
function show_options(e,url)
{
	var outer_box=document.createElement('div');
	outer_box.setAttribute('id','outer_box');
	document.body.appendChild(outer_box);

	var inner_box=document.createElement('div');
	inner_box.setAttribute('id','inner_box');
	document.getElementById('outer_box').appendChild(inner_box);

	document.getElementById("outer_box").style.position='absolute';
	document.getElementById("inner_box").style.border='1px solid';

	var explorer=navigator.userAgent.toLowerCase();
	
	if(explorer.indexOf("msie")>-1)
	{
		document.getElementById("outer_box").style.display='block';
		document.getElementById("outer_box").style.top=event.y +  document.documentElement.scrollTop;
		document.getElementById("outer_box").style.left=event.x + document.documentElement.scrollLeft;	
	}
	else
	{
		document.getElementById("outer_box").style.display='block';
		document.getElementById("outer_box").style.top=e.pageY + "px";
		document.getElementById("outer_box").style.left=e.pageX + "px";
	}
	ajax(url,'inner_box');
}
/*
This function will simply hide options menu
*/
function hide_options()
{	
	if(document.getElementById("outer_box")!=null)
	{
		document.getElementById("outer_box").style.display="none";
	}
}
/*
This function call a page dinamically.
It takes two parameters
	1) URL of page to be called
	2) id of element where result is to be shown
*/
function ajax(url,result)
{
	var xmlHttp;
	document.getElementById(result).innerHTML="";
	try	
	{  
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}
	catch (e)
	{  
		// Internet Explorer  
		try
		{   
		  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}
		catch (e)
		{    
			try	
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}
			catch (e)
			{      
				alert("Your browser does not support AJAX!");      
				return false;      
			}    
		}  
	}
	
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{			
			document.getElementById(result).innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
/*
This function call a page dinamically using POST method.
It takes three parameters
	1) URL of page to be called
	2) id of element where result is to be shown
	3) Parameter to be posted.
*/
function ajax_post(url,result,params)
{
	var xmlHttp;
	try	
	{  
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}
	catch (e)
	{  
		// Internet Explorer  
		try
		{   
		  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}
		catch (e)
		{    
			try	
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}
			catch (e)
			{      
				alert("Your browser does not support AJAX!");      
				return false;      
			}    
		}  
	}	

	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById(result).innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.send(params);
}