/******************************************* 通用函数 ******************************************/

function $(id) {
    return document.getElementById(id);
}

function $n(name) {
    return document.getElementsByTagName(name);
}

function getTopWindow(){
	var topWindow = window;
	while(topWindow != topWindow.parent){
		topWindow = window.parent;
	}
	return topWindow;
}

function NOW(){
	var ThisTime = new Date();
	return ThisTime.getFullYear()+"-"+(ThisTime.getMonth()+1)+"-"+ThisTime.getDate()+" "+ThisTime.getHours()+":"+ThisTime.getMinutes()+":"+ThisTime.getSeconds();
}

function Show(objId,type){
	if($(objId)!=null){
		var obj = $(objId);
		if(type=='class'){
			try{
				obj.setAttribute("class","disY");
				obj.setAttribute("className","disY");
			}
			catch(e){
			}
		}
		else{
			obj.style.cssText = "display:block;";
		}
	}
}

function Hide(objId,type){
	if($(objId)){
		var obj = $(objId);
		if(type=='class'){
			try{
				obj.setAttribute("class","disN");
				obj.setAttribute("className","disN");
			}
			catch(e){
			}
		}
		else{
			obj.style.cssText = "display:none;";
		}
	}
}

function setTargetXY(target){
	if(!target) return;
	
	var de = document.documentElement;
	var db = document.body;
	
	var clientWidth = de.clientWidth != 0 ? Math.min(de.clientWidth, db.clientWidth) : db.clientWidth;
	var clientHeight = de.clientHeight != 0 ? Math.min(de.clientHeight, db.clientHeight) : db.clientHeight;
		
	var scrollLeft =  de.scrollLeft ? de.scrollLeft : db.scrollLeft;
	var scrollTop =  de.scrollTop ? de.scrollTop : db.scrollTop;
	
	//alert(clientWidth+'_'+clientHeight+'_'+scrollLeft+'_'+scrollTop);
	
	var targetWidth = target.style.width ? target.style.width.replace("px","") : target.clientWidth;
	var targetHeight = target.style.height ? target.style.height.replace("px","") : target.clientHeight;
	target.style.left = ((clientWidth-targetWidth)/2+scrollLeft)+"px";
	target.style.top = ((clientHeight-targetHeight)/2+scrollTop)+"px";
}

function alltrim(str){
    // left trim space，使用正则表达式判断
    while (str.substring(0,1).match(/\s/) && str.length>0)
      {
          if (str.length==1)
           {
             str="";
           } 
          else
             str=str.substring(1,str.length);
      }
 
    // right trim space
    while (str.substring(str.length-1,str.length).match(/\s/) && str.length>0)
      {
        if (str.length==1)
          {
            str="";
          }
         else
            str=str.substring(0,str.length-1);
      }
    return str;
}
/******************************************* function.bind()函数 ******************************************/
Function.prototype.bind = function(obj) {
	var method = this,
	temp = function() {
		return method.apply(obj, arguments);
	};
	return temp;
}

/******************************************* prototype ******************************************/
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
/******************************************* 通用Ajax函数 ******************************************/
function Ajax() {
	
	var xmlHttpReq = null;
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();
	} else {
		if (window.ActiveXObject) {
			xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	var handler = null;
	
	this.invoke = function (url, mode, synchro, _handler) {
		handler = _handler;
		xmlHttpReq.open(mode, url, synchro);
		xmlHttpReq.onreadystatechange = this.callback;
		xmlHttpReq.send(null);
	};
	
	this.callback = function () {
		if (xmlHttpReq.readyState == 4) {
			if (xmlHttpReq.status == 200) {
				handler(xmlHttpReq.responseText);
				
			} else {
				alert("请刷新页面！");
			}
		}
	};
}

/******************************************* Cookie操作 ******************************************/
// utility function to retrieve an expiration data in proper format;
function getExpDate(days, hours, minutes)
{
    var expDate = new Date();
    if(typeof(days) == "number" && typeof(hours) == "number" && typeof(hours) == "number")
    {
        expDate.setDate(expDate.getDate() + parseInt(days));
        expDate.setHours(expDate.getHours() + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
        return expDate.toGMTString();
    }
}

//utility function called by getCookie()
function getCookieVal(offset)
{
    var endstr = document.cookie.indexOf(";", offset);
    if(endstr == -1)
    {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

// primary function to retrieve cookie by name
function getCookie(name) 
{ 
    m=document.cookie; 
    re1=new   RegExp("(?!w)"+name+"=[^;]+",""); 
    re2=new   RegExp("^"+name+"=",""); 
    try 
    {    
       var a=m.match(re1)[0];
    }
    catch(e) 
    { 
       return null 
    } 
    var o=a.replace(re2,"").replace(/&/g,"',").replace(/=/g,":'"); 
    return unescape(o); 
}

// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
/*
var time = new date(); 
time.setTime(time.getTime + 24 * 3600 * 1000); //过期时间为24小时. 
document.cookie = "sName = " + escape(sValue) + "; expires= " + time.toGMTString();
*/

// remove the cookie by setting ancient expiration date
function deleteCookie(name,path,domain) 
{
    if(getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/******************************************* 通用事件函数 ******************************************/
var Event = new Object();
Event.history = new Array();
Event.apply = function(handler, args){
	return function(evt){
		Event.event = evt ? evt : window.event;
		handler.apply(null, args);
	}
}
Event.observe = function(target, eventType, handler){
	var args = [];
	for ( var i = 3; i < arguments.length; i++ ) {
		args.push(arguments[i]);
	}
	if(target.attachEvent){
		target.attachEvent("on"+eventType, Event.apply(handler, args));
	}
	else if(target.addEventListener){
		target.addEventListener(eventType, Event.apply(handler, args), false);
	}
}
Event.disObserve = function(target, eventType, handler){
	if(target.detachEvent){
        target.detachEvent("on"+eventType, handler); 
    }
    else if(target.removeEventListener){ 
        target.removeEventListener("on"+eventType, handler, true); 
    }	
}

/*********************************** 通用拖拽方法  ***********************************/

// 用到了  staticZindex

var Drag = function(){
	
	var _X = 0;
	var _Y = 0;	
	var _target = null;
	var downFlag = 0;

	//鼠标按下时的处理函数
	var _handleDown = function(targetId){
		
		var evt = Event.event;
		downFlag = 1;
		_target = $(targetId);
		//alert(_target.id);
		if(_target != null){
			if(document.all){
				_target.setCapture();
				_X = evt.screenX - _target.style.pixelLeft;
				_Y = evt.screenY - _target.style.pixelTop;
			}
			else if(window.captureEvents){
				window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
				_X = evt.clientX - parseInt(_target.style.left);
				_Y = evt.clientY - parseInt(_target.style.top);
			}
			
			_target.style.zIndex = ++staticZindex;
			
			if(typeof _handleMove == "function"){
				Event.observe(document, "mousemove", _handleMove);
			}
	
			if(typeof _handleUp == "function"){
				Event.observe(document, "mouseup", _handleUp, targetId);
			}
		}
	}

	//鼠标移动时的处理函数
	var _handleMove = function(){	
		if ( downFlag == 1)
		{
			var evt = Event.event;
			if(_target != null){
				if(document.all){
					_target.style.left = evt.screenX - _X;
					_target.style.top = evt.screenY - _Y;
				}
				else if(window.captureEvents){
					_target.style.left = evt.clientX - _X + "px";
					_target.style.top  = evt.clientY - _Y + "px";
				}
			}
		}	
	}

	//鼠标松开时的处理函数
	var _handleUp = function(targetId){	
		var evt = Event.event;
		downFlag = 0;
		_target = $(targetId);
		if(_target != null){
			if(document.all){
				_target.releaseCapture();
			}
			else if(window.captureEvents){
				window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			}
			
			Event.disObserve(document, "mousemove", _handleMove);
			Event.disObserve(document, "mouseup", _handleUp);
		 }
	}

	this.makeDraggable = function(targetId){
		if(typeof _handleDown == "function"){
			//alert($(targetId));
			Event.observe($(targetId), "mousedown", _handleDown, targetId);
		}
		$(targetId).style.zIndex = ++staticZindex;
	}
}

/*********************************** 通用遮罩方法  ***********************************/

// 用到了 staticZindex

var Cover = function(){
	
	var _target = null;
	
	var doc = getTopWindow().document;
	
	var _createCover = function(zIndex){
		
		var coverDiv = doc.createElement("div");
		coverDiv.setAttribute("id", "coverDiv");
		var windowWidth = document.body.clientWidth-10 ;
		var windowHeight = document.body.clientHeight ;
		coverDiv.style.cssText = "position:absolute;background-color:#000;filter:alpha(Opacity=10);opacity: 0.10; left:0; top:0; width:"+windowWidth+"px; height:"+windowHeight+"px; z-index:"+zIndex+";";	
		coverDiv.innerHTML = "<iframe id='coverFrame' src='' scrolling='no' frameborder='0' style='z-index:-1;position:absolute;background-color:#000;filter:alpha(Opacity=10);opacity: 0.10;; top:0px; left:0px;width:"+windowWidth+"px;height:"+windowHeight+";'></iframe>";
		doc.body.appendChild(coverDiv);
		
		setTargetXY(_target);
	}
	
	var _closeCover = function(){
		var coverDiv = doc.getElementById("coverDiv");
		coverDiv.parentNode.removeChild(coverDiv);
	}
	
	var _setCover = function(){	
		var windowWidth = document.body.scrollWidth ;
		var windowHeight = document.body.scrollHeight ;
		var cover = document.getElementById("coverDiv");
		if(!cover) return;
		cover.style.width = windowWidth;
		cover.style.height = windowHeight;
		
		var coverFrame = document.getElementById("coverFrame");
		coverFrame.style.width = windowWidth+"px";
		coverFrame.style.height = windowHeight+"px";
		
		// 改变_target的位置
		setTargetXY(_target);
	}
	
	this.makeCover = function(targetId){
		
		//staticZindex = iLayerMaxNum;
		
		_target = $(targetId);
		
		if ( _target != null ){
			
			_createCover( parseInt(++staticZindex)-1);

			 _target.style.zIndex = ++staticZindex;
			
			if(typeof _setCover == "function"){
				Event.observe(getTopWindow(), "resize", _setCover);
			}
		}
	}
	
	this.removeCover = function(){
		
		_target = null;
		
		_closeCover();
		
		if(typeof _setCover == "function"){
			Event.disObserve(getTopWindow(), "resize", _setCover);
		}
	}
}
/*************************************************************************************/

var Position = function(){
	this.x;
	this.y;
	this.z;
}
function toInt(str){
	var ret=0;
	var mid_num;
	for(var i=0;i<str.length;i++){
		mid_num=str.charAt(i);
		if(mid_num<'0' || mid_num>'9')
		{
			return -1;
		}
		ret=ret*10+(mid_num-'0');
	}
	return ret;
}

function searchMsg(){
	var noStr = alltrim(document.getElementById("scripNo").value);
	var no = parseInt(noStr);
	if(isNaN(no)  || toInt(noStr)== -1){
		alert("字条编号必须为数字");
		return false;
	}else if(no < 1){
		alert("字条编号必须为正整数");
		return false;
	}else if(no > 2147483647){
		alert("对不起，您搜索的字条不存在");
		return false;
	}else{
		if(window.location.href.indexOf("index") == -1){
			return true;
		}
		searchScrip(no);
		return false;
	}
}

function setBodyWidth(){
	//alert(document.body.clientWidth);
	var width = document.body.clientWidth <= 1000 ? "1000px": document.body.clientWidth;
	var wrapper = $("wrapper");
	if( wrapper != null ){
		wrapper.style.width = width;
	}
	//document.getElementById("wrapper").style = width;
	//document.body.setAttribute("clientWidth", width);
}

/*********************************** 通用显示浮动层方法  ***********************************/
//显示层
function explor(id)
{
	$(id).style.display = "block";
	Drag.makeDraggable(id);
	cover.makeCover(id);
}
//显示层
function explorNoDrag(id)
{
	$(id).style.display = "block";
	//Drag.makeDraggable(id);
	cover.makeCover(id);
}
//关闭层
function boxclose(id)
{
	$(id).style.display = "none";
}
//关闭遮罩
function covercolose(id)
{
	cover.removeCover();
	$(id).style.display = "none";
}
/*********************************** Request["name"]  ***********************************/
//用JS的字符串处理函数也是可以得到QueryString中的参数的。
//2005-9-6 
//功能相当于Asp的Request("name"),如Request["name"]
//var Request=new QueryString();    //使用new运算符创建参数对象实例
//如URL为http://www.a.com/a.htm?File=asdasd.asd
//在JS里面使用Request["File"]即可以得到asdasd.asd 
function QueryString()
{
//构造参数对象并初始化
 var name,value='',i;
 var str=location.href.toString().toLowerCase();  //获得浏览器地址栏URL串
 var num=str.indexOf("?")
 str=str.substr(num+1);  //截取“?”后面的参数串
 var arrtmp=str.split("&"); //将各参数分离形成参数数组
 for(i=0;i < arrtmp.length;i++)
 {
  num=arrtmp[i].indexOf("=");
  if(num>0)
  {
   name=arrtmp[i].substring(0,num);//取得参数名称
   value=arrtmp[i].substr(num+1); //取得参数值
   this[name]=value;    //定义对象属性并初始化
  }  
 }  
}
