/*
Name: Ajax
Version: 1.0.1
Author:	shine@美林通信息技术有限公司
E-mail: shine@126.com
Website: http://www.meilintong.com/
Copyright (c) hisisoft All Rights Reserved
*/

/**
* Ajax类
* @class		 Ajax
* @param		 url	  	提交地址
* @param		 onload		提交完成回调函数
* @param		 onerror	发生错误执行函数
* @param		 xml		提交的数据
* @param		 force		是否强制更新 默认为强制更新
*/
function Ajax(url,xml,onload,onerror,force){
	this.url=url;
	this.onload=(onload)?onload:this.defaultLoad;
	this.onerror=(onerror)? onerror:this.defaultError;
	this.xml =(xml)? xml:'';
	this.force= (force==undefined || !force) ? true:false;
	this.xmlHttp = this.createXMLHttpRequest();
	var obj=this;
	if(this.xmlHttp)this.xmlHttp.onreadystatechange = function (){Ajax.onReadyState.call(obj)};
}

Ajax.prototype.createXMLHttpRequest = function()
{
	var xmlHttp=null;
	if(window.ActiveXObject)
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(window.XMLHttpRequest)
	{
		xmlHttp = new XMLHttpRequest();
	}
	return xmlHttp;
}

Ajax.prototype.get = function ()
{
	if(!this.xmlHttp)return;
	try{
		var queryString = this.force?this.URLAddTimestamp(this.url):this.url +(this.xml?("&" + this.xml):'');
		this.xmlHttp.open("GET",queryString,true);
		this.xmlHttp.send(null);
	}catch(err)
	{
		this.onerror(this);
	}
}

Ajax.prototype.post = function()
{
	if(!this.xmlHttp)return;
	try{
		var url = this.force?this.URLAddTimestamp(this.url):this.url;
		var queryString = this.xml;
		this.xmlHttp.open("POST",url,true);
		this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		this.xmlHttp.send(queryString);
	}catch(err)
	{
		this.onerror(this);
	}
}

Ajax.prototype.getXML=function()
{
	return this.xmlHttp?this.xmlHttp.responseXML:null;
}

Ajax.prototype.getText=function()
{
	return this.xmlHttp?this.xmlHttp.responseText:'';
}

Ajax.onReadyState = function()
{
	if (this.xmlHttp.readyState == 4)
	{
		if (this.xmlHttp.status == 200)
		{
			this.onload(this);
		}else
		{
			this.onerror(this);
		}
	}
}

Ajax.prototype.URLAddTimestamp=function(url){
	var preg=/\?.{1,}\=.{1,}/i;
	return url + (preg.test(url)?"&Timestamp=": "?Timestamp=")+ new Date().getTime();
}

Ajax.prototype.defaultLoad=function(){}
Ajax.prototype.defaultError=function(){
  alert("链接服务器失败"
  	+"\n\nurl:"+this.url
    +"\n\nreadyState:"+this.xmlHttp.readyState
    +"\nstatus: "+this.xmlHttp.status
    +"\nheaders: "+this.xmlHttp.getAllResponseHeaders());
}

//*********************************************************************************************************************
function $(_sId){ return document.getElementById(_sId);}

function getMsgId(str)
{
	var regexp=/<!--msg:\d+-->/i;
	str=regexp.exec(str);
	regexp=/\d+/i;
	return regexp.exec(str);
}


function getClientSize(){
	var size=new Size(0,0);
	size.width=document.documentElement.clientWidth;
	size.height=document.documentElement.clientHeight;
	return size;
}
function getBodySize(){
	var size=new Size(0,0);
	size.width=document.documentElement.scrollWidth;
	size.height=document.documentElement.scrollHeight;
	return size;
}

function getScrollXY(){
	var point=new Point(0,0);
	point.X=Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
	point.Y=Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	return point;
}
//*********************************************************************************************************************
function ImageSize(imgSize,bSize)
{
	var ws=imgSize.width-bSize.width;
	var hs=imgSize.height-bSize.height;
	if(ws <=0 && hs<=0) return new Size(imgSize.width,imgSize.height);
	var w=0;
	var h=0;
	if(ws>=0 && ws >= hs)
	{
		//master width
		w=bSize.width;
		h=parseInt(bSize.width*imgSize.height/imgSize.width);
	}else
	{
		h=bSize.height;
		w=parseInt(bSize.height*imgSize.width/imgSize.height);
	}
	return new Size(w,h);
}
//******************************************************************************************
//| 字符串

//string class
function StringBuffer()
{
	this._strings=new Array;
}
StringBuffer.prototype.append=function(str)
{
	this._strings.push(str);
}
StringBuffer.prototype.toString=function()
{
	var str=arguments.length>0?arguments[0]:'';
	return this._strings.join(str);
}
function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, ""); 
}
function ltrim(str){
	return str.replace(/^\s*/g, "");
}
function rtrim(str){
	return str.replace(/\s*$/g, ""); 
}
function ftrim(str){
	return str.replace(/\s*/g, ""); 
}


function getStamp(){
	return new Date().getTime().toString()+Math.round(Math.random()*100).toString();
}

//全角转半角
function toDBC(Str){
	var DBCStr = new StringBuffer();
	for(var i=0; i<Str.length; i++){
		var c = Str.charCodeAt(i);
		if(12288==c) {
			DBCStr.append(String.fromCharCode(32));
			continue;
		}
		if (c > 65280 && c < 65375){
			DBCStr.append(String.fromCharCode(c - 65248));
			continue;
		}
		DBCStr.append(String.fromCharCode(c));
	}
	return DBCStr.toString();
}
//字符串长度
function strlen(str){
	var len=0;
	for(i=0;i<str.length;i++){
		if(str.charCodeAt(i) ==32) continue;
		len++;
	}
	return  len;
}

function Size(w,h)
{
	this.width=w;
	this.height=h;
}
function Point(x,y)
{
	this.X=x;
	this.Y=y;
}
function Message(id,message){
	this.id=id;
	this.message=message;
}
//format message
var getMessage=function(str){
	str=(/<!--msg:\[.*?\].*?-->/i).exec(str);
	if(!str) return null;
	str= str.toString().replace(/(^<!--msg:)|(-->$)/ig,'');
	var code=(/^\[.*?\]/i).exec(str);
	if(code){
		id=code.toString().replace(/(^\[)|(\]$)/ig,'');
		str=str.replace(/^\[.*?\]/i,'');
		return new Message(id,str);
	}
	return null;
}
//*********************************************************************************************************************
var EventUtil=new Object;
EventUtil.addEventHander=function(oTarget,sEventType,fnHander,bCapture){
	bCapture=bCapture?true:false;
	if(oTarget.addEventListener){
		oTarget.addEventListener(sEventType,fnHander,bCapture);
	}else if(oTarget.attachEvent){
		oTarget.attachEvent('on'+sEventType,fnHander);
	}else{
		oTarget['on'+sEventType]=fnHander;
	}
}
EventUtil.removeEventHander=function(oTarget,sEventType,fnHander){
	bCapture=bCapture?true:false;
	if(oTarget.addEventListener){
		oTarget.removeEventListener(sEventType,fnHander,bCapture);
	}else if(oTarget.attachEvent){
		oTarget.detachEvent('on'+sEventType,fnHander);
	}else{
		oTarget['on'+sEventType]=null;
	}
}



