// JavaScript Document

Object.prototype.Ajax = function(){
	function F(){
		/**
			* @desc Default properties
		*/
		this.Container = null;
		this.CallBack = null;
		this.Query = null;
		this.Form = null;
		this.Submitter = null;
		this.Script = null;
		this.Text = null;
		this.ParamsGlue = ',' ; 
		this.KeyValueGlue = ':' ;
		this.Monitor = null ;
		this._Http = 	(window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP" ):false);
		var _Self = this;
		
		/**
	 		* @description Register properties
	 	*/
		this.Register = function(oProperties){
			for(var p in oProperties){
				this[p] = oProperties[p];	
				}
			};
		
		/**
	 		* @description Build this query
	 		* @require function getVal from general.js
	 	*/
		this._BuildQuery = function(){
			if(this.Query){
				this.Query = this.Query.replace(/\:/g, '_colon;');
				this.Query = this.Query.replace(/\,/g, '_comma;');
				this.Query = this.Query.replace(/\=/g, this.KeyValueGlue);
				this.Query = this.Query.replace(/\&/g, this.ParamsGlue);
				}
					
			var aRet = [];
				
			/** 
				* @desc Building AJAX key/variable  pairs
				*  from a custom form. Custom forms rely on custom validation rules.				
				*
			*/
			if(this.Form){
				var oForm = document.forms[this.Form];
				
				for(var i=0; i<oForm.elements.length; i++){
					var elName = oForm.elements[i].name;
					if(elName.indexOf(':') > -1){
						elName = oForm.elements[i].name.split(':')[1];
						}
					var elVal = getVal(oForm.elements[i]) ? getVal(oForm.elements[i]) : '';
					if(typeof(elVal).toLowerCase() == 'string'){
						elVal = elVal.replace(/\:/g, '_colon;');
						elVal = elVal.replace(/\,/g, '_comma;');
						elVal = elVal.replace(/\&/g, '_amps;');
						elVal = elVal.replace(/\=/g, '_equal;');
						}
					aRet.push(elName+this.KeyValueGlue+escape(elVal));	
					}
				if(this.Query !== ''){
					this.Query += this.ParamsGlue;				
					}
				this.Query += aRet.join(this.ParamsGlue);
				}
					
			if(this.Query){
				this.Query = 'axcmd='+this.Command+'&params='+this.Query;
				}		
			if(!this.Query){
				return false;
				}
			// disable the submitter
			if(this.Submitter){
				this.Submitter.disabled = true;
				}			
			};
		
		/**
	 		* @description Response utilities
	 	*/
		this._Respond = function(){
			if(_Self._Http.readyState == 4 && _Self._Http.status == 200){
				// Text returned FROM the PHP script 
				_Self.Text = _Self._Http.responseText;
				if(_Self.Text){ 
					// run CallBack function
					if(_Self.CallBack){
						var prm = [];
						for(var p=0; p<_Self.CallBack.params.length; p++){
							prm.push("'"+escape(_Self.CallBack.params[p])+"'");
							}
						prm.push = ["'"+escape(_Self.Text)+"'"];
						var runFn = _Self.CallBack.fn+'('+prm.join(',')+');';
						setTimeout(runFn,1000);
						}
					// Show return text
					if(_Self.Container){
						if(_Self.Monitor){
							_Self.Monitor.style.display = 'none';
							}
						_Self.Container.innerHTML = _Self.Text;
						// enable the submitter
						if(_Self.Submitter){
							_Self.Submitter.disabled = false;
							}
						}
					}
				}
			};
		
		/**
			* @description Send this request by post
		*/
		this.Post = function(){
			if(!this._Http){
				return false;
				}
			this._BuildQuery();
			if(this.Query){
				if(this.Monitor){
					this.Monitor.style.display = 'inline';
					this.Monitor.innerHTML = 'Loading...';
					}
				this._Http.open('post', this.Script);
				this._Http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
				this._Http.onreadystatechange = this._Respond;
				this._Http.send(this.Query);
				return true;
				}
			return false;
			};
		
		/**
			* @description Send this request by get
			* @param string sScriptURL
			* @access public
		*/
		this.Get = function(){
			if(!this._Http){
				return false;
				}
			this._BuildQuery();
			if(this.Query){
				if(this.Monitor){
					this.Monitor.style.display = 'inline';
					this.Monitor.innerHTML = 'Loading...';
					}
				this._Http.open('get', this.Script+'?'+this.Query); 				
				this._Http.onreadystatechange = this._Respond;
				this._Http.send(null);
				return true;
				}
			return false;
			};
		}
	
	F.prototype = this;	
	return new F();
};