/**********************************************************
  **  版权信息：昆明东电科技有限公司                     **
  **  项    目：滇东电业局管理信息系统                   **
  **  JAVA版本：jdk1.4.1及以上                           **
  **  模块名称：公用模块                                 **
  **  版    本：1                                        **
  **  修改历史：2004.08.12                               **
  **  修改内容：新建                                     **
  **  作    者：沈映凡                                   ** 
  *********************************************************/
 
 /*********************************************************
  JavaScript Document                               
  该文件为表格动态增加、删除、插入的的客户端程序，主要功能为：
	1、在客户端动态的对表格操作，包括增加行、删除行、插入行、上移行、下移动行。
	2、其主要用在那些不确定记录条数的页面中。
  文件主调函数说明如下：
	1、add_row(the_table)——在表格的末尾增加一行；
	2、del_row(the_table)——删除表格的当前行；
	3、insert_row(the_table)——在表格的当前行处插入一行；
	4、up_row(the_table)——上移表格的当前行；
	5、down_row(the_table)——下移表格的当前行；
	6、参数the_table为所操作的表格对象，如<table id="the_table">
  使用说明如下：	
	1、具体使用为：<input type="text" value="增加" onClick="add_row(the_Table)">
	2、表格增加行时，第一次是以表格的第三行为依据，以后每次增加都一此行为依据，无论此行是否存在。

  ——作者：沈映凡于2004年8月6日。
  *******************************************************************/

var theRowOfTheDynamicTable=null;//用于存放表格要添加的行的模板。
var theCellsInnerHTMLOfTheRow=null;//存放各个单元格的innerHTML。

function add_row(the_table,theTemplate){
	if(typeof the_table == "string")
		the_table=document.getElementById(the_table);
	if(the_table==null)
		return;
	
	var i=0;
	var the_row,the_cell;
	var row_index=-1;

	if(typeof theTemplate == "string")
		theTemplate=document.getElementById(theTemplate);
	if(theTemplate!=null){//有模板的，按照模板添加。
		theRowOfTheDynamicTable=theTemplate;
	}else if(theRowOfTheDynamicTable==null){//没有模板的，按the_table.rows[2]添加。
		theRowOfTheDynamicTable=the_table.rows[2];
	}
	
	if(theRowOfTheDynamicTable==null)
		return;
	
	theCellsInnerHTMLOfTheRow=new Array();
	theCellsClassNameOfTheRow=new Array();
	theCellsAlignOfTheRow=new Array();
	for(i=0;i<theRowOfTheDynamicTable.cells.length;i++){
		the_cell=theRowOfTheDynamicTable.cells[i];
		theCellsInnerHTMLOfTheRow[i]=the_cell.innerHTML;
		theCellsClassNameOfTheRow[i]=the_cell.className;
		theCellsAlignOfTheRow[i]=the_cell.align;
	}
	
	row_index=the_table.rows.length;
	var newrow=the_table.insertRow(row_index);
	newrow.className=theRowOfTheDynamicTable.className;
	for(i=0;i<theCellsInnerHTMLOfTheRow.length;i++){
		the_cell=newrow.insertCell(i);
		the_cell.align=theCellsAlignOfTheRow[i];
		the_cell.className=theCellsClassNameOfTheRow[i];
		the_cell.innerHTML=theCellsInnerHTMLOfTheRow[i];
	}
}

function get_Element(the_ele,the_tag){
	the_tag = the_tag.toLowerCase();
	if(the_ele.tagName.toLowerCase()==the_tag)
		return the_ele;
	while(the_ele=the_ele.offsetParent){
		if(the_ele.tagName.toLowerCase()==the_tag)
			return the_ele;
	}
	return(null);
}

function del_row(the_table){
	var the_cell,the_row;
	var i=0;

	if(theRowOfTheDynamicTable==null){//没有模板的，按the_table.rows[2]添加。
		theRowOfTheDynamicTable=the_table.rows[2];
	}
/*
	if(theRowOfTheDynamicTable==null){
		if(theTemplate!=null)
			theRowOfTheDynamicTable=theTemplate;
		if(theRowOfTheDynamicTable==null)
			theRowOfTheDynamicTable=the_table.rows[2];
		if(theRowOfTheDynamicTable==null)
			return;
	}
	if(the_table.rows.length<=3) 
		return;
*/
	
    the_cell=get_Element(event.srcElement,"td");
	if(the_cell==null) 
		return;

	the_row=the_cell.parentElement.rowIndex;
	the_table.deleteRow(the_row);
}

function up_row(the_table){ 
	var the_cell,cur_row,up_row;
	var i=0;
	the_cell=get_Element(event.srcElement,"td");
	if(the_cell==null) return;
	if(the_table.rows.length<=1) return;
	
	cur_row=the_cell.parentElement;
	row_index=cur_row.rowIndex;
	if(row_index<=0) return;
	up_row=the_table.insertRow(row_index-1);
	
	for(i=0;i<cur_row.cells.length;i++){
		the_cell=up_row.insertCell(i);
		the_cell.align="center";
		the_cell.innerHTML=cur_row.cells[i].innerHTML;
	}
	the_table.deleteRow(row_index+1);
}

function down_row(the_table){ 
	var the_cell,cur_row,down_row;
 	var i=0;
    the_cell=get_Element(event.srcElement,"td");
	if(the_cell==null) return;
	
	if(the_table.rows.length<=1) return;
	
	cur_row=the_cell.parentElement;
	row_index=cur_row.rowIndex;
	if(row_index>=the_table.rows.length-1) return;
	
	down_row=the_table.insertRow(row_index+2);
	
	for(i=0;i<cur_row.cells.length;i++){
		the_cell=down_row.insertCell(i);
		the_cell.align="center";
		the_cell.innerHTML=cur_row.cells[i].innerHTML;
	}
	the_table.deleteRow(row_index);
}

function insert_row(the_table){ 
	var the_cell,the_row,temp,row_index,newrow;
 	var i=0;
    the_cell=get_Element(event.srcElement,"td");
	if(the_cell==null) return;
	if(the_table.rows.length<=0) return;
	the_row=the_cell.parentElement;
	row_index=the_row.rowIndex;
	newrow=the_table.insertRow(row_index);
	for(i=0;i<the_row.cells.length;i++){
		the_cell=newrow.insertCell(i);
		the_cell.align="center";
		the_cell.innerHTML=the_row.cells[i].innerHTML;
	}
}
