/**************************************************************************************************
Author           : Vedo
Created On       : 07/08/2006
NameSpace	     : 
Class		     : 
BussinessFacade  : 
DataAcess        : 
Description		 : Includes all the javascript validations to be done in different pages.
Modified On	     : 
Modifications	 : 	
**************************************************************************************************/

/*************************************************************************************************
The following function allows the user to enter only alphabets. ( a-z and A-Z)
*************************************************************************************************/

function allowAlphabets(which)
{
	if( (which.keyCode >= 97 && which.keyCode <= 122) || (which.keyCode >= 65 && which.keyCode <= 90) ) 
	{ 
		return true; 
	} 
	else 
	{ 
		return false; 
	}
}

/*************************************************************************************************
The following function allows the user to enter only alphabets and space. ( a-z and A-Z, space)
*************************************************************************************************/

function allowAlphabetsWithSpace(which)
{
	if( (which.keyCode >= 97 && which.keyCode <= 122) || (which.keyCode >= 65 && which.keyCode <= 90) || which.keyCode == 32 ) 
	{ 
		return true; 
	} 
	else 
	{ 
		return false; 
	}
}


/*************************************************************************************************
The following function allows the user to enter only alphabets and some special characters.
( a-z,A-Z,space,dot,&,%,-,,(comma),)
*************************************************************************************************/
function allowAlphabetsWithCharacters(which)
{
	if( (which.keyCode >= 97 && which.keyCode <= 122) || (which.keyCode >= 65 && which.keyCode <= 90)
		 || which.keyCode == 32 || which.keyCode == 46 || which.keyCode == 38  || which.keyCode == 37 
		 || which.keyCode == 45 || which.keyCode == 44 ) 
	{ 
		return true; 
	} 
	else 
	{ 
		return false; 
	}
}

/*************************************************************************************************
The following function allows the user to enter only alphabets and some special characters except < >.
*************************************************************************************************/
function allowAlphaNumericWithCharacters(which)
{
	if( which.keyCode != 60  && which.keyCode != 62 ) 
	{ 
		return true; 
	} 
	else 
	{ 
		return false; 
	}
}

function allowAlphabetswithNumerics(which)
{
	if( (which.keyCode >= 97 && which.keyCode <= 122) || (which.keyCode >= 65 && which.keyCode <= 90) || ( which.keyCode >= 48 && which.keyCode <= 57 ) ) 
	{ 
		return true; 
	} 
	else 
	{ 
		return false; 
	}
}

/*************************************************************************************************
The following function allows the user to enter only numerics.
*************************************************************************************************/
function allowNumerics(which)
{
	if( ( which.keyCode >= 48 && which.keyCode <= 57 ) ) 
	{
		return true; 
	}
	else 
	{ 
		return false; 
	}
}

/*************************************************************************************************
The following function allows the user to enter decimals.
*************************************************************************************************/
function allowDecimals(which)
{
	if( ( which.keyCode >= 48 && which.keyCode <= 57 ) || which.keyCode == 46 ) 
	{
		return true; 
	}
	else 
	{ 
		return false; 
	}
}

/*************************************************************************************************
The following function checks whether the entered number is a decimal number and it is in the 
format of Decimal(flt,prec). 'flt' represents Floating Number, 'prec' represents Precision. 
The values to be passed to this function should be of the following format chkDecimal(flt,prec,num)
'num' represents the value of the textbox.
*************************************************************************************************/
function chkDecimal(flt,prec,num,id)
{


	//****** Check for the entered value is a number or not
	if ( isNaN (num) )
	{
		alert("Please enter a decimal number");
		id.value='';
		id.focus();
		return false;
	}
	/*else
	{
		num = parseFloat(num);
	}*/
	
	/*var decimalPat=/^(\d{1,3})\.(\d{1,3})$/
	var matchArray=num.match(decimalPat);	
	if (matchArray==null) 
	{
		alert("Enter a decimal number.");
		return false
	}*/
	
	//****** Check for the decimal format (to be of the format eg. 345.67 or 34.56 or 3.45)
	var arrNum = num.split(".");
	
	if ( arrNum.length > 2 )
	{
		id.focus();
		alert("Entered value is not in decimal number format.");
		return false;	
	}
	
	//****** Check for the floating numbers
	var arrNumFloat = arrNum[0].split("");	
	if( arrNumFloat.length > flt )
	{
		id.focus();
		alert("The floating number should not be greater than "+flt+ " digits.");
		id.value='';
		return false;	
	}
	
	//****** Check whether precisionis present or not
	if ( ! (arrNum.length < 2) )
	{		
		//****** Check for the precision digits
		var arrNumPrecision = arrNum[1].split("");
		if( arrNumPrecision.length > prec )
		{
			alert("The precision should be "+prec+" digits only.");			
			id.value=truncateDecimal(num);
			//return false;	
		}
	}
	else
	{
		id.value=truncateDecimal(num);
	}
	
	return true;
}

/*************************************************************************************************
The following function truncates the precision to two digits.
*************************************************************************************************/
function truncateDecimal(num)
{
	var s = "";
	var decmal;
	num = parseFloat(num);
	if ( ! ( isNaN(num) ) )
	{
		//****** round to nearest cent
		num = Math.floor(num * 100);
		num = num / 100;

		//****** format the output
		s = new String(num);
		decmal = s.indexOf(".");
		if (decmal == -1) 
		{
			//****** whole number
			s+= ".00";
		}
		else 
		{
			if (decmal == (s.length - 2)) 
			{
			//****** needs a trailing zero
			s+= "0";
			}
		}
	} 
	else 
	{
		//****** not a number so return zero
		s = "0.00";
	} 
	
	return s;
}

/*************************************************************************************************
The following function trims the string (removes the leading and trailing spaces).
*************************************************************************************************/
function Trim(str) 
{ 

	if(str.charAt(0) == " ") 
	{ 
		str = Trim(str.substring(1)); 
	} 
	if (str.charAt(str.length-1) == " ") 
	{ 
		str = Trim(str.substring(0,str.length-1)); 
	} 
	return str; 
}
/*************************************************************************************************
The following function checks whether the entered number is a decimal number and it is in the 
format of Decimal(flt,prec). 'flt' represents Floating Number, 'prec' represents Precision. 
The values to be passed to this function should be of the following format chkDecimal(flt,prec,num)
'num' represents the value of the textbox.
*************************************************************************************************/
function chkDecimalsForRepeater(flt,prec,num,id)
{


	//****** Check for the entered value is a number or not
	if ( isNaN (num) )
	{
		alert("Please enter a decimal number");
		//id.value='';
		//id.focus();
		return false;
	}
	/*else
	{
		num = parseFloat(num);
	}*/
	
	/*var decimalPat=/^(\d{1,3})\.(\d{1,3})$/
	var matchArray=num.match(decimalPat);	
	if (matchArray==null) 
	{
		alert("Enter a decimal number.");
		return false
	}*/
	
	//****** Check for the decimal format (to be of the format eg. 345.67 or 34.56 or 3.45)
	var arrNum = num.split(".");
	
	if ( arrNum.length > 2 )
	{
		//id.focus();
		alert("Entered value is not in decimal number format.");
		return false;	
	}
	
	//****** Check for the floating numbers
	var arrNumFloat = arrNum[0].split("");	
	if( arrNumFloat.length > flt )
	{
		//id.focus();
		alert("The floating number should not be greater than "+flt+ " digits.");
		//id.value='';
		return false;	
	}
	
	//****** Check whether precisionis present or not
	if ( ! (arrNum.length < 2) )
	{		
		//****** Check for the precision digits
		var arrNumPrecision = arrNum[1].split("");
		if( arrNumPrecision.length > prec )
		{
			alert("The precision should be "+prec+" digits only.");			
			//id.value=truncateDecimal(num);
			//return false;	
		}
	}
	else
	{
		id.value=truncateDecimal(num);
	}
	
	return true;
}
function chkNumericForRepeater(num)
{
	//****** Check for the entered value is a number or not
	if ( isNaN (num) )
	{
		alert("Please enter a number");		
		return false;
	}
	//****** Check for the decimal format (to be of the format eg. 345.67 or 34.56 or 3.45)
	var arrNum = num.split(".");
	
	if ( arrNum.length > 1 )
	{		
		alert("Please enter a number");
		return false;	
	}
	return true;
}
/************************************************************************************************
This function enables the user to enter in LOC when PCT equals 100
************************************************************************************************/
function chkLOCRepeater(id,val)
{
	if ( parseInt(val,10) > 100 )
	{	
		document.getElementById(id).value = "";
		document.getElementById(id).focus();
		alert("Percentage Complete should not be greater than 100"); 
		return false;		
	}
	if ( parseInt(val,10) == 100 )
	{	 
		var txtString = id.replace("txtReviewOff","txtLoc");		
		document.getElementById(txtString).readOnly = false;
		return true;
	}
	if ( parseInt(val,10) != 100 )
	{	 
		var txtString = id.replace("txtReviewOff","txtLoc");
		document.getElementById(txtString).value = "";
		return true;
	}
}

