function GetXmlHttpObject(){ 
	// This function creates an instance of the "XMLHTTP" object
	// Initialise "objXMLHttp"
	var objXMLHttp=null
	// For FireFox
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}
	// For Internet Explorer
	else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	// Return the "objXMLHttp" object
	return objXMLHttp
}
// End of function "GetXmlHttpObject"

function swapImage(img, newSRC){
	img.src = "graphics/"+newSRC;
}
// End of function "swapImage()"

function ShowHide(id){
	// This function either shows or hides the element which has an ID
	// that is equal to the parameter that is passed to this function which is "id"
	// Check to see if the element is set to "display:none;"
	if(document.getElementById(id).style.display=="none")
	{	
		// Element is set to "display:none;"
		// Show this element
		document.getElementById(id).style.display = "block";
		document.getElementById(id).style.visibility = "visible";
	}
	else
	{	
		// Element is NOT set to "display:none;"
		// Do NOT show this element
		document.getElementById(id).style.display = "none";
		document.getElementById(id).style.visibility = "hidden";
	}
}
// End of function "ShowHide"
		
function UserConfirm(msg){
	ActionAlert = window.confirm(msg)
	if(!ActionAlert){
		return false
	}
	else{
		return true;
	}
}
// End of function "UserConfirm"
		
function ClearDefault(el){
	if (el.defaultValue == el.value) el.value = "";
}
// End of function "ClearDeafault"

function SetDefault(el){
	if (el.value==""){
		el.value = el.defaultValue;
	}
}
// End of function "SetDeafault"


function ValidateAddThread(el){
	if(el.ThreadTitle.value == ""){
		window.alert("WARNING - You MUST enter a Discussion Title.");
		el.ThreadTitle.focus();
		return false;
	}
	if(el.PostTitle.value == ""){
		window.alert("WARNING - You MUST enter a Post Subject.");
		el.PostTitle.focus();
		return false;
	}
	if(el.PostContent.value == ""){
		window.alert("WARNING - You MUST enter a some Content for your Post.");
		el.PostContent.focus();
		return false;
	}
	else{
		return true;	
	}
}
// End of fucntion "ValidateAddThread"
	
function ValidateAddPost(el){
	if(el.PostTitle.value == ""){
		window.alert("WARNING - You MUST enter a Post Subject.");
		el.PostTitle.focus();
		return false;
	}
	if(el.PostContent.value == ""){
		window.alert("WARNING - You MUST enter a some Content for your Post.");
		el.PostContent.focus();
		return false;
	}
	else{
		return true;	
	}
}
// End of function "ValidateAddPost"
	
function ValidateLogin(MyForm){
	if(MyForm.username.value == ""){
		window.alert("WARNING - You MUST enter a Username.");
		MyForm.username.focus();
		MyForm.username.value = "";
		MyForm.password.value = "";
		return false;
	}
	if(MyForm.password.value == ""){
		window.alert("WARNING - You MUST enter a Password.");
		MyForm.password.focus();
		MyForm.username.value = "";
		MyForm.password.value = "";
		return false;
	}	
	else{
		return true;	
	}
}
// End of function "ValidateLogin"

function ValidateForumRegister(MyForm){
	/* Validate the "FirstName" element */
	if(MyForm.FirstName.value == ""){
		window.alert("WARNING - You MUST enter a First Name.");	
		MyForm.FirstName.focus();
		return false;
	}
	/* Validate the "Surname" element */
	if(MyForm.Surname.value == ""){
		window.alert("WARNING - You MUST enter a Surname.");	
		MyForm.Surname.focus();
		return false;
	}
	/* Validate the "UserRole" element */
	if(MyForm.UserRole.selectedIndex <= 0){
		window.alert("WARNING - You MUST select a role.");	
		MyForm.UserRole.focus();
		return false;
	}
	/* check to see if "UserRole" is equal to "other" */
	if(MyForm.UserRole.value == "other"){
		/* Validate the "UserRoleOther" element */
		if(MyForm.UserRoleOther.value == "" || MyForm.UserRoleOther.value == "Please State"){
			window.alert("WARNING - You MUST enter your role whilst selecting \"other\".");	
			MyForm.UserRoleOther.focus();
			return false;
		}
	}
	/* Validate the "LocalAuth" element */
	if(MyForm.LocalAuth.selectedIndex <= 0){
		window.alert("WARNING - You MUST select a Local Authority.");	
		MyForm.LocalAuth.focus();
		return false;
	}
	/* Validate the "SchoolName" element */
	if(MyForm.SchoolName.value == ""){
		window.alert("WARNING - You MUST enter a School Name.");	
		MyForm.SchoolName.focus();
		return false;
	}
	/* Validate the "Email" element */
	if(MyForm.Email.value == ""){
		window.alert("WARNING - You MUST enter a valid E-Mail address.");	
		MyForm.Email.focus();
		return false;
	}
	/* Validate the "Username" element */
	if(MyForm.Username.value == ""){
		window.alert("WARNING - You MUST enter a Username.");	
		MyForm.Username.focus();
		return false;
	}
	/* Validate the "Password" element */
	if(MyForm.Password.value == ""){
		window.alert("WARNING - You MUST enter a Password.");
		// Reset the pasword fields
		MyForm.Password.value = "";
		MyForm.PasswordConfirm.value = "";
		MyForm.Password.focus();
		return false;
	}
	/* Validate the "PasswordConfirm" element */
	if(MyForm.PasswordConfirm.value == ""){
		window.alert("WARNING - You MUST confirm your password by re-typing it.");
		// Reset the pasword fields
		MyForm.Password.value = "";
		MyForm.PasswordConfirm.value = "";
		MyForm.Password.focus();
		return false;
	}
	/* check to see if both passwords match */
	if(!(MyForm.Password.value == MyForm.PasswordConfirm.value)){
		window.alert("WARNING - Passwords do not match.");
		// Reset the pasword fields
		MyForm.Password.value = "";
		MyForm.PasswordConfirm.value = "";
		MyForm.Password.focus();
		return false;
	}
	else{
		return true;	
	}
}
// End of function "ValidateForumRegister"

function SetOtherForRegister(MyForm){
	if(MyForm.UserRole.value == "other"){
		document.getElementById("UserRoleOtherContainer").style.display = "block";
		document.getElementById("UserRoleOtherContainer").style.visibility = "visible";
		MyForm.UserRoleOther.disabled = false;
	}
	else{
		document.getElementById("UserRoleOtherContainer").style.display = "none";
		document.getElementById("UserRoleOtherContainer").style.visibility = "hidden";
		MyForm.UserRoleOther.value = MyForm.UserRoleOther.defaultValue;
		MyForm.UserRoleOther.disabled = true;
	}
}
// End of function "SetOtherForRegister"

function ShowUserProfile(UserID, EventObject){
	// "UserID" refers to a particualr user and is used to obtain the appropriate details for the user with that particular UserID.
	// "EventObject" is the "event" object.
	// This has to be passed as an arguement as this seems to work on both IE and FF.
	// Using "window.event" within this function did not work in FF.
	// Get the User Profile div container.
	ProfileContainer = document.getElementById("UserProfile")
	// The Profile div container should not be shown by default.
	// Display the profile container
	ProfileContainer.style.display = "block";
	ProfileContainer.style.visibility = "visible";
	// Inform user that the profile is loading.
	document.getElementById("ProfileContent").innerHTML = "Loading";
	// Get the cursor position using the "clientX" and "clientY" property of the "event" object which was passed as an arguement in "EventObject"
	// The "clientX" and "clientY" properties refer to a position that is apparently viewable/visible within the window.
	// So if a page is scrolled, it DOES NOT take into account that part of the page that is NOT apparently visible.
	// Using the "pageX" and "pageY" properties instead, will fix this and will take into account the areas of that page that are NOT apparently visible.
	// However, this is not implemented within Internet Explorer.
	MouseX = EventObject.clientX;
  	MouseY = EventObject.clientY;
	// Get the scroll ofsets. (i.e. the number of pixels that the page has been scrolled)
	ScrollOffsetsXY = GetScrollXY();
	ScrollX = ScrollOffsetsXY[0];
	ScrollY = ScrollOffsetsXY[1];
	//window.alert("X = "+ScrollX+"\n\nY = "+ScrollY);
	// Position the "UserProfile" div at the same position as the cursor.
	// Cursor position is obtained by adding the current position of the cursor (obtained by "clientX/Y") and the scroll offset.
	ProfileContainer.style.top = (MouseY+ScrollY)+"px";
	ProfileContainer.style.left = (MouseX+ScrollX)+"px";
	// Call the "DisplayUserProfile" function to display the profiel for the user with the corresponding "UserID".
	DisplayUserProfile(UserID);
}
// End of function "ShowUserProfile"

function HideUserProfile(){
	// Get the User Profile div container.
	ProfileContainer = document.getElementById("UserProfile")
	// Hide the profile container
	ProfileContainer.style.display = "none";
	ProfileContainer.style.visibility = "hidden";
	// Clear all of the HTML within the user profile content
	document.getElementById("ProfileContent").innerHTML = "";
}
// End of function "HideUserProfile"

function DisplayUserProfile(UserID){
	// Create XmlHttpObject
	var xmlHttp=GetXmlHttpObject();
	// Check to see if the "xmlHttp" object was created
	if (xmlHttp==null){
		// The "xmlHttp" object was NOT created
		alert ("Browser does not support HTTP Request")
		return
	}
	// Build URL and append querystring variables
	var url="includes/UserProfile.asp?UserID="+UserID+"&sid="+Math.random();
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4){ 
			var GetUserInfoStatus = xmlHttp.responseText;
			document.getElementById("ProfileContent").innerHTML = GetUserInfoStatus;
		}
	}
	xmlHttp.open("get",url,true);
	xmlHttp.send(null);
}
// End of function "DisplayUserProfile"

function GetScrollXY() {
	// This function obtains the number of pixels the page has been scrolled.
	// The function returns an array that includes the number of pixels the page has been scrolled.
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
	//Netscape compliant
	scrOfY = window.pageYOffset;
	scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	//DOM compliant
	scrOfY = document.body.scrollTop;
	scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	//IE6 standards compliant mode
	scrOfY = document.documentElement.scrollTop;
	scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}
// End of function "GetScrollXY"

function ClearForm(MyForm){
	// Loop through each form element.
	for(i=0;i<MyForm.elements.length;i++){
		// Check to see if the form element is a "checkbox".
		if(MyForm.elements[i].type == "checkbox"){
			// Check to see if the checkbox was "checked" by default.
			if(MyForm.elements[i].defaultChecked == true){
				// Checkbox WAS "checked" by default.
				// Set the "checkbox" to "true" so that it IS checked .
				MyForm.elements[i].checked = true;
			}
			else{
				// Checkbox was NOT "checked" by default.
				// Set the "checkbox" to "false" so that it is NOT checked .
				MyForm.elements[i].checked = false;
			}
			// End of If Else Statement.
		}
		// Check to see if the form element is a Drop Menu.
		else if(MyForm.elements[i].type == "select-one"){
			// Set the first element in the drop menu to be selected initially.
			MyForm.elements[i].options[0].selected = true;
			// Loop through each option in the Drop Menu
			for(OptionCount=0;OptionCount<MyForm.elements[i].options.length;OptionCount++){
				// Check to see if the current option is selected by deafault.
				if(MyForm.elements[i].options[OptionCount].defaultSelected == true){
					// Change the "selected" property to "true"
					MyForm.elements[i].options[OptionCount].selected = true;
					// Set the first element in the drop menu to be UNSELECTED.
					MyForm.elements[i].options[0].selected = false;
				}
				else{
					// Change the "selected" property to "false"
					MyForm.elements[i].options[OptionCount].selected = false;
				}
				// End of If Else Statement.
			}
			// End of For Loop using counter "OptionCount".
		}
		else{
			// Form element is NOT a "checkbox" .
			// Set the form element to the default value.
			MyForm.elements[i].value=MyForm.elements[i].defaultValue;
		}
		// End of If Else Statement.
	}
	// End of For Loop using counter "i".
}
// End of Function "clearForm()"

function PreloadImages(){
// Home Tab Button
HomeTab = new Image();	
HomeTab.src = "../graphics/HomeTab.gif";
HomeTabOver = new Image();	
HomeTabOver.src = "../graphics/HomeTabOver.gif";
// Forum Tab Button
ForumTab = new Image();	
ForumTab.src = "../graphics/ForumTab.gif";
ForumTabOver = new Image();	
ForumTabOver.src = "../graphics/TForumabOver.gif";
// Login Tab Button
LoginTab = new Image();	
LoginTab.src = "../graphics/LoginTab.gif";
LoginTabOver = new Image();	
LoginTabOver.src = "../graphics/LoginTabOver.gif";
// Logout Tab Button
LogoutTab = new Image();	
LogoutTab.src = "../graphics/LogoutTab.gif";
LogoutTabOver = new Image();	
LogoutTabOver.src = "../graphics/LogoutTabOver.gif";
// Register Tab Button
RegisterTab = new Image();	
RegisterTab.src = "../graphics/RegisterTab.gif";
RegisterTabOver = new Image();	
RegisterTabOver.src = "../graphics/RegisterTabOver.gif";
// Contact Tab Button
ContactTab = new Image();	
ContactTab.src = "../graphics/ContactTab.gif";
ContactTabOver = new Image();	
ContactTabOver.src = "../graphics/ContactTabOver.gif";
// Help Tab Button
HelpTab = new Image();	
HelpTab.src = "../graphics/HelpTab.gif";
HelpTabOver = new Image();	
HelpTabOver.src = "../graphics/HelpTabOver.gif";
// Profile Tab Button
ProfileTab = new Image();	
ProfileTab.src = "../graphics/ProfileTab.gif";
ProfileTabOver = new Image();	
ProfileTabOver.src = "../graphics/ProfileTabOver.gif";
}

window.onload = PreloadImages;
