﻿
/// 
/// PublicFunctions.js
/// 公共方法库
/// 主要放置跟业务无关的公用函数
/// guanxuejun@126.com
/// 
/// 函数列表: 
/// 
/// function changeTrBgColor(HtmlTrObject obj)
/// 说明: 改变选择行的样式，主要用于列表行的点击选择模拟
/// 
/// function setVar(HtmlTrObject obj)
/// 说明: 存储当前被选中的记录的GUID
/// 
/// function showConTextMenu(Integer sKey)
/// 说明: 显示右键菜单方法
/// 
/// function getSelectedList()
/// 说明: 获得选择列表
/// 
/// function optionCheckbox(HtmlCheckbox oChkbox)
/// 说明: 设置复选框全选或者全不选
/// 
/// function selectAll()
/// 说明: 选择所有
/// 
/// function selectCancel()
/// 说明: 取消选择
/// 
/// function selectReverse()
/// 说明: 反向选择
/// 
/// function refreshTreeNode()
/// 说明: 刷新树节点
/// 
/// function refreshCurrentPage()
/// 说明: 刷新当前页
/// 
/// function createDaysOption(Integer iType, Integer iCurrentDay)
/// 说明: 创建客户端日期select组
/// 
/// function resetValue(Integer iType)
/// 说明: 选择年度时设置月份为1日期为1
/// 
/// function keepDecimalPlace(Decimal sValue, Integer iLimit)
/// 说明: 根据设置保留小数位
/// 




/// 
///	改变list行的背景色
///	思路：每次变色这里就存储本次变色的对象
///	并实现当前对象的color变色
///	每次当前对象变色就同时将上个对象的color设置为默认值
///
/// 临时存放上一个对象
var tempObj = null;
var tempClassName = "";
function changeTrBgColor(obj)
{
	if (tempObj != null) tempObj.className = tempClassName;
	if (obj.name == "dataTR")
	{
		tempClassName = obj.getAttribute("sourceClass");
		if (tempClassName==null||tempClassName==undefined) tempClassName="";
		obj.className = "trClick";
		tempObj = obj;
	}	
}

/// 
/// 全局变量 用来存储当前被选中的记录的GUID 在执行右键方法的时候再调用它
/// 
/// 
var sTempId = "";
function setVar(obj)
{
	sTempId = obj.getAttribute("guid");
}


/// 
/// 显示右键菜单方法
/// 
/// 
/// var oConTextMenu = "";
function showConTextMenu(sKey)
{
	var oContextMenu = document.getElementById('conTextMenuBody');

	if (sKey == 0) 
	{
		oContextMenu.style.visibility = "hidden";
	}
	else if (sKey == 1) 
	{
		var iContextmenuWidth  = oContextMenu.offsetWidth;
		var iContextmenuHeight = oContextMenu.offsetHeight;
		
		var iBodyWidth		= document.body.clientWidth;
		var iBodyHeight		= document.body.clientHeight;
		var iEventClientX	= event.clientX - 1;
		var iEventClientY	= event.clientY - 1 + document.body.scrollTop;

		if(iBodyWidth < (iContextmenuWidth + iEventClientX)) iEventClientX = iEventClientX - iContextmenuWidth;
		if(iBodyHeight < (iContextmenuHeight + iEventClientY - document.body.scrollTop)) iEventClientY = iEventClientY - iContextmenuHeight + 5;

		oContextMenu.style.left		= iEventClientX;
		oContextMenu.style.top		= iEventClientY;
		oContextMenu.style.visibility = "visible";
	}
}


/// 
/// 获得选择列表
/// 
/// 
var ChkboxName = "chkboxInfoId";
function getSelectedList()
{
	var sIDList;
	sIDList = "";
	var CheckBoxs, CheckBoxItem;
	CheckBoxs = window.document.getElementsByName(ChkboxName);
	for(var i=0;i<CheckBoxs.length;i++)
	{
		if(CheckBoxs[i].checked)
		{
			if(sIDList=="") 
				sIDList = "'" +CheckBoxs[i].value +"'";
			else
				sIDList = sIDList +"," +"'" +CheckBoxs[i].value +"'";
		}
	}
	return sIDList;
}

/// 
/// 获得选择列表2
/// 
/// 
function getSelectedIdList()
{
	var sIDList;
	sIDList = "";
	var CheckBoxs, CheckBoxItem;
	CheckBoxs = window.document.getElementsByName(ChkboxName);
	for(var i=0;i<CheckBoxs.length;i++)
	{
		if(CheckBoxs[i].checked)
		{
			if(sIDList=="") 
				sIDList = CheckBoxs[i].value;
			else
				sIDList = sIDList +"," +CheckBoxs[i].value;
		}
	}
	return sIDList;
}


/// 
/// 设置复选框全选或者全不选
/// Public Viod function optionCheckbox(Object oChkbox)
/// 
function optionCheckbox(oChkbox)
{
	if (oChkbox == null) return;
	try
	{
		if (oChkbox.checked == true)
		{
			selectAll();
		}
		else
		{
			selectCancel();
		}
	}
	catch(e){}
}

/// 
/// 选择所有
/// 
function selectAll()
{
	var CheckBoxs = window.document.getElementsByName(ChkboxName);
	for(var i=0;i<CheckBoxs.length;i++) CheckBoxs[i].checked = true;
}

/// 
/// 取消选择
/// 
function selectCancel()
{
	var CheckBoxs = window.document.getElementsByName(ChkboxName);
	for(var i=0;i<CheckBoxs.length;i++) CheckBoxs[i].checked = false;
}

/// 
/// 反向选择
/// 
function selectReverse()
{
	var CheckBoxs = window.document.getElementsByName(ChkboxName);
	for(var i=0;i<CheckBoxs.length;i++)
	{
		if(CheckBoxs[i].checked)
			CheckBoxs[i].checked = false;
		else
			CheckBoxs[i].checked = true;
	}
}


/// 
/// 刷新树节点
/// 
function refreshTreeNode()
{
	var oRichTree = window.parent.oRichTree;
	var oCurrentNode = oRichTree.getCurrentNode();
	var sNodeId = oCurrentNode.getAttribute("id");
	var sNodeSrc = oCurrentNode.getAttribute("src");
	oRichTree.loadXmlSrc(sNodeId, sNodeSrc);
}

/// 
/// 刷新当前页
/// 
function refreshCurrentPage()
{
	sTempId = "";
	//window.location.reload();
	if (document.location.href == ""||document.location.href == null)
	{
		document.location.reload(true);
	}
	else
	{
		document.location.reload(document.location.href);
	}
}



/// 
/// 创建客户端单套选择下拉日期select组 开始
/// 需要配合 prototype.js 
/// guanxuejun all rights reserved
/// 2008-07-02
/// 

/// 创建单套选择下拉
/// 不指定 iCurrentDay 则是创建当前日期
function CreateSingleOption(year, month, day)
{
	if (year == ""||year == null)
	{
		var d = new Date();
		year = d.getFullYear();
		month = d.getMonth();//这是实际的月份(0-11)
		day = d.getDate();		
	}
	else
	{
		year = parseInt(year);
		month = parseInt(month);
		day = parseInt(day);
	}
	var max = new Date(year, month+1, 0).getDate();//得到选择月份的最大天数
	if (month == 11) max = new Date(year+1, 0, 0).getDate();//得到选择月份的最大天数

	var iWorking_dYear	= $("iWorking_dYear");
	var iWorking_dMonth	= $("iWorking_dMonth");
	var iWorking_dDay	= $("iWorking_dDay");

	//先删除所有的option
	for(var i=(iWorking_dYear.options.length-1); i>=0; i--) iWorking_dYear.options[i] = null;
	for(var i=(iWorking_dMonth.options.length-1); i>=0; i--) iWorking_dMonth.options[i] = null;
	for(var i=(iWorking_dDay.options.length-1); i>=0; i--) iWorking_dDay.options[i] = null;

	//year
	for(var i=year-50; i<=year; i++)
	{
		var oTempOption		= document.createElement("OPTION");
		oTempOption.text	= i.toString()+"年";
		oTempOption.value	= i;
		iWorking_dYear.options.add(oTempOption);
	}
	iWorking_dYear.value = year;
	//month
	for(var i=0; i<=11; i++)
	{
		var oTempOption		= document.createElement("OPTION");
		oTempOption.text	= (i+1).toString()+"月";
		oTempOption.value	= i;
		iWorking_dMonth.options.add(oTempOption);
	}
	iWorking_dMonth.value = month;
	//day
	for(var i=1; i<=max; i++)
	{
		var oTempOption		= document.createElement("OPTION");
		oTempOption.text	= i.toString()+"日";
		oTempOption.value	= i;
		iWorking_dDay.options.add(oTempOption);
	}
	iWorking_dDay.value = day;
}



/// 设置年度和设置月份时，日期为1
function SetSingleDate(year, month)
{
	if (year == ""||year == null||month == ""||month == null)
	{
		year = $F("iWorking_dYear");
		month = $F("iWorking_dMonth");
	}
	if (year == ""||year == null||month == ""||month == null) return;
	if (isNaN(year)||isNaN(month)) return;
	year = parseInt(year);
	month = parseInt(month);
	var max = new Date(year, month+1, 0).getDate();//得到选择月份的最大天数
	if (month == 11) max = new Date(year+1, 0, 0).getDate();//得到选择月份的最大天数
	var iWorking_dDay = $("iWorking_dDay");
	//先删除所有的option
	for(var i=(iWorking_dDay.options.length-1); i>=0; i--) iWorking_dDay.options[i] = null;
	//day
	for(var i=1; i<=max; i++)
	{
		var oTempOption		= document.createElement("OPTION");
		oTempOption.text	= i.toString()+"日";
		oTempOption.value	= i;
		iWorking_dDay.options.add(oTempOption);
	}
	iWorking_dDay.value = 1;
}

/// 
/// 创建客户端单套选择下拉日期select组 结束
/// 2008-07-02
/// 







/// 
/// 创建客户端日期select组
/// guanxuejun all rights reserved
/// 2004-7-18
/// 

/// 根据实际的天数创建options
function createDaysOption(iType, iCurrentDay)
{
	//得到第一套select
	var oRS_dStartTimeYearSelect	= document.all("RS_dStartTimeYearSelect",0);
	var oRS_dStartTimeMonthSelect	= document.all("RS_dStartTimeMonthSelect",0);
	var oRS_dStartTimeDaySelect		= document.all("RS_dStartTimeDaySelect",0);
	//得到第二套select
	var oRS_dCloseTimeYearSelect	= document.all("RS_dCloseTimeYearSelect",0);
	var oRS_dCloseTimeMonthSelect	= document.all("RS_dCloseTimeMonthSelect",0);
	var oRS_dCloseTimeDaySelect		= document.all("RS_dCloseTimeDaySelect",0);
	
	if (iType == 1)
	{
		//start
		var oTempDate	= new Date(oRS_dStartTimeYearSelect.value, oRS_dStartTimeMonthSelect.value, 0);
		var oOptions	= oRS_dStartTimeDaySelect.options;
	}
	else if (iType == 2)
	{
		//end
		var oTempDate	= new Date(oRS_dCloseTimeYearSelect.value, oRS_dCloseTimeMonthSelect.value, 0);	
		var oOptions	= oRS_dCloseTimeDaySelect.options;		
	}
	
	//得到选择月份的最大天数
	var iMaxDay = oTempDate.getDate();

	//先删除所有的option
	for(var i=(oOptions.length-1); i>=0; i--) oOptions[i] = null;

	//创建指定数量的option
	for(var i=0; i<iMaxDay; i++)
	{
		var oTempOption		= document.createElement("OPTION");
		oTempOption.text	= (i+1)+"日";
		oTempOption.value	= i+1;
		oOptions.add(oTempOption);
	}

	//如果传递参数iCurrentDay，设置selected状态
	if (iCurrentDay == ""||iCurrentDay == null) return;

	if (iType == 1)
	{
		// start
		oRS_dStartTimeDaySelect.selectedIndex = iCurrentDay;
	}
	else if (iType == 2)
	{
		// end
		oRS_dCloseTimeDaySelect.selectedIndex = iCurrentDay;		
	}		
}


/// 选择年度时设置月份为1日期为1
function resetValue(iType)
{
	// 得到第一套select
	var oRS_dStartTimeMonthSelect	= document.all("RS_dStartTimeMonthSelect",0);
	var oRS_dStartTimeDaySelect		= document.all("RS_dStartTimeDaySelect",0);
	// 得到第二套select
	var oRS_dCloseTimeMonthSelect	= document.all("RS_dCloseTimeMonthSelect",0);
	var oRS_dCloseTimeDaySelect		= document.all("RS_dCloseTimeDaySelect",0);
	if (iType == 1)
	{
		// start
		oRS_dStartTimeMonthSelect.value = 1;
		oRS_dStartTimeDaySelect.value	= 1;
	}
	else if (iType == 2)
	{
		// end
		oRS_dCloseTimeMonthSelect.value = 1;
		oRS_dCloseTimeDaySelect.value	= 1;
	}
	createDaysOption(iType);
}



/// 
/// 根据设置保留小数位
/// 例如保留2位小数 先把数字扩大100倍，取整数，然后再除以100得到保留的小数
/// guanxuejun@126.com
/// 
function keepDecimalPlace(sValue, iLimit)
{
	if (isNaN(sValue)) return "给定值不是数值！";
	if (isNaN(iLimit)) return "保留小数位不是数值！";
	if (iLimit < 1) return "保留小数位至少为1位。";
	var sTempValue = sValue;
	iLimit = parseInt(iLimit);
	var iScale = 1;
	for (var i=0; i<iLimit; i++) iScale = iScale*10;
	return parseInt(sTempValue*iScale)/iScale;
}



/// getElementById
function EID(sId)
{
	if (sId == ""||sId == null) return null;
	return document.getElementById(sId);
}

/// getElementsByName
function ESN(sName)
{
	if (sName == ""||sName == null) return null;
	return document.getElementsByName(sName);
}

/// getElementsByTagName
function ESTN(sTagName)
{
	if (sTagName == ""||sTagName == null) return null;
	return document.getElementsByTagName(sTagName);
}


/// 创建一个 XMLHttpRequest 对象(安全可靠的)
function XHR()
{
	var oXmlHttp;
	// 非IE
	try
	{
		oXmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// IE的所有版本
		var sXmlHttpVersion = ["MSXML2.XMLHTTP.6.0", 
			"MSXML2.XMLHTTP.5.0", 
			"MSXML2.XMLHTTP.4.0", 
			"MSXML2.XMLHTTP.3.0", 
			"MSXML2.XMLHTTP", 
			"Microsoft.XMLHTTP"];
		for(var i=0; i<sXmlHttpVersion.length&&!oXmlHttp; i++)
		{
			try{oXmlHttp=new ActiveXObject(sXmlHttpVersion[i]);}catch(e){}
		}
	}
	if(!oXmlHttp)
	{
		alert("Error: Could Not Create the XMLHttpRequest Object!");
		return null;
	}
	else
	{
		return oXmlHttp;
	}
}


// 获取当前运行的文件名(包括扩展名)
// sPage += "document.location.protocol = '"+document.location.protocol+"'　　　\n";
// sPage += "document.location.host = '"+document.location.host+"'　　　\n";
// sPage += "document.location.hostname = '"+document.location.hostname+"'　　　\n";
// sPage += "document.location.href = '"+document.location.href+"'　　　\n";
// sPage += "document.location.pathname = '"+document.location.pathname+"'　　　\n";
// sPage += "document.location.port = '"+document.location.port+"'　　　\n";
// sPage += "document.location.search = '"+document.location.search+"'　　　\n";
// sPage += "document.location.target = "+document.location.target+"　　　\n";
// sPage += "document.location.hash = "+document.location.hash+"　　　\n";
// alert(sPage);
function getCurrentPageFullName()
{
	var sUrl = document.location.pathname;
	var oRex = /\//g;
	var oArray = sUrl.split(oRex);
	if (typeof(oArray) != "object") return "";
	if (oArray.length > 0)
	{
		return oArray[oArray.length-1];
	}
}

/// 获取元素的X，Y坐标 根据不同的参数取不同的值
function getElementPos(oElement, e)
{
	var iPos = 0;
	el = oElement;
	while(el)
	{
		if (e == "x")
		{
			iPos = iPos + el.offsetLeft;
		}
		else if (e == "y")
		{
			iPos = iPos + el.offsetTop;
		}
		else
		{
			return -1;
		}
		el = el.offsetParent;
	}
	return iPos;
}
