/**
* @author Laurent Dinclaux <laurent.dinclaux@gmail.com>
* @since  Monday Jul 18 2006
*/

/**
 * Submit form theForm using HTTPrequest
 */
function http_submitForm(theForm, requestHandler) {

	xhr_form = theForm;

	data = '';
	for(var i=0; i< theForm.elements.length; i++) {
		data = data + theForm.elements[i].name + "=";
		data = data + theForm.elements[i].value + "&";
	}

	formMethod = theForm.method;
	method = formMethod.toUpperCase();

	http_send_request ( theForm.action, data , method, requestHandler );

}

/**
 * Request a page (filename) and loads response to div (divId)
 */
function http_send_request ( filename, data , method, handler) {

	xhr_object = init_http_request ();

	if(method == "GET" && data != null) {
		filename += "?"+data;
		data      = null;
	}

	xhr_object.open(method, filename, true);

	xhr_object.onreadystatechange = handler;

	if(method == "POST") {
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	}

	xhr_object.send(data);

}


function init_http_request () {

	var xhr_object = null;

	try {
		// Firefox
		return xhr_object = new XMLHttpRequest();
	}
	catch (error) {
		try {
			return xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (error) {

			// XMLHttpRequest non supporté par le navigateur
			alert("Votre navigateur est trop ancien pour ce site. Vous risquez de rencontrer des problème d'utilisation.");
			document.cookie = "ajax_enabled=0";
			return false;

		}
	}
}

document.iframeLoaders = {};

function iframe_formSubmit (form, responseHandler) {

	var objThis = this;

	this.form 				= form;
	this.responseHandler 	= responseHandler;

	// uniqid for generated iframe
	this.uniqueId 			= new Date().getTime();
	
	// add iframe to iframe array for reference
	document.iframeLoaders[this.uniqueId] = this;

	// init response object
	this.response = {};

	/**
	 * Call this one to submit your form in an iframe
	 */
	this.submit = function () {

		this.create_iframe (this.uniqueId);
		this.iframe		 		= $('frame_'+this.uniqueId);

		this.form.target = 'frame_'+this.uniqueId;
		//this.form.setAttribute("target", 'frame_'+this.uniqueId); // in case the other one fails.this.form.submit();

		this.form.submit();
	}
	
	/**
	 * This function will be called using the iframe "onload" event
	 * and will call the responseHandler
	 */
	this.onStateChange = function () {

		// get iframe content
		try {   var doc = this.iframe.contentDocument.document; this.iframe.contentDocument.document.close(); }	// For NS6

		catch (e){
			
			try{ var doc = this.iframe.contentWindow.document; this.iframe.contentWindow.document.close(); } // For IE5.5 and IE6
			
			catch (e){
				
				try { var doc = this.iframe.document; this.iframe.document.body.close(); } // for IE5
				catch (e) {
					try	{ var doc = window.frames['frame_'+this.uniqueId].document; } // for really nasty browsers
					catch (e) { } // forget it.
				}
				
			}
		}


		// response text allways defined
		this.response.responseText = doc;

		
		// is response an xmldoc?
		if ( !(navigator.userAgent.indexOf('MSIE') > 0 && navigator.userAgent.indexOf('Opera') == -1) ) { // all browsers

			
			if (doc instanceof XMLDocument) {
				isXML = true;
				
				// store xml object
				this.response.responseXML = doc;
			} 
			else {
				isXML = false;
				this.response.responseXML = null;
			}
		}
		else { // Internet explorer
			
			var xmlDocument = doc.XMLDocument;
			
			if (xmlDocument != null) {
				isXML = true;
				
				// store xml object
				this.response.responseXML = doc.XMLDocument;
			} 
			else {
				isXML = false;
				this.response.responseXML = null;
			}
			
		}

		
		window[this.responseHandler](this.response);
		
	}

	this.create_iframe = function (id) {

		var border = '1px';
		var height = '100px';
		var width = '200px';

		var divElm = document.createElement('DIV');
		divElm.setAttribute("id", "div_"+this.uniqueId);
		divElm.style.visibilty = "hidden";
		divElm.style.display = 'none';

		if (document.createElement && !(navigator.userAgent.indexOf('MSIE') > 0 && navigator.userAgent.indexOf('Opera') == -1) ) {

			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id', 'frame_' + id);
			tempIFrame.setAttribute('name', 'frame_' + id);
			tempIFrame.style.border	= border;
			tempIFrame.style.width	= width;
			tempIFrame.style.height = height;
			tempIFrame.addEventListener("load", function (){ objThis.onStateChange()} , false);

			divElm.appendChild(tempIFrame);

		}
		else {

			// switch to the crappy solution for IE and others
			iframeHTML='\<iframe id="frame_' + id + '" ';
			iframeHTML+= 'name="frame_' + id + '" style="';
			iframeHTML+='border:' + border + ';';
			iframeHTML+='width:' + width + 'px;';
			iframeHTML+='height:' + height + 'px;" ';
			iframeHTML+='onload=\"document.iframeLoaders['+this.uniqueId+'].onStateChange()"><\/iframe>';
			divElm.innerHTML = iframeHTML;

		}
		document.getElementsByTagName("body").item(0).appendChild(divElm);
	}
}


