﻿//Misc:
var receipt_popup = null;
var trans_popup = null;

function PopReceipt (url)
{
	if ((receipt_popup == null) || (receipt_popup.closed))
	{
		var sFeatures = 'width=640,height=540,scrollbars=yes';
		receipt_popup = window.open(url, '', sFeatures);
	}
	else
	{
		receipt_popup.document.location.href = url;
	}
}

function PopImage (url)
{
	if ((trans_popup == null) || (trans_popup.closed))
	{
		var sFeatures = 'width=800,height=600,scrollbars=yes,resizable=yes';
		trans_popup = window.open(url, '', sFeatures);
	}
	else
	{
		trans_popup.document.location.href = url;
	}
}

function PopTranslationWindow (url)
{
	if ((trans_popup == null) || (trans_popup.closed))
	{
		var sFeatures = 'width=640,height=640,scrollbars=yes';
		trans_popup = window.open(url, '', sFeatures);
	}
	else
	{
		trans_popup.document.location.href = url;
	}
}

function numeric_onkeypress(event)
{
	var c;
	
	if ( document.all )
		c = event.keyCode;
	else
		c = event.charCode;

	keyvalid = ( ((c>34) && (c<41)) || ((c>47) && (c<58)) || (((c==99) || (c==67) || (c==86) || (c==118)) && (event.ctrlKey)) || (c==0));
	
	if ( !keyvalid )
	{
		event.returnValue = false;

		if ( document.all )
			return false;
		
		event.preventDefault ();

		return false;
	}

	return true;
}

var _tpNS = (document.all)?false:true;

function getOfsLeft (el)
{
	var sl = el;
	var ol = el.offsetLeft;
	var sh = 0;
  
	while ((el = el.offsetParent) != null)
	{
		ol += el.offsetLeft;

		if (el.offsetParent && el.offsetParent.offsetParent)
		{
			var scrollLeft = el.offsetParent.scrollLeft;
			
			if(!isNaN(scrollLeft))
			{
				sh -= scrollLeft;
			}
		}
	}

	el = sl;

	if(_tpNS)
	{
		while((el = el.parentNode) != null)
		{
			if (el.parentNode && el.parentNode.parentNode && !(el.parentNode.tagName &&
				el.parentNode.tagName.toUpperCase() == "BODY"))
			{
				var scrollLeft = el.parentNode.scrollLeft;
				
				if(!isNaN(scrollLeft) && scrollLeft > 0 )
					sh -= scrollLeft;
            }
        }
	}

	return ol+sh;
}

function getOfsTop (el)
{
	var ot = el.offsetTop;
	var sl = el;
	var sh = 0;
	
	while((el = el.offsetParent) != null)
	{
		ot += el.offsetTop;
		
		if(el.offsetParent && el.offsetParent.offsetParent)
		{
			var scrollTop = el.offsetParent.scrollTop;
			
			if(!isNaN(scrollTop)) sh -= scrollTop;
		}
	}
	
	el = sl;
	
	if(_tpNS)
	{
		while((el = el.parentNode) != null)
		{
			if(el.parentNode && el.parentNode.parentNode && !(el.parentNode.tagName &&
				el.parentNode.tagName.toUpperCase() == "BODY"))
			{
				var scrollTop = el.parentNode.scrollTop;
				
				if(!isNaN(scrollTop) && scrollTop > 0 )
				{
					sh -= scrollTop;
				}
			}
		}
	}

	return ot + sh;
}

function popUser( obj, url, iframename, showLeft )
{
   var x = getOfsLeft(obj) - 220;
   var y = getOfsTop(obj);
   
   if (x < 0)
   {
        x = getOfsLeft(obj) + 22;
   }

   var iframeobj = document.getElementById(iframename);
   
   if (iframeobj != null)
        document.body.removeChild(iframeobj);
   
   iframeobj = document.createElement("iframe");
   iframeobj.src = url;
   iframeobj.style.position = "absolute";
   iframeobj.style.left = x + "px";
   iframeobj.style.top = y + "px";
   iframeobj.style.border = "0";
   iframeobj.id = iframename;
   iframeobj.width = "220px";
   iframeobj.height = "147px";
   iframeobj.allowTransparency = "true";
   iframeobj.setAttribute("scrolling", "no"); 
   iframeobj.setAttribute("frameBorder", "0"); 
   document.body.appendChild(iframeobj); 
}

function getWindowHeight() 
{        

    var windowHeight=0;
    if (typeof(window.innerHeight)=='number') 
    {
        windowHeight = window.innerHeight;
    }
    else 
    {
        if (document.documentElement && document.documentElement.clientHeight) 
        {
            windowHeight = document.documentElement.clientHeight;
        }
        else 
        {
            if (document.body && document.body.clientHeight) 
            {
                windowHeight=document.body.clientHeight;
            }
        }
    }

    return windowHeight;
}

function setFooter(obj) 
{
    if (document.getElementById) 
    {
        var windowHeight=getWindowHeight();
               
        if (windowHeight>0) 
        {
            var footerElement= obj;
            var footerHeight=footerElement.offsetHeight;
            if (windowHeight-footerHeight >= 0) 
            {
                footerElement.style.position='relative';
                footerElement.style.top= (windowHeight - footerHeight) +'px';
            }
            else 
            {
                footerElement.style.position='static';
            }
        }
    }
}

//Misc. game:
function openHelp(url, width, height, windowname)
{
	var w = width;
	var h = height;
	var x = 0;
	var y = 0;
	var sb = 'yes';
	var rz = 'no';
	if(screen.width)
	{
		x = (screen.width - w)/2;
		y = (screen.height - h)/2;
		if (screen.width <= 800)
		{
			sb = 'yes';
			rz = 'yes';
		}
	}
	if(x < 0)
	{
		x = 0;
	}
	if(y < 0)
	{
		y = 0;
	}
	var f = 'channelmode=0, directories=no, fullscreen=no, height=' + h + ', left=' + x + ', location=no, menubar=no, resizable=' + rz + ', ' +
			'scrollbars=' + sb + ', status=no, titlebar=no, toolbar=no, top=' + y + ', width=' + w;
	var random = new String(Math.random());
	random = random.substring (2, 11);
	var new_window = window.open(url + '&random=' + random, windowname, f);
	new_window.focus();
}

function openGame(url, width, height, windowname)
{
	var w = width;
	var h = height;
	var x = 0;
	var y = 0;
	var sb = 'no';
	var rz = 'yes';
	if(screen.width)
	{
		x = ((screen.width - w)/2);
		y = ((screen.height - h)/2);
		if (screen.width <= 800)
		{
			sb = 'yes';
		}
	}
	if(x < 0)
	{
		x = 0;
	}
	if(y < 0)
	{
		y = 0;
	}
	var f = 'channelmode=0, directories=no, fullscreen=no, height=' + h + ', left=' + x + ', location=no, menubar=no, resizable=' + rz + ', ' +
			'scrollbars=' + sb + ', status=no, titlebar=no, toolbar=no, top=' + y + ', width=' + w;
	var random = new String(Math.random());
	random = random.substring (2, 11);
	var new_window = window.open(url + '&random=' + random, windowname, f);
	new_window.focus();
}

function openGameDev(url, width, height, windowname)
{
    openGame(url,width,height,new Date().getMilliseconds() + "");
}

//QuickMessage:
function getScrollXY() 
{
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) 
  {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } 
  else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } 
  else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getWindowHeight() 
{
      var myWidth = 0, myHeight = 0;
      if( typeof( window.innerWidth ) == 'number' ) 
      {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
      } 
      else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
      {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
      } 
      else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
      {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
      }
        
      return [ myWidth, myHeight ];
}
        
function assignToEventHandlers(func)
{
    func();
    if( typeof( window.innerWidth ) == 'number' ) 
    {
        //Non-IE
        window.onscroll = chainEvents(func, window.onscroll);
        window.onresize = chainEvents(func, window.onresize);
    } 
    else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
    {
                //IE 6+ in 'standards compliant mode'
       document.body.onscroll = chainEvents(func, document.body.onscroll);
       document.body.onresize = chainEvents(func, document.body.onresize);
    } 
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
    {
          //IE 4 compatible
       document.body.onscroll = chainEvents(func, document.body.onscroll);
       document.body.onresize = chainEvents(func, document.body.onresize);
    }
        
}

function chainEvents(f1, f2)
{
    return function () 
    {
        f1();
         if (f2 != null) 
        { 
            f2();
        }
    }
}