/*
 +======================================================================================================+
 | PURPOSE OF THIS FILE
 |
 | Contain different JAVASCRIPT functions to be used in other files
 |
 +======================================================================================================+
*/

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : Fixes the netscape resizing bug
 |	ARGUMENTS : init
 |	RETURN VALUE : No return value
 +-------------------------------------------------------------------+
*/
function MM_reloadPage(init) //reloads the window if Nav4 resized
{  
	 if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
		document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
	 else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : Manages the WIDTH of the DIVISION used for scrolling
 |	ARGUMENTS : division id
 |	RETURN VALUE : No return value
 +-------------------------------------------------------------------+
*/
function manageDivWidth(divId)
{
	var screenWidth = screen.availWidth;
											
	if(screenWidth==1024)
	{
		eval("document.all."+divId+".style.width=705");
	}
	else if(screenWidth==800)
	{
		eval("document.all."+divId+".style.width=480");
	}
	else if(screenWidth==1152)
	{
		eval("document.all."+divId+".style.width=822");
	}
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	Manages the SIZE & POSITION of the popup window for help
 |				and comment on TRACKER in USER section
 |	ARGUMENTS : No Argument
 |	RETURN VALUE : top left position of the window
 +-------------------------------------------------------------------+
*/
function adjustPopUp()
{
	var screenWidth = screen.availWidth;
	var topLeftVal = "";
										
	if(screenWidth==1024)
	{
		topLeftVal = "top=480,left=600";
		return topLeftVal;
	}
	else if(screenWidth==800)
	{
		topLeftVal = "top=310,left=370";
		return topLeftVal;
	}
	else if(screenWidth==1152)
	{
		topLeftVal = "top=570,left=720";
		return topLeftVal;
	}
	
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	Manages the SIZE & POSITION of the popup window for 
 |				various alerts when ADMIN logs in.
 |	ARGUMENTS : No Argument
 |	RETURN VALUE : top left position of the window
 +-------------------------------------------------------------------+
*/
function adjustPopUpAdmin()
{
	var screenWidth = screen.availWidth;
	var topLeftVal = "";
										
	if(screenWidth==1024)
	{
		topLeftVal = "top=550,left=745";
		return topLeftVal;
	}
	else if(screenWidth==800)
	{
		topLeftVal = "top=380,left=520";
		return topLeftVal;
	}
	else if(screenWidth==1152)
	{
		topLeftVal = "top=647,left=875";
		return topLeftVal;
	}
	
}
function isDateEmpty(monthValue,dayValue,yearValue)
{
	if(monthValue==-1 || dayValue == -1 || yearValue ==-1)
	{
		return false;
	}
	else
	{
		return true;
	}
}
/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	Checks for the valid date
 |	ARGUMENTS : month, day, year
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/

function validDate(monthValue,dayValue,yearValue)	
{
	var month = monthValue;
	var day = dayValue;
	var year = yearValue;
	if(month != "")
	{
		if( !(isNaN(month)) )
		{
			if(month > 12)
			{
				alert("Please enter a value less than or equal to '12' for months");
				return false;
			}
			else
			{
				if( (day == "") || (year == "") )
				{
					alert("Please enter complete date");
					return false;
				}
			}
		}
		else
		{
			alert("Please enter a valid numeric value for month");
			return false;
		}
	}
	
	if(day != "")
	{
		if( !(isNaN(day)) )
		{
			if(day > 31)
			{
				alert("Please enter a value less than or equal to '31' for day");
				return false;
			}
			else
			{
				if( (month == "") || (year == "") )
				{
					alert("Please enter complete date");
					return false;
				}
			}
		}
		else
		{
			alert("Please enter a valid numeric value for day");
			return false;
		}
	}
	
	if(year != "")
	{
		if( !(isNaN(year)) )
		{
			if(month==4 || month==6 || month==9 || month==11)
			{
				if(day>30)
				{
					alert("Please enter a value less than or equal to '30' for day");
					return false;
				}
			}
			else if(month==2)
			{
				if(year%4==0)
				{
					if(day>29)
					{
						alert("Please enter a value less than or equal to '29' for day");
						return false;
					}
				}
				else
				{
					if(day>28)
					{
						alert("Please enter a value less than or equal to '28' for day");
						return false;
					}
					
				}
				
			}
			else
			{
				if( (month == "") || (day == "") )
				{
					alert("Please enter complete date");
					return false;
				}
			}
		}
		else
		{
			alert("Please enter a valid numeric value for year");
			return false;
		}
	}
	return true;
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	Compare two dates where second date should be greater
 |				than the first date.
 |	ARGUMENTS : date1 month, date1 day, date1 year, date2 month, date2 day, date12 year
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/
function compareDates(effMonth,effDay,effYear,terMonth,terDay,terYear)
{
	effective_date = new Date(effYear , effMonth-1 , effDay , 0 ,0, 0, 0);
	termination_date = new Date(terYear , terMonth-1 , terDay , 0 ,0, 0, 0);

	if(effective_date >= termination_date)
	{
		return false;
	}
	else
	{
		return true;
	}
	
}
	
/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	Checks if the provided field value is empty
 |	ARGUMENTS : value of the field
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/
function isEmpty(elementValue)
{
	if( (elementValue=="") || (elementValue==null) )
	{
		return true;	//if empty
	}
	else
	{
		return false;	//if not empty
	}
}


/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	Checks if the provided value is integer
 |	ARGUMENTS : value of the field
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/
function isNumeric(elementValue)
{
	if(isNaN(elementValue))
	{
		return false;	//not numeric
	}
	else
	{
		return true;
	}
}


/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	Checks if any chechbox is selected or not
 |	ARGUMENTS : form name, check box id, check box name
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/
function chkIfSelected(formName,chkBoxId,chkBoxName)
{
	var idCount = eval("document."+formName+"."+chkBoxId+".length");
	var isChecked=0;
	
		//checks if any checkbox is selected or not
	for(i=0;i<idCount;i++)
	{
		if(eval("document."+formName+"."+chkBoxId+"["+i+"].checked"))
		{
			isChecked=1;
		}
	}

	return isChecked;
}


//FUNCTION:	to trim spaces from right side of the string if any
//	 INPUT: 
//			strVal		-	parameter containing the string to be trimed
//	OUTPUT: 
//			1			-	if	parameter is not supplied 
//							else
//			substring	-	string	without spaces on right side			   
//
function rTrim(strVal)
{	
	nNoOfArguments = rTrim.arguments.length;
	
	if(nNoOfArguments < 1)
	{
		return 1;
	}
	
	strVal = new String(strVal);	//convert the string to string object
	
	var nLen = strVal.length;
	var nIndex = 0;
	for (nIndex = nLen-1; nIndex >=0; nIndex--)
	{
		if (strVal.charAt(nIndex) ==  " ")
		{
			nLen--;
		}
		else
		{
			break;
		}
	}
	//return substring starting between 0 as starting index to nLen as ending index
	return strVal.substring(0,nLen)
}
				
//FUNCTION:	to trim spaces from left side of the string if any
//	 INPUT: 
//			strVal		-	parameter containing the string to be trimed
//	OUTPUT: 
//			1			-	if	parameter is not supplied 
//							else
//			substring	-	string without spaces on left side
function lTrim(strVal)
{
	nNoOfArguments = lTrim.arguments.length;
	
	if(nNoOfArguments < 1)
	{
		return 1;
	}
	
	strVal = new String(strVal);	//convert the value to a string object

	var nLen = strVal.length // get the length of string
	var nIndex = 0;
	for (nIndex = 0; nIndex < nLen-1; nIndex++)
	{
		if (strVal.charAt(nIndex) !=  " ")
		{
			break;
		}
	}
	//return substring starting between nIndex as starting index to nLen as ending index
	return strVal.substring(nIndex,nLen) 
}

//FUNCTION:		to check whether the string contains any value
//	 INPUT: 
//			strVal	-	parameter to be checked
//	OUTPUT: 
//			true	-	if the string contains characters
//			false	-	if the string contains no characters or no parameter is supplied
function IsValueExist(strVal)
{
	nNoOfArguments = IsValueExist.arguments.length;
	
	if(nNoOfArguments < 1)
	{
		return false;
	}
	strVal = new String(strVal);	//convert the value to a string object
	
	// if string contains no character
	if(strVal.length == 0) 
	{
		return false;
	}
	return true;
}

//FUNCTION:	to check whether the first character of the string is space or not
//			this function is for internal use only. Call- checkFirstCharBlank(strVal)
//			to use it externally.
//	 INPUT: 
//			strVal	-	parameter to be checked
//	OUTPUT: 
//			true	-	if the first character is blank or no parameter is supplied
//			false	-	if the first character is not blank  
function IsFirstCharBlank(strVal)
{
	nNoOfArguments = IsFirstCharBlank.arguments.length;
	
	if(nNoOfArguments < 1)
	{
		return true;
	}
		
	strVal = new String(strVal);	//convert the value to a string object
	
	// if first character is blank
	if (strVal.charAt(0) == " ") 
	{
		return true;
	}
	return false;
}

//FUNCTION:	to check whether the string supplied is without spaces
//			this function is for internal use only. Call- checkStringForSpaces(strVal)
//			to use it externally.
//	 INPUT:  
//			strVal	-	parameter to be checked
//	OUTPUT: 
//			true	-	if the string contains spaces
//			false	-	if the string is without spaces
function IsStringWithSpaces(strVal)
{
	nNoOfArguments = IsStringWithSpaces.arguments.length;
	
	if(nNoOfArguments < 1)
	{
		return false;
	}	
	
	var strInValidChars = ' '; //invalid charaters i.e. space in this case
	
	bReturn = strVal.indexOf(strInValidChars);	//check for invalid characters in string
	
	if(bReturn != -1)//if any invalid character found
	{
		return true;
	}
	return false;
}

//FUNCTION:	to check whether the string supplied is a valid alphabet value
//	 INPUT:  
//			strVal	-	parameter to be checked
//	OUTPUT: 
//			true	-	if the parameter contain valid alphabet value
//			false	-	if the parameter is a invalid field or no parameter is supplied
function IsValidAlphabetValue(strVal)
{
	nNoOfArguments = IsValidAlphabetValue.arguments.length;

	//if no parameter is supplied
	if(nNoOfArguments < 1)
	{
		return false;
	}	

	//valid characters a supplied string can have
	var sValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	strVal = new String(strVal);	//convert the value to a string object
	
	var bReturn = true
	var i = new Number(0);
	while ((bReturn) && (i < strVal.length))
    {
		bReturn = (sValidChars.indexOf(strVal.charAt(i)) >= 0)
		i++
    }
	return (bReturn)
}

//FUNCTION:	to check whether the string supplied is a valid numeric value
//	 INPUT:  
//			strVal	-	parameter to be checked
//	OUTPUT: 
//			true	-	if the parameter contain valid numeric value
//			false	-	if the parameter is a invalid field or no parameter is supplied
function IsValidNumericValue(strVal)
{
	nNoOfArguments = IsValidNumericValue.arguments.length;
	
	//if any of the three parameter is not supplied
	if(nNoOfArguments < 1)
	{
		return false;
	}	
	
	//valid characters a supplied string can have
	var sValidChars = "0123456789";
	
	strVal = new String(strVal);	//convert the value to a string object
	
	var bReturn = true
	var i = new Number(0);
	
	//if number is 0
	if(0 == parseInt(strVal,10))
	return true;
	
	//if first character is 0
	if(0 == strVal.charAt(0))
	return false;
	
	while ((bReturn) && (i < strVal.length))
    {
		bReturn = (sValidChars.indexOf(strVal.charAt(i)) >= 0)
		i++
    }
	return (bReturn);
}

//FUNCTION: to check whether the string supplied is a valid Phone or Fax value
//			Second parameter(IsSpecialChar) to this function is by default 'true' 
//			i.e. a phone or fax number can have SpecialChar - '+' and '-'.
//			if you don't want to have special characters in number and want it 
//			to be pure numeric supply 'false' as second parameter
//			if you have three seperate fields for phone or fax number call this function
//			seperatly for each field value 	
//	 INPUT:  
//			strVal			-	parameter to be checked
//			IsSpecialChar	-	this parameter can have either of two values, 
//								't'	- this is default parameter it will check for 
//									  special characters i.e. '+' and '-' only
//								'f'	- if this value is passed then phone or fax 
//									  number will be checked for only numbers  	
//	OUTPUT: 
//			true			-	if the parameter contains valid phone or fax number
//			false			-	if the parameter contains invalid phone or fax number
function IsValidPhoneFaxNumber(strVal,IsSpecialChar)
{
	nNoOfArguments = IsValidPhoneFaxNumber.arguments.length;
	//if no parameter is supplied
	if(nNoOfArguments < 1)
	{
		return false;
	}		
	
	//if no IsSpecialChar value is supplied then set it to 't'
	if((nNoOfArguments == 1) && (nNoOfArguments < 2))
	{
		IsSpecialChar = 't';
	}
	
	if('f' == IsSpecialChar)
	{
		//valid characters if IsSpecialChar is false
		var sValidChars = "0123456789";
	}
	
	if('t' == IsSpecialChar)
	{
		//valid characters if IsSpecialChar is true
		var sValidChars = "0123456789+-";
	}	
	
	
	var bReturn = true;	
	var i = new Number(0);
	
	strVal = new String(strVal);
	
	if(strVal.indexOf("+") != strVal.lastIndexOf("+"))//if more than one + exist in value
	return false;
	
	while ((bReturn) && (i < strVal.length))
    {
		bReturn = (sValidChars.indexOf(strVal.charAt(i)) >= 0)
		i++
    }
	return (bReturn)
}


//FUNCTION:	to check whether the string supplied is a valid name value
//	 INPUT:  
//			strVal	-	parameter to be checked
//	OUTPUT: 
//			true	-	if the parameter is a valid name value
//			false	-	if the parameter is a invalid name value or parameter is not supplied
function IsValidNameValue(strVal)
{
	nNoOfArguments = IsValidNameValue.arguments.length;
	//if no parameter is supplied
	if(nNoOfArguments < 1)
	{
		return false;
	}
		
	//valid character a supplied string can have
	var sValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. ";
	
	var strVal = new String(strVal);
	var bReturn = true
	var i = new Number(0);
	while ((bReturn) && (i < strVal.length))
    {
		bReturn = (sValidChars.indexOf(strVal.charAt(i)) >= 0)
		i++
    }
	return (bReturn)
}

//FUNCTION: to check whether the string supplied is a valid Email value
//	 INPUT:  
//			strVal	-	parameter to be checked for validity
//	OUTPUT: 
//			true	-	if the email is valid
//			false	-	if the email is invalid or no parameter is supplied
function IsValidEMail(strValue)
{
	nNoOfArguments = IsValidEMail.arguments.length;
	//if no parameter is supplied
	if(nNoOfArguments < 1)
	{
		return false;
	}	
	
	var strVal	= new String(strValue);
	var bReturn1 = false;
	var bReturn2 = false;
	var bReturn3 = false;
	var bReturn4 = false;
	var bReturn5 = false;
	var bReturn5 = false;
	
	//if '@' comes after first character then true
	bReturn1	=	(strValue.indexOf("@") > 0);							
	
	//if '.' comes after first character then true
	bReturn2	=	(strValue.indexOf(".") > 0);							
	
	//if single '@' exist in string then true
	bReturn3	=	(strValue.indexOf("@") == strValue.lastIndexOf("@"));	
	
	//if '.' exist after @ in string then true
	bReturn4	=	(strValue.indexOf(".",strValue.indexOf("@")) > (strValue.indexOf("@")));		
	
	//if '.' does not comes immediatly after @ then true
	bReturn5	=	(strValue.indexOf(".",strValue.indexOf("@")) != (strValue.indexOf("@")+1));		
	
	//if '.' does not comes immediatly before @ then true
	bReturn6	=	(strValue.lastIndexOf(".",strValue.indexOf("@")) != (strValue.indexOf("@")-1)); 
	
	//if all values are true return true else false.
	return(bReturn1 && bReturn2 && bReturn3 && bReturn4 && bReturn5 && bReturn6);
}

/*
FUNCTION checkNameField: checks any name field code value for various possible cases
INPUT 
		strValue			-  is name value, supplied as string
		nMaxlen (optional)	-  is the maximum length a input field can have, supplied as integer
		nMinlen (optional)	-  is the minimum length a input field can have, supplied as integer

OUTPUT
		0 if "No Error"
		1 if "Name field is blank or value is not supplied"		
		2 if "First character is space in Value supplied"
		3 if "Name's field is not valid alpahabatic value
		4 if "Name field's characters length is below prescribed length"
		5 if "Name field's characters length is above prescribed length"
		6 if "nMaxlen or nMinlen are invalid or equal to 0 or less then 0, in function parameters"
		
*/
function checkNameField(strValue,nMaxlen,nMinlen)
{
	//if nMinlen is equal to 0 and value i.e. strValue is not supplied return success code
	//else return error code
	if (0 == nMinlen)
	{
		if(!strValue)
		{
			return(0);
		}
	}
	else
	{
		if(!strValue)
		{
			return(1);
		}
	}
	
	//if String value supplied is empty or contains nothing	
	if(!IsValueExist(strValue))
	{	
		return (1);
	}
	
	//if the first character of the string value supplied is space
	if(IsFirstCharBlank(strValue))
	{
		return (2);
	}
	
	//if the string value supplied is not a valid Name i.e. it should be of pure alphabets etc.
	if(!IsValidNameValue(strValue))
	{
		return (3);
	}	
	
	//if nMinlen value supplied then
	if(nMinlen)
	{
		//if invalid minimum length supplied
		if(isNaN(nMinlen) || parseInt(nMinlen) < 0)
		{		
			return (6);
		}		
		//if valid length supplied but string length is less then nMinlen
		if(strValue.length < parseInt(nMinlen))	
		{
			return (4);
		}		
		
	}
	
	//if nMaxlen value supplied then	
	if(nMaxlen)
	{ 
		//if invalid maximum length supplied
		if(isNaN(nMaxlen) || parseInt(nMaxlen) <= 0)
		{	
			return (6);	
		}
		//if valid length supplied but string length is greater than nMaxlen
		if(strValue.length > parseInt(nMaxlen))
		{
			return (5);
		}
	}
	return (0);
}

/*
FUNCTION checkStringField: checks any string value for various possible cases
INPUT 
		strValue			-  is input field value supplied as string.
		nMaxlen (optional)	-  is the maximum length a input field can have, supplied as integer.
		nMinlen (optional)	-  is the minimum length a input field can have, supplied as integer.

OUTPUT
		0 if "No Error"
		1 if "string value is blank or value is not supplied"
		2 if "string value started with blank space"
		3 if "string value's characters length is below prescribed length"
		4 if "string value's characters length is above prescribed length"
		5 if "nMinlen or nMaxlen are invalid or less then 0 in Input field"
		
*/
function checkStringField(strValue,nMaxlen,nMinlen)
{

	//if nMinlen is equal to 0 and value i.e. strValue is not supplied return success code
	//else return error code
	if (0 == nMinlen)
	{
		if(!strValue)
		{
			return(0);
		}
	}
	else
	{
		if(!strValue)
		{
			return(1);
		}
	}
	
	//if string value supplied is empty or contains nothing
	if(!IsValueExist(strValue))
	{	
		return (1);
	}
	
	//if the first character of the string value supplied is space
	if(IsFirstCharBlank(strValue))
	{
		return (2);
	}			
		
	//if nMinlen value supplied then
	if(nMinlen)
	{
		//if invalid minimum length supplied
		if(isNaN(nMinlen) || parseInt(nMinlen) < 0)
		{		
			return (5);
		}		
		//if valid length supplied but the string length is less then nMinlen
		if(strValue.length < parseInt(nMinlen))	
		{
			return (3);
		}		
		
	}
	
	//if nMaxlen value supplied then	
	if(nMaxlen)
	{ 
		//if invalid maximum length supplied
		if(isNaN(nMaxlen) || parseInt(nMaxlen) <= 0)
		{	
			return (5);	
		}
		//if valid length supplied but the string length is greater then nMaxlen
		if(strValue.length > parseInt(nMaxlen))
		{
			return (4);
		}
	}
	return (0);
}

/*
FUNCTION checkEmail: checks email value for various possible cases
INPUT 
		strValue			-	is email input field value supplied as string.
		nMinlen(Optional)	-	supply value 0 if you if email field is optional or don't supply this parameter	
		
OUTPUT
		0 if "No Error"
		1 if "Input field is blank or parameter is supplied"
		2 if "Input field contain spaces"
		3 if "Invalid Email value is supplied"
		
*/
function checkEmail(strValue,nMinlen)
{
	//if nMinlen is equal to 0 and value i.e. strValue is not supplied return success code
	//else return error code
	if (0 == nMinlen)
	{
		if(!strValue)
		{
			return(0);
		}
	}
	else
	{
		if(!strValue)
		{
			return(1);
		}
	}
	
	//if email value supplied is emtpy or contains nothing	
	if(!IsValueExist(strValue))
	{	
		return (1);
	}
	
	//if email value supplied contain spaces
	if(IsStringWithSpaces(strValue))
	{
		return (2);
	}	
	
	//if not a valid email value supplied	
	if(!IsValidEMail(strValue))
	{
		return (3)
	}
	return (0);
}

/*
FUNCTION checkPhoneFaxString: checks any Phone and Fax string value for various possible cases
INPUT 
		strVal				-	phone or fax string.
		nMinLen(optional)	-	minimum length phone or fax field can have
								if 0 is supplied as nMinLen filed will be considered as optional
								i.e. nothing is passed to function in strVal with nMinLen as 0 it 
								will return sucess code.	
		nMaxLen(optional)	-	minimum length phone or fax field can have
	
OUTPUT
		0 if "No Error"
		1 if "Phone or Fax number value is blank or compulsory parameters is not supplied"
		2 if "Phone or Fax number value contains invalid characters"		
		3 if "Phone or Fax number value contain blank space"
		4 if "Phone or Fax number value's characters length is invalid"		
		5 if "nMinLen or nMaxLen is invalid or less then 0 in function parameters"		
		
*/
function checkPhoneFaxString(strVal,nMinLen,nMaxLen)
{
	//if nMinLen is equal to 0 and value i.e. strVal is not supplied return success code
	//else return error code
	if (0 == nMinLen)
	{
		if(!strVal)
		{
			return(0);
		}
	}
	else
	{
		if(!strVal)
		{
			return(1);
		}
	}
	
	//if Phone or Fax number supplied is empty or contain nothing	
	if(!IsValueExist(strVal))
	{	
		return (1);
	}

	//check if not a valid phone or fax number
	if(!IsValidPhoneFaxNumber(strVal))
	{
		return (2);
	}
	
	//if nMinLen value supplied then
	if(nMinLen)
	{
		//if invalid length supplied
		if(isNaN(nMinLen) || parseInt(nMinLen) <= 0)
		{		
			return (5);
		}		
		//if valid length supplied but string length of the supplied value is less then nMinLen
		if(strVal.length < parseInt(nMinLen))	
		{
			return (4);
		}				
	}
	//if nMaxLen value supplied then
	if(nMaxLen)
	{
		//if invalid length supplied
		if(isNaN(nMaxLen) || parseInt(nMaxLen) <= 0)
		{		
			return (5);
		}		
		//if valid length supplied but string length of the supplied value is greater then nMaxLen
		if(strVal.length > parseInt(nMaxLen))	
		{
			return (4);
		}				
	}
	
	return (0);
}

/*
FUNCTION checkValidNumZipCode: checks if the value supplied is a valid Numeric zip code
INPUT 
		strVal				-	is zip code supplied as string/numeric
		nMinLen(optional)	-	is the minimum length of the zip code supplied as numeric
							 	if nMinLen is equal to 0 and value i.e. strVal is not supplied 
							 	field will be consider as optional and function will return 
							 	success code i.e. it will check filed only if strVal is supplied 
							 	with any value
		nMaxLen(optional)	-	is the maximum length of the zip code supplied as numeric
		
OUTPUT
		0 if "No Error"
		1 if "Zip code value is blank or parameter is not supplied"
		2 if "Zip code value contains invalid characters"
		3 if "Zip code value contain blank space"
		4 if "Zip code value's characters length is invalid"
		5 if "nMinLen or nMaxLen is invalid or less then 0 in function parameter"
*/
function checkValidNumZipCode(strVal, nMinLen,nMaxLen)
{	

	//if nMinLen is equal to 0 and value i.e. strVal is not supplied return success code
	//else return error code
	if (0 == nMinLen)
	{
		if(!strVal)
		{
			return(0);
		}
	}
	else
	{
		if(!strVal)
		{
			return(1);
		}
	}

	//if zip value supplied is empty or contains nothing		
	if(!IsValueExist(strVal))
	{	
		return (1);
	}
	
	//if not a valid numeric value
	if(!IsValidNumericValue(strVal))
	{	
		return (2);
	}
	
	//if zip code value contain spaces
	if(IsStringWithSpaces(strVal))
	{
		return (3);
	}
	
	//if nMinLen value supplied then
	if(nMinLen)
	{
		//if invalid length supplied
		if(isNaN(nMinLen) || parseInt(nMinLen) <= 0)
		{		
			return (5);
		}		
		//if valid length supplied but string length of zip code value is less then nMinLen
		if(strVal.length < parseInt(nMinLen))	
		{
			return (4);
		}				
	}
	//if nMaxLen value supplied then
	if(nMaxLen)
	{
		//if invalid length supplied
		if(isNaN(nMaxLen) || parseInt(nMaxLen) <= 0)
		{		
			return (5);
		}		
		//if valid length supplied but string length of zip code value is greater then nMaxLen
		if(strVal.length > parseInt(nMaxLen))	
		{
			return (4);
		}				
	}


	return (0);	
} 

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	This function returns the MONTH in the INT format
 |				while the month passed to it is in STRING format
 |				e.g, 	0 for Jan
 |						1 for Feb and so on..
 |	ARGUMENTS : Month in String format
 |	RETURN VALUE : integer value for the month
 +-------------------------------------------------------------------+
*/
function createStringToIntMonth(stringMonth)
{
	switch(stringMonth)
	{
      		case "JAN":
        		return 0;
        	case "FEB" :
        		return 1;
           	case "MAR" :
        		return 2;
        	case "APR" :
        		return 3;
        	case "MAY" :
        		return 4;
        	case "JUN" :
        		return 5;
        	case "JUL" :
        		return 6;
        	case "AUG" :
        		return 7;
        	case "SEP" :
        		return 8;
        	case "OCT" :
        		return 9;
        	case "NOV" :
				return 10;
        	case "DEC" :
        		return 11;
        	default:
				return -1;
  	}

}


/*
FUNCTION chkValidDate: checks if the value supplied is a valid Date
INPUT 
		strVal				-	is Date supplied as string/numeric
		
		
OUTPUT
		0 if "No Error"
		1 if "Invalid Date Format"
		2 if "Innvalid Month"
		3 if "Days should Be less than or equal to 30"
		4 if "Days should Be less than or equal to 31"
		5 if "Days should Be less than or equal to 29"
		6 if "Days should Be less than or equal to 28"
		
*/
function chkValidDate(strVal)
{	
	//alert("dfdf");
	arrOpeningDate = strVal.split("-");//alert("i am ok"+arrOpeningDate[0]+arrOpeningDate[1]);
	if(arrOpeningDate.length != 3)
	{
		return -1;
	}
	year = arrOpeningDate[2];
	month = createStringToIntMonth(arrOpeningDate[0].toUpperCase());
	day =  arrOpeningDate[1];
	if( (isNaN(day)) || (isNaN(year)) || month ==-1 )
	{
		return -1;
	}
	month++;
	if(day > 31)
	{
		alert("Please enter a value less than or equal to '31' for day");
		return -2;
	}
	if(month==4 || month==6 || month==9 || month==11)
	{
		if(day>30)
		{
			alert("Please enter a value less than or equal to '30' for day");
			return -2;
		}
	}
	else if(month==2)
	{
		if(year%4==0)
		{
			if(day>29)
			{
				alert("Please enter a value less than or equal to '29' for day");
				return -2;
			}
		}
		else
		{
			if(day>28)
			{
				alert("Please enter a value less than or equal to '28' for day");
				return -2;
			}
			
		}
		
	}
	return true;
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	This function validates the URL provided as parameter
 |	ARGUMENTS : URL as a string
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/
function IsValidURL(strValue)
{
	nNoOfArguments = IsValidURL.arguments.length;
	//if no parameter is supplied
	if(nNoOfArguments < 1)
	{
		return false;
	}	
	
	var strVal	= new String(strValue);
	var bReturn1 = false;
	var bReturn2 = false;
	var bReturn3 = false;
	var bReturn4 = false;
	var bReturn5 = false;
	var bReturn6 = false;
	var bReturn7 = false;
	var bReturn8 = false;
	var bReturn9 = false;
	var temp = new String(strVal.toUpperCase());
	//if '@' comes after first character then true
	bReturn1	=	(strValue.indexOf("@") == -1);							
	
	//if '.' comes after first character then true
	bReturn2	=	(strValue.indexOf(".") > 0);							
	
	//if single '//' exist in string then true
	if(strValue.indexOf("//")!=-1)
	{
		bReturn3	=	((strValue.indexOf("//") == strValue.lastIndexOf("//")) && ((temp.indexOf("HTTP://") == 0) || (temp.indexOf("HTTPS://") == 0)));
		bReturn3 = (((temp.indexOf("HTTPS://") == temp.lastIndexOf("HTTPS://")) || (temp.indexOf("HTTP://") == temp.lastIndexOf("HTTP://"))) && bReturn3);
			//if ':' exist after // in string then true
		//bReturn3 = (bReturn3 && (!(strValue.indexOf(":",strValue.indexOf("//")) > (strValue.indexOf("//")))));
		
		//if '.' exist after // in string then true
		bReturn4	=	(strValue.indexOf(".",strValue.indexOf("//")) > (strValue.indexOf("//")));		
		
		//if '.' does not comes immediatly after // then true
		bReturn5	=	(strValue.indexOf(".",strValue.indexOf("//")) != (strValue.indexOf("//")+1));		
		
		//if '.' does not comes immediatly before // then true
		bReturn6	=	(strValue.lastIndexOf(".",strValue.indexOf("//")) != (strValue.indexOf("//")-1)); 
	
	}
	else
	{
		bReturn3 = true;
		bReturn3 = true;
		bReturn4 = true;
		bReturn5 = true;
		bReturn6 = true;
	}
	
	//if all values are true return true else false.
	return(bReturn1 && bReturn2 && bReturn3 && bReturn4 && bReturn5 && bReturn6);
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	This function validates the logical expression string
 |	ARGUMENTS : logical expression string, new value (operator / operand)
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/

function validateExpression(txtAreaValue, clickedValue)
{
	var temp= txtAreaValue;
	var eventName = clickedValue;
	
	if(temp!="") //trim the string
	{
		temp = rTrim(temp);
		if(temp!="")
		{
			temp = lTrim(temp);
		}
	}
	
	if(temp=="")	// if expression is empty
	{
		if(eventName == ")" || eventName.toUpperCase() == "AND" || eventName.toUpperCase() == "OR" || eventName.toUpperCase() == "XOR")
		{
			alert("Closing bracket or binary operator can not come at the first position of logical expression.");
			return false;
		}
		return true;
		
	}
	else	//when expression is not empty
	{
		chkExpression = temp.split(" ");	
		index = chkExpression.length;
		//check for operator should not come just after the open bracket and not operator.
		if(chkExpression[index - 1 ] == "(" || chkExpression[index - 1 ].toUpperCase() == "NOT")
		{
			if(eventName == ")" || eventName.toUpperCase() == "AND" || eventName.toUpperCase() == "OR" || eventName.toUpperCase() == "XOR")	
			{
				alert("Closing bracket or binary operator can not come just after the '" + chkExpression[index - 1 ] + "' in logical expression.");
				return false;
			}
		}
		else
		{
			//check for operator repitation
			if(chkExpression[index - 1 ].toUpperCase() == "AND" || chkExpression[index - 1 ].toUpperCase() == "OR" || chkExpression[index - 1 ].toUpperCase() == "XOR")	
			{
				if(eventName.toUpperCase() == "AND" || eventName.toUpperCase() == "OR" || eventName.toUpperCase() == "XOR")	
				{
					alert("Binary operator can not come together in logical expression.");
					return false;
				}
				if(eventName == ")")
				{
					alert("Closing bracket can not come just after the binaryoperator in logical expression.");
					return false;
				}
			}
			else
			{	// check for event related ckecks and closing bracket checks.
				
				if(eventName == "(")
				{
					if(chkExpression[index - 1 ] != ")" )//it should be event only
					{
						alert("The start bracket can not come just after the operand (Event) in logical expression.");
						return false;	
					}
					if(chkExpression[index - 1 ] == ")" )//it should be event only
					{
						alert("There should be one binary operator between closing bracket and opening bracket.");
						return false;	
					}
				}
				if(eventName.toUpperCase() == "NOT")
				{
					if(chkExpression[index - 1 ] != ")" )//it should be event only
					{
						alert("The NOT operator can not come just after the operand (Event) in logical expression.");
						return false;	
					}
					if(chkExpression[index - 1 ] == ")" )//it should be event only
					{
						alert("The NOT operator can not come just after the closing bracket ')' in logical expression.");
						return false;	
					}
				}
				if(eventName.toUpperCase() != "AND" && eventName.toUpperCase() != "OR" && eventName.toUpperCase() != "XOR" && eventName.toUpperCase() != "NOT" && eventName != "(" && eventName != ")")	
				{
					if(chkExpression[index - 1 ] == ")" )
					{
						alert("The operand (Event) can not come just after the closing bracket ')' in logical expression.");
						return false;
					}
					else
					{
						alert("There should be one binary operator between two operands (Events).");
						return false;
					}
				}
			}
		}
		return true;
		
	}
return false;
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	This function validates the event names in logical expression string
 |	ARGUMENTS : logical expression string, a string of event names seperated by coma and 
 				white space from name should be replaced by underscore
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/

function isEventNameCorrect(txtAreaValue, strEvantNames)
{
	txtAreaValue= txtAreaValue.split(" ");
	strEvantNames = strEvantNames.split(" ");
	var blnNameCorrect =  false;
	var index = txtAreaValue.length;
	for(i=0; i < index; i++)
	{
		if(txtAreaValue[i] != "(" && txtAreaValue[i] != ")" && txtAreaValue[i].toUpperCase() != "AND" && txtAreaValue[i ].toUpperCase() != "OR" && txtAreaValue[i].toUpperCase() != "XOR" && txtAreaValue[i].toUpperCase() != "NOT")	
		{
			blnNameCorrect = false;
			for(j=0; j< strEvantNames.length; j++) // iterate for all the event name
			{
				if(strEvantNames[j]==txtAreaValue[i])// if name match found 
				{
					blnNameCorrect = true;
				}
			}
			if(!blnNameCorrect)
			{
				alert("Please check the events name '" + txtAreaValue[i] + "'. \nEvent name is case sensitive and white space within the name should be replaced by '_' underscore");
				return false;
			}
		}
	}
	
	return true;
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	This function validates the logical expression string
 |	ARGUMENTS : logical expression string, a string of event names seperated by coma and 
 				white space from name should be replaced by underscore
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/

function validateExpressionSubmited(txtAreaValue, strEvantNames)
{
	var index, temp, returnValue= false;
	if(txtAreaValue=="")
	{
		//alert("Logical expression box is empty please enter the expression");
		return true;
	}

	if(!isEventNameCorrect(txtAreaValue, strEvantNames))
	{
		return false;
	}
	
	txtAreaValue= txtAreaValue.split(" ");
	index = txtAreaValue.length;
	for(i=0; i< index; i++)
	{
		if(i==0)
		{
			temp = "";
			returnValue = validateExpression(temp, txtAreaValue[i]);
		}
		else
		{
			temp = temp + " " + txtAreaValue[i-1];
			returnValue = validateExpression(temp, txtAreaValue[i]);
		}
		if(!returnValue)
		{
			return false;
		}
	}
	
	return true;
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	This function validates the bracket count (opening bracket == closing bracket)
 				in logical expression string
 |	ARGUMENTS : logical expression string
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/

function checkBracketCount(txtAreaValue)
{
	leftBracket = txtAreaValue.split("(");
	rightBracket = txtAreaValue.split(")");
	if(leftBracket.length == rightBracket.length)
	return true;
	
	if(leftBracket.length > rightBracket.length)
	{
		alert("Right bracket ')' is expected.");
		return false;
	}
	if(leftBracket.length < rightBracket.length)
	{
		alert("Left bracket '(' is expected.");
		return false;
	}
}
