var alnsAjaxUtils = new Object();

alnsAjaxUtils.chkAjaBrowser = function ()
{
	var a,ua = navigator.userAgent;
	
	this.bw= {
		safari    : ((a=ua.split('AppleWebKit/')[1])?a.split('.')[0]:0)>=124 ,
		konqueror : ((a=ua.split('Konqueror/')[1])?a.split(';')[0]:0)>=3.3 ,
		mozes     : ((a=ua.split('Gecko/')[1])?a.split(' ')[0]:0) >= 20011128 ,
		opera     : (!!window.opera) && ((typeof XMLHttpRequest)=='function') ,
		msie      : (!!window.ActiveXObject)?(!!alnsAjaxUtils.createHttpRequest()):false 
	}
	return (this.bw.safari||this.bw.konqueror||this.bw.mozes||this.bw.opera||this.bw.msie);
}

alnsAjaxUtils.createHttpRequest = function ()
{
	if(window.XMLHttpRequest){
		 //Win Mac Linux m1,f1,o8 Mac s1 Linux k3 & Win e7
		return new XMLHttpRequest() ;
	} else if(window.ActiveXObject){
		 //Win e4,e5,e6
		try {
			return new ActiveXObject('Msxml2.XMLHTTP') ;
		} catch (e) {
			try {
				return new ActiveXObject('Microsoft.XMLHTTP') ;
			} catch (e2) {
				return null ;
 			}
 		}
	} else  {
		return null ;
	}
}


alnsAjaxUtils.sendRequest = function (callback,data,method,url,async,sload,user,password)
{
	// create XMLHttpRequest object
	var oj = alnsAjaxUtils.createHttpRequest();
	if( oj == null ) return null;
	
	// force load or not
	var sload = (!!alnsAjaxUtils.sendRequest.arguments[5])?sload:false;
	if(sload || method.toUpperCase() == 'GET')url += '?';
	if(sload)url=url+'t='+(new Date()).getTime();
	
	//check browser
	var bwoj = new alnsAjaxUtils.chkAjaBrowser();
	var opera	  = bwoj.bw.opera;
	var safari	  = bwoj.bw.safari;
	var konqueror = bwoj.bw.konqueror;
	var mozes	  = bwoj.bw.mozes ;
			
	// sprit callback
	//{onload:xxxx,onbeforsetheader:xxx}
	if(typeof callback=='object'){
		var callback_onload = callback.onload;
		var callback_onbeforsetheader = callback.onbeforsetheader;
	} else {
		var callback_onload = callback;
		var callback_onbeforsetheader = null;
	}

	//accepting operation
	//opera has bug on onreadystatechange, so onload is safe.
	//Moz,FireFox oj.readyState==3 is onload 
	//Win ie onload does not work
	//Konqueror : onload is unstable
	//reference http://jsgt.org/ajax/ref/test/response/responsetext/try1.php
	if(opera || safari || mozes){
		oj.onload = function () { callback_onload(oj); }
	} else {
	
		oj.onreadystatechange =function () 
		{
			if ( oj.readyState == 4 ){
				//alert(oj.status+'--'+oj.getAllResponseHeaders());
				callback_onload(oj);
			}
		}
	}

	//URL encode
	data = uriEncode(data,url);
	if(method.toUpperCase() == 'GET') {
		url += data
	}
	
	//open method
	oj.open(method,url,async,user,password);

	
	if(!!callback_onbeforsetheader)callback_onbeforsetheader(oj);

	//default header is application/x-www-form-urlencoded
	setEncHeader(oj);
	
	
	//debug 
	//alert('////jslb_ajaxxx.js//// \n data:'+data+' \n method:'+method+' \n url:'+url+' \n async:'+async);
	
	//send method
	oj.send(data);

	//URI encode header set
	function setEncHeader(oj){
		var contentTypeUrlenc = 'application/x-www-form-urlencoded; charset=UTF-8';
		if(!window.opera){
			oj.setRequestHeader('Content-Type',contentTypeUrlenc);
		} else {
			if((typeof oj.setRequestHeader) == 'function')
				oj.setRequestHeader('Content-Type',contentTypeUrlenc);
		}	
		return oj
	}

	//URL encode
	function uriEncode(data,url){
		var encdata =(url.indexOf('?')==-1)?'?dmy':'';
		if(typeof data=='object'){
			for(var i in data)
				encdata+='&'+encodeURIComponent(i)+'='+encodeURIComponent(data[i]);
		} else if(typeof data=='string'){
			if(data=='')return '';
			//aplit with &,= , and encode them
			var encdata = '';
			var datas = data.split('&');
			for(var i=1;i<datas.length;i++)
			{
				var dataq = datas[i].split('=');
				encdata += '&'+encodeURIComponent(dataq[0])+'='+encodeURIComponent(dataq[1]);
			}
		} 
		return encdata;
	}



	return oj
}

