/*==========================================================================*\
|| ######################################################################## ||
|| # ILance 3.0.0 Release Candidate 1 - Enterprise Edition			  	  #	||
|| # -------------------------------------------------------------------- #	||
|| # Copyright ©2000–2005 ILance Inc. All Rights Reserved.	              #	||
|| # This file may not be redistributed in whole or significant part. 	  #	||
|| # ----------------- ILANCE IS NOT FREE SOFTWARE ---------------------- #	||
|| #	 http://www.ilance.ca | http://www.ilance.ca/license.html	  	  #	||
|| # -------------------------------------------------------------------- #	||
|| # Title........: Custom functions for 3.0.0
|| # -------------------------------------------------------------------- # ||
|| ######################################################################## ||
|| # CVS History and Technical Application Information
|| # -------------------------------------------------------------------- # ||
|| # CVS:	$RCSfile: custom.js,v $
|| # CVS:	$Version: 3.0.0 $
|| # CVS:  $Revision: 1.1.1.1 $
|| # CVS:	   $Date: 2006/12/04 13:22:42 $
|| # CVS:    $Author: root $
|| # -------------------------------------------------------------------- # ||
|| ######################################################################## ||
\*==========================================================================*/

function preloadImages(){}

function createHTTPRequest()
{
  if(window.XMLHttpRequest) {
	try { req = new XMLHttpRequest(); } catch(e) { req = false; }
  }
  else if(window.ActiveXObject)
  {
	try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {
	try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { req = false; } }
  }
  return req;	
	
}
  
    function handleResponse() {

       if(sughttp.readyState == 4 && sughttp.status == 200 ){

          // Text returned FROM the PHP script
          var response = sughttp.responseText;

          if(response) {
             // UPDATE  content
             document.getElementById("suggestResults").innerHTML = response;
			 document.getElementById("suggestResults").style.display = "block";
			 current = -1;
          }

       }

    }
	
	function keyPressed(ev)
	{
		var key = (window.event) ? window.event.keyCode : ev.keyCode;
		var ARRUP = 38;
		var ARRDN = 40;
		var RETURN = 13;
		var TAB = 9;
		var ESC = 27;
		
		switch(key)
		{

			case RETURN:
				b = setHighlightedValue();
				return b;
				break;
		
			case ESC:
				return clearSuggestions();
				break;

		}
		return true;
	}

	function keyUp(ev)
	{
		var key = (window.event) ? window.event.keyCode : ev.keyCode;
		// set responses to keydown events in the field
		// this allows the user to use the arrow keys to scroll through the results
		// ESCAPE clears the list
		// TAB sets the current highlighted value
		//
			var ARRUP = 38;
		var ARRDN = 40;
		var RETURN = 13;
		var TAB = 9;
		var ESC = 27;
		switch(key)
		{
			case ARRUP:
				changeHighlight(-1);
				break;
	
			case ARRDN:
				changeHighlight(1);
				break;

			case RETURN:
				//return setHighlightedValue();
				break;
		
			case ESC:
				return false;
				break;

			default:
				
				sendRequest(document.getElementById("tasks_keyword").value);
		}
	
		return !((key==ESC || key == RETURN) && document.getElementById("suggestResults").style.display == "block");
	}
	
	function sendRequest(m) {
   	   sughttp = createHTTPRequest();
       // Open PHP script for requests
   	   if (sughttp)
	   {
       	sughttp.open('get', '/main.php?cmd=suggest&m='+m);
       	sughttp.onreadystatechange = handleResponse;
       	sughttp.send("");
	   }
    }
	function clearSuggestions()
	{
		current = -1;
		if (document.getElementById("suggestResults"))
		 document.getElementById("suggestResults").style.display = "none";
		return true;
	}
	
	function setHighlightedValue()
	{
		if (document.getElementById("result"+current))
		{
			document.getElementById("m").value = document.getElementById("result"+current).innerHTML;
			clearSuggestions();
			return false;
		}
		return true;
	}
	
	function changeHighlight(dir)
	{
		if (current + dir < 0)	
		   document.getElementById("keyword_text").focus();
		else if(document.getElementById("result"+(current+dir)))
		{
			if (document.getElementById("result"+current))
			{
				document.getElementById("result"+current).style.background="#FFFFFF";
				document.getElementById("result"+current).style.color="#000000";	
			}
			current += dir;
			document.getElementById("result"+current).style.background="#f5821f";
			document.getElementById("result"+current).style.color="#FFFFFF";	
		}
	}
	
	function submitLogin()
	{
		document.getElementById("loginForm").submit();	
		
	}
	
	function showLogin()
	{
		document.getElementById("loginForm").style.display = "inline";	
		document.getElementById("toplineText").style.display = "none";	
	}
	
	function closeResults()
	{
		clearSuggestions();
		
	}
	
	function copyToClipboard(inElement) {
	   if (inElement.createTextRange) {
			var range = inElement.createTextRange();
			if (range && BodyLoaded==1)
			 range.execCommand('Copy');
		} else {
			var flashcopier = 'flashcopier';
			if(!document.getElementById(flashcopier)) {
			  var divholder = document.createElement('div');
			  divholder.id = flashcopier;
			  document.body.appendChild(divholder);
			}
			document.getElementById(flashcopier).innerHTML = '';
			var divinfo = '<embed src="/images/_clipboard.swf" FlashVars="clipboard='+escape(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
			document.getElementById(flashcopier).innerHTML = divinfo;
	  }
	  document.getElementById("copySuccess").style.display = "inline";
	}
