var iframeObj = null;
var iframeResponseCBFunc = null;

/*
urf8 helper functions, see
http://www.worldtimzone.com/res/encode/?fld=%C3%BC%C3%B6%C3%A4
/* ***************************
** Most of this code was kindly 
** provided to me by
** Andrew Clover (and at doxdesk dot com)
** http://and.doxdesk.com/ 
** in response to my plea in my blog at 
** http://worldtimzone.com/blog/date/2002/09/24
** It was unclear whether he created it.
*/
function utf8(wide) {
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) {
  return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

function encodeURIComponentNew(s) {
  var s = utf8(s);
  var c;
  var enc = "";
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1)
      enc += "%"+toHex(s.charCodeAt(i));
    else
      enc += s.charAt(i);
  }
  return enc;
}


/*
 submit a RPC via an IFRAME
 a variable argument list is passed to the server, format:
 (<serverURL>, <name1>, <value1>, <name2>, <value2>, ...)
   becomes
 ?name1=value1&name2=value2&...
*/
function rpcIFrame() {

  var qs = ''
  for (e=1;e<rpcIFrame.arguments.length;e+=2) {
      qs+=(qs=='')?'?':'&'
//      qs+=rpcIFrame.arguments[e]+'='+encodeURIComponentNew(rpcIFrame.arguments[e+1])
	    //qs+=rpcIFrame.arguments[e]+'='+encodeURI(rpcIFrame.arguments[e+1])
	    qs+=rpcIFrame.arguments[e]+'='+rpcIFrame.arguments[e+1]
    }
	
	var URL = rpcIFrame.arguments[0];
	URL = URL + qs;
		
	if (!document.createElement) {
//		return true
	};
	var IFrameDoc;

	if (!iframeObj && document.createElement) {
		/*
		create the IFrame and assign a reference to the
		object to our global variable iframeObj.
		this will only happen the first time 
		rpcIFrame() is called
		*/
		try {
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','RSIFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			iframeObj = document.body.appendChild(tempIFrame);
			
			if (document.frames) {
				/*
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				*/
				iframeObj = document.frames['RSIFrame'];
			}
		} catch(exception) {
			/*
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			*/
			iframeHTML='<iframe id="RSIFrame" style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			iframeObj = new Object();
			iframeObj.document = new Object();
			iframeObj.document.location = new Object();
			iframeObj.document.location.iframe = document.getElementById('RSIFrame');
			iframeObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}
	
	if (navigator.userAgent.indexOf('Gecko') !=-1 && !iframeObj.contentDocument) {
		/*
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		*/
		setTimeout('rpcIFrame()',10);
//		return false;
	}
	
	if (iframeObj.contentDocument) {
		/* For NS6 */
		IFrameDoc = iframeObj.contentDocument; 
	} else if (iframeObj.contentWindow) {
		/* For IE5.5 and IE6 */
		IFrameDoc = iframeObj.contentWindow.document;
	} else if (iframeObj.document) {
		/* For IE5 */
		IFrameDoc = iframeObj.document;
	} else {
//		return true;
	}
	
	IFrameDoc.location.replace(URL);
//	return false;
}

function setResponseCallbackFunction( theSpecificCB )
{
	iframeResponseCBFunc = theSpecificCB
}

function handleResponse(msg) {
	if( iframeResponseCBFunc==null )
		;
	else
		if(typeof window[iframeResponseCBFunc] == 'function') window[iframeResponseCBFunc](msg);
}

function encode_utf8(rohtext) {
// dient der Normalisierung des Zeilenumbruchs
rohtext = rohtext.replace(/\r\n/g,"\n");
var utftext = "";
for(var n=0; n<rohtext.length; n++)
{
// ermitteln des Unicodes des  aktuellen Zeichens
var c=rohtext.charCodeAt(n);
// alle Zeichen von 0-127 => 1byte
if (c<128)
utftext += String.fromCharCode(c);
// alle Zeichen von 127 bis 2047 => 2byte
else if((c>127) && (c<2048)) {
utftext += String.fromCharCode((c>>6)|192);
utftext += String.fromCharCode((c&63)|128);}
// alle Zeichen von 2048 bis 66536 => 3byte
else {
utftext += String.fromCharCode((c>>12)|224);
utftext += String.fromCharCode(((c>>6)&63)|128);
utftext += String.fromCharCode((c&63)|128);}
}
return utftext;
}

