﻿var $I=function(id)
{
    return document.getElementById(id);
}

var $$=function(name)
{
    return document.getElementsByName(name);
}

//多浏览器 innerText
try
{
    HTMLElement.prototype.__defineGetter__
    (
        "innerText",
        function ()
        {
            var anyString = "";
            var childS = this.childNodes;
            for(var i=0; i<childS.length; i++)
            {
                if(childS[i].nodeType==1)
                    anyString += childS[i].tagName=="BR" ? '"n' : childS[i].innerText;
                else if(childS[i].nodeType==3)
                    anyString += childS[i].nodeValue;
            }
            return anyString;
        }
    ); 
}
catch(e){}

//添加 CSS
function fAddClass(XEle, XClass)
{     
    //if(!XClass) throw new Error("XClass 不能为空!");
    if(XEle.className!="")
    {
        var Re = new RegExp("\\b"+XClass+"\\b\\s*", "");
        XEle.className = XEle.className.replace(Re, "");
        XEle.className = XClass+" "+XEle.className;
    } 
    else
    {
        XEle.className = XClass;
    }
}
//移除 CSS
function fRemoveClass(XEle, XClass)
{
    //if(!XClass) throw new Error("XClass 不能为空!");
    if(XEle.className!="")
    {
        var Re = new RegExp("\\b"+XClass+"\\b\\s*", "");
        XEle.className = XEle.className.replace(Re, "");
    }
}

//去除空格
String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }  
String.prototype.LTrim = function() { return this.replace(/(^\s*)/g, ""); }  
String.prototype.RTrim = function() { return this.replace(/(\s*$)/g, ""); } 

// 根据参数创建名为name的对象
function createObj(name)
{
    // 返回创建的对象
    return document.createElement(name);
}

// 计时器
function DoCallTimer(id)
{  
    var minute="00";
    var second="00";
    CallTimeLen = parseInt(CallTimeLen) + 1;
    hour = parseInt(CallTimeLen / 3600);
    minute = parseInt(CallTimeLen / 60);
    second = CallTimeLen % 60;
    if (second<10){second = "0" + second;}
    if (minute<10){minute = "0" + minute;}
    if (hour<10){hour = "0" + hour;}
    //document.getElementById(id).innerText = hour + ":" + minute + ":" + second;
    $I(id).innerText = hour + ":" + minute + ":" + second;
    window.timer1 = window.setTimeout("DoCallTimer('"+id+"')",1000);
}

function StopCallTimer()
{
    clearTimeout(window.timer1);
}


// 生成随机数
function GetRandomNum(Min,Max)
{
    var Range = Max - Min;
    var Rand = Math.random();
    return(Min + Math.round(Rand * Range));
}

//左边填充字符
function padleft(val, ch, num) 
{
    var re = new RegExp(".{" + num + "}$");
    var pad = "";

    do{
        pad += ch;
    }while(pad.length < num)

    return re.exec(pad + val);
}
//右边填充字符
function padright(val, ch, num)
{
    var re = new RegExp("^.{" + num + "}");
    var pad = "";

    do{
        pad += ch;
    } while (pad.length < num)

    return re.exec(val + pad);
}

// 获取页面宽度
function getWidth()
{
    var strWidth,clientWidth,bodyWidth;
    clientWidth = window.parent.document.documentElement.clientWidth;
    bodyWidth = window.parent.document.body.clientWidth;
    if(bodyWidth > clientWidth){
        strWidth = bodyWidth + 20;
    } else {
        strWidth = clientWidth;
    }
    return strWidth;
}

//获取页面高度
function getHeight()
{
    var strHeight,clientHeight,bodyHeight;
    clientHeight = window.parent.document.documentElement.clientHeight;
    bodyHeight = window.parent.document.body.clientHeight;
    if(bodyHeight > clientHeight){
        strHeight = bodyHeight + 30;
    } else {
        strHeight = clientHeight;
    }
    return strHeight;
}

//获取 URL 参数
function requestUrl(paras)   //调用方法：request('str')
{ 
    var url = location.search.substring(1);//location.href; 
    var paraString = url.split("&");//url.substring(url.indexOf("?")+1,url.length).split("&"); 
    var paraObj = {} 
    for (i=0; j=paraString[i]; i++)
    { 
        paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length); 
    } 
    var returnValue = paraObj[paras.toLowerCase()]; 
    returnValue = decodeURI(returnValue);
    if(typeof(returnValue)=="undefined") { return ""; }
    else{ return returnValue; } 
}

//字符串编码
function EncodeStr(str)
{
    var sURL = encodeURI(str);
    return sURL;
}

//字符串解码
function DecodeStr(str)
{
    var sURL = decodeURI(str);
    return sURL;
}

//设置控件是否可用
function ctrlSetProperty(id,property)
{
    switch(property)
    {
        case "Able":
            //document.getElementById(id).disabled=false;
            $I(id).disabled=false;
            break;
        case "Disable":
            //document.getElementById(id).disabled=true;
            $I(id).disabled=true;
            break;
    }
}

//清空下拉列表
function sltDeleteOption(id)
{
    //document.getElementById(id).options.length=0;
    $I(id).options.length=0;
}

//向下拉列表中添加项
function sltAddOption(id,text,value)
{
    var oOption = document.createElement('OPTION');
    oOption.text = text;
    oOption.value = value;
    //document.getElementById(id).options.add(oOption);
    $I(id).options.add(oOption);
}

//设置多选框是否可用
function chkSetProperty(name,property)
{
    //var obj = document.getElementsByName(name);
    var obj = $$(name);
    for(i=0;i<obj.length;i++)
    {     
        if(property=="Disable"){obj[i].checked=false;}
        ctrlSetProperty(obj[i].id,property);
    }
}

//获取多选框选定值
function chkGetValue(name,strChar)
{
    var strValue="";
    //var obj = document.getElementsByName(name);
    var obj = $$(name);
    for(i=0;i<obj.length;i++)
    {     
        if(obj[i].checked==true){strValue=strValue+obj[i].value+strChar;}
    }
    try{strValue=strValue.substring(0,strValue.length-strChar.length);}
    catch(e){}
    return strValue;
}

//设置图片显示路径
function imgSrc(id,imgUrl)
{
    //document.getElementById(id).src = imgUrl;
    $I(id).src = imgUrl;
}

//xml异步请求
function HttpRequest()
{
	if(this==window)throw new Error(0,"HttpRequest is unable to call as a function.")
	var me=this;
   	var asyncFlag=false;
   	var typeFlag=false;
	var r;
	function onreadystatechange()
	{
		if(me.onreadystatechange)me.onreadystatechange.call(r);
		if(r.readyState==4)
		{
			if(Number(r.status)>=300)
			{
				if(me.onerror)me.onerror.call(r,new Error(0,"Http error:"+r.status+" "+r.statusText));
				if(typeFlag)r.onreadystatechange=Function.prototype;
				else r.onReadyStateChange=Function.prototype;
				r=null;
				return;
			}
			me.status=r.status;
			me.statusText=r.statusText;
			me.responseText=r.responseText;
			me.responseBody=r.responseBody;
			me.responseXML=r.responseXML;
			me.readyState=r.readyState;
			if(typeFlag)r.onreadystatechange=Function.prototype;
			else r.onReadyStateChange=Function.prototype;
			r=null;
			if(me.onfinish)me.onfinish();
		}
	}
	
	function creatHttpRequest(){
		var e;
		try{
			r=new window.XMLHttpRequest();
			typeFlag=true;
		} catch(e) {			
			var ActiveXName=[
				'MSXML2.XMLHttp.6.0',
				'MSXML2.XMLHttp.3.0',
				'MSXML2.XMLHttp.5.0',
				'MSXML2.XMLHttp.4.0',
				'Msxml2.XMLHTTP',
				'MSXML.XMLHttp',
				'Microsoft.XMLHTTP'
			]
			function XMLHttpActiveX()
			{
				var e;
				for(var i=0;i<ActiveXName.length;i++)
				{
					try{
						var ret=new ActiveXObject(ActiveXName[i]);
						typeFlag=false;
					} catch(e) {
						continue;
					}
					return ret;
				}
				throw {"message":"XMLHttp ActiveX Unsurported."};
			}
			try{
				r=new XMLHttpActiveX();
				typeFlag=false;
			} catch(e) {
				throw new Error(0,"XMLHttpRequest Unsurported.");
			}
		}
	}
	creatHttpRequest();


	this.abort=function(){
		r.abort();
	}
	this.getAllResponseHeaders=function(){
		r.getAllResponseHeaders();
	}
	this.getResponseHeader=function(Header){
		r.getResponseHeader(bstrHeader);
	}
	this.open=function(Method,Url,Async,User,Password){
		asyncFlag=Async;
		try{
			r.open(Method,Url,Async,User,Password);
		} catch(e) {
			if(me.onerror)me.onerror(e);
			else throw e;
		}
	}
	this.send=function(Body){
		try{
			if(typeFlag)r.onreadystatechange=onreadystatechange;
			else r.onReadyStateChange=onreadystatechange;

			r.send(Body);
			//alert("sended");
			if(!asyncFlag){
				this.status=r.status;
				this.statusText=r.statusText;
				this.responseText=r.responseText;
				this.responseBody=r.responseBody;
				this.responseXML=r.responseXML;
				this.readyState=r.readyState;

				if(typeFlag)r.onreadystatechange=Function.prototype;
				else r.onReadyStateChange=Function.prototype;

				r=null;
			}
		} catch(e) {
			if(me.onerror)me.onerror(e);
			else throw e;
		}
		//alert("sended");
	}
	this.setRequestHeader=function(Name,Value){
		r.setRequestHeader(Name,Value);
	}
}