var defaultButton=null;
var dialogButton=null;
var pickerButton=null;
var requests = new Array();
var bufferedUpdates = new Array();
var xhRequest;

/// <summary>
/// X-browser function to create an instance of the XML HTTP class of the browser environment
/// </summary>
if(typeof(XMLHttpRequest) == 'undefined')
var XMLHttpRequest = function()
{
	var request = null;
	try
	{
		request = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch(e)
	{
		try
		{
			request = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch(ee)
		{
			http_request = new XMLHttpRequest();
		}
	}
	return request;
}

/// <summary>
/// Stops currently running ajax requests
/// </summary>
function ajax_stop()
{
	for(var i=0; i<requests.length; i++)
	{
		if(requests[i] != null)
			requests[i].abort();
	}
}

/// <summary>
/// Creates an ajax request
/// </summary>
/// <param name="context">Current HTTP Context to access session state</param>
function ajax_create_request(context)
{
	for(var i=0; i<requests.length; i++)
	{
		if(requests[i].readyState == 4)
		{
			requests[i].abort();
			requests[i].context = null;
			return requests[i];
		}
	}

	var pos = requests.length;
	
	requests[pos] = Object();
	requests[pos].obj = new XMLHttpRequest();
	requests[pos].context = context;
	
	return requests[pos];
}


// insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement()
// for Netscape 6/Mozilla by Thor Larholm me@jscript.dk
// Usage: include this code segment at the beginning of your document
// before any other Javascript contents.
if(typeof HTMLElement!="undefined" && !
HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentElement = function
(where,parsedNode)
	{
		switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) 
this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function
(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}


	HTMLElement.prototype.insertAdjacentText = function
(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}

/// <summary>
/// Shows a div with a hourglass cursor on top of everything in the page
/// </summary>
function setWaiting()
{
	document.body.style.cursor = 'wait';
	var cursorDiv = document.getElementById('thisPageCursor');
	if (cursorDiv != null)
	{
		cursorDiv.style.display = '';
	}
	else
	{
		htmlText = "<div id='thisPageCursor' class='PageWaitCursor' style=\"cursor:wait; z-index:999; position:absolute; left:0; top: 0;" + 
		"width:100%; height:100%;\"></div>";
		
		document.body.insertAdjacentHTML("afterBegin", htmlText);
	}
	window.status = 'loading...';
}

/// <summary>
/// Hides the hourglass div
/// </summary>
function setReady()
{
	document.body.style.cursor = 'default';
	// remove the div when it exists
	var cursorDiv = document.getElementById('thisPageCursor');
	if (cursorDiv != null)
	{
		//alert('hide cursor');
		cursorDiv.style.display = 'none';
	}
	window.status = '';
}

function created_buffered_update(view, fieldName, modelProperty, newValue, controllerId)
{  
	var found = false;	
	for (var i = 0; i < bufferedUpdates.length; i++)
	{
		if (bufferedUpdates[i].modelProperty == modelProperty && bufferedUpdates[i].controllerId == controllerId)
		{							
			bufferedUpdates[i].view = view;
			bufferedUpdates[i].modelProperty = modelProperty;
			bufferedUpdates[i].newValue = encode(newValue);
			bufferedUpdates[i].controllerId = controllerId;	
			bufferedUpdates[i].fieldName = fieldName;	
			found = true;
		}
	}
	if (!found)
	{
		pos = bufferedUpdates.length;
		bufferedUpdates[pos] = new Object();
		bufferedUpdates[pos].view = view;
		bufferedUpdates[pos].modelProperty = modelProperty;
		bufferedUpdates[pos].newValue = encode(newValue);
		bufferedUpdates[pos].controllerId = controllerId;	
		bufferedUpdates[pos].fieldName = fieldName;	
	}
	return true;
}


function getBufferedUpdates()
{
	var data = "";
	for (var i = 0; i< bufferedUpdates.length; i++)
	{
		data = data + 'action=BufferedUpdate&view=' + bufferedUpdates[i].view
			+ '&model_property=' + bufferedUpdates[i].modelProperty
			+ '&controller_id=' + bufferedUpdates[i].controllerId
			+ '&field_name=' + bufferedUpdates[i].fieldName
			+ '&new_value=' + bufferedUpdates[i].newValue + '\r\n';
	}
	return data;
}

function clearBufferedUpdates()
{
	bufferedUpdates = new Array();
}

function getElementValue(el)
{	
	if (el.type == "checkbox") 
	{
		if (el.checked) return "true";
		else return "false";		
	}
	else return el.value;
}

function immediatePostback(view, fieldName, async)
{
	var commandString = new String();
	var controllerId = null;
	eval('controllerId = ' + view + 'ControllerId;');
		
	commandString = controllerURL + '?_method=ImmediatePostback&_sender=[FIELDNAME]&_controller_id=[CONTROLLERID]';	
	commandString = commandString.replace(/\[FIELDNAME\]/g,fieldName);	
	commandString = commandString.replace(/\[CONTROLLERID\]/g,controllerId);	
	
	ajax_request(commandString,'',null, null,async);	
}
	
function ajax_response()
{	
	if (xhRequest.obj.readyState != 4) { return;}
	
	setReady();	
	this.error = null;
	this.value = null;
	this.context = xhRequest.context;
	
	if(xhRequest.obj.status == 200 || xhRequest.obj.status == 500)
	{
		try
		{			
			this.value = object_from_json(xhRequest);						
	
			handleReturnCommands(this.value);
			if(this.value && this.value.error)
			{
				this.error = this.value.error;
				this.value = null;
			}
		}
		catch(e)
		{
			this.error = new ajax_error(e.name, e.description, e.number);
		}
	}
	else
	{	
		this.error = new ajax_error('HTTP request failed with status: ' + xhRequest.obj.status, xhRequest.obj.status);
	}
	
	return this;
}

function ajax_request(url, data, callback, context, async)
{
	setWaiting(); //alert ('Async: ' + async);
	if (async == null) async = false;
	if (xhRequest != null) {
		if (xhRequest.obj != null) {
			if (xhRequest.obj.readyState != 4) { return;}
		}
	}
	xhRequest = ajax_create_request(context);
	data += getBufferedUpdates();
	clearBufferedUpdates();
	xhRequest.obj.open('POST', url, async);
	if (async) xhRequest.obj.onreadystatechange = ajax_response;
	xhRequest.obj.send(data);
	if (!async) return new ajax_response(xhRequest);
}

function ajax_single_request(url, data, callback, context, async)
{
	setWaiting(); //alert ('Async: ' + async);
	if (async == null) async = false;
	if (xhRequest != null) {
		if (xhRequest.obj != null) {
			if (xhRequest.obj.readyState != 4) { return;}
		}
	}
	xhRequest = ajax_create_request(context);
	xhRequest.obj.open('POST', url, async);
	if (async) xhRequest.obj.onreadystatechange = ajax_response;
	xhRequest.obj.send(data);
	if (!async) return new ajax_response(xhRequest);
}

function handleReturnCommands(response)
{	
	var returnCommands = response.getElementsByTagName("ReturnCommands")[0];
	
	if (returnCommands != null)
	{
		var setRedirectReturnCommands = returnCommands.getElementsByTagName("Redirect");
		if (setRedirectReturnCommands != null)
		{		
			for (var i=0; i < setRedirectReturnCommands.length; i++) handleRedirectCommand(setRedirectReturnCommands[i]);		
		}
		
		var setDefaultButtonCommands = returnCommands.getElementsByTagName("DefaultButtonCommand");
		if (setDefaultButtonCommands != null)
		{
			for (var i=0; i < setDefaultButtonCommands.length; i++) handleSetDefaultButtonCommand(setDefaultButtonCommands[i]);
		}
		
		var updateCommands = returnCommands.getElementsByTagName("Update");
		var setClosePopUpCommands = returnCommands.getElementsByTagName("ClosePopUpCommand");
		if (setClosePopUpCommands != null)
		{	
			for (var i=0; i < setClosePopUpCommands.length; i++) handleClosePopUpCommand(setClosePopUpCommands[i]);
		}
		
		if (updateCommands != null)
		{
			for (var i=0; i < updateCommands.length; i++) handleUpdateCommand(updateCommands[i]);
		}
		
		var updateKeyValueList = returnCommands.getElementsByTagName("KeyValueListUpdate");
		if (updateKeyValueList != null)
		{		
			for (var i=0; i < updateKeyValueList.length; i++) handleUpdateKeyValueListCommand(updateKeyValueList[i]);
		}
		
		var tableDataUpdate = returnCommands.getElementsByTagName("TableDataUpdateCommand");
		if (tableDataUpdate != null)
		{		
			for (var i=0; i < tableDataUpdate.length; i++) handleTableDataUpdateCommand(tableDataUpdate[i]);		
		}
		
		var showDialogCommands = returnCommands.getElementsByTagName("ShowDialogCommand");
		if (showDialogCommands != null)
		{		
			for (var i=0; i < showDialogCommands.length; i++) handleShowDialogCommand(showDialogCommands[i]);		
		}
		
		var dataInvalidReturnCommands = returnCommands.getElementsByTagName("DataInvalid");
		if (dataInvalidReturnCommands != null)
		{		
			for (var i=0; i < dataInvalidReturnCommands.length; i++) handleDataInvalidReturnCommand(dataInvalidReturnCommands[i]);		
		}	
		
		var setVisibilityReturnCommands = returnCommands.getElementsByTagName("SetVisibility");
		if (setVisibilityReturnCommands != null)
		{		
			for (var i=0; i < setVisibilityReturnCommands.length; i++) handleSetVisibilityReturnCommand(setVisibilityReturnCommands[i]);		
		}
		
		var setEnabledReturnCommands = returnCommands.getElementsByTagName("SetEnabled");
		if (setEnabledReturnCommands != null)
		{		
			for (var i=0; i < setEnabledReturnCommands.length; i++) handleSetEnabledReturnCommand(setEnabledReturnCommands[i]);		
		}
		
		
		
		var setShowPopUpCommands = returnCommands.getElementsByTagName("ShowPopUpCommand");
		if (setShowPopUpCommands != null)
		{
			for (var i=0; i < setShowPopUpCommands.length; i++) handleShowPopUpCommand(setShowPopUpCommands[i]);
		}
		
		var setShowNewWindowCommands = returnCommands.getElementsByTagName("ShowNewWindowCommand");
		if (setShowNewWindowCommands != null)
		{
			for (var i=0; i < setShowNewWindowCommands.length; i++) handleShowNewWindowCommand(setShowNewWindowCommands[i]);
		}
		
		var setExecuteScriptCommands = returnCommands.getElementsByTagName("ExecuteScriptCommand");
		if (setExecuteScriptCommands != null)
		{
			for (var i=0; i < setExecuteScriptCommands.length; i++) handleExecuteScriptCommand(setExecuteScriptCommands[i]);
		}
		
	}
}

function handleRedirectCommand(command)
{
	var url = command.getAttribute("url");
	if (url != null) window.location = url;
}

function handleExecuteScriptCommand(command)
{
	var script = command.getAttribute("script");
	eval(script);
}

function handleTableDataUpdateCommand(command)
{	//alert('tabledataupdate');
	var tableName = command.getAttribute("TableName");
	var view = command.getAttribute("View");
		
	//eval( view + 'ValueStore.' + tableName + ' = command.getElementsByTagName(\"TableData\")[0];');
	//eval( view + 'TableDef' + tableName + ' = command.getElementsByTagName(\"TableDef\")[0];');
	
	var divElement = null;
	eval('divElement = document.getElementById(' + view + 'Controls.' + tableName + ')');
	
	if (divElement != null)
	{	
		var cmd = 'writeTableHeader("[VIEW]", "[TABLENAME]", [VIEW]Controls.[TABLENAME], command, [VIEW]ValueStore.[TABLENAME]);';
		cmd = cmd.replace(/\[VIEW\]/g,view);
		cmd = cmd.replace(/\[TABLENAME\]/g,tableName);	

		eval(cmd);	
	}
	//alert('finished updating table');
}

/// <summary>
/// Updates a value of a bound control with the new value as specified
/// in the update command
/// </summary>
/// <param name="command">Update command</param>
function handleUpdateCommand(command)
{
	try
	{
		var view = command.getElementsByTagName("view")[0].childNodes[0].nodeValue;
		var propertyName = command.getElementsByTagName("propertyName")[0].childNodes[0].nodeValue;
		
		var newValue = new String();
		if (command.getElementsByTagName("newValue")[0].childNodes.length == 0)		
			newValue = "";
		else
			newValue = decode_utf8(decode(command.getElementsByTagName("newValue")[0].childNodes[0].nodeValue));
		//alert(newValue);
		
		var modelMapping = null;
		//alert(view);
		eval( 'modelMapping = ' + view + 'ModelMapping[\'' + propertyName + '\'];');	
		
		for (var i = 0; i < modelMapping.length; i++)
		{
			var control = null;
			eval('control = ' + view + 'Controls.' + modelMapping[i] + ';');
			//alert(control);		
			if (control != null)
			{
				document.getElementById(control).value = newValue;
				
				if (newValue == 'false')
				{
					document.getElementById(control).checked = false;				
				}
				else
					document.getElementById(control).checked = true;
				
				clearErrorMark(view, modelMapping[i]);
			}
		}
	}
	catch(e)
	{
	}
}

/// <summary>
/// Updates a combo box value list with the values specified in the update command
/// </summary>
/// <param name="command">Update Key Value list command</param>
function handleUpdateKeyValueListCommand(command)
{
	var view = command.getElementsByTagName("view")[0].childNodes[0].nodeValue;
	var keyValueListName = command.getElementsByTagName("keyValueListName")[0].childNodes[0].nodeValue;

	var newValue = command.getElementsByTagName("rows")[0].childNodes;
	var list = new Array();
	
	var values = new Array();
	var text = new Array();
	for (var i = 0; i < newValue.length; i ++)
	{
		//text.[i] = decode_utf8(decode(newValue[i].childNodes[0].nodeValue));
		if (newValue[i].childNodes.length > 0)
			text[i] = newValue[i].childNodes[0].nodeValue;
		else
			text[i] = '';
		values[i] = newValue[i].attributes[0].nodeValue;
	}
	
	var newList = new Object();
	newList.values = values;
	newList.text = text;

	eval(view + 'ValueStore.' + keyValueListName + ' = newList;');
	
	var keyValueListMapping = null;
	
	eval( 'keyValueListMapping = ' + view + 'KeyValueListMapping[\'' + keyValueListName + '\'];');	

	for (var i = 0; i < keyValueListMapping.length; i++)
	{	
		updateDropDownList(view, keyValueListMapping[i] , keyValueListName);
	}
	
}

function updateDropDownList(view, control, list)
{	

	var dropdown = null;	
	eval('dropdown = ' + view + 'Controls.' + control);
	dropdown = document.getElementById(dropdown);

	dropdown.innerHTML = '';	
	var valueList = null
	eval('valueList = ' + view + 'ValueStore.' + list + '.values');
	var textList = null
	eval('textList = ' + view + 'ValueStore.' + list + '.text');

	for (var i = 0; i < valueList.length; i++)
	{		
		
		var option = document.createElement("OPTION");
		option.value = valueList[i];
		option.text = decode_utf8(decode(textList[i]));
		try
		{
			dropdown.add(option,null);		
		}
		catch(e)
		{
			dropdown.add(option);
		}
	}
	dropdown.size = valueList.length;
	dropdown.size = 1;
}

function handleShowDialogCommand(command)
{
	showDialog(command);
}

function handleShowNewWindowCommand(command)
{ 
	var url = command.getAttribute("url");
	var title = command.getAttribute("title");
	var width = command.getAttribute("width");
	var height = command.getAttribute("height");
	
	var confirmWin = null;
	var window_properties = 'width="' + width + '",height="' + height + '", status=1, resizable=1, scrollbars=1';
	
	//	deze met ="90%" / ="1000"
	//	1. width: de breedte van het venster wanneer het geopend wordt
	//	2. height : de hoogte van het venster wanneer het geopend wordt
		
	//	deze wel of niet aanwezig (alternatief: =yes / =no), niet aanwezig is default
	//	3. location : de adresbalk van het venster (waar de URL van de pagina altijd in staat)
	//	4. status : de statusbalk van het venster (staat bij IE standaard onderin het scherm)
	//	5. menubar :de menubalk van het venster
	//	6. directories : andere directory-knoppen van de browser
	//	7. toolbar : hier staan de verschillende knoppen op: vorige, home, print, etcetera
	//	8. resizable : geeft aan of de bezoeker de grootte van het venster mag aanpassen
	//	9. scrollbars : het venster krijgt schuifbalken als de inhoud groter is dan de vensterafmetingen
	
	confirmWin = window.open(url, title, window_properties);

	if((confirmWin != null) && (confirmWin.opener==null)) 
	{ 	confirmWin.opener = self;	}
}

function handleShowPopUpCommand(command)
{//alert('showpopup');
	var title = command.getAttribute("title");
	//alert(title);
	var url = command.getAttribute("url");
	//alert(url);
	var width = command.getAttribute("width");
	var height = command.getAttribute("height");
	var view = command.getAttribute("view");
	//	showPopUp(url, title, parseInt(width), parseInt(height), view);	
	
	showPopUp(url, title, width, height, view);
}

function handleClosePopUpCommand(command)
{ 
	var view = command.getAttribute("view");
	hidePopWinFromPopUp(view);
}

function handleSetVisibilityReturnCommand(command)
{
	var fieldName = command.getAttribute("fieldName");
	var view = command.getAttribute("view");
	var visible = command.getAttribute("visible");
	
	setVisibleFunction(fieldName, view, visible);
}

function setVisibleFunction(fieldName, view, visible)
{

	var elementId = "";
	
	eval ('elementId = ' + view + 'Controls.' + fieldName);	
	
	if (elementId != null)
	{
		var el = document.getElementById(elementId);
		if (el != null)
		{
			try
			{
				if (visible == "false")
				{
					el.style.display = "none";
				}
				else
					el.style.display = "";
			}
			catch (e)
			{
			}			
		}
	}
}

function handleSetEnabledReturnCommand(command)
{
	
	var fieldName = command.getAttribute("fieldName");
	var view = command.getAttribute("view");
	var enabled = command.getAttribute("enabled");
	
	setEnabledFunction(fieldName, view, enabled);
	

}

function setEnabledFunction(fieldName, view, enabled)
{	
	var elementId = "";
	
	eval ('elementId = ' + view + 'Controls.' + fieldName);
	
	if (elementId != null)
	{
	
		var el = document.getElementById(elementId);
		if (el != null)
		{
			try
			{
			//alert(el.type);
				if (enabled == "false")
				{
					el.disabled = true;
					if (el.type == 'submit')
						el.className = "submitReadOnly";
					else if (el.type == 'select-one')
						el.className = "SelectReadOnly";
					else if (el.type == 'checkbox')
						el.className = "CheckboxReadOnly";
					else if (el.type == "text" || el.type == "password")
						el.className = "textReadOnly";
					else if (el.type == "file")
						el.className = "fileReadOnly";
					else if (el.type == "textarea")
						el.className = "textareaReadOnly";
				}
				else
				{
					el.disabled = false;
					if (el.type == 'submit')
						el.className = "submit";
					else if (el.type == 'select-one')
						el.className = "Select";
					else if (el.type == 'checkbox')
						el.className = "Checkbox";
					else if (el.type == "text" || el.type == "password")
						el.className = "text";
					else if (el.type == "file")
						el.className = "File";
					else if (el.type == "textarea")
						el.className = "textarea";
					
				}
			}
			catch (e)
			{}
			try
			{
				el = document.getElementById(elementId + '_' + fieldName + '_imgSearch');
				if (el != null)
				{
				
					if (enabled == "false")
					{
						el.width = 0;
						el.disabled = true;
					}
					else
					{
						el.width = 16;
						el.disabled = false;
					}
				}
			}
			catch (e)
			{}
		}
	}
}

function setFocusFunction(fieldName, view)
{	
	var elementId = "";
	
	eval ('elementId = ' + view + 'Controls.' + fieldName);
	
	if (elementId != null)
	{
		var el = document.getElementById(elementId);
		if (el != null)
		{
			try
			{
				el.focus();
			}
			catch (e) {}
		}
	}
}

function handleDataInvalidReturnCommand(command)
{		
	var fieldName = command.getAttribute("fieldName");
	var view = command.getAttribute("view");
	var elementId = "";
	
	eval ('elementId = ' + view + 'Controls.' + fieldName);
	var el = document.getElementById(elementId);
	el.value = '';
	var parentEl = el.parentElement;
	
	
	var elErrorDiv = document.getElementById(elementId + '_errorDiv');
	if (elErrorDiv == null) elErrorDiv = document.createElement("<span id='" + elementId + "_errorDiv' style='font-size:larger;font-weight:bold;color:#ff0000'>");
	
	elErrorDiv.innerHTML = "&nbsp;&nbsp;!";
	//el.insertAdjacentElement("afterEnd",elErrorDiv);
	parentEl.appendChild(elErrorDiv);
}

function handleSetDefaultButtonCommand(command)
{
	var buttonName = command.getAttribute("buttonName");
	//alert('setting ' + buttonName + ' as default button');
	defaultButton = document.getElementById(buttonName);
	//alert(defaultButton);
}

function clearErrorMark(view, fieldName)
{	
	var elementId = "";
	
	eval ('elementId = ' + view + 'Controls.' + fieldName);
	
	var el = document.getElementById(elementId);
	var parentEl = el.parentElement;
	
	var elErrorDiv = document.getElementById(elementId + '_errorDiv');
	if (elErrorDiv != null) parentEl.removeChild(elErrorDiv);
}


function enc(s)
{
	return s.toString().replace(/\%/g, "%26").replace(/=/g, "%3D");
}

function object_from_json(request)
{
	if(request.obj.responseXML != null && request.obj.responseXML.xml != null && request.obj.responseXML.xml != '')
		return request.obj.responseXML;	
	else
	{
		return xmlDOM(request.obj.responseText);
	}
	
	//alert(request.obj.responseText);
	//var r = null;	
	//eval('r=' + request.obj.responseText + ';');
	//return r;
}

function ajax_error(name, description, number)
{
	this.name = name;
	this.description = description;
	this.number = number;

	return this;
}

ajax_error.prototype.toString = function()
{
	return this.name + " " + this.description;
}

function json_from_object(o)
{
	if(o == null)
		return 'null';

	switch(typeof(o))
	{
		case 'object':
			if(o.constructor == Array)		// checks if it is an array [,,,]
			{
				var s = '';
				for(var i=0; i<o.length; ++i)
				{
					s += json_from_object(o[i]);

					if(i < o.length -1)
						s += ',';
				}

				return '[' + s + ']';
			}
			break;
		case 'string':
			return '"' + o.replace(/(["\\])/g, '\\$1') + '"';
		default:
			return String(o);
	}
}


var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

function encode(decStr){
  decStr=encodeURIComponent(decStr);		//line add for chinese char
  var bits, dual, i = 0, encOut = '';
  while(decStr.length >= i + 3){
    bits =
    (decStr.charCodeAt(i++) & 0xff) <<16 |
    (decStr.charCodeAt(i++) & 0xff) <<8  |
     decStr.charCodeAt(i++) & 0xff;
    encOut +=
     base64s.charAt((bits & 0x00fc0000) >>18) +
     base64s.charAt((bits & 0x0003f000) >>12) +
     base64s.charAt((bits & 0x00000fc0) >> 6) +
     base64s.charAt((bits & 0x0000003f));
    }
  if(decStr.length -i > 0 && decStr.length -i < 3){
    dual = Boolean(decStr.length -i -1);
    bits =
     ((decStr.charCodeAt(i++) & 0xff) <<16) |
     (dual ? (decStr.charCodeAt(i) & 0xff) <<8 : 0);
    encOut +=
      base64s.charAt((bits & 0x00fc0000) >>18) +
      base64s.charAt((bits & 0x0003f000) >>12) +
      (dual ? base64s.charAt((bits & 0x00000fc0) >>6) : '=') +
      '=';
    }
  return encOut
  }

function decode(encStr) {
  var bits, decOut = '', i = 0;
  for(; i<encStr.length; i += 4){
    bits =
     (base64s.indexOf(encStr.charAt(i))    & 0xff) <<18 |
     (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | 
     (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 |
      base64s.indexOf(encStr.charAt(i +3)) & 0xff;
    decOut += String.fromCharCode(
     (bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
    }
  if(encStr.charCodeAt(i -2) == 61)
    undecOut=decOut.substring(0, decOut.length -2);
  else if(encStr.charCodeAt(i -1) == 61)
    undecOut=decOut.substring(0, decOut.length -1);
  else undecOut=decOut;
  
	try
	{
		return decodeURIComponent(undecOut);		//line add for chinese char
	}
	catch(e)
	{
	}
  return unescape(undecOut);
}

function decode_utf8(utftext) { 
    var plaintext = ""; var i=0; var c=c1=c2=0;
    // while-Schleife, weil einige Zeichen uebersprungen werden
    while(i<utftext.length)
        {
        c = utftext.charCodeAt(i);
        if (c<128) {
            plaintext += String.fromCharCode(c);
            i++;}
        else if((c>191) && (c<224)) {
            c2 = utftext.charCodeAt(i+1);
            plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
            i+=2;}
        else {
            c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
            plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
        i+=3;}
        }
    return plaintext;
}
function xmlDOM(xml) {	
	var xmlDoc = null;	
	try
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false"
		xmlDoc.loadXML(xml)
	}
	catch(e)
	{
		var parser = new DOMParser(); 
		xmlDoc = parser.parseFromString(xml, 'text/xml');		
	}
	return xmlDoc;
}


if(document.all){ //ie has to block in the key down
    document.onkeypress = onKeyPress;
}else if (document.layers || document.getElementById){ 
    document.onkeydown = onKeyPress;
}

function onKeyPress(evt)
{
	if (getKeyCode(evt)==13)
	{
		if (pickerButton!=null)
			pickerButton.click();
		else if (dialogButton!=null)
		{
			dialogButton.click();
			dialogButton = null;
		}
		else if (defaultButton!=null)		
		{			
			defaultButton.focus();
			defaultButton.click();
		}
		return false;
	}
}



function getKeyCode(evt)
{
	var oEvent = (window.event) ? window.event : evt;
	return oEvent.keyCode ? oEvent.keyCode :
					oEvent.which ? oEvent.which : 
					void 0;	
}


var RealJAXVersion = '0.0.0.1'



