
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 DeleteUnSuccessfull(){
	$('#unsuccesful').hide( 1000 );
	ajaxCall( '../swinxs/deleteunsuccesful.php?', function(){}, 1, 0, 0 );
}

function Ask_DownloadUpdate(){
	$('#dlfirmware').show(1000);
}

function DownloadUpdate( id ){
  $('#divfirmware').hide();
	ajaxCall( '../swinxs/install_firmware.php?id=' + id, function(){
		checkQueue();
	}, 1, 0, 0 ); 
}

function confirmInstallFirmware(download_id)
{
	if ( confirm("Weet je zeker dat je je Swinxs nu wilt voorzien van de nieuwste software?") )
  {
  	ajaxCall( '../swinxs/install_firmware.php?id=' + download_id, function() {
	  	checkQueue();
	  }, 1, 0, 0 ); 
  }
}

function onBlocksResize(){
  var Block_2 = document.getElementById( 'Block_2' );
  var w = $(Block_2).parent().width();
  Block_2.style.width = (w - $('#Block_1').width() - 10 ) + '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) 
{
	//status code of 200 means OK (regular status codes)
	return ajaxCallRich(dataUrl, function(http, returnVar) { if ( http.status == 200 ) returnFunction(http,returnVar); }, nocache, debug, returnVar);
}

function ajaxCallRich(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 ) returnFunction(http, returnVar);
	}
	//close the connection (very important for memory leaks)
	http.send(null);
	return true;
}

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 openviaform( id ){
	$('form.via_form').find('div').hide();
	document.forms['via_form'].know_swinxs.value = id;
	$('#via_' + id).show();
}

function jquery_open( url, jquery_target ){
	if ( $( jquery_target ).length > 0 ){
		jQuery.get( url, { inline: 'true' }, function( data ) {
			$( jquery_target ).html( data );
		} );
		return false;
	}
	return true;
}

function jquery_submit_form( button, jquery_target ){
	if ( $( jquery_target ).length > 0 ){
		var form = $(button).parents('form').get( 0 );
		if ( form ){
			var data = { inline: 'true' };
		
			// todo, textarea, checkbox, radio
			$(form).find( 'input,textarea,select' ).each(function(){
				data[ this.name ] = this.value;
			});

			jQuery.ajax( {
				url: form.action,
				data: data,
				type: form.method,
				success: function( data ){
					$( jquery_target ).html( data );
				}
			} );	
			
			return false;
		}
	}
	return true;
}

