// -----------------------------//
// Common Javascript functions  //
// -----------------------------//

//-------------------------------
// String manipulation functions
//-------------------------------
/*  Trim string */
function fnTrimStr(argStr)
{
	return argStr.replace(/^\s+/,'').replace(/\s+$/,'');
}	

/*  Left Trim string */
function fnLTrimStr(argStr)
{
	return argStr.replace(/^\s+/,'');
}

/*  Right Trim string */
function fnRTrimStr(argStr)
{
	return argStr.replace(/\s+$/,'');
}			

// ---------------------------------------------------------------
//	Function - Validate string input whether it is empty or not
//  ---------------------------------------------------------------
function fnIsEmpty(argStrInput)
{
	// Set the inital result to False
	var blnResult = false;
	// Front and back spaces will be trimmed before evaluation
	if (fnTrimStr(argStrInput).length > 0)
	{
		blnResult = false;
	}
	else
	{
		blnResult = true;
	}
	// Return result
	return blnResult;
}

//  -----------------------------------------------------------------------
//	Function - Check to see if the input has any alphanumeric characters.
//  -----------------------------------------------------------------------
function fnIsAlphaNumeric(argStrValue)
{
	// Set the inital result to false
	var blnResult = false;
	if (argStrValue.match(/^[a-zA-Z0-9]+$/))
	{
		blnResult = true;
	}
	else
	{
		blnResult = false;
	}	
	// Return result
	return blnResult;
}

//  -----------------------------------------------------------------------
//	Function - Check to see if the is alphabetic
//  -----------------------------------------------------------------------
function fnIsAlpha(argStrValue)
{
	// Set the inital result to false
	var blnResult = false;
	
	if (argStrValue.match(/^[a-zA-Z ]+$/))
	{
		blnResult = true;
	}
	else
	{
		blnResult = false;
	}	
	// Return result
	return blnResult;
}

//  -----------------------------------------------------------
//	Function - Check to see if the input has any white spaces.
//  -----------------------------------------------------------
function fnHasWhiteSpaces(argStrValue)
{
	// Set the inital result to false
	var blnResult = false;
	var i;
	for(i = 0; i < argStrValue.length; i++)
	{
		if (argStrValue.charAt(i) == ' ')
		{
			blnResult = true;
			break;
		}
	}
	// Return result
	return blnResult;	
}

//  -----------------------------------------------------------
//	Function - Validate input value with n decimal places
//  -----------------------------------------------------------
function fnHasDecimalPlaces(argStrValue, argIntNumDecimal)
{
	var blnResult = false;
	var strRegEx;
	
	// Construct regular expression based on different scenarios
	switch (argIntNumDecimal)
	{
		case 1:
			strRegEx = /^((\d*)|(\d*\.\d{1}))$/;
			break;
			
		case 2:
			strRegEx = /^((\d*)|(\d*\.\d{1})|(\d*\.\d{2}))$/;
			break;
			
		case 3:
			strRegEx = /^((\d*)|(\d*\.\d{1})|(\d*\.\d{2})|(\d*\.\d{3}))$/;
			break;			
			
		case 4:
			strRegEx = /^((\d*)|(\d*\.\d{1})|(\d*\.\d{2})|(\d*\.\d{3})|(\d*\.\d{4}))$/;
			break;						
	}

	// Validate regular expression
	if (argStrValue.match(strRegEx))	
	{
		blnResult = true;
	}
	else
	{
		blnResult = false;
	}
	// Return result
	return blnResult;
}

/* 
Fix Bug #1700 (added by Kris)
Function: fnIndicateProductField(argStrProductSelectName, argStrLoginInputName, argStrPasswordInputName, argStrAssignedNumberSpanName, argStrUsernameSpanName, argStrFaxSpanName, argStrLongDistanceSpanName, argStrWebConfSpanName)
 	- Indicate field name, field's max length & display message according to the selected product
Arguments:
	argStrProductSelectName		: Html select name for Product 
	argStrLoginInputName		: Html input name for Login
	argStrPasswordInputName 	: Html input name for Password
	argStrAssignedNumberSpanName: Html span name for displaying the text 'Assigned Number:'
	argStrUsernameSpanName 		: Html span name for displaying the text 'Username:'
	argStrFaxSpanName 			: Html span name for displaying the Fax login instruction
	argStrLongDistanceSpanName	: Html span name for displaying the LongDistance login instruction
	argStrWebConfSpanName		: Html span name for displaying the webConf login instruction
*/
function fnIndicateProductField(argStrProductSelectName, argStrLoginInputName, argStrPasswordInputName, argStrAssignedNumberSpanName, argStrUsernameSpanName, argStrFaxSpanName, argStrLongDistanceSpanName, argStrWebConfSpanName){

	var strSelectedProduct = document.getElementById(argStrProductSelectName).value;
	
	if (strSelectedProduct == "webConf") {
		document.getElementById(argStrAssignedNumberSpanName).style.display = "none";
		document.getElementById(argStrUsernameSpanName).style.display = "inline";
		if (argStrFaxSpanName != '') {
			document.getElementById(argStrFaxSpanName).style.display = "none";
		}
		if (argStrLongDistanceSpanName != '') {		
			document.getElementById(argStrLongDistanceSpanName).style.display = "none";
		}
		if (argStrWebConfSpanName != '') {	
			document.getElementById(argStrWebConfSpanName).style.display = "inline";
		}
		document.getElementById(argStrLoginInputName).maxLength = 50;
	}
	else if (strSelectedProduct == "fax") {
		document.getElementById(argStrAssignedNumberSpanName).style.display = "inline";
		document.getElementById(argStrUsernameSpanName).style.display = "none";
		if (argStrFaxSpanName != '') {
			document.getElementById(argStrFaxSpanName).style.display = "inline";
		}
		if (argStrLongDistanceSpanName != '') {		
			document.getElementById(argStrLongDistanceSpanName).style.display = "none";
		}
		if (argStrWebConfSpanName != '') {	
			document.getElementById(argStrWebConfSpanName).style.display = "none";
		}
		document.getElementById(argStrLoginInputName).maxLength = 10;
	}
	else if (strSelectedProduct == "longDistance"){
		document.getElementById(argStrAssignedNumberSpanName).style.display = "inline";
		document.getElementById(argStrUsernameSpanName).style.display = "none";
		if (argStrFaxSpanName != '') {
			document.getElementById(argStrFaxSpanName).style.display = "none";
		}
		if (argStrLongDistanceSpanName != '') {		
			document.getElementById(argStrLongDistanceSpanName).style.display = "inline";
		}
		if (argStrWebConfSpanName != '') {	
			document.getElementById(argStrWebConfSpanName).style.display = "none";
		}
		document.getElementById(argStrLoginInputName).maxLength = 10;
	}
	
	if (strSelectedProduct != '') {
		document.getElementById(argStrLoginInputName).disabled = false;
		document.getElementById(argStrLoginInputName).style.background = "white";
		document.getElementById(argStrLoginInputName).focus();
		if (argStrPasswordInputName != '') {
			document.getElementById(argStrPasswordInputName).disabled = false;
			document.getElementById(argStrPasswordInputName).style.background = "white";
		}
	}
	else {
		document.getElementById(argStrLoginInputName).value = '';
		document.getElementById(argStrLoginInputName).disabled = true;
		document.getElementById(argStrLoginInputName).style.background = "#FFF799";
		if (argStrPasswordInputName != '') {
			document.getElementById(argStrPasswordInputName).value = '';
			document.getElementById(argStrPasswordInputName).disabled = true;
			document.getElementById(argStrPasswordInputName).style.background = "#FFF799";
		}
	}

	return true;
}

/*
function: fnIsNumeric(argStr) - Check whether the input parameter string is numeric or not
*/
function fnIsNumeric(argStr){
	var bOK = true;

	if (argStr.length == 0)
		bOK = false;
	for(var i=0; i<argStr.length; i++){
		if (! (argStr.charAt(i) >= '0' && argStr.charAt(i) <= '9')){
			bOK = false;
			break;
		}
	}
	return bOK;
}

/*
function: fnCountTotalChecked() - Count how many box are checked in checkbox array
*/
function fnCountTotalChecked(argCbxControl){
	var intCount = 0;

	if (argCbxControl != null){
		if (argCbxControl.length == null){
			//only one element in argSelControl
			if (argCbxControl.checked)
					intCount++;
		}else{
			for(var i=0; i<argCbxControl.length; i++){
				if (argCbxControl[i].checked)
					intCount++;
			}
		}
	}
	return intCount;
}

/*
function: fnIsItemExistInOptions() - Check whether the item exists in SELECT control or not
*/
function fnIsItemExistInOptions(argSelControl, argStrText, argIntCompareText){
	for(var i=0; i<argSelControl.options.length; i++){
		if (argIntCompareText == 1){
			//compare option text
			if (argSelControl.options[i].text == argStrText){
				return true;
			}
		}else{
			//compare option value
			if (argSelControl.options[i].value == argStrText){
				return true;
			}
		}
	}
	return false;
}

/*
function: fnSelectOption() - Select a specific option of a SELECT control
*/
function fnSelectOption(argStrFrmName, argStrSelectControlName, argStrValue, argIntCompareText){
	var objSel = document.forms[argStrFrmName][argStrSelectControlName];
	for(var i=0; i<objSel.options.length; i++){
		if (argIntCompareText == 1){
			//compare option text
			if (objSel.options[i].text == argStrValue){
				objSel.selectedIndex = i;
				return;
			}
		}else{
			//compare option value
			if (objSel.options[i].value == argStrValue){
				objSel.selectedIndex = i;
				return;
			}
		}
	}
	objSel.selectedIndex = 0;
}

/*
function: fnCheckYear() - Check leap year
*/
function fnCheckYear(argIntYear) { 
	return (((argIntYear % 4 == 0) && (argIntYear % 100 != 0)) || (argIntYear % 400 == 0)) ? 1 : 0;
}

/*
function: fnCheck30Days() - Check whether the input month only has 30 days
*/
function fnCheck30Days(argIntMM){
	if ((argIntMM == 4) || (argIntMM == 6) || (argIntMM == 9) || (argIntMM == 11))
		return true;
	return false;
}

/*
function: fnValidateDate() - Validate the input date
*/
function fnValidateDate(argIntYYYY, argIntMM, argIntDD){
	if (fnCheckYear(argIntYYYY)){
		if ( (argIntMM == 2) && (argIntDD > 29) ){
			return false;
		}
	}else{
		if ( (argIntMM == 2) && (argIntDD > 28) ){
			return false;
		}
	}

	if (argIntMM != 2){
		if ( (fnCheck30Days(argIntMM)) && (argIntDD > 30) ){
			return false;
		}
	}
	return true;
}

// Open a new window
function popUp(url, windowTitle) {
	if (url.indexOf('http://') == -1) {
		url = 'http://' + url;
	}
	newWindow = window.open(url, windowTitle);
	
	if (window.focus) {
		newWindow.focus();
	}
 }

/*
function: fnGoto() - change the action of a form and submit it
*/
function fnGoto(argStrFrmName, argStrURL){
	document.forms[argStrFrmName].action = argStrURL;
	document.forms[argStrFrmName].submit();
}

/*
function: fnGotoSetTarget() - change the action of a form, set target and submit it
*/
function fnGotoSetTarget(argStrFrmName, argStrURL){
	document.forms[argStrFrmName].action = argStrURL;
	document.forms[argStrFrmName].target = "_self";
	document.forms[argStrFrmName].submit();
}

/*
function: fnSetImgAltText(argStrImgId, argStrImgAltText) - Set an image alternate text
*/
function fnSetImgAltText(argStrImgId, argStrImgAltText) {
	var blnReturn = false;
	if (argStrImgId != '') {
		document.getElementById(argStrImgId).alt = argStrImgAltText;
		blnReturn = true;
	}
	return blnReturn;
}

/*
function: fnSetSelectedValue(argStrFrmName, strElementName, argStrSelectedValue) 
	- Set the selected value for the drop down list after form submited
*/
function fnSetSelectedValue(argStrFrmName, strElementName, argStrSelectedValue) {
	with (document.forms[argStrFrmName]) {
		var objTemp = elements[strElementName];

		if (objTemp != null) {
			if (argStrSelectedValue == "") {
				objTemp.options[0].selected = true;
				return;
			}			
	
			for (var i = 0; i < objTemp.length; i++) {
				if (objTemp.options[i].value == argStrSelectedValue) {
					objTemp.options[i].selected = true;
					return;
				}
			}
		}
	}
}

// -----------------------------------------------------------------------------------
// function fnCheckBackPage(): Check the hidden value to back to different pages
// -----------------------------------------------------------------------------------
function fnCheckBackPage(argStrFrmName, argStrBackPage, argStrDefaultPage)
{
	if (argStrBackPage == "" || argStrBackPage == null)
	{
		window.location = argStrDefaultPage;
	}
	else
	{
		document.forms[argStrFrmName].action = argStrBackPage;
		document.forms[argStrFrmName].submit();
	}
}

// --------------------------------------------------------------------------------------------------------------
// Function: fnValidateByRegExp() - Returns TRUE if the string input matches the regular expression 
//                                  pattern, otherwise, return FALSE
//
// argStrPattern: pattern required
// argStrInput	: input string
//
// JScript: 	/xxxxx/
// VBScript:	"xxxxx"
//
// DID pattern: "[0-9]{10}"
// Email pattern: /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
// Mask CC num pattern: "^[0-9]{4}[0-9x]+[0-9]{4}$"
// CC num pattern: "^[0-9]+$"
// Double pattern: "^[-]?(([0-9]+)|([0-9]+\.[0-9]+))$"
// Non-negative double pattern with at most 2 d. p.: "^(([0-9]+)|([0-9]+\.[0-9]{1})|([0-9]+\.[0-9]{2}))$"
// --------------------------------------------------------------------------------------------------------------	
function fnValidateByRegExp(argStrPattern, argStrInput){
	if (argStrInput.match(argStrPattern) == null)
	{
		return false;
	}
	else
	{
		return true;
	}
}

// ----------------------------------------------------------------------------------------------------
// function: fnSetMultiSelectedValue(argStrFrmName, strElementName, argStrMultiSelectedValue) 
//	- Set the selected value for the drop down list after form submited
//	
// argStrMultiSelectedValue: multi-selected values string which are separated by a "|"
// ----------------------------------------------------------------------------------------------------
function fnSetMultiSelectedValue(argStrFrmName, strElementName, argStrMultiSelectedValue) {
	with (document.forms[argStrFrmName]) {
		var arrSelectedValue;
		var objTemp = elements[strElementName];

		if (argStrMultiSelectedValue == "") {
			return;
		}			
		else {
			arrSelectedValue = argStrMultiSelectedValue.split("|")
		
			for (var j = 0; j < arrSelectedValue.length; j++) {
				for (var i = 0; i < objTemp.length; i++) {
					if (objTemp.options[i].value == arrSelectedValue[j]) {
						objTemp.options[i].selected = true;
					}
				}
			}
		}
	}
	return;
}

/*
Function: fnIsVisibleCharacter(argStr) - Check whether the input parameter string contains visible characters (according to ASCII table),
										 EXCEPT space, single quote and double quote.
									   - Return 'True' if the string does not contain invisible characters, spaces, single quotes nor double quotes
										 Return 'False' if the string contains any invisible characters, spaces, single quotes or double quotes 
									   - argStr: input parameter string
*/
function fnIsVisibleCharacter(argStr){
	
	var blnResult = true;

	if (argStr.length == 0)
		blnResult = false;
		
	for(var i=0; i<argStr.length; i++){
		// check whether all characters are visible according to ASCII table (i.e. decimal character code's range 33 - 126, 32(space) is not included)
		// check whether a string contains double quote(34) and single quote(39)
		if (!(argStr.charCodeAt(i) >= 33 && argStr.charCodeAt(i) <= 126) || (argStr.charCodeAt(i) == 34) || (argStr.charCodeAt(i) == 39))
		{
			blnResult = false;
			break;
		}
	}
	
	return blnResult;
}

/* 
Function: fnIsValidTrustedNumber() - check whether the trusted phone number is valid or not  
*/
function fnIsValidTrustedNumber(argStrPhoneNumber) {
	return fnValidateByRegExp("^(1)([0-9]{10})$", argStrPhoneNumber)
}