/* Research Database Management System (RDMS)
 ' Copyright(C) 2008 Graham Philips
 ' RDMS is licensed under the GNU General Public License.
 ' Please See license.html or http://www.gnu.org/licenses/gpl.txt for the full license
 ' If there are problems, see http://sourceforge.net/projects/rdms/ for support.
 '
 ' RDMS comes with ABSOLUTELY NO WARRANTY
 '  
 ' This program is free software; you can redistribute it and/or modify it under
 ' the terms of the GNU General Public License as published by the Free Software
 ' Foundation; either version 2 of the License, or (at your option) any later
 ' version. */

/*
Purpose:  	This page has all the main javascript declarations for the system - individual AJAX calls in separate functions
*/

function formToggleAll(inputFormName)
{
	window.isAlTicked =! window.isAlTicked;
	
	inputForm = document.getElementById(inputFormName);
	for(var i = 0;inputForm.elements[i];i++)
	{
	if (inputForm.elements[i].type == 'checkbox')
		{
			inputForm.elements[i].checked = window.isAlTicked;
		}
	}
}

/* AJAX document loading code */

var url;
var xmlHttp;
var messagePrefix = "<p class=\"himessage\">";
var messageSuffix = "</p>";

function GetXmlHttpObject()
{ 
	var objXMLHttp = null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (objXMLHttp === null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	return objXMLHttp;
}

function xmlHttpResponse(messageID,messageContent,displayID)
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{
		if (xmlHttp.status == 200 || xmlHttp.status == 500)
		{
			document.getElementById(messageID).innerHTML = messageContent;
			document.getElementById(displayID).innerHTML = xmlHttp.responseText;
		}
		else if (xmlHttp.status == 404)
		{
			document.getElementById(messageID).innerHTML = 'AJAX page not found. '+
				'Please report the attempted action and this response to a system '+
				'administrator.';
		}
		else if (xmlHttp.status == 500)
		{
			document.getElementById(messageID).innerHTML = 'Server processing '+
				'error. Please check folder and file write permissions and '+
				'database connection settings and permissions.';
		}
	}
}

function clearMessageDisplayBox() 
{
	document.getElementById('messageDisplayBox').innerHTML = "";
}

/* Pop up tooltip resource box */

var IE = document.all?true:false;

function helpbox(e, xcoVal, title, desc)
{
    document.getElementById('helpbox').innerHTML = "<table class='dhpopup' border='0'><tr><td class='dhpopuptitle'>" + title + "</td></tr><tr><td class='dhpopuptext'>" + desc + "</td></tr></table>";
	 
	if (IE)
	{
		coordX = event.clientX + document.body.scrollLeft;
		coordY = event.clientY + document.body.scrollTop;
	}
	else
	{
		coordX = e.pageX;
		coordY = e.pageY;
	}
	
     var t = document.getElementById('helpbox').style;
     t.left = coordX+xcoVal+"px";
     t.top = coordY+15+"px";
     t.visibility = 'visible';
}

function clearhelpbox()
{
     document.getElementById('helpbox').style.visibility = 'hidden';
}

/* AJAX loading code for the user search page */

function stateChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		document.getElementById("searchLoading").innerHTML = "Search Complete - Results below";
		document.getElementById("userSearchResults").innerHTML = xmlHttp.responseText;
	} 
}

function createResults(str)
{
	document.getElementById("searchLoading").innerHTML = "Searching Database, please wait...";
	setTimeout("showResults('"+str+"')",1000);
}

function showResults(str)
{
	xmlHttp = new GetXmlHttpObject();
	var url = "/includes/ajax/users/usersearch.asp?q="+escape(str)+"&sid="+Math.random();
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

/* Place holding AJAX code for when form inputs are required somewhere */

function showComments() 
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{
		document.getElementById("project_comments").innerHTML = xmlHttp.responseText;
		document.getElementById('project_comment_text').value = "";
	}
}

var str;

function postComment(opt)
{
	xmlHttp = new GetXmlHttpObject();
	str = escape(document.getElementById('project_comment_text').value);
	url = "/functions/AJAX/project_comments.asp?c="+opt+"&sid="+Math.random()+"&p="+projectID;
	xmlHttp.onreadystatechange = showComments;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send("q="+str);
}

// -->
