// JavaScript Document
/** 
* Class for sending POST Requests to a Server 
* usable for IE and Firefox
*/
function MF_xmlRequest(myurl) {
	this.url=myurl; /* The URL to Use for the Request */
	this.response=''; /* The Response of the Request */	
	this.postvars=new Array(); /* The PostVars as an Array */
	this.filevars=new Array(); /* The PostVars as an Array */
	this.request;
	this.contenttype = 'application/x-www-form-urlencoded;charset=iso-8859-1';
}

/** 
* Do the request  
*
* @param string callback the callback-function 
*
* @return void
*/
MF_xmlRequest.prototype.doRequest=function(callback) {
	this.getRequestHandler();
	if(!this.request) return this.doRequestIframe(callback);
	this.callback=callback;
//	this.request.open("POST", this.url, false);
	
	// Now the Handler for async Mode
	var self=this;
	this.request.onreadystatechange=function() {
		if(self.request.readyState==4) {
			self.process(self.callback);
		}
	}

        this.request.open("POST", this.url, true);	
	var poststring=this.getPostRequest();
	this.request.setRequestHeader('Content-type',this.contenttype);
	this.request.setRequestHeader('Content-length', poststring.length);
	this.request.setRequestHeader('Connection', 'close');
	this.request.send(poststring);

	return true;
}

/** 
* Do the iframe request  
*
* @param string callback the callback-function 
*
* @return void
*/
MF_xmlRequest.prototype.doRequestIframe=function(callback) {

	// Get the Div Element of the IFrame
	if(document.getElementById('MF_xmlhttpdiv')) {
		var iframediv = document.getElementById('MF_xmlhttpdiv');
	} else {
		var iframediv = document.createElement('div');
		iframediv.id = 'MF_xmlhttpdiv';
		var mydiv = findDivContainer();
		if(!mydiv) return false;
		mydiv.appendChild(iframediv);
	}
	iframediv.innerHTML='';
	// Generate the IFrame
	if(document.getElementById('MF_xmlhttpiframe')) {



		var iframe=document.getElementById('MF_xmlhttpiframe');
		if(document.frames && document.frames['MF_xmlhttpiframe']) {
			form = document.frames['MF_xmlhttpiframe'].document.createElement('form');
			form.id = 'MF_xmlhttpiframe_form';
			document.frames['MF_xmlhttpiframe'].document.appendChild(form);
		} else {
			form=document.getElementById('MF_xmlhttpiframe_form');
		}
	} else {
		iframe = document.createElement('iframe');
		iframe.id = 'MF_xmlhttpiframe';
		iframe.name = 'MF_xmlhttpiframe';
		iframediv.appendChild(iframe);
		if(document.frames && document.frames['MF_xmlhttpiframe']) {
			form = document.frames['MF_xmlhttpiframe'].document.createElement('form');
			form.id = 'MF_xmlhttpiframe_form';
			document.frames['MF_xmlhttpiframe'].document.appendChild(form);
		} else {
			form = document.createElement('form');
			form.id = 'MF_xmlhttpiframe_form';
			form.target='MF_xmlhttpiframe';
		}
		if(document.frames && document.frames['MF_xmlhttpiframe']) {
			document.frames['MF_xmlhttpiframe'].document.appendChild(form);
		} else {
			iframe.appendChild(form);
		}
	}

	form.method='POST';
	form.action=this.url;
	for(var i=0;i<this.postvars.length;i++) {
		this.addinput(form, this.postvars[i], this.postvars[i+1]);
		i++;
	}

	for(var i=0;i<this.filevars.length;i++) {
		this.addfile(form, this.postvars[i]);
	}
	
	
	if(document.frames && document.frames['MF_xmlhttpiframe']) {
		var mycall = callback;
		iframe.onreadystatechange=function() {
			if(document.frames && document.frames['MF_xmlhttpiframe'].document.readyState=='complete') {
				var response = document.frames['MF_xmlhttpiframe'].document.body.innerHTML;
				// Set a blank if the response is empty
				if(!response || response==null) response=' ';
				eval(mycall)(response);
			}
		}
	}
	
	if(document.frames && document.frames['MF_xmlhttpiframe']) {
		document.frames['MF_xmlhttpiframe'].document.getElementById('MF_xmlhttpiframe_form').submit();
	} else {
		form.submit();
	}
	return true;
}

/**
* Generates a div for the XMLHTTP Iframe substitution. If the default div is not found
* it searches in document.all for a matching Element
* @return object the div element for the xmlhttpiframe
*/
function findDivContainer() {
	if(document.getElementById('MF_xmlhttp')) {
		var mydiv = document.getElementById('MF_xmlhttp');
		return mydiv;
	} else {
		var mydiv = document.createElement('div');
		mydiv.id='MF_xmlhttp';
		mydiv.style.border='2px';
		mydiv.style.visibility='hidden';
		mydiv.style.display = 'none';
		for(var i=0;i<document.all.length;i++) {
			if(document.all[i].tagName=='SPAN' || document.all[i].tagName=='DIIV' || document.all[i].tagName=='TD') {
				document.all[i].appendChild(mydiv);
				return mydiv;
			}
		}
	}
	return false;
}

/** 
* @desc Add a input field to the form
* @param object form the form object
* @param string name the name of the input field
* @param string value the value of the input field
*
* @return void
*/
MF_xmlRequest.prototype.addinput=function(form, myname, myvalue) {
	if(document.frames && document.frames['MF_xmlhttpiframe']) {
		var input=document.frames['MF_xmlhttpiframe'].document.createElement('input');
	} else {
		var input=document.createElement('input');
	}
	input.name=myname;
	input.value=encodeURIComponent(myvalue);
	form.appendChild(input);
}
	
/** 
* @desc process the result
* @return void
*/
MF_xmlRequest.prototype.process=function(callback) {
	var response=this.request.responseText;
	// Set a blank if the response is empty
	if(!response || response==null) response=' ';
	eval(callback)(response);
}


/** 
* @desc Browser Check and getting the request Handler
* @return void 
*/
MF_xmlRequest.prototype.getRequestHandler=function() {
	if (window.XMLHttpRequest) {
       	this.request = new XMLHttpRequest();
		if(this.request && this.request.readyState < 4) {
			this.request.abort();
		}
   	} else {	
		try {
        	this.request = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		this.request = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          	
        	}
      	}
	}
}

/** 
* @desc adds Variables to the PostRequest 
* @param name the name of the variable
* @param the value of the variable
* @return void
*/
MF_xmlRequest.prototype.addPostVar=function(name,value) {
	this.postvars.push(name);
	this.postvars.push(value);
}

/** 
* @desc Generates the POST String from the postVars Array
* @return string the PostString
*/	
MF_xmlRequest.prototype.getPostRequest=function() {
	var poststring='';
	for (var i=0;i<this.postvars.length;i++) {
		if(poststring!='') poststring=poststring+'&';
		poststring=poststring+this.postvars[i]+'='+encodeURIComponent(this.postvars[i+1]);
		i++;
	}
	return poststring;
}

/**
* Get the vars from a Form
*
* @param string the name of the form
*
* @return void
*/
MF_xmlRequest.prototype.getFormData=function(name) {
	for(var i=0; i < document.forms.length; i++) {
		if(document.forms[i].name == name) {
			for(var f=0; f < document.forms[i].elements.length; f++) {
				var myfield = document.forms[i].elements[f];
				switch(myfield.type) {
					case 'select-multiple':
						for(var k=0;k<myfield.options.length;k++) {
							if(myfield.options[k].selected==true) {
								this.addPostVar(myfield.name, myfield.options[k].value);
							}
						}
					break;
					case 'select-one':
						for(var k=0;k<myfield.options.length;k++) {
							if(myfield.options[k].selected==true) {
								this.addPostVar(myfield.name, myfield.options[k].value);
							}
						}
					break;
					case 'radio':
						if(myfield.checked) this.addPostVar(myfield.name, myfield.value);
					break;
					case 'checkbox':
						if(myfield.checked) this.addPostVar(myfield.name, myfield.value);
					break;
					default:
						this.addPostVar(myfield.name, myfield.value);
				}
			}
		}
	}
}



