/*
// CommonScriptLib.js  :  This is a common script library;
// Author: linde
// E-mail:changling@itownet.cn
// Date:2004-06-24

//本js文件包括:
//公用的方法

//浏览器操作
BrowserInfo						//--定义了一个关于浏览器的类

//cookie操作
readCookie(name)				//--返回cookie的值，name是cookie的名字
writeCookie(name, value, hours)	//--

//层操作
showHideLayers(...)				//--显示或者隐藏层，Example: showHideLayers(Layer1,'','show',Layer2,'','hide');

//查找对象
findObj(obj)					//--查找对象，可以是table、tr、td等的id，也可以是表单对象的name 

//checkbox操作


//select(list)操作
selectAll(list)                 //--选中一个多选的select（list）中的所有项，list是该select的name
deselectAll(list)               //--删除一个多选的select（list）中的所有项，list是该select的name
GetCurrentListValues(list)		//--获得一个多选的select（list）中当前选中的项，list是该select的name	
AddItemToList(list,string)		//--向多选的select（list）中增加一个string项，string是要增加的字符串，list是该select的name
DelItemFromList(list)			//--删除多选的select（list）中当前选中的项
list1TOlist2(list2,list1)		//--将多选的select（list1）中的某些选中的项转移到另一个多选的select（list2）中，list1为源，list2是目的

//本函数适用于弹出checkbox组或者radiobutton组的静态页面的onload事件
//用法：<body onLoad="setCheckOrRadioGroup(window)">
function setCheckOrRadioGroup(chkObj)

//本函数适用于弹出checkbox组或者radiobutton组的静态页面的带回选定的值并关闭窗口
//用法：<input type="button" onClick="getCheckOrRadioGroup(window)">
function getCheckOrRadioGroup(chkObj)

//弹出窗口操作
PopupWindow(urlStr,X,Y)			//--弹出窗口，urlStr为弹出的页面的url，X为窗口的width，Y为窗口的height
showPopup(urlStr,objStr)		//--用于弹出查询窗口urlStr,并要求该弹出窗口向原来的主窗口中返回一个值到objStr对象中,
closeWin(dataStr)				//--返回一个值,并关闭该窗口.
showPopup_line(urlStr,lineNO)	//--用于弹出查询窗口urlStr,并要求该弹出窗口向原来的主窗口中返回一个值到objStr对象中,
closeWin_line(dataStr)			//--返回一个值,并关闭该窗口.

//其他
collapseORexpand()              //--第一个参数是点击对象图片(this),其他参数为要显示或者是隐藏的table的ID;//Example：collapseORexpand(this,tablID)。

*删除表格中的行，表格必须有ID，并且每行必须有radiobutton来指定操作那一行
*tableID -- 表格的ID，字符串
*radioName -- radio的名字,不能用ID，字符串
*返回删除的行数
* 使用方法： onClick="deleteTableRow('tb1','myradio') "; 

function deleteTableRow(tableID,radioName)
function confirmBeforeDelete(aTR)
*/
//-----------------------------------browser--------------------------------------------
function BrowserInfo()
{
  this.name = navigator.appName;
  this.codename = navigator.appCodeName;
  this.version = navigator.appVersion.substring(0,4);
  this.platform = navigator.platform;
  this.javaEnabled = navigator.javaEnabled();
  this.screenWidth = screen.width;
  this.screenHeight = screen.height;
}
/*
//du qu cookie
// Example:
// alert( readCookie("myCookie") );

//对cookie的读写操作
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  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;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}
//---------xie ru cookie
// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}              
 */

//-------xian shi he ying cang ceng
// * Dependencies * 
// this function requires the following snippets:
// JavaScript/readable_MM_functions/findObj
//
// Accepts a variable number of arguments, in triplets as follows:
// arg 1: simple name of a layer object, such as "Layer1"
// arg 2: ignored (for backward compatibility)
// arg 3: 'hide' or 'show'
// repeat...
// Example: showHideLayers(Layer1,'','show',Layer2,'','hide');
function showHideLayers()
{ 
  var i, visStr, obj, args = showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3)
  {
    if ((obj = findObj(args[i])) != null)
    {
      visStr = args[i+2];
      if (obj.style)
      {
        obj = obj.style;
        if(visStr == 'show') visStr = 'visible';
        else if(visStr == 'hide') visStr = 'hidden';
      }
      obj.visibility = visStr;
    }
  }
}
		

//--------------------------------------------------------------------------------------
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}
//------------------------------------------------------------------------------------
function selectAll(list)
{
	for(var i = 0;i < list.length;i++)
	{
		list.options[i].selected = true;
	}
}
function deselectAll(list)
{
	for(var i = 0;i < list.length;i++)
	{
		list.options[i].selected = false;
	}
}
//------get select item from select(multiple)-------------------------------------------
function GetCurrentListValues(list)  //return the item's value
{
	var string="";			
	for(var i=0;i<list.length;i++)
	{
		if(list.options[i].selected==true)
		{ 
		string += list.options[i].text+",";
		}
	}
	return string;				
	
}
//-------add a item to a list(select multiple)------------------------------------------
function AddItemToList(list,string)
{
	var listlen = list.length;
	var strarray = string.split(",");
	for(var i=0;i<strarray.length;i++)
	{
		list.options[listlen+i] = new Option(strarray[i]);
	}
	list.options[listlen+i-1] = null;
}
function DelItemFromList(list)
{
	for(var i=0;i<list.length;i++)
	{
		if(list.options[i].selected) list.options[i] = null;		
	}
	
}
//将list1中的一项转移到list2中
//list1、list2是select对象
function list1TOlist2(list1,list2) 
{
	
    list1len = list1.length ;
    for ( i=0; i<list1len ; i++)
	{
        if (list1.options[i].selected == true ) 
	{
            list2len = list2.length;
            list2.options[list2len]= new Option(list1.options[i].text);
        }
    }
	for ( i=(list1len-1); i>=0; i--) 
	{
        if (list1.options[i].selected == true ) 
		{
            list1.options[i] = null;
        }
    }
}
//------------------------------------------------------------------------------------------
//本函数适用于弹出checkbox组或者radiobutton组的静态页面的onload事件
//用法：<body onLoad="setCheckOrRadioGroup(window)">
function setCheckOrRadioGroup(chkObj)
{
	if(typeof(window.opener.OperateObj)=="undefined")
	{
		return false;
	}
	var operateObj = window.opener.OperateObj;
	chkStr = operateObj.value.split(",");	
	var chklen = chkObj.length;
	for(var i=0;i<chklen;i++)
	{
		for(var j=0;j<chkStr.length;j++)
		{
			if(chkObj[i].alt == chkStr[j])
			{
				chkObj[i].checked = true;
			}
		}
	}
	
	
}
//本函数适用于弹出checkbox组或者radiobutton组的静态页面的带回选定的值并关闭窗口
//用法：<input type="button" onClick="getCheckOrRadioGroup(window)">
function getCheckOrRadioGroup(chkObj)
{
	if(typeof(window.opener.OperateObj)=="undefined")
	{
		return false;
	}
	
	var operateObj = window.opener.OperateObj;
	var chklen = chkObj.length;
	var returnValue = "";
	for(var i=0;i<chklen;i++)
	{
		if(chkObj[i].checked)
		{
			returnValue += chkObj[i].alt +",";
		}
	}
	operateObj.value = returnValue;
	window.close();
}

//--=========showPopup将弹出的query或detail页面的内容回写到主页面的某个对象(输入框、文本框)中=========
function showPopup(urlStr,objStr,X,Y)
{
PopupWindow(urlStr,X,Y);
OperateObj = objStr;
}
//------------------------------------------------------------------------------------------
function PopupWindow(urlStr,X,Y)    
{
//将弹出的窗口定位于父窗口的正中位置.
// 参数:urlStr是链接页面的url;
//X是该弹出窗口的width;
//Y是该弹出窗口的height.

var pos_X = (screen.width)?(screen.width-X)/2:100;
var pos_Y= (screen.height)?(screen.height-Y)/2-50:100;
var winpos = "left="+pos_X+",top="+pos_Y+",width="+X+",height="+Y;
window.open(urlStr, "Popup", "fullscreen=0,center=0,directories=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0,"+winpos);
}

//-----------***********-----------------------(changling)
function closeWin(dataStr)
{//将组合起来的字符串写回文本框对象，并关闭弹出的窗口。
 var valObj = window.opener.OperateObj;
  if(dataStr!='')
  {   
    valObj.value= dataStr;    
  }
 window.close();
}
//------------------------------------------------------------------------------------------
function showPopup_line(urlStr,lineNO)
{
PopupWindow(urlStr,550,380);
try{
 Operateline = lineNO;
}
catch(e)
{
  Alert("JavaScript 错误!");
}
}
//-----------------------------------------------write data that's in a line of a Popup-window to a td 
function closeWin_line(dataStr)
{
	var ol = window.opener.Operateline;	
  	if(ol>-1&&dataStr!='')
  	{
  		if(navigator.appName.indexOf('Microsoft')==-1)
  		{
   			alert("您所使用的浏览器不支持此功能！请安装IE5.0及以上版本的浏览器！");
  		}
  		else
  		{
   			var objStr = window.opener.listTD[ol+1];
   			objStr.innerText = dataStr;
  		}     
  	}
 	window.close(); 
}
//------------==========================================----------------------
//折叠释放函数
//第二个参数点击对象图片
//其他参数为要显示或者是隐藏的table ID
//example : collapseORexpand(this,tablID)
function collapseORexpand()
{
	args = collapseORexpand.arguments;
	obj= args[0];
	argslen = args.length;
	if(obj.src .indexOf("images/expand.jpg")>0  )
	{
		for(var i=1;i<argslen;i++)
		{
			args[i].style.display = "none";
			obj.src =obj.src.replace("expand","collapse");
		}
	}
	else
	{	
		for(i=1;i<argslen;i++)
		{
			args[i].style.display = "block";
			//obj.src = obj.src +"collape.jpg";
			obj.src =obj.src.replace("collapse","expand");
		}
	}
}
//-----------------------------------------------------------------------
function documentDoKeyDown (e) 
{
    var myKeyCode      = e.keyCode;
    var mySrcElement   = e.srcElement;
    var isShiftPressed = e.shiftKey;
    var isCtrlPressed  = e.ctrlKey;
    var isAltPressed   = e.altKey;
    // Enter(13), Shift(16), Ctrl(17), Alt(18), CapsLock(20) keys?
    if (myKeyCode > 13 && myKeyCode <= 20)
        return true;
    if(isCtrlPressed)
    {
		if( myKeyCode==83)//CTRL+S
		{
            e.keyCode="";
			//win.form[0].submit();
            //applyData();
	    	return false;
	 	}
    }
    if(myKeyCode==13&&!isShiftPressed&&!isCtrlPressed&&!isAltPressed)
	{
	  	e.keyCode =9;
	  	return true;
	 }
	else
	 {
         return true;
	 }
}

