/*
=======================================================================
Document:         xmlObj.js
Author:           Adamos Fiakkas
Version:          0.4.2
Last Revision:    Jul - 26 - 2010
Purpose:          httpRequest simplified
=======================================================================
*/
function xmlObj()
{
	xmlObj.prototype.xmlHttp = '';
 	xmlObj.prototype.postData = '';
	
	xmlObj.prototype.init = function init()
	{
		try{		
			if (window.XMLHttpRequest != undefined)
				this.xmlHttp = new XMLHttpRequest();
			else 
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(initErr){}
	}
	
	xmlObj.prototype.xmlPost = function xmlPost(url,postDataList,obj,code)
	{
		try{
			var request = '';
		
			for(var i = 0; i < postDataList.length; i++)
			{
				var data = postDataList[i][1];
				
				data = data.replace(/&/g,'-amp-Sign-');
				
				request += postDataList[i][0] + '=' + encodeURI(data);
			
				if(i < postDataList.length)
					request += '&';
			}
		
			request = request.substring(0, (request.length -1));
										
			this.init();
			this.post(url);
			this.setPostData(request);
			this.xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			this.setEvent(obj,code);
			this.sendRequest();
		}
		catch(xmlPostErr){}
	}
	
	xmlObj.prototype.xmlGet = function xmlGet(url,obj,code)
	{
		try{
			this.init();
			this.post(url);
			this.setEvent(obj,code);
			this.sendRequest();
		}
		catch(xmlGetErr){}
	}
	
	xmlObj.prototype.post = function post(url)
	{
		try{
			this.xmlHttp.open('POST', url, true);
		}
		catch(postErr){}
	}
	
	xmlObj.prototype.get = function get(url)
	{
		try{
			this.xmlHttp.open('GET', url, true);
		}
		catch(getErr){}
	}
	
	xmlObj.prototype.sendRequest = function sendRequest()
	{
		try{
			this.xmlHttp.send(this.postData);
		}
		catch(sendRequestErr){}
	}
	
	xmlObj.prototype.setEvent = function setEvent(obj, code)
	{
		try{
			var request = this.xmlHttp;
	
			request.onreadystatechange = function() 
			{
				if (request.readyState == 4 && request.status == 200) 
				{
					var result = request.responseText;
													
					if((typeof obj).toLowerCase() == 'object')
					{
						if(obj.innerHTML != null)
							obj.innerHTML = result;
						else if(obj.data != null)
							obj.data = result;
						else if(obj.length != null)
							obj.push(result);
					}
					else
						obj = request.responseText;
					
					try{
						if(code != '')
							eval(code);
					}
					catch(evalCodeErr){}
					
					//Clear request
					try{
						request.abort();
					}
					catch(abortErr){}
				}
				else if (request.readyState == 4 && request.status != 200)
				{
					try{
						obj.innerHTML = 'Request failed.. Please refresh this page and try again.';
					}
					catch(requestNotOKErr){}
					
					//Clear request
					try{
						request.abort();
					}
					catch(abortErr){}
				}
			}
		}
		catch(setEventErr){} 
	}
	
	xmlObj.prototype.setPostData = function setPostData(pd)
	{
		try{
			this.postData = pd;
		}
		catch(setPostDataErr){}
	}
}

