/**************************************************************************************
 * File Name	:	Common.js
 * Description	:	This file contains all the common client side javascript functions which will 
 *						be used in SMBSA application.
 *
 * Modification Log
 * Ver		Date					Author                       Description
 * ------------------------------------------------------------
 * 0.00a  	17-Dec-2004        Sasidhar GN				Initail Draft
 * 1.00		15-Nov-2005			vidrao					New method to submit a form
 * 1.01		04-Apr-2006			vidrao				Modifications in method to open in parent window
 **************************************************************************************/


//******************************************************************************
// Function Name		:	fnOpenChildWindow
// Created Date			:	31-Dec-2004
// Description			:	Opens a new window with no location bars, toolbars
// Input Parameters		:	strURL - URL which to be opened in the new window
//							strWindowName - Name of the New window
// Output Parameters	:	None
//******************************************************************************		
function fnOpenChildWindow(strURL,strWindowName)
{
    var popUp = '';
    if(fnIsBlank(strWindowName))
		strWindowName=new Date().getTime();
    popUp = window.open(strURL, strWindowName, var_SRWindowAttributes);
    if (!popUp.opener)
         popUp.opener = self;
    popUp.focus();
    
}


//******************************************************************************
// Function Name		:	fnOpenWindow
// Created Date			:	31-Dec-2004
// Description			:	Opens a new normal window 
// Input Parameters		:	strURL - URL which to be opened in the new window
//									strWindowName - Name of the New window
// Output Parameters	:	None
//******************************************************************************		
function fnOpenWindow(strURL, strWindowName)
{
	if(fnIsBlank(strWindowName))
		strWindowName = 'newWindow';

    var popUp = '';
    popUp = window.open(strURL, strWindowName, 'width=700,height=600,top=50,left=70,location=1,toolbar=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');
    if (!popUp.opener)
         popUp.opener = self;
    popUp.focus();     
}


//******************************************************************************
// Function Name		:	fnOpenInParentWindow
// Created Date			:	04-Feb-2005
// Description			:	Opens url in parent window
// Input Parameters		:	strURL - URL which to be opened in the parent window
// Output Parameters	:	None
//******************************************************************************		
function fnOpenInParentWindow(strURL)
{
	//Modifications done to open in new window if opener is not present
    if( (!window.opener) || (window.opener.closed) )
		{
			var popUp;
			popUp = window.open(strURL, "OpenerWindow");
			popUp.focus();
		}else
		{
		    window.opener.location.href = strURL;
			window.opener.focus();
		}	     
}


//******************************************************************************
// Function Name			:	fnLTrim
// Created Date			:	17-Dec-2004
// Description				:	Returns a string containing a copy of a specified string with no leading spaces 
// Input Parameters		:	strData - Any valid string expression
// Output Parameters	:	None
//******************************************************************************		
function fnLTrim(strData)
{
	if(fnIsNull(strData))
		return "";
		
	for(var iCount=0; strData.charAt(iCount)==" "; iCount++);
		return strData.substring(iCount, strData.length);
}

//******************************************************************************
// Function Name			:	fnRTrim
// Created Date			:	17-Dec-2004
// Description				:	Returns a string containing a copy of a specified string with no trailing spaces 
// Input Parameters		:	strData - Any valid string expression
// Output Parameters	:	None
//******************************************************************************		
function fnRTrim(strData)
{
	if(fnIsNull(strData))
		return "";
		
	for(var iCount=strData.length-1; strData.charAt(iCount)==" "; iCount--);
		return strData.substring(0, iCount+1);
}
	
//******************************************************************************
// Function Name			:	fnTrim
// Created Date			:	17-Dec-2004
// Description				:	Returns a string containing a copy of a specified string with no leading and trailing spaces 
// Input Parameters		:	strData - Any valid string expression
// Output Parameters	:	None
//******************************************************************************		
function fnTrim(strData)
{	
	return fnIsNull(strData) ? "" : fnLTrim(fnRTrim(strData));
}

//******************************************************************************
// Function Name			:	fnIsNull
// Created Date			:	17-Dec-2004
// Description				:	Returns true if value is null
// Input Parameters		:	strData - Any string expression
// Output Parameters	:	Boolean value
//******************************************************************************		
function fnIsNull(strData)
{
	return(strData==null);
}


//******************************************************************************
// Function Name			:	fnIsBlank
// Created Date			:	17-Dec-2004
// Description				:	Returns true if value only contains spaces
// Input Parameters		:	strData - Any string expression
// Output Parameters	:	Boolean value
//******************************************************************************
function fnIsBlank(strData)
{
	if(strData==null) 
		{return true;}
	for(var iCount=0; iCount<strData.length; iCount++) 
	{
		if ((strData.charAt(iCount)!=' ')&&(strData.charAt(iCount)!="\t")&&(strData.charAt(iCount)!="\n")&&(strData.charAt(iCount)!="\r")){return false;}
	}
	return true;
}



//******************************************************************************
// Function Name			:	fnCheckMaxLength
// Created Date			:	17-Dec-2004
// Description				:	This functions is used to find whether data entered in a text box 
//									is crossed the maximum size or not.
// Input Parameters		:	sFieldValue - Value entered in the object (either textbox or textarea) 
//									iMaxLength - Maximum size of the data to check
// Return					:	Boolean - Returns true if data entered more than the maximum size else return false.
//******************************************************************************	
function fnCheckMaxLength (sFieldValue, iMaxLength)
{
    var DB_SIZE = 4;
    var sRetValue = "";
    var iTmp = 0;
    var iCount= 0;
    var iContentLength = 0;
    var iLen = 0;

    iLen = sFieldValue.length;
    for (iCount = 0; iCount < iLen ; iCount++)
    {
        iTmp = escape(sFieldValue.charAt(iCount)).length;
        // If  iTmp greater than 4 increment length by 2
        if (DB_SIZE <= iTmp)
        {
            iContentLength = iContentLength + 2;
        }
        else    // else increment by 1
        {
            iContentLength++;
        }
    }

    if (iContentLength > iMaxLength)
        sRetValue = false;
    else
        sRetValue = true;

    return sRetValue;
}

//**************************************************************************************************************************
//Function Name		: fnIsValidEmail
//Created Date		: 17 Dec, 2004
//Description			: This method is used to validate Email id. 
//Input Parameters	: sValue - Email ID value to validate
//Output Parameters : True if it is a valid email value else return false.
//**************************************************************************************************************************
function fnIsValidEmail(sValue)
{	
	var strPattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/	
	var matchArray = sValue.match(strPattern);
	if (matchArray == null)
	{		
		return false;
	}
	return true;
}

//******************************************************************************
// Function Name			:	fnCompareDates
// Created Date			:	11-Jan-2005
// Description				:	This function is used to compare 2 dates and return boolean value. 
//									If fromDate is passed as null then toDate will compare with current date.
// Input Parameters		:	Dates which needs to compare.
// Return					:	Returns  0 - If Both the dates are equal
//												1 - If Date1 is less than Date2
//												2 - If Date1 is greater than Date2
//******************************************************************************
function fnCompareDates(fromDate, toDate)
{
	var arrDate, dd, mm, yy;

	if ( (null != fromDate) && (fromDate != "") )
	{
		var arrDate = fromDate.split("/");

		var mm = parseInt(arrDate[0], 10);
		var dd = parseInt(arrDate[1], 10);
		var yy = parseInt(arrDate[2], 10);

		fromDate = new Date(yy, mm-1, dd);
	}
	else
	{
		fromDate = new Date();
	}

	if ( (null != toDate) && (toDate != "") )
	{
		var arrDate = toDate.split("/");

		var mm = parseInt(arrDate[0], 10);
		var dd = parseInt(arrDate[1], 10);
		var yy = parseInt(arrDate[2], 10);

		toDate = new Date(yy, mm-1, dd);

		if(fromDate == toDate)
		{
			return 0;
		}
		else if (fromDate < toDate)
		{
			return 1;
		}
		else if (fromDate > toDate)
		{
			return 2;
		}
	}
}


//******************************************************************************
// Function Name			:	fnValidateDate
// Created Date			:	11-Jan-2005
// Description				:	This function is used to validate the date whether it is in the 
//									right format (mm/dd/yyyy) or not.
// Input Parameters		:	It takes date as a parameter 
// Return					:	Returns true if date format is proper else returns false
//******************************************************************************
function fnValidateDate(strDate)
{
	var m_flag;

	strDate=fnTrim(strDate);
	
	if (strDate.length == 0)
	{
		return false;
	}
	else
	{
		m_flag=strDate.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/g);
	}

	if(!m_flag)
		return false;

	arrDate=new Array();
	arrDate=strDate.split("/");

	var mm1=parseInt(arrDate[0],10);
	var dd1=parseInt(arrDate[1],10);
	var yy1=parseInt(arrDate[2],10);

	if(mm1 >12 || mm1 <1 || dd1 > 31 || dd1 <1 || yy1 >9999 || yy1 <1)
		return false;


	var days= fnDaysInMonth(mm1,yy1);
	if(dd1 > days)
		return false;

	return true;
}

//******************************************************************************
// Function Name			:	fnDaysInMonth
// Created Date			:	11-Jan-2005
// Description				:	This function is used to find number of days in a given month and year
// Input Parameters		:	Takes month and year as a parameters
// Return					:	Returns number of days in a month
//******************************************************************************
function fnDaysInMonth(month, year)
{
    var days;

    if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
	{
        days=31;
	}
    else if (month==4 || month==6 || month==9 || month==11)
	{
        days=30;
	}
    else if (month==2)
    {
        if (isLeapYear(year))
        {
            days=29;
        }
        else
        {
            days=28;
        }
    }

    return (days);
}

//******************************************************************************
// Function Name			:	isLeapYear
// Created Date			:	11-Jan-2005
// Description				:	This function is used to find if year is a leap year or not
// Input Parameters		:	Takes year as a parameter
// Return					:	Returns true if it is a leap year else return false.
//******************************************************************************
function isLeapYear(Year) 
{
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) 
        return true;
    else 
        return false;

}
/*Begin ADD vidrao 15-Nov-2005*/
//******************************************************************************
// Function Name		:	fnSubmitForm
// Created Date			:	15-Nov-2005
// Description			:	This function set the form action to passed actions and submits it
// Input Parameters		:	formAction-Form's action
// Return				:	None
//******************************************************************************

	function fnSubmitForm(formAction){
		document.forms[0].action=formAction;
		document.forms[0].submit();		
	}
/*End ADD vidrao 15-Nov-2005*/

