
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";
	}
}

//////////////////////////////////////////////////////////////////////
// ajax call (HttpRequest is onvolledig)

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;
}

//////////////////////////////////////////////////////////////////////
// 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( url, parameters ){
  this.parameters = parameters ? parameters : {};
  this.url = url;
  this.method = 'POST';
  this.OnResponse = null;
  this.OnResponseJson = null;

  this.http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    this.http_request = new XMLHttpRequest();
    if (this.http_request.overrideMimeType) {
      this.http_request.overrideMimeType('text/html');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!this.http_request) {
    alert('Cannot create XMLHTTP instance');
    return false;
  }

  var self = this;
  this.http_request.onreadystatechange = function(){
    if (self.http_request.readyState == 4) {
      if (self.http_request.status == 200) {
        if ( self.OnResponse ){
          self.OnResponse( self.http_request.responseText );
        }
        if ( self.OnResponseJson ){
          self.OnResponseJson( eval( "(" + self.http_request.responseText + ")" ) );
        }
      }
    }
  };
}

AjaxCall.prototype.Send = function(){
  this.method = this.method.toUpperCase() == 'GET' ? 'GET' : 'POST';

  var url = this.url;

  var parameters = '';
  for( var i in this.parameters ){
    if ( parameters != '' ){
      parameters += '&';
    }
    parameters += i + '=' + encodeURI( this.parameters[ i ] );
  }

  if ( this.method == 'GET' ){
    var join = this.url.indexOf( '?' ) == -1 ? "?" : "&";
    url = this.url + join + parameters;
  }

  this.http_request.open( this.method, url, true);

  this.http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  this.http_request.setRequestHeader("Content-length", parameters.length);
  this.http_request.setRequestHeader("Connection", "close");
  this.http_request.send( this.method == 'POST' ? parameters : null );
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
  var a_all_cookies = document.cookie.split( ';' );
  var a_temp_cookie = '';
  var cookie_name = '';
  var cookie_value = '';
  var b_cookie_found = false; // set boolean t/f default f

  for ( i = 0; i < a_all_cookies.length; i++ )
  {
    // now we'll split apart each name=value pair
    a_temp_cookie = a_all_cookies[i].split( '=' );


    // and trim left/right whitespace while we're at it
    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

    // if the extracted name matches passed check_name
    if ( cookie_name == check_name )
    {
      b_cookie_found = true;
      // we need to handle case where cookie has no value but exists (no = sign, that is):
      if ( a_temp_cookie.length > 1 )
      {
        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
      }
      // note that in cases where cookie is initialized but no value, null is returned
      return cookie_value;
      break;
    }
    a_temp_cookie = null;
    cookie_name = '';
  }
  if ( !b_cookie_found )
  {
    return null;
  }
}

/* this function opens the first element, and check's if the button exists and hides it */
function JQOpen( jquery_identifier ){
	$( jquery_identifier ).show();
	if ( jquery_identifier.substring( 0, 1 ) == '#' ){
		$( '#button_'  + jquery_identifier.substring( 1 ) ).hide();
	}
	return false;
}

function JQClose( jquery_identifier ){
	$( jquery_identifier ).hide();
  if ( jquery_identifier.substring( 0, 1 ) == '#' ){
    $( '#button_'  + jquery_identifier.substring( 1 ) ).show();
  }
	return false;
}

/* this function checks if the link can be injected insteaf of the html element */
function InjectLink( link, jqueryTag ){
	if ( $( jqueryTag ).length > 0 ){
		jQuery.get(
			link,
			{
				return_mode: 'inject'
			},
			function(data){
				$( jqueryTag ).replaceWith(data);
			}
		); 
		return true;
	}
	return false;
}
