// Function Name : AtLeastOneFieldInputFromTwoInputChecker
// Objective : To check at least 1 field input from 2 text box entry.
// Input :
// 1. strInput1 - TextBox
// 2. strInput2 - TextBox 
// 3. strErrorMessage - Error message displaying

// Output :
// Return True if there is at least 1 input and vice-versa
function AtLeastOneFieldInputFromTwoInputChecker(strInput1, strInput2, strErrorMessage){
	if ( (strInput1 == "") && (strInput2 == "") ){
		alert(strErrorMessage);
		return false;
	}
	return true;
}

// Function Name : AtLeastOneFieldInputFromThreeInputChecker
// Objective : To check at least 1 field input from 3 text box entry.
// Input :
// 1. strInput1 - TextBox
// 2. strInput2 - TextBox 
// 3. strInput3 - TextBox
// 4. strErrorMessage - Error message displaying

// Output :
// Return True if there is at least 1 input and vice-versa
function AtLeastOneFieldInputFromThreeInputChecker(strInput1, strInput2, strInput3, strErrorMessage){
	if ( (strInput1 == "") && (strInput2 == "") && (strInput3 == "") ){
		alert(strErrorMessage);
		return false;
	}
	return true;
}

// Function Name : AtLeastOneFieldInputFromFourInputChecker
// Objective : To check at least 1 field input from 4 text box entry.
// Input :
// 1. strInput1 - TextBox
// 2. strInput2 - TextBox 
// 3. strInput3 - TextBox
// 4. strInput4 - TextBox
// 5. strErrorMessage - Error message displaying

// Output :
// Return True if there is at least 1 input and vice-versa
function AtLeastOneFieldInputFromFourInputChecker(strInput1, strInput2, strInput3, strInput4, strErrorMessage){
	if ( (strInput1 == "") && (strInput2 == "") && (strInput3 == "") && (strInput4 == "")){
		alert(strErrorMessage);
		return false;
	}
	return true;
}


// Function Name : AtLeastOneFromThreeSelectionInput
// Objective : To check at least 1 field input from 3 different data type entry.
// Input :
// mode - 1 : multiple selection or single selection ; by passing in _guiForm.FieldName.selectedIndex 
// mode - 2 : single input
// output - return true if at least one input field 
function AtLeastOneFromThreeSelectionInput(strInput1, strInput2, strInput3, strErrorMessage, intMode){
	var NON_SELECT_INDEX	=	-1;
	var EMPTY_STRING		=	"";
	if (intMode == 1){
		if ( (strInput1 == NON_SELECT_INDEX) && (strInput2 == NON_SELECT_INDEX) && (strInput3 == NON_SELECT_INDEX) ){
			alert(strErrorMessage);
			return false;
		}
	}
	else if (intMode == 2){
		if ( (strInput1 == EMPTY_STRING) && (strInput2 == EMPTY_STRING) && (strInput3 == EMPTY_STRING) ){
			alert(strErrorMessage);
			return false;
		}
	}
	return true;
}


// Function Name : CheckAtLeastNFieldNotEmpty
// Objective : At least N field input is not empty from a selected field list.
// Input :
// 1. strFormName - Name of Form
// 2. strFieldName - A list of field name with comma as separator
// 3. strErrorMessage - An error message display when error
// 4. lngNumberOfField - Number of field input must not be empty

// Output :
// 1. True - Pass the validation process
// 2. False - Fail in validation process

// Remarks : Improve this function for later validation by
// 1. Check at least N field(s) is/are not empty/empty
// 2. Check is N field(s) is/are not empty/empty
function CheckAtLeastNFieldNotEmpty (strFormName, strFieldName, strErrorMessage, lngNumberOfField){
	var objForm	=	eval("document." + strFormName);
	var arrFieldName = strFieldName.split(",");
	var strFormElementType;
	var objField;
	var strFieldValue;
	var NULL_VALUE;
	var EMPTY_STRING;
	
	var lngCounter	= 0;
	NULL_VALUE = null;
	EMPTY_STRING = "";
	
	for (var i=0; i<arrFieldName.length; i++){
	
		objField			= eval("objForm." + arrFieldName[i]);
		strFormElementType	= objField.type;
		strEmptyValue		= GetEmptyValueFormElementType(strFormElementType);
		strFieldValue		= objField.value;
		
		if ( strFormElementType == "radio" || strFormElementType == "checkbox"){
			strFieldValue	=	objField.checked;
		}
		
		if (strFieldValue != strEmptyValue && strFieldValue != NULL_VALUE && strFieldValue != EMPTY_STRING){
			lngCounter = lngCounter + 1
		}
	}
	
	if (lngCounter < lngNumberOfField){
		alert (strErrorMessage);
		return false
	}
	
	return true;
}


// Function Name : OnlyOneFieldInputFromTwoInputChecker
// Objective : To check only 1 input required for the form submit.
// Input :
// 1. strInput1 - Text Box
// 2. strInput2 - Text Box
// 3. strErrorMessage - An error message display when error
function OnlyOneFieldInputFromTwoInputChecker(strInput1, strInput2, strErrorMessage){
	if ( ( (strInput1 == "") && (strInput2 == "") ) || (strInput1 != "") && (strInput2 != "") ){
		alert(strErrorMessage);
		return false;
	}
	
	return true;
}

/*
Form Action Redirect
*/

// Function Name : FormRedirect
// Objective : Form redirect with passing 1 unique key code
// Input :
// 1. strFormName - Name of Form
// 2. strAction - Next URL
// 3. strFieldName - Form element name
// 4. strFieldValue - Form element value
function FormRedirect(strFormName, strAction, strFieldName, strFieldValue){
	var objForm = eval("document." + strFormName);
	if (strFieldName != "") {
		var objField = eval("objForm." + strFieldName);
		objField.value = strFieldValue;
	}
	objForm.action = strAction;
	objForm.submit();
}

function FormRedirectWithJavascriptChecking(strFormName, strAction, strFieldName, strFieldValue, strFormChecker){
	var objForm = eval("document." + strFormName);
	
	var objFormChecker = eval(strFormChecker + '()');
	if(objFormChecker){
		if (strFieldName != "") {
			var objField = eval("objForm." + strFieldName);
			objField.value = strFieldValue;
		}

		objForm.action = strAction;
		objForm.submit();
	}
}

// Function Name : FormRedirectWithTwoValue
// Objective : Form redirect with passing 2 unique key code
function FormRedirectWithTwoValue(strFormName, strAction, strFieldName1, strFieldValue1, strFieldName2, strFieldValue2){
	var objForm = eval("document." + strFormName);
	var objField1 = eval("objForm." + strFieldName1);
	var objField2 = eval("objForm." + strFieldName2);
	objField1.value = strFieldValue1;
	objField2.value = strFieldValue2;
	objForm.action = strAction;
	objForm.submit();
}

// Function Name : FormRedirectWithTwoValueConfirm
// Objective : Form redirect with passing 2 unique key code with confirm control
function FormRedirectWithTwoValueConfirm(strFormName, strAction, strFieldName1, strFieldValue1, strFieldName2, strFieldValue2, strConfirmMessage){
	if (strConfirmMessage != ""){
		if (confirm(strConfirmMessage)){
			FormRedirectWithTwoValue(strFormName, strAction, strFieldName1, strFieldValue1, strFieldName2, strFieldValue2);
		}
	}else{
		FormRedirectWithTwoValue(strFormName, strAction, strFieldName1, strFieldValue1, strFieldName2, strFieldValue2);
	}
}


// Function Name : FormRedirectWithTwoValueConfirm
// Objective : Form redirect with passing 3 unique key code with confirm control
function FormRedirectWithThreeValue(strFormName, strAction, strFieldName1, strFieldValue1, strFieldName2, strFieldValue2, strFieldName3, strFieldValue3){
	var objForm = eval("document." + strFormName);
	var objField1 = eval("objForm." + strFieldName1);
	var objField2 = eval("objForm." + strFieldName2);
	var objField3 = eval("objForm." + strFieldName3);
	objField1.value = strFieldValue1;
	objField2.value = strFieldValue2;
	objField3.value = strFieldValue3;
	objForm.action = strAction;
	objForm.submit();
}

// Function Name : FormRedirectWithTwoValueConfirm
// Objective : Form redirect with passing 4 unique key code with confirm control
function FormRedirectWithFourValue(strFormName, strAction, strFieldName1, strFieldValue1, strFieldName2, strFieldValue2, strFieldName3, strFieldValue3, strFieldName4, strFieldValue4){
	var objForm = eval("document." + strFormName);
	var objField1 = eval("objForm." + strFieldName1);
	var objField2 = eval("objForm." + strFieldName2);
	var objField3 = eval("objForm." + strFieldName3);
	var objField4 = eval("objForm." + strFieldName4);
	objField1.value = strFieldValue1;
	objField2.value = strFieldValue2;
	objField3.value = strFieldValue3;
	objField4.value = strFieldValue4;
	objForm.action = strAction;
	objForm.submit();
}

// Optional Value Comparator
// If value to be compared is empty, return true. Else doing a comparation.
function OptionalValueComparator(strFormName, strCompareFieldName, strFieldValue, strCompareValue, strErrorMessage, intMode){
	var strFormElementType = GetFormElementType(strFormName, strCompareFieldName);
	var strEmptyValue = GetEmptyValueFormElementType(strFormElementType);
	
	if ( !(strEmptyValue == strCompareValue) ){
		return ValueComparator(strFieldValue, strCompareValue, strErrorMessage, intMode);
	}
	return true;
}


/*
Function : OneSelectionTextFieldMandatory
Input:
strTextField - text field value (e.g. _guiForm.ShipName.value)
strSelection - one selection index (e.g. _guiForm.CountryCodeID.selectedIndex)
strTextErrorMessage - a error message for text field is empty
strSelectionErrorMessage - a error message for no selection from a drop down list

Output:
return true if error
*/
function OneSelectionTextFieldMandatory(strTextField, strSelection, strTextErrorMessage, strSelectionErrorMessage){
	var NON_SELECT_ONE_INDEX	=	0;
	var EMPTY_STRING			=	"";

	if	( strTextField != EMPTY_STRING && strSelection == NON_SELECT_ONE_INDEX){
		alert(strSelectionErrorMessage);
		return false;
	}
	if	( strTextField == EMPTY_STRING && strSelection != NON_SELECT_ONE_INDEX){
		alert(strTextErrorMessage);
		return false;
	}
	return true;
}


/*
function to check with a text field input, the selection list must be selected with non-empty value.
*/
function OneSelectionMandatoryWithTextFieldInput(strTextField, strSelection, strSelectionErrorMessage){
	var NON_SELECT_ONE_INDEX	=	0;
	var EMPTY_STRING			=	"";

	if	( strTextField != EMPTY_STRING && strSelection == NON_SELECT_ONE_INDEX){
		alert(strSelectionErrorMessage);
		return false;
	}
	return true;
}

// Function Name : TwoMandatoryFieldChecker
// Objective : To check 3 form element are mandatory
function TwoMandatoryFieldChecker(strInput1, strInput2, strErrorMessage){
	if (strInput2 != "" ) {
		if(strInput1 == ""){
			alert(strErrorMessage);
			return false
		}
	}
	return true;
}



// Function Name : ThreeMandatoryFieldChecker
// Objective : To check 3 form element are mandatory
function ThreeMandatoryFieldChecker(strInput1, strInput2, strInput3, strErrorMessage){
	if (strInput2 != "" || strInput3 != ""){
		if(strInput1 == ""){
			alert(strErrorMessage);
			return false
		}
	}
	return true;
}

// Function Name : FourMandatoryFieldChecker
// Objective : To check 4 form element are mandatory
function FourMandatoryFieldChecker(strInput1, strInput2, strInput3, strInput4, strErrorMessage){
	if (strInput2 != "" || strInput3 != "" || strInput4 != ""){
		if(strInput1 == ""){
			alert(strErrorMessage);
			return false
		}
	}
	return true;
}

// Function Name : SevenMandatoryFieldChecker
// Objective : To check 7 form element are mandatory
function SevenMandatoryFieldChecker(strInput1, strInput2, strInput3, strInput4, strInput5, strInput6, strInput7, strErrorMessage){
	if (strInput2 != "" || strInput3 != "" || strInput4 != "" || strInput5 != "" || strInput6 != "" || strInput7 != ""){
		if(strInput1 == ""){
			alert(strErrorMessage);
			return false
		}
	}
	return true;
}

// Function Name : TextFieldMandatoryWithTrueCondition
// Objective : To check the text field is mandatory for input if condition given is 'True'
//           : It will empty the selected field value if condition given is 'False'
function TextFieldMandatoryWithTrueCondition(blnCondition, objField, strErrorMessage){
	return MandatoryFieldWithConditionTrue(blnCondition, objField, strErrorMessage);
}

function MandatoryFieldWithConditionTrue(blnCondition, objField, strErrorMessage){
	if (blnCondition){
		if (objField.value == ""){
			alert(strErrorMessage);
			return false;
		}
	}else{
		if (objField.value != ""){
			objField.value = "";
		}
	}
	return true;
}


// Function Name : MultipleCheckBoxWithTrueCondition
// Objective : To check at 1 checkbox is ticked on if condition given is 'True'
//			 : It will auto tick off if condition given is 'False' and there is any checkbox(es) being checked
function MultipleCheckBoxWithTrueCondition(blnCondition, strFormName, strField, strErrorMessage){
	var i, blnIsChecked, objForm, objField;
	blnIsChecked = false;
	objForm = eval("document." + strFormName);
	intArrayLength = eval("objForm." + strField + ".length");
	if (blnCondition){
		for (i=0; i< intArrayLength; i++){
			blnIsChecked = eval("objForm." + strField + "[" + i + "].checked");
			if ( blnIsChecked ){
				break;
			}
		}
		if (!blnIsChecked){
			alert(strErrorMessage);
			return false;
		}
	}else{
		for (i=0; i < intArrayLength; i++){
			objField = eval("objForm." + strField + "[" + i + "]");
			objField.value = ""
		}
	}
	return true;
}

// Function Name : MandatoryFieldWithSelectionListValue
// Objective : Text field must not be empty if the matching is 'True', it will empty the text field if the matching is 'False'.
function MandatoryFieldWithSelectionListValue(strSelectionListValue, strMatchValue, objField, strErrorMessage){
	if (strSelectionListValue == strMatchValue){
		if (objField.value == ""){
			alert(strErrorMessage);
			return false;
		}
	}else{
		if (objField.value != ""){
			objField.value = "";
		}
	}
	return true;
}

// Function Name : MandatoryTextFieldWithTextValue
// Objective : Mandatory text field if the specify text box is not empty
function MandatoryTextFieldWithTextValue(strInput, objField, strErrorMessage){
	if (strInput != ""){
		if (objField.value == ""){
			alert(strErrorMessage);
			return false;
		}
	}else{
		if (objField.value != ""){
			objField.value = "";
		}
	}
	return true;
}


// --------------
// Button Action
// --------------
// Function Name : _ValidateDeleteDynamicMessage
// Objective : To check at least 1 item is checked for action with confirmation dialog panel prompt.
function _ValidateDeleteDynamicMessage(strForm, strItem, strAction, strAlertMessage, strConfirmMessage)
{
	var objForm;
	eval("objForm = document." + strForm + ";");

	var blnIsChecked = _IsCheckboxChecked(strForm, strItem);

	if (!blnIsChecked)
	{
		alert(strAlertMessage);
		return false;
	}
	else
	{
		if (strConfirmMessage != "") {
			if (!confirm(strConfirmMessage))
			{
				return false;
			}
			else
			{
				objForm.action = strAction;
				objForm.submit();
			}
		}else{
			objForm.action = strAction;
			objForm.submit();
		}
	}
}


// Function Name : ValidateDeleteWithOneRecordRemain
// Objective : To check at least 1 record remain for action.
function ValidateDeleteWithOneRecordRemain(strForm, strItem, strAction, strAlertMessage, strConfirmMessage, strAlertMessageRemainOneRecord, lngTotalCheckBox){
	var objForm;
	eval("objForm = document." + strForm + ";");
	eval("objCheckbox = objForm." + strItem + ";");
	var lngCheckboxChecked = _CheckboxCount(objCheckbox, lngTotalCheckBox)
	
	if ( lngCheckboxChecked == lngTotalCheckBox ) {
		alert(strAlertMessageRemainOneRecord)
		return false;
	}
	_ValidateDeleteDynamicMessage(strForm, strItem, strAction, strAlertMessage, strConfirmMessage);
}

// Function Name : btnSubmitControl
// Objective : To process a form validation before form submitting
function btnSubmitControl(strFormName, strAction, objControl, strFormChecker){
	var objForm = eval("document." + strFormName);
	if (strFormChecker != ""){
		var objFormChecker = eval(strFormChecker + '()');
		if(!objFormChecker){
			return false;
		}
	}
	objForm.action = strAction;
	objForm.btnType.value = objControl.value;
	objForm.submit();
}

// Function Name : btnConfirm
// Objective : To validate form and confirm for action.
function btnConfirm(strFormName, strAction, strConfirmMessage, strFormChecker, objControl){
	var objForm = eval("document." + strFormName);
	if (strFormChecker != ""){
		var objFormChecker = eval(strFormChecker + '()');
		if(!objFormChecker){
			return false;
		}
	}
	if (confirm(strConfirmMessage)){
		objForm.btnType.value = objControl.value;
		objForm.action = strAction;
		objForm.submit();
	}
	return false;
}

// Function Name : AtLeastOneFieldInputFromTwoInputChecker
// Objective : To check mandatory for if at least 1 field input not empty.
// Input :
// 1. strInput1 - TextBox
// 2. strInput2 - TextBox 
// 3. strErrorMessage - Error message displaying
function MandatoryIfAtLeastOneFieldFilledChecker(strInput1, strInput2, strErrorMessage){
	if ( strInput2 != "" && strInput1 == "" ){
		alert(strErrorMessage);
		return false;
	}
	return true;
}

