// Function to keep all related datetime function 


/*
------------------------------
Function IsDateFieldEmpty
-------------------------------
Desc : Check is the date field is empty
Input : 1. strFormName - Name of Form 
		2. strFieldName - Name of date field
Output	: Return True if empty else False
*/
function IsDateFieldEmpty(strFormName, strFieldName){
	var varDateField = GetDateFieldValue(strFormName, strFieldName);
	if (varDateField == ""){
		return true;
	}
	return false
}


/*
------------------------------
Function GetDateFieldValue
-------------------------------
Desc : Get date value or drop down list value
Input : 1. strFormName - Name of Form 
		2. strFieldName - Name of date field
Output	: Return selected value
*/
function GetDateFieldValue(strFormName, strFieldName){
	var objForm = eval("document." + strFormName);
	var objField = eval("objForm." + strFieldName);
	var varDateField = objField.options[objField.selectedIndex].value;
	return varDateField;
}

/* 
------------------------------
Function DateFieldEmptyChecker
-------------------------------
Desc : Check on drop list is field empty 
Input : 1. strFormName - Name of Form 
		2. strFieldName - Name of date field 
		3. strErrorMessage - Display a error message 
Output	: Return true or false 
*/
function DateFieldEmptyChecker(strFormName, strFieldName, strErrorMessage){
	if (IsDateFieldEmpty(strFormName, strFieldName)){
		alert(strErrorMessage);
		return true;
	}
	return false;
}

/* 
------------------------------
Function SetDateToEmpty
-------------------------------
Desc : Set a drop down list to blank
Input : 1. strFormName - Name of Form 
		2. strFieldName - Name of date field 
*/
function SetDateToEmpty(strFormName, strFieldName){
	var objForm = eval("document." + strFormName);
	var objField = eval("objForm." + strFieldName)
	objField.options[0].selected = true;
}


/*
----------------------------
Function is date valid 
----------------------------
Desc: Check valid date input
Input : 1. strFormName - Name of Form 
		2. strFieldName - Name of date field 
		3. strErrorMessage - Message to display when date is not valid.
*/

function IsDateValid(strFormName, strFieldName, strErrorMessage){
	var intDay 			= GetDateFieldValue(strFormName, strFieldName + "_day")
	var intMonth		= GetDateFieldValue(strFormName, strFieldName + "_month")
	var intYear			= GetDateFieldValue(strFormName, strFieldName + "_year")
	
	if (intDay != "" && intMonth != "" && intYear != ""){
		if ( !( _CheckDateHelper(intDay, intMonth, intYear, strErrorMessage)) ) {
			return false;
		}
	}
	return true;
}

/*
----------------------------
Function is year empty
----------------------------
Desc: Check year not empty
Input : 1. strFormName - Name of Form 
		2. strFieldName - Name of date field 
		3. strErrorMessage - Message to display when date is not valid.
*/

function IsDateElementEmpty(strFormName, strFieldName, strErrorMessage){
	var intDateElement	= GetDateFieldValue(strFormName, strFieldName)
	
	if (intDateElement == ""){
		alert(strErrorMessage);
		return false;
	}
	return true;
}

// -----------------
// Full Date
// -----------------
// Function Name : DateComparatorWithOption
// Objective : This function will compare 2 date if Date 2 is not empty.

// Input :
// 1. strFormName - Name of Form
// 2. strDateField1 - Date field 1 (selection)
// 3. strDateField2 - Date field 2 (selection)
// 4. intMode - 
// 		1 = Equal, 
// 		2 = Not Equal, 
// 		3 = GreaterThan, 
// 		4 = GreaterThanEqual, 
// 		5 = Less Than,
// 		6 = LessThanEqual
// 5. strEmptyMessage - An error message when one of these 2 date is empty
// 6. strErrorMEssage - An error message display when error occurs.

// Output :
// True - not error
// False - error
function DateComparatorWithOption(strFormName, strDateField1, strDateField2, intMode, strEmptyMessage, strErrorMessage){
	var objForm = eval("document." + strFormName);
	var strDateValue1 = _GetDateValue(objForm, strDateField1);
	var strDateValue2 = _GetDateValue(objForm, strDateField2);
	
	if ( strDateValue2 != "") {
		if ( strDateValue1 == "" ) {
			alert(strEmptyMessage);
			return false;
		}
		if ( strDateValue1 != "" ) {
			if (!ValueComparator(strDateValue1, strDateValue2, strErrorMessage, intMode)){
				return false;
			}
		}
	}
	return true;
}


// Function Name : DateComparator2InputValue
// Objective : This function will compare 2 date if they are not empty
// Input : 2 date are selection input
function DateComparator2InputValue(strFormName, strDateField1, strDateField2, intMode, strErrorMessage){
	var objForm = eval("document." + strFormName);
	var strDateValue1 = _GetDateValue(objForm, strDateField1);
	var strDateValue2 = _GetDateValue(objForm, strDateField2);
	if (strDateValue1 != "" && strDateValue2 != ""){
		if (!ValueComparator(strDateValue1, strDateValue2, strErrorMessage, intMode)){
			return false;
		}
	}
	return true;
}


// Function Name : DateComparator2InputValue
// Objective : This function will compare 2 date if they are not empty
// Input : 2 date are selection input
function DateComparator(strFormName, strDateField1, strDateField2, intMode, strErrorMessage){
	var objForm = eval("document." + strFormName);
	var strDateValue1 = _GetDateValue(objForm, strDateField1);
	var strDateValue2 = _GetDateValue(objForm, strDateField2);
	if (!ValueComparator(strDateValue1, strDateValue2, strErrorMessage, intMode)){
		return false;
	}
	return true;
	//return lngDateValue;
}

// Function Name : DateValueComparator
// Objective : This funtion to compare 2 date value mandatory with 1 selection, 1 is string value e.g: 12/31/2002 (MM/DD/YYYY)
function DateValueComparator(strFormName, strDateField1, strDateField2, intMode, strErrorMessage, strSeparator){
	var objForm = eval("document." + strFormName);
	var objDate1 = eval("objForm." + strDateField1);
	var arrDate = objDate1.value.split(strSeparator);
	var intDay = arrDate[1];
	var intMonth = arrDate[0];
	var intYear = arrDate[2];
	var strDateValue1 = (10000 * intYear) + (100 * intMonth) + (1 * intDay);
	var strDateValue2 = _GetDateValue(objForm, strDateField2);
	if (!ValueComparator(strDateValue1, strDateValue2, strErrorMessage, intMode)){
		return false;
	}
	return true;
}

// Function Name : DateValueComparator2InputValue
// Objective : This function to compare 2 date value when they are not empty.
function DateValueComparator2InputValue(strFormName, strDateField1, strDateField2, intMode, strErrorMessage, strSeparator){
	var objForm = eval("document." + strFormName);
	var objDate1 = eval("objForm." + strDateField1);
	var arrDate = objDate1.value.split(strSeparator);
	var intDay = arrDate[1];
	var intMonth = arrDate[0];
	var intYear = arrDate[2];
	var strDateValue1 = (10000 * intYear) + (100 * intMonth) + (1 * intDay);
	var strDateValue2 = _GetDateValue(objForm, strDateField2);
	if (strDateValue1 != "" && strDateValue2 != "") {
		if (!ValueComparator(strDateValue1, strDateValue2, strErrorMessage, intMode)){
			return false;
		}
	}
	return true;
}

/*
function to check rebuilt date
Check Date Field with Full Date - YYMMDD, 
Date Mode
----------
1 - YYMMDD
2 - YYMM
*/
function ComparePartialDate(strFormName, strFieldName1, intDateMode1, strFieldName2, intDateMode2, intMode, strErrorMessage, blnIsNull){
	
	var objForm = eval("document." + strFormName);
	var intYear1 = GetDateFieldValue(strFormName, strFieldName1 + "_year" );
	var intMonth1 = GetDateFieldValue(strFormName, strFieldName1 + "_month" );
	var intYear2 = GetDateFieldValue(strFormName, strFieldName2 + "_year" );
	var intMonth2 = GetDateFieldValue(strFormName, strFieldName2 + "_month" );
	var intDay1 = 0; // reserved 
	var intDay2 = 0; // reserved 
	
	if ( intDateMode1 == 1 ) {
		strDateValue1 = _GetDateValue(objForm, strFieldName1);
	}
	else if( intDateMode1 == 2 ){
		strDateValue1	=	(10000 * intYear1) + (100 * intMonth1) + (1 * intDay1);
	}
	
	if ( intDateMode2 == 1 ) {
		strDateValue1 = _GetDateValue(objForm, strFieldName2);
	}
	else if( intDateMode2 == 2 ){
		strDateValue2	=	(10000 * intYear2) + (100 * intMonth2) + (1 * intDay2);
	}
	
	if ( (blnIsNull == 'Y' || blnIsNull == 'y') && (strDateValue1 == 0 || strDateValue2 == 0) ){
		return true;
	}
	
	if (!ValueComparator(strDateValue1, strDateValue2, strErrorMessage, intMode)){
			return false;
	}

	return true;
}



