/*
 * jsAJAX 1.0
 * 
 * Programmed by: Ryan Sandigan (ryanzkizen@gmail.com)
 * Date Created: 2007.02.14
 */

function jsAJAX() { this._construct() }
(function() {
 
	jsAJAX.prototype = {
		_target: "",
		_action: "",
		_form: null,
		
		_construct: function() {  },
		
		_createFrame: function(form, events) {
			var sForm = "";
			if (form.name) sForm = "document.forms['" + form.name + "']"; 
			else sForm = "document.forms[0]";
			var frameID = 'frame' + Math.floor(Math.random() * 99999);
			var createDIV = document.createElement('DIV');
			createDIV.innerHTML = '<iframe style="display:none" src="about:blank" id="'+frameID+'" name="'+frameID+'" onload="var ojsAJAX = new jsAJAX(); ojsAJAX._form = '+sForm+'; ojsAJAX._action = \''+this._action+'\'; ojsAJAX._target = \''+this._target+'\'; ojsAJAX.param=' + (typeof(this.param) == 'string' ? '\'' + this.param + '\'' : this.param) + '; ojsAJAX.param2=' + (typeof(this.param2) == 'string' ? '\'' + this.param2 + '\'' : this.param2) + '; ojsAJAX._loadedFrame(\''+frameID+'\')"></iframe>';
			document.body.appendChild(createDIV);
			
			var oframeID = document.getElementById(frameID);
			if (events && typeof(events.onComplete) == 'function') {
				oframeID.onComplete = events.onComplete;
			}
			
			return frameID;
		},
		
 		_loadedFrame: function(id) {

			var oframeID = document.getElementById(id);
			if (oframeID.contentDocument) {
				var oContent = oframeID.contentDocument;
			} else if (oframeID.contentWindow) {
				var oContent = oframeID.contentWindow.document;
			} else {
				var oContent = window.frames[id].document;
			}
			if (oContent.location.href == "about:blank") {
				return;
			}

			if (typeof(oframeID.onComplete) == 'function') {
				this._form.setAttribute('action', this._action);
				this._form.setAttribute('target', '');
				oframeID.onComplete(oContent.body.innerHTML,oContent.forms,this.param,this.param2);
				//this._parseXML(oContent.body.innerHTML)
				//document.getElementById(id).parentNode.removeChild(document.getElementById(id));
				//document.close();
			}
		},
		
		_xmlhttpPost: function(form, events) {
			
			var xmlHttpReq = false;
			var self = this;
			
			if (window.XMLHttpRequest) {
				// Mozilla/Safari
				self.xmlHttpReq = new XMLHttpRequest(); 
			} else if (window.ActiveXObject) { 
				// IE
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
			
			self.xmlHttpReq.open('POST', this.action, true);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			if (events && typeof(events.onComplete) == 'function') {
				self.xmlHttpReq.onreadystatechange = function() {
					if (self.xmlHttpReq.readyState == 4) {
						events.onComplete(self.xmlHttpReq.responseText);
					}
				}
			}
			
			self.xmlHttpReq.send(this._createQueryString(form));
			
			return false;
		},
		
		_createQueryString: function(form) {
			var returnString = "";
			if (form) {
				for(var i=0; i<form.length; i++) {
					if (form[i].name)
					returnString += "&" + form[i].name + "=" + escape(form[i].value);
				}
				
				if (form.action.split('?').length > 1)
					returnString += form.action.split('?')[1].split('&');
			}
			
			return returnString;
		},
		
		_initialize: function(form) {
			if(this.action == "") { this.action = form.action; } else { this._action = form.action; form.setAttribute('action', this.action); }
			return form;
		},
		
		//Properties
		action: "",
		param: null,
		param2: null,
		
		//Procedures
		submit: function(form, events) {
			form = this._initialize(form);
			
			if(form.enctype == 'multipart/form-data') {
				this._form = form;
				this._target = form.target;
				
				form.setAttribute('target', this._createFrame(form, events));
				
				form.submit();

				if (events && typeof(events.onStart) == 'function') {
					return events.onStart();
				} else {
					return true;
				}
			} else {
				if (events && typeof(events.onStart) == 'function') {
					if(events.onStart()) {
						return this._xmlhttpPost(form, events);
					}
				} 
			}
		},
		
		XMLhttp: function(url, querystring, events, param) {
			
			var xmlHttpReq = false;
			var self = this;
			
			if (window.XMLHttpRequest) {
				// Mozilla/Safari
				self.xmlHttpReq = new XMLHttpRequest(); 
			} else if (window.ActiveXObject) { 
				// IE
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
						
			self.xmlHttpReq.open('POST', url, true);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			if (events && typeof(events.onComplete) == 'function') {
				self.xmlHttpReq.onreadystatechange = function() {
					if (self.xmlHttpReq.readyState == 4) {
						events.onComplete(self.xmlHttpReq.responseText, param);
					}
				}
			}
			
			self.xmlHttpReq.send(querystring);
			
			return false;
		}
	}
})();