﻿// ---------------------------------------------------------------------------------------------------------------
// Global variables, accessible in all javascript files
// ---------------------------------------------------------------------------------------------------------------
var MustFillArray = new Array();
var ValidationArray = new Array();
var SelectListArray = new Array();
var FlipArray= new Array();
var RichTextArray = new Array();

var MenuState = new Array();
MenuState[0] = "";	// dummy; dynamically resizes +1 because we're writing in the end item

var FieldIdx;	// used in iSite.cls_RecSetItem.StuffCtlArray
var FKIndex;

var Msg_MustFill;
var Msg_Updated;
var Msg_ConfirmDelete;
var Msg_Validate;

var Current_DisplayedChild;


/*---------------------------------------------------------------------------------------------------------------
函式名稱：	Expand the String class to have trim functions
Src:		From the web
撰寫日期：	2006-08-28
撰寫人員：	iTeam
使用範例：	document.forms['JumpForm']['DisplayChild'].value = document.forms['JumpForm']['DisplayChild'].value.trim();
---------------------------------------------------------------------------------------------------------------*/
String.prototype.trim=trim;		//傳回去除前後空白的值
String.prototype.ltrim=ltrim;	//傳回去除左邊空白的值
String.prototype.rtrim=rtrim;	//傳回去除右邊空白的值

function trim() 
{
  return this.replace(/^\s+|\s+$/g, "");
} 
function ltrim() 
{
  return this.replace(/(^\s*)/g, "");
}
function rtrim() 
{
  return this.replace(/(\s*$)/g, "");
}


/*---------------------------------------------------------------------------------------------------------------
函式名稱：	ImportScript()
撰寫日期：	2006-05-03
撰寫人員：	Sunil Narayan
函式功能：	Opens a new window
參數：		
使用範例：	
---------------------------------------------------------------------------------------------------------------*/
function ImportScript(ScriptFile)
{ 
    var Loopy;
	var base;
	var Element_Head;
	var Element_Scripts;
	var NewScript;

	NewScript = document.createElement('script');
	NewScript.type = 'text/javascript'; 
	NewScript.src = ScriptFile;
	
	if (AppendFlag)
	{
		Element_Head = document.getElementsByTagName('head');
		Element_Head[0].appendChild(script);
	}
	else
	{
		Element_Scripts = document.getElementsByTagName("script");
		for (Loopy=0; Loopy < Element_Scripts.length; Loopy++)
		{
			if (Element_Scripts[Loopy].src.match(NewScript))
			{ 
				//base = scripts[i].src.replace(NewScript, "");
				Element_Scripts[Loopy].src = NewScript.src;
				break;
			}
		} 
		//document.write("<" + "script src=\"" + base + path + "\"></" + "script>"); 
	}
} 


/*---------------------------------------------------------------------------------------------------------------
函式名稱：	LoadCombo_Massive(FormID, CtlName)
撰寫日期：	2006-10-24
撰寫人員：	Sunil Narayan
函式功能：	Sets a flag in the iFrame, which has a timer checking the flag status
參數：		FormID:		id of the form in the main document; [Tablename]_[PKVal]
			CtlName:	id of the combo
使用範例：
---------------------------------------------------------------------------------------------------------------*/
function LoadCombo_Massive(FormID, CtlName)
{
	var XDiv;
	var timer;
	var XFrame;

	XDiv = document.getElementById(FormID + '_' + CtlName + '_Button');		// hide the ... button span
	XDiv.style.display = "none";

	XDiv = document.getElementById(FormID + '_' + CtlName + '_Text');		// hide the displaytext span
	XDiv.style.display = "none";

	XDiv = document.getElementById(FormID + '_' + CtlName + '_Loading');	// show the animation span
	XDiv.style.display = "inline";

	XFrame = frames[FormID + '_' + CtlName + '_iFrame'];					// set the flag in the hidden iFrame
	XFrame.GoFlag = true;	// set the flag to trigger execution
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：	ClearCombo_Massive(FormID, CtlName)
撰寫日期：	2006-10-24
撰寫人員：	Sunil Narayan
函式功能：	massive combo: clears the combo, or the displaytext & value
參數：		FormID:		id of the form in the main document; [Tablename]_[PKVal]
			CtlName:	id of the combo
使用範例：
---------------------------------------------------------------------------------------------------------------*/
function ClearCombo_Massive(FormID, CtlName)
{
	var XDiv;

	if (typeof document.forms['CMS_Form_' + FormID][CtlName].options != "undefined")	// combo is showing
	{
		document.forms['CMS_Form_' + FormID][CtlName].selectedIndex = 0;	
	}
	else																				// disabled combo: hidden field + displaytext
	{
		document.forms['CMS_Form_' + FormID][CtlName].value=0;							// set value of hidden field to 0
		
		XDiv = document.getElementById(FormID + '_' + CtlName + '_Text');				// clear the displaytext
		XDiv.innerHTML = '';			// get rid of the displayed text
	}
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：	ChildWindow(URL, WindowName, width, height, ShowMenuBar, Resizable, ScrollBars, X, Y, hCenter, vCenter)
撰寫日期：	2006-05-03
撰寫人員：	Sunil Narayan
函式功能：	opens a child window with the following parameters
參數：		- URL:	address of page to display
				...

使用範例：	
---------------------------------------------------------------------------------------------------------------*/
function ChildWindow(URL, WindowName, width, height, ShowMenuBar, Resizable, ScrollBars, X, Y, hCenter, vCenter)
{
	var OptionsString = '';
	var WindowHandle = null;

	if (width !=0)
	{
		OptionsString += 'width=' + width + ',';
	}
	if (height !=0)
	{
		OptionsString += 'height=' + height + ',';
	}
	if (ShowMenuBar)
	{
		OptionsString += 'menubar=1,';
	}
	else
	{
		OptionsString += 'menubar=0,';
	}
	if (Resizable)
	{
		OptionsString += 'resizable=1,';
	}
	else
	{
		OptionsString += 'resizable=0,';
	}
	if (ScrollBars)
	{
		OptionsString += 'scrollbars=1,';
	}
	else
	{
		OptionsString += 'scrollbars=0,';
	}

	if (hCenter)
	{
		X = (window.screen.availWidth - width) / 2;		// override passed posn
	}
	if (vCenter)
	{
		Y = (document.body.offsetHeight - height) / 2;	// override passed posn
	}
	
	if (X > 0)
	{
		OptionsString += 'left=' + X + ',screenX=' + X + ','
	}
	if (Y > 0)
	{
		OptionsString += 'top=' + Y + ',screenY=' + Y + ','
	}

	//alert(OptionsString);
	WindowHandle = window.open(URL, WindowName, 'alwaysRaised=1,directories=0,status=1,' + OptionsString);

	/*
	if (X > 0)
	{
		//WindowHandle.moveTo(X,Y);
	}
	*/
	WindowHandle.focus();
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：	DisplayChild(WhichChild)
撰寫日期：	2006-05-03
撰寫人員：	Sunil Narayan
函式功能：	Displays a layer
參數：		- WhichChild:	identifier of layer to show

使用範例：	DisplayChild('Admins3')
---------------------------------------------------------------------------------------------------------------*/
function DisplayChild(WhichChild)
{
	var XChild;
	var JumpForm;
	var TD_Selected;
	// master forms don't use layers, so we can skip
	if(document.getElementById('Layer_' + Current_DisplayedChild) != null && document.getElementById('Layer_' + Current_DisplayedChild) != null )	
	{
		if (Current_DisplayedChild != "")
		{
			XChild = document.getElementById('Layer_' + Current_DisplayedChild);
			XChild.className = "DisplayNone";

			TD_Selected = document.getElementById('Arrow_' + Current_DisplayedChild)
			
			if (TD_Selected != null)
			{
				TD_Selected.innerHTML = "";
			}
		}

		XChild = document.getElementById('Layer_' + WhichChild);
		if (XChild != null)
		{
			XChild.className = "DisplayAll";
			Current_DisplayedChild = WhichChild;

			TD_Selected = document.getElementById('Arrow_' + WhichChild)
			if (TD_Selected != null)
			{
				TD_Selected.innerHTML = "<img src='/iCMS/Images/Bullet_7_H.gif' width='7' height='12'>"
			}
		}

		try
		{
			document.forms['JumpForm']['DisplayChild'].value = WhichChild;
			//alert(WhichChild);
		}
		catch (e)
		{
			//alert('form doesnt exist')
		}
		//document.forms['JumpForm']['ShowChild_' + FormID].value = ShowChild;	// this is the child we were displaying before the jump

	}
	//else alert('Layer_' + Current_DisplayedChild + ' not found')
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：Set_Focus(frm, fld)
撰寫日期：2004/8/3
撰寫人員：Lance Huang
函式功能：取得焦點
參數：
	- frm：表單名稱
	- fld：欄位名稱

使用範例：Set_Focus('frmMemberAdd','Birthday')
---------------------------------------------------------------------------------------------------------------*/
function Set_Focus(frm, fld)
{
	document.forms[frm][fld].focus();
}


function pausecomp(millis)
{
	date = new Date();
	var curDate = null;

	do
	{
		var curDate = new Date();
	}
	while(curDate-date < millis);
}


/*---------------------------------------------------------------------------------------------------------------
函式名稱：	CheckNumeric(frm, fld)
撰寫日期：	2006-06-06
撰寫人員：	Sunil Narayan
函式功能：	Checks if a formfield is numeric
參數：		
使用範例：	
---------------------------------------------------------------------------------------------------------------*/
function CheckNumeric(frm, fld)
{
	var TestValue = document.forms[frm][fld].value;

	if (isNumeric(TestValue))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isNumeric(TestValue)	// the 'match' function returns 0 if the value didn't match
{
	var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; // Note: this WILL allow a number that ends in a decimal: -452.
	var result = TestValue.match(RegExp);
	return result;
}




/*---------------------------------------------------------------------------------------------------------------
函式名稱：CheckDate()
撰寫日期：2004/8/2
撰寫人員：Lance Huang
函式功能：公司統一編號檢查
參數：
	- frm：表單名稱
	- fld：欄位名稱

使用範例：CheckDate('frmMemberAdd','Birthday')
---------------------------------------------------------------------------------------------------------------*/
function CheckDate(frm, fld) {
	var sample =/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/;

	if (!sample.test(document.forms[frm][fld].value))
	{
		return false;
	}
	else
	{
		return true;
	}
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱:	Check_Alpha_Num_Len_Range(frm, fld, MinLength, MaxLength)
撰寫日期:	2006-03-27 / 2006-05-02
撰寫人員:	Carol Wang / Sunil Narayan
函式功能:	判斷輸入的字串必為英文+數字且可限定字串長度範圍
參數:		- MinLength:	最短可以接受的字串長度
			- MaxLength:	最長可以接受的字串長度

使用範例:	Check_Alpha_Num_Len_Range('abced8',6,99)	// pass
			Check_Alpha_Num_Len_Range('a1', 6,99);		// fail (length)
			Check_Alpha_Num_Len_Range('a', 6, 99);		// fail (number, length)
			Check_Alpha_Num_Len_Range('3225', 6,8);		// fail (alpha, length)
---------------------------------------------------------------------------------------------------------------*/
function Check_Alpha_Num_Len_Range(frm, fld, MinLength, MaxLength)
{
	var Code = document.forms[frm][fld].value;
	var Accept_Str = new RegExp(/[A-Za-z]/);
	var Accept_Num = new RegExp(/[0-9]/);
	var Match_Str = Accept_Str.exec(Code);
	var Match_Num = Accept_Num.exec(Code);
	if (MinLength == '')
	{
		MinLength = 0;
	}
	if (MaxLength == '')
	{
		MaxLength = 1024;
	}

	var Pass_Str = (Match_Str != null);
	var Pass_Num = (Match_Num != null);
	var Pass_Len = (Code.length >= MinLength) && (Code.length <= MaxLength) ;

	if (Pass_Str && Pass_Num && Pass_Len)
	{
		//alert('pass: ' + Code);
		return true;
	}
	else
	{
		//alert("problem: " + Code + ' Str:' + Pass_Str + ' Num: ' + Pass_Num + ' Len: ' + Pass_Len);
		return false;
	}
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：CheckPhone()
撰寫日期：2004/8/3
撰寫人員：Lance Huang
函式功能：電話格式檢查，不包括區碼及分機號碼
參數：
	- frm：表單名稱
	- fld：欄位名稱

使用範例：CheckPhone('frmMemberAdd','Tel')
---------------------------------------------------------------------------------------------------------------*/
function CheckPhone(frm, fld) {
	var sample1 = /^(\d{6,8})$/;
	var sample2 = /^(\d{3,4}[\-{1}\s{1}\.{1}]\d{3,4})$/;

	if (!sample1.test(document.forms[frm][fld].value) && !sample2.test(document.forms[frm][fld].value)) {
		return false;
	}
	else {
		return true;
	}
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：CheckExtension()
撰寫日期：2004/8/3
撰寫人員：Lance Huang
函式功能：分機號碼格式檢查，
參數：
	- frm：表單名稱
	- fld：欄位名稱

使用範例：CheckExtension('frmMemberAdd','Ext')
---------------------------------------------------------------------------------------------------------------*/
function CheckExtension(frm, fld) {
	var sample = /^\d{2,4}$/

	if (!sample.test(document.forms[frm][fld].value)) {
		return false;
	}
	else {
		return true;
	}
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：CheckEmail()
撰寫日期：2004/8/2
撰寫人員：Lance Huang
函式功能：公司統一編號檢查
參數：
	- frm：表單名稱
	- fld：欄位名稱

使用範例：CheckEmail('frmMemberAdd','Email')
---------------------------------------------------------------------------------------------------------------*/
function CheckEmail(frm, fld) {
	var sample = /^(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,4})$/;

	if (!sample.test(document.forms[frm][fld].value)) {
		return false;
	}
	else {
		return true;
	}
}
/*---------------------------------------------------------------------------------------------------------------
函式名稱：Mustfill_Checkboxes()
撰寫日期：2006/5/22
撰寫人員：Carol Wang / Sunil Narayan
函式功能：判斷Checkbox被選取的個數
參數：
	- frm：表單名稱
	- fld：欄位名稱
	- minCount：最小個數
	- maxCount：最大個數

使用範例：Mustfill_Checkboxes('frmMemberAdd','chkbox','minCount','maxCount')
---------------------------------------------------------------------------------------------------------------*/
function Mustfill_Checkboxes(frm, fld, minCount, maxCount)
{
	var ckCount = 0;
	var Len = document.forms[frm][fld].length;
	var RetVal;
	//alert(maxCount);
	if(typeof Len != "undefined")	// if there is only one checkbox, it is not an array, so .length is undefined
	{
		for( i=0;i<Len;i++ )
		{             
			if(document.forms[frm][fld][i].checked == true)
			{
				ckCount++;			
			}		
		}
	}
	else
	{
		if (document.forms[frm][fld].checked)
		{
				ckCount++;			
		}
	}

	if (ckCount < minCount || ckCount > maxCount)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：CheckGUID()
撰寫日期：2004/8/2
撰寫人員：Lance Huang
函式功能：公司統一編號檢查
參數：
	- frm：表單名稱
	- fld：欄位名稱

使用範例：CheckGUID('frmMemberAdd','guid')
---------------------------------------------------------------------------------------------------------------*/
function CheckGUID(frm, fld) {
	var guid = document.forms[frm][fld].value;

	var CheckSum = new Array('1','2','1','2','1','2','4','1');
 	var p_stage1 = new Array(8);
	var p_stage2 = new Array(8);
	var sum = 0;

	for (i=0; i<p_stage1.length; i++) {
		p_stage1[i] = parseInt(guid.substr(i,1)) * parseInt(CheckSum[i]);
		
		if (p_stage1[i].toString().length < 2) {
			p_stage1[i] = '0' + p_stage1[i];
		}
		else {
			p_stage1[i] = p_stage1[i];
		}

		p_stage2[i] = parseInt(p_stage1[i].toString().substr(0,1)) + parseInt(p_stage1[i].toString().substr(1,1))
		sum = sum + parseInt(p_stage2[i]);
	}

	if (sum % 10 == 0) {
		return true;
	}
	else {
		return false;
	}
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：CheckSID()
撰寫日期：2004/8/2
撰寫人員：Lance Huang
函式功能：身份證字號檢查
參數：
	- frm：表單名稱
	- fld：欄位名稱

使用範例：CheckSID('frmMemberAdd','sid')
---------------------------------------------------------------------------------------------------------------*/
function CheckSID(frm, fld) 
{
	var NID = document.forms[frm][fld].value;
	return CheckNID(NID);
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：CheckInputID()
撰寫日期：2007/02/13
撰寫人員：Jones Lin
函式功能：身份證字號檢查 .核對輸入的生日年月日以及性別
參數：
	- frm：表單名稱
	- fld：欄位名稱

使用範例：CheckInputID('frmMemberAdd','sid','sex','year','month','day')
---------------------------------------------------------------------------------------------------------------*/
function CheckInputID(frm, fld,fldSex,fldYear,fldMonth,fldDay) 
{
	var NID = document.forms[frm][fld].value;
	var SexIsMale = document.forms[frm][fldSex][0].checked;
	var Year = document.forms[frm][fldYear].value;
	var Month = document.forms[frm][fldMonth].value;
	var Day = document.forms[frm][fldDay].value;
	return CheckNID_InChina(NID,SexIsMale,Year,Month,Day);
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：CheckNID_InChina()
撰寫日期：2007/2/13
新增15碼驗證: 2007/06/04
撰寫人員：Jones Lin
函式功能：身份證字號檢查
參數：
	- NID : 身份證字號

使用範例：CheckNID('361000197611248713')
---------------------------------------------------------------------------------------------------------------*/
function CheckNID_InChina(NID,Sex,Year,Month,Day) 
{
	//init values
	if(Month < 10){
		Month = '0'+Month
	}
	if(Day < 10){
		Day = '0'+Day
	}

	//==============================================================================================
	//=========== Check 18 Digits NID
	//==============================================================================================
	if ( NID.length == 18)
	{ // input is 18 digits

		//	1. 对前17位数字本体码加权求和
	　　//	公式为：S = Sum(Ai * Wi), i = 0, ... , 16
	　　//	其中Ai表示第i位置上的身份证号码数字值，Wi表示第i位置上的加权因子，其各位对应的值依次为： 
		//	7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
		//
		//	2. 以11对计算结果取模
	　　//	Y = mod(S, 11)
		//
		//	3. 根据模的值得到对应的校验码
		//	对应关系为：
		//  Y值：		0 1 2 3 4 5 6 7 8 9 10
	　　//	校验码：	1 0 X 9 8 7 6 5 4 3 2 

		//==============================================================================================
		//=========== Check user Input birthday and gender with NID 
		//==============================================================================================
		if(  !(NID.substr(6,4) == Year) || !(NID.substr(10,2) == Month) || !(NID.substr(12,2)== Day) ) 
		{
			return false;
		}
		if (Sex)
		{
			//alert("Sex[male]:"+Sex);
			if( !(NID.substr(16,1) % 2 ==1)){
				return false;
			}
		}
		else if(!Sex){
			//alert("Sex[female]:"+Sex);
			if( !(NID.substr(16,1) % 2 ==0)){
				return false;
			}
		}
		//==============================================================================================

		
		var LastCheckDigit; //Last Digit of NID. check digit ( for 18 digits NID only)

		// exponent numbers 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 加權碼
		var exponent = new Array('7','9','10','5','8','4','2','1','6','3','7','9','10','5','8','4','2');
		var NID_Sum=0;
		var temp_NID;

		
		var y=0; // NID_smu % 11  -- check key

		//if ( NID.length == 18)
		//{
			temp_NID = new Array(18);
			//LastCheckDigit = NID.substr(17,1);
			if(!checkShortDate(NID.substr(6,8)))
				return false;
			
			for(i = 0;i<NID.length;i++)
			{
				temp_NID[i]= NID.substr(i,1);
			}

			//====================================
			//==    check with special calculation
			//====================================

			for (i=0;i<17 ;i++)
			{
				NID_Sum = NID_Sum + temp_NID[i] * exponent[i];
			}
			
			Y = NID_Sum % 11;

			if(Y==0){
				if(temp_NID[17]==1)
					return true;
				else
					return false;
			}
			else if(Y==1){
					if(temp_NID[17]==0)
						return true;
					else
						return false;
			}
			else if(Y==2){
					if(temp_NID[17]=='X')
						return true;
					else
						return false;
			}
			else if(Y==3){
					if(temp_NID[17]==9)
						return true;
					else
						return false;
			}
			else if(Y==4){
					if(temp_NID[17]==8)
						return true;
					else
						return false;
			}
			else if(Y==5){
					if(temp_NID[17]==7)
						return true;
					else
						return false;
			}
			else if(Y==6){
					if(temp_NID[17]==6)
						return true;
					else
						return false;
			}
			else if(Y==7){
					if(temp_NID[17]==5)
						return true;
					else
						return false;
			}
			else if(Y==8){
					if(temp_NID[17]==4)
						return true;
					else
						return false;
			}
			else if(Y==9){
					if(temp_NID[17]==3)
						return true;
					else
						return false;
			}
			else if(Y==10){
					if(temp_NID[17]==2)
						return true;
					else
						return false;
			}else{
				return false;
			}
		

	//==============================================================================================
	//=========== Check 15 Digits NID
	//==============================================================================================
	}else if (NID.length == 15)
	{
		//==============================================================================================
		//=========== Check user Input birthday and gender with NID 
		//==============================================================================================

		if(  !(NID.substr(6,2) == Year.substr(2,2)) || !(NID.substr(8,2) == Month) || !(NID.substr(10,2)== Day) ) 
		{
			return false;
		}
		if (Sex)
		{
			//alert("Sex[male]:"+Sex);
			if( !(NID.substr(14,1) % 2 ==1)){
				return false;
			}
		}
		else if(!Sex){
			//alert("Sex[female]:"+Sex);
			if( !(NID.substr(14,1) % 2 ==0)){
				return false;
			}
		}
		//==============================================================================================
		return true;
	}else
	{
		return false;
	}
	

}
//检查日期格式(YYYYMMDD)   
function   checkShortDate(date_time)   {   
	var   re   =   /^\d{8}$/;   
	var   bolReturn   =   re.test(date_time);   
	if(bolReturn){   
		var   y   =   parseInt(date_time.substr(0,4),10);   
		var   m   =   parseInt(date_time.substr(4,2),10);   
		var   d   =   parseInt(date_time.substr(6,2),10);   
		if   (y<1949   ||   m<1   ||   m>12   ||   d<1   ||   d>31   ||   ((m==4   ||   m==6   ||   m==9   ||   m==11)   &&   d>30)   ||   
		(m==2   &&   ((y   %   4>0   &&   d>28)   ||   d>29)))     
		bolReturn   =   false;   
	}   
	return   bolReturn;   
}

//检查手機電話格式(xxxxxxxxxxxx)   
function   checkMobilPhone(phone)   {   
	var   re   =   /^\d{11}$/;   
	var   bolReturn   =   re.test(phone);   
	//if(bolReturn){   
		 
	//	bolReturn   =   false;   
	//}   
	return   bolReturn;   
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：CheckNID()
撰寫日期：2004/8/2
撰寫人員：Lance Huang
函式功能：身份證字號檢查
參數：
	- NID : 身份證字號

使用範例：CheckNID('A123456789')
---------------------------------------------------------------------------------------------------------------*/
function CheckNID(NID) 
{
	if(NID.length != 10){
		return false
	}

	var FinalChkSum = NID.substr(NID.length-1,1); // 身份證字號最後一碼, 檢查碼
	var NID_ChkSum = new Array('1','9','8','7','6','5','4','3','2','1');
	var tempNID = new Array(10);
	var p = new Array(10);
	var sum = 0;
	var NID_City = new Array();
	NID_City[0] = new Array('A','10');
	NID_City[1] = new Array('B','11');
	NID_City[2] = new Array('C','12');
	NID_City[3] = new Array('D','13');
	NID_City[4] = new Array('E','14');
	NID_City[5] = new Array('F','15');
	NID_City[6] = new Array('G','16');
	NID_City[7] = new Array('H','17');
	NID_City[8] = new Array('J','18');
	NID_City[9] = new Array('K','19');
	NID_City[10] = new Array('L','20');
	NID_City[11] = new Array('M','21');
	NID_City[12] = new Array('N','22');
	NID_City[13] = new Array('P','23');
	NID_City[14] = new Array('Q','24');
	NID_City[15] = new Array('R','25');
	NID_City[16] = new Array('S','26');
	NID_City[17] = new Array('T','27');
	NID_City[18] = new Array('U','28');
	NID_City[19] = new Array('V','29');
	NID_City[20] = new Array('X','30');
	NID_City[21] = new Array('Y','31');
	NID_City[22] = new Array('W','32');
	NID_City[23] = new Array('Z','33');
	NID_City[24] = new Array('I','34');
	NID_City[25] = new Array('O','35');

	for (j=0; j<NID_City.length; j++) 
	{
		if (NID_City[j][0] == NID.substr(0,1).toUpperCase()) 
		{
			tempNID[0] = NID_City[j][1].toString().substr(0,1);
			tempNID[1] = NID_City[j][1].toString().substr(1,1);
			break;
		}
		else 
		{
			continue;
		}
	}

	for (k=1; k<NID.length-1; k++) 
	{
		tempNID[k+1] = NID.substr(k,1);
	}

	sum = (parseInt(tempNID[0])+9*parseInt(tempNID[1])+8*parseInt(tempNID[2])+7*parseInt(tempNID[3])+6*parseInt(tempNID[4])+5*parseInt(tempNID[5])+4*parseInt(tempNID[6])+3*parseInt(tempNID[7])+2*parseInt(tempNID[8])+1*parseInt(tempNID[9])+parseInt(FinalChkSum));

	if ((parseInt(sum) % 10) == 0) 
	{
		return true;
	}
	else 
	{
		return false;
	}
}




function ValidateForm(TableName, FormID)
{
	var FormString = "CMS_Form_" + FormID;
	var Loopy;
	var StatusFlag = true;
	var MustFillArrayLength = MustFillArray.length;
	var ThisCtl;
	var CtlName;
	var CtlType;

	//var TableName = document.forms[FormString]["prm_TableName"].value;
	//DumpMustFillArray();

	if (RichTextArray.length > 0)
	{
		iFrame_Save(FormID);		// in Editor.js
	}
	if (SelectListArray.length > 0)
	{
		SelectList_Save(FormID, TableName);	// below
	}
	if (document.forms[FormString]["prm_Mode"].value == 2)
	{
		return true;	// enable submit if we're deleting, because we don't care how badly the form was filled out
	}
	

	// ----------------------------------------------------------------------------
	// Loopy through the entire MustFillArray
	// ----------------------------------------------------------------------------	
	for (Loopy=0; Loopy < MustFillArrayLength ; Loopy++)
	{
		//alert(MustFillArrayLength + ' ' + Loopy + ' ' + MustFillArray[Loopy][1]);

		if (TableName == MustFillArray[Loopy][0])
		{
			CtlName = MustFillArray[Loopy][1];
			CtlType = MustFillArray[Loopy][2];

			switch (CtlType)	// switch on Type
			{
				case "Text":
				case "Password":
				case "TextArea":
				case "HTMLArea":
				case "Date":
				case "DateTime":
				case "Time":
					if(typeof document.forms[FormString][CtlName] != "undefined")
					{
						if (document.forms[FormString][CtlName].value.length == 0)
						{
							StatusFlag = false;
							MustFillArray[Loopy][4] = 0;
						}
						else MustFillArray[Loopy][4] = 1;
					}
					break;
				case "Combo":
				case "Combo_Massive":
					ThisCtl = document.forms[FormString][CtlName];
					MustFillArray[Loopy][4] = 1;
					
					if(typeof ThisCtl != "undefined")
					{
						if(typeof ThisCtl.selectedIndex != "undefined")	// fails if the mustfill control is listed as a combo but it's been reused as a hidden
						{
							if (ThisCtl.selectedIndex == 0 || ThisCtl[ThisCtl.selectedIndex].value == 0)
							{
								StatusFlag = false;
								MustFillArray[Loopy][4] = 0;
							}
						}
						else	// a disabled combo or an unloaded combo_massive have a hidden field, so we can directly read the value
						{
							if(document.forms[FormString][CtlName].value == "0")
							{
								StatusFlag = false;
								MustFillArray[Loopy][4] = 0;
							}
						}
					}
					break;
				case "SelectList":
					ThisCtl = document.forms[FormString][CtlName];
					MustFillArray[Loopy][4] = 1;
					
					if(typeof ThisCtl != "undefined")
					{
						if (ThisCtl.length == 0)
						{
							StatusFlag = false;
							MustFillArray[Loopy][4] = 0;
						}
					}
					break;
				
				case "Checkbox":
					if(typeof document.forms[FormString][CtlName + "_Checkbox"] != "undefined")
					{
						if (document.forms[FormString][CtlName + "_Checkbox"].checked == false)
						{
							StatusFlag = false;
							MustFillArray[Loopy][4] = 0;
						}
						else MustFillArray[Loopy][4] = 1;
					}
					break;
				case "Radio":
					var RadioPass = false;
					var LoopyRadio;

					if(typeof document.forms[FormString][CtlName] != "undefined")
					{
						for (LoopyRadio=0; LoopyRadio < document.forms[FormString][CtlName].length; LoopyRadio++)
						{
							if (document.forms[FormString][CtlName][LoopyRadio].checked == true)
							{
								RadioPass = true;
							}
						}
						if (!RadioPass)
						{
							StatusFlag = false;
							MustFillArray[Loopy][4] = 0;
						}
						else MustFillArray[Loopy][4] = 1;
					}
					break;
				case "File":
				case "Image":
					if(typeof document.forms[FormString][CtlName] != "undefined")
					{
						if (document.forms[FormString][CtlName].value.length == 0)
						{
							StatusFlag = false;
							MustFillArray[Loopy][4] = 0;
						}
						else MustFillArray[Loopy][4] = 1;
					}
					break;
				default:
			} // end switch
		} // end if
	} // end for

	if (!StatusFlag)
	{
		var StatusMsg = "";
		var ProblemControl = "";
	
		for (Loopy=0; Loopy < MustFillArrayLength; Loopy++)	// identify which controls weren't filled
		{
			CtlName = MustFillArray[Loopy][1];
			CtlType = MustFillArray[Loopy][2];

			if (TableName == MustFillArray[Loopy][0] && MustFillArray[Loopy][4] == 0)
			{
				StatusMsg += (', [' + MustFillArray[Loopy][3] + ']')

				if (CtlType != 'Radio' && CtlType != 'Combo_Massive' && CtlType != 'SelectList_Massive')
				{
					Set_ControlStyle(FormString, CtlName, ' ErrorField', 'set');
					if (ProblemControl == "")	// jump to the topmost field with problems
					{
						document.forms[FormString][CtlName].focus();
						ProblemControl = CtlName;
					}
				}
				else if (CtlType == 'Radio')
				{
					document.forms[FormString][CtlName][0].focus();
				}
			}
			else if (TableName == MustFillArray[Loopy][0] && MustFillArray[Loopy][4] != 0 && CtlType != 'Radio' && CtlType != 'Combo_Massive')
			{
				Set_ControlStyle(FormString, CtlName, ' ErrorField', 'clear');
			}
		}
		alert (StatusMsg.substr(2) + ' ' + Msg_MustFill);
	}
	else	// Check for validation rules for fields that have been filled
	{
		StatusMsg = '';
		for (i=0; i < ValidationArray.length; i++)
		{
			var V_Field = ValidationArray[i][0].toString();
			var V_Caption = ValidationArray[i][1].toString();
			var V_RuleID = ValidationArray[i][2].toString();
			var V_EntryDesc = ValidationArray[i][12].toString();
			
			var P1_Name = ValidationArray[i][4].toString();
			var P1_Val = ValidationArray[i][5].toString();
			var P2_Name = ValidationArray[i][6].toString();
			var P2_Val = ValidationArray[i][7].toString();
			var P3_Name = ValidationArray[i][8].toString();
			var P3_Val = ValidationArray[i][9].toString();
			var P4_Name = ValidationArray[i][10].toString();
			var P4_Val = ValidationArray[i][11].toString();
			var DoValidate_Flag = false;
			var RetVal = true;

			if(typeof document.forms[FormString][V_Field] != "undefined")
			{
				if (V_EntryDesc == 'Text' || V_EntryDesc == 'TextArea' || V_EntryDesc == 'HTMLArea' || V_EntryDesc == 'Password' || V_EntryDesc == 'Date' || V_EntryDesc == 'DateTime' || V_EntryDesc == 'Time' || V_EntryDesc == 'File' || V_EntryDesc == 'Image' || V_EntryDesc == 'Link')
				{
					if (document.forms[FormString][V_Field].value.length != 0)
					{
						DoValidate_Flag = true;	// we want to validate if something's been filled. otherwise, because it's not must-fill, we ignore it.
					}
				}
				else
				{
					DoValidate_Flag = true;	// we always want to validate all other types of fields
				}
			}
			if (DoValidate_Flag) switch (V_RuleID)	// check the System_Validation rules with this file
			{
				case '4':
					RetVal &= CheckEmail(FormString, V_Field);
					break;
				case '6':
					RetVal &= CheckPhone(FormString, V_Field);
					break;
				case '11':
					RetVal &= CheckExtension(FormString, V_Field);
					break;
				case '12':
					RetVal &= CheckDate(FormString, V_Field);
					break;
				case '13':
					RetVal &= CheckGUID(FormString, V_Field);
					break;
				case '14':
					RetVal &= CheckSID(FormString, V_Field);
					break;
				case '15':
					RetVal &= Check_Alpha_Num_Len_Range(FormString, V_Field, P1_Val, P2_Val);
					break;

				default:	// check the CMS_Validation rules in _ProjectSpecific_Javascript.js
					var RetVal = CustomValidate(FormString, V_Field, V_RuleID);
					break;
			} // end switch

			if (RetVal == 0)
			{
				Set_ControlStyle(FormString, V_Field, ' ErrorField', 'set');
				Set_Focus(FormString, V_Field);
				StatusMsg += Msg_Validate+V_Caption + '\n';
			}
			else
			{
				Set_ControlStyle(FormString, V_Field, ' ErrorField', 'clear');
			}
		}
		if (RetVal == 0)
		{
			alert(StatusMsg);
			StatusFlag = false
		}
	}

	return StatusFlag;
}


function Set_ControlStyle(FormString, FieldName, WhichStyle, WhichAction)
{
	var ThisCtl = document.forms[FormString][FieldName];

	if (typeof ThisCtl.className != "undefined")
	{
		if (WhichAction == 'set')
		{
			ThisCtl.className += WhichStyle ;
		}
		else
		{
			ThisCtl.className = ThisCtl.className.replace(WhichStyle, "");
		}
	}
}

function StuffMustFillArray(TableName, FieldName, FieldType, FieldCaption)
{
	var PrevLength = MustFillArray.length;

	MustFillArray[PrevLength] = new Array(5);	// dynamically resizes +1 because we're writing in the end item

	MustFillArray[PrevLength][0] = new String;
	MustFillArray[PrevLength][0] = TableName;
	MustFillArray[PrevLength][1] = new String();
	MustFillArray[PrevLength][1] = FieldName;
	MustFillArray[PrevLength][2] = new String();
	MustFillArray[PrevLength][2] = FieldType;
	MustFillArray[PrevLength][3] = new String();
	MustFillArray[PrevLength][3] = FieldCaption;
	MustFillArray[PrevLength][4] = new String;	// Status = 0,1
	MustFillArray[PrevLength][4] = 1;

}

function DumpMustFillArray()
{
	var PrevLength = MustFillArray.length;
	var Loopy;
	var TempVar;
	var MFColumn;

	TempVar = '';
	TempVar += '<table border=1>';
	TempVar += '<tr>';
	TempVar += '<td class="BasicBold">Item</td>';
	TempVar += '<td class="BasicBold">TableName</td>';
	TempVar += '<td class="BasicBold">FieldName</td>';
	TempVar += '<td class="BasicBold">FieldType</td>';
	TempVar += '<td class="BasicBold">FieldCaption</td>';
	TempVar += '<td class="BasicBold">Validation Status</td>';
	TempVar += '</tr>';

	for (Loopy=0; Loopy < PrevLength; Loopy++)
	{
		TempVar += '<tr>';
		TempVar += '<td>' + Loopy + '</td>';

		for (MFColumn=0; MFColumn < 5; MFColumn++)
		{
			TempVar += '<td>' + MustFillArray[Loopy][MFColumn] + '</td>';
		}
		TempVar += '</tr>';
	}
	TempVar += '</table>';
	document.getElementById("Debug1").innerHTML = TempVar;
}

function StuffValidationArray(FieldName, FieldCaption, EntryDesc, RuleID, P1_Name,P1_Val,P2_Name,P2_Val,P3_Name,P3_Val,P4_Name,P4_Val)	// P1 ~ P4 = additional optional parameters
{
	var PrevLength = ValidationArray.length;

	ValidationArray[PrevLength] = new Array(12);	// dynamically resizes +1 because we're writing in the end item
	ValidationArray[PrevLength][0] = new String();
	ValidationArray[PrevLength][0] = FieldName;

	ValidationArray[PrevLength][1] = new String();
	ValidationArray[PrevLength][1] = FieldCaption;

	ValidationArray[PrevLength][2] = new String();
	ValidationArray[PrevLength][2] = RuleID;

	ValidationArray[PrevLength][3] = new String(); // Status
	ValidationArray[PrevLength][3] = 1;

	ValidationArray[PrevLength][4] = new String();
	ValidationArray[PrevLength][4] = P1_Name;
	ValidationArray[PrevLength][5] = new String();
	ValidationArray[PrevLength][5] = P1_Val;

	ValidationArray[PrevLength][6] = new String();
	ValidationArray[PrevLength][6] = P2_Name;
	ValidationArray[PrevLength][7] = new String();
	ValidationArray[PrevLength][7] = P2_Val;

	ValidationArray[PrevLength][8] = new String();
	ValidationArray[PrevLength][8] = P3_Name;
	ValidationArray[PrevLength][9] = new String();
	ValidationArray[PrevLength][9] = P3_Val;

	ValidationArray[PrevLength][10] = new String();
	ValidationArray[PrevLength][10] = P4_Name;
	ValidationArray[PrevLength][11] = new String();
	ValidationArray[PrevLength][11] = P4_Val;

	ValidationArray[PrevLength][12] = new String();
	ValidationArray[PrevLength][12] = EntryDesc;
}

function SetButtonState(FormID, State)
{
	var FormString = "CMS_Form_" + FormID;
	var SubmitString = "Submit_" + FormID;
	var ResetString = "Reset_" + FormID;
	//alert (ResetString);

	//alert (document.forms[FormString][SubmitString].disabled)
	if (document.forms[FormString][SubmitString].disabled != State)
	{
		document.forms[FormString][SubmitString].disabled = State;
		document.forms[FormString][ResetString].disabled = State;
	}
	//alert (document.forms[FormString][SubmitString].disabled)
}
function SetRadioState(ThisForm, RadioControlName, VarValue)
{
	//var FormString = "CMS_Form_" + FormID;
	//alert(ThisForm.name);
	document.forms[ThisForm.name][RadioControlName][2].checked = true;
}
function ClearFileField(ThisForm, FileControlName)
{
	//alert(ThisForm[FileControlName].value);
	ThisForm[FileControlName].value = "";
	//alert(ThisForm[FileControlName].value);
}
function SetCheckboxState(FormID, CheckboxControlName)
{
	var FormString = "CMS_Form_" + FormID;
	var CheckboxName = CheckboxControlName + '_Checkbox'

	//alert(document.forms[FormString][CheckboxName].checked);
	//alert('Orig:' + document.forms[FormString][CheckboxControlName].value);
	if (document.forms[FormString][CheckboxName].checked)
	{
		document.forms[FormString][CheckboxControlName].value = -1;
	}
	else
	{
		document.forms[FormString][CheckboxControlName].value = 0;
	}
	//alert('Final:' + document.forms[FormString][CheckboxControlName].value);
}

function ComboMenu_Jump(CtlName, CtlVal, TargetURL)
{
	var Target;
	Target = TargetURL + CtlVal
	window.location=Target;
}


// --------------------------------------------------------------------------------
// Select List Stuff
// --------------------------------------------------------------------------------

function StuffSelectList(TableName, CtlName)
{
	var PrevLength = SelectListArray.length;

	SelectListArray[PrevLength] = new Array();	
	SelectListArray[PrevLength][0] = TableName;
	SelectListArray[PrevLength][1] = CtlName;
}

function SelectList_Save(FormID, TableName)
{
	var FormString = "CMS_Form_" + FormID;
	var Loopy;
	var Loopy_SL;
	var SL_TableName;
	var SL_CtlName;

	if (SelectListArray.length)
	{
		for (Loopy=0; Loopy < SelectListArray.length; Loopy++)
		{
			SL_TableName = SelectListArray[Loopy][0];
			SL_CtlName = SelectListArray[Loopy][1];

			if (SL_TableName == TableName)
			{
				var SL_Dest = document.forms[FormString][SL_CtlName].options
				if (SL_Dest.length)
				{
					for (Loopy_SL=0; Loopy_SL < SL_Dest.length; Loopy_SL++)
					{
						if (!SL_Dest[Loopy_SL].selected)
						{
							SL_Dest[Loopy_SL].selected = true;
						}
					}
				}
			}
		}
	}
}

function SelectList_ShowSelection(FormID, FieldName, WhichList)
{
	var FormString = "CMS_Form_" + FormID;
	var SL_Src = document.forms[FormString][WhichList].options
	var XDiv = eval('SS_' + FieldName + "_" + FormID);
	var TempString = "";
	//var SelectedIdx = document.forms[FormString][WhichList].selectedIndex

	for (var Loopy=0; Loopy < SL_Src.length; Loopy++)
	{
		if (SL_Src[Loopy].selected)
		{
			TempString = TempString + SL_Src[Loopy].text + '; '
			//if (XDiv.innerHTML.indexOf(SL_Src[Loopy].text) != -1)
			//{
			//	XDiv.innerHTML = SL_Src[Loopy].text;
			//	break;
			//}
		}
	}
	if (TempString != "")
	{
		TempString = TempString.slice(0, TempString.length -2);
	}
	XDiv.innerHTML = TempString; //document.forms[FormString][WhichList].options[SelectedIdx].text;
}

function SelectList_Transfer(FormID, Src, Dest, MoveAll)
{
	var FormString = "CMS_Form_" + FormID;
	//alert('FormString: ' + FormID + '\nSrc: ' + Src + '\nDest: ' + Dest)	
	//alert(document.forms[FormString][Dest].options);

	var SL_Src = document.forms[FormString][Src].options
	var SL_Dest = document.forms[FormString][Dest].options
	for (var Loopy=0; Loopy < SL_Src.length; Loopy++)
	{
		if (MoveAll || SL_Src[Loopy].selected)
		{
			SL_Dest[SL_Dest.length] = new Option(SL_Src[Loopy].text, SL_Src[Loopy].value, false, true);
			SL_Src[Loopy] = null;
			Loopy--;	// go back 1 because we've just deleted 1 ?
		}
	}
}

function SelectList_Flip(FormID, FK, Dest)	// based on Flip()
{
	var FormString = "CMS_Form_" + FormID;
	var Loopy;

	var SL_Src = document.forms[FormString]['SL_' + Dest].options;
	var SL_Dest = document.forms[FormString][Dest].options;
	var NumDestOptions = SL_Dest.length;

	var NumCtls;		// total number of controls in the flip array
	var CtlIndex=-1;	// position of this control in the flip array

	var NumFK;			// number of foreign key groups for this control (ie parent variations)
	var FKIndex =-1;
	var NumOptions;
	var Counter=0;
	
	for (Loopy = SL_Src.length-1; Loopy >= 0; Loopy--)
	{
		SL_Src[Loopy] = null;						// remove all existing SL options
	}

	NumCtls = FlipArray.length;									
	for (Loopy = 0; Loopy < NumCtls; Loopy++)					// find our Src combo by field name
	{
		if (FlipArray[Loopy][0] == Dest)						// set the position marker for the Src control's index in the FlipArray
		{
			CtlIndex = Loopy;
			break;
		}
	}
	//alert('CtlIndex: ' + CtlIndex);

	if (CtlIndex != -1)												// this can happen if the combo has no options at all, or we forgot to stuff it
	{
		var SrcValue;
		var DestValue;

		NumFK = FlipArray[CtlIndex][1].length;						// each FK has its own array of subkeys; NumFK is the number of FK groups for this ctl

		for (Loopy = 0; Loopy < NumFK; Loopy++)
		{
			if (FlipArray[CtlIndex][1][Loopy][0] == FK)				// set the position marker for the this FK in the FlipArray
			{
				FKIndex = Loopy;
				break;
			}
		}
		//alert('FK: ' + FK + ' FKIndex: ' + FKIndex);

		if (FKIndex == -1)
		{
			NumOptions = -1
		}
		else
		{
			NumOptions = FlipArray[CtlIndex][1][FKIndex][1].length;		// number of subkey options for this control
		}
		//alert('NumOptions: ' + NumOptions);

		for (Loopy = 0; Loopy < NumOptions; Loopy++)
		{
			var FoundFlag = false;

			SrcValue = FlipArray[CtlIndex][1][FKIndex][1][Loopy].value;
			//alert(SrcValue);

			for (var Loopy_Dest=0; Loopy_Dest < NumDestOptions; Loopy_Dest++)	// we lock the initial count at the very beginning, don't care how .length changes
			{
				DestValue = SL_Dest[Loopy_Dest].value;

				if (DestValue == SrcValue)
				{
					FoundFlag = true;
					break;
				}
			}
			
			if (!FoundFlag)
			{
				// Counter starts at zero, and increments
				SL_Src[Counter++] = new Option(FlipArray[CtlIndex][1][FKIndex][1][Loopy].text, FlipArray[CtlIndex][1][FKIndex][1][Loopy].value);
			}
		}
	}
}


// --------------------------------------------------------------------------------
// Flipping Combos Stuff
// --------------------------------------------------------------------------------


/*	
	--------------------------------------------------------------------------------
	Hopefully this example will help debug later...
	Structure:	FlipArray[x][0] = FieldName
				FlipArray[x][1] = Array

				FlipArray[x][1][0] = FK
				FlipArray[x][1][1] = Array

				FlipArray[x][1][1][0..N] = Options for this FK
	--------------------------------------------------------------------------------
*/

/*
Stuff('City',4,15,'花蓮縣');

Stuff('City',19,16,'苗栗縣');
Stuff('City',19,17,'臺中市');
Stuff('City',19,18,'臺中縣');
Stuff('City',19,19,'彰化縣');

Stuff('City',21,20,'南投縣');

Stuff('Bob',1,21,'基隆市');

document.write ('Num Fields : FlipArray.length: ' + FlipArray.length + '<br><br>');

document.write ('FieldName = FlipArray[0][0]: ' + FlipArray[0][0] + '<br>');
document.write ('FieldName = FlipArray[1][0]: ' + FlipArray[1][0] + '<br><br>');

document.write ('Citys numFK: FlipArray[0][1].length (3= 0,1,2): ' + FlipArray[0][1].length + '<br><br>');

document.write ('City FK = FlipArray[0][1][0][0]: ' + FlipArray[0][1][0][0] + '<br>');
document.write ('City FK = FlipArray[0][1][1][0]: ' + FlipArray[0][1][1][0] + '<br>');
document.write ('City FK = FlipArray[0][1][2][0]: ' + FlipArray[0][1][2][0] + '<br><br>');


document.write ('FK 4s NumOptions = FlipArray[0][1][0][1].length: ' + FlipArray[0][1][0][1].length + '<br>');
document.write ('FK 19s NumOptions = FlipArray[0][1][1][1].length: ' + FlipArray[0][1][1][1].length + '<br>');
document.write ('FK 21s NumOptions = FlipArray[0][1][2][1].length: ' + FlipArray[0][1][2][1].length + '<br><br>');



document.write ('19s options = FlipArray[0][1][1][1][0]: ' + FlipArray[0][1][1][1][0] + '<br>');
document.write ('19s options = FlipArray[0][1][1][1][1]: ' + FlipArray[0][1][1][1][1] + '<br>');
document.write ('19s options = FlipArray[0][1][1][1][2]: ' + FlipArray[0][1][1][1][2] + '<br>');
document.write ('19s options = FlipArray[0][1][1][1][3]: ' + FlipArray[0][1][1][1][3] + '<br>');

*/

function Flip(FormID, FK, Src, Dest, DefaultVal, LoadMode)
{
	var FormString = "CMS_Form_" + FormID;
	var Loopy;
	
	//alert('FormString:' + FormString + '\n' + 'FK:' + FK + '\n' + 'Src:' + Src + '\n' + 'Dest:' + Dest + '\n' + 'DefaultVal:' + DefaultVal + '\n')

	var ComboOptions_Dest;
	var NumCtls;		// total number of controls in the flip array
	var CtlIndex=-1;	// position of this control in the flip array

	var NumFK;			// number of foreign key groups for this control (ie parent variations)
	var FKIndex =-1;
	var NumOptions;
	var SelectedOption = 0;

	if (document.forms[FormString] != null)								// normal combos
	{
		ComboOptions_Dest = document.forms[FormString][Dest].options;
	}
	else																// iFrame flipping the parent
	{
		ComboOptions_Dest = parent.document.forms[FormString][Dest].options;
	}

	for (Loopy = ComboOptions_Dest.length-1; Loopy > 0; Loopy--)
	{
		ComboOptions_Dest[Loopy] = null;						// destroy everything but the zero
	}
	//alert('Dest:' + Dest + '\nComboOptions_Dest.length')

	NumCtls = FlipArray.length;									


	for (Loopy = 0; Loopy < NumCtls; Loopy++)					// find our combo by field name
	{
		if (LoadMode == 'BySrc')
		{
			if (FlipArray[Loopy][0] == Src)						// set the position marker for the Src control's index in the FlipArray
			{
				CtlIndex = Loopy;
				break;
			}
		}
		else
		{
			if (FlipArray[Loopy][0] == Dest)					// set the position marker for the control's index in the FlipArray
			{
				CtlIndex = Loopy;
				break;
			}
		}
	}

	if (CtlIndex != -1)											// this can happen if the combo has no options at all
	{
		NumFK = FlipArray[CtlIndex][1].length;						// each FK has its own array of subkeys; NumFK is the number of FK groups for this ctl

		for (Loopy = 0; Loopy < NumFK; Loopy++)
		{
			if (FlipArray[CtlIndex][1][Loopy][0] == FK)				// set the position marker for the this FK in the FlipArray
			{
				FKIndex = Loopy;
				break;
			}
		}

		if (FKIndex == -1)
		{
			NumOptions = -1
		}
		else
		{
			NumOptions = FlipArray[CtlIndex][1][FKIndex][1].length;		// number of subkey options for this control
		}
		
		for (Loopy = 0; Loopy < NumOptions; Loopy++)
		{
			if (DefaultVal == FlipArray[CtlIndex][1][FKIndex][1][Loopy].value)
			{
				SelectedOption = Loopy+1;
			}
			ComboOptions_Dest[Loopy+1] = new Option(FlipArray[CtlIndex][1][FKIndex][1][Loopy].text, FlipArray[CtlIndex][1][FKIndex][1][Loopy].value);
		}
		ComboOptions_Dest.selectedIndex = SelectedOption;
	}
}


function Stuff_AddField(FieldName)
{
	var PrevLength;
	var FieldIndex;
	var Loopy;

	PrevLength = FlipArray.length;
	FieldIndex = PrevLength;

	for (Loopy = 0; Loopy < PrevLength; Loopy++)				// go through the FieldNames
	{
		if (FlipArray[Loopy][0] == FieldName)					// if we found the key, set the position marker
		{
			FieldIndex = Loopy;
			break;
		}
	}

	if (FieldIndex == PrevLength)							// means we didn't find the name, so we need to add a top-level array
	{
		FlipArray[FieldIndex] = new Array();				// automatically resizes FlipArray to 1 bigger than before
		FlipArray[FieldIndex][0] = FieldName;
		FlipArray[FieldIndex][1] = new Array();
		return FieldIndex;
	}
	else
	{
		alert('Error in iCMS.js :: Stuff_AddField() has already inserted field ' + FieldName + ' at index ' + FieldIndex);
		return -1;
	}
}

function Stuff_AddFK(FieldIndex, FK)
{
	var PrevLength;
	var FKIndex;
	var Loopy;

	PrevLength = FlipArray[FieldIndex][1].length;
	FKIndex = PrevLength;
	
	for (Loopy = 0; Loopy < PrevLength; Loopy++)				// go through the FieldNames
	{
		if (FlipArray[FieldIndex][1][Loopy][0] == FK)					// if we found the key, set the position marker
		{
			FKIndex = Loopy;
			break;
		}
	}
	
	if (FKIndex == PrevLength)										// means we didn't find the FK
	{
		FlipArray[FieldIndex][1][FKIndex] = new Array();			// automatically resizes FlipArray to 1 bigger than before
		FlipArray[FieldIndex][1][FKIndex][0] = FK;				
		FlipArray[FieldIndex][1][FKIndex][1] = new Array();
	}
	return FKIndex;
}

function Stuff(FieldIndex, FKIndex, OptValue, OptText)
{
	var Loopy;
	var PrevLength;

	PrevLength = FlipArray[FieldIndex][1][FKIndex][1].length;		// automatically resizes FlipArray to 1 bigger than before
	FlipArray[FieldIndex][1][FKIndex][1][PrevLength] = new Option(OptText, OptValue, false, true);
}

/*		LEAVE THIS HERE FOR REFERENCE! It helps to understand Flipping
function Stuff(FieldName, FK, OptValue, OptText)
{
	var Loopy;
	var PrevLength;
	var KeyIndex;
	var FKIndex;

	PrevLength = FlipArray.length;
	KeyIndex = PrevLength;
	for (Loopy = 0; Loopy < PrevLength; Loopy++)				// go through the FieldNames
	{
		if (FlipArray[Loopy][0] == FieldName)					// if we found the key, set the position marker
		{
			KeyIndex = Loopy;
			break;
		}
	}
	
	if (KeyIndex == PrevLength)							// means we didn't find the name, so we need to add a top-level array
	{
		FlipArray[KeyIndex] = new Array();				// automatically resizes FlipArray to 1 bigger than before
		FlipArray[KeyIndex][0] = FieldName;
		FlipArray[KeyIndex][1] = new Array();
	}

	PrevLength = FlipArray[KeyIndex][1].length;
	FKIndex = PrevLength;
	for (Loopy = 0; Loopy < PrevLength; Loopy++)				// go through the FieldNames
	{
		if (FlipArray[KeyIndex][1][Loopy][0] == FK)					// if we found the key, set the position marker
		{
			FKIndex = Loopy;
			break;
		}
	}
	
	if (FKIndex == PrevLength)									// means we didn't find the FK
	{
		FlipArray[KeyIndex][1][FKIndex] = new Array();			// automatically resizes FlipArray to 1 bigger than before
		FlipArray[KeyIndex][1][FKIndex][0] = FK;				
		FlipArray[KeyIndex][1][FKIndex][1] = new Array();
	}

	PrevLength = FlipArray[KeyIndex][1][FKIndex][1].length;		// automatically resizes FlipArray to 1 bigger than before
	FlipArray[KeyIndex][1][FKIndex][1][PrevLength] = new Option(OptText, OptValue, false, true);
}
*/

// ---------------------------------------------------------------------------------------------------------------
// iTree
// ---------------------------------------------------------------------------------------------------------------

function iTreeClick(FormID, MenuID, FieldName)
{
	var FormString = "CMS_Form_" + FormID;
	
	//alert('Old val ' + document.forms[FormString][FieldName].value);
	document.forms[FormString][FieldName].value = MenuID;
	//alert('CMS mode! ' + document.forms[FormString][FieldName].value);
}

function iTreeClear(FormID, FieldName)
{
	var FormString = "CMS_Form_" + FormID;
	
	//alert('Old val ' + document.forms[FormString][FieldName].value);
	document.forms[FormString][FieldName].value = 0;
	//alert('CMS mode! ' + document.forms[FormString][FieldName].value);
}


// ---------------------------------------------------------------------------------------------------------------
// Select_Filter: uses a keyed hint to jump to the right place in a huge combo or selectlist
// ---------------------------------------------------------------------------------------------------------------
function Select_Filter(pattern, WhichList)
{
	var pattern = new RegExp('^' + pattern, "i");
	var OptionSet = WhichList.options;
	var NumOptions = OptionSet.length;
	var Loopy=0;
	var TargetIdx=0;

	while(Loopy < NumOptions)
	{
		if (pattern.test(OptionSet[Loopy].text))
		{
			TargetIdx = Loopy;
			break;
		}
		Loopy++;
	}
	WhichList.options.selectedIndex = TargetIdx;
}


// ---------------------------------------------------------------------------------------------------------------
// DHTML Functions
// ---------------------------------------------------------------------------------------------------------------


/*---------------------------------------------------------------------------------------------------------------
函式名稱：	WriteLayer(LayerID, Content)
撰寫日期：	2006-08-08
撰寫人員：	Sunil Narayan
函式功能：	Writes content to a div/span
參數：		- LayerID:	identifier of layer to write to
			- Content: text or html content to put in the layer
使用範例：	WriteLayer('BoboTable', 'hello')
---------------------------------------------------------------------------------------------------------------*/

function WriteLayer(LayerID, Content)
{
	if(document.getElementById)
	{
		document.getElementById(LayerID).innerHTML=Content;
	}
	else if(document.all)
	{
		document.all[LayerID].innerHTML=Content;
	}
	else if(document.layers)
	{
		document.layers[LayerID].document.open();
		document.layers[LayerID].document.write(Content);
		document.layers[LayerID].document.close();
	}
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：	ReadLayer(LayerID)
撰寫日期：	2006-11-22
撰寫人員：	Sunil Narayan
函式功能：	grabs the content of a div/span/td
參數：		- LayerID:	identifier of layer to read
使用範例：	x = ReadLayer('BoboTable')
---------------------------------------------------------------------------------------------------------------*/

function ReadLayer(LayerID)
{
	var RetVal;

	if(document.getElementById)
	{
		RetVal = document.getElementById(LayerID).innerHTML;
	}
	else if(document.all)
	{
		RetVal = document.all[LayerID].innerHTML;
	}
	else if(document.layers)
	{
		//RetVal = document.layers[LayerID].document;	// not sure if this is right...
	}
	return RetVal;
}


/*---------------------------------------------------------------------------------------------------------------
函式名稱：	Position
撰寫日期：	2006-05-03
撰寫人員：	Matt Kruse (http://www.JavascriptToolbox.com/)
函式功能：	class to get/set element item's position on the page

Methods：	Position.resolveObject()	// Resolve a string identifier to an element
			Position.get()				// Retrieve the position and size of an element
			Position.getCenter()		// Retrieve the position of an element's center point
			Position.set()				// Set the position of an object
			Position.resize()			// Sets the size of an object
			Position.clone()			// moves and resizes a source element to the target element's location & dimensions
----------------------------------------------------------------------------
使用範例：	Position.get(), Position.getCenter()
Inputs:		Element (by object or by string)
Outputs:	object with 4 properties: .left, .top, .height, .width

			var obj_Element = document.getElementById(ElementName_String)	// retrieve the element object by name
			var obj_Element = Position.resolveObject(ElementName_String)	// same thing

			var pos = Position.get(obj_Element)								// retrieve position by object
			var pos = Position.get(ElementName_String)						// retrieve position by element name

			alert (pos.left)
----------------------------------------------------------------------------
使用範例：	Position.set()
Inputs:		- Element (by object or by string), left, top
Outputs:	nothing

			Position.set(obj_Element, 100, 50)								// set by object, x=100, y=50
			Position.set(ElementName_String, 100, 50)						// set by name, x=100, y=50

----------------------------------------------------------------------------
使用範例：	Position.resize()
Inputs:		- Element (by object or by string), width, widthUnit, height, heightUnit
Outputs:	true / false for succeed/fail

			Position.resize(obj_Element, 100, 'px', 50, '%')				// set by object, width=100px, height=50%
----------------------------------------------------------------------------
使用範例：	Position.clone()
Inputs:		- ElementToMove, ElementDestination	 (by object or by string)
Outputs:	object with 4 properties: .left, .top, .height, .width

			Position.clone('iFrame_Hider', 'NavCat_1_3')					// set by object, width=100px, height=50%
---------------------------------------------------------------------------------------------------------------*/

var Position =
(
	function()
	{
		// -------------------------------------------
		// Resolve a string identifier to an object
		// -------------------------------------------
		function resolveObject(s)
		{
			if (document.getElementById && document.getElementById(s)!=null)
			{
				return document.getElementById(s);
			}
			else if (document.all && document.all[s]!=null)
			{
				return document.all[s];
			}
			else if (document.anchors && document.anchors.length && document.anchors.length>0 && document.anchors[0].x)
			{
				for (var i=0; i<document.anchors.length; i++)
				{
					if (document.anchors[i].name==s)
					{
						return document.anchors[i]
					}
				}
			}
			alert
		}

		var pos = {};

		// -----------------------------
		// Set the position of an object
		// -----------------------------
		pos.set = function(o,left,top)
		{
			if (typeof(o)=="string")
			{
				o = resolveObject(o);
			}
			if (o==null || !o.style)
			{
				return false;
			}
			o.style.position = "absolute";

			if (typeof(left)=="object")	// If the second parameter is an object, it is assumed to be the result of getPosition()
			{
				var pos = left;
				left = pos.left;
				top = pos.top;
			}

			o.style.left = left + "px";
			o.style.top = top + "px";
			return true;
		};

		// -----------------------------
		// Sets the size of an object
		// -----------------------------
		pos.resize = function(o, width, widthUnit, height, heightUnit)
		{
			if (typeof(o)=="string")
			{
				o = resolveObject(o);
			}
			if (o==null || !o.style)
			{
				return false;
			}

			o.style.width = width + widthUnit;
			o.style.height = height + heightUnit;
			return true;
		};

		// -------------------------------------------
		// Retrieve the position and size of an object
		// -------------------------------------------
		pos.get = function(o)
		{
			var fixBrowserQuirks = true;
			if (typeof(o)=="string")	// If a string is passed in instead of an object ref, resolve it
			{
				o = resolveObject(o);
			}

			if (o==null)
			{
				return null;
			}

			var left = 0;
			var top = 0;
			var width = 0;
			var height = 0;
			var parentNode = null;
			var offsetParent = null;
	
			offsetParent = o.offsetParent;
			var originalObject = o;
			var el = o; // "el" will be nodes as we walk up, "o" will be saved for offsetParent references

			while (el.parentNode!=null)
			{
				el = el.parentNode;
				if (el.offsetParent==null)
				{
				}
				else
				{
					var considerScroll = true;
					/*
					In Opera, if parentNode of the first object is scrollable, then offsetLeft/offsetTop already
					take its scroll position into account. If elements further up the chain are scrollable, their
					scroll offsets still need to be added in. And for some reason, TR nodes have a scrolltop value
					which must be ignored.
					*/
					if (fixBrowserQuirks && window.opera)
					{
						if (el==originalObject.parentNode || el.nodeName=="TR")
						{
							considerScroll = false;
						}
					}
					if (considerScroll)
					{
						if (el.scrollTop && el.scrollTop>0)
						{
							top -= el.scrollTop;
						}
						if (el.scrollLeft && el.scrollLeft>0)
						{
							left -= el.scrollLeft;
						}
					}
				}

				if (el == offsetParent)		// If this node is also the offsetParent, add on the offsets and reset to the new offsetParent
				{
					left += o.offsetLeft;
					if (el.clientLeft && el.nodeName!="TABLE")
					{
						left += el.clientLeft;
					}

					top += o.offsetTop;
					if (el.clientTop && el.nodeName!="TABLE")
					{
						top += el.clientTop;
					}
					o = el;
					if (o.offsetParent==null)
					{
						if (o.offsetLeft)
						{
							left += o.offsetLeft;
						}
						if (o.offsetTop)
						{
							top += o.offsetTop;
						}
					}
					offsetParent = o.offsetParent;
				}
			}

			if (originalObject.offsetWidth)
			{
				width = originalObject.offsetWidth;
			}
			if (originalObject.offsetHeight)
			{
				height = originalObject.offsetHeight;
			}

			return {'left':left, 'top':top, 'width':width, 'height':height};
		};

		// -------------------------------------------------
		// Retrieve the position of an object's center point
		// -------------------------------------------------
		pos.getCenter = function(o)
		{
			var c = this.get(o);
			if (c==null)
			{
				return null;
			}
			c.left = c.left + (c.width/2);
			c.top = c.top + (c.height/2);
			return c;
		};
		
		// --------------------------------------------------------------------------------
		// moves and resizes a source element to the target element's location & dimensions
		// --------------------------------------------------------------------------------
		/*pos.clone = function(oSrc, oDest)
		{
			var c = this.get(oDest);

			if (c == null)
			{
				return null;
			}

			this.set(oSrc, c.left, c.top);
			this.resize (oSrc, c.width, 'px', c.height, 'px')

			return cDest;
		};
		*/
		return pos;
	}
)();


function SetupNavMenu(WhichMenu)
{
	if (document.all&&document.getElementById)
	{
		var XMenu = document.getElementById("Nav_Menu_" + WhichMenu);
		var navRoot;
		var subMenuRoot;	// the child menu list
		navRoot = XMenu.childNodes[0];	// locate the UL: the first item is always UL, so just use 0 instead of parsing for it
		// --------------------------------
		// mouseover for category LI blocks
		// --------------------------------
		for (i=0; i < navRoot.childNodes.length; i++)
		{
			node = navRoot.childNodes[i];	//if (node.nodeName=="LI")	// don't need this check because the ul ONLY contains li children
			node.onmouseover=function()
				{
					this.className+=" over";
					SetNavMenuState(WhichMenu, true);
					Shim_Show(this);
				}
			node.onmouseout= function()
				{
					this.className=this.className.replace(" over", "");
					SetNavMenuState(WhichMenu, false);
				}

			// ---------------------------------
			// then locate the subMenu UL somewhere inside the LI
			// ---------------------------------
			for (j=0; j < node.childNodes.length; j++)		
			{
				subMenuNode = node.childNodes[j];
				if (subMenuNode.nodeName=="UL")
				{
					subMenuRoot = subMenuNode;
					break;
				}
			}
						
			// --------------------------------
			// mouseover for subMenu LI blocks
			// --------------------------------
			for (j=0; j < subMenuRoot.childNodes.length; j++)
			{
				subMenuNode = subMenuRoot.childNodes[j];	//if (node.nodeName=="LI")	// don't need this check because the ul ONLY contains li children
				subMenuNode.onmouseover=function()
					{
						SetNavMenuState(WhichMenu, true);
					}
				subMenuNode.onmouseout= function()
					{
						SetNavMenuState(WhichMenu, false);
					}
			}
		}
	
		// --------------------------------------------------------
		// mouseover for TD block
		// --------------------------------------------------------
		node = document.getElementById("Nav_Menu_TD_" + WhichMenu);
		node.onmouseover=function()
			{
				SetNavMenuState(WhichMenu, true)
			}
		node.onmouseout= function()
			{
				SetNavMenuState(WhichMenu, false)
			}

		// --------------------------------------------------------
		// move menus to right spot
		// --------------------------------------------------------
		NavMenu_Reposition(1);
		NavMenu_Reposition(2);
	}
}

function NavMenu_Reposition(WhichMenu)
{
	var obj_pos;
	var NewPosnX;
	var NewPosnY;
	var NewWidth;
	var NewHeight;

	obj_pos = Position.get('Nav_Menu_TD_' + WhichMenu);
	NewPosnY = obj_pos.top + obj_pos.height - 2;
	NewPosnX = obj_pos.left - 2;
	
	Position.set('Nav_Menu_' + WhichMenu, NewPosnX, NewPosnY);
	Position.set('Nav_Menu_iFrame_' + WhichMenu, NewPosnX, NewPosnY);
}

function SetNavMenuState(WhichMenu, WhichState)
{
	var XMenu = document.getElementById("Nav_Menu_" + WhichMenu);			// this refers to the div container of the whole menu
	var XFrame = document.getElementById("Nav_Menu_iFrame_" + WhichMenu);
	var OtherMenu = (WhichMenu == 1)?2:1;
	var obj_OtherMenu = document.getElementById("Nav_Menu_" + OtherMenu);
	var obj_OtherFrame = document.getElementById("Nav_Menu_iFrame_" + OtherMenu);
	
	if (WhichState == true)
	{
		if(obj_OtherMenu != null)
		{
			if (obj_OtherMenu.style.display == "block")		// first order of the day: close the other menu immediately if it's open
			{
				MenuState[OtherMenu][0] = false;
				obj_OtherMenu.style.display == "none";
				obj_OtherFrame.style.display == "none";
			}
		}
		MenuState[WhichMenu][0] = true;
		if (XMenu.style.display == "none")
		{
			XFrame.style.display = "block";
			XMenu.style.display = "block";
		}
	}
	else
	{
		MenuState[WhichMenu][0] = false;
		timer = setTimeout('NavMenu_Hide(' + WhichMenu + ')', 500);	// insert a 0.5 sec delay before hiding, just in case the user moved to a submenu or something
	}
}

function NavMenu_Hide(WhichMenu)
{
	var XMenu = document.getElementById("Nav_Menu_" + WhichMenu);			// this refers to the div container of the whole menu
	var XFrame = document.getElementById("Nav_Menu_iFrame_" + WhichMenu);

	if (MenuState[WhichMenu][0] == false && XMenu.style.display == "block")		// if after delaying it's still false
	{
		Shim_Hide();
		XFrame.style.display = "none";
		XMenu.style.display = "none";
	}
}

function Shim_Show(WhichBlock)
{
	//alert(WhichBlock.id);
	var obj_pos;
	var NewPosnX;
	var NewPosnY;
	var NewHeight;
	var WhichMenu = WhichBlock.id.substr(8, 1);		// for eg, NavMenu_1_12, we need the 1
	var WhichSubMenu = WhichBlock.id.substr(10);	// for eg, NavMenu_1_12, we need the 12
	var Shim = document.getElementById('iFrame_Shim');			

	obj_pos = Position.get(WhichBlock);
	NewPosnY = obj_pos.top -2;
	NewPosnX = obj_pos.left + obj_pos.width -2;
	NewHeight = MenuItemHeight * MenuState[WhichMenu][WhichSubMenu];

	Position.resize('iFrame_Shim', MenuItemWidth, 'px', NewHeight, 'px');
	Position.set('iFrame_Shim', NewPosnX, NewPosnY);

	if (Shim.style.display = "none")
	{
		Shim.style.display = "block";
	}
}

function Shim_Hide()
{
	var Shim = document.getElementById('iFrame_Shim');			
	if (Shim.style.display = "block")
	{
		Shim.style.display = "none";
	}
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：	WriteLayer(LayerID, Content)
撰寫日期：	2006-08-08
撰寫人員：	Sunil Narayan
函式功能：	Writes content to a div/span
參數：		- LayerID:	identifier of layer to write to
			- Content: text or html content to put in the layer
使用範例：	WriteLayer('BoboTable', 'hello')
---------------------------------------------------------------------------------------------------------------*/

function WriteLayer(LayerID, Content)
{
	if(document.getElementById)
	{
		document.getElementById(LayerID).innerHTML=Content;
	}
	else if(document.all)
	{
		document.all[LayerID].innerHTML=Content;
	}
	else if(document.layers)
	{
		document.layers[LayerID].document.open();
		document.layers[LayerID].document.write(Content);
		document.layers[LayerID].document.close();
	}
}

/*---------------------------------------------------------------------------------------------------------------
函式名稱：	clock(TimeZone, TargetLayer)
撰寫日期：	2006/8/8
撰寫人員：	Sunil
函式功能：	Display the running clock
參數：		- TimeZone: 
			- TargetLayer: 

使用範例：	clock(8, 'ClockLayer')

// http://www.merlyn.demon.co.uk/js-date4.htm
// http://www.maxxblade.co.uk/clock/
// http://www.irt.org/script/date.htm
---------------------------------------------------------------------------------------------------------------*/
function clock_GetDate(TimeZone)
{
	var obj_Date = new Date();

	x = new Date(obj_Date.getUTCFullYear(), obj_Date.getUTCMonth(), obj_Date.getUTCDate(), obj_Date.getUTCHours(), obj_Date.getUTCMinutes(), obj_Date.getUTCSeconds()); 
	x.setTime(x.getTime()+ TimeZone * 60*60*1000); 
	return x; 
}

function ShowClock(TimeZone, TargetLayer, XMode)
{
	var obj_Date = clock_GetDate(TimeZone);
	var hours = obj_Date.getHours();
	var minutes = obj_Date.getMinutes();
	var seconds = obj_Date.getSeconds();
	var ampm = '';
	var d = obj_Date.getDate();
	var m = obj_Date.getMonth();
	var y = obj_Date.getFullYear();
	var Days_Array = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
	var ShortDays_Array = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
	var Months_Array = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	var NextRun;

	var nday = obj_Date.getDay();
	var dispTime;

	obj_Date.setSeconds( seconds+1 );

	if (minutes < 10) minutes = '0' + minutes;
	if (seconds < 10) seconds = '0' + seconds;

	if (XMode == 'short')
	{
		m++;
	}
	if (m < 10)
	{
		m = '0' + m;
	}
	if (d < 10)
	{
		d = '0' + d;
	}

	/*
	ampm	= (hours > 11) ? "pm" : "am";
	hours	= (hours > 12) ? hours-12 : hours;
	hours	= (hours == 0) ? 12 : hours;  
	*/
	//dispTime = Days_Array[nday] + ", " + " " + Months_Array[m] + " "+d+ ", " + y + " &nbsp;&nbsp;"+hours + ":" + minutes + ":" + seconds + " " + ampm + '&nbsp;&nbsp;'
	
	switch (XMode)
	{
		case 'long':
			dispTime = Days_Array[nday] + '&nbsp;&nbsp;' + y + '.' + m + '.' + d + '&nbsp;&nbsp;' + hours + ":" + minutes //+ ":" + seconds + '&nbsp;&nbsp;'
			break;
		case 'short':
			dispTime = ShortDays_Array[nday] + '&nbsp;&nbsp;' + m + '.' + d + '&nbsp;&nbsp;' + hours + ":" + minutes //+ ":" + seconds + '&nbsp;&nbsp;'
			break;
	}
	
	WriteLayer( TargetLayer, dispTime );

	NextRun = (60 - eval(seconds)) *  1000;	// update once per minute

	setTimeout("ShowClock(" + TimeZone + ",'" + TargetLayer + "','" + XMode + "')", NextRun);	
}


/*---------------------------------------------------------------------------------------------------------------
函式名稱：SearchJump()
撰寫日期：2006/11/22
撰寫人員：Sunil Narayan
函式功能：Jump directly from a search combo to the CMS
參數：	- CMSID：iSite_CMSID
使用範例：CalcPaymentSummary('Quotation1')
---------------------------------------------------------------------------------------------------------------*/
function SearchJump(CMSID, CMS_Form, prm_MasterPK_Val)
{
	var Page_Location;

	//alert('CMSID: ' + CMSID + '\nCMS_Form: ' + CMS_Form + '\nprm_MasterPK_Val: ' + prm_MasterPK_Val);
	
	Page_Location = CMS_Form + '?prm_CMSID=' + CMSID + '&prm_MasterPK_Val=' + prm_MasterPK_Val;
	document.location = Page_Location;
}


function removeElement(ParentElement, ChildElement) 
{
	var Parent = document.getElementById(ParentElement);
	var Child = document.getElementById(ChildElement);
	Parent.removeChild(Child);
}