		top.currentCall = 0;
		top.data = Object();
		
		jQuery(document).ready(
			function() {
				nazorg(jQuery('body'));

				if (jQuery.browser.mozilla) {	
					top.brows = 'moz';
				} else if (jQuery.browser.webkit) {
					top.brows = 'webkit';
				} else if (jQuery.browser.opera) {
					top.brows = 'opera';
				} else if (jQuery.browser.msie) {
					top.brows = 'msie';
				} else {
					top.brows = 'unknown';
				}

				jQuery('#spinner').css('-' + top.brows + '-border-radius','10px 0px 10px 0');

			}
		);

		function say(something) {
			if(typeof console == 'object') {
				if(typeof console.log == 'function') {
					console.log(something);
				}
			}		
		}
		

		function postform2div(formid) {
			// does not send input[type=file] !!!

			var rawdata = jQuery('#' + formid).serializeArray(); 
			
			var data = {} ;
			jQuery.each(rawdata, function(i, field) {
				data[field.name] = field.value;
			});

			url = jQuery('#' + formid).attr('action');

			post2div(url,  data['div'], data);
			return false;
		}

		function sendForm(formid) {
			jQuery('body').append('<iframe class="formHandler" name="formHandler" id="formHandler"/>');
			jQuery('#' + formid).attr('target', 'formHandler');
			return true;
		}



		
		function append2div(url, div, data) {
			data['append'] = 'true';
			post2div(url, div, data);
		}

		function post2div(url, div, data) {
			spinner(true);
			if (div == '') {
				data['div'] = 'commandDiv';
			} else {
				data['div'] = div;
			}
			data['json'] = 'true';
			var currentTime = new Date();
			data['time'] = currentTime.getTime();
			top.currentCall++;
			data['currentCall'] = top.currentCall;
			//console.log(data);

			jQuery.post(url, data, getreply);
		}

		
		function getreply(rawdata) {
			if ((rawdata.substring(0, 1) != '{') && (rawdata.substring(0, 1) != '[')) {
				// geen echte json teruggekregen
				//mainerror('<h1>Error</h1><h2>No valid reply from server:</h2><xmp>' + rawdata + '</xmp>');
				mainerror('<h1>Error</h1><P style="cursor: pointer;" onClick="jQuery(\'#errormessage\').css(\'display\',\'block\');">The server returned an error. Please try again. </p><span id="errormessage" style="display: none; color: red;"><xmp>' + rawdata + '</xmp></span>');
			} else {
				var result = parseJson(rawdata);
//				mainerror ("<p>got valid reply " + result['currentCall']  + "</p><xmp>" + rawdata + "</xmp>");
				if ((result['currentCall'] >= top.currentCall) ||  (result['forced'] == 'true')) {
//					mainerror ("<p>using reply " + result['currentCall']  + "</p><xmp>" + rawdata + "</xmp>");
					if (result['prescript']) {
						eval(result['prescript']); // bevat eventuele javascript code die van te voren moet worden uitgevoerd
					}
					if (jQuery('#' + result['div']).length > 0) {
						if (result['append']) {
							say('append');
							jQuery('#' + result['div']).append(result['html']);
						} else {
							say('replace');
							jQuery('#' + result['div']).empty().append(result['html']);
						}
					} else {
						jQuery('body').append('<div id=\''+ result['div'] + '\'>' + result['html'] + '</div>');				
					}
					nazorg(jQuery('#'+ result['div']));
					if (result['javascript']) {
						eval(result['javascript']); // bevat eventuele javascript code die na afloop moet worden uitgevoerd
					}
					jQuery('form').submit(
						function() {
							jQuery('#spinner').css('display','block');					
						}
					)

				} else {
					say('dropping server response for call ' + result['currentCall'] + ' to ' + result['div'] + '. html is: ' + result['html']);

				} 
			}
			if (result['callback']) {
				eval(result['callback']); // bevat eventuele callback function
			}
			spinner(false);
 		}

		function nazorg(div) {
			say(div);
			div.find('span').each(
				function(index) {
					if (jQuery.isFunction(jQuery(this).attr('onclick'))) jQuery(this).addClass('spanlink');
				}
			);
			div.find('a').click(
				function() {
					spinner(true);
				}
			)
			div.find('.ajaxform').submit(
				function(e) {

					//alert('special');
					if (jQuery(this).attr('id') !== '') {
						if (jQuery(this).has('input[type=file]').length) {
							//alert('eerste');
							return sendForm(jQuery(this).attr('id'));
						} else {
							e.preventDefault();
							//alert('tweede');
							postform2div(this.id);
						}
					} else {
						e.preventDefault();
						alert('form.ajaxform heeft geen id!!');

					}

					return true;
				}
			);
			if (isdefined('customNazorg')) {
				customNazorg(div);
			}

		}

		function spinner(aan) {
			if (aan) {
				jQuery('#spinner').css('opacity','0.75').show();
			} else {
				jQuery('#spinner').fadeOut();
			}
		}
		function parseJson(data) {
			if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '[')) {
    				return { status: 0, data: data.length ? data : 'Unspecified error' };
  			}
  			return eval('(' + data + ');');
		};

		
 		function mainerror (error) {
				jQuery('#mainerror').remove();
				jQuery('body').append('<div id="mainerror"></div>');
				jQuery('#mainerror').empty().append(error + '<span class="close button right" style="margin-top: 30px;" onClick="jQuery(\'#mainerror\').remove();">close</span>');
				//{ zIndex: 99999, title: 'eror', modal: true, buttons: { "Ok": function() { $(this).dialog("close"); } } }
				//timerid = setTimeout('jQuery(\'#mainerror\').remove();', 15000);
 		
 		}
// JavaScript Document
/*
// ///////////////////////////
// isdefined v1.0
// 
// Check if a javascript variable has been defined.
// 
// Author : Jehiah Czebotar
// Website: http://www.jehiah.com
// Usage  : alert(isdefined('myvar'));
// ///////////////////////////
*/
function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}


/* function createWindow(window_id) {
	if (jQuery('#' + window_id).length == 0) {
		jQuery('body').append('			<div id="window_' + window_id + '" class="containerPlus resizable draggable {left:100, top:100, width:800, height:500, buttons:\'m,c\', skin:\'white\'}" ' + 
'				<div class="no"><div class="ne"><div class="n">' + window_id + '</div></div> ' + 
'					<div class="o"><div class="e"><div class="c"> ' + 
'						<div class="mbcontainercontent"> ' +
'						<span id="' + window_id + '"></span> ' + 
'						</div> ' + 
'					</div></div></div> ' + 
'					<div > ' + 
'					<div class="so"><div class="se"><div class="s"></div></div></div> ' + 
'					</div> ' + 
'				</div> ' + 
'			</div>');
		jQuery(function(){
			jQuery(".containerPlus").buildContainers({
				containment:"document",
			});
		});
		// center:
		jQuery('#window_editor').css('left','50%').css('margin-left','-' + jQuery('#window_editor').css('width').replace('px', '') / 2  + 'px').css('top','100px');
		

	}
}

*/

