/*
 +======================================================================================================+
 | PURPOSE OF THIS FILE
 |
 | Contain different 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 : Checks for empty and null fields
 |	ARGUMENTS : Element value
 |	RETURN VALUE : True if empty, else false
 +-------------------------------------------------------------------+
*/
function isEmpty(elementValue)
{
	if( (elementValue=="") || (elementValue==null) )
	{
		return true;	//if empty
	}
	else
	{
		return false;	//if not empty
	}
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : Checks for valid ip address
 |	ARGUMENTS : ip1,ip2,ip3,ip4
 |	RETURN VALUE : True if valid, else false
 +-------------------------------------------------------------------+
*/
function isValidIp(ip1,ip2,ip3,ip4)
{
	if(isEmpty(ip1) || isEmpty(ip2) || isEmpty(ip3) || isEmpty(ip4))
	{
		alert("Please enter a valid IP");
		return false;
	}
	else
	{
		if((ip1 < 0) || (ip1 >255) || (ip2 < 0) || (ip2 >255) || (ip3 < 0) || (ip3 >255) || (ip4 < 0) || (ip4 >255) )
		{
			alert("Please enter a valid IP");
			return false;
		}
		
		if(ip1==127 && ip2==0 && ip3==0 && ip4==1)
		{
			alert("IP 127.0.0.1 is a reserved IP, it can not be blocked/activated");
			return false;
		}
		
		if(ip1==0 && ip2==0 && ip3==0 && ip4==0)
		{
			alert("IP 0.0.0.0 is a reserved IP, it can not be blocked/activated");
			return false;
		}
		
		if(ip1==255 && ip2==255 && ip3==255 && ip4==255)
		{
			alert("IP 255.255.255.255 is a reserved IP, it can not be blocked/activated");
			return false;
		}
		
		if((isNaN(ip1)) || (isNaN(ip2)) || (isNaN(ip3)) || (isNaN(ip4)))
		{
			alert("Please enter a valid IP");
			return false;
		}
	}
	return true;
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : Checks whether the string supplied is a valid Email or not
 |	ARGUMENTS : String Value(parameter to be checked for validity)
 |	RETURN VALUE : True if valid, else false
 +-------------------------------------------------------------------+
*/
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);
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : Checks whether the string supplied is a valid URL or not
 |	ARGUMENTS : String Value(parameter to be checked for validity)
 |	RETURN VALUE : True if valid, else 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 : Checks whether any check box is selected or not
 |	ARGUMENTS : Form Name,check box id, check box name
 |	RETURN VALUE : 1 if checked, else 0
 +-------------------------------------------------------------------+
*/
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;
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : To trim spaces from left side of the string if any
 |	ARGUMENTS : String containing the string to be trimed
 |	RETURN VALUE : 1 if	parameter is not supplied, else 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) 
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : To trim spaces from right side of the string if any
 |	ARGUMENTS : String containing the string to be trimed
 |	RETURN VALUE : 1 if	parameter is not supplied, else 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)
}

/*
 +-------------------------------------------------------------------+
 |	PURPOSE : Checks for the valid dates (leap year checks, days in odd and even months)
 |	ARGUMENTS : Month,day,year
 |	RETURN VALUE : 1 if	parameter is not supplied, else string without spaces on right side
 +-------------------------------------------------------------------+
*/
function validDateGuestBook(month,day,year)	
{
	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
 |	ARGUMENTS : Month,day,year
 |	RETURN VALUE : True if effectiveDate < terminationDate else 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 : 	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;
}

//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) 
}


/*
 +-------------------------------------------------------------------+
 |	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 adjustPopUpGuestBook()
{
	var screenWidth = screen.availWidth;
	var topLeftVal = "";
										
	if(screenWidth==1024)
	{
		topLeftVal = "top=575,left=715";
		return topLeftVal;
	}
	else if(screenWidth==800)
	{
		topLeftVal = "top=405,left=490";
		return topLeftVal;
	}
	else if(screenWidth==1152)
	{
		topLeftVal = "top=670,left=845";
		return topLeftVal;
	}
	
}

/*
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:	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 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;
}

/*
 +-------------------------------------------------------------------+
 |	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;
}