var def_dialog_title="Dialog";
var def_dialog_loading="Loading... Please Wait...";
var modal=null;
var dialog=null;
var dialog_title=null;

function show_ajax_dialog(title,page,parms)
{
	if(title=="")
	{
		dialog_title.innerHTML=def_dialog_title;
	}
	else
	{
		dialog_title.innerHTML=title;
	}
	dialog.innerHTML=def_dialog_loading;
	show(modal);
	return(ajax_load(dialog,page,parms));
}

function ajax_load(container,page,parms)
{
	var cont  = container;
	var t_parms = "ajax=1";
	var txt   = "";
	var ajax  = null;
	var scr   = "";
	var x     = 0;
	var script_pattern = /^.*\<script[^\>]*\>([^\<]*).*$/gi;
	
	if(parms!=undefined) t_parms += "&" + parms;
	load_args =  Array.prototype.slice.call(arguments).join(",");
	timeout_timer = 300;
	
	try
	{
		if(typeof(cont)=="string")
		{
			cont = document.getElementById(container);
		}
		if(typeof(cont)!="object" | cont==null)
		{
			return(error("load","Invalid container: "+container));
		}
	
		if(typeof(ajax)!="object" | ajax==null)
		{
			if(window.XMLHttpRequest)
			{// code for IE7+, Firefox, Chrome, Opera, Safari
				ajax=new XMLHttpRequest();
			}
			else
			{// code for IE6, IE5
				ajax=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	}
	catch(er)
	{
		return(error("load","Prep: " + er.message));
	}
	if(typeof(ajax)!="object" | ajax==null)
	{
		return(error("load","Invalid AJAX object"));
	}
	/*
	** Now that the AJAX object has been initialized, lets build the URL
	** and parameters and make the call.
	*/
	try
	{
		txt=cont.innerHTML;
		cont.innerHTML="<i>Loading...</i>";
//alert("ajax load: " + page + "?" + t_parms);
		ajax.open("POST",page,true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("Content-length", t_parms.length);
		ajax.setRequestHeader("Connection", "close");
		ajax.onreadystatechange = function()
			{
				if(ajax.readyState==4)
				{
					if(ajax.status==200)
					{
						cont.innerHTML=ajax.responseText;
						src=ajax.responseText.replace(/\n/gi,"");
						if(src.match(script_pattern))
						{
							src=src.replace(script_pattern,"$1");
							if(src!="" & src!=ajax.responseText)
							{
								eval(src);
							}
						}
					}
					else
					{
						cont.innerHTML="Error " + ajax.status + ' ' + page + '<br>' + load_args;
					}
				}
			}
		ajax.send(t_parms);
		return(true);
	}
	catch(er)
	{
		cont.innerHTML=txt;
		return(error("load","Call: " + er.message));
	}
}

function showhide(id)
{
	var obj=null;
	try
	{
		if(typeof(id)=="object")
		{
			obj=id;
		}
		else
		{
			obj=document.getElementById(id);
		}
		if(obj!=null)
		{
			if(obj.style.display=="none")
			{
				obj.style.display="";
			}
			else
			{
				obj.style.display="none";
			}
		}
	}
	catch(er)
	{
		// ignore errors
	}
}

function hide(id)
{
	var obj=null;
	try
	{
		if(typeof(id)=="object")
		{
			obj=id;
		}
		else
		{
			obj=document.getElementById(id);
		}
		if(obj!=null)
		{
			obj.style.display="none";
			if(obj.id=='modal') 
			{
				dialog.innerHTML=def_dialog_loading;
				dialog_title.innerHTML=def_dialog_title;
				show("dialog_title_bar");
				show("dialog_close");
			}
		}
	}
	catch(er)
	{
		error("hide",er.message);
	}
}

function show(id)
{
	try
	{
		if(typeof(id)=="object")
		{
			id.style.display="";
		}
		else
		{
			var obj=document.getElementById(id);
			if(obj!=null) obj.style.display="";
		}
	}
	catch(er)
	{
		error("show",er.message);
	}
}

function error(module,message)
{
	alert("Error!\n\n(" + module + ") " + message);
	return(false);
}

