var is_mac = false;
var is_ie = true;
var is_ie4 = false;

function Taoxi_PHP_Emulator()
{
}

// 仿效PHP的 htmlspecialchars 函数
Taoxi_PHP_Emulator.prototype.htmlspecialchars = function(str)
{
	//var f = new Array(/&(?!#[0-9]+;)/g, /</g, />/g, /"/g);
	var f = new Array(
		(is_mac && is_ie ? new RegExp('&', 'g') : new RegExp('&(?!#[0-9]+;)', 'g')),
		new RegExp('<', 'g'),
		new RegExp('>', 'g'),
		new RegExp('"', 'g')
	);
	var r = new Array(
		'&amp;',
		'&lt;',
		'&gt;',
		'&quot;'
	);

	for (var i = 0; i < f.length; i++)
	{
		str = str.replace(f[i], r[i]);
	}

	return str;
}

Taoxi_PHP_Emulator.prototype.unhtmlspecialchars = function(str)
{
	f = new Array(/&lt;/g, /&gt;/g, /&quot;/g, /&amp;/g);
	r = new Array('<', '>', '"', '&');

	for (var i in f)
	{
		str = str.replace(f[i], r[i]);
	}

	return str;
}

// 仿效PHP函数
// 转义正则表达式字符
// 以 str 为参数并给其中每个属于正则表达式语法的字符前面加上一个反斜线。如果你需要以动态生成的字符串作为模式去匹配则可以用此函数转义其中可能包含的特殊字符
Taoxi_PHP_Emulator.prototype.preg_quote = function(str)
{
	// replace + { } ( ) [ ] | / ? ^ $ \ . = ! < > : * with backslash+character
	return str.replace(/(\+|\{|\}|\(|\)|\[|\]|\||\/|\?|\^|\$|\\|\.|\=|\!|\<|\>|\:|\*)/g, "\\$1");
}

// 查找子串位置
Taoxi_PHP_Emulator.prototype.stripos = function(haystack, needle, offset)
{
	if (typeof offset == 'undefined')
	{
		offset = 0;
	}

	index = haystack.toLowerCase().indexOf(needle.toLowerCase(), offset);

	return (index == -1 ? false : index);
}

// 去除字符串左边空格
Taoxi_PHP_Emulator.prototype.ltrim = function(str)
{
	return str.replace(/^\s+/g, '');
}
Taoxi_PHP_Emulator.prototype.rtrim = function(str)
{
	return str.replace(/(\s+)$/g, '');
}
Taoxi_PHP_Emulator.prototype.trim = function(str)
{
	return this.ltrim(this.rtrim(str));
}

// 大数组中查找相应值的索引位置
// string ineedle 值
// array haystack 数组
// bool caseinsensitive 大小不区分
Taoxi_PHP_Emulator.prototype.in_array = function(ineedle, haystack, caseinsensitive)
{
	var needle = new String(ineedle);

	if (caseinsensitive)
	{
		needle = needle.toLowerCase();
		for (var i in haystack)
		{
			if (haystack[i].toLowerCase() == needle)
			{
				return i;
			}
		}
	}
	else
	{
		for (var i in haystack)
		{
			if (haystack[i] == needle)
			{
				return i;
			}
		}
	}
	return -1;
}

// 仿效PHP函数,自动换行函数
Taoxi_PHP_Emulator.prototype.str_pad = function(text, length, padstring)
{
	text = new String(text);
	padstring = new String(padstring);

	if (text.length < length)
	{
		padtext = new String(padstring);

		while (padtext.length < (length - text.length))
		{
			padtext += padstring;
		}

		text = padtext.substr(0, (length - text.length)) + text;
	}

	return text;
}

// 仿效 PHP urlencode 函数，并不完成一样
// 返回字符串，此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号（%）后跟两位十六进制数，空格则编码为加号（+）。此编码与 WWW 表单 POST 数据的编码方式是一样的，同时与 application/x-www-form-urlencoded 的媒体类型编码方式一样。由于历史原因，此编码在将空格编码为加号（+）方面与 RFC1738 编码（参见 rawurlencode()）不同
Taoxi_PHP_Emulator.prototype.urlencode = function(text)
{
	text = escape(text.toString()).replace(/\+/g, "%2B");

	// this escapes 128 - 255, as JS uses the unicode code points for them.
	// This causes problems with submitting text via AJAX with the UTF-8 charset.
	var matches = text.match(/(%([0-9A-F]{2}))/gi);
	if (matches)
	{
		for (var matchid = 0; matchid < matches.length; matchid++)
		{
			var code = matches[matchid].substring(1,3);
			if (parseInt(code, 16) >= 128)
			{
				text = text.replace(matches[matchid], '%u00' + code);
			}
		}
	}

	// %25 gets translated to % by PHP, so if you have %25u1234,
	// we see it as %u1234 and it gets translated. So make it %u0025u1234,
	// which will print as %u1234!
	text = text.replace('%25', '%u0025');

	return text;
}

// 首字符大写
Taoxi_PHP_Emulator.prototype.ucfirst = function(str, cutoff)
{
	if (typeof cutoff != 'undefined')
	{
		var cutpos = str.indexOf(cutoff);
		if (cutpos > 0)
		{
			str = str.substr(0, cutpos);
		}
	}

	str = str.split(' ');
	for (var i = 0; i < str.length; i++)
	{
		str[i] = str[i].substr(0, 1).toUpperCase() + str[i].substr(1);
	}
	return str.join(' ');
}

// 实例 Taoxi_PHP_Emulator
var PHP = new Taoxi_PHP_Emulator();


/**
* XML Sender Class
*
* @param	boolean	Should connections be asyncronous?
*/
function Taoxi_AJAX_Handler(async)
{
	/**
	* Should connections be asynchronous?
	*
	* @var	boolean
	*/
	this.async = async ? true : false;
}

// =============================================================================
// Taoxi_AJAX_Handler methods

/**
* Initializes the XML handler
*
* @return	boolean	True if handler created OK
*/
Taoxi_AJAX_Handler.prototype.init = function()
{
	if (typeof vb_disable_ajax != 'undefined' && vb_disable_ajax == 2)
	{
		// disable all ajax features
		return false;
	}

	this.handler = false;
	// Mozilla, Safari,...
	if (window.XMLHttpRequest)
	{
	 	this.handler = new XMLHttpRequest();
	 	if (this.handler.overrideMimeType) 
		{
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			this.handler.overrideMimeType('text/html');
	 	}
	}
	else if (window.ActiveXObject) 
	{ // IE
	 	try
		{
			this.handler = new ActiveXObject("Msxml2.XMLHTTP");
	 	}
		catch (e) 
		{
			try {
		   		this.handler = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
	 	}
	}

	if(!this.handler)
		return false;
	else
		return true;
}

/**
* Detects if the browser is fully compatible
*
* @return	boolean
*/
Taoxi_AJAX_Handler.prototype.is_compatible = function()
{
	if (typeof vb_disable_ajax != 'undefined' && vb_disable_ajax == 2)
	{
		// disable all ajax features
		return false;
	}

	if (is_ie && !is_ie4) { return true; }
	else if (typeof XMLHttpRequest != 'undefined')
	{
		try { return XMLHttpRequest.prototype.setRequestHeader ? true : false; }
		catch(e)
		{
			try { var tester = new XMLHttpRequest(); return tester.setRequestHeader ? true : false; }
			catch(e) { return false; }
		}
	}
	else { return false; }
}

/**
* Checks if the system is ready
*
* @return	boolean	False if ready
*/
Taoxi_AJAX_Handler.prototype.not_ready = function()
{
	return (this.handler.readyState && (this.handler.readyState < 4));
}

/**
* OnReadyStateChange event handler
*
* @param	function
*/
Taoxi_AJAX_Handler.prototype.onreadystatechange = function(event)
{
	if (!this.handler)
	{
		if  (!this.init())
		{
			return false;
		}
	}
	if (typeof event == 'function')
	{
		this.handler.onreadystatechange = event;
	}
	else
	{
		alert('XML Sender OnReadyState event is not a function');
	}
	return false;
}

/**
* Sends data
*
* @param	string	Destination URL
* @param	string	Request Data
*
* @return	mixed	Return message
*/
Taoxi_AJAX_Handler.prototype.send = function(desturl, datastream)
{
	if (!this.handler)
	{
		if (!this.init())
		{
			return false;
		}
	}
	if (!this.not_ready())
	{
		this.handler.open('POST', desturl, this.async);
		this.handler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      	this.handler.setRequestHeader("Content-length", datastream.length);
      	this.handler.setRequestHeader("Connection", "close");		
		this.handler.send(datastream);

		if (!this.async && this.handler.readyState == 4 && this.handler.status == 200)
		{
			return true;
		}
	}
	return false;
}

Taoxi_AJAX_Handler.prototype.SubmitUsePost = function(desturl, datastream, targetdiv)
{
	if (!this.handler)
	{
		if (!this.init())
		{
			return false;
		}
	}
	if (!this.not_ready())
	{
		this.handler.open('POST', desturl, this.async);
		this.handler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.handler.send(datastream);

		if (!this.async && this.handler.readyState == 4 && this.handler.status == 200)
		{
			if(targetdiv)
			{
				targetdiv.innerHTML=this.handler.responseText;
				targetdiv.style.display="";
			}
			return true;
		}
	}
	return false;
}

Taoxi_AJAX_Handler.prototype.AsynSubmitUseGet = function(desturl, datastream, targetdiv,statechangefunc)
{
	if (!this.handler)
	{
		if (!this.init())
		{
			return false;
		}
	}
	
	this.targetdiv = targetdiv;

	if (!this.not_ready())
	{
		desturl += "?"+datastream;

		this.handler.open('GET', desturl, true);
		this.handler.send(null);
		this.handler.onreadystatechange = statechangefunc;
	}
	return false;
}

Taoxi_AJAX_Handler.prototype.SubmitUseGet = function(desturl, datastream, targetdiv)
{
	if (!this.handler)
	{
		if (!this.init())
		{
			return false;
		}
	}
	if (!this.not_ready())
	{
		desturl += "?"+datastream;

		//this.handler.open('GET', desturl, this.async);
		this.handler.open('GET', desturl, this.async);
		this.handler.send(null);
		
		if (!this.async && this.handler.readyState == 4 && this.handler.status == 200)
		{
			if(targetdiv != "")
			{
				targetdiv.innerHTML=this.handler.responseText;
				targetdiv.style.display="";
				return true;
			}
			else
			{
				return this.handler.responseText;
			}
		}
	}
	return false;
}

/**
* Fetches the contents of an XML node
*
* @param	object	XML node
*
* @return	string	XML node contents
*/
Taoxi_AJAX_Handler.prototype.fetch_data = function(xml_node)
{
	if (xml_node && xml_node.firstChild && xml_node.firstChild.nodeValue)
	{
		return PHP.unescape_cdata(xml_node.firstChild.nodeValue);
	}
	else
	{
		return '';
	}
}

// we can check this variable to see if browser is AJAX compatible
var AJAX_Compatible = Taoxi_AJAX_Handler.prototype.is_compatible();
var AJAX = null;
if(AJAX_Compatible)
	AJAX = new Taoxi_AJAX_Handler();