/*
Copyright 2007 Piotr Korzeniewski www.mintAjax.pl

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

// mintAjax version 1.0.1
var mint =
{
	Request : function()
	{
		var newRequestObject =
		{
			xmlHttpRequest : null,

			responseText : null,
			responseXML : null,
		
			params : new Array(),

			url : "",
			async : true,
			method : "POST",
			contentType : "text/plain",
			username : "",
			password : "",

			form : null,
			disableForm : true,
			
			status : null,
			statusText : null,

			reqDone : false,
			retryCount : 0,
			retryNum : 0,
			timeout : 500000,

			OnStateChange : function() {},
			OnLoading : function() {},
			OnLoaded : function() {},
			OnInteractive : function() {},
			OnComplete : function() {},
			OnSuccess : function() {},
			OnError : function() {},
			OnAbort : function() {},
			OnRetry : function() {},
			OnTimeout : function() {},

			Send : function(url, target)
			{
				var paramStr = "";

				this.reqDone = false;

				!url ? url = this.url : this.url = url;

				if(window.XMLHttpRequest)
					this.xmlHttpRequest = new XMLHttpRequest();
				else if(window.ActiveXObject)
				{
					try	{
						this.xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
					}
					catch(e) {
						this.xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
					}
				}

				for(var i in this.params)
				{
					if(i != 0)
						paramStr += "&";

					paramStr += this.params[i].name+"="+this.params[i].value;
				}

				if(this.method == "post")
					this.xmlHttpRequest.open(this.method, url, this.async, this.username, this.password);
				else
					this.xmlHttpRequest.open(this.method, url+(!/\?/.test(url) ? "?"+paramStr : "&;"+paramStr), this.async, this.username, this.password);
					
				try {
			
					this.xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
				} catch(e) {}
					
				try {
					this.xmlHttpRequest.setRequestHeader("If-Modified-Since", "Sat, 11 Jan 1977 00:00:00 GMT");
				} catch(e) {}

				var that = this;

				this.xmlHttpRequest.onreadystatechange =
				function()
				{
					that.OnStateChange();

					switch(that.xmlHttpRequest.readyState)
					{
						case 1:
							that.OnLoading();
							break;
						case 2:
							that.OnLoaded();
							break;
						case 3:
							that.OnInteractive();
							break;
						case 4:
							that.OnComplete();

							if(that.xmlHttpRequest.status == 200)
							{
								that.reqDone = true;

								that.responseText = that.xmlHttpRequest.responseText;
								that.responseXML = that.xmlHttpRequest.responseXML;
								
								that.status = that.xmlHttpRequest.status;
								that.statusText = that.xmlHttpRequest.statusText;
								
								if(target)
									$(target).innerHTML = that.responseText;

								

								if(that.form && that.disableForm)
								{
									for(var i = 0; i < that.form.elements.length; i++)
									{
										that.form.elements[i].disabled = false;
									}
								}

								that.OnSuccess();
							}
							else
								that.OnError(that.xmlHttpRequest.status);

							break;
					}
				}
					
				
					this.xmlHttpRequest.send(paramStr);
				
				setTimeout(
						function()
						{
			    			if(!that.reqDone)
							{
								that.xmlHttpRequest.onreadystatechange = function() {};
								that.xmlHttpRequest.abort();
								that.OnTimeout();

								if(that.retryCount < that.retryNum)
								{
									that.retryCount++;
									that.Send();
									that.OnRetry();
								}
								else
								{
									that.retryCount = 0;
									that.OnAbort();
								}
							}
						},
						this.timeout);

				this.params.length = 0;
			},

			SendForm : function(form, url, method)
			{
				this.form = $(form);

				method ? this.method = method : this.method = this.form.method

				if(!url) url = this.form.action;

				var input = this.form.elements;

				for(var i = 0; i < input.length; i++)
				{
					if(this.disableForm)
						input[i].disabled = true;

					switch(input[i].type)
					{
						case "radio":
						case "checkbox":
							if(input[i].checked)
								this.AddParam(input[i].name, input[i].value);
							break;
						case "select-one":
							this.AddParam(input[i].name, input[i].options[input[i].selectedIndex].value);
							break;
						case "select-multiple":
							for(var x = 0; x < input[i].options.length; x++)
							{
								if(input[i].options[x].selected)
									this.AddParam(input[i].name, input[i].options[x].value);
							}
							break;
						default:
							this.AddParam(input[i].name, input[i].value);
					}
				}

				this.Send(url);
			},

			AddParam : function(name, value)
			{
				var newParam =
				{
					name : name,
					value : value
				}

				this.params.push(newParam);
			}
		}

		return newRequestObject;
	}

};
function $(id)
{
	if(typeof(id) == "string")
		return document.getElementById(id);
	else
		return id;
}
