function showResult(str)
{
	if (str.length==0)
	{
		document.getElementById("results").innerHTML="";
		document.getElementById("resultBox").style.display="none";
		return;
	}
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("results").innerHTML=xmlhttp.responseText;
			document.getElementById("resultBox").style.display = "";
			document.getElementById("resultBox").style.zIndex = "1";
		}
	}
	
xmlhttp.open("GET","search.php?searchGlossary="+str,true);
xmlhttp.send();
}

function hide()
{
	document.getElementById("resultBox").style.display="none";
}

// Confirm if the user wants to destroy the term
function destroy(term)
{
	answer = confirm("Are you sure you want to delete this term?");
	if (answer)
	{
		window.location = "destroyTerm.php?term="+term;
	}
}

// Confirm if the user wants to destroy the user
function destroyUser(user)
{
	answer = confirm("Are you sure you want to delete this user?");
	if (answer)
	{
		window.location = "destroyUser.php?user="+user;
	}
}

// Confirm if the user wants to duplicate the term
function duplicateTerm(term)
{
	answer = confirm("Are you sure you want to duplicate this term?");
	if (answer)
	{
		window.location = "duplicate.php?term="+term;
	}
}

// Takes the first letter of the value entered in the term field and puts it in the letter field
function takeFirstLetter()
{
	term = document.getElementById("term").value;
	
	// Get first letter
	sub = term.substring(0, 1);
	
	document.getElementById("letter").value = sub.toUpperCase();
}

var inival=0; // Initialise starting element number

// Call this function to add image textbox
function addImageBox()
{
	var newArea = add_New_Element_Image();
	var htcontents = "<p><input type='file' name='imagebx[]' size='60'/>&nbsp;&nbsp;Image Reference <input type='text' name='imgRefbx[]' size='50'/></p>";
	document.getElementById(newArea).innerHTML = htcontents; // You can any other elements in place of 'htcontents'
}


function add_New_Element_Image() 
{
	inival=inival+1; // Increment element number by 1
	var ni = document.getElementById('moreImages');
	var newdiv = document.createElement('div'); // Create dynamic element
	var divIdName = 'my'+inival+'Div';
	newdiv.setAttribute('id',divIdName);
	ni.appendChild(newdiv);
	return divIdName;
}

function addVideoBox()
{
	var newArea = add_New_Element_Video();
	var htcontents = "<p><input type='text' name='videobx[]' size='60'/>&nbsp;&nbsp;Video Reference <input type='text' name='vidRefbx[]' size='50'/></p>";
	document.getElementById(newArea).innerHTML = htcontents; 
}


function add_New_Element_Video() 
{
	inival=inival+1; // Increment element number by 1
	var ni = document.getElementById('moreVideos');
	var newdiv = document.createElement('div'); // Create dynamic element
	var divIdName = 'my'+inival+'Div';
	newdiv.setAttribute('id',divIdName);
	ni.appendChild(newdiv);
	return divIdName;
}

function getElementsByClass (searchClass) {
  // This function returns an array of all HTML objects with the
  // specified className.  Tag is optional   
  var returnArray = [];
  var els = document.getElementsByTagName('*');
  var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
  for (var i = 0; i < els.length; i++) {
    if ( pattern.test(els[i].className) ) { returnArray.push(els[i]); }
  }
  return returnArray;
}

function popVideo(vid, darken) {
  // This function accepts a division ID (vid), either a string or the actual
  // object itself.   vid is mandatory.   darken is optional, if it's true
  // the page will be greyed out under the video.
  var videos = getElementsByClass('video');     // Get all the videos on the page.
  var isplaying=null;
  for(i=0; i<videos.length; i++) {              // Loop through all the videos
    if (videos[i].style.display=='block') {     // This video is playing 
      isplaying=videos[i].id;                   // remember its name
      var tmp=videos[i].innerHTML;              // Get the division contents
      videos[i].innerHTML='';                   // destroy the contents
      videos[i].style.display='none';           // Terminate the video.
      videos[i].innerHTML=tmp;                  // rebuild the contents.
    }
  }
  // This handles the darkening of the background while a video is playing.
  // First we see if the dark layer exists.
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');          // Create the layer.
        tnode.style.backgroundColor='rgb(0, 0, 0)';     // Make it black.
        tnode.style.opacity='0.7';                      // Set the opacity (firefox/Opera)
        tnode.style.MozOpacity='0.70';                  // Firefox 1.5
        tnode.style.filter='alpha(opacity=70)';         // IE.
        tnode.style.zIndex='1';                         // Zindex of 50 so it "floats"
        tnode.style.position='absolute';                // Position absolutely
        tnode.style.top='0px';                          // In the top
        tnode.style.left='0px';                         // Left corner of the page
        tnode.style.overflow='hidden';                  // Try to avoid making scroll bars            
        tnode.style.display='none';                     // Start out Hidden
        tnode.id='darkenScreenObject';                  // Name it so we can find it later
    tbody.appendChild(tnode);                           // Add it to the web page
    dark=document.getElementById('darkenScreenObject'); // Get the object.
  }
  dark.style.display='none';
  if ((isplaying==vid)||(/^close$/i.test(vid))) { return false; }
  if (typeof(vid)=="string") { vid=document.getElementById(vid); }
  if (vid&&typeof(vid)=="object") {
    if (darken) {
      // Calculate the page width and height 
      if( window.innerHeight && window.scrollMaxY )  { 
        var pageWidth = window.innerWidth + window.scrollMaxX;
        var pageHeight = window.innerHeight + window.scrollMaxY;
      } else if( document.body.scrollHeight > document.body.offsetHeight ) {
        var pageWidth = document.body.scrollWidth;
        var pageHeight = document.body.scrollHeight;
      } else { 
        var pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
        var pageHeight = document.body.offsetHeight + document.body.offsetTop; 
      }
      //set the shader to cover the entire page and make it visible. 
      dark.style.width= pageWidth+'px';
      dark.style.height= pageHeight+'px';
      dark.style.display='block';                                
    }
    // Make the video visible and set the zindex so its on top of everything else
    vid.style.zIndex='100';    
    vid.style.display='block';
    var scrollTop = 0;
    if (document.documentElement && document.documentElement.scrollTop)
      scrollTop = document.documentElement.scrollTop;
    else if (document.body)
      scrollTop = document.body.scrollTop;

    // set the starting x and y position of the video
	//vid.style.top=scrollTop+Math.floor((document.documentElement.clientHeight/2)-(vid.offsetHeight/2))+'px';
    //vid.style.left=Math.floor((document.documentElement.clientWidth/2)-(vid.offsetWidth/2))+'px';
  }
  return false;
}
