/***********************************************************\
	nAjax - small AJAX kit
	Author: Vladas Tomkevicius aka Neodan
	Type: tools
	Date: 2008-08-13
\***********************************************************/
/************************* Example *************************\
 <html>
<head>
	<title>nAjax kurimas ir testavimas</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<script language="JavaScript" type="text/javascript" src="najax.js"></script>
</head>
<body>
	<script language="JavaScript" type="text/javascript">
		njax=new nAjax("http://192.168.1.9/~vladas/test/tserver.php");
		njax.setUrlVar("vardas","Neodan");
		njax.onCompletion=function(){
			alert(njax.result);
		}
		njax.sendRequest();
	</script>
</body>
</html>
\***********************************************************/
/************************** Manual **************************\
	obj - nAjax object
	
	obj.msg(type, message);
		send message in to screen;
		vars:
			type - mesage type [error];
			message - message text;
			
	obj.setMethod(method);
		set request method;
		vars:
			method - [ get | post ];
			
	obj.setRequestFile(file);
		set request target file;
		vars:
			file - target file name (http://example.com/file.php);
			
	obj.setCharset(charset);
		set request charset
		vars:
			charset - charset name (utf-8; windows-1257...);
			
	obj.setUrlVar(name, value);
		set request var;
		vars:
			name - var name;
			value - var value;
			
	obj.sendRequest();
		sending request;
		
\***********************************************************/

function nAjax(file){
	//-> nAjax variables
	this.ajaxObj=null;
	this.requestFile=file;
	this.method="POST";
	this.charset="utf-8";
	this.URLString="";
	this.result="";
	
	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onCompletion = function() { };
	
	//-> message output
	this.msg=function(type, message){
		if (type=="error"){
			message="Error: "+message;
		}
		alert(message);
	};
	
	//-> set request file
	this.setRequestFile=function(file){
		this.requestFile=file;
	};
	
	//-> set method
	this.setMethod=function (method){
		method=method.toLowerCase();
		if (method=="get"){
			this.method="GET";
		}else if (method=="post"){
			this.method="POST";
		}else{
			this.msg("error","Wrong method!");
		}
	};
	
	//-> set charset
	this.setCharset=function(charset){
		this.charset=charset;
	};
	
	//-> set url var
	this.setUrlVar=function(name, value){
		if (this.URLString.length < 3){
			this.URLString=name+"="+value;
		}else{
			this.URLString+="&"+name+"="+value;
		}
	};
	
	//-> send ajax request
	this.sendRequest=function(){
		if (this.ajaxObj!=null){
			var self=this;
			// ruosiamas duomenu issiuntimas, readyState=1
			this.ajaxObj.open(this.method, this.requestFile, true);
			// POST hederis 
			this.ajaxObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset="+this.charset);
			//siunciami duomenys i serva
			this.ajaxObj.send(this.URLString);
			//
			this.ajaxObj.onreadystatechange = function (){
				switch (self.ajaxObj.readyState){
					case 1:
						self.onLoading();
					break;
					case 2:
						self.onLoaded();
					break;
					case 3:
						self.onInteractive();
					break;
					case 4:
						if (self.ajaxObj.status == 200){
							self.result=self.ajaxObj.responseText;
							self.onCompletion();
						}else if (self.ajaxObj.status == 400){
						//	self.msg("error","Bad request!");
						}else if (self.ajaxObj.status == 500){
						//	self.msg("error","Server error!");
						}else if (self.ajaxObj.status == 503){
							var time = self.ajaxObj.getResponseHeader('Retry-After')*1000;
						//	self.msg("error","Serveris perkrautas. Uzklausa bus pasiusta po: "+time+" sekundziu");
							setTimeout(self.sendRequest(),time);
						}else{
						//	self.msg("error","Wrong result!");
						}
					break;
				}
			};
		}
	};
	
	//-> create ajax object
	this.createAjaxObj=function(){
		try {
			this.ajaxObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err) {
				try{
					this.ajaxObj = new XMLHttpRequest();
				} catch(err){
					this.ajaxObj=null;
					this.msg("error","Can't create ajax object!");
				}
			}
		}
	};
	
	this.createAjaxObj();
}