

function combine (o) {

  var encode = function (data) {
	  var regexp = /%20/g;
	  return encodeURIComponent(data).replace(regexp,"+");
  }

  var map = function (f, ar) {
	  var res = [];
	  for (var i = 0; i < ar.length; i++) {
		  res.push(f(ar[i]));
	  }
	  return res;
  }

  var vals = [];
  for (var p in o) {
	  if (o[p] instanceof Array) {
		  vals.push(encode(p) + "=" + map(encode, o[p]).join("&" + encode(p) + "="));
	} else {
		vals.push(encode(p) + "=" + encode(o[p]));
	}	
  }
  return vals.join("&");
}

function collect3 (id) {

	var cbs = {'text':
			   function (o) { return o.value; }

			   ,'hidden':
			   function (o) { return o.value; }

			   ,'textarea':	
			   function (o) { return o.value; }
			   ,'select-one' :
			   function (o) {
				   var idx = o.options.selectedIndex;
				   return idx != -1 ? o.options[idx].value : null;
			   }

			   ,'select-multiple' :
			   function (o) {
				   var vals = [];
				   for (var i = 0; i < o.options.length; i++) {
					   if (o.options[i].selected) {
						   vals.push(o.options[i].value);
					   }
				   }
				   return vals.length ? vals : null;
			   }

			   ,'radio':
			   function (o) {
					   if (o.checked) {
						   return o.value;
					   }
				   }

			   ,'checkbox':
			   function (o) {
				   if (o.checked) {
					   return o.value;
				   }
			   }
			  };


	var node = document.getElementById(id);
	var es = node.elements;
	var values = {};
	for (var i = 0; i < es.length; i++) {
		var e = es[i];
		var t = e.type;
		if (cbs[t] !== undefined) {
			var val =  cbs[t](e);
			if (val) {
				if (values[e.name]) {
					values[e.name].push(val);
				} else {
					if (val instanceof Array) {
						values[e.name] = val;
					} else {
						values[e.name] = [val];
					}
				}
			}
		}
	}
	return combine(values);
}

function SendPostIdReplace(params) {
	var url	   = params.url || '';
	var formid = params.formid || 'form1';
	var divid  = params.divid || 'div1';
	var cb	   = params.cb || function (resp, err) {
		                          var node = document.getElementById(divid);
								  if (!err) {
									  node.innerHTML = resp;
								  }
						      }

	SendRequest(url, cb, 'POST', collect3(formid));
}



function XMLRequester(callback, datatype)
{
    var xmlHttp = new getXMLRequester();
    if( !xmlHttp ) return;
    this.send = send;
    this.process = process;

    function send (method, query,data, sync)
    {
	sync = sync ? false : true;
	switch (method)
	    {
	    case 'POST':
			xmlHttp.open("POST", query, sync);
			xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
			xmlHttp.setRequestHeader( 'Content-length', data.length );
			break;
	    case 'HEAD':
			xmlHttp.open("HEAD", query, sync);
			data = null;
			break;
	    default: // GET
			xmlHttp.open("GET", query, sync);
	    }
	xmlHttp.onreadystatechange = this.process;
	xmlHttp.send(data);
    }

    function process ()
    {
	// status 0 UNINITIALIZED open() has not been called yet.
	// status 1 LOADING send() has not been called yet.
	// status 2 LOADED send() has been called, headers and status are available.
	// status 3 INTERACTIVE Downloading, responseText holds the partial data.
	// status 4 COMPLETED Finished with all operations.
	switch(xmlHttp.readyState)
	    {
		// uninitialized
	    case 0:
		// loading
	    case 1:
		// loaded
	    case 2:
		// interactive
	    case 3:
		break;
		// complete
	    case 4:
			if( xmlHttp.status == 200 )	// success
				callback(datatype == 'xml' ? xmlHttp.responseXML : xmlHttp.responseText);
			else // loading not successfull, e.g. page not available
				callback(xmlHttp.statusText, xmlHttp.status);
	    }
    }
}


function SendRequest (query, callback, method, data, datatype, sync)
{
    var req = new XMLRequester(callback, datatype);
    if( !req ) return;
    req.send(method, query, data, sync);
}

/**
 * instantiates a new xmlhttprequest object
 *
 * @return xmlhttprequest object or false
 */
function getXMLRequester( )
{
    var xmlHttp = false;
    // try to create a new instance of the xmlhttprequest object
    try	{
	// Mozilla, Opera und Safari
	if( window.XMLHttpRequest ) {
	    xmlHttp = new XMLHttpRequest();
	}
	// Internet Explorer
	else if( window.ActiveXObject ) {
	    for( var i = 5; i; i-- ) {
		try {
		    // loading of a newer version of msxml dll (msxml3 - msxml5) failed
		    // use fallback solution
		    // old style msxml version independent, deprecated
		    if( i == 2 ) {
			xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
		    }
		    // try to use the latest msxml dll
		    else {
			xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
		    }
		    break;
		}
		catch( excNotLoadable ) {
		    xmlHttp = false;
		}
	    }
	}
    }
    // loading of xmlhttp object failed
    catch( excNotLoadable ) {
	xmlHttp = false;
    } 
    return xmlHttp ;
}



