﻿
/******************************************
*
* 本文件为本站所有js文件的父文件
* Power By LiShilin@Gmail.com	2008-04-30
*
* Code Rework By LiShilin@Gmail.com  2008-06-25
*
******************************************/

/************************
	获取页面对象
************************/
//根据id获取页面对象
function $(objID)
{
	return document.getElementById(objID);
}
//根据name获取页面对象数组
function $Name(objsName)
{
	return document.getElementsByName(objsName);
}
//根据tagname获取页面对象数组
function $TagName(objsTagName)
{
	return document.getElementsByTagName(objsTagName);
}
//获取下拉控件select的当前选定项的值
function $DropGetValue(objID)
{
	var obj = $(objID);
	return obj.options[obj.selectedIndex].value;
}
//获取下拉控件select的当前选定项的文本
function $DropGetText(objID)
{
	var obj = $(objID);
	return obj.options[obj.selectedIndex].text;
}
//按值设置下拉控件select的当前选定项
function $DropSetByValue(dropID,value)
{
	var dropobj = $(dropID);
	var list = dropobj.options;
	for(var i=0;i<list.length;i++)
	{
		dropobj[i].selected = (dropobj[i].value.toLowerCase() == value.toLowerCase()) ? true : false;
	}
}
//按文本设置下拉控件select的当前选定项
function $DropSetByText(dropID,text)
{
	var dropObj = $(dropID);
	var list = dropObj.options;
	for(var i=0;i<list.length;i++)
	{
		dropObj[i].selected = (dropObj[i].text.toLowerCase() == text.toLowerCase()) ? true : false;
	}
}
/*****************************************
	字符串去除头尾空格
*****************************************/
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, ""); 
} 
/*******************************
   限制键盘输入
*******************************/
//限制不允许输入除0～9之外的所有字符
function KeyPress(objTR)
{	
	var txtval=objTR.value;		
	
	var key = event.keyCode;

	if((key < 48||key > 57)&&key != 46)
	{		
		event.keyCode = 0;
	}
	else
	{
		if(key == 46)
		{
			event.keyCode = 0;
		}
	}
}
//限制只允许输入0~9及小数点
function KeyPressPoint(objTR)
{	
	var txtval=objTR.value;		
	
	var key = event.keyCode;

	if((key < 48||key > 57)&&key != 46)
	{		
		event.keyCode = 0;
	}
	else
	{
		if(key == 46)
		{
			if((objTR.value.length == 0) || (objTR.value.indexOf('.') >= 1))
			{
				event.keyCode = 0;
			}
		}
	}
}
//限制只能输入数字和横线(-)
function KeyPressLine(objTR)
{	
	var txtval=objTR.value;
	
	var key = event.keyCode;

	if((key < 48||key > 57)&&(key != 45))
	{		
		event.keyCode = 0;
	}
}

//限制不允许输入'和"
function KeyPressYin(objTR)
{
	var key = event.keyCode;
	
	if(key == 34||key == 39)
	{		
		event.keyCode = 0;
	}
}
/**********************************************
	页面跳转
**********************************************/
//跳到本页并且去除url参数
function RefreshMe()
{
	location.href(location.pathname);
}
//跳转到本页，不去除参数
function LocationToMe()
{
	location.href(location.href);
}
//跳转到本页，并删除指定的参数
//参数：param:应为匹配指定参数的js正则表达式字符串
function RefreshMeDelParam(param)
{
	var url = location.href;
	var reg = new RegExp(param,"ig");
	var x = url.replace(reg,"");
	LocationTo(x);
}
//跳到指定页
function LocationTo(url)
{
	location.href(url);
}
//顶部窗口跳转
function TopLocationTo(url)
{
	top.location.href(url);
}
//跳转到本页，并添加参数
function LocationToMeParam(param)
{
	location.href(SetUrlAddParam(location.href,param));
}
//提交页面
function SubmitToMeParam(url)
{
	document.forms[0].action = url;
	document.forms[0].submit();
}

//打开窗口的js类，默认打开空页面
function NewWindow()
{
	this.Url = "about:blank";
	this.Name = "_blank";
	this.Height = Math.round(window.screen.height / 2);
	this.Width = Math.round(window.screen.width / 2);
	this.ToolBar = "yes";
	this.MenuBar = "yes";
	this.ScrollBars = "yes";
	this.Location = "yes";
	this.Status = "yes";
	this.Resizable = "yes";
	this.Left = Math.round(((window.screen.availWidth-this.Width)/2)/2);
	this.Top = Math.round(((window.screen.availHeight-this.Height)/2)/2);
}
NewWindow.prototype.Open = function()
{
	window.open(this.Url,this.Name,'height='+this.Height+',width='+this.Width+',toolbar=' + this.ToolBar + ',menubar=' + this.MenuBar + ',scrollbars=' + this.ScrollBars + ',resizable=' + this.Resizable + ',location=' + this.Location + ',status=' + this.Status + ',left='+ this.Left +',top='+ this.Top +'');
}
//打开一个没有任何限制的窗口
function OpenUrl(url)
{
	var win = new NewWindow();
	win.Url = url;
	win.Open();
}
//打开一个所有操作工具条都没有的窗口
function OpenUrlNotAll(url,width,height)
{
	if(url == null || url == "")
		url = "/";
	if(width == null || width == 0)
		width = Math.round(window.screen.width / 2);
	if(height == null || height == 0)
		height = Math.round(window.screen.height / 2);
		
	var win = new NewWindow();
	win.Url = url;
	win.Height=height;
	win.Width = width;
	win.ToolBar="no";
	win.MenuBar = "no";
	win.ScrollBars = "auto";
	win.Location = "no";
	win.Status = "no";
	win.Resizable = "yes";
	win.Left = Math.round((window.screen.availWidth-win.Width)/2);
	win.Top = Math.round((window.screen.availHeight-win.Height)/2);
	win.Open();
}
/***************************************
	异步操作
***************************************/
function XmlHttp()
{
	//xmlhttprequest对象
	this.xmlHttpObj =  function(){
		try
		{
			return new XMLHttpRequest();
		}
		catch(x)
		{
			try
			{
				return new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(xx)
			{
				try
				{
					return new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(xxx)
				{
					return null;
				}
			}
		}
	};
	//要获取数据的地址
	this.url = "";
	//处理数据的函数名
	this.handler = null;
};
XmlHttp.prototype.GetXMLData = function(){
	if(this.xmlHttpObj == null)return false;
	if(this.url == "") return false;
	if(this.handler == null) return false;
	if(this.url.indexOf("?") > 0)
	{
		this.url += "&x=" + Math.random();
	}
	else
	{
		this.url += "?x=" + Math.random();
	}
	try
	{
		this.xmlHttpObj.open("get",this.url,true);
		this.xmlHttpObj.setRequestHeader("Content-Type","text/xml");
		this.xmlHttpObj.onreadystatechange = this.handler;
		this.xmlHttpObj.send();
	}
	catch(x)
	{
		return false;
	}
}
/**********************************************
	url操作
**********************************************/
//获取url中一个指定的参数值
function GetRequest(paramName,defaultValue)
{
	var search = paramName + "=";
	var FieldValue="";
	var URL=location.href;
	var offset = URL.indexOf(search);
	if (offset != -1)
	{ 
	  offset += search.length;
	  var end = URL.indexOf("&", offset);
	  if (end == -1)
	  { 
		FieldValue=URL.substring(offset);
	  }
	  else
	  {
		FieldValue=URL.substring(offset,end);
	  }
	}
	if(FieldValue == "")
	{
		FieldValue = defaultValue;
	}
	return FieldValue.toLowerCase();
}
//为url添加参数，自动判断是更新还是添加
//参数格式：如：page=1的形式，不能是“page=1&params=1”的形式，即一次只能添加一个参数
function SetUrlAddParam(url,param)
{
	var interrogation = url.indexOf("?");

	if (interrogation == -1)
	{
		url+="?"+param;
	}
	else
	{
		//3\如果?后面有查询字符串,则检测有没有该字段，如果有，则重新付值
		var fp = param.split("=");
		var search = fp[0] + "=";
		var offset = url.indexOf(search);
		if (offset != -1)
		{ 
		  offset += search.length;
		  end = url.indexOf("&", offset);
		  if (end == -1)
		  { 
		  	url=url.substring(0,offset)+fp[1];
		  }
		  else
		  {
		  	url=url.substring(0,offset)+fp[1]+url.substring(end);
		  }
		}
		else
		{
			url=url+"&"+param;
		}
	}
	return url;
}
/**************************************************
	可用于显示页面进度条的隐藏层操作
**************************************************/
//创建一个浮动的div，未指定宽高度及位置，指定部分样式
function CreateStateDiv(divID)
{
	var div = document.createElement("div");
	div.id = divID;
	div.style.display = "none";
	div.style.position = "absolute";
	div.style.zIndex = "100000";
	div.style.border = "1px solid #cccccc";
	div.style.backgroundColor = "#ffffff";
	return div;
}
//进度条操作：关闭
function HiddenState(divID)
{
	if($(divID)!= null)
		$(divID).style.display = "none";
	if(timer != null)window.clearTimeout(timer);
}
//进度条操作：显示信息，并延时关闭
var timer = null;
function ShowMsg(divID,text,time)
{
	$(divID).innerHTML = text;
	$(divID).style.top = (document.documentElement.scrollTop+(document.documentElement.clientHeight-$(divID).offsetHeight)/2)+"px";
	$(divID).style.left = (document.documentElement.scrollLeft+(document.documentElement.clientWidth-$(divID).offsetWidth)/2)+"px";
	timer = window.setTimeout("HiddenState('"+divID+"')",time);
}
//延时将某个对象的显示设置为不显示
function HiddenObjTimer(objid,time)
{
	if($(objid) != null)
		timer = window.setTimeout("$('" + objid + "').style.display='none';if(timer!=null)window.clearTimeout(timer);",time);
}
/*************************************************
	获取某个元素在页面上的坐标
*************************************************/
//获取元素的纵坐标 
//e:要获取的元素
function GetTop(e)
{
	var offset=e.offsetTop; 
	if(e.offsetParent!=null) 
		offset+=GetTop(e.offsetParent); 
	return offset; 
} 
//获取元素的横坐标 
//e:要获取的元素
function GetLeft(e)
{
	var offset=e.offsetLeft; 
	if(e.offsetParent!=null)
		offset+=GetLeft(e.offsetParent); 
	return offset; 
}
