// misc.js

var mouseX = 0;
var mouseY = 0;

var routeMouseMove = null;
var routeMouseDown = null;
var routeMouseUp = null;

function javaScriptGlobalInit()
{	
  if (window.Event)
  	document.captureEvents(Event.MOUSEMOVE);
  	
  document.onmousemove = mouseMove;  
	document.onmousedown = mouseDown;
	document.onmouseup   = mouseUp;
}

function mouseMove(e)
{
	if (routeMouseMove != null)
		return routeMouseMove(e);
	
  e = e || window.event;
  
  if (e.pageX || e.pageY)
  {
    mouseX = e.pageX;
    mouseY = e.pageY;
  }
  else
	{
    mouseX = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    mouseY = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }
}

function mouseDown(e)
{
	if (routeMouseDown != null)
		return routeMouseDown(e);	
}

function mouseUp(e)
{
	if (routeMouseUp != null)
		return routeMouseUp(e);	
}

function checkEnter(e, formname)
{
	var character;

	if (e && e.which)
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}

	if (characterCode == 13)
	{
		document.forms[formname].submit();
		return false;
	}
	else
		return true;

}

function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) 
{
	return stringToTrim.replace(/\s+$/,"");
}

function ajax(url, vars, object)
{
    if (window.XMLHttpRequest)
        var request = new XMLHttpRequest();
    else
        var request = new ActiveXObject("MSXML2.XMLHTTP.3.0");
        
    request.open("POST", url, true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");

    request.onreadystatechange = function()
    {
        if (request.readyState == 4 && request.status == 200)
            if (request.responseText)
            {
                obj = request.responseText;
                object(obj);
            }
    }
            
    request.send(vars);
}

function GetElementPosition(obj)
{
	var currLeft = 0;
	var currTop = 0;

	if (obj.offsetParent)
	{
		currLeft = obj.offsetLeft;
		currTop = obj.offsetTop;
		
		while (obj = obj.offsetParent)
		{
			currLeft += obj.offsetLeft;
			currTop += obj.offsetTop;
		}
	}
	
	return [currLeft, currTop];
}

function HoverMenuItem(item, enable)
{
    document.getElementById(item).style.backgroundColor = ((enable) ? '#000000' : 'transparent');
}

function MM_swapImgRestore()
{
  var i, x, a = document.MM_sr;
  
  for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) 
      x.src = x.oSrc;
}

function MM_findObj(n, d)
{
  var p, i, x;

  if (!d)
      d = document;

  if ( (p = n.indexOf("?")) > 0 && parent.frames.length)
  {
    d = parent.frames[ n.substring(p + 1) ].document;
    n = n.substring(0, p);
  }

  if ( !(x = d[n]) && d.all)
      x = d.all[n];

  for (i = 0; !x && i < d.forms.length; i++)
      x = d.forms[i][n];

  for (i = 0; !x && d.layers && i < d.layers.length; i++)
      x = MM_findObj(n, d.layers[i].document);

  if(!x && d.getElementById)
      x = d.getElementById(n);

  return x;
}
        
function MM_preloadImages() 
{
  var d = document; 
  
  if (d.images)
  {
      if ( !d.MM_p )
          d.MM_p = new Array();
          
    var i, j = d.MM_p.length, a = MM_preloadImages.arguments; 

    for(i = 0; i < a.length; i++)
        if (a[i].indexOf("#") != 0)
        {
            d.MM_p[j] = new Image;
            d.MM_p[j++].src = a[i];
        }
  }
}

function MM_swapImage()
{
  var i, j = 0, x, a = MM_swapImage.arguments;
  
  document.MM_sr = new Array;
  
  for(i = 0; i < (a.length - 2); i += 3)
      if ( (x = MM_findObj(a[i])) != null)
      {
          document.MM_sr[j++] = x;
          
          if (!x.oSrc)
              x.oSrc = x.src;
              
          x.src = a[i+2];
      }
}

function SetOpacity(item, opacity)
{	
  if (item.style.setAttribute)
      item.style.setAttribute('filter', 'alpha(opacity=' + parseInt(opacity) + ');');

  item.style.MozOpacity = opacity / 100.0;
  item.style.opacity = opacity / 100.0;
  item.style.KhtmlOpacity = opacity / 100.0;
}

var toggleImages = new Array();

function ToggleButton(id, imgA, imgB)
{
	if (toggleImages[id] != 'toggleA' && toggleImages[id] != 'toggleB')
		toggleImages[id] = 'toggleA';
	
	if (toggleImages[id] == 'toggleA')
		document.getElementById(id).src = imgA;
	else
		document.getElementById(id).src = imgB;
}

function ToggleButtonChange(id, imgA, imgB)
{
	if (toggleImages[id] == 'toggleA')
	{
		toggleImages[id] = 'toggleB';
		document.getElementById(id).src = imgB;
	}
	else
	{
		toggleImages[id] = 'toggleA';
		document.getElementById(id).src = imgA;	
	}
}

function GetWindowWidth()
{
    var width = 0;
    if (typeof(window.innerWidth) == 'number') // Non-IE
        width = window.innerWidth;
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) // IE 6+ in 'standards compliant mode'
        width = document.documentElement.clientWidth;
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
        width = document.body.clientWidth;

    return width;
}

function GetWindowHeight()
{
    var height = 0;
    if (typeof(window.innerWidth) == 'number') // Non-IE
        height = window.innerHeight;
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) // IE 6+ in 'standards compliant mode'
        height = document.documentElement.clientHeight;
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) // IE 4 compatible
        height = document.body.clientHeight;

    return height;
}


var indicatorFadingFlag = false;
var indicatorFadingCancelFlag = false;
var indicatorFadingOpacity = 0;
function IndicatorFader(fadein)
{
	indicatorFadingFlag = true;

	var indicator = document.getElementById('statusindicator');
	
	if (indicatorFadingCancelFlag)
		fadein = false;
		
	if (fadein)
		if (indicatorFadingOpacity < 1)
		{
			indicatorFadingOpacity = 5;
			indicator.style.visibility = 'visible';
		}
		else
		{
			indicatorFadingOpacity *= 1.6;
			if (indicatorFadingOpacity > 95)
			{
				indicatorFadingOpacity = 100;
				if (!indicatorFadingCancelFlag)
				{
					indicatorFadingFlag = false;
					return;
				}
				else
					fadein = false;
			}
		}
	else
	{
		indicatorFadingOpacity *= 0.6;
		if (indicatorFadingOpacity < 5)
		{
			indicator.style.visibility = 'hidden';
			indicatorFadingOpacity = 0;
			indicatorFadingFlag = false;
			return;
		}
	}
	
	SetOpacity(indicator, indicatorFadingOpacity);
	
	setTimeout('IndicatorFader(' + (fadein ? 'true' : 'false') + ')', 40);
}

function LoadingStatus(enable)
{
	if (!indicatorFadingFlag)
	{
		indicatorFadingCancelFlag = false;
		indicatorFadingFlag = true;
		setTimeout('IndicatorFader(' + (enable ? 'true' : 'false') + ')', 40);
	}
	else
		if (!enable)
			indicatorFadingCancelFlag = true;
}

function IsLoading()
{
	return indicatorFadingFlag;
}


function ForceToggleDirection(box, direction)
{
	var downButtonName = box + '_button_toggle_down';
	var upButtonName = box + '_button_toggle_up';
	
	if (direction == 'down')
	{
		document.getElementById(downButtonName).style.display = 'inline';
		document.getElementById(upButtonName).style.display = 'none';
	}
	else
	{
		document.getElementById(downButtonName).style.display = 'none';
		document.getElementById(upButtonName).style.display = 'inline';
	}	
}

