//*********************************************************************************
//		GLOBAL COMMON METHODS
//*********************************************************************************
/**************************************************************************************
Function : ChkSelect

Purpose  : Function to check whether a filed has any information entered into it.
Input    : Search Type, Module Type, Alert Messsage 
Output   : The test result as a Boolean
**************************************************************************************/	

// If form has only one checkbox in the result, then this method helps to select the checkbox
function ChkSelect(strSearchType, strModuleType, bManyPage)
{			
	// If the form has more than one page then don't preselect checkboxes on the result form.
	if(bManyPage)
		return false;
	var coll = document.all;
	var objChk;
	var iCount = 0;
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
		{
			if(coll.item(i).type == "checkbox")			
			{	
				objChk = coll.item(i);
				// If the search is on LHSM && for Multiple LOT then 
				// all the checkboxes has to be selected by default.								
				if(strSearchType == "multiple_land" && strModuleType == "LHSM")	
					objChk.checked = true;												
				iCount++;			
			}
		}
		if(iCount == 1)
			objChk.checked = true;
	}
}
// If form has only one checkbox in the result, then this method helps to select the checkbox
function ChkSelectForBack(strModule)
{			
	var coll = document.all;
	var objChk;	
	if (coll!=null) 
	{
		for (i=0; i<coll.length; i++) 
		{
			if(coll.item(i).type == "checkbox")			
			{	
				objChk = coll.item(i);
				if(objChk.checked)
				{
					if(strModule =="PTEIM" || strModule =="PTI")
					{
						// DEV titles
						var strId = objChk.id.toString();
						if ((strId.indexOf('chkDevDocNum') != -1)||(strId.indexOf('pgtblDevTable')!= -1))
						{
							iChkboxSelectionCountForDev++;
						}									
						else if ((strId.indexOf('chkNonDevDocNum')!= -1) ||(strId.indexOf('pgtblNonDevTable')!= -1))
						{
							iChkboxSelectionCountForNonDev++;
						}
					}
					else
					{					
						iChkboxSelectionCount++;	
					}	
				}			
			}
		}		
	}		
}
// This method helps increasing or decreasing the checkbox selected count based on its state.
// This will be useful to maintain the count & state of the checkboxes when multiple pages of result comes.
//### BUG_FIX_TO_CHECK_NO_OF_CHECKBOX_EXCEED_25_START  2236 change by Catherine 13 July 2006
function ChangeCheckboxStatus(oCheckbox, isRule23)
{	
	ChangeCheckboxStatus2(oCheckbox);
	
	if(isRule23 && oCheckbox.checked)
		CallAlertRule23(oCheckbox, isRule23);
}
function ChangeCheckboxStatus2(oCheckbox)
{
	var strId = oCheckbox.id.toString();
	
	//alert('strId is ' + strId);	
	//alert(iChkboxSelectionCountForNonDev + ' ' + iChkboxSelectionCountForDev);
	
	if ((strId.indexOf('chkNonDevDocNum')!= -1) ||(strId.indexOf('pgtblNonDevTable')!= -1))
	{
		if(oCheckbox.checked)
			iChkboxSelectionCountForNonDev++;
		else
			iChkboxSelectionCountForNonDev--;
	
		if (iChkboxSelectionCountForNonDev > 25)
		{
			alert("You may not select more than 25 title documents.");
			iChkboxSelectionCountForNonDev--;
			oCheckbox.checked = false;
		}
		
		//alert('first ' + iChkboxSelectionCountForNonDev + ' ' + iChkboxSelectionCountForNonDev);
	}
	else if ((strId.indexOf('chkDevDocNum') != -1)||(strId.indexOf('pgtblDevTable')!= -1))
	{
		if(oCheckbox.checked)
			iChkboxSelectionCountForDev++;
		else
			iChkboxSelectionCountForDev--;
		
		if (iChkboxSelectionCountForDev > 25)
		{
			alert("You may not select more than 25 title documents.");
			iChkboxSelectionCountForDev--;
			oCheckbox.checked = false;
		}
		
		//alert('second ' + iChkboxSelectionCountForNonDev + ' ' + iChkboxSelectionCountForNonDev);
	}
	else
	{
		if(oCheckbox.checked)
			iChkboxSelectionCount++;
		else
			iChkboxSelectionCount--;
		
		if (iChkboxSelectionCount > 25)
		{
			alert("You may not select more than 25 title documents.");
			iChkboxSelectionCount--;
			oCheckbox.checked = false;
		}
		
		//alert('third ' + iChkboxSelectionCountForNonDev + ' ' + iChkboxSelectionCountForNonDev);
	}
	
	//alert('to go');
}
function CallAlertRule23(oChekBox, isRule23)
{
	
	var Title = " ("+isRule23+")";
	var isShowTitleNo = true;
	
	if (document.getElementById("hdnTitleChkBoxCount") != null)
	{
		if(document.getElementById("hdnTitleChkBoxCount").value == 1)
		{
			isShowTitleNo = false;
		}
	}
	else if (document.getElementById('hdnCheckBoxCount') != null && 
		document.getElementById("hdnCheckBoxCount").value == 1)
	{
		isShowTitleNo = false;
	}
	else if ((document.getElementById("hdnDevCheckBoxCount") != null) && (document.getElementById("hdnNonDevCheckBoxCount") != null))
	{
		var DevChkCount =  Math.round(document.getElementById('hdnDevCheckBoxCount').value);
		var NonDevChkCount =  Math.round(document.getElementById('hdnNonDevCheckBoxCount').value);
		if ((DevChkCount == 1 && NonDevChkCount == 0) || (DevChkCount == 0 && NonDevChkCount == 1 ))
		{
			isShowTitleNo = false;
		}
	}		  
	if(!confirm("Separate titles issued for the units in the development" + ((isShowTitleNo)? Title : "") + ". Do you wish to proceed with the search? Click [OK] to proceed with the search. If not, click [Cancel]"))
	{
		oChekBox.checked = false;
		ChangeCheckboxStatus2(oChekBox);
	}
	else
	{
		oChekBox.checked = true;
		bAlreadyPrompt = true;
	}	
}

// Represents an bool instance which holds a flag value whether Validation Rule23 already prompted or not.
var bAlreadyPrompt = false;

//### BUG_FIX_TO_CHECK_NO_OF_CHECKBOX_EXCEED_25_END  2236 change by Catherine 13 July 2006
//*********************************************************************************
//		GLOBAL Variables
//*********************************************************************************
// This is an instance which holds the flag value which represents weahter to proceed 
// with submit button's validations.
var bProccedSubmitValidation = true;
// This is an instance which holds the count of selected checkbox when multiple result pages.
var iChkboxSelectionCount = 0;
// This is an instance which holds the count of selected checkbox when multiple result pages for PTEIM which has DEV and NON DEV.
var iChkboxSelectionCountForDev = 0;
var iChkboxSelectionCountForNonDev = 0;
// Represents an instance which holds the Checkbox control Id when Rule 23 is needed to be 
// satisfied on Submit button click.
var Rule23CheckBoxID = "";
var Rule23CheckBoxText = "";

// This flag variable will be true if the search result form has multiple pages of results, 
// else the flag value will will be false.
var bMorePages = false;

/**********************************************************************************
Name				:	CommonValidations.js
Description			:	This contains all common validation functions.
Developer			:	Nidhish Dhru	
Creation Date		:	17/08/2005
***********************************************************************************/
// **BEGIN COMMON VALIDATION FUNCTIONS**

/**************************************************************************************
Function : checkempty

Purpose  : Function to check whether a filed has any information entered into it.
Input    : Form Name,Field Name,Alert Messsage 
Output   : The test result as a Boolean
**************************************************************************************/	

		
		function checkempty(formName,fieldName,msg)
			{
				if(eval("document."+formName+"."+fieldName+".value")==0)
					{
						alert(msg);	
						eval("document."+formName+"."+fieldName+".focus()");	
						return false;
					}
				else
					{
						return true;
					}
			}
/**************************************************************************************
Function : isEmpty

Purpose  : A function to determine if an input value has been entered at all

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/
function isEmpty(inputStr) 
{
	inputStr = inputStr.toString();
	if (inputStr == "" || inputStr == null) 
	{
		return true
	}
	//Check for string of all spaces
	var empty = "Y"
	for (var i=0; i < inputStr.length; i++) 
	{
		var oneChar = inputStr.charAt(i)
		if (oneChar != " ")
		{
			empty = "N"
		}
	}
	if (empty == "Y")
	{
		return true
	}
	return false
}


/**************************************************************************************
Function : isAlpha

Purpose  : A function to determine if a string contains only letters of the alphabet

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/
	
	function isAlpha(strInput)
	{
		regExp = new RegExp("[^A-Za-z]");
		return (!regExp.test(strInput));
	}

/**************************************************************************************
Function : isFirstCharAlpha

Purpose  : A function to determine if the first char of a string contains only the alphabet

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/

	function isFirstCharAlpha(strInput)
	{		
		return (isAlpha(strInput.substr(0,1)));
	}
	
/**************************************************************************************
Function : isAlphaSlash

Purpose  : A function to determine if a string contains only letters of the alphabet or Slash

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/
	
	function isAlphaSlash(strInput)
	{
		regExp = new RegExp("[^A-Za-z/]");
		return (!regExp.test(strInput));
	}
	
/**************************************************************************************
Function : isNumeric

Purpose  : A function to determine if a string is a number

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/

	function isNumeric(strInput) 
	{
		re = new RegExp("[^0-9]");
		return (!re.test(strInput));
	}


/**************************************************************************************
Function : isAlphaNumeric

Purpose  : A function to determine if the String contains any character other than 'a-z', 'A-Z', '0-9'

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/

	function isAlphaNumeric(strInput)
	{
		re = new RegExp("[^0-9A-Za-z]");
		return (!re.test(strInput));
	}


/**************************************************************************************
Function : isAlphaNumericAndUnderscore

Purpose  : A function that takes in a string and returns a bool which says whether
			the string contains only alphanumeric with underscore or not.

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/
function isAlphaNumericAndUnderscore(strValue)
{
		
		re = new RegExp("[^0-9A-Za-z_]", "i");
		return (!re.test(strValue));
}

/**************************************************************************************
Function : isAlphaNumericAndHyphen

Purpose  : A function that takes in a string and returns a bool which says whether
			the string contains only alphanumeric with hyphen or not.

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/
function isAlphaNumericAndHyphen(strValue)
{
	re = new RegExp("[^0-9A-Za-z-]", "i");
	return (!re.test(strValue));
}


/**************************************************************************************
Function : isAlphaNumericAndSpace

Purpose  : A function that takes in a string and returns a bool which says whether
			the string contains only alphanumeric and space or not.

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/
function isAlphanumericAndSpace(strValue)
{
	re = new RegExp("[^0-9A-Z ]", "i");
	return (!re.test(strValue));
}


/**************************************************************************************
Function : isAlphaNumericAndFewSpecialChar

Purpose  : A function that takes in a string and returns a bool which says whether
			the string contains only alphanumeric, space, single quote(') , hyphen (-) , 
			forward slash (/) or amphesend (&) or a dot (.)

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/
function isAlphaNumericAndFewSpecialChar(strValue)
{
	re = new RegExp("[^0-9A-Z'-/&. ]", "i");
	return (!re.test(strValue));
}



/**************************************************************************************
Function : isValidBlockHouseNo

Purpose  : A function that takes in a string and returns a bool which says whether
			the string contains only alphanumeric, space, hyphen and forward slash.

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/
function isValidBlockHouseNo(strValue)
{
	re = new RegExp("[^0-9A-Z-/ ]", "i");
	return (!re.test(strValue));
}


/**************************************************************************************
Function : isInRange

Purpose  : A function to determine if value is in acceptable range for this application

Input    : String, LowerRange, UpperRange
Output   : The test result as a Boolean
**************************************************************************************/

	function isInRange(inputStr, lo, hi) 
	{
		var num = parseInt(inputStr, 10)
		if (num < lo || num > hi) 
		{
			return false
		}
		return true
	}


/**************************************************************************************
Function : lTrim

Purpose  : A function to trim leading spaces

Input    : String 
Output   : The trimmed string
**************************************************************************************/

	function lTrim(strVar)
	{
		var count=0;
		var chknewline;
		tempStr = new Array();
		tempStr = strVar.split('\n');
		chknewline = strVar.indexOf("\n")
		strVar = "";
		for(var intIndex = 0; intIndex < tempStr.length; intIndex++)
		{
			count = 0;
			while(tempStr[intIndex].charAt(count)==null||tempStr[intIndex].charAt(count)==" "||tempStr[intIndex].charAt(count)==' ')
				count++;		

			tempStr[intIndex] = tempStr[intIndex].substring(count,tempStr[intIndex].length);
			if( chknewline!= -1)
			{
				if(tempStr[intIndex] == null || tempStr[intIndex] == " " || tempStr[intIndex] == "" || tempStr[intIndex] == "\n" ||tempStr[intIndex].length == 1)
				{
					continue;
				}
				else
				{
					strVar += tempStr[intIndex];
					strVar += "\n";
				}
			}
			strVar = tempStr[intIndex]; 
		
		}
		
		return strVar;
	}


/**************************************************************************************
Function : rTrim

Purpose  : A function to trim trailing spaces

Input    : String 
Output   : The trimmed string
**************************************************************************************/

	function rTrim(strVar)
	{
		while (strVar.charAt(strVar.length-1)  == ' ')
		      strVar = strVar.substring(0,strVar.length-1);
		return strVar;      
	}


/**************************************************************************************
Function : trim

Purpose  : A function to trim leading and trailing spaces

Input    : String 
Output   : The trimmed string
**************************************************************************************/

	function trim(strInput)
	{
		var index;
		var startindex=-1;
		var lastindex=-1;
		var strTemp;
		for (i=0;i < strInput.length; i++)
		{
			if(strInput.charAt(i) != ' ')
			{
				startindex=i
				break;
			}
		}
		for (i=strInput.length-1;i >= 0; i--)
		{
			if(strInput.charAt(i) != ' ')
			{
				lastindex=i
				break;
			}
		}
		strTemp=strInput.substring(startindex,lastindex+1);
		return strTemp;
	}


/**************************************************************************************
Function : replace

Purpose  : A function to replace a particular string by another

Input    : String , SearchText, ReplacementText
Output   : The replaced string
**************************************************************************************/
	function replace(string,text,by)
	{
	    var strLength = string.length;
	    var txtLength = text.length;
	    if ((strLength == 0) || (txtLength == 0))
	    return string;

	    var i = string.indexOf(text);

	    if ((!i) && (text != string.substring(0,txtLength)))
	    return string;

	    if (i == -1)
	    return string;

	    var newstr = string.substring(0,i) + by;

	    if (i+txtLength < strLength)
	        newstr += replace(string.substring(i+txtLength,strLength),text,by);

	    return newstr;
	}



/**************************************************************************************
Function : select

Purpose  : A function to select a field and focus the cursor to that field

Input    : Field 
Output   : None
**************************************************************************************/

	function select(field) 
	{
		field.focus()
		field.select()
	}


/**************************************************************************************
Function : isValidStringLength

Purpose  : A function that checks the length of the String value passed on to it

Input    : String ,MaxLength
Output   : The test result as a Boolean
**************************************************************************************/

	function isValidStringLength(strInput,intMaxLength)
	{	
		var strTemp= new String(strInput);
		if (strTemp.length > intMaxLength)
			return false;
		else
			return true;
	}
 
 
/**************************************************************************************
Function : isFloat

Purpose  : A function to check if the string is a valid float value

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/

	function isFloat(strInput) 
	{
		var arrSplit;
		strInput=trim(strInput);
		if(strInput.length==0) return false;
		
		arrSplit = strInput.split("."); 

		if (arrSplit.length > 2) 
		{ 
			return false; 
		} 
		else 
		{ 
			for (i=0; i < strInput.length; i++) 
			{ 
				var ch = strInput.substring(i,i+1) 
				if ((ch < "0" || "9" < ch) && ch !=".") 
				{ 
					return false; 
				} 
			} 
		} 
		return true; 
	}


/**************************************************************************************
Function : isThreeDecimal

Purpose  : A function to check for 3 decimal places

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/
	
	function isThreeDecimal(strValue)
	{

			var i = 0;
			var sLength = strValue.length;
			if (isEmpty(strValue)) return false;
			if (!isFloat(strValue)) return false;
			if (trim(strValue) == ".") return false;
			// look for .
			while ((i < sLength) && (strValue.charAt(i) != "."))
			{ 
				i++
			}
			if (i == sLength) return true;
			// there can be maximum of 3 numbers after the .
			if (((i+2) == sLength) || ((i+3) == sLength) || ((i+4) == sLength))return true;
			else return false;
	}


/**************************************************************************************
Function : isFloatWithinNDecimals

Purpose  : A function to check for N numbers after the decimal place.This function returns 
			true if there are N or less numbers after the decimal place.
			
Input    : String , NoOfDigitsAfterDecimal
Output   : The test result as a Boolean
**************************************************************************************/	
	
	function isFloatWithinNDecimals(szFloat,nDecimals)
	{	
		//call is float
		var arrSplit;
		if (isFloat(szFloat))
		{
			arrSplit = szFloat.split("."); 
			return arrSplit[1].length >= nDecimals;
		}
		else
		{	
			return false;
		}
		
	}
	

/**************************************************************************************
Function : Validate15Point3Format

Purpose  : A function to determine if a string has not more than 15 digits before decimal 
			and not more than 3 digits after decimal.

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/	
	function Validate15Point3Format(szFloat)
	{
		return isFloatInValidFormat(szFloat,15,3);
	}


/**************************************************************************************
Function : isFloatInValidFormat

Purpose  : A function to determine if an input string has digits not more than nBeforeDecimals
			before decimal and not more than nAfterDecimals after decimal.

Input    : String, nBeforeDecimals, nAfterDecimals
Output   : The test result as a Boolean
**************************************************************************************/	
	function isFloatInValidFormat(szFloat,nBeforeDecimals,nAfterDecimals)
	{
		var arrSplit;
		if (isFloat(szFloat))
		{			
			if(hasDecimalPoint(szFloat))
			{
				arrSplit = szFloat.split("."); 
				if((arrSplit[0].length > nBeforeDecimals)||(arrSplit[1].length > nAfterDecimals))
				{
					return false;
				}
				else
				{
					return true;
				}
			}
			else
			{
				if(szFloat.length > nBeforeDecimals)
				{
					return false;
				}
				else
				{
					return true;
				}
			}
		}
		else
		{
			return false;
		}
				
	}


/**************************************************************************************
Function : isNumberInValidFormat

Purpose  : A function to determine if an input string is within the Maximun length and 
			has digits not more than nBeforeDecimals before decimal and not more than 
			nAfterDecimals after decimal.
			
Input    : String ,nMaxDigits,nBeforeDecimals,nAfterDecimals
Output   : The test result as a Boolean
**************************************************************************************/	
	function isNumberInValidFormat(szFloat,nMaxDigits,nBeforeDecimals,nAfterDecimals)
	{
		var arrSplit;
		if(isNumeric(szFloat))
		{
			if(parseInt(szFloat,10) <= 0)
			{
				return false;
			}
			if(szFloat.length  > nMaxDigits)
			{
				return false;
			}
			else
			{
				return true;
			}
					
		}
		else
		{
			if (isFloat(szFloat))
			{	
				arrSplit = szFloat.split(".");
				
				if(arrSplit[0].length == 0 || arrSplit[1].length == 0)
				{
					return false;
				}
				if((parseInt(arrSplit[0],10) <= 0) && (parseInt(arrSplit[1],10) <=0))
				{
					return false;
				}
				if((arrSplit[0].length > nBeforeDecimals)||(arrSplit[1].length > nAfterDecimals))
				{
					return false;
				}			
				else
				{
					return true;
				}
			}
			else
			{
				return false;
			}
		}		
	}


/**************************************************************************************
Function : isNegativeNumberInValidFormat

Purpose  : A function to determine if an input string is within the Maximun length and 
			has digits not more than nBeforeDecimals before decimal and not more than 
			nAfterDecimals after decimal.

Input    : String ,nMaxDigits,nBeforeDecimals,nAfterDecimals,blIsNegative
Output   : The test result as a Boolean
**************************************************************************************/
	function isNegativeNumberInValidFormat(szFloat,nMaxDigits,nBeforeDecimals,nAfterDecimals,blIsNegative)
	{
		var arrSplit;
		if(isNumeric(szFloat))
		{
			if(blIsNegative)
			{
				if(parseInt(szFloat) <= 0)
				{
					return false;
				}
			}
			if(szFloat.length  > nMaxDigits)
			{
				return false;
			}
			else
			{
				return true;
			}		
		}
		else
		{
			if (isFloat(szFloat))
			{	
				arrSplit = szFloat.split(".");
				if((arrSplit[0].length == 0) || (arrSplit[1].length == 0))
				{
					return false;
				}
				if(blIsNegative)
				{
					if((parseInt(arrSplit[0],10) <= 0) && (parseInt(arrSplit[1],10) <= 0))
					{
						return false;
					}
				}
				if((arrSplit[0].length > nBeforeDecimals)||(arrSplit[1].length > nAfterDecimals))
				{
					return false;
				}			
				else
				{
					return true;
				}
			}
			else
			{
				return false;
			}
		}		
	}


/**************************************************************************************
Function : hasDecimalPoint

Purpose  : A function to check whether the given number has a decimal point

Input    : String 
Output   : The test result as a Boolean
**************************************************************************************/	

	function hasDecimalPoint(szNumber)
	{
		var re = new RegExp(".[\.]");
		return(re.test(szNumber));
	}


/**************************************************************************************
Function : ReplaceSpecialCharactersForHTML

Purpose  : A function to replace special characters in HTML

Input    : String 
Output   : The replaced string
**************************************************************************************/	
		
	function ReplaceSpecialCharactersForHTML(szInput)
	{
		var sztemp=new String(szInput);
		var regexp1=/&/g;
		var regexp2=/\"/g;
		var regexp3=/</g;
		var regexp4=/>/g;
		sztemp=trim(sztemp);
		if(sztemp != "")
		{
			sztemp=sztemp.replace(regexp1,"&amp;");
			sztemp=sztemp.replace(regexp2,"&quot;");
			sztemp=sztemp.replace(regexp3,"&lt;");
			sztemp=sztemp.replace(regexp4,"&gt;");
		}
		return sztemp; 
	}
	
	
/****************************************************************************************************
Function : ReplaceSpecialCharacters

Purpose  : A function to replace the special characters in the string to be formed as a part of an xml

Input    : String 
Output   : The replaced string
****************************************************************************************************/	
	
	function ReplaceSpecialCharacters(szInput)
	{	
		var sztemp=new String(szInput);
		var regexp1=/&/g;
		var regexp2=/\"/g;
		var regexp3=/</g;
		var regexp4=/>/g;
		var regexp5=/'/g;
		sztemp=trim(sztemp);
		if(sztemp != "")
		{
			sztemp=sztemp.replace(regexp1,"&amp;");
			sztemp=sztemp.replace(regexp2,"&quot;");
			sztemp=sztemp.replace(regexp3,"&lt;");
			sztemp=sztemp.replace(regexp4,"&gt;");
			sztemp=sztemp.replace(regexp5,"&apos;");
		}
		return sztemp;
	}


/**************************************************************************************
Function : replacewithregularexpression

Purpose  : A function that replaces all occurance of the searcgh string in the source 
			with the replacement string

Input    : String 
Output   : The replaced string
**************************************************************************************/	

	function replacewithregularexpression(szsource,szsearchstring,szreplacement)
	{
		var szRegExp="";
		var oRegExp;
		var szdst;
		szRegExp+=szsearchstring;
		oRegExp=new RegExp(szRegExp,"g");
		szsource=trim(szsource)
		if (szsource.length > 0)
		{
			szdst=szsource.replace(oRegExp,szreplacement);
		}
		return szdst; 
	}
/******************************************************************************************
/Name	:	Checkforspacesintextarea
'Purpose:	Check for spaces in every given number of characters chars
'Input  :	string to be checked for spaces and the maximum continues chars allowed
'Output :	None
'Return :	Bool. Yes if spaces are present false otherwise
'******************************************************************************************/
	function Checkforspacesintextarea(strData,intcharcount)
	{
		try
		{
			var strSubData;
			var iCounter;
			var Position;
			var Start;
			var loopCount;
			if (parseInt(intcharcount) <= 0) 
			{
				return false;
			}
			intcharcount = parseInt(intcharcount) + 1
			strSubData = new String();
			iCounter=0;
			loopCount=0;
			while(loopCount <= strData.length)
			{
				// Extract intcharcount number of characters
				strSubData = strData.substring(iCounter,iCounter+intcharcount);
				if (strSubData.length <= intcharcount-1)
					return true;
				// Check for spaces
				Position = strSubData.indexOf(" ",0);
				if(Position == -1)
				{
					return false;
				}
				else
				{
					iCounter = Position+1;
				}
				loopCount = loopCount+iCounter
			}
			return true;
		}
		catch(e)
		{
			// Handle Java Script Error
			TrapError(FORM_NAME,PAGE_NAME,e);
		}
	}
	
/******************************************************************************************
/Name	:	IsNegativeNumberOk
'Purpose:	function to validate the correctness of a negative number
'Input  :	number to be checked for 
'Output :	None
'Return :	Bool. 
'******************************************************************************************/
	function IsNegativeNumberOk(szNumber)
	{
		var szTemp=new String(szNumber);
		if(szTemp.indexOf("-",0)==-1) return true; 
		if(szTemp.indexOf("-",0)==0)
		{
			szTemp=szTemp.substring(1,szTemp.length);
			if(szTemp.indexOf("-",0) > 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		if(szTemp.indexOf("-",0) > 0)
		{
			return false;
		}
	}
	
/******************************************************************************************
/Name	:	IsNegativeNumber
'Purpose:	function to validate if the input number is negative
'Input  :	number to be checked for 
'Output :	None
'Return :	Bool. 
'******************************************************************************************/
	function IsNegativeNumber(szNumber)
	{
		var szTemp=new String(szNumber);
		if(szTemp.indexOf("-",0) == -1)return false;
		if(szTemp.indexOf("-",0) >= 0)return true;
	}

/******************************************************************************************
/Name	:	isNegativeFloat
'Purpose:	function to checl if the input number is a negative float
'Input  :	number to be checked for 
'Output :	None
'Return :	Bool. 
'******************************************************************************************/
	function isNegativeFloat(szInput) 
	{
		var ch =''; 
		var arrSplit;
		szInput=trim(szInput);
		arrSplit = szInput.split("."); 
		if (arrSplit.length > 2)return false; 
		else 
		{ 
			for (i=0; i < szInput.length; i++) 
			{ 
				ch = szInput.substring(i,i+1); 
				
				if ((ch < "0" || ch > "9"))
				{
					if(ch !=".")
					{
						if(ch !="-")
						{
							return false;
						}
					}
				}
			} 
		} 
		return true; 
	}
	

/******************************************************************************************
/Name	:	CheckNegativeNumbersAndDecimals
'Purpose:	function to check if the input number is a negative number and the decimal 
			portion is within the specified precision
'Input  :	Number to check, total length of the value, recision before decimal, Precision 
			after decimal, flag to indicate of its a negative number
'Output :	None
'Return :	Bool. 
'******************************************************************************************/
	function CheckNegativeNumbersAndDecimals(szValue,nLength,nPrecisionBefore,nPrecisionAfter,blIsNegative)
	{
		if (trim(szValue).length == 0) return false; 
		return isNegativeNumberInValidFormat(szValue,nLength,nPrecisionBefore,nPrecisionAfter,blIsNegative); 
		
	}
	
/******************************************************************************************
/Name	:	isNumberValidEx
'Purpose:	Extended function that encapsulates the complete number validation feature.
'Input  :	number to be checked for , totaL length of the value, precision before decimal, 
			precision after decimal.
'Output :	None
'Return :	Bool. 
'******************************************************************************************/
	function isNumberValidEx(szNumber,nTotalLength,nBefore,nAfter)
	{
		if (trim(szNumber).length == 0) return false; 
		if(isNegativeFloat(szNumber))
		{
			if(IsNegativeNumber(szNumber))
			{
				if (!IsNegativeNumberOk(szNumber))
				{
					return false;
				}
				else
				{
					return CheckNegativeNumbersAndDecimals(szNumber.substring(1,szNumber.length),nTotalLength,nBefore,nAfter,true);
				}
			}
			else
			{
				return CheckNegativeNumbersAndDecimals(szNumber,nTotalLength,nBefore,nAfter,false);					
			}
		}
		else
		{
			return false;
		}
	}
	
/******************************************************************************************
/Name	:	ValidateComboSelect
'Purpose:	Function to check if a combo entry has been selected or not.
'Input  :	combo to check for, alert message to be displayed.
'Output :	None
'Return :	None. 
'******************************************************************************************/
	function ValidateComboSelect(comboObject,msg)
	{		
		if( comboObject.selectedIndex == 0 )
		{
			alert(msg);
		}
	}
	
	/**********************************************************************
	DESCRIPTION: Validates that a string contains a valid currency format. 
		
	PARAMETERS:
	strValue - String to be tested for validity
		
	RETURNS:
	True if valid, otherwise false.
	************************************************************************/
	function validateCurrency( objCtrl, strMessage)  
	{			
		var objRegExp = /^([1-9]{1}[0-9]{0,}(\.[0-9]{1,2})?|[0-9]{1}(\.[0-9]{1,2})?|[1-9]{1}([0-9]{0,}|(\,[0-9]{1,}))*(\.[0-9]{1,2})?)$/;
		//var objRegExp = /^([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/;
 
		if(!objRegExp.test( trim(objCtrl.value)))
		{
			if(strMessage != '')
				alert(strMessage);
			else
				alert("Please enter valid amount.");
			objCtrl.select();
			return false;
		}
		return true;
	}
	/**********************************************************************
	DESCRIPTION: Validates that a string contains a valid currency format with + - sign. 
		
	PARAMETERS:
	strValue - String to be tested for validity
		
	RETURNS:
	True if valid, otherwise false.
	************************************************************************/
	function validatePositiveNagativeCurrency( objCtrl, strMessage,strSign)  
	{		
		if(strSign == '+'){
			var objRegExp = /^\+?([1-9]{1}[0-9]{0,}(\.[0-9]{1,2})?|[0-9]{1}(\.[0-9]{1,2})?|[1-9]{1}([0-9]{0,}|(\,[0-9]{1,}))*(\.[0-9]{1,2})?)$/;
		}
		else if(strSign == '-'){
			var objRegExp = /^\-([1-9]{1}[0-9]{0,}(\.[0-9]{1,2})?|[0-9]{1}(\.[0-9]{1,2})?|[1-9]{1}([0-9]{0,}|(\,[0-9]{1,}))*(\.[0-9]{1,2})?)$/;
			//var objRegExp = /^\-([1-9]{1}[0-9]{0,}(\.[0-9]{1})?|[0-9]{1}(\.[0-9]{1})?|[1-9]{1}([0-9]{0,}|(\,[0-9]{1,}))*(\.[0-9]{1})?)$/;
		}
		else if(strSign == '+-'){
			var objRegExp = /^(\+?|\-)([1-9]{1}[0-9]{0,}(\.[0-9]{1,2})?|[0-9]{1}(\.[0-9]{1,2})?|[1-9]{1}([0-9]{0,}|(\,[0-9]{1,}))*(\.[0-9]{1,2})?)$/;
			//var objRegExp = /^\-([1-9]{1}[0-9]{0,}(\.[0-9]{1})?|[0-9]{1}(\.[0-9]{1})?|[1-9]{1}([0-9]{0,}|(\,[0-9]{1,}))*(\.[0-9]{1})?)$/;
		}
		if(!objRegExp.test( GetAmountWithoutCommas(trim(objCtrl.value))))
		{
			if(strMessage != '')
				alert(strMessage);
			else
				alert(" Please enter valid amount.");				
			objCtrl.select();
			return false;
		}
		return true;
	}

	/**********************************************************************
	DESCRIPTION: Validates that a string contains a valid share format. 
		
	PARAMETERS:
	strValue - String to be tested for validity
		
	RETURNS:
	True if valid, otherwise false.
	************************************************************************/
	function validateShare(strValue)
	{
		var objRegExp = /^([1-9]{1}[0-9]{0,1}(\.[0-9]{1,2})?|[1]{1}[0]{1,2}(\.[0]{1,2})?|[0]{1}(\.[0-9]{1,2})?)$/;
		return objRegExp.test(trim(strValue));
	}
	
	/**********************************************************************
	DESCRIPTION: Returns the amount number without the commas, if any. 
		
	PARAMETERS:
	strValue - Amount string that may contain commas
		
	RETURNS:
	Amount string with commas removed
	************************************************************************/
	function GetAmountWithoutCommas(strValue)
	{
		return replace(strValue,",","")
	}
	
	/**********************************************************************
	DESCRIPTION: Function to check if the file exists. 
		
	PARAMETERS:
	strPath - path of the document chosen by the user.
		
	RETURNS:
	true/false
	************************************************************************/
	function fnDoesFileExist(strPath)
		{
			/// By Ravi on 25_06_2004
			//var objFSO = new ActiveXObject("Scripting.FileSystemObject");
			//return objFSO.FileExists(strPath);
			return true;
		}	
		
	/**********************************************************************
	DESCRIPTION: Function to format the currency on client side. 
		
	PARAMETERS:
	num - number to be formatted
		
	RETURNS:
	formatted number
	************************************************************************/
	function fnFormatCurrency_JS(num) 
	{
		num = num.toString().replace(/\$|\,/g,'');
		
		if(isNaN(num))
			num = "0";
		
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		
		if(cents<10)
			cents = "0" + cents;
			
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + num + '.' + cents);
	}

	/**********************************************************************
	DESCRIPTION: Function to check whether Enter button has click. 
		
	PARAMETERS:
	e - Key down Event
		
	RETURNS:
	boolean
	************************************************************************/

	function checkEnter(e)
	{ //e is event object passed from function invocation
		var characterCode //literal character code will be stored in this variable
	
		if(e && e.which)
		{ //if which property of event object is supported (NN4)
			e = e
			characterCode = e.which //character code is contained in NN4's which property
		}
		else
		{
			e = event
			characterCode = e.keyCode //character code is contained in IE's keyCode property
		}

		if(characterCode == 13)
		{ 
			//if generated character code is equal to ascii 13 (if enter key)
			return true
		}
		else
		{
			return false 
		}
	}
	
	
/**************************************************************************************
Function : compareDates
Purpose  : This function compares 2 given dates.
Input    : Form Name,Field Name,Alert Messsage 
Output   : The test result as a Boolean
**************************************************************************************/	

		function compareDates(formName,fieldName1,fieldName2,msg ) 
					{
						var inputStr1 = document.getElementById(fieldName1).value;
						var inputStr2 = document.getElementById(fieldName2).value;
						var delim1 = inputStr1.indexOf("/");
						var delim2 = inputStr1.lastIndexOf("/");
						var delim3 = inputStr2.indexOf("/");
						var delim4 = inputStr2.lastIndexOf("/");
						
						var day1 = parseInt(inputStr1.substring(0,delim1),10);
						var month1 = parseInt(inputStr1.substring(delim1 + 1,delim2),10);
						var year1 = parseInt(inputStr1.substring(delim2 + 1,inputStr1.length),10);
						
						var day2 = parseInt(inputStr2.substring(0,delim3),10);
						var month2 = parseInt(inputStr2.substring(delim3 + 1,delim4),10);
						var year2 = parseInt(inputStr2.substring(delim4 + 1,inputStr2.length),10);
						if (year1 < year2)
						{
							alert(msg);
							eval("document."+formName+"."+fieldName2+".focus()");
							return false;
						}
						else if (year1 == year2 && month2 > month1)
						{
							alert(msg);
							eval("document."+formName+"."+fieldName2+".focus()");
							return false;
						}
						else if (year1 == year2 && month1 == month2 && day2 > day1) 
						{
							alert(msg);
							eval("document."+formName+"."+fieldName2+".focus()");
							return false;
						}	
						return true
					}
					
/**************************************************************************************
Function : isDateLessThanEqualToday
Purpose  : This function checks whenther a given date is less then or equal to the current date
Input    : Form Name,Field Name,Alert Messsage, To Set Focus
Output   : The test result as a Boolean
**************************************************************************************/	
			
			function isDateLessThanEqualToday(formName,fieldName,msg, toSetFocus) 
					{
						var today = new Date();
						var todayyear = today.getFullYear();
						var todaymonth = today.getMonth();
						var todayday = today.getDate();
					
						var inputStr = document.getElementById(fieldName).value;
						
						var delim1 = inputStr.indexOf("/");
						var delim2 = inputStr.lastIndexOf("/");
						
						var month = parseInt(inputStr.substring(delim1 + 1,delim2),10)-1;
						var day = parseInt(inputStr.substring(0,delim1),10);
						var year = parseInt(inputStr.substring(delim2 + 1,inputStr.length),10);
							
						if (todayyear < year)
						{
							alert(msg);
							if(toSetFocus)
								eval("document."+formName+"."+fieldName+".focus()");
							return false;
						}
						else if (todayyear==year && month>todaymonth)
						{
							alert(msg);
							if(toSetFocus)
								eval("document."+formName+"."+fieldName+".focus()");
							return false;
						}
						else if (todayyear == year && todaymonth == month && day > todayday) 
						{
							alert(msg);
							if(toSetFocus)
								eval("document."+formName+"."+fieldName+".focus()");
							return false;
						}	
						return true;
					}
					
/**************************************************************************************
Function : CheckValue
Purpose  : This function checks whenther 
			the string contains only alphanumeric, space, single quote(') , hyphen (-) , 
			forward slash (/) or amphesend (&) or a dot (.) of the given textbox control
Input    : Form Name,Field Name,Alert Messsage, To Set Focus
Output   : The test result as a Boolean
**************************************************************************************/	
					
	function CheckValue(controlname)
	{
		if(document.getElementById(controlname).value != "")
		{
			if(!isAlphaNumericAndFewSpecialChar(document.getElementById(controlname).value))
			{
				alert('Invalid reference number.');
				return false;
			}
		}
	}
	
	
	// ### DIS ### - ALvEN - For HDB instrument number Search
	/**************************************************************************************
	Function : IsAlphaAndForwardSlash()

	Purpose  : A function that takes in a string and returns a bool which says whether
				the string contains only alphanumeric with hyphen or not.

	Input    : String 
	Output   : The test result as a Boolean
	**************************************************************************************/
	function IsAlphaAndForwardSlash(strValue)
	{
		re = new RegExp("[^A-Za-z/]", "i");
		return (!re.test(strValue));
	}
					
//**END COMMON VALIDATION FUNCTIONS**