/**
* @copyright Copyright (C) 2003 - 2006 Unity In Work Ltd.  All Rights Reserved.
* @author  John Jameson
* @access  public
* @package Interface
* @version  01 Jan 2003
*/



//***********************************************************
// Persistent flag to indicate what level to report debug info.
// Note: SET TO FALSE FOR PRODUCTION VERSION.
//***********************************************************
var GLOB_DEBUG_NONE = 0;
var GLOB_DEBUG_ERROR = 1;
var GLOB_DEBUG_WARN = 3;
var GLOB_DEBUG_NOTICE = 5;
var GLOB_DEBUG_TRACE = 9;
//var GLOB_DEBUG_LEVEL = GLOB_DEBUG_NOTICE;
var GLOB_DEBUG_LEVEL = GLOB_DEBUG_NONE;


// Persistent store of task list
var m_TaskList = new Array();
var m_TaskIndex = 0;

// Flag to switch on/off live updates.
var m_bLive = new Boolean(true);

var myBackgroundColour = 'transparent';

// Store mouse pos.
var m_MouseDownPos = new Object;
m_MouseDownPos.X = 0;
m_MouseDownPos.Y = 0;


var m_SubmitReloadPending =new Boolean(false);



// Lists for autocomplete.

var m_JobTitles = new Array('Manager','Accounts Manager','Marketing Manager',
                            'Sales Manager','Director','Accounts Director',
                            'Marketing Director','Sales Director','CEO','Clerk',
                            'Engineer','Purchaser');

var m_PersonTitles = new Array('Mr','Mrs','Miss','Ms','Dr','Prof','Fr','Br','President');

var m_CityNames = new Array('London','Glasgow','Edinburgh','Birmingham','Exeter',
                            'Okehampton','Coventry','Warwick','Bovey Tracy','Bodmin',
                            'Weymouth','Weston-Super-Mare',
                            'Plymouth','Truro','Falmouth','St Austell','Newton Abbot',
                            'Torquay','Painton','Teignbridge');

var m_CountryNames = new Array('UK','England','Scotland','Wales','USA','Canada','France',
                               'Spain',
                               'Germany','Italy','Austria','Sweden','Estonia','Hungary',
                               'Poland','Slovenia','Czech Republic');




/**
* Handle exceptions by showing the user the first one.
*
*/
var glob_bFirstException = true;

function catchException(strMsg,e)
{
    if (glob_bFirstException)
    {
        alert(strMsg + e);
        
        glob_bFirstException = false;
    }
    
} // catchException() //




/**
* Handle debug msgs.
*
*/
function jjDbgOut(level,strReporter,strMsg)
{
    if (level <= GLOB_DEBUG_LEVEL)
    {
        alert('DEBUG: ' + strReporter + ': ' + strMsg);
    }
    
} // catchException() //



/**
* Debugging/development function to expose the contents of an object.
*
*/
function inspect(objTgt)
{
    var strVal = '';
    
    if(objTgt)
    {
        for(var elem in objTgt)
        {
            strVal += elem + "\n"
        }
    }
    else
    {
        strVal = 'Not an object';
    }
    
    alert(strVal);
    
} // inspect() //


// Utility to dump an object's available methods + properties.
function inspectAvailable(objTgt)
{
    var strVal = '';
    
    if(objTgt)
    {
        for (var elem in objTgt)
        {
            if(objTgt.getAttribute
               && objTgt.getAttribute(elem))
            {
                strVal += elem + ": " + objTgt.getAttribute(elem) + "\n";
            }
        }
    }
    else
    {
        strVal = 'Not an object';
    }
    
    alert(strVal);
}


function getObject(objectId) {
    // cross-browser function to get an object given its id.
    if(document.getElementById && document.getElementById(objectId)) 
    {
		// W3C DOM
		return document.getElementById(objectId);
    }
    else if (document.all && document.all(objectId)) 
    {
		// MSIE 4 DOM
		return document.all(objectId);
    }
    else if (document.layers && document.layers[objectId]) 
    {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
    } 
    else
    {
		return false;
    }
} // getObject


// Recursive function to find a window object by name.  The search
// scope is limited to within rootWin.
function getChildWindow(rootWin,strName)
{
    var result=false;
    if(rootWin.name && rootWin.name == strName)
    {
		result = rootWin;
    }
    else
    {
		if(rootWin.frames && rootWin.frames.length && rootWin.frames.length > 0)
		{
			for(var i = 0;i< rootWin.frames.length;i++)
			{
				win = getChildWindow(rootWin.frames[i],strName);
				if(win)
				{
					result = win;
					break;
				}
			}
		}
    }
    return result;
}


// Browser compatibility utility.
function initAll()
{
	/*     document.all = (document.all)?  */
	/* 	document.all : */
	/* 	((document.getElementsByTagName("*").length > 0)? */
            /* 	 document.getElementsByTagName("*") : */
            /* 	 null) */
}


function onSubmitHandler()
{
    m_SubmitReloadPending = true;
}

function refreshSingleWidget(obj)
{
	// Only refresh if we are live.
    if (m_bLive)
    {
		if (obj)
		{
			innerHTMLText = obj.innerHTML;
			obj.innerHTML = innerHTMLText;
		}
    }
}

function isNetscape()
{
    if(navigator.appName=='Netscape')
    {
		return (true); 
    }
    else
    {
		return (false);
    }
}



function mouseOverFor(obj,overClass,offClass,isOver)
{
    if (obj)
    {
		if (isOver)
		{
			// Select this obj.
			obj.className = overClass;
		}
		else
		{
			// Unselect this obj.
			obj.className = offClass;
		}
    }
}


function mouseDownFor(obj,downClass,upClass,isDown)
{
    if (obj)
    {
		if (isDown)
		{
			// Select this obj.
			obj.className = downClass;
		}
		else
		{
			// Unselect this obj.
			obj.className = upClass;
		}
    }
}


function selectTabInSet(selTabObj,tabObjSet,selClass,unselClass)
{
    if (tabObjSet)
    {
		if (selTabObj)
		{
			for (i = 0; i < tabObjSet.length; i++)
			{
				if (tabObjSet[i] == selTabObj)
				{
					// Select this obj.
					tabObjSet[i].className = selClass;
					tabObjSet[i].tabState = true;
				}
				else
				{
					// Unselect this obj.
					tabObjSet[i].className = unselClass;
					tabObjSet[i].tabState = false;
				}
			}
		}
    }
}


function isSelectedTab(tabObj,tabObjSet,selClass)
{
    var result = false;
    if (tabObjSet && tabObj)
    {
		for (i = 0; i < tabObjSet.length; i++)
		{
			if (tabObjSet[i]
				&& tabObjSet[i].tabState 
			&& tabObjSet[i] == tabObj 
			&& tabObjSet[i].tabState == true)
			{
				result = true;
			}
		}
    }
    return (result);
}


function unSelectAllTabsInSet(tabObjSet,unselClass)
{
    if (tabObjSet)
    {
		for (i = 0; i < tabObjSet.length; i++)
		{
			// Unselect this obj.
			tabObjSet[i].className = unselClass;
			tabObjSet[i].tabState = false;
		}
    }
}



function registerTaskList(taskList)
{
	
    // Record list of tasks.
    if (taskList)
    {
		m_TaskList = taskList;
    }
}


function setTask(index)
{
    num = parseInt(index);
    if (num >= 0 && num < m_TaskList.length)
    {
		m_TaskIndex = num;
    }
}


function nextTask()
{
    m_TaskIndex++;
    if (m_TaskIndex == m_TaskList.length)
    {
		m_TaskIndex = 0;
    }
    obj = getObject('TaskIndexSpan');
    if (obj)
    {
		obj.innerHTML = String(m_TaskIndex + 1);
    }
    return (m_TaskList[m_TaskIndex]);
}


function prevTask()
{
    m_TaskIndex--;
    if (m_TaskIndex < 0)
    {
		m_TaskIndex = m_TaskList.length - 1;
    }
    obj = getObject('TaskIndexSpan');
    if (obj)
    {
		obj.innerHTML = String(m_TaskIndex + 1);
    }
    return (m_TaskList[m_TaskIndex]);
}


function helpAction(obj,str)
{
    // Display the help msg.
    if(str)
    {
		alert(str);
    }
    if(obj)
    {
    }
}




function copyHashed(obj1,obj2,seed)
{
    bResult = false;
    if(obj1)
    {
		if(obj2)
		{
			// * TEMP * //
			hashed = obj1.value;
			// * TEMP END * //
			
			obj2.value = hashed;
			bResult = true;
		}
		else
		{
			alert('RRR');
		}
    }
	
    return(bResult);
}

function autoComplete(obj,aList)
{
    var bChanged = false;
	
    if(obj 
       && obj.value
	&& aList)
    {
		if(!obj.prevValue)
		{
			obj.prevValue=obj.value;
		}
		if(obj.prevValue != obj.value)
		{
			var aChoices = matchList(obj.value,aList);
			
			if(aChoices
               && aChoices.length
			&& aChoices.length > 0 
			&& obj.prevValue.length 
			&& obj.value.length)
			{
				if(obj.prevValue.length < obj.value.length)
				{
					if(aChoices.length == 1)
					{
						obj.value = aChoices.toString();
						bChanged = true;
					}
					else
					{
						obj.value = aChoices[0].substr(0,obj.value.length);
						bChanged = true;
					}
				}
			}
			
			obj.prevValue = obj.value;
		}
    }
	
    return bChanged;
}


function matchList(strItem,aList)
{
    var regMatch = new RegExp('[a-z]','i');
    
    if(regMatch.test(strItem))
    {
		var aMatched = new Array();
        
		for(var i in aList)
		{
			regMatch = new RegExp(strItem,'i');
            
			if(aList[i].search(regMatch) == 0)
			{
				aMatched.push(aList[i]);
			}
		}
    }
    return aMatched;
}


function limitLength(obj,iLength)
{
    if(!obj.prevValue)
    {
		obj.prevValue = obj.value;
    }
    else if(obj.prevValue != obj.value)
    {
        if(obj.prevValue.length < obj.value.length)
		{
			if(obj.value.length > iLength)
			{
				obj.value = obj.prevValue;
			}
		}
		
		obj.prevValue=obj.value;
    }
}


function viewPortHeight() 
{ 
    var hgt=0; 
    if (typeof(window.innerHeight)=='number')
    { 
		hgt = window.innerHeight; 
    }
    else
    { 
		if (document.documentElement&& 
			document.documentElement.clientHeight) 
		{ 
			hgt = document.documentElement.clientHeight; 
		}
		else 
		{ 
			if (document.body&&document.body.clientHeight) 
			{ 
				hgt = document.body.clientHeight; 
			}
		}
    }
    return hgt; 
}


function getElementAttribute(obj,strAttrib)
{
    var result = null;
    
    if(obj)
    {
        if ( eval ('obj.' + strAttrib) )
		{
            eval ('result = obj.' + strAttrib);
		}
        else if (obj.getAttribute)
        {
                 result = obj.getAttribute(strAttrib);
		}
        
		return result;
    }
}


function setElementAttribute(obj,strAttrib,attribValue)
{
   if(obj)
   {
      if ( eval ('obj.' + strAttrib) )
		{
         eval ('obj.' + strAttrib + ' = ' + attribValue + ';');
		}
      else if (obj.setAttribute)
      {
         result = obj.setAttribute(strAttrib,attribValue);
		}
   }
}


function replaceElementClass(obj,strOld,strNew)
{
   if(obj)
   {
      if ( !obj.className)
      {
         obj.className = strNew;
      }
      else if (strOld.length > 0)
      {
         obj.className = obj.className.replace(new RegExp(strNew,'g'),'');
      }
      
      obj.className += ' ' + strNew;
      obj.className = obj.className.replace(/^\s*|\s*$/g,'');
   }
}






