/*
Modification log:
	19/1/2009 - Drop first 0 in function getISODay
	16/1/2009 - CSS Class 
		function getObjCSSClass return object's CSS class
		function hasObjCSSClass check whether the object's CSS class is set
		function setObjCSSClass set object's CSS class 
		function getObjByName retrieve object by name 
		function getCtrlCSSClass return return cotrol's CSS class name
		function hasCtrlCSSClass check whether the control's CSS class is set
		function setCtrlCSSClass return return cotrol's CSS class name
		function dateCtrl::setCSSClass set CSS class for date control
	15/1/2009 - Date control
		function initDateCtrl initiate the date control
		function lkDateCtrl link the date control
		Class dateCtrl compile date related component in the object
		Even dateCtrl::onChangeDate even of changing date of the control
		Even dateCtrl::onChangeYear even of changing year of the control
		Even dateCtrl::onChangeMonth even of changing month of the control
		Even dateCtrl::onChangeDay even of changing day of the control
		function dateCtrl::refresh refresh method of date control
		function dateCtrl::ChangeYear even of changing year control
		function dateCtrl::ChangeMonth even of changing month control
		function dateCtrl::ChangeDay even of changing day control
		function dateCtrl::setDate set date of the control
		function dateCtrl::cbY::onChange even of change year control
		function dateCtrl::cbM::onChange even of change month control
		function dateCtrl::cbD::onChange even of change day control
	26/11/2008 - Serve keypress event with FireFox
		function cthEvent catch event object
		function txtInt changed
		function txtNumeric changed
	21/11/2008 - Hide div and not occupy space of page
		function hideDiv changed
		function showDiv changed
	10/10/2008 - add get object x, y position function
		function getObjLeft get object x position of the page
		function getObjTop get object y position of the page
	22/08/2008 - Background color changing functions
		String obj::sOriBGColor Original color
		function chBGColor change background color
		function restoreBGColor restore back the original color
	20/08/2008 - prompt dialog resizable.
		function promptDialog changed:
			add new opening option resizable=yes
			add new param @param - String - sName - Dialog name
			return openned window object
	30/07/2008 - make modification getAttribute() is not allowed for oStyle, it serve for component
		function isDivVisibile check whether the div is visible.
		function hideDiv hide the div
		function setDivVisibility set vibility of div.
	25/09/2007 - Date control function.
		function getDtByISO generate date object by pass-in ISO date format.
		function getISODt generate ISO date format by pass-in date object.
	04/10/2007 - Form backup & restore function
		function form::bkSetting backup form setting.
		function form::rsSetting restore form setting from back value..
		function getTransFrm set the 2 bove methods before export Master form object.
	05/10/2007 - Add DIV functions.
		function getDiv return div.
		function setDivVisibility set vibility of div.
		function isDivVisibile check whether the div is visible.
		function showDiv show the div
		function hideDiv hide the div
	
*/
/**
 * common.js keeps common javascript function.
 * For Foundermall SMS system.
 * Version: 1.1.9  Date: 19/1/2009
*/
// -------------------------------------------------------------------------------------------------------------
/**
	* function chkTransFrmExt is used to check whether the transaction form existed in the user interface.
	*@return - Boolean - Flag to indicate whether the form is existed.
*/
function chkTransFrmExt()
{
	var bResult = false;
	// Make sure the form is exited.
	if(typeof(document.Master) == 'undefined') return bResult;
	bResult = true;
	return bResult;
}//End of function chkTransFrmExt
// -------------------------------------------------------------------------------------------------------------
/**
	* function getTransFrm return the transaction form.
	* It return false while the transaction form is not existed.
	*@return - object - Transaction form.
*/
function getTransFrm()
{
	var result = false;
	// Make sure the form is existed.
	if(!chkTransFrmExt()) return result;

	result = document.Master;
	
	// Extend the methods.Lay 04/10/2007
	result.bkSetting = formBKSetting;
	result.rsSetting = formRSSetting;
	return result;
}//End of function getTransFrm
// -------------------------------------------------------------------------------------------------------------
/**
	* function chkTransFieldExt check whether the field is existed in the transaction form by pass-in the field name.
	*@param - String - Field's name.
	*@return - Boolean - Flag indicate whether the field is existed.
*/
function chkTransFieldExt(strFieldName)
{
	var bResult = false;
	// Make sure the pass-in is string.
	if(typeof(strFieldName) != 'string') return bResult;

	var oTransFrm = getTransFrm();
	// Make sure the transaction form is existed.
	if(oTransFrm == false) return bResult;

	// Fetch the control.
	var oCtrl = oTransFrm.elements[strFieldName];
	// Make sure the control is existed.
	if(typeof(oCtrl) == 'undefined') return bResult;
	// Make sure it has value. (No need to make sure)
	// if(typeof(oCtrl.value) != 'string') return bResult;

	bResult = true;
	return bResult;
}//End of function chkTransFieldExt
// -------------------------------------------------------------------------------------------------------------
/**
	* funtion getTransFieldCtrl return the control of the transaction field.
	* It return false while the transaction form is not existed.
	*@param - String - Field's name.
	*@return - object - Field control.
*/
function getTransFieldCtrl(strFieldName)
{
	var result = false;
	// Make sure it is existed.
	if(!chkTransFieldExt(strFieldName)) return result;

	var oTransFrm = getTransFrm();
	// Make sure the transaction form is existed.
	if(oTransFrm == false) return result;
	// Fetch the control.
	var oCtrl = oTransFrm.elements[strFieldName];
	// Make sure the control is existed.
	if(typeof(oCtrl) == 'undefined') return result;
	result = oCtrl;

	return result;
}//End of function getTransFieldCtrl
// -------------------------------------------------------------------------------------------------------------
/**
	* function getTransFieldValue return the value of the transaction field.
	*@param - String - Field's name.
	*@return - String - Field's value.
*/
function getTransFieldValue(strFieldName)
{
	var strResult = "";
	var oCtrl = getTransFieldCtrl(strFieldName);
	if(oCtrl == false) return strResult;
	// Make sure it has value.
	if(typeof(oCtrl.value) != 'string') return strResult;
	strResult = oCtrl.value;
	return strResult;
}//End of function getTransFieldValue
// -------------------------------------------------------------------------------------------------------------
/**
	* function getTransRowFieldNumber return the number of field that apply the field name.
	*@param - String - Field's name.
	*@return - Integer - Number of fields.
*/
function getTransRowFieldNumber(strFieldName)
{
	var intResult = 0;

	// Make sure pass-in field name is string.
	if(typeof(strFieldName) != 'string') return intResult;

	// Retrieve the field list.
	// Compose the field list name.
	var strRowFieldName = strFieldName + '[]';
	var arrList = getTransFieldCtrl(strRowFieldName);
	if(arrList != false)
	{ // The list is found.
		intResult = 1; // At least one field.
		if(typeof(arrList.length) != 'undefined')
		{
			intResult = arrList.length;
		}//End of if(typeof(arrList.length) != 'undefined')
	}//End of if(arrList != false)

	return intResult;
}//End of function getTransRowFieldNumber
// -------------------------------------------------------------------------------------------------------------
/**
	* function getTransRowFieldCtrl return the control of the row item of transaction by given field name.
	* It return false while the control is not found.
	*@param - String - Field's name.
	*@param - Integer - Row no.
	*@return - object - Field's control.
*/
function getTransRowFieldCtrl(strFieldName, rowIdx)
{
	var result = false;

	// Make sure pass-in field name is string.
	if(typeof(strFieldName) != 'string') return result;
	//Make sure pass-in row no is valid.
	var intRowIdx = parseInt(rowIdx);
	// Row no show be zero and positive.
	if(!(intRowIdx >= 0)) return result;
	// Compose the field list name.
	var strRowFieldName = strFieldName + '[]';
	var arrCtrl = getTransFieldCtrl(strRowFieldName);
	if(arrCtrl == false) return result;
	// Make sure it is not null
	if(typeof(arrCtrl) != 'object') return result;

	// Check whether it is array.
	var bIsArray = true;
	if(typeof(arrCtrl.length) == 'undefined') bIsArray = false;
	else if(arrCtrl.length == 0) bIsArray = false;

	if(!bIsArray)  // an array should have a value for length property.
	{// It is not an array.
		if(intRowIdx == 0)
		{
			// Take itself as the first item.
			if(typeof(arrCtrl) == 'object') result = arrCtrl;
		}//End of if(intRowIdx == 0)
		return result;
	}//End of if(!bIsArray)

	// Out of bound.
	if(intRowIdx >= arrCtrl.length) return result;
	result = arrCtrl[intRowIdx];

	return result;
}//End of function getTransRowFieldCtrl
// -------------------------------------------------------------------------------------------------------------
/**
	* function getTransRowFieldValue return the value of the row item of transaction by given field name.
	*@param - String - Field's name.
	*@param - Integer - Row no.
	*@return - String - Field's value.
*/
function getTransRowFieldValue(strFieldName, rowIdx)
{
	var strResult = "";
	// Get the row item control.
	var oCtrl = getTransRowFieldCtrl(strFieldName, rowIdx);
	// Make sure it is found.
	if(oCtrl == false) return strResult;
	// Make sure the control has value.
	if(typeof(oCtrl.value) == 'undefined') return strResult;

	strResult = oCtrl.value;
	return strResult;
}//End of function getTransRowFieldValue
/**
 * function isUndefined check whether the pass-in object is undefined.
 * @param - Variable - Object to be checked.
 * @return - Boolean - Flag indicate whether the pass-in is underfined.
*/
function isUndefined(obj)
{ // check whether the pass-in object is undefined.
	var bResult = true;
	if(obj == null) return bResult;
	if(typeof(obj) == 'undefined')  return bResult;
	bResult = false;
	return bResult;
} // End of function isUndefined
/**
 * function setWinStatus set content to window status bar.
 * @param - String - Message to be shown in windows status bar.
*/
function setWinStatus(sMsg)
{ // set content to window status bar.
	if(sMsg == null) return;
	if(typeof(sMsg) == 'undefined') return;
	
	window.status = sMsg;
} // End of function setWinStatus
/**
 * function refreshOpener refresh open dialog.
*/
function refreshOpener()
{ // refresh open dialog.
	if(window.opener) window.opener.location.reload();
} // End of function refreshOpener
/**
 * function promptDailog prompt dailog by pass-in URL.
 * @param - String - Page URL.
 * @param - Integer - Dialog width. Default = 100
 * @param - Integer - Dialog height. Default = 100
 * @param - String - sName - Dialog name
 * @return - window - Openned window
*/
function promptDailog(strURL, iWidth, iHeight, sName)
{ // prompt dailog by pass-in URL.
	//strURL = trim(strURL);
	if(strURL == '') return;
	if(isUndefined(iWidth)) iWidth = 100;
	if(isUndefined(iHeight)) iHeight = 100;
	if(isUndefined(sName)) sName = '_blank';
	var sAttr = 'menubar = no, location = no, resizable=yes, status = yes, scrollbars = yes';
	sAttr += ', width = ' + iWidth;
	sAttr += ', height = ' + iHeight;
	var newWin = window.open(strURL, sName,sAttr);
	newWin.width = iWidth;
	newWin.height = iHeight;
	if(newWin.focus) newWin.focus();
	return newWin;
} // End of function promptDailog
/**
 * function cthEvent catch event object
 * It return null while the event object is fail to caught
 * @return - Object - Event object
*/
function cthEvent()
{// catch event object
	var oResult = null;
	if(event) oResult = event;
	if(window.event) oResult = window.event;
	return oResult;
} // End of function cthEvent
/**
 * function txtNumeric make text accept numeric key only.
 * It should be call by even ONKEYPRESS.
 * @param - event - Component pass-in event
 * @return - Boolean - Flag indicate success or not.
*/
function txtNumeric(oEvent)
{
	// Handle pass-in event
	if(isUndefined(oEvent)) oEvent = cthEvent();// Call window event while it is not pass-in
	if(isUndefined(oEvent))
	{
		setWinStatus('Key press event missing!');
		return false; // Terminated while missing event object
	}// end of if(isUndefined(oEvent))
	var sKeyCode = oEvent.keyCode;
	if(!isUndefined(oEvent.which)) sKeyCode = oEvent.which;
	// 0 ~ 9
	if((sKeyCode >= 48 && sKeyCode <= 57)) return true;
	// 0 ~ 9 Small numpad area
	switch(sKeyCode)
	{
		//case 0:
		case 8: // Backspace
		case 9: // tab
		case 35: // End
		case 36: // Home
		case 37: // Move left
		case 39: // Move right
		case 46: // Delete
		case 109:
		case 110: // . - numpad
		case 189: // -
		case 190: // .
			return true;
	}// End of switch(sKeyCode)
	return false;
}// End of function txtNumeric
/**
 * function txtInt make text accept integer only.
 * It should be call by even ONKEYPRESS.
 * @param - event - Component pass-in event
 * @return - Boolean - Flag indicate success or not.
*/
function txtInt(oEvent)
{ // make text accept integer only.
	// Handle pass-in event
	// Handle pass-in event
	if(isUndefined(oEvent)) oEvent = cthEvent();// Call window event while it is not pass-in
	if(isUndefined(oEvent))
	{
		setWinStatus('Key press event missing!');
		return false; // Terminated while missing event object
	}// end of if(isUndefined(oEvent))
	var sKeyCode = oEvent.keyCode;
	if(!isUndefined(oEvent.which)) sKeyCode = oEvent.which;
	if(!txtNumeric(oEvent)) return false;
	
	switch(sKeyCode)
	{
		case 110: // . - numpad
		case 190: // .
		return false;
	}// End of switch(sKeyCode)
	return true;
} // End of function txtInt
/**
 * function isTextBox check whether the pass-in object is textbox.
 * @return - Boolean - Flag indicate success or not.
*/
function isTextBox(obj)
{ // check whether the pass-in object is textbox.
	var bResult = false;
	
	if(typeof(obj) != 'object')
	{
		setWinStatus('It\'s is not object!');
		return bResult;
	}// End of if(typeof(obj) != 'object')
	if(isUndefined(obj.type))
	{
		setWinStatus('It\'s is not input!');
		return bResult;
	}// End of if(isUndefined(obj.type))
	if(obj.type != 'text')
	{
		setWinStatus('It\'s type is not text!');
		return bResult;
	}// End of if(obj.type != 'text')
	
	bResult = true;
	return bResult;
} // End of function isTextBox
/**
 * function chkTxtValidNumeric check whether textbox content is valid numeric.
 * @return - Boolean - Flag indicate success or not.
*/
function chkTxtValidNumeric(oTxt)
{ // check whether textbox content is valid numeric.
	var bResult = true;
	// Do nothing while found it is not textbox.
	if(!isTextBox(oTxt)) return bResult;
	var sContent = oTxt.value;
	if(sContent == '') sContent = 0.00;
	var sConvert = parseFloat(sContent);
	oTxt.value = sConvert;
	bResult = true;
	return bResult;
} // End of function chkTxtValidNumeric
// ############################################################################ Date function
// -------------------------------------------------------------------------------------------------------------
/**
 * function isISODt check whether the pass-in is ISO date format.
 * @param - String - Date in ISO format.
 * @return - Boolean - Flag showing whether the pass-in is ISO date format.
*/
function isISODt(sDt)
{// check whether the pass-in is ISO date format.
	var bResult = false;
	if(sDt == '') return bResult;
	if(typeof(sDt) != 'string') return bResult;
	if(sDt.length < 8) return bResult;
	if(sDt.charAt(4) != '-') return bResult;
	if(sDt.charAt(6) != '-')
	{
		if(sDt.charAt(7) != '-') return bResult;
	}// End of if(sDt.charAt(6) != '-')
	bResult = true;
	return bResult;
} // End of function isISODt
// -------------------------------------------------------------------------------------------------------------
/**
 * function getISOYear get year part of ISO date format.
 It return 0 while the pass-in is not ISO date format.
 * @param - String - Date in ISO format.
 * @return - Integer - Year of the date.
*/
function getISOYear(sDt)
{ //  get year part of ISO date format.
	var iResult = 0;
	if(!isISODt(sDt)) return iResult;
	var sYear = sDt.substring(0,4);
	iResult = parseInt(sYear);
	return iResult;
} // End of function getISOYear
// -------------------------------------------------------------------------------------------------------------
/**
 * function getISOMonth get month part of ISO date format.
 It return 0 while the pass-in is not ISO date format.
 * @param - String - Date in ISO format.
 * @return - Integer - Month of the date.
*/
function getISOMonth(sDt)
{ //  get year part of ISO date format.
	var iResult = 0;
	if(!isISODt(sDt)) return iResult;
	var sMonth = sDt.substring(5,7);
	// Arrange the month segment.
	if(sMonth.charAt(0) == '-') sMonth = sMonth.substring(1);
	if(sMonth.charAt(0) == '0') sMonth = sMonth.substring(1);
	iResult = parseInt(sMonth);
	return iResult;
} // End of function getISOMonth
// -------------------------------------------------------------------------------------------------------------
/**
 * function getISODay get day part of ISO date format.
 It return 0 while the pass-in is not ISO date format.
 * @param - String - Date in ISO format.
 * @return - Integer - Day of the date.
*/
function getISODay(sDt)
{ //  get year part of ISO date format.
	var iResult = 0;
	if(!isISODt(sDt)) return iResult;
	var iLen = sDt.length;
	var sDay = sDt.substring(iLen - 2,iLen);
	// Drop first 0
	if(sDay.substring(0, 1) == '0') sDay = sDay.substring(1);
	iResult = parseInt(sDay);
	if(iResult < 0) iResult *= -1;
	return iResult;
} // End of function getISODay
// -------------------------------------------------------------------------------------------------------------
/**
 * function getDtByISO generate date object by pass-in ISO date format.
 * It return 0 while the pass-in is not a valid ISO date format.
 * @param - String - Date in ISO format.
 * @return - Date - Generated date object.
*/
function getDtByISO(sDt)
{//  generate date object by pass-in ISO date format.
	var dtResult = 0;
	if(!isISODt(sDt)) return dtResult;
	dtResult = new Date();
	var iY = getISOYear(sDt);
	var iM = getISOMonth(sDt);
	var iD = getISODay(sDt);
	dtResult.setYear(iY);
	dtResult.setMonth(iM - 1);
	dtResult.setDate(iD);
	return dtResult;
} // End of function getDtByISO
// -------------------------------------------------------------------------------------------------------------
/**
 * function getISODt generate ISO date format by pass-in date object.
 * It return empty string while is the pass-in is not a date object.
 * @param - Date - Date to be convert.
 * @return - String - Date in ISO format.
*/
function getISODt(dt)
{ // generate ISO date format by pass-in date object.
	var sResult = '';
	if(typeof(dt) != 'object') return sResult;
	if(!dt.getDate) return sResult;
	var iY = dt.getYear();
	if(iY < 1900) iY += 1900;
	sResult += iY;
	sResult += '-';
	var iM = dt.getMonth();
	iM ++;
	if(iM < 10) sResult += '0';
	sResult += iM;
	sResult += '-';
	var iD = dt.getDate();
	if(iD < 10) sResult += '0';
	sResult += iD;
	return sResult;
} // End of function getISODt
// -------------------------------------------------------------------------------------------------------------
/**
 * function chkDtCtrlExt check whether the date control is existed.
 * @param - String - Name of date control.
 * @return - Boolean - Flag show whether the date control is existed.
*/
function chkDtCtrlExt(sName)
{ // check whether the date control is existed.
	var bResult = false;
	if(isUndefined(sName))
	{
		var sMsg = 'Required pass-in date control name!';
		sMsg += 'Date control value retrieving terminated!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(isUndefined(sName))
	if(sName == '')
	{
		var sMsg = 'Date control name can not be empty!';
		sMsg += 'Date control value retrieving terminated!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(sName == '')
	// Get year control
	var oY = getTransFieldCtrl(sName + 'Y');
	if(oY == false)
	{
		// Compose message.
		var sMsg = sName;
		sMsg += 'Y year control missing!';
		sMsg += 'Date control value retrieving terminated!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(oY == false)
	// Get month control
	var oM = getTransFieldCtrl(sName + 'M');
	if(oM == false)
	{
		// Compose message.
		var sMsg = sName;
		sMsg += 'M month control missing!';
		sMsg += 'Date control value retrieving terminated!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(oM == false)
	// Get day control
	var oD = getTransFieldCtrl(sName + 'D');
	if(oD == false)
	{
		// Compose message.
		var sMsg = sName;
		sMsg += 'D day control missing!';
		sMsg += 'Date control value retrieving terminated!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(oD == false)
	bResult = true;
	return bResult;
} // End of function chkDtCtrlExt
// -------------------------------------------------------------------------------------------------------------
/**
 * function getDtCtrlVal return date control value.
 * It return 0 while the pass-in is not a valid ISO date format.
 * @param - String - Name of date control.
 * @return - Date - Value of date control
*/
function getDtCtrlVal(sName)
{ // return date control value.
	var dtResult = 0;
	if(!chkDtCtrlExt(sName)) return dtResult;
	
	// Get year control
	var oY = getTransFieldCtrl(sName + 'Y');
	var iY = parseInt(oY.value);
	// Get month control
	var oM = getTransFieldCtrl(sName + 'M');
	var iM = parseInt(oM.value);
	// Get day control
	var oD = getTransFieldCtrl(sName + 'D');
	var iD = parseInt(oD.value);
	dtResult = new Date();
	dtResult.setYear(iY);
	dtResult.setMonth(iM - 1);
	dtResult.setDate(iD);
	return dtResult;
} // End of function getDtCtrlVal
// -------------------------------------------------------------------------------------------------------------
/**
 * function setDtCtrl set date control value.
 * @param - String - Name of date control.
 * @param - Date - Date value to be set with the date control.
 * @return - Boolean - Flag indicate whether the process is run successfully
*/
function setDtCtrl(sName, dt)
{ // set date control value.
	var bResult = false;
	if(!chkDtCtrlExt(sName)) return bResult;
	
	// Make sure the pass-in is date.
	if(typeof(dt) != 'object') return bResult;
	if(!dt.getDate) return bResult;
	var iY = dt.getYear();
	if(iY < 1900) iY += 1900;
	var iM = dt.getMonth();
	var iD = dt.getDate();
	// Get year control
	var oY = getTransFieldCtrl(sName + 'Y');
	oY.value = iY;
	// Get month control
	var oM = getTransFieldCtrl(sName + 'M');
	oM.value = iM + 1;
	// Get day control
	var oD = getTransFieldCtrl(sName + 'D');
	oD.value = iD;
	
	return bResult;
} // End of function setDtCtrl
// -------------------------------------------------------------------------------------------------------------
/**
 * function lkDateCtrl link the date control
 * It make links between the components of the date control
 * @param - Object - Master object of the control
 * @return - Boolean - Flag telling whether the process is done
*/
function lkDateCtrl(oMaster)
{// link the date control
	var bResult = false;
	// Make sure the master object is existed
	if(isUndefined(oMaster))
	{
		// Compose message
		var sMsg = 'Master control missing!';
		sMsg += ' Date control initiation fail!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(isUndefined(oMaster))
	var sName = oMaster.name;
	// Define the 3 selector controls' name
	var sYName = sName + 'Y';
	var sMName = sName + 'M';
	var sDName = sName + 'D';
	var oY = document.getElementById(sYName);
	// Make sure the year object is existed
	if(isUndefined(oY))
	{
		// Compose message
		var sMsg = 'Year control missing!';
		sMsg += ' Date control initiation fail!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(isUndefined(oY))
	oY.master = oMaster;
	oY.onchange = dateCtrlcbYonChange;// Set the even
	oMaster.cbY = oY; // Link the year control
	
	
	var oM = document.getElementById(sMName);
	// Make sure the month object is existed
	if(isUndefined(oM))
	{
		// Compose message
		var sMsg = 'Month control missing!';
		sMsg += ' Date control initiation fail!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(isUndefined(oM))
	oM.master = oMaster;
	oM.onchange = dateCtrlcbMonChange; // even of change month control
	oMaster.cbM = oM; // Link the month control
	
	var oD = document.getElementById(sDName);
	// Make sure the day object is existed
	if(isUndefined(oD))
	{
		// Compose message
		var sMsg = 'Day control missing!';
		sMsg += ' Date control initiation fail!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(isUndefined(oD))
	oD.master = oMaster;
	oD.onchange = dateCtrlcbDonChange; // even of change day control
	oMaster.cbD = oD; // Link the day control
	
	bResult = true;
	return bResult;
} // End of function lkDateCtrl
// #################################################################### Date control functions 
/**
 * Class dateCtrl compile date related component in the object
 * Even dateCtrl::onChangeDate even of changing date of the control
 * @param - Integer - Date value going to be changed
 * @return - Boolean - Flag telling whether the process is allowed to run
 * Even dateCtrl::onChangeYear even of changing year of the control
 * @param - Integer - Year value going to be changed
 * @return - Boolean - Flag telling whether the process is allowed to run
 * Even dateCtrl::onChangeMonth even of changing month of the control
 * @param - Integer - Month value going to be changed
 * @return - Boolean - Flag telling whether the process is allowed to run
 * Even dateCtrl::onChangeDay even of changing day of the control
 * @param - Integer - Day value going to be changed
 * @return - Boolean - Flag telling whether the process is allowed to run
*/
// -------------------------------------------------------------------------------------------------------------
/**
 * function dateCtrl::refresh refresh method of date control
 * It update UI by it dt property
*/
function dateCtrlrefresh()
{ // refresh method of date control
	var dt = this.dt;
	this.value = getISODt(dt); // Refresh the ISO value
	
	// Update the year control
	this.iY = dt.getYear();
	if(this.iY < 1900) this.iY += 1900;
	this.cbY.value = this.iY;
	
	// Update the month control
	this.iM = dt.getMonth();
	this.iM ++;
	this.cbM.value = this.iM;
	
	// Update the day control
	this.iD = dt.getDate();
	this.cbD.value = this.iD;
} // End of function dateCtrl::refresh
// -------------------------------------------------------------------------------------------------------------
/**
 * function dateCtrl::ChangeYear even of changing year control
 * @return - Boolean - Flag tellign whether the process is done
*/
function dateCtrlChangeYear()
{ // even of changing year control
	var bResult = false;
	if(isUndefined(this.cbY)) return bResult;
	var oCtrl = this.cbY;
	var bSucess = true;
	
	// External even handle  
	if(!isUndefined(this.onChangeDate))
	{
		var dtRef = getDtCtrlVal(this.name);
		bSucess = this.onChangeDate(dtRef);
	}// End of if(!isUndefined(this.onChangeDate))
	
	if(!isUndefined(this.onChangeYear))
	{
		// The even of changing year is existed
		bSucess = this.onChangeYear(oCtrl.value);
	}// end of if(!isUndefined(this.onChangeYear))
	
	// Revert
	if(bSucess == false)
	{// The changes is blocked
		// Revert the changes
		var iY = this.dt.getYear()
		if(iY < 1900) iY += 1900;
		oCtrl.value = iY;
		return bResult;
	}// End of if(bSucess == false)
	
	this.dt.setYear(oCtrl.value);
	this.refresh();
	bResult = true;
	return bResult;
}// End of function dateCtrl::ChangeYear 
// -------------------------------------------------------------------------------------------------------------
/**
 * function dateCtrl::ChangeMonth even of changing month control
 * @return - Boolean - Flag tellign whether the process is done
*/
function dateCtrlChangeMonth()
{// even of changing month control
	var bResult = false;
	if(isUndefined(this.cbM)) return bResult;
	var oCtrl = this.cbM;
	var bSucess = true;

	// External even handle  
	if(!isUndefined(this.onChangeDate))
	{
		var dtRef = getDtCtrlVal(this.name);
		bSucess = this.onChangeDate(dtRef);
	}// End of if(!isUndefined(this.onChangeDate))
	
	if(!isUndefined(this.onChangeMonth))
	{
		// The even of changing month is existed
		bSucess = this.onChangeMonth(oCtrl.value);
	}// end of if(!isUndefined(this.onChangeMonth))
	
	// Revert
	if(bSucess == false)
	{// The changes is blocked
		// Revert the changes
		var iM = this.dt.getMonth()
		iM ++;
		oCtrl.value = iM;
		return bResult;
	}// End of if(bSucess == false)
	
	var iM = oCtrl.value;
	iM --;
	this.dt.setMonth(iM);
	this.refresh();
	bResult = true;
	return bResult;
} // End of function dateCtrl::ChangeMonth 
// -------------------------------------------------------------------------------------------------------------
/**
 * function dateCtrl::ChangeDay even of changing day control
 * @return - Boolean - Flag telling whether the process is done
*/
function dateCtrlChangeDay()
{// even of changing day control
	var bResult = false;
	if(isUndefined(this.cbD)) return bResult;
	var oCtrl = this.cbD;
	var bSucess = true;

	// External even handle  
	if(!isUndefined(this.onChangeDate))
	{
		var dtRef = getDtCtrlVal(this.name);
		bSucess = this.onChangeDate(dtRef);
	}// End of if(!isUndefined(this.onChangeDate))
	
	if(!isUndefined(this.onChangeDay))
	{
		// The even of changing day is existed
		bSucess = this.onChangeDay(oCtrl.value);
	}// end of if(!isUndefined(this.onChangeDay))
	
	// Revert
	if(bSucess == false)
	{// The changes is blocked
		// Revert the changes
		var iD = this.dt.getDate()
		oCtrl.value = iD;
		return bResult;
	}// End of if(bSucess == false)
	
	var iD = oCtrl.value;
	this.dt.setDate(iD);
	this.refresh();
	bResult = true;
	return bResult;
} // end of function dateCtrl::ChangeDay 
// -------------------------------------------------------------------------------------------------------------
/**
 * function dateCtrl::setDate set date of the control
 * @param - Date - Date object to be set 
 * @return - Boolean - Flag telling whether the process is done
*/
function dateCtrlsetDate(dt)
{// set date of the control
	var bResult = false;
	
	if(isUndefined(dt.getDate)) return bResult;
	if(isUndefined(dt.getMonth)) return bResult;
	if(isUndefined(dt.getYear)) return bResult;
	
	// Transfer date
	this.dt.setDate(dt.getDate());
	this.dt.setMonth(dt.getMonth());
	this.dt.setYear(dt.getYear());
	
	this.refresh();
	bResult = true;
	return bResult;
} // End of function dateCtrl::setDate
// -------------------------------------------------------------------------------------------------------------
/**
 * function dateCtrl::cbY::onChange even of change year control
 * @return - Boolean - Flag tellign whether the process is done
*/
function dateCtrlcbYonChange()
{// even of change year control
	var bResult = false;
	if(isUndefined(this.master)) return bResult;
	bResult = this.master.changeYear();
	return bResult;
} // End of function dateCtrl::cbY::onChange
// -------------------------------------------------------------------------------------------------------------
/**
 * function dateCtrl::cbM::onChange even of change month control
 * @return - Boolean - Flag tellign whether the process is done
*/
function dateCtrlcbMonChange()
{//  even of change month control
	var bResult = false;
	if(isUndefined(this.master)) return bResult;
	bResult = this.master.changeMonth();
	return bResult;
} // End of function dateCtrl::cbM::onChange 
// -------------------------------------------------------------------------------------------------------------
/**
 * function dateCtrl::cbD::onChange even of change day control
 * @return - Boolean - Flag tellign whether the process is done
*/
function dateCtrlcbDonChange()
{//  even of change day control
	var bResult = false;
	if(isUndefined(this.master)) return bResult;
	bResult = this.master.changeDay();
	return bResult;
} // End of function dateCtrl::cbD::onChange 
// -------------------------------------------------------------------------------------------------------------
/**
 * function dateCtrl::setCSSClass set CSS class for date control
 * @param - String - Class name
 * @return - Boolean - Flag tellign whether the process is done
*/
function dateCtrlsetCSSClass(sCSS)
{// set CSS class for date control
	var bResult = false;
	setObjCSSClass(this.cbY, sCSS);
	setObjCSSClass(this.cbM, sCSS);
	setObjCSSClass(this.cbD, sCSS);
	bResult = true;
	return bResult;
} // End of function dateCtrl::setCSSClass
// -------------------------------------------------------------------------------------------------------------
// ############################################################## end of Date control functions 
// -------------------------------------------------------------------------------------------------------------
/**
 * function initDateCtrl initiate the date control
 * @param - String - Name of the date control
 * @return - Boolean - Flag telling whether the process is done
*/
function initDateCtrl(sName)
{ // initiate the date control
	var bResult = false;
	var oMaster = document.getElementById(sName);
	
	// Init the value
	if(isISODt(oMaster.value)) oMaster.dt = getDtByISO(oMaster.value);
	else oMaster.dt = new Date();
	
	// Assign methods
	oMaster.refresh = dateCtrlrefresh; // Refresh
	oMaster.changeYear = dateCtrlChangeYear; // Even of changing year control
	oMaster.changeMonth = dateCtrlChangeMonth; // Even of changing month control
	oMaster.changeDay = dateCtrlChangeDay; // Even of changing day control
	oMaster.setDate = dateCtrlsetDate; // Set date value
	oMaster.setCSSClass = dateCtrlsetCSSClass; //  set CSS class for date control
	
	if(!lkDateCtrl(oMaster)) return bResult;
	oMaster.refresh();
	bResult = true;
	return bResult;
} // End of function initDateCtrl
// -------------------------------------------------------------------------------------------------------------
// ######################################################################## End of Date function
// #################################################################### FORM overwriting functions 
// -------------------------------------------------------------------------------------------------------------
/**
 * function form::bkSetting backup form setting.
 * It does backup for properties action, target and method.
 * @return - Boolean - Flag indicates whether the process is run successfully.
*/
function formBKSetting()
{// backup form setting.
	var bResult = false;
	this.sOriAction = this.action;
	this.sOriTarget = this.target;
	this.sOriMethod = this.method;
	bResult = true;
	return bResult;
} // End of function formbkSetting
// -------------------------------------------------------------------------------------------------------------
/**
 * function form::rsSetting restore form setting from back value..
 * @return - Boolean - Flag indicates whether the process is run successfully.
*/
function formRSSetting()
{// restore form setting from back value..		
	var bResult = false;
	if(isUndefined(this.sOriAction))
	{
		// Compose message.
		var sMsg = 'Form action backup missing!';
		sMsg += 'Form setting restore terminated!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(isUndefined(this.sOriAction))

	if(isUndefined(this.sOriTarget))
	{
		// Compose message.
		var sMsg = 'Form target backup missing!';
		sMsg += 'Form setting restore terminated!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(isUndefined(this.sOriTarget))
	
	if(isUndefined(this.sOriMethod))
	{
		// Compose message.
		var sMsg = 'Form method backup missing!';
		sMsg += 'Form setting restore terminated!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(isUndefined(this.sOriMethod))

	this.action = this.sOriAction;
	this.target = this.sOriTarget;
	this.method = this.sOriMethod;
	bResult = true;
	return bResult;
} // End of function formRSSetting
// -------------------------------------------------------------------------------------------------------------
//form.prototype.bkSetting = formBKSetting;
//form.prototype.rsSetting = formRSSetting;
// ################################################################ End of FORM overwriting functions 
// ############################################################################### DIV control functions
// -------------------------------------------------------------------------------------------------------------
/**
 * function getDiv return div.
 * It return null while the div is not found.
 * @param - String - Div id.
 * @return - DIV
*/
function getDiv(strDivId)
{ // return div.
	var oResult = null;
	// Get from layer array.
	if(document.layers)
	{
		var arrLayer = document.layers;
		if(!isUndefined(arrLayer[strDivId])) oResult = arrLayer[strDivId];
	}// End of if(document.layers)
	if(!isUndefined(oResult)) return oResult;
	
	// Get from all array.
	if(document.all)
	{
		var arrLayer = document.all;
		if(!isUndefined(arrLayer[strDivId])) oResult = arrLayer[strDivId];
	}// End of if(document.all)
	if(!isUndefined(oResult)) return oResult;

	// Retrieve by getElementId
	if(document.getElementById)
	{
		var oElement = document.getElementById(strDivId);
		if(!isUndefined(oElement)) oResult = oElement;
	}// End of if(document.getElementById)
	return oResult;
} // End of function getDiv
// -------------------------------------------------------------------------------------------------------------
/**
 * function setDivVisibility set vibility of div.
 * @param - String - Div id.
 * @return - Boolean -Flag indicates whether the process is run successfully.
*/
function setDivVisibility(strDivId, strVisibility)
{ // set vibility of div.
	var bResult = false;
	var oDiv = getDiv(strDivId);
	if(isUndefined(oDiv)) return bResult;
	if(isUndefined(oDiv.style)) return bResult;
	var oStyle = oDiv.style;
	//var oStyle = oDiv.getAttribute('style');
	oStyle.visibility = strVisibility;
	//oStyle.setAttribute('visibility',strVisibility);
	bResult = true;
	return bResult;
} // End of function setDivVisibility
// -------------------------------------------------------------------------------------------------------------
/**
 * function isDivVisibile check whether the div is visible.
 * @param - String - Div id.
 * @return - Boolean - Flag shown whether the div is visible.
*/
function isDivVisibile(strDivId)
{ // check whether the div is visible.
	var bResult = false;
	var oDiv = getDiv(strDivId);
	if(isUndefined(oDiv)) return bResult;
	if(isUndefined(oDiv.style)) return bResult;
	var oStyle = oDiv.style;
	//var sVisibility = oStyle.getAttribute('visibility');
	var sVisibility = oStyle.visibility;
	bResult = true;
	if(isUndefined(sVisibility)) return bResult;
	bResult = (sVisibility != 'hidden');
	return bResult;
} // End of function isDivVisibile
// -------------------------------------------------------------------------------------------------------------
/**
 * function showDiv show the div
 * @param - String - Div id.
 * @return - Boolean -Flag indicates whether the process is run successfully.
*/
function showDiv(strDivId)
{ // show the div
	var bResult = false;
	bResult = setDivVisibility(strDivId, 'visible');
	if(bResult)
	{
		var oDiv = getDiv(strDivId);
		var oStyle = oDiv.style;
		var sPrePos = oDiv.getAttribute('sPrePos');
		// Restore back to the previous position..
		if(!isUndefined(sPrePos))
		{
			// Set it back to previous position.
			//oStyle.setAttribute('position',sPrePos);
		}// End of if(!isUndefined(sPrePos))
		oStyle.display = ''; // Clear no space occupied
	}// End of if(bResult)
	return bResult;
} // End of function showDiv
// -------------------------------------------------------------------------------------------------------------
/**
 * function hideDiv hide the div
 * @param - String - Div id.
 * @return - Boolean -Flag indicates whether the process is run successfully.
*/
function hideDiv(strDivId)
{ // show the div
	var bResult = false;
	bResult = setDivVisibility(strDivId, 'hidden');
	if(bResult)
	{
		var oDiv = getDiv(strDivId);
		var oStyle = oDiv.style;
		//var sPosition = oStyle.getAttribute('position');
		var sPosition = oStyle.position;
		if(isUndefined(sPosition)) sPosition = 'relative';
		if(sPosition == '') sPosition = 'relative';
		// Backup the previous position.
		if(sPosition.length > 0) oDiv.setAttribute('sPrePos',sPosition);
		oStyle.display = 'none'; // No space occupied
		// Set it absolute.
		//oStyle.setAttribute('position','absolute');
	}// End of if(bResult)
	return bResult;
} // End of function hideDiv
// ######################################################################## End of DIV control functions
// -------------------------------------------------------------------------------------------------------------
/**
 * function chBGColor change background color
 * @param - Object - Object to be changed back ground color
 * @param - String - Background color setting
 * @return - Boolean - Flag telling whether the process is done
*/
function chBGColor(obj, sBGColor)
{ // change background color
	// Make sure the object is allowed to change it background color
	
	// Object not existed
	if(isUndefined(obj)) return false;
	// No style with the object
	if(isUndefined(obj.style)) return false;
	var oStyle = obj.style;
	
	// Backup the original color
	if(!isUndefined(oStyle.backgroundColor))
	{
		var sOriBGColor = oStyle.backgroundColor;
		if(!isUndefined(sOriBGColor))
		{
			if(isUndefined(obj.sOriBGColor)) obj.sOriBGColor = sOriBGColor;
		}// End of if(!isUndefined(sOriBGColor))
	}// End of if(!isUndefined(oStyle.backgroundColor))
	
	oStyle.backgroundColor = sBGColor;
	return true;
} // End of function chBGColor
// -------------------------------------------------------------------------------------------------------------
/**
 * function restoreBGColor restore back the original color
 * @param - Object - Object to be restored its original back ground color
 * @return - Boolean - Flag telling whether the process is done
*/
function restoreBGColor(obj)
{ // restore back the original color
	// Object not existed
	if(isUndefined(obj)) return false;
	// No style with the object
	if(isUndefined(obj.style)) return false;
	var oStyle = obj.style;
	if(isUndefined(obj.sOriBGColor)) return false;
	oStyle.backgroundColor = obj.sOriBGColor;
	return true;
} // End of function restoreBGColor
// -------------------------------------------------------------------------------------------------------------
/**
 * function getObjLeft get object x position of the page
 * It return -1 whole the position is not detected
 * @param - object - position object
 * @return - Integer - X position in pixel
*/
function getObjLeft(oObj) 
{
  var iResult = 0;
  // Refer to container
  if (oObj.container)
  {
	if(oObj.x) iResult += oObj.x;
	iResult += oObj.container.pageX;
	return iResult;
  }// End of if (oObj.container)

  // Refer itself position
  if(oObj.y)
  {
	iResult += oObj.x;
	return iResult;
  }// End of if(oObj.x)
  
  // Refer to offset parent
  if(oObj.offsetParent)
  {
	var oParent = oObj;
	while (oParent.offsetParent != null)
	{
      iResult += oParent.offsetLeft;
      oParent = oParent.offsetParent; // Go to parent of parent
	}// End of while (obj.offsetParent != null)
    return iResult;
  }// end of if(obj.offsetParent)
  
  // Nothing to refer
  iResult = -1; // No left found
  return iResult;
}// End of function getObjLeft(oObj) 
// -------------------------------------------------------------------------------------------------------------
/**
 * function getObjTop get object y position of the page
 * It return -1 whole the position is not detected
 * @param - object - position object
 * @return - Integer - Y position in pixel
*/
function getObjTop(oObj) 
{
  var iResult = 0;
  // Refer to container
  if (oObj.container)
  {
	if(oObj.y) iResult += oObj.y;
	iResult += oObj.container.pageY;
	return iResult;
  }// End of if (oObj.container)

  // Refer itself position
  if(oObj.y)
  {
	iResult += oObj.y;
	return iResult;
  }// End of if(oObj.y)
  
  // Refer to offset parent
  if(oObj.offsetParent)
  {
	var oParent = oObj;
	while (oParent.offsetParent != null)
	{
      iResult += oParent.offsetTop;
      oParent = oParent.offsetParent; // Go to parent of parent
	}// End of while (obj.offsetParent != null)
    return iResult;
  }// end of if(obj.offsetParent)
  
  // Nothing to refer
  iResult = -1; // No top found
  return iResult;
}// End of function getObjTop(oObj) 
// ######################################################### CSS
// -------------------------------------------------------------------------------------------------------------
/**
 * function getObjCSSClass return object's CSS class
 * It return empty string while no CSS class found!
 * @param - Object - Object to be retrieved its CSS class
 * @return - String - CSS class name
*/
function getObjCSSClass(obj)
{// return object's CSS class
	var sResult = '';
	if(isUndefined(obj)) return sResult;
	if(typeof(obj) != 'object') return sResult;
	// Refer properties className
	if(!isUndefined(obj.className))
	{
		sResult = obj.className;
		return sResult;
	}// End of if(!isUndefined(obj.className))
	
	// Refer properties class
	var sRef = obj.getAttribute('class');
	if(sRef != null) sResult = sRef;
	return sResult;
} // End of function getObjCSSClass
// -------------------------------------------------------------------------------------------------------------
/**
 * function hasObjCSSClass check whether the object's CSS class is set
 * @param - Object - Object to be retrieved its CSS class
 * @return - Boolean - Flag telling whether the CSS class is set
*/
function hasObjCSSClass(obj)
{//  check whether the object's CSS class is set
	var bResult = false;
	if(isUndefined(obj)) return bResult;
	if(typeof(obj) != 'object') return bResult;
	sRef = getObjCSSClass(obj);
	if(isUndefined(sRef)) return bResult;
	if(sRef == '') return bResult;
	bResult = true;
	return bResult;
} // End of function hasObjCSSClass
// -------------------------------------------------------------------------------------------------------------
/**
 * function setObjCSSClass set object's CSS class 
 * @param - Object - Object to be retrieved its CSS class
 * @param - String - CSS class name
 * @return - Boolean - Flag telling whether the process is done
*/
function setObjCSSClass(obj, sName)
{// set object's CSS class
	var bResult = false;
	if(isUndefined(obj)) return bResult;
	if(typeof(obj) != 'object') return bResult;
	if(isUndefined(sName)) sName = '';
	
	// Refer properties className
	if(!isUndefined(obj.className))
	{
		obj.className = sName;
		bResult = true;
		return bResult;
	}// End of if(!isUndefined(obj.className))
	
	// Refer properties class
	var sRef = obj.getAttribute('class');
	if(sRef != null)
	{
		obj.setAttribute('class', sName);
		bResult = true;
		return bResult;
	}// End of if(sRef != null)
	
	return bResult;
} // End of function setObjCSSClass
// -------------------------------------------------------------------------------------------------------------
/**
 * function getObjByName retrieve object by name 
 * It return null if the object is not found
 * @param - String - Control name 
 * @return - Object - Object with its name of id matched 
*/
function getObjByName(sName)
{// retrieve object by name 
	var oResult = null;
	
	if(isUndefined(sName)) return oResult;
	if(sName == '') return oResult;
	
	// Call from Master form
	var oCtrl = getTransFieldCtrl(sName);
	if(oCtrl != false)
	{
		oResult = oCtrl;
		return oResult;
	}// end of if(oCtrl != false)
	// Retrieve from document element list
	if(document.getElementsByName) // by name
	{
		oCtrl = document.getElementsByName(sName);
		if(!isUndefined(oCtrl))
		{
			oResult = oCtrl;
			return oResult;
		}// end of if(!isUndefined(oCtrl))
	}// end of if(document.getElementsByName)
	
	if(document.getElementByID) // By ID
	{
		oCtrl = document.getElementByID(sName);
		if(!isUndefined(oCtrl))
		{
			oResult = oCtrl;
			return oResult;
		}// end of if(!isUndefined(oCtrl))
	}// end of if(document.getElementsByName)
	
	return oResult;
} // End of function getObjByName
// -------------------------------------------------------------------------------------------------------------
/**
 * function getCtrlCSSClass return return cotrol's CSS class name
 * It return empty string while no CSS class found!
 * @param - String - Control name 
 * @return - String - CSS class name
*/
function getCtrlCSSClass(sName)
{// return return cotrol's CSS class name
	var sResult = '';
	// Retrieve the control object
	var oCtrl = getObjByName(sName);
	// Object not found
	if(isUndefined(oCtrl)) return sResult;
	sResult = getObjCSSClass(oCtrl);
	return sResult;
} // End of function getCtrlCSSClass
// -------------------------------------------------------------------------------------------------------------
/**
 * function hasCtrlCSSClass check whether the control's CSS class is set
 * @param - String - Control name 
 * @return - Boolean - Flag telling whether the CSS class is set
*/
function hasCtrlCSSClass(sName)
{ // check whether the control's CSS class is set
	var bResult = false;
	// Retrieve the control object
	var oCtrl = getObjByName(sName);
	// Object not found
	if(isUndefined(oCtrl)) return sResult;
	if(!hasObjCSSClass(oCtrl)) return bResult;
	bResult = true;
	return bResult;
} // End of function hasCtrlCSSClass
// -------------------------------------------------------------------------------------------------------------
/**
 * function setCtrlCSSClass set control class name
 * @param - String - Control name 
 * @param - String - CSS class name
 * @return - Boolean - Flag telling whether the process is done
*/
function setCtrlCSSClass(sName, sClassName)
{// return return cotrol's CSS class name
	var bResult = false;
	// Retrieve the control object
	var oCtrl = getObjByName(sName);
	// Object not found
	if(isUndefined(oCtrl)) return sResult;
	bResult = setObjCSSClass(oCtrl,sClassName);
	return bResult;
} // End of function setCtrlCSSClass
// ################################################### End of CSS