﻿// JavaScript Document
var nowRowIndex='';
var NewRow='';

function InsertRow(TTable,FTable,Len,CssYN,ISNO)//(目標，來源，序號長度，是否清除上一筆Css，是否含序號(顯示序號) )
{
	var TTable = document.getElementById(TTable);//目標table
	var FTable = document.getElementById(FTable); //來源table
	if( TTable == undefined )  return false;//判斷目標物件是否存在
	if( FTable == undefined )  return false;//判斷來源物件是否存在
	
	
	var TTableRows = eval(TTable.rows.length);  //table 列數
	var FTableRows = eval(FTable.rows.length);  //table 列數
	
	if( TTableRows < 0 || FTableRows < 0) return false; //判斷目標及來源物件是否有『列(row)』存在
	
	
	var TTableRowCells = eval(TTable.rows[TTableRows-1].cells.length);  //一列(row)有幾個欄位數
	var FTableRowCells = eval(FTable.rows[0].cells.length);  //一列(row)有幾個欄位數
	
	if( FTableRowCells > TTableRowCells)//來源row欄位數『是否大於』目標row欄位數
	{
		alert('Program Error !! ');
		return false; 
	}
	
	if( CssYN.toUpperCase() == 'Y' ){	TTable.rows[TTableRows-1].className='';	}//是否清除上一筆Css
	
	NewRow = TTable.insertRow(TTableRows);//目標Table新增一列
	NewRow.className="Newrow";//設定新增列的Css
	
	if( ISNO.toUpperCase() == 'Y' )//是否含序號(顯示序號)
	{
		var	NewCell=NewRow.insertCell(0);//新增一個欄位
		//NewCell.align="center";
	
		var tempNo = '' + NewRow.rowIndex;//取得所新增的列在目標Table在第幾行
		for (i=1; i<Len; i++){
			if (tempNo.length < Len){ tempNo = '0' + tempNo; }
			else { break; }
		}
		NewCell.innerHTML = tempNo + '.';	//填入序號
	}
	
	var startCell = 0; //預設取得欄位起始
	if( TTableRowCells == FTableRowCells && ISNO.toUpperCase() == 'Y' ){ var startCell= 1;	 }//目標Table和來源Table的欄位數一樣且含序號時，新增列的欄位從第二欄位開始(0=>第一欄；1=>第二欄.....)
	if( TTableRowCells != FTableRowCells && ISNO.toUpperCase() != 'Y' ){ NewRow.insertCell(0);}//目標Table和來源Table的欄位數不一樣且未含序號時，新增一個列的欄位
	
	for(var i=startCell ;i<FTable.rows[0].cells.length;i++)
	{
		NewCell=NewRow.insertCell(i);
		NewCell.align=FTable.rows[0].cells[i].align;
		NewCell.innerHTML=FTable.rows[0].cells[i].innerHTML;
	}
	nowRowIndex=NewRow.rowIndex;		
}

function CleanRows(theForm,HYN)//(表單,是否含表頭)
{
	if(theForm != undefined)
	{
		TRow=document.all[tableid].rows.length;
		if(TRow > 1)
		{
			if (HYN == 'Y' || HYN == 'y'){ var from=1; } else { var from=0; }
			for (i=from; i<=nowRowIndex; i++){ document.all[tableid].deleteRow(); }
		}
	}
}

function CellIndex(obj){	return(obj.parentNode.cellIndex);	}
function RowIndex(obj){		return(obj.parentNode.parentNode.rowIndex);	}




function RowDisabled(obj)
{
	for( i=0 ; i < obj.length ; i++)	
	{
		if(TextName[i] != undefined)
		{
			if(TextName[i].type != 'checkbox'){	TextName[i].style.backgroundColor ='#E1E1E1';	}
			TextName[i].disabled=true;
		}
	}
}

function RowUnDisabled(obj)
{
	for( i=0 ; i < obj.length ; i++)	
	{
		if(TextName[i] != undefined)
		{
			if(TextName[i].type != 'checkbox'){	TextName[i].style.backgroundColor ='white';	}
			TextName[i].disabled=false;
		}
	}
}
/*-------------------------------------------*/
function RowDisabledYN(obj,Fields,Pic) {	// 啟動刪除列元件,(關閉/開啟元素名稱),(啟用圖示,關閉圖示)
	theForm = obj.form;
	ArrField = Fields.split(',');
	objs = getFormElementsByObjName(obj);
	
	if ( objs.length > 1 ){
		Row = RowIndex(obj)-1;
		var ConTF = true;
	} else {
		var ConTF = false;
	}
	
	if ( obj.src != undefined){
		if ( Pic == undefined ){
			Pic = obj.src+','+obj.src;
		}
		TmpPic=Pic.split(',');
		if ( obj.title.toUpperCase() == 'N'){
			var checked = true;
			obj.title = 'Y';
			obj.src = TmpPic[1];
		} else {
			var checked = false;
			obj.title = 'N';
			obj.src = TmpPic[0];
		}
	}
	checked = obj.checked;
	
	for (i=0; i<ArrField.length; i++){
		TRunObj=document.getElementsByName(ArrField[i]);
		if (ConTF){
			RunObj=TRunObj[Row];
		} else {
			RunObj=TRunObj[0];
		}
		
		if ( RunObj != undefined && RunObj != null){
			if (obj.checked){
				var ColorStr='#C1C1C1';
			} else {
				var ColorStr='';
			}
			if (RunObj.type.toUpperCase() != "CHECKBOX"){ RunObj.style.backgroundColor = ColorStr; }			
			RunObj.disabled=checked;
		}
	}
}
