var PopWin = null;
var n = Math.round(Math.random()*65535);
var MyWindowTitle = "_MyPW0" + n.toString();
if (typeof(PopupStatus) == "undefined")
{
	var PopupStatus = 0;
}
var PopupSizeX = 700;
var PopupSizeY = 500;
var DivBody = "";
if ((self.opener != null) && (self.name.substr(0,6) == "_MyPW0"))
{
	PopupSizeX -= 50;
	PopupSizeY -= 50;
	self.PopupStatus |= self.opener.PopupStatus;
	if (self.opener.opener != null)
	{
		if (self.opener.opener.opener != null)
		{
			// 3 level maximum on popups
			self.PopupStatus = 0;
		}
	}
	if (PopupSizeX < 400)
	{
		PopupSizeX = 400;
	}
	if (PopupSizeY < 300)
	{
		PopupSizeY = 300;
	}
}
function SelectPopUp(sel)
{
	PopupStatus = sel.selectedIndex;
}
function PopupLink(file, x, y)
{
	if (PopupStatus)
	{
		var options = "scrollbars,dependent='yes',resizable,height=" + y + ",width=" + x;
		PopWin = open(file, MyWindowTitle, options);
		PopWin.focus();
	}
	else
	{
		window.location=file
	}
}
// Popup file
function PopupFile(file)
{
	if (PopupStatus)
	{
		PopupLink(file, PopupSizeX, PopupSizeY);
	}
	else
	{
		window.location=file
	}
}
// Popup file
function PopupWebsite(file)
{
	if (PopupStatus)
	{
		PopupLink(file, PopupSizeX, PopupSizeY);
	}
	else
	{
		var options = "scrollbars,resizable,titlebar,toolbar,status";
		PopWin = open(file, MyWindowTitle+"f", options);
		PopWin.focus();
	}
}
// Return filename of current document
function FileNameGet()
{
	var fn = document.location.href;
	var end = (fn.indexOf("?") < 0) ? fn.length : fn.indexOf("?");
	return fn.substring(fn.lastIndexOf("/")+1, end);
}
function SubdomainLink(subdom, file, dom)
{
	var host;
	if (typeof(dom) == "undefined")
	{
		host = top.location.host;
		var HostArray = host.split(".");
		var HostLen = HostArray.length;
		host = HostArray[HostLen-2] + "." + HostArray[HostLen-1];
	}
	else
	{
		host = dom;
	}
	var FileName = "http://" + subdom + "." + host + "/" + file;
	window.location=FileName;
}
function FileLink(link)
{
	var FileName = link +"."+"php"
	window.location=FileName
}
function AlertFunction(txt)
{
	alert(txt);
}
function ReplaceDocumentText(txt)
{
	// document.documentElement or document.all[] is root
	document.all[0].innerHTML = txt;
}
function FormCgiParamsGet(form, ArrayNames)
{
	var frm = document.forms[form];
	var size;
	var params = "";
	var c;
	for (size = 0; size < frm.elements.length; size++)
	{
		var name = frm.elements[size].name;
		var value = frm.elements[size].value;
		if (typeof(ArrayNames) != "undefined")
		{
			var ary = ArrayNames.split(',');
			for (c = 0; c < ary.length; c++)
			{
				var len;
				len = ary[c].length;
				if (name.length == (len+1))
				{
					if (ary[c] == name.substr(0, len))
					{
						var ch;
						ch = name.charCodeAt(len);
						if (ch >= 48 && ch <= 57)
						{
							// This is an array
							name = ary[c] + "[]";
						}
					}
				}
			}
		}
		if (frm.elements[size].type.toLowerCase() == 'submit')
		{
			name = 'submit';
			value = '1';
		}
		if (frm.elements[size].type.toLowerCase() == 'radio')
		{
			if (!frm.elements[size].checked)
			{
				name = '';
				value = '';
			}
		}
		if (name.length && value.length)
		{
			if (params.length)
			{
				params += "&";
			}
			params += name;
			params += "=";
			params += escape(value);
		}
	}
	if (0)
	{
		PopWin = open('', MyWindowTitle, "scrollbars,dependent='yes',resizable,height=200,width=400");
		PopWin.focus();
		PopWin.document.write('params = <BR>');
		PopWin.document.write(params + '<BR>');
	}
	return params;
}
function CgiFileText(FileName, params, response, RspDat)
{
	var rv = "";
	var srv = false;
	try
	{
		// Opera 8.0+, Firefox, Safari
		srv = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			srv = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				srv = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return "Javascript/Ajax error, Try updating your browser";
			}
		}
	}
	srv.open('POST', FileName, true);
	srv.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	srv.setRequestHeader("Content-length", params.length);
	srv.setRequestHeader("Connection", "close");
	srv.onreadystatechange = function()
	{
		if (srv.readyState == 4)
		{
			rv = srv.responseText;
			if (typeof(response) != "undefined" ? response.length : false)
			{
				if (typeof(RspDat) != "undefined")
				{
					response(rv, RspDat);
				}
				else
				{
					response(rv);
				}
			}
			else
			{
				PopWin.document.write(rv);
			}
		}
	}
	srv.send(params);
}
function AlertCgiFileText(form, FileName, ArrayNames)
{
	CgiFileText(FileName, FormCgiParamsGet(form, ArrayNames), AlertFunction);
}
function PopupCgiFileText(form, FileName, x, y, text, ArrayNames)
{
	if (typeof(x) == "undefined")
	{
		x = PopupSizeX;
	}
	if (typeof(y) == "undefined")
	{
		y = PopupSizeY;
	}
	var options = "scrollbars,dependent='yes',resizable,height=" + y + ",width=" + x;
	PopWin = open('', MyWindowTitle, options);
	PopWin.focus();
	if (typeof(text) != "undefined" ? text.length : false)
	{
		PopWin.document.write(text);
	}
	CgiFileText(FileName, FormCgiParamsGet(form, ArrayNames));
}

// Popup topic in php file that does own topic filtering
function PopupPhpTopic(file, id)
{
	var fn;
	if (PopupStatus)
	{
		fn = file + "?topic=" + id;
		if (DivBody.length)
		{
			fn += "&DivBody=" + DivBody;
		}
		var options = "scrollbars=1,dependent=1,resizable=1,location=1,status=0,menubar=0,height=" + PopupSizeY + ",width=" + PopupSizeX;
		if (PopWin ? !PopWin.closed : false)
		{
			PopWin.focus();
			PopWin.location=fn;
		}
		else
		{
			PopWin = open(fn, MyWindowTitle, options);
		}
	}
	else
	{
		fn = file + "#" + id;
		window.location=fn;
	}
	PopWin.resizeTo(PopupSizeX,PopupSizeY)
}
function PopupPhpTopic1(fn, id)
{
	var OldPop = PopupStatus;
	PopupStatus = 1;
	PopupPhpTopic(fn, id);
	PopupStatus = OldPop;
}
function PopupPhpTopic0(fn, id)
{
	var OldPop = PopupStatus;
	PopupStatus = 0;
	PopupPhpTopic(fn, id);
	PopupStatus = OldPop;
}

// Popup topic
function PopupTopic(file, id)
{
	var fn;
	if (PopupStatus)
	{
		fn = "file=" + file + "&pop=1" + "&id=" + id;
		if (DivBody.length)
		{
			fn += "&DivBody=" + DivBody;
		}
		var options = "scrollbars=1,dependent=1,resizable=1,location=1,status=0,menubar=0,height=" + PopupSizeY + ",width=" + PopupSizeX;
		if (PopWin ? !PopWin.closed : false)
		{
			PopWin.document.getElementsByTagName('body')[0].innerHTML = '';
		}
		else
		{
			PopWin = open('', MyWindowTitle, options);
		}
		PopWin.focus();
		CgiFileText('./SharedServer/php/FileTextFunction.php', fn);
	}
	else
	{
		fn = file + "#" + id;
		window.location=fn;
	}
}

function ShowIncludeTopic(txt, div)
{
	document.getElementById(div).innerHTML = txt;
}

// Popup topic
function IncludeTopic(file, id, div)
{
	var fn = "file=" + file + "&pop=0" + "&id=" + id;
	CgiFileText('./SharedServer/php/FileTextFunction.php', fn, ShowIncludeTopic, div);
}

function PopupTopic1(fn, id)
{
	var OldPop = PopupStatus;
	PopupStatus = 1;
	PopupTopic(fn, id);
	PopupStatus = OldPop;
}
function PopupTopic0(fn, id)
{
	var OldPop = PopupStatus;
	PopupStatus = 0;
	PopupTopic(fn, id);
	PopupStatus = OldPop;
}

// Popup topic
function PopupCategory(file, cat, pop)
{
	var fn;
	if (typeof(pop) == "undefined")
	{
		pop = 0;
	}
	fn = "file=" + file + "&cat=" + cat + "&pop=" + pop;
	if (DivBody.length)
	{
		fn += "&DivBody=" + DivBody;
	}
	if (PopupStatus)
	{
		var options = "scrollbars=1,dependent=1,resizable=1,location=1,status=0,menubar=0,height=" + PopupSizeY + ",width=" + PopupSizeX;
		if (PopWin ? !PopWin.closed : false)
		{
			PopWin.document.getElementsByTagName('body')[0].innerHTML = '';
		}
		else
		{
			PopWin = open('', MyWindowTitle, options);
		}
		PopWin.focus();
	}
	CgiFileText('./SharedServer/php/FileTextFunction.php', fn);
}

function download(file)
{
	var prm = 'file=' + file;
	PopWin = open('', MyWindowTitle, "scrollbars,dependent='yes',resizable,height=200,width=400");
	PopWin.focus();
	PopWin.location = './SharedServer/php/FileDownloadFunction.php?' + prm;
}

