/**
 * @author luocp
 * ?2004 ThinkerSoft Corporation. All rights reserved
 * 
 * 系统JavaScript文件，共用JS程序集合
 * 
 **/

//==========================================================
//		系统程序路径
//==========================================================

function system_path()
{
	var path="";
	this.path=path;
	this.UserServlet= path+"/servlet/com.thinkersoft.Servlet";
}
//外部使用的全局变量
systemPath=new system_path();


//设置按入回车键后进入下一行
//list为记录顺序ID的列表
function putEnterToNext(list,ev){	
	try{
		for(var i =0;(i+1) < list.length;i ++){
			var temp = eval(list[i]);
			temp._nextObj = list[i+1];
			//temp.onkeydown = _putEnterToNext;
		}
		if(typeof(ev)!= "undefined"){
			var temp = eval(list[i]);
			temp._nextObj = ev;
		}

	}catch(e){
		alert("putEnterToNext Error:"+e);
		return;
	}
	if(document.body.onkeydown != null){
		_temp_putEnterToNext = document.body.onkeydown;
		document.body.onkeydown = new Function("_temp_putEnterToNext();\n_putEnterToNext();");
	}else{
		document.body.onkeydown = _putEnterToNext;
	}
}
function _putEnterToNext(){
	
	try{
		var obj = event.srcElement;
		//alert(obj.outerHTML)
		if(event.keyCode != 13)return;
		var nextObj = obj._nextObj;
		if(nextObj.indexOf("()") != -1){
			eval(nextObj);
		}else{
			nextObj = eval(nextObj);
			nextObj.focus();
		}
	}catch(e){}
}


var callServerGetTest=false;
document.write("<div id='postdiv' style='display:none'></div>");
if(callServerGetTest==true)
	document.write("<iframe name='buffer' frameborder=1 width=20 height=20></iframe>")
else
	document.write("<iframe name='buffer' frameborder=0 width=0 height=0 style='display:none'></iframe>");

//外部程序用
function callServerGet(program,act,param,name){
	var bufferName=(name == null)?"buffer":name;
	if(typeof(act) == "undefined")alert("访问服务器必须指定动作");
	isTimeout = false;
	//==================
	var tempStr ='<FORM METHOD="post" ACTION="'+ program +'"target="'+bufferName+'" name="posts" style="display:none">\n';
	tempStr += '<input type="hidden" name="action" value="'+act+'">\n'
	var s = new Array;
	param = param.substring(param.indexOf("&")+1,param.length);
	for(var i = 0;param.indexOf("&") != -1;i++)
	{
		s[i] = param.substring(0,param.indexOf("&"));
		param = param.substring(param.indexOf("&")+1,param.length);
	}
	if(param!=""&&typeof(param)!="undefined")s[s.length]=param;
	for(i = 0;i < s.length;i ++)
	{
		tempStr += '<TEXTAREA name="'+s[i].substring(0,s[i].indexOf("="))+'">'+s[i].substring(s[i].indexOf("=")+1,s[i].length)+'</TEXTAREA>\n';
	}
	tempStr += '</FORM>';
	postdiv.innerHTML = tempStr;
	//postdiv.style.top=3000;
	posts.submit();
	//==================
}

//使用get方式访问URL请求target=windowName;
function getModeRequest(target,url,act){
	var nowTime = new Date().getTime();
	var str = url+"?"+act+"&"+nowTime;
	//target.location = str;
	window.open(str,target);
	//document.title = "jsRequest:"+str;
}

document.write("<script id=jsRequestArea src=><"+"/script>");
function jsRequest(url,act){//通过javascript访问get方式
	var nowTime = new Date().getTime();
	var str = url+"?nowTime_time="+nowTime;
	if(act != "")str += "&"+act
	str = replaceAll(str,"+","＋");

	jsRequestArea.src = str;
	//alert(jsRequestArea.src)
	//document.title = "jsRequest:"+str;
}

//表单文本域不能为空//格式new Array("inputObj,alertName",.....)//inputObj例如：form.password;alertName例如：用户密码
function formInputNoNull(list){
	try{

	for(var i =0;i < list.length;i ++){
				
		var temp = list[i].split(",");
		var obj = eval(temp[0]);
		
		if(obj.value == ""){
			alert(temp[1]+"域不能为空");
			obj.focus();
			return false;
		}
	}
	return true;
	}catch(e){alert("formInputNotNull(list) Error:"+e)}
}

function formInputReplace(form,reD){//替换表单中域的reD数组里的所有字符串reD=new Array("a=b","c=d")
	for(var i =0;i < form.all.length;i ++){
		try{
		if(form(i).tagName != "INPUT" && form(i).tagName != "TEXTAREA"){
			continue;
		}

		var value = form(i).value+"";
		if(value == "")
			continue;

		for(var j =0;j < reD.length;j ++){
//				alert(reD[i])

				var reDs = reD[j].split("=");
				value = replaceAll(value,reDs[0],reDs[1]);
		}
		if(value != form(i).value)
			form(i).value = value;
		}catch(e){}
	}
}

//替换window.open()功能，解决<a href=javascript:window.open>会返回对象
function windowOpen(file,target,p){
	if(typeof(p) != "undefined")
		window.open(file,target,p);
	else if(typeof(target) != "undefined")
		window.open(file,target);
	else
		window.open(file);
}


//用window.open实现对话框模式，替换showModalDialog的一些安全模式
var bodyonfocus;
var showDialogWindowObj;
function showDialogWindowPost(Url,Features){
	Features = _setShowDialogWindowFeatures(Features);
	showDialogWindowObj = window.open("about:blank","showDialogWindowObj","status=1,"+Features);
	bodyonfocus = document.body.onfocus;
	var program = Url.split("?");
	callServerGet(program[0],"","&"+program[1],"showDialogWindowObj");
	document.body.onfocus = showDialogWindow_onfocus;

}
function showDialogWindow(Url,Features){
	Features = _setShowDialogWindowFeatures(Features);
	showDialogWindowObj = window.open(Url,"_blank","status=1,"+Features);
	bodyonfocus = document.body.onfocus;
	document.body.onfocus = showDialogWindow_onfocus;
	return showDialogWindowObj;
}
function _setShowDialogWindowFeatures(Features){	//将窗口至中
	var height;
	var width;
	try{
	var a = Features.split(",");
	for(var i = 0;i < a.length;i ++){
		var d = a[i].split("=");
		if(d[0] == "width")
			width = d[1];
		else if(d[0] == "height")
			height = d[1];
	}
	Features += ",top="+(screen.height-height-50)/2+",left="+(screen.width-width-10)/2
		return Features;
	}catch(e){
		alert("showDialogWindow()的Features参数有误，请参照window.open的Features");
		return "";
	}
}
function showDialogWindow_onfocus(){
	try{
		showDialogWindowObj.document.body.focus();
	}catch(e){
		document.body.onfocus = bodyonfocus;
	}
}

function setMaxWindow(){	//最大化窗口
	self.moveTo(-4,-4);
	self.resizeTo(screen.availWidth+8,screen.availHeight+8) ;
}

//实现模拟多窗口操作界面
function multi_windows(name){//类型
	this.name = name;
	this.init = multi_windows_init;
	this.open = insert_window;
	backObj = new Array(1000);//访问对象的顺序
	this.backObj = backObj;
	index = backObj.length-1;
	this.index = index;
	this.show_window = show_window_obj;
	this.close_window = close_window_obj;

}

function multi_windows_init(){//初始化
	var obj = "mwa_"+new Date().getTime();
	document.write("<table width=100% height=100% border=0 cellspacing=0 cellpadding=0 id="+obj+"><tr><td valign=top colspan=3></td></tr><tr style='background-image:url(/images/tr_bg.gif)'><td height=31 width=20 style='cursor:hand'  onmouseover='windowScrollS=true;windowScrollRight(this.parentElement.cells(1).all(0))' onmouseout='windowScrollS=false'><img src=/images/scroll_r.gif></td><td  width=100% noWrap><div style='height:31;overflow:hidden;width:100%' id=s"+obj+"></div></td><td height=31 width=20 align=right onmouseover='windowScrollS=true;windowScrollLeft(this.parentElement.cells(1).all(0))' onmouseout='windowScrollS=false' style='cursor:hand' ><img src=/images/scroll_l.gif></td></tr></table>");
	this.obj = eval(obj);
}
var windowScrollS = false;
function windowScrollLeft(obj){//右移选择栏
	if(!windowScrollS)return;
	obj.scrollLeft += 3;
	setTimeout("windowScrollLeft("+obj.id+")",50);
}
function windowScrollRight(obj){//左移选择栏
	if(!windowScrollS)return;
	obj.scrollLeft -= 3;
	setTimeout("windowScrollRight("+obj.id+")",50);
}
function insert_window(url,text,remark,ico){//新建立窗口

	if(typeof(url) == "undefined" || typeof(text) == "undefined" || typeof(remark) == "undefined"){
		alert("multi_windows_open(url,text,remark,ico).Error()");
	}
	var obj = this.obj.rows(0).cells(0);
	for(var i =0;i < obj.all.length;i ++){
		//alert("|"+obj.all(i).text +"|"+ " "+text+" "+"|")
		if(obj.all(i).text == text){//已经存在窗口，显示此窗口
			this.show_window(text);
			return;
		}
	}
	this.obj.rows(0).cells(0).insertAdjacentHTML("BeforeEnd","<iframe text="+text+" style='display:none' width=100% height=100% src='"+url+"' frameborder=0></iframe>");
	this.obj.rows(1).cells(1).all(0).insertAdjacentHTML("BeforeEnd","<span onclick='"+this.name+".show_window(\""+text+"\")' onmouseover='status=\""+remark+"\"'  onmouseout='status=\"\"' style='font-size:13;cursor:default;	width:113;height:100%;'><span>&nbsp;</span><span style='height:100%;position: relative;top:5'>"+text+"</span><img style='cursor:hand;position: relative;top:-8'title='close'onclick='"+this.name+".close_window(\""+text+"\")' src='/images/close_button.gif'><span>&nbsp;</span></span>");
	this.show_window(text);
}

function show_window_obj(text){//显示窗口
	//设置历史记录
	this.backObj[this.index] = text;
	this.index -- ;
	var st = false;
	//设置选择栏显示
	var obj = this.obj.rows(1).cells(1).all(0);
	for(var i =0;i < obj.all.length;i += 5){
		if(obj.all(i).innerText == " "+text+" "){
			obj.all(i).style.backgroundImage = "url(/images/open_bg.gif)";
			st = true;
		}else{
			obj.all(i).style.backgroundImage = "url(/images/close_bg.gif)";
		}
	}

	//设置窗口显示
	obj = this.obj.rows(0).cells(0);
	for(var i =0;i < obj.all.length;i ++){
		if(obj.all(i).text != text)
			obj.all(i).style.display = "none";
		else
			obj.all(i).style.display = "";
	}
	return st;

}


function close_window_obj(text){//关闭窗口
	//关闭选择栏显示
	var obj = this.obj.rows(1).cells(1).all(0);
	for(var i =0;i < obj.all.length;i += 5){
		if(obj.all(i).innerText == " "+text+" "){
			obj.all(i).outerHTML = "";
		}
	}

	//关闭窗口显示
	obj = this.obj.rows(0).cells(0);
	for(var i =0;i < obj.all.length;i ++){
		if(obj.all(i).text == text)
			obj.all(i).outerHTML = "";
	}

	//显示历史记录
	for(var i =this.index+1;i < this.backObj.length;i ++){
			//alert(this.show_window(text))
			if(this.show_window(backObj[i])){
				return;
			}

	}
}







function getChackboxList(obj){	//获取Chackbox选中的列表
	try{
		obj = eval(obj);
		if(typeof(obj.length) == "undefined"){
			if(obj.checked)
				return new Array(obj.value);
			return new Array;
		}
		
		var tempA = new Array;
		for(var i =0;i < obj.length;i ++){
			if(obj(i).checked)
				tempA[tempA.length] = obj(i).value;
		}
		return tempA;
	}catch(e){alert(e);return new Array;}
}


function getChackboxListValues(obj){	//获取Chackbox选中的列表的参数，解决value="的"会变没掉
	try{
		obj = eval(obj);
		if(typeof(obj.length) == "undefined"){
			if(obj.checked)
				return new Array(obj.values);
			return new Array;
		}
		
		var tempA = new Array;
		for(var i =0;i < obj.length;i ++){
			if(obj(i).checked)
				tempA[tempA.length] = obj(i).values;
		}
		return tempA;
	}catch(e){alert(e);return new Array;}
}


function replaceAll(s,a1,a2){	//替换所有字符串
	s = s+"";
	var rs = ""
	s = s.split(a1);
	for(var i =0;i < s.length;i ++)
		rs += a2+s[i];
	rs = rs.substring(a2.length);
	return rs;
}

function setTextAlt(text,title){//设置文本显示详细信息类似IMG的ALT信息
	return "<span title='"+title+"'>"+text+"</span>";
}


//更新select数据  
function update_select(data,obj)//(数据,select的对象)  
{  
	//清除select里的所有数据  
	clean_select(obj);  
  
	for(var i = 0; i < data.length;i ++)  
	{  
		//往select里输入数据  
		insert_select(data[i][0],data[i][1],i,obj);  
	}  
	  
}  
  
//清空select数据  
function clean_select(obj)  
{  
	while(obj.options.length > 0)  
		obj.remove(0);  
}

//删除options数据  
function delete_select(obj,value)  
{  
	for(var i =0;i < obj.options.length;i ++){
		if(obj.options(i).value == value)
			obj.remove(i);
	}
}  
  
//往select里输入数据  
function insert_select(value,text,index,obj)//(value,text,位置,select的对象)  
{  
  
  
	//创建OPTION对象  
	var temp = document.createElement("OPTION");  
	temp.value = value;  
	temp.text = text;  
	obj.add(temp,index);  
	//obj.options.selectedIndex = obj.options.length-1;  
	obj.options.selectedIndex = index;
  
}  


function timeDateToLack(str){	//去掉时间年前面两位，及秒数
	return str.substring(2,16);
}

function timeDateToLack1(str){	//去掉时间年，及秒数
	return str.substring(5,16);
}

function setFocalize(size,text){//获得size长度的字符串
	var obj = document.createElement("span");
	obj.innerHTML = text;
	obj.style.position = "absolute";
	obj.style.top = "-1000";
	document.body.appendChild(obj);

	while(obj.offsetWidth > size-15){//截短字符串使之减少到长度可以容忍的大小
		var tt = obj.innerHTML;
		obj.innerHTML = tt.substring(0,tt.length-2);
	}
	if(obj.innerHTML == text){
		obj.outerHTML = "";
		return text;
	}
	

	var str = "<span title='"+text+"'>"+obj.innerHTML+"...</span>";
	obj.outerHTML = "";
	return str

}

function setFocalizeWrite(size,text){//获得size长度的字符串并document.write
	document.write(setFocalize(size,text));
}


function checkInputData(obj,type,noalert){//检测数据类型是否正确
	var fi = 1;
	if(obj.length>1)fi = obj.length;
	for(var i =0;i < fi;i++){
		var to;
		if(fi == 1)to = obj;
		else to = obj(i);
		if(type.toUpperCase() == "NUMBER"){
			if(to.value*0 != 0){
				if(!noalert){
					alert("输入内容必须为数字");
					to.select();
				}
				return false;
			}
		}else if(type.toUpperCase() == "NONULL"){
			if(to.value == ""){
				if(!noalert){
					alert("不能为空，必须输入内容");
					to.select();
				}
				return false;
			}
		}else if(type.toUpperCase() == "INT"){
			if(!checkInputData(obj,"number"))return false;
			if(to.value.indexOf(".") != -1){
				if(!noalert){
					alert("只能输入整数");
					to.select();
				}
				return false;
			}
		}
	}
	return true;
}


//待修改
function setSelectInput(obj){//设置选择列表能输入汉字
	try{
		for(var i =0;i < obj.length;i ++){
			var to = eval(obj[i]);
			if(to.tagName != "SELECT"){
				alert(obj[i]+" cannot be resolved");
				return;
			}
			to.onmousedown = new Function("_setSelectInput();");
		}

	}catch(e){
		alert("setSelectInput() Error:"+e)
	}
}
document.write("<input type=text style='position: absolute;border-width: 0;border-bottom-style: solid;display:none' id=setSelectInput_obj onblur='_setSelectInputObj.style.display = \"\";setSelectInput_obj.style.display = \"none\";'>");
_setSelectInputObj = null;
function _setSelectInput(){
	var obj = event.srcElement;
	if(_setSelectInputObj != null)_setSelectInputObj.style.display = "";
	_setSelectInputObj = obj;
	var posRect = obj.getClientRects()(0);
	var top = posRect.top+document.body.scrollTop;
	var bottom = posRect.bottom+document.body.scrollTop;
	var left = posRect.left;
	var right = posRect.right;
	setSelectInput_obj.style.top = top;
	setSelectInput_obj.style.left = left;
	setSelectInput_obj.style.width = right - left - 20;
	setSelectInput_obj.style.height = bottom - top;
	setSelectInput_obj.style.display = "";
	setSelectInput_obj.value = _setSelectInputObj.value;
	setSelectInput_obj.select();
	_setSelectInputObj.style.display = "none";
	//document.title = bottom + "|" + left;
}


function  get_cookie(Name)  {
	var  search  =  Name  +  "="
	var  returnvalue  =  "";
	if  (document.cookie.length  >  0)  {
		offset  =  document.cookie.indexOf(search)
		if  (offset  !=  -1)  {
			offset  +=  search.length
			end  =  document.cookie.indexOf(";",  offset);
			if  (end  ==  -1)
				end  =  document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset,  end))
		}
	}
	return  returnvalue;
}

function operation(a,b,c){//加减运算 解决浮点运算的问题
	if(b != "+" && b != "-")return alert("operation 函数只能解决加减运算");
	a = parseInt(a * 10000000);
	c = parseInt(c * 10000000);
	return Number(eval(a+b+c))/10000000;
}

function roundToN(x, N){//解决浮点运算的问题
    var K = 1;
    for ( var i = 0; i < N; i ++ )
        K *= 10;        // K = 10^N
    x *= K;            // 放大 K 倍
    x = Math.round(x); // 整数圆整
    return Number(x / K);    // 缩小 K 倍
}

document.writeln("<script src='"+systemPath.path+"/js/sys_tree.js'></"+"script>");
document.writeln("<script src='"+systemPath.path+"/js/sys_list.js'></"+"script>");
