﻿// JScript File
function YDajax()
{
   
    var _XmlHttp = null;
	var _IsDisposed = false;
	var _RequestHeaders = ({'Count' : 0, 'Items' : {}});
	var _UserName = '', _UserPassword = '';	
	this.Request= function (method, url, data, callBack, disableCache) {	
		if ('string' !== typeof(method))
		{
			throw new Error('参数 method 必须是一个字符串类型的值！');
		}	
		if ('string' !== typeof(url))
		{
			throw new Error('参数 url 必须是一个字符串类型的值！');
		}	
		method = method.toLowerCase();
		if ('get' !== method && 'post' !== method)
		{
			method = 'get';
		}
		_InitXmlHttp();
		
		var async = ('function' === typeof(callBack));
		try
		{
			if (async)
			{
				_XmlHttp.onreadystatechange = function () {
					if (4 == _XmlHttp.readyState)
					{
						callBack(_XmlHttp);
					}
				};
			}
			if ('get' === method && 'string' === typeof(data) && data.length > 0)
			{
				_XmlHttp.open(method, url + ((-1 === url.indexOf('?')) ? '?' + data : '&' + data), async, _UserName, _UserPassword);
			}
			else
			{
				_XmlHttp.open(method, url, async, _UserName, _UserPassword);
			}
			
			if (_RequestHeaders.Count > 0)
			{
				for (var key in _RequestHeaders.Items)
				{
					_XmlHttp.setRequestHeader(key, _RequestHeaders.Items[key]);
				}
			}
			if (true === disableCache)
			{
				_XmlHttp.setRequestHeader('If-Modified-Since', '0');
			}
			
			if ('undefined' === typeof(data) || '' === data)
			{
				data = null;
			}
			
			if ('get' === method)
			{
				_XmlHttp.send(data);
			}
			else
			{
				_XmlHttp.send(data);
			}
		}
		catch ($e)
		{
			_XmlHttp.abort();
			throw $e;
		}
		if (!async)
		{
			return _XmlHttp;
		}
	};
	this.SubmitForm = function (form, callBack, disableCache) {
	
		if (!form || 'undefined' === typeof(form.tagName) || 'form' !== form.tagName.toString().toLowerCase() || 'undefined' === typeof(form.elements))
		{
			throw new Error('参数 form 不是有效的表单元素的引用！');
		}
		
		var enctype = form.enctype.toString().trim().toLowerCase();
		if ('multipart/form-data' === enctype)
		{
			throw new Error('不支持带有文件上传功能的表单！');
		}	
	
		var url = form.action.toString().trim();
		if ('' === url)						
		{
			url = window.location.href;
		}
		else if ('?' === url.charAt(0))	
		{
			url = window.location.href.replace(window.location.search, '') + url;
		}
		var method = form.method.toString().trim().toLowerCase();
		if ('get' !== method && 'post' !== method)
		{
			method = 'get';
		}
		var data = '', seperator = '', ele = null, type = '';
		var reg = /[\x00-\x24]|[\x26-\x2f]|[\x5b-\x60]|[\x7b-\x7f]/ig;
		
		for (var i = 0; i < form.elements.length; i++)
		{
			ele = form.elements[i];
			if (ele.disabled)
			{
				continue;			
			}
			else if ('' === ele.name)
			{
				continue;
			}
			else if ('select' === ele.name && -1 === ele.selectedIndex)
			{
				continue;
			}
			if(ele.type)
			{
			    type = ele.type.toString().toLowerCase();
			    if ('checkbox' === type && !ele.checked)
			    {
				   continue;
			    }
			    else if ('radio' === type && !ele.checked)
		        {
				   continue;
			    }
			}
			data += seperator + ele.name + '=' + escape(ele.value).replace(reg, function (matched){return encodeURIComponent(matched);});
			seperator = '&';
		}		
		
		if (document.cookie)
		{
			this.SetRequestHeader('Cookie', document.cookie);
		}	
		this.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
		return this.Request(method, url, data, callBack, disableCache);
	};
	this.SetRequestHeader = function (name, value) {
	
		if ('string' !== typeof(name))
		{
			throw new Error('参数 name 不是合法的字符串类型的变量！');
		}
		if ('string' !== typeof(value))
		{
			throw new Error('参数 value 不是合法的字符串类型的变量！');
		}
				
		_RequestHeaders.Count = _RequestHeaders.Count + 1;
		_RequestHeaders.Items[name] = value;
	};
	this.SetLoginInfo = function (userName, userPassword) {
		if ('string' !== typeof(userName))
		{
			return false;
		}
		if ('string' !== typeof(userPassword))
		{
			return false;
		}
		
		_UserName = userName;
		_UserPassword = userPassword;
	};
	this.Abort = function () {
		if (null != _XmlHttp)
		{
			_XmlHttp.abort();
		}
	};
	this.Dispose = function () {
		if (null == _XmlHttp)
		{
			return;
		}
		try
		{
		
			_Initialize();
		
			_IsDisposed = true;
		
			_XmlHttp.abort();
		
			_XmlHttp.onreadystatechange = null;
		}
		catch ($e)
		{
		}
	};
	function _CreateXmlHttp() {
		if (window.XMLHttpRequest)
		{
			return new XMLHttpRequest();
		}		
		var msxml = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];
		for (var i = 0; i < msxml.length; i++)
		{
			try
			{
				return new ActiveXObject(msxml[i]);
			}
			catch($e)
			{
			}
		}
		return null;
	}
	function _Initialize()
	{
		
		if (!('trim' in String.prototype))
		{
			String.prototype.trim = function () {
				return this.replace(/(^\s*)|(\s*$)/g, '');
			};
		}
		
		_RequestHeaders = ({'Count' : 0, 'Items' : {}});
	}

	_Initialize();
	
	function _InitXmlHttp()
	{
	
		if (true === _IsDisposed)
		{
			throw new Error('已调用过 Dispose 方法！');
		}
	
		if (null == _XmlHttp)
		{
			_XmlHttp = _CreateXmlHttp();
			if (null == _XmlHttp)
			{
				throw new Error('您的浏览器不支持或未启用 XmlHttp！');
			}
		}		
	
		if (4 == _XmlHttp.readyState)
		{
			_Initialize();
			_XmlHttp.abort();
			return _XmlHttp;
		}
	}
}
