/**********************************************************************************************************************
	Name	: libApriori.js
	Purpose	: Library of javascript functions that do common VB function tasks as well as other common tasks
	Note	: Some functions originally downloaded from Apriori-IT website on 01.12.2007
**********************************************************************************************************************/
/*****************************************************************************************************
	Name			: CBool
	Purpose			: converts parameter to Boolean value
	Parameters		: value
	Return Value	: true / false
****************************************************************************************************/
function CBool(VALUE)
{
	VALUE = new String(VALUE);
	VALUE = VALUE.toLowerCase();
	if (VALUE == "1" || VALUE == "-1" || VALUE == "true" || VALUE == "yes")
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*****************************************************************************************************
	Name			: Len
	Purpose			: returns the character length of a string
	Parameters		: string
	Return Value	: length (interger)
****************************************************************************************************/
function Len(STRING)
{
	return STRING.length;
}

/*****************************************************************************************************
	Name			: InStr
	Purpose			: 
	Parameters		:
	Return Value	:
****************************************************************************************************/
function InStr(STRING, SUBSTRING, COMPARE, START)
{
	if (START)
	{
		STRING = STRING.substring(START, STRING.length);
	}

	if (CBool(COMPARE) || COMPARE == undefined)
	{
		STRING = STRING.toLowerCase();
		SUBSTRING = SUBSTRING.toLowerCase();
	}

	if (STRING.indexOf(SUBSTRING) > -1)
	{
		return STRING.indexOf(SUBSTRING)
	}
	else
	{
		return 0;
	}
}

/*****************************************************************************************************
	Name			: Mid
	Purpose			: 
	Parameters		:
	Return Value	:
****************************************************************************************************/
function Mid(STRING, START, END)
{
	if (!START)
	{
		START = 0;
	}
	
	if (!END || END > STRING.length)
	{
		END = STRING.length;
	}

	if (END != STRING.length)
	{
		END = START + END;
	}
	
	return STRING.substring(START, END);
}

/*****************************************************************************************************
	Name			: Trim
	Purpose			: Removes leading and trailing white space from string 
	Parameters		: String value
	Return Value	: String value		
****************************************************************************************************/
function Trim(STRING)
{
	STRING = LTrim(STRING);
	return RTrim(STRING);
}

/*****************************************************************************************************
	Name			: RTrim
	Purpose			: Removes trailing white space from string 
	Parameters		: String value
	Return Value	: String value		
****************************************************************************************************/
function RTrim(STRING)
{
	while(STRING.charAt((STRING.length -1))==" ")
	{
		STRING = STRING.substring(0,STRING.length-1);
	}
	return STRING;
}

/*****************************************************************************************************
	Name			: LTrim
	Purpose			: Removes leading white space from string 
	Parameters		: String value
	Return Value	: String value		
****************************************************************************************************/
function LTrim(STRING)
{
	while(STRING.charAt(0)==" ")
	{
		STRING = STRING.replace(STRING.charAt(0),"");
	}
	return STRING;
}

/*****************************************************************************************************
	Name			: IsEmpty
	Purpose			: Determines if string value is Null or has length of zero
	Parameters		: String value
	Return Value	: True / False		
****************************************************************************************************/
function IsEmpty(myText) 
{
	if ((myText.value.length == 0) || (myText.value == null)) 
	{
		return true;
	}
	else 
	{
		return false; 
	}
}

/*****************************************************************************************************
	Name			: IsNumeric
	Purpose			: Determines if string value is numerical value
	Parameters		: String value
	Return Value	: True / False		
****************************************************************************************************/
function IsNumeric(sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

/*****************************************************************************************************
	Name			: FormatDate
	Purpose			: Determines if string value is numerical value
	Parameters		: String value
	Return Value	: True / False		
****************************************************************************************************/
function FormatDate(DateToFormat, FormatAs)
{
	if (DateToFormat == "")
	{
		return "";
	}
	
	if (!FormatAs)
	{
		FormatAs = "dd/mm/yyyy";
	}

	var strReturnDate;

	FormatAs = FormatAs.toLowerCase();
	DateToFormat = DateToFormat.toLowerCase();
	
	var arrDate
	var arrMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var strMONTH;
	var Separator;

	while(DateToFormat.indexOf("st") > -1)
	{
		DateToFormat = DateToFormat.replace("st", "");
	}

	while(DateToFormat.indexOf("nd") > -1)
	{
		DateToFormat = DateToFormat.replace("nd", "");
	}

	while(DateToFormat.indexOf("rd") > -1)
	{
		DateToFormat = DateToFormat.replace("rd", "");
	}

	while(DateToFormat.indexOf("th") > -1)
	{
		DateToFormat = DateToFormat.replace("th", "");
	}

	if (DateToFormat.indexOf(".") > -1)
	{
		Separator = ".";
	}

	if(DateToFormat.indexOf("-") > -1)
	{
		Separator = "-";
	}

	if(DateToFormat.indexOf("/") > -1)
	{
		Separator = "/";
	}

	if(DateToFormat.indexOf(" ")> - 1)
	{
		Separator = " ";
	}

	arrDate = DateToFormat.split(Separator);
	DateToFormat = "";
	for(var iSD = 0; iSD < arrDate.length; iSD++)
	{
		if (arrDate[iSD] != "")
		{
			DateToFormat += arrDate[iSD] + Separator;
		}
	}
	
	DateToFormat = DateToFormat.substring(0, DateToFormat.length - 1);
	arrDate = DateToFormat.split(Separator);

	if(arrDate.length < 3)
	{
		return "";
	}

	var DAY = arrDate[0];
	var MONTH = arrDate[1];
	var YEAR = arrDate[2];

	if (parseFloat(arrDate[1]) > 12)
	{
		DAY = arrDate[1];
		MONTH = arrDate[0];
	}

	if (parseFloat(DAY) && DAY.toString().length == 4)
	{
		YEAR = arrDate[0];
		DAY = arrDate[2];
		MONTH = arrDate[1];
	}

	for(var iSD = 0; iSD < arrMonths.length; iSD++)
	{
		var ShortMonth = arrMonths[iSD].substring(0, 3).toLowerCase();
		var MonthPosition = DateToFormat.indexOf(ShortMonth);
		if (MonthPosition > -1)
		{
			MONTH = iSD + 1;
			if (MonthPosition == 0)
			{
				DAY = arrDate[1];
				YEAR = arrDate[2];
			}
			break;
		}
	}

	var strTemp = YEAR.toString();
	if (strTemp.length == 2)
	{

		if (parseFloat(YEAR) > 40)
		{
			YEAR = "19" + YEAR;
		}
		else
		{
			YEAR = "20" + YEAR;
		}
	}

	if (parseInt(MONTH) < 10 && MONTH.toString().length < 2)
	{
		MONTH = "0" + MONTH;
	}

	if (parseInt(DAY) < 10 && DAY.toString().length < 2)
	{
		DAY = "0" + DAY;
	}
	
	switch (FormatAs)
	{
		case "dd/mm/yyyy":
			return DAY + "/" + MONTH + "/" + YEAR;
		
		case "mm/dd/yyyy":
			return MONTH + "/" + DAY + "/" + YEAR;
			
		case "dd/mmm/yyyy":
			return DAY + " " + arrMonths[MONTH - 1].substring(0, 3) + " " + YEAR;
			
		case "mmm/dd/yyyy":
			return arrMonths[MONTH -1].substring(0, 3) + " " + DAY + " " + YEAR;
	
		case "dd/mmmm/yyyy":
			return DAY + " " + arrMonths[MONTH - 1] + " " + YEAR;
			
		case "mmmm/dd/yyyy":
			return arrMonths[MONTH - 1] + " " + DAY + " " + YEAR;
	}
	
	return DAY + "/" + strMONTH + "/" + YEAR;
}

/*****************************************************************************************************
	Name			: IsDate
	Purpose			: Determines if parameter is a vaid date value
	Parameters		: String value
	Return Value	: True / False		
****************************************************************************************************/
function IsDate(DateToCheck)
{
	if (DateToCheck == "")
	{
		return true;
	}

	var m_strDate = FormatDate(DateToCheck);
	
	if (m_strDate == "")
	{
		return false;
	}
	
	var m_arrDate = m_strDate.split("/");
	var m_DAY = m_arrDate[0];
	var m_MONTH = m_arrDate[1];
	var m_YEAR = m_arrDate[2];

	if(m_YEAR.length > 4)
	{
		return false;
	}

	m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;

	var testDate = new Date(m_strDate);

	if (testDate.getMonth() + 1 == m_MONTH)
	{
		return true;
	} 
	else
	{
		return false;
	}
}

/*****************************************************************************************************
	Name        	: validateDateValue
	Purpose			: Determines if entered date is valid in form and value
	Return Value	: boolean value, true if date is valid, false otherwise. 
	Parameters  	: strDateValue - string containing the range beginning date.
*****************************************************************************************************/
/*
function validateDateValue(strDateValue)
{

	alert("rsa [" + strDateValue.value + "]");
	
	var bDateFlag;
	//var intSlashPos1;
	//var intSlashPos2;

	bDateFlag = false;
	
	// *******InStr(STRING, SUBSTRING, COMPARE, START)*******

	intSlashPos1 = InStr(strDateValue, "/",, 1);	//intSlashPos1 = InStr(1, strDateValue, "/", 1);
	alert("intSlashPos1 [" + intSlashPos1 + "]");
	
	
	return false;

	intSlashPos2 = InStr(intSlashPos1 + 1, strDateValue, "/", 1);

	
	alert("intSlashPos2 [" + intSlashPos2 + "]");

	if (intSlashPos1 <= 0 || intSlashPos2 <= 0)
	{
		bDateFlag = false;
	}
	else
	{
		strMonth = Mid(strDateValue, 1, intSlashPos1 - 1);
		strDay = Mid(strDateValue, intSlashPos1 + 1, intSlashPos2 - intSlashPos1 - 1);
		strYear = Mid(strDateValue, intSlashPos2 + 1, Len(strDateValue) - intSlashPos2);
		
		alert("month[" + strMonth + "] day[" + strDay + "] Year[" + strYear + "]");
		
		if (Len(strMonth) > 2  || Len(strDay) > 2 || Len(strYear) <> 4)
		{
			bDateFlag = false;
		}
		else
		{
			if (!IsNumeric(strMonth))
			{
				bDateFlag = false;
			}
			else
			{
				if (!IsNumeric(strDay))
				{
					bDateFlag = false;
				}
				else
				{
					if (!IsNumeric(strYear))
					{
						bDateFlag = false;
					}
					else
					{
						if (!IsDate(strDateValue))
						{
							bDateFlag = false;
						}
					}
				}
			}
		}
	}	
    return bDateFlag;
}
*/
/*****************************************************************************************************
	Name			: validateTextBoxDATE
	Purpose			: Determines if textbox value is valid (i.e. Not Null and Not whitespace and valid date)
	Parameters		: String value, Alert Box Message
	Return Value	: True / False		
****************************************************************************************************/
function validateTextBoxDATE(myTextBox, myMsg)
{
	var blnValid;
	
	myTextBox.value = Trim(myTextBox.value);
			
	if (IsEmpty(myTextBox))
	{
		alert(myMsg);
		blnValid = false; 
	}
	else
	{
		if (!IsDate(myTextBox.value)) 
		{
			alert(myMsg);
			blnValid = false; 
		}
		else
		{
			blnValid = true;
		}	
	}
	
	return blnValid;
}

/*****************************************************************************************************
	Name			: validateTextBoxSTRING
	Purpose			: Determines if textbox value is valid (i.e. Not Null and Not whitespace)
	Parameters		: String value, Alert Box Message
	Return Value	: True / False		
****************************************************************************************************/
function validateTextBoxSTRING(myTextBox, myMsg)
{
	var blnValid;
	
	myTextBox.value = Trim(myTextBox.value);
			
	if (IsEmpty(myTextBox))
	{
		alert(myMsg);
		blnValid = false; 
	}
	else
	{
		blnValid = true;
	}

	return blnValid;
}

/*****************************************************************************************************
	Name			: validateTextBoxNUMBER
	Purpose			: Determines if textbox value is valid (i.e. Not Null and Not whitespace and Numeric)
	Parameters		: String value, Alert Box Message
	Return Value	: True / False		
****************************************************************************************************/
function validateTextBoxNUMBER(myTextBox, myMsg)
{
	var blnValid;
	
	myTextBox.value = Trim(myTextBox.value);
			
	if (IsEmpty(myTextBox))
	{
		alert(myMsg);
		blnValid = false; 
	}
	else
	{
		if (!IsNumeric(myTextBox.value)) 
		{
			alert(myMsg);
			blnValid = false; 
		}
		else
		{
			blnValid = true;
		}
	}

	return blnValid;
}

/*****************************************************************************************************
	Name			: validateTextBoxHOUR
	Purpose			: Determines if textbox value is valid (i.e. Not Null and Not whitespace and Numeric between 1 and 12)
	Parameters		: String value, Alert Box Message
	Return Value	: True / False		
****************************************************************************************************/
function validateTextBoxHOUR(myTextBox, myMsg)
{
	var blnValid;
	
	myTextBox.value = Trim(myTextBox.value);
			
	if (IsEmpty(myTextBox))
	{
		alert(myMsg);
		blnValid = false; 
	}
	else
	{
		if (!IsNumeric(myTextBox.value)) 
		{
			alert(myMsg);
			blnValid = false; 
		}
		else
		{
			if (myTextBox.value < 1 || myTextBox.value > 12)
			{
				alert(myMsg);
				blnValid = false;
			}
			else
			{
				blnValid = true;
			}
		}
	}

	return blnValid;
}

/*****************************************************************************************************
	Name			: validateTextBoxMINUTE
	Purpose			: Determines if textbox value is valid (i.e. Not Null and Not whitespace and Numeric between 0 and 59)
	Parameters		: String value, Alert Box Message
	Return Value	: True / False		
****************************************************************************************************/
function validateTextBoxMINUTE(myTextBox, myMsg)
{
	var blnValid;
	
	myTextBox.value = Trim(myTextBox.value);
			
	if (IsEmpty(myTextBox))
	{
		alert(myMsg);
		blnValid = false; 
	}
	else
	{
		if (!IsNumeric(myTextBox.value)) 
		{
			alert(myMsg);
			blnValid = false; 
		}
		else
		{
			if (myTextBox.value < 0 || myTextBox.value > 59)
			{
				alert(myMsg);
				blnValid = false;
			}
			else
			{
				blnValid = true;
			}
		}
	}

	return blnValid;
}