//Compat fuction for IE.

if (!window.XMLHttpRequest) {
	window.XMLHttpRequest = function() {

		var xmlVersions = [ "MSXML2.XMLHttp.5.0",
			"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
			"MSXML2.XMLHttp","Microsoft.XMLHttp"
		];
	
		for (var i = 0; i < xmlVersions.length; i++) {
			try {
				var x = new ActiveXObject(xmlVersions[i]);
				return x;
			} catch (e) {}
		}
	};
	
}

(function() {
	
	var totalPendingRequests = 0;
	var handlers = {
		activityStart : LSF.emptyFunction,
		activityStop : LSF.emptyFunction,
		requestStart : LSF.emptyFunction,
		requestStop : LSF.emptyFunction
	}
	
	LSF.Net.Ajax = {
		setHandler : function (handler, callback) {
			callback = typeof callback == 'function' ? callback : LSF.emptyFunction;
			if (handlers[handler]) {
				var oldHandler = handlers[handler];
				handlers[handler] = callback;
				return oldHandler;
			}
		},
		request : function (url, data, callback, method) {
			var request = new LSF.Net.Ajax.Request (url, data, callback, method);
			return request.doRequest();
		},
	
		Request : new Class ({
			__construct : function (url, data, callback, options) {
				options = options || {};
				this.method = options.method || 'POST';
				this.timeout = options.timeout || 10;
				this.requestObject = new XMLHttpRequest();
				
				if (typeof callback == 'function') {
					this.successCallback = callback;
					this.errorCallback = false;
					this.timeoutCallback = false;
				} else if (typeof callback != 'undefined') {
					this.successCallback = callback.success || false;
					this.errorCallback = callback.error || false;
					this.timeoutCallback = callback.timeout || false
				}
				this.data = data;
				this.url = url;
			},
			
			tryCallback : function (callback) {
				if (callback === false) {
					throw new Error (arguments[1]);
				} else {
					callback (arguments[1], arguments[2]);
				}
			},
			
			removeRequestFromQueue : function() {
	    		totalPendingRequests--;
	    		handlers.requestStop();
	    		if (totalPendingRequests == 0) {
	    			handlers.activityStop();
	    		}// only if "OK"
			},
			
			onReadyStateChange : function (event, isTimeout) {
			    // only if req shows "loaded"
		    	if (this.requestObject.readyState == 4) {
		        	
		    		this.removeRequestFromQueue();
		    		
		        	if (isTimeout) {
		        		this.tryCallback (this.timeoutCallback, 'Server timeout');
		        	} else if (this.requestObject.status == 200) {
		        		this.tryCallback (this.successCallback, this.requestObject.responseText, this.requestObject.responseXML);
		        	} else {
		        		this.tryCallback (this.errorCallback, "Server returned status " + this.requestObject.status);
		        	}
		        } else {
		            /*throw new Error ("There was a problem retrieving the XML data:\n" + requestObject.statusText);*/
		        }
			},
			
			doRequest : function() {
				//AjaxLoader.start();
				
				var async = typeof this.successCallback == 'function';
	
				this.requestObject.open (this.method, this.url, async);
				this.requestObject.setRequestHeader ("Content-Language", "en");
				this.requestObject.setRequestHeader ("Content-Charset", "utf-8");
				this.requestObject.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
				this.requestObject.setRequestHeader("Accept-Charset", "utf-8"); 
				var thisref = this;

				if (async) {
					this.requestObject.onreadystatechange = function (e) {
						thisref.onReadyStateChange.call (thisref, e);
					};
				}
				this.requestObject.send (this.data);
				
	    		if (totalPendingRequests++ == 0) {
	    			handlers.activityStart();
	    		}

	       		handlers.requestStart();
				
	    		if (!async) {
		    		this.removeRequestFromQueue();
					return this.requestObject.responseText;
	    		}
	    		
			}
			
		})
		
	};

})();

var Iframe = {
    iframe : undefined,
    submit : function (form, callback) {
        var ifr = new Iframe.Fallback (form, callback);
        ifr.submit();
    },
	Fallback : new Class ({
	    __construct : function (form, callback) {
	        this.form = form;
	        this.callback = callback;
	        if (Iframe.iframe === undefined) {
	        	Iframe.iframe = document.getElementById ('iframe');
	        }
	    },
	    submit : function() {
	        this.form.submit();
	        this.interval = setInterval (Delegate (this, function() {
	            if (Iframe.iframe.contentWindow && Iframe.iframe.contentWindow.isLoaded) {
	                Iframe.iframe.contentWindow.isLoaded = false;
	                clearInterval (this.interval);
	                this.callback (Iframe.iframe.contentWindow.randomId, Iframe.iframe.contentWindow.filesCount);
	                //delete thisref.interval;              
	            }
	        }), 100);
	    }
	})
};
/*

var Iframe = {

    iframe : undefined,

    server : 'iframe.php',

    submit : function (form, callback) {
        var ifr = new Iframe.Fallback (Iframe.server, form, callback);
        ifr.submit();
    }
    
};

Iframe.Fallback = new Class ({

    __construct : function (server, form, callback) {
        this.server = server;
        this.form = form;
        this.callback = callback;
        if (Iframe.iframe === undefined) {
        	Iframe.iframe = document.getElementById ('iframe');
            //Iframe.iframe = document.createElement ('IFRAME');
            //Iframe.iframe.className = 'upload'; 
            //Iframe.iframe.src = "javascript:;";
            //Iframe.iframe.id = Iframe.iframe.name = 'iframe';
            //document.body.appendChild (Iframe.iframe);
        }
    },
    
    submit : function() {
        this.oldAction = this.form.action;
        this.oldTarget = this.form.target;
        this.form.action = this.server;
        this.form.target = Iframe.iframe.id;
        this.form.submit();
        var thisref = this;
        this.interval = setInterval (function() {
            if (Iframe.iframe.contentWindow && Iframe.iframe.contentWindow.isLoaded) {
                Iframe.iframe.contentWindow.isLoaded = false;
                clearInterval (thisref.interval);
                thisref.form.action = thisref.oldAction;
                thisref.form.target = thisref.oldTarget;
                
                thisref.callback (Iframe.iframe.contentWindow.randomId);
                //delete thisref.interval;              
            }
        }, 100);
    }

});
*/


LSF.quickAccess ('ajax', LSF.Net.Ajax.request);