/*
	Modification log:
	9/8/09 Fix setAttribute problem
		function nuoSetDivVisibility changed
	9/8/2009 Add Background color changing function
		function chObjBKColor change object background color
	13/4/2008 Add date control functions:
		function isISODt check whether the pass-in is ISO date format.
		function getISOYear get year part of ISO date format.
		function getISOMonth get month part of ISO date format.
		function getISODay get day part of ISO date format.
		function getDtByISO generate date object by pass-in ISO date format.
		function getISODt generate ISO date format by pass-in date object.
		function chkDtCtrlExt check whether the date control is existed.
		function getDtCtrlVal return date control value.
		function setDtCtrl set date control value.
	05/11/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.
	06/11/2007 - Number format.
		function nuoNumberFormat format pass-in float.
*/
/**
 * common.js keeps common javascript function.
 * For Foundermall SMS system.
 * Version: 1.1.0 Date: 9/8/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 05/11/2007
	result.bkSetting = frmBkSetting;
	result.rsSetting = frmRSSetting;

	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
*/
function promptDailog(strURL, iWidth, iHeight)
{ // prompt dailog by pass-in URL.
	//strURL = trim(strURL);
	if(strURL == '') return;
	if(isUndefined(iWidth)) iWidth = 100;
	if(isUndefined(iHeight)) iHeight = 100;
	var sAttr = 'menubar = no, location = no, status = yes, scrollbars = yes';
	sAttr += ', width = ' + iWidth;
	sAttr += ', height = ' + iHeight;
	var newWin = window.open(strURL,'',sAttr);
	newWin.width = iWidth;
	newWin.height = iHeight;
} // End of function promptDailog
/**
 * function txtNumeric make text accept numeric key only.
 * It should be call by even ONKEYPRESS.
  * @param - Event.
* @return - Boolean - Flag indicate success or not.
*/
function txtNumeric(e)
{
	if(isUndefined(e)) e = window.event;
	
	var sKeyCode = '';
	if(!isUndefined(e.keyCode)) sKeyCode = e.keyCode;
	if(!isUndefined(e.which)) sKeyCode = e.which;
	// 0 ~ 9
	if((sKeyCode >= 48 && sKeyCode <= 57)) return true;
	// 0 ~ 9 Small numpad area
	//if((event.keyCode >= 96 && event.keyCode <= 105)) return true;
	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 44: // ,
		case 46: // .
		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.
 * @return - Boolean - Flag indicate success or not.
*/
function txtInt(e)
{ // make text accept integer only.
	if(isUndefined(e)) e = window.event;
	if(!txtNumeric(e)) return false;
	var sKeyCode = '';
	if(!isUndefined(e.keyCode)) sKeyCode = e.keyCode;
	if(!isUndefined(e.which)) sKeyCode = e.which;
	//setWinStatus(sKeyCode);
	//alert(sKeyCode);
	switch(sKeyCode)
	{
		case 46: // .
		case 110: // . - numpad
		case 190: // .
		return false;
	}// End of switch(sKeyCode)
	return true;
} // End of function txtInt
/**
 * function nuoNumberFormat format pass-in float.
 * @param - Float - Number to be formatted.
 * @param - Integer - Number of digi after decimal point.
 * @param - String - Decimal symbol
 * @param - String - Thousands seperator
 * @return - String - Formatted number.
*/
function nuoNumberFormat(fNum, iDecimals, sDecPoint, sThounsandsSep)
{ //  format pass-in float.
	var sResult = '';
	// Default parameters
	if(isUndefined(iDecimals)) iDecimals = 2;
	if(isUndefined(sDecPoint)) sDecPoint = '.';
	if(isUndefined(sThounsandsSep)) sThounsandsSep = ',';
	var fVal = parseFloat(fNum);
	// Make sure pass-in is valid number.
	if(fVal == 'NaN')
	{
		// Compose message.
		var sMsg = 'Pass-in is not valid number!';
		sMsg += '(' + fNum + ')';
		sMsg += 'Number formating terminated!';
		setWinStatus(sMsg);
		return sResult;
	}// End of if(fVal == 'NaN')
	
	// Handle decimal point.
	for(var i = 0; i < iDecimals; i++)
	{
		fVal *= 10;
	}// End of for(var i = 0; i < iDecimals; i++)
	fVal = Math.round(fVal); // Drop the rest decimal digi.
	for(var i = 0; i < iDecimals; i++)
	{
		fVal /= 10;
	}// End of for(var i = 0; i < iDecimals; i++)
	
	// Handle thounsands seperator.
	var iInteger = Math.floor(fVal);
	var sInteger = '' + iInteger;
    if (sInteger.length > 3) {
        var iMod = sInteger.length%3;
        var sFinal = (iMod > 0 ? (sInteger.substring(0,iMod)) : '');
        for (var i=0 ; i < Math.floor(sInteger.length/3) ; i++) 
		{
            if ((iMod ==0) && (i ==0))
                sFinal+= sInteger.substring(iMod+3*i,iMod+3*i+3);
            else
                sFinal+= sThounsandsSep + sInteger.substring(iMod+3*i,iMod+3*i+3);
        }
    }
    else sFinal = sInteger;
	
	var fDecVal = fVal%1;
	var sDecVal = '';
	for(var i = 0; i < iDecimals; i ++) sDecVal += '0';
	if(fDecVal > 0)
	{
		sDecVal = '' + fDecVal;
		sDecVal = sDecVal.substring(2, sDecVal.length - 1);
		if(sDecVal.length > iDecimals) sDecVal = sDecVal.substring(0, iDecimals);
		while(sDecVal.length < iDecimals) sDecVal += '0';
	}// End of if(fDecVal > 0)
	
	sResult = sFinal;
	sResult += sDecPoint;
	sResult += sDecVal;
	return sResult;
} // End of function nuoNumberFormat
/**
 * 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
// ############################################################################# Array related functions
// Over-write Array method.
/**
 * function Array::copy copy another array element to current array.
 * @param - Array - Array to be copy in.
 * @return - Boolean - Flag indicates whether the process is run successfully.
*/
function ArrayCopy(arr)
{
	var bResult = false;
	// Make sure the pass-in is array.
	if(!nuoIsArray(arr))
	{
		// Compose array.
		var sMsg = 'Pass-in is not an array';
		sMsg += 'Array copy terminated!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(!nuoIsArray(arr))
	bResult = true;
	var iLen = arr.length;
	for(var i=0; i< iLen ; i++)
	{
		this[i] = arr[i];
	}// End of var(var i=0;i< iLen; i+++)
	return bResult;
}// End of function ArrayCopy
/**
 * function Array::listElement compose element into string.
 * @return - String - Element in string.
*/
function ArrayList()
{
	var sResult ='';
	var iLen = this.length;
	for(var i=0;i<iLen; i++)
	{
		if(sResult != '') sResult += "\r\n";
		sResult += i;
		sResult += " => ";
		sResult += this[i];
	}// End of for(var i=0;i<iLen; i++)
	return sResult;
}// End of function ArrayList
/**
 * function Array::showElement prompt out array content.
*/
function ArrayShowList()
{
	var sMsg = this.listElement();
	alert(sMsg);
}// End of function ArrayShowList
/**
 * function Array::remove remove element by pass-in id.
 * @param - Integer - Index of the element.
 * @return - Boolean - Flag indicates whether the process is run successfully.
*/
function ArrayRemove(intIdx)
{ // remove element by pass-in id.
	var bResult = false;
	if(isUndefined(intIdx)) return bResult;
	// Out of bound.
	if(intIdx >= this.length) return bResult;
	//delete this[intIdx];
	bResult = true;
	for(var i=0,n=0;i<this.length;i++)
	{
        if(i != intIdx)
        {
            this[n++]=this[i];
        }
	}// End of for(var i=0,n=0;i<this.length;i++)
	this.length-=1;
	return bResult;
}// End of function Array::remove

Array.prototype.copy = ArrayCopy;
Array.prototype.listElement = ArrayList;
Array.prototype.showElement = ArrayShowList;
Array.prototype.remove= ArrayRemove;

/**
 * function nuoIsArray check whether the pass-in is array.
 * @param - var - Object to be checked.
 * @return - Boolean - Flag indicates whether the pass-in is array.
*/
function nuoIsArray(oObj)
{ // check whether the pass-in is array.
	var bResult = false;
	if(oObj == null) return bResult;
	if(isUndefined(oObj)) return bResult;
	if(typeof(oObj) != 'object') return bResult;
	if(oObj.length == null) return bResult;
	if(isUndefined(oObj.length)) return bResult;
	if(typeof(oObj.length) != 'number') return bResult;
	bResult = true;
	return bResult;
} // End of function nuoIsArray
/**
 * function nuoAppendArray append something to array.
 * @param - Array - Array to be appended.
 * @param - var - Object to append.
*/
function nuoAppendArray(arrList, oObj)
{ // append something to array.
	var bResult = false;
	// Make sure the list is array.
	if(!nuoIsArray(arrList))
	{
		// Compose message.
		var sMsg = 'First pass-in is not array.';
		sMsg += ' function nuoAppendArray terminated!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(!nuoIsArray(arrList))

	var i = arrList.length;
	arrList[i] = oObj;
	bResult = true;
	return bResult;
} // End of function nuoAppendArray
// ############################################################################ 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);
	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

// ######################################################################## 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 frmBkSetting()
{// 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 frmBkSetting
/**
 * function form::rsSetting restore form setting from back value..
 * @return - Boolean - Flag indicates whether the process is run successfully.
*/
function frmRSSetting()
{// 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 frmRSSetting
		
//form.prototype.bkSetting = frmBkSetting;
//form.prototype.rsSetting = frmRSSetting;
// ################################################################ End of FORM overwriting functions 

// ############################################################################### DIV control functions
/**
 * function nuoGetDiv return div.
 * It return null while the div is not found.
 * @param - String - Div id.
 * @return - DIV
*/
function nuoGetDiv(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 nuoGetDiv
/**
 * function nuoSetDivVisibility set vibility of div.
 * @param - String - Div id.
 * @return - Boolean -Flag indicates whether the process is run successfully.
*/
function nuoSetDivVisibility(strDivId, strVisibility)
{ // set vibility of div.
	var bResult = false;
	var oDiv = nuoGetDiv(strDivId);
	if(isUndefined(oDiv)) return bResult;
	if(isUndefined(oDiv.style)) return bResult;
	var oStyle = oDiv.style;
	if(oStyle.setAttribute)
	{// Make sure the attrivebute is existed
		oStyle.setAttribute('visibility',strVisibility);
		bResult = true;
		return bResult;
	}// End of if(oStyle.setAttribute)
	
	if(!isUndefined(oStyle.visibility))
	{// Make sure the properties is defined
		oStyle.visibility = strVisibility;
		bResult = true;
		return bResult;
	}// End of if(!isUndefined(oStyle.visibility))
	
	//bResult = true;
	return bResult;
} // End of function nuoSetDivVisibility
/**
 * function nuoIsDivVisibile check whether the div is visible.
 * @param - String - Div id.
 * @return - Boolean - Flag shown whether the div is visible.
*/
function nuoIsDivVisibile(strDivId)
{ // check whether the div is visible.
	var bResult = false;
	var oDiv = nuoGetDiv(strDivId);
	if(isUndefined(oDiv)) return bResult;
	if(isUndefined(oDiv.style)) return bResult;
	var oStyle = oDiv.style;
	var sVisibility = "";
	if(oStyle.getAttribute) sVisibility = oStyle.getAttribute('visibility');
	if(!isUndefined(oStyle.visibility)) sVisibility = oStyle.visibility;
	bResult = true;
	if(isUndefined(sVisibility)) return bResult;
	bResult = (sVisibility != 'hidden');
	return bResult;
} // End of function nuoIsDivVisibile
/**
 * function nuoShowDiv show the div
 * @param - String - Div id.
 * @return - Boolean -Flag indicates whether the process is run successfully.
*/
function nuoShowDiv(strDivId)
{ // show the div
	var bResult = false;
	bResult = nuoSetDivVisibility(strDivId, 'visible');
	if(bResult)
	{
		var oDiv = nuoGetDiv(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 nuoShowDiv
/**
 * function nuoHideDiv hide the div
 * @param - String - Div id.
 * @return - Boolean -Flag indicates whether the process is run successfully.
*/
function nuoHideDiv(strDivId)
{ // show the div
	var bResult = false;
	bResult = nuoSetDivVisibility(strDivId, 'hidden');
	if(bResult)
	{
		var oDiv = nuoGetDiv(strDivId);
		var oStyle = oDiv.style;
		if(oStyle.getAttribute)
		{// By using methods
			var sPosition = oStyle.getAttribute('position');
			if(isUndefined(sPosition)) sPosition = 'relative';
			if(sPosition == '') sPosition = 'relative';
			// Backup the previous position.
			if(sPosition.length > 0) oDiv.setAttribute('sPrePos',sPosition);
			oDiv.setAttribute('position',sPosition);
			return bResult;
		}// End of if(oStyle.getAttribute)
		
		if(!isUndefined(oStyle.position))
		{// Direct operate the properties
			var sPosition = oStyle.position;
			if(sPosition == '') sPosition = 'relative';
			// Backup the previous position.
			if(sPosition.length > 0) oDiv.sPrePos = sPosition;
		}// End of if(!isUndefined(oStyle.position))
		oStyle.display = 'none'; // No space occupied
	}// End of if(bResult)
	return bResult;
} // End of function nuoHideDiv
// ######################################################################## End of DIV control functions
/**
 * function chObjBKColor change object 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 chObjBKColor(obj, sBGColor)
{// change object 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 chObjBKColor
// ############################################################################ SELECT control functions
/**
 * function nuoIsSELECT check whether the pass-in is SELECT control.
 * @param - val - Object to be check.
 * @return - Boolean - Flag indicates whether the object is SELECT.
*/
function nuoIsSELECT(oObj)
{ // check whether the pass-in is SELECT control.
	var bResult = false;
	// Object undefine.
	if(isUndefined(oObj)) return bResult;
	// Object options undefine.
	if(isUndefined(oObj.options)) return bResult;
	var arrOption = oObj.options;
	// Invalid options list.
	if(!nuoIsArray(arrOption)) return bResult;

	bResult = true;
	return bResult;
} // End of function nuoIsSELECT
/**
 * function nuoLdSELECTArr load array into select control.
 * @param - SELECT - Combo box to be load.
 * @param - Array - Array to be load. 
 * @param - Integer - Selected index.
 * @param - Integer - Sorting method. 0 - No sorting, 1 - Sort by value, 2 - Sort by text.
 * @return - Boolean -Flag indicates whether the process is run successfully.
*/
function nuoLdSELECTArr(oCB, arr, intIdx, iSort)
{ // load array into select control.
	var bResult = false;
	if(isUndefined(iSort)) iSort = 0;
	if(isUndefined(oCB))
	{
		// Compose message.
		var sMsg = 'Combo box undefined.';
		sMsg += ' Array loading to combo box terminate!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(isUndefined(oCB))
	
	if(!nuoIsArray(arr))
	{
		// Compose message.
		var sMsg = 'Array invalid undefined.';
		sMsg += ' Array loading to combo box terminate!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(!nuoIsArray(arr))
	var arrOption = oCB.options;
	if(!nuoIsArray(arrOption))
	{
		// Compose message.
		var sMsg = 'Invalid options list in combo box.';
		sMsg += ' Array loading to combo box terminate!';
		setWinStatus(sMsg);
		return bResult;
	}// End of if(!nuoIsArray(arrOption))
	if(isUndefined(intIdx))	intIdx = 0;
	// Clear previous content.
	var iLen = arrOption.length;
	for(var i = iLen -1; i >= 0; i --) oCB.remove(i);

	var arrSort = arr;
	if(iSort > 0)
	{// Sort the option.
		// Bobble sorting.
		for(var i = 0; i < arrSort.length; i ++)
		{
			var iStart = arrSort.length;
			iStart --;
			for(var j = iStart; j > i; j --)
			{
				var oCurObj = arrSort[j];
				var oNextObj = arrSort[j - 1];
				var bChange = false;
				var iIdx = 0; // Element to be compare. Default compare by value.
				if(iSort == 2) iIdx = 1; // Compare by text.
				if(isUndefined(oCurObj[iIdx])) continue;
				if(isUndefined(oNextObj[iIdx])) continue;
				var sChk = oCurObj[iIdx];
				var sRef = oNextObj[iIdx];


				if(sRef > sChk) bChange = true;
				if(bChange)
				{
					arrSort[j - 1] = oCurObj;
					arrSort[j] =  oNextObj;
				}// End of if(bChange)
			}// End of for(var j = arrSort.length -1; j > i; j --)
		}// End of for(var i = 0; i < arrSort.length; i ++)
	}// End of if(iSort > 0)
	arrSort = arr;
	// Load array into option list.
	var iLen = arr.length;
	for(var i=0;i < iLen; i++)
	{
		var opt = document.createElement("OPTION");
		var arrOpt = arr[i];
		opt.text = arrOpt[1];
		opt.value = arrOpt[0];
		if(intIdx == i) opt.selected = true;
		oCB.options.add(opt);
	}// End of for(var i=0;i < iLen; i++)
	bResult = true;
	return bResult;
} // End of function nuoLdSELECTArr
/**
 * function getSELECTOption return selecting option
 * It return null while error occur.
 * @param - SELECT - Select object.
 * @return - OPTION - Selected option.
*/
function getSELECTOption(oSel)
{ // return selecting option
	var oResult = null;
	if(isUndefined(oSel))
	{
		// Compose message.
		var sMsg = 'Combo box undefined.';
		sMsg += ' Get selecting option terminate!';
		setWinStatus(sMsg);
		return oResult;
	}// End of if(isUndefined(oSel))
	if(isUndefined(oSel.selectedIndex))
	{
		// Compose message.
		var sMsg = 'Selected index missing.';
		sMsg += ' Get selecting option terminate!';
		setWinStatus(sMsg);
		return oResult;
	}// End of if(isUndefined(oSel.selectedIndex))
	var intIdx  = oSel.selectedIndex;
	var arrOption = oSel.options;
	if(!nuoIsArray(arrOption))
	{
		// Compose message.
		var sMsg = 'Invalid options list in combo box.';
		sMsg += ' Get selecting option terminate!';
		setWinStatus(sMsg);
		return oResult;
	}// End of if(!nuoIsArray(arrOption))

	if((intIdx < 0) || (intIdx >= arrOption.length))
	{
		// Compose message.
		var sMsg = 'Out of option bound.';
		sMsg += ' Get selecting option terminate!';
		setWinStatus(sMsg);
		return oResult;
	}// End of if((intIdx < 0) || (intIdx >= arrOption.length))
	
	oResult = arrOption[intIdx];

	return oResult;
} // End of function getSELECTOption
/**
 * function getSELECTText return selecting text.
 * It return empty string while error occur.
 * @param - SELECT - Select object.
 * @return - String - Selecting text.
*/
function getSELECTText(oSel)
{ // return selecting text.
	var strResult = '';
	var oOpt = getSELECTOption(oSel);
	if(oOpt == null) return strResult;

	strResult = oOpt.text;

	return strResult;
} // End of function getSELECTText
// ##################################################################### End of SELECT control functions
// ############################################### Sorting
/**
 * function resort re-sorting the search result.
 * @param - String - Sorting field name.
*/
function resort(sField)
{
	// Get master form.
	var oFrm = getTransFrm();
	if(oFrm == false) 
	{
		var sMsg = 'Master form missing!';
		sMsg += 'Sorting terminated!';
		setWinStatus(sMsg);
		return;
	} // End of if(oFrm == false) 
	
	// Get sorting controls.
	var oSortField = getTransFieldCtrl('sSortFld');
	if(oSortField == false) 
	{
		var sMsg = 'Sorting field missing!';
		sMsg += 'Sorting terminated!';
		setWinStatus(sMsg);
		return;
	} // End of if(oSortField == false) 
	var oSortMethod = getTransFieldCtrl('bSortMth');
	if(oSortMethod == false) 
	{
		var sMsg = 'Sorting method missing!';
		sMsg += 'Sorting terminated!';
		setWinStatus(sMsg);
		return;
	} // End of if(oSortMethod == false) 
	
	if(oSortField.value == sField)
	{// Same sorting field.
		// Reverst the method.
		if(oSortMethod.value == '1') oSortMethod.value = '0';
		else oSortMethod.value = '1';
	}// End of if(oSortField.value == sField)
	else
	{// Different sorting field,
		oSortField.value = sField;
		oSortMethod.value = '1';
	}// End of if(oSortField.value == sField)
	oFrm.submit();
}// End of function resort
function gotoPage(iPage)
{
	if(!(iPage > 0)) return;
	var oFrm = getTransFrm();
	// Make sure transaction form is existed.
	if(oFrm == false) return;
	var oCtrl = getTransFieldCtrl('iPg');
	// Make sure the page control is existed.
	if(oCtrl == false) return;
	oCtrl.value = iPage;
	oFrm.submit();
}// End of function gotoPage
