var isIE, isOpr, isFox, isMoz;

var agent = (navigator.userAgent) ? navigator.userAgent.toLowerCase() : "";
isOpr = agent.indexOf("opera") > -1;
isFox = (agent.indexOf("firefox") > -1) && !isOpr;
isIE = (agent.indexOf("msie") > -1) && !isOpr && !isFox;


String.prototype.trim = function()
{
	return this.replace(/^\s*|\s*$/g, "");
}

function $(id)
{
	return document.getElementById(id);
}

function getElementPosition(el)
{
	var left = 0;
	var top  = 0;
	
	while(el)
	{
	    //document.title += " " + el.tagName;
		left += el.offsetLeft;
		top  += el.offsetTop;
		
		
		if ( el.style )
		{
			left += (isNaN(parseInt(el.style.borderLeftWidth))) ? 0 : parseInt(el.style.borderLeftWidth);
			top  += (isNaN(parseInt(el.style.borderTopWidth)))  ? 0 : parseInt(el.style.borderTopWidth);
		}
		el = el.offsetParent;
	}
	return {x:left, y:top};
}

function getMousePosition(ev)
{
	ev = ev || window.event;
	if( ev.pageX || ev.pageY )
	{
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft,
		y:ev.clientY + document.documentElement.scrollTop - document.documentElement.clientTop
	};
}

function getMouseOffset(ev, el)
{
	ev = ev || window.event;

	var epos = getElementPosition(el);
	var mpos = getMousePosition(ev);
	return {x:mpos.x - epos.x, y:mpos.y - epos.y};
}

function getH()
{
    if (window.innerHeight && window.scrollMaxY) {
                yScroll = window.innerHeight + window.scrollMaxY;
            } else if (document.body.scrollHeight > document.body.offsetHeight){
                yScroll = document.body.scrollHeight;
            } else {yScroll = document.body.offsetHeight;}
            return yScroll;
}

function getScrollPos()
{
    var scrollPos; 
    if (typeof window.pageYOffset != 'undefined')    //针对Netscape 浏览器
    {
        scrollPos = window.pageYOffset; 
    } 
    else if (typeof document.compatMode != 'undefined' &&   document.compatMode != 'BackCompat')
    {
        scrollPos = document.documentElement.scrollTop; 
    } 
    else if (typeof document.body != 'undefined') 
    {
        scrollPos = document.body.scrollTop; 
    } 
    scrollPos = parseInt(scrollPos);
    return isNaN(scrollPos) ? 0 : scrollPos;
}

function stopB(ev)
{
	ev = ev || window.event;
	ev.cancelBubble = true;
}


// WebRequest
function WebRequest(url)
{
	this.Method = "GET";
	this.Url = url;
	this.Async = true;
	this.Refresh = false;
	this.onReady = null;
	this.onLoad = null;
	this.onError = null;
	this.responseText = null;
	this.responseXml = null;
	
	var req = GetRequest();
	var headers = new Array();

	function GetRequest()
	{
		var req = null;
		
		if ( window.XMLHttpRequest )
		{
			req = new XMLHttpRequest();
		}
		else if ( window.ActiveXObject )
		{
			try
			{
				req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		
		return req;
	}
	
	
	
	this.SetRequestHeader = function(name, value)
	{
		for ( var i = 0, j = headers.length; i < j; i++ )
		{
			if ( headers[i].Name == name )
			{
				headers[i] = {Name:name, Value:value};
				return;
			}
		}
		
		var _header = {Name:name, Value:value};		
		headers.push(_header);
	}
	
	this.Send = function(obj)
	{	
		if ( req == null )
		{
			alert("Can't create object.");
			return;
		}
		if ( this.Async )
		{
			var ohttp = this;
			req.onreadystatechange = function()
			{
				if (  req.readyState == 4 )
				{
					ohttp.responseText = req.responseText;
					ohttp.responseXml = req.responseXML;
					
					if ( ohttp.onReady )
						ohttp.onReady();
				}
				else
				{
					if ( ohttp.onLoad )
						ohttp.onLoad();
				}
			}
		}
		
		
		try
		{
			var rnd = "";
			if ( this.Refresh )
			{
				rnd = "webrequestrnd=" + Math.random();
				if ( this.Url.indexOf("?") >= 0 )
					rnd = "&" + rnd;
				else
					rnd = "?" + rnd;
			}
			req.open(this.Method, this.Url + rnd, this.Async);
			
			for ( var i = 0, j = headers.length; i < j; i++ )
			{
				var header = headers[i];
				req.setRequestHeader(header.Name, header.Value);
			}
			
			req.send(obj);
			if ( !this.Async )
			{
				this.responseText = req.responseText;
				this.responseXml = req.responseXML;
			}
		}
		catch(err)
		{
			if ( this.onError )
				this.onError(err);
			else
				alert("Failed: " + err.message);
		}
	}
	
	this.Abort = function()
	{
		if ( req != null ) req.abort();
	}
}


function ResponseReader(response)
{
	/*
		<response>
			<error>是否有错误（0 - 无错误 1 - 有错误）</error>
			<message>返回信息</message>
			<responsebody>返回结构</responsebody>
		</response>
	*/
	this.hasError = false;
	this.errorMessage = "";
	this.responseBody = null;
	this.responseText = "";

	if ( response != null )
	{
		var errcode, message, rbody, bodytext;
		
		try
		{
			var resobj = response.getElementsByTagName("response")[0];
			errcode = resobj.getElementsByTagName("error")[0].firstChild.nodeValue * 1;
			message = (resobj.getElementsByTagName("message")[0].firstChild) ? resobj.getElementsByTagName("message")[0].firstChild.nodeValue : "";
			rbody = resobj.getElementsByTagName("responsebody")[0].firstChild;
			bodytext = (rbody) ? rbody.nodeValue : "";
			this.hasError = (errcode != 0);
			this.errorMessage = message;
			this.responseBody = rbody;
			this.responseText = bodytext;
		}
		catch(err)
		{
			this.hasError = true;
			this.errorMessage = "返回信息格式错误";
			this.responseBody = null;
			this.responseText = "";
		}
	}
}

function GetTimeStamp()
{
	var date = new Date();
	var str = date.getFullYear();
	str += ((date.getMonth() + 1) > 9) ? ((date.getMonth() + 1)).toString() : "0" + ((date.getMonth() + 1)).toString();
	str += ((date.getDate() * 1) > 9) ? date.getDate().toString() : "0" + date.getDate().toString();
	str += ((date.getHours() * 1) > 9) ? date.getHours().toString() : "0" + date.getHours().toString();
	str += ((date.getMinutes() * 1) > 9) ? date.getMinutes().toString() : "0" + date.getMinutes().toString();
	str += ((date.getSeconds() * 1) > 9) ? date.getSeconds().toString() : "0" + date.getSeconds().toString();
	
	return str;
}

// 提示框
function Box(id)
{  
    var _bgid = "box_bg";
    var _id = (id) ? id : "box_main";
    var _bdid = "box_main_body";
    var _title = "提示框";
    var _width = 400;
    var _content = "";
    var _body = null;
    var _closeImg = "";
    var _closeImgHover = "";
    var _bbg = null;
    var _bm = null;
    
    this.SetTitle = function(t)
    {
        if (t)
            _title = t;
    }
    
    this.SetWidth = function(w)
    {
        _width = w;
    }
    
    this.SetBody = function(el)
    {
        _body = el;
    }
    
    this.Close = function()
    {
        var bg = $(_bgid);
        var main = $(_id);
        
        if ( bg && main )
        {
            showSelect();        
            bg.style.display = "none";
            main.style.display = "none";
        }
    }
    var def_close = this.Close;
    
    this.Show = function()
    {
        IniDiv();
    
        var bg = document.getElementById(_bgid);
        var main = document.getElementById(_id);
        
        if ( bg && main )
        {
            hidSelect();
            
            var ph = main.clientHeight;
            var sh = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
            var bh = getH();
            var sp = getScrollPos();

            if (ph > sh)
            {
                bg.style.height = (bh > ph ? bh : ph) + "px";
                //main.style.marginTop = ((bh > sh) ? bh - sh : 0) + "px";
            }
            else
            {
                //main.style.marginTop = "-" + ph/2 + 'px';
                main.style.marginTop = ((bh > sh) ? bh - sh : 0) + "px";
                bg.style.height = (sh > bh ? sh : bh) +"px";
            }
            main.style.marginTop = sp + "px";
            main.style.left = (document.body.offsetWidth - 425) / 2 + "px";
            
            bg.style.display = "block";
            main.style.display = "block";
        }
    }
    
    function showSelect()
    {
        var sels = document.getElementsByTagName("select");
        if (sels)
        {
            for (var i = 0, j = sels.length; i < j; i++ )
            {
                sels[i].style.visibility = "visible";
            }
        }
    }
    
    function hidSelect()
    {
        var sels = document.getElementsByTagName("select");
        if (sels)
        {
            for (var i = 0, j = sels.length; i < j; i++ )
            {
                sels[i].style.visibility = "hidden";
            }
        }
    }
    
    function IniDiv()
    {
        if ($(_bgid))
        {
            _bbg = $(_bgid);
        }
        else
        {
            _bbg = document.createElement("div");
            _bbg.setAttribute("id", _bgid);
            document.body.appendChild(_bbg);
        }
        
        if ($(_id))
        {
            _bm = $(_id);
            _bm.innerHTML = "";
        }
        else
        {
            _bm = document.createElement("div");
            _bm.setAttribute("id", _id);
            document.body.appendChild(_bm);
        }
        _bm.style.width = _width + "px";
        
        // title
        var bmt = document.createElement("div");
        bmt.className = "box_title";
        
        var bmtc1 = document.createElement("div");
        bmtc1.className = "box_caption";
        bmtc1.innerHTML = _title;
        
        var bmtc2 = document.createElement("div");
        bmtc2.className = "box_ctrl";
        bmtc2.innerHTML = "<a class=\"v_boxx\" href=\"###\" onclick=\"javascript:var box = new Box().Close();\"></a>";
        bmtc2.onclick = def_close
        
        bmt.appendChild(bmtc1);
        bmt.appendChild(bmtc2);
        
        // body
        var bmb = document.createElement("div");
        bmb.setAttribute("id", _bdid);
        bmb.className = "box_body";
        if (_body)
            bmb.appendChild(_body);
        
        _bm.appendChild(bmt);
        _bm.appendChild(bmb);
    }
}
// 提示框结束

// 常用提示框
function showOK(message)
{
    var box = new Box();
    var con = document.createElement("div");
    var txtl = document.createElement("div");
    txtl.className = "box_textline";
    txtl.innerHTML = message;
    
    var btnl = document.createElement("div");
    btnl.className = "box_btnline";
    var btn = document.createElement("a");
    btn.setAttribute("href", "###");
    btn.className = "dialogbtn";
    btn.innerHTML = "确定";
    btn.onclick = function()
    {
        var box = new Box();
        box.Close();
    }
    
    btnl.appendChild(btn);
    con.appendChild(txtl);
    con.appendChild(btnl);
    box.SetTitle("提示框");
    box.SetWidth(500);
    box.SetBody(con);
    box.Show();
}

function showWindow(url, width, height, title)
{  
    var ifm = document.createElement("iframe");
    ifm.style.width = "100%";
    ifm.style.height = height + "px";
    ifm.style.borderWidth = "0px";
    ifm.setAttribute("name", "ifm_content");
    ifm.setAttribute("id", "ifm_content");
    ifm.setAttribute("frameborder", "0", 0);
    ifm.setAttribute("scrolling", "no");
    ifm.setAttribute("src", url);
    
    var box = new Box();
    box.SetTitle(title);
    box.SetWidth(width);
    box.SetBody(ifm);
    box.Show();
}
// 常用提示框结束

// 淡入淡出
var intTimeStep = 20;
var intAlphaStep = (isIE) ? 5 : 0.05;

function fadein(id)
{
    var obj = $(id);
    if (!obj) return;
    
    if ( isIE )
    {
        obj.style.filter = "alpha(opacity=0)";
        obj.filters.alpha.opacity = 0;
    }
    else
    {
        obj.style.opacity = 0;
    }
    obj.style.display = "block";
    setObjOpen(id, 0);
}
function fadeout(id)
{
    var obj = $(id);
    if (!obj) return;
    
    if ( isIE )
    {
        obj.style.filter = "alpha(opacity=100)";
        obj.filters.alpha.opacity = 100;
    }
    else
    {
        obj.style.opacity = 1;
    }
    
    obj.style.display = "block";
    setObjClose(id, 1);
}

function setObjOpen(id, iniop)
{
    var obj = $(id);
    if (!obj) return;
    if (isNaN(iniop)) iniop = 0;
    
    if(isIE)
    {
        obj.filters.alpha.opacity += intAlphaStep;
        if ( obj.filters.alpha.opacity < 100 ) setTimeout("setObjOpen('" + id + "', null)", intTimeStep);
    }
    else
    {
        iniop += intAlphaStep;
        obj.style.opacity = iniop;
        if (iniop < 1) setTimeout("setObjOpen('" + id + "', " + iniop + ")", intTimeStep);
    }
}

function setObjClose(id, iniop)
{
    var obj = $(id);   
    if (!obj) return;
    if (isNaN(iniop)) iniop = 1;
    
    if(isIE)
    {
        obj.filters.alpha.opacity -= intAlphaStep;
        if (obj.filters.alpha.opacity > 0)
        {
            setTimeout("setObjClose('" + id + "', null)", intTimeStep);
        }
        else 
        {
            obj.style.display = "none";
            obj.filters.alpha.opacity = 100;
        }
    }
    else
    {
        iniop -= intAlphaStep;
        if (iniop > 0)
        {
            obj.style.opacity = iniop;
            setTimeout("setObjClose('" + id + "', " + iniop + ")", intTimeStep);
        }
        else
        {
            obj.style.display = "none";
            obj.style.opacity = 1;
        }
    }
}
// 淡入淡出结束


function gbcount(id, max)
{
    var txtVal = $(id);
    if (txtVal.value.length > max)
        txtVal.value = txtVal.value.substring(0, max);
}