﻿/**
 * 客户回调程序，需要xmlhttp支持
 * 闭至海
 * @param {Object} url post的地址
 * @param {Object} postValue 参数值，以post形式描述(假如为带特殊字符的符串则用parameStr(value)获取编码后的值，服务器端要进行解码)
 * @param {Object} resultFunction 处理完成后返回的外理函数
 * @param {Object} failedCallback 出错时的函数
 * @return 直接返回Array,值只支持五种类型：string,integer,boolean,float,datetime
 */
function CallBackXMLArray(url,postValue,succeededCallback,failedCallback)
{
	if (_this!=null)
	{
		_this=null;
	}
	var _this=this;

	this.url=url;
	this.postValue=postValue;
	this.succeededCallback=succeededCallback;
	this.failedCallback=failedCallback;
	this._xmlHttpRequest = null;
	this._xmlHttpRequest=getXMLHttpRequest_();
	
	this.getCallBack=function()
	{
		//deal url and post	
		//alert(_this.url + "?" + _this.postValue);
	    _this._xmlHttpRequest.open("POST",_this.url,true);
		_this._xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
	    _this._xmlHttpRequest.onreadystatechange = _this.returnXmlResult;
		try
		{
	    	_this._xmlHttpRequest.send(_this.postValue);
		}
		catch (e)
		{
			alert("执行参数或网址出错！");
			_this.failedCallback();
		}
	}
	this.returnXmlResult=function()
	{
		if (_this._xmlHttpRequest.readyState === 4 )
		{
			if (_this._xmlHttpRequest.status === 200)
			{
				//alert(_this._xmlHttpRequest.responseXML.xml);
				/*新的回调*/
				var xml;
				try
				{
					xml = _this._xmlHttpRequest.responseXML;
			        if (!xml || !xml.documentElement) {
			
			                        xml = new XMLDOM(_this._xmlHttpRequest.responseText);
	
			                        if (!xml || !xml.documentElement)
			                _this.succeededCallback(null);
			        }
			                else if (navigator.userAgent.indexOf('MSIE') !== -1) {
			            xml.setProperty('SelectionLanguage', 'XPath');
			        }
			
			                if (xml.documentElement.namespaceURI === "http://www.mozilla.org/newlayout/xml/parsererror.xml" &&
			            xml.documentElement.tagName === "parsererror") {
			            _this.succeededCallback(null);
			        }
			        
			                if (xml.documentElement.firstChild && xml.documentElement.firstChild.tagName === "parsererror") {
			            _this.succeededCallback(null);
			        }
				} catch (eeee__) { alert("错误的XML格式。"); }
				var result;
				if (xml)
				{
					result = Foreach_(xml);
				}
		       _this.succeededCallback(result);
				/*新的回调end*/
        	}
			else if (_this._xmlHttpRequest.status === 404)
			{
				alert("非法的服务器请求，HTTP 错误 404 没有找到网络路径。");
				if (_this.failedCallback()) _this.failedCallback();
			}
			else if (_this._xmlHttpRequest.status === 403)
			{
				alert("非法的服务器请求，HTTP 错误 403 - 禁止访问。");
				if (_this.failedCallback()) _this.failedCallback();
			}
			else
			{
				alert("服务器请求错误，HTTP 状态 " + _this._xmlHttpRequest.status);
				if (_this.failedCallback()) _this.failedCallback();
			}
		}
	}
}

function getXMLHttpRequest_()
{
	var xmlHttpRequest_ = null;
	if (window.ActiveXObject)
	{
		try
		{
			xmlHttpRequest_ = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch(e)
		{
			try
			{
				xmlHttpRequest_ = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(ee)
			{
				alert("您的浏览器不支持XMLHTTP服务器访问，请安装Internet Explorer6.0以上版本。\n您可以访问微软公司的网站下载此支持软件");
				throw ee;
			}
			throw e;
		}
	}
	else if (window.XMLHttpRequest)
	{
		try
		{
			xmlHttpRequest_ = new XMLHttpRequest();
		}
		catch(e)
		{
			alert("您的浏览器不支持XMLHTTP服务器访问，请安装Internet Explorer6.0以上版本。\n您可以访问微软公司的网站下载此支持软件");
			throw e;
		}
	}
	return xmlHttpRequest_;
}


/**
 * 分析XMLHTTP返回的.responseXML，并把结果存入Array里
 */
/**
 * 从根节点历遍xml
 */
function Foreach_(dom)
{
	var root=dom.getElementsByTagName('items').item(0);//根目录
	if (!root)
	{
		if (document.all)
		{
			root = dom.childNodes[1];//IE
		}
		else
		{
			root = dom.childNodes[0];//Firfox
		}
	}
	var value = GetValueFromNode(root);
	return value;
}
function GetValueFromNode(node)
{
	var ValueFromXmlArray = new Array();
	for (var i=0;i<node.childNodes.length;i++)
	{
		if (node.childNodes[i].childNodes.length==1)
		{
			//只有一个值时,只支持五种类型：string,integer,boolean,float,datetime
			var thisType = "";
			var tmpValue;
			try
			{
				thisType = node.childNodes[i].getAttribute("type");
				tmpValue = node.childNodes[i].lastChild.nodeValue;
			}
			catch(Ex)
			{
				//假如取不到的处理
				
			}
			if (thisType=="string")
			{
				ValueFromXmlArray[ValueFromXmlArray.length] = tmpValue;
			}
			else if (thisType=="boolean")
			{
				if (tmpValue.toLowerCase()=="true")
				{
					ValueFromXmlArray[ValueFromXmlArray.length] = true;
				}
				else
				{
					ValueFromXmlArray[ValueFromXmlArray.length] = false;
				}
			}
			else if (thisType=="integer")
			{
				tmpValue = parseInt(tmpValue);
				if (isNaN(tmpValue))
				{
					ValueFromXmlArray[ValueFromXmlArray.length] = 0;
				}
				else
				{
					ValueFromXmlArray[ValueFromXmlArray.length] = tmpValue;
				}
			}
			else if (thisType=="float")
			{
				tmpValue = parseFloat(tmpValue);
				if (isNaN(tmpValue))
				{
					ValueFromXmlArray[ValueFromXmlArray.length] = 0;
				}
				else
				{
					ValueFromXmlArray[ValueFromXmlArray.length] = tmpValue;
				}
			}
			else if (thisType=="datetime")
			{
				var dateValue = new Date(tmpValue);
				ValueFromXmlArray[ValueFromXmlArray.length] = dateValue;
			}
			else
			{
				//ValueFromXmlArray[ValueFromXmlArray.length] = tmpValue;
				//还有子节点
				ValueFromXmlArray[ValueFromXmlArray.length] = GetValueFromNode(node.childNodes[i]);
			}
		}
		else
		{
			ValueFromXmlArray[ValueFromXmlArray.length] = GetValueFromNode(node.childNodes[i]);
		}
	}
	return ValueFromXmlArray;
}
/**
* 取得上经编码后回传的字符串参数
*/
function parameStr(value)
{
	return escape(encodeURIComponent(value));
}

/**
* 显示正在加载提示框,每个项目用到的样式不同
*/
var loadingModImgPath = "/images/";//正在加载的图片路径
function coverAll(msg,nocover) {
	if(!msg) msg="uploading...";
	var e,p;
		if( (e =  document.getElementById('cover_all')) == null) {	//如果没有该loading DIV
			e = document.createElement("DIV");
			e.id = 'cover_all';
			document.body.appendChild(e);
			e.style.cssText="filter:alpha(opacity=0);background:#0f0;display:none;position:absolute;width:100%;height:100%;left:0px;top:30px;z-index:990";
		}
		if( (p = document.getElementById('processing')) == null) {
			p = document.createElement("DIV");
			p.id = 'processing';
			document.body.appendChild(p);
			p.style.cssText="display:none;position:absolute;height:20px;left:450px;font:12px;z-index:101";
		}
		try{ e.style.height = document.body.scrollHeight-10 + "px"; }
		catch (x) { e.style.height = document.documentElement.scrollHeight-10 + "px"; }
		p.style.top = document.body.scrollTop+ 200;
		if(document.documentElement&&document.documentElement.clientWidth)
			p.style.left = document.documentElement.scrollLeft+(document.documentElement.clientWidth-240)/2;
		else
			p.style.left = document.body.scrollLeft+(document.body.clientWidth-240)/2;
		if(!msg.match(/^\s*$/)) {	//如果不为空
			p.innerHTML = '<table border="0" cellspacing="0" cellpadding="0" style="z-index:991"><tr><td width="8" align="right" style="background-image:url(' + loadingModImgPath + 'cover_box_l.gif);background-repeat:no-repeat;"><img src="' + loadingModImgPath + 'none.gif" /></td>' + 
			'<td style="width:230px;background-image:url(' + loadingModImgPath + 'cover_box_c.gif);background-repeat:repeat-x;"><table cellpadding="3" cellspacing="0" style="font-weight:bold;color:#985807;padding:5px 0px 0px 5px;font-size:10pt;"><tr><td><img src="' + loadingModImgPath + 'loading3.gif" /></td><td>'+ msg+ '</td></tr></table></td>' +
			'<td width="52" height="40" style="background-image:url(' + loadingModImgPath + 'cover_box_r.gif);background-repeat:no-repeat;"><img src="' + loadingModImgPath + 'none.gif" /></td></tr></table>';
		}
	if(!nocover) {
		if(e) e.style.display  = "";
	}
	if(p) p.style.display = "";
}
function uncoverAll() {
	var e,p;
	e = document.getElementById('cover_all');
	p = document.getElementById('processing');
	try{ e.style.display = p.style.display = "none"; } catch(e){}
}

/**
 * 弹出信息框
 * @param {Object} msg 信息
 * @param {Object} nocover 是否是不要覆盖页面
 * @param {Object} width_ 提示框宽度
 * @param {Object} height_ 提示框高度
 */
function coverAllWithMsg(title_,msg,nocover,width_,height_,top_,left_) {
	if (isNaN(top_))
	{
		top_ = 100;
	}
	if (isNaN(left_))
	{
		left_ = 240;
	}
	if (title_=="")
	{
		title_ = "no title";
	}
	if(!msg) msg="uploading...";
	var e,p;
		if( (e =  document.getElementById('cover_all')) == null) {	//如果没有该loading DIV
			e = document.createElement("DIV");
			e.id = 'cover_all';
			document.body.appendChild(e);
			e.style.cssText="filter:alpha(opacity=0);background:#0f0;display:none;position:absolute;width:100%;height:100%;left:0px;top:30px;z-index:990";
		}
		if( (p = document.getElementById('processing')) == null) {
			p = document.createElement("DIV");
			p.id = 'processing';
			document.body.appendChild(p);
			p.style.cssText="display:none;position:absolute;height:20px;left:450px;font:12px;z-index:101";
		}
		try{ e.style.height = document.body.scrollHeight-10 + "px"; }
		catch (x) { e.style.height = document.documentElement.scrollHeight-10 + "px"; }
		p.style.top = document.body.scrollTop+ top_;
		if(document.documentElement&&document.documentElement.clientWidth)
			p.style.left = document.documentElement.scrollLeft+(document.documentElement.clientWidth-left_)/2;
		else
			p.style.left = document.body.scrollLeft+(document.body.clientWidth-left_)/2;
		if(!msg.match(/^\s*$/)) {	//如果不为空
			p.innerHTML = '<div style="z-index:991;width:' + width_ + 'px;height:' + height_ + 'px;" class="msg_box">' +
			'<div class="msgBoxTitile"><div class="msgBoxTitile_right"><a href="#" onclick="uncoverAll();return false;">关闭</a></div>' + title_ + '</div>' + 
			'<div class="context">' + msg
			'</div></div>';
		}
	if(!nocover) {
		if(e) e.style.display  = "";
	}
	if(p) p.style.display = "";
}

function getParameter(name){
 var paramStr=location.search;
 if(paramStr.length==0)
  return null;
 if(paramStr.charAt(0)!='?')
  return null;
 paramStr=unescape(paramStr);
 paramStr=paramStr.substring(1);
 if(paramStr.length==0)
  return null;
 var params=paramStr.split('&');
 for(var i=0;i<params.length;i++){
  var parts=params[i].split('=',2);
  if(parts[0]==name){
   if(parts.length<2||typeof(parts[1])=="undefined"||parts[1]=="undefined"||parts[1]=="null")
    return "";
   return parts[1];
  }
 }
 return null;
}
//正则验证
function RegExpCheck(value,expression)
{
		var thisChecked=true;
		var thisExp=new RegExp(expression);
		//var m=value.match(thisExp);
		if (!thisExp.test(value))
		{
			thisChecked=false;
		}
		return thisChecked;
}

function $get(id_)
{
	var e = document.getElementById(id_);
	if (e==null)
	{
		e = document.getElementsByName(id_);
	}
	return e;
}
function $set(e,value)
{
	if (e)
	{
		if (e.type=="text" || e.type=="textarea" || e.type=="hidden")
		{
			e.value = value;
		}
		else
		{
			e.innerHTML = value;
		}
	}
}
function RemoveHTML(strText)
{
var regEx = /<[^>]*>/g;
return strText.replace(regEx, "");
}