var g_Name = "";
var g_Description = "";

function getObjectByID(id) {
	if (document.getElementById)
		x = document.getElementById(id);
	else if (document.all)
		x = document.all[id];
	return x;
}

function changeClass(id, classNameStr)
{
	getObjectByID(id).className = classNameStr;
}

function showhide(id)
{
	obj = getObjectByID(id);
	if (obj.style.display == "none")
		obj.style.display = "";
	else
		obj.style.display = "none";
}

function show(id)
{
	getObjectByID(id).style.display = "";
}

function hide(id)
{
	getObjectByID(id).style.display = "none";
}

var xmlhttp;
function XMLHttp()
{
	var locxmlrec;
	if (window.XMLHttpRequest)
		locxmlrec = new XMLHttpRequest();
	else{
		if (window.ActiveXObject){
			try {
				locxmlrec = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) {
				locxmlrec = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	}
	return locxmlrec;
}
xmlhttp = new XMLHttp();
function async_post_ret(url,data)
{
	if(xmlhttp)	{
		xmlhttp.open("POST",url,false);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp.send(data);
		return xmlhttp.responseText;
	}
	return "";
}

function perf_search(form) {
	if (form == null)
		return;
	// Get the data from the form
	var q  = document.forms[0].q.value;
	var ret = async_post_ret("search.php","q=" + q);
	if (ret != 0) {
		getObjectByID('result').innerHTML = ret;
	}
	else
		getObjectByID('result').innerHTML = "<h1>No results!</h1>";
}


function login(fid, foo, action, name,description) {
	if (foo != true) {
		var pagesize = getPageSize();
		var win_x = 400;
		var win_y = 250;
		var win_l = (pagesize[0] - win_x) / 2;
		var win_t = (pagesize[1] - win_y) / 2;
		var iframe = "<iframe style=\"height: " + pagesize[1] +"px; width: "+pagesize[0]+"px;\" id=\"login_hide\"></iframe>";
		var overlay= "<div style=\"height: " + pagesize[1] +"px; width: "+pagesize[0]+"px;\" id=\"login_overlay\"></div>";
		var login_window = "<div style=\"width: "+win_x+"px; left: "+win_l+"px; top: "+win_t+"px; display: block;\" id=\"login_window\">";
		if (action == "delete") {
			var formStr = "<div id='login_title'>Enter the login/password from the <a href=\"http://forum.guestresearcher.org\">forum</a>:</div><br /><form name=\"login_form\"><table width='100%'><tr><td>Username</td><td><input type=\"text\" name=\"username\" /></td></tr><tr><td>Password</td><td><input type=\"password\" name=\"password\" /></td></tr></table></form>";
			getObjectByID('login').innerHTML = iframe + overlay + login_window + formStr + "<div id=\"login_caption\"><a href='#' onclick='delete_file("+fid+",true)'>Submit</a> &nbsp; <a href='#' onclick='closeLoginPage()'>Close</a></div></div>";
		}
		else {
			var formStr = "<div id='login_title'>Enter the login/password from the <a href=\"http://forum.guestresearcher.org\">forum</a>:</div><br /><form name=\"login_form\"><table width='100%'><tr><td>Username</td><td><input type=\"text\" name=\"username\" /></td></tr><tr><td>Password</td><td><input type=\"password\" name=\"password\" /></td></tr><tr><td>File name</td><td><input type=\"text\" name=\"name\" /></td></tr><tr><td>Description</td><td><textarea name=\"description\" rows=\"3\" cols=\"50\" ></textarea></td></tr></table></form>";
			getObjectByID('login').innerHTML = iframe + overlay + login_window + formStr + "<div id=\"login_caption\"><a href='#' onclick='rename_file("+fid+",true)'>Submit</a> &nbsp; <a href='#' onclick='closeLoginPage()'>Close</a></div></div>";
		}
		return false;
	}
	else
	{
		// test the AJAX login
		var username = document.forms[2].username.value;
		var password = document.forms[2].password.value;

		if (action == "rename")
		{
			g_Name = document.forms[2].name.value;
			g_Description = document.forms[2].description.value;
		}
		var ret = async_post_ret("login.php","username=" + username + "&password=" + password);
		if (ret == "1")
			return true;
		else
			return false;
	}
	return false;
}

function delete_file(fid, foo) {
	// popup username
	if (login(fid, foo, "delete"))
	{
		async_post_ret("index.php","fid="+fid+"&action=delete");
		document.location.href = "index.php";
	}
}
function rename_file(fid, foo) {
	// popup username
	if (login(fid, foo, "rename"))
	{
		var ret = async_post_ret("index.php","fid="+fid+"&action=rename&name="+g_Name+"&description="+g_Description);
		document.location.href = "index.php";
	}
}

// login page management

function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h)
	return arrayPageSize;
}

function closeLoginPage() {
	getObjectByID('login').innerHTML = "";
}




