
var focus_set = 0; // Communicates with the ShowEditForm() function

function trySetFocus() 
{
  if ( !document.forms || !document.forms[0] || !document.forms[0].elements )
    {
      return;
    }

  if ( focus_set ) return;

	var pos = document.forms[0].name == 'searchForm' ? 1 : 0;

	var frm = document.forms[pos];

  for ( i = 0 ; i < frm.elements.length ; i++ ) 
    {
      var el = frm.elements[i];
      if ( el.type && el.type != 'hidden' && ! el.disabled )
        {
          el.focus();
          return; 
        }
    }
}

function doVfill()
{
	setTimeout( "setVfill()", 200 );
}

function setVfill()
{
	var vfill;
	var leftContent;
	if ( ( vfill = document.getElementById( 'vfill' ) ) && ( leftContent = document.getElementById( 'leftContent' ) ) )
	{
		var fillHeight = leftContent.offsetHeight - vfill.offsetTop - 15;
		vfill.style.height = fillHeight+"px";
	}
}

// hier komt de HttpRequest-"class" die gebruikt wordt om het php pspell-script te raadplegen.

function HttpRequest() {
	var _xmlhttp;
	var _post;

	try
	{
		this._xmlhttp = new XMLHttpRequest();
	}
	catch (a)
	{
		try
		{
			this._xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (b)
		{
			try
			{
				this._xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(c)
			{
				this._xmlhttp = false;
			}
		}
  }
}

HttpRequest.prototype.sendGet = function(url)
{
	if (!this._xmlhttp) return false;
	this._xmlhttp.open("GET", url, false);
	this._xmlhttp.send(null);
	return this._xmlhttp.responseText;
}

HttpRequest.prototype.setPostVariables = function( post )
{
	if (!this._xmlhttp) return false;
	var separator = '';
	this._post = '';
	for ( name in post )
	{
		this._post +=	separator + encodeURIComponent( name ) + '=' +
									encodeURIComponent( post[name] );
		separator = '&';
	}
}

HttpRequest.prototype.sendPost = function(url)
{
	if (!this._xmlhttp) return false;
	this._xmlhttp.open("POST", url, false);
	this._xmlhttp.setRequestHeader(
		'Content-Type',
		'application/x-www-form-urlencoded; charset=UTF-8'
	);
	this._xmlhttp.setRequestHeader('Content-Length', this._post.length);
	this._xmlhttp.send(this._post);
	return this._xmlhttp.responseText;
}

function ajaxCall(dataUrl,returnFunction,nocache,debug,returnVar) {
	//create a variable for handling requests to be reused
	var http = null;

	//If nocache is passed, make each call unique
	if (nocache != null && nocache == 1) {
		var dt = new Date();
		var dtString = ''+dt.getFullYear()+dt.getMonth()+dt.getDate()+dt.getHours()+dt.getMinutes()+dt.getMilliseconds();
		//check for cookie - if disabled then append request.nocookies
		dataUrl = dataUrl + '&dtm='+dtString;
	} 
	if (debug != null && debug == 1 ) {prompt('',dataUrl);};
	
	//try to create the xmlHttpRequest object with non-IE code first, else fallback on IE
	try {
		http = new XMLHttpRequest(); // non-IE
		} 
	catch (a)
	{
		try
		{
			http = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (b)
		{
			try
			{
				http = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(c)
			{
				return false;
			}
		}
  }
	// more error checking
	try {
		http.open("GET", dataUrl , true);
	} catch (error) {
		return false;
	}
	//upon a change of status of the request for the lookup page, call the javascript handler
	http.onreadystatechange = function() {
		//readystate of 4 means the request is complete
		if (http.readyState == 4) {
			//status code of 200 means OK (regular status codes)
			if (http.status != 200) {
				return false;
			} else {
				returnFunction(http,returnVar);
			}
		} 
	}
	//close the connection (very important for memory leaks)
	http.send(null);
	return false;
}

function empty() {
	;
}

String.prototype.addslashes = function()
{
        return this.replace(/\\/g,'\\\\').replace(/\'/g,'\\\'');

};

String.prototype.htmlSpecialChars = function(attribute)
{
	var str = this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;');
	if (typeof attribute != 'undefined' && !attribute) str = str.replace(/"/g, '&quot;');
	return str;
}

function shareWithDevs(game_id, control)
{
  new Ajax.Request('../home/personal_developers.php', {
									   method: 'post',
 									   parameters: { game_id: game_id, shared: control.checked },
									   onSuccess: function(transport) { 
                       if ( ! transport.responseText || transport.responseText != 'ok' )
                         control.checked = ! control.checked;
										 },
                     onFailure: function(){ control.checked = ! control.checked }
									 });

}

/* Infowindow 'class' */
function InfoWindow(){
	this.Element = null;
	this.baseNodeType = 'div';
	this.markerOffset = 50;
	this.AnimateOnShow = true;
}

InfoWindow.prototype.Create = function( ){
	this.Element = document.createElement( this.baseNodeType );
 	this.Element.className = 'Details';

  var div1 = document.createElement( 'div' );
  var div2 = document.createElement( 'div' );
  var div3 = document.createElement( 'div' );
  var div4 = document.createElement( 'div' );
  var marker = document.createElement( 'div' );

  div1.className = 'Window_RB';
  div2.className = 'Window_LB';
  div3.className = 'Window_RT';
  div4.className = 'Window_LT';
  marker.className = 'Window_Marker';

  marker.style.left = this.markerOffset + 'px';

  div2.appendChild( div1 );
  div3.appendChild( div2 );
  div3.appendChild( div2 );
  div4.appendChild( div3 );
  div4.appendChild( marker );

 	this.Element.appendChild( div4 );
}

InfoWindow.prototype.SetText = function( html_or_node ){
	
	if ( this.Element != null ){
	  if ( typeof(html_or_node) == 'string' ){
			$(this.Element).find( 'div.Window_RB' ).get(0).innerHTML = html_or_node;
	  } else {
			$(this.Element).find( 'div.Window_RB' ).children().remove();
			$(this.Element).find( 'div.Window_RB' ).get(0).appendChild( html_or_node );
	  }
	}

}

InfoWindow.prototype.AddToDocument = function( parentNode, nextNode ){
	if ( nextNode ){
		parentNode.insertBefore( this.Element, nextNode );
	} else {
		parentNode.appendChild( this.Element );
	}

  if ( this.AnimateOnShow ){
    if ($.browser.msie) {
      var element = this.Element;
      var height = $(element).height();
      $(element).css( "height", $(element).height() );
      $(element).children().hide( 1, function(){
         $(element).children().show();
      } );
    } else {
      $(this.Element).children().css( 'opacity', '0' );
      $(this.Element).children().animate( { opacity: 1 }, 2000 );
    }
  }
}

InfoWindow.prototype.Remove = function( ){
	$(this.Element).remove();
}

function CreateInfoWindow( html_or_node, options ){
	var w = new InfoWindow();

	if ( typeof( options ) == 'object' ){
		for( var i in options ){
			switch( i ){
				case 'markerOffset' : {
					w[ i ] = Math.max( options[ i ], 25 );
				} break;
				default:
					w[ i ] = options[ i ];
			}
		}
	}
	w.Create();
	w.SetText( html_or_node );
	return w;
}

function RemoveInfoWindow( handle ){
	handle.Remove();
}

function NavigateToElement( jquery_tag ){
	var p = $( jquery_tag ).position();

	$('html').stop();

	var difference = p.top - $('html').scrollTop();

	$('html').animate(
		{
			scrollTop: '+=' + ( difference ) + 'px'
		}, 
		1000	
	);
}

function DeleteGame(game_id, title)
{
	if ( confirm("Are you sure you want to delete the game '" + title + "'? To get it back here, you need to upload it again.") )
	{
		window.location = 'personal.php?action=delete&game_id=' + game_id;
	}
}
