// JavaScript Document

var xmlhttp = false;
	//Check if we are using IE.
	try {
		//If the Javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using Internet Explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}

	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	function makerequest(serverPage, objID) {
		
		var obj = document.getElementById(objID);
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	}
	
	// TEXT COUNTDOWN FUNCTION	
	function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if the current length is more than allowed
	field.value =field.value.substring(0, maxlimit); // don't allow further input
	else
	countfield.value = maxlimit - field.value.length;} // set the display field to remaining number

// #########################
// TOGGLE CHAT
// #########################

function togglechat(obj) {
	var objCheck = "item_discuss" + obj;
	var imageName = "discussimage" + obj;
	var imageSrc = document[imageName].src;
	var dotLocation = imageSrc.indexOf(".png");
	var imageFilename = imageSrc.substr(0, dotLocation);
	
	if (document.getElementById(objCheck).style.display == "none") { // IF THE ITEM IS HIDDEN, SHOW IT AND makerequest
	    var callurl = "ajax_discussion.php?item=" + obj;
		document[imageName].src = imageFilename + "-open" + ".png";
		document.getElementById(objCheck).style.display = "block";
		makerequest(callurl, objCheck);
	} else {
		document.getElementById(objCheck).style.display = "none";
		document[imageName].src = imageFilename.substr(0, (imageFilename.length - 5)) + ".png";
	}
}
	

// #########################
// THE SCROLLING STUFF
// #########################

  var leftcolfocus = 1 ;
  var morefocus = 1 ;
  function changefocus(newfocus){
	if(leftcolfocus != newfocus) {
	  Effect.SlideDown('leftbar' + newfocus, { duration: 0.4 });
	  Effect.SlideUp('leftbar' + leftcolfocus, {duration: 0.4 });
	  leftcolfocus = newfocus;
	}
  }
  function listfocus(newfocus){
	if(morefocus != newfocus){
		Effect.SlideDown('listfocus' + newfocus, { duration: 0.3 });
		Effect.SlideUp('listfocus' + morefocus, { duration: 0.3 });
		morefocus = newfocus;
	}
  }			

// ######################
// POST THE FORM, LADIES!
// ######################

// fobj is the name of the form
	function getformvalues (fobj, valfunc){
		
		var str = "";
		aok = true;
		var val;
		
		//Run through a list of all objects contained within the form.
		for(var i = 0; i < fobj.elements.length; i++){
			if(valfunc) {
				if (aok == true){
					val = valfunc (fobj.elements[i].value,fobj.elements[i].name); 
					if (val == false){
						aok = false;
					}
				}
			}
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
		}
		//Then return the string values.
		return str;
	}
	
	// theForm = Form ID - serverPage = php processing script - objID = ID to return results to - valfunc = validate reference?
	
	function submitform (theform, serverPage, objID, valfunc){
		var file = serverPage;
		var str = getformvalues(theform,valfunc);
		//If the validation is ok.
		if (aok == true){
			obj = document.getElementById(objID);
			processajax (serverPage, obj, "post", str);
		}
	}
	
	//Function to create an XMLHttp Object.
	function getxmlhttp (){
		//Create a boolean variable to check for a valid microsoft active X instance.
		var xmlhttp = false;
		
		//Check if we are using internet explorer.
		try {
			//If the javascript version is greater than 5.
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			//If not, then use the older active x object.
			try {
				//If we are using internet explorer.
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				//Else we must be using a non-internet explorer browser.
				xmlhttp = false;
			}
		}
		
		//If we are using a non-internet explorer browser, create a javascript instance of the object.
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
			xmlhttp = new XMLHttpRequest();
		}
		
		return xmlhttp;
	}
	
	//Function to process an XMLHttpRequest.
	function processajax (serverPage, obj, getOrPost, str){
		//Get an XMLHttpRequest object for use.
		xmlhttp = getxmlhttp ();
		if (getOrPost == "get"){
			xmlhttp.open("GET", serverPage);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					obj.innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(null);
		} else {
			xmlhttp.open("POST", serverPage, true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					obj.innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(str);
		}
	}

// ######################
// THE SEARCH SUGGEST BOX
// ######################

function searchSuggest() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('message').value);
		searchReq.open("GET", './ajax_searchsuggest.php?search=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest')
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}
//Click function
function setSearch(value) {
	document.getElementById('message').value = value;
	document.getElementById('search_suggest').innerHTML = '';
}

//SIDE MENUS
	  function changefocus(newfocus){
		if(leftcolfocus != newfocus) {
		  Effect.BlindDown('leftbar' + newfocus, { duration: 0.4 });
		  Effect.BlindUp('leftbar' + leftcolfocus, {duration: 0.4 });
		  leftcolfocus = newfocus;
		}
	  }
	  function listfocus(newfocus){
		if(morefocus != newfocus){
			Effect.BlindDown('listfocus' + newfocus, { duration: 0.3 });
			Effect.BlindUp('listfocus' + morefocus, { duration: 0.3 });
			morefocus = newfocus;
		}
	  }		
	  
//  Scriptlets code written by Jeremy Edmiston
//  The functions have been adapted from various sources
//  and re-written to provide maximum flexibility
//  and compatability with various browsers.

//Global Declarations
var ie = (document.all) ? true : false;

function toggleClass(objClass){
//  This function will toggle obj visibility of an Element
//  based on Element's Class
//  Works with IE and Mozilla based browsers

  if (getElementByClass(objClass).style.display=="none"){
    showClass(objClass)
  }else{
    hideClass(objClass)
  }
}

function hideClass(objClass){
//  This function will hide Elements by object Class
//  Works with IE and Mozilla based browsers

var elements = (ie) ? document.all : document.getElementsByTagName('*');
  for (i=0; i<elements.length; i++){
    if (elements[i].className==objClass){
      elements[i].style.display="none"
    }
  }
}

function showClass(objClass){
//  This function will show Elements by object Class
//  Works with IE and Mozilla based browsers
var elements = (ie) ? document.all : document.getElementsByTagName('*');
  for (i=0; i<elements.length; i++){
    if (elements[i].className==objClass){
      elements[i].style.display="block"
    }
  }
}

function toggleID(objID){
//  This function will toggle obj visibility of an Element
//  based on Element's ID
//  Works with IE and Mozilla based browsers
var element = (ie) ? document.all(objID) : document.getElementById(objID);
  if (element.style.display=="none"){
    showID(objID)
  }else{
    hideID(objID)
  }
}

function hideID(objID){
//  This function will hide Elements by object ID
//  Works with IE and Mozilla based browsers
var element = (ie) ? document.all(objID) : document.getElementById(objID);
  element.style.display="none"
}

function showID(objID){
//  This function will show Elements by object ID
//  Works with IE and Mozilla based browsers
var element = (ie) ? document.all(objID) : document.getElementById(objID);
  element.style.display="block"
}

function getElementByClass(objClass){
//  This function is similar to 'getElementByID' since there
//  is no inherent function to get an element by it's class
//  Works with IE and Mozilla based browsers
var elements = (ie) ? document.all : document.getElementsByTagName('*');
  for (i=0; i<elements.length; i++){
    //alert(elements[i].className)
    //alert(objClass)
    if (elements[i].className==objClass){
    return elements[i]
    }
  }
}