/**
 * WebAjax对象
 */
function WebAjax() {
	
	var thisobj = this;
	
	var activeX = ['MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP'];
	
	/**
	 * 创建xmlhttp对象
	 */
	this.createXmlHttp = function() {
		try {
			return new XMLHttpRequest();
		}catch(e) {
			for(var i=0; i<activeX.length; i++) {
				 try {
                    return new ActiveXObject(activeX[i]);                        
                    break;
                } catch(e1) {
                }
			}
		}
		alert('No browser XMLHttpRequest (AJAX) support');
	}
	
	/**
	 * 解析参数
	 * @param {} params
	 */
	this.parseParams = function(paramsobj) {
		var paramsstr = "";
		for(var key in paramsobj) {
			var value = paramsobj[key];
			if(JU.isEmpty(value, true)) continue;
			if(thisobj.isArray(value)) {
				for(var i=0; i<value.length; i++) {
					paramsstr += "&" + key + "=" + value[i];
				}
			}else {
				paramsstr += "&" + key + "=" + value;
			}
		}
		return paramsstr;
	}
	
	/**
	 * 发送请求
	 * @param {} config
	 *  url: 访问路径
	 *  method: POST  or  GET  缺省为:POST
	 *  params: 参数对 {userName:userName,userpassword:userpassword,name:name}
	 *  success: 访问服务器成功所执行的方法
	 *  failure: 访问服务器失败所执行的方法
	 *  async: 是否为异步 异步:true 同步:false 缺省为true
	 */
	this.request = function(config) {
		if(JU.isEmpty(config.method)) config.method = "POST";
		if(JU.isEmpty(config.async)) config.async = false;
		var xmlhttp = this.createXmlHttp();
		//异步处理
		if(config.async) {
			if(JU.isFunction(config.success) || JU.isFunction(config.failure)) {
				xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4) {
						if (xmlhttp.status == 200) {
							if(JU.isFunction(config.success)) config.success(xmlhttp);
						} else {
							if(JU.isFunction(config.failure)) config.failure(xmlhttp);
						}
					}
				};
			}
		}
		xmlhttp.open(config.method, config.url, config.async);
		var sendparams = null;
		if(JU.isObject(config.params)) {
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
			sendparams = this.parseParams(config.params);
			if(sendparams.length > 0) sendparams = sendparams.substring(1);
		}
		xmlhttp.send(sendparams);
		//同步处理
		if(!config.async) {
			if(xmlhttp.status == 200) {
				if(JU.isFunction(config.success)) {
					config.success(xmlhttp);
				}else {
					return xmlhttp;
				}
			}else {
				if(JU.isFunction(config.failure)) {
					config.failure(xmlhttp);
				}else {
					return new Error(xmlhttp.responseText);
				}
			}
		}
	}
	
	
	/**
	 * 采用ajax访问服务器
	 * @param action: 所访问的action-path, 缺省为dynamicAction
	 * @param method: 所调用service中的方法名称
	 * @param params: 符带参数
	 * @param callback: 访问服务器后返回后所执行的方法callback(result)
	 * @param errorcallback: 服务器出现异常时所调用的方法errorcallback(response), 如果找不到此方法则打印:'访问服务器失败! 可能存在网络故障或其他未知原因!'
	 * @param msg: 访问服务器时所显示的加载框, msg=false表示不显示, msg=true表示显示默认框, msg=string表示显示框中的提示信息为此string, 缺省为true
	 */
	this.ajax = function(config) 
	{
		if(JU.isEmpty(config.action)) config.action = '';
		
		var params = JU.isObject(config.params) ? config.params : {};
		var url = config.action;
		var msg = true;
		if(config.msg!=undefined && config.msg!=null) msg = config.msg;
		if(typeof(msg) == "boolean") {
			if(msg) thisobj.showMsg({title:"Please Wait",msg:"正在访问服务器, 请稍候...",option:5});
		}else {
			thisobj.showMsg({title:"Please Wait",msg:msg,option:5});
		}
		params.method = config.method;
		if(JU.isEmpty(this.webAjax)) this.webAjax = new WebAjax();
		this.webAjax.request({
			url: url,
			method: 'POST',
			params: params,
			success: function(response) {
				var result = response.responseText;
				if(msg) thisobj.hideMsg();
				if(JU.isFunction(config.callback)) 
				{
					if(!JU.isEmpty(result) && result.length>1) 
					{
						var l = result.substring(0,1);
						var r = result.substring(result.length-1);
						if(l=="[" && r=="]") 
						{
							config.callback(eval(result));
						}
						else if(l=="{" && r=="}") 
						{
							config.callback(eval("("+result+")"));
						}
						else 
						{
							config.callback(result);
						}
					}
					else 
					{
						config.callback(result);
					}
				}
			},
			failure: function(response, options) {
				if(msg) thisobj.hideMsg();
				thisobj.showMsg({msg:"访问服务器失败! 可能存在网络故障或其他未知原因!"});
			}
		});
	}
	
	/**
	 * 弹出窗口
	 * @param {} config
	 * title: 
	 * msg: 
	 * option: -1=OK  0=YESNO  1=YESNOCANCEL  2=OKCANCEL  3=INPUT  4=TEXTAREA  5=Wait
	 * width: 对话框宽度  <=0 时表示自由缩放
	 * callback : 当点击选项按钮之后所执行的方法
	 */
	this.showMsg = function(config) 
	{
		if(JU.isEmpty(JU.webMsgBox)) JU.webMsgBox = new WebMessageBox();
			JU.webMsgBox.show(config);
	}
	/**
	 * 关闭信息窗口
	 */
	this.hideMsg = function() {
		if(!JU.isEmpty(JU.webMsgBox)) JU.webMsgBox.hide();
	}
}
