/*
 Author:
	RUS
 Created:
	04-JAN-2005
 Description:
	Common javascript validation functions are defined here
 Parameters:
	
 Usage:
 	include the script file using <script src=""> tag

 Modifications: 
	
*/

	/*
	  Purpose	 : function to validate selection box
				   function checks for the value of the currently selected item
				   if the item value is -1 error message string is displayed
	  Parameters :
				   pObjSelectionbox selection box object
				   pstErrorMessage  error message string to be displayed

	  Returns	 :
					boolean
					true if valid selection is selected in the drop down
					false if invalid selection is selected in the drop down
				
	*/
		
		function validateSelectBox2(pObjSelectionbox,pstErrorMessage)
		{
			
			if(pObjSelectionbox.options[pObjSelectionbox.selectedIndex].value==0)
			{
					
					return false;
			}
			return true;
		}
		function validateSelectBox(pObjSelectionbox)
		{
			
			if((pObjSelectionbox.options[pObjSelectionbox.selectedIndex].value==-1)||(pObjSelectionbox.options[pObjSelectionbox.selectedIndex].value==""))
			{
					
					return false;
			}
			return true;
		}
		function validateSelectBox1(pObjSelectionbox)
		{
			
			if(pObjSelectionbox.options[pObjSelectionbox.selectedIndex].value==0)
			{
					
					return false;
			}
			return true;
		}
		
	/*
	  Purpose	 : function to validate text box
	  Parameters :
				   pObjTextbox text box object
				   pstErrorMessage  error message string to be displayed

	  Returns	 :
					boolean
					true if not text box is not empty
					false if text box is empty
				
	*/
		function validateTextBox(pObjTextbox,pstErrorMessage)
		{
			
			if(trimchar(pObjTextbox.value)=="")
			{
					alert(pstErrorMessage);
					pObjTextbox.focus();
					return false;
			}
		
			return true;
		}
		
		
		
		/*
	  Purpose	 : function to validate text box
	  Parameters :
				   pObjTextbox text box object
				   pstErrorMessage  error message string to be displayed when textbox is empty
				   pstErrorLentghMessage  error message string to be displayed when textbox  length is greater than the specified value
				   pinMaxLength  maximum length for the textbox
	  Returns	 :
					boolean
					true if not text box is not empty or length is less than Maximum length of textbox
					false if text box is empty or length is greater than maximum length of textbox
				
	*/
		function validateTextBox1(pObjTextbox,pstErrorMessage,pstErrorLengthMessage,pinMaxLength)
		{
			if(trimchar(pObjTextbox.value)=="")
			{
					alert(pstErrorMessage);
					pObjTextbox.focus();
					return false;
			}
			else
			{
				if(trimchar(pObjTextbox.value).length > pinMaxLength)
					{
					
						alert(pstErrorLengthMessage);
						pObjTextbox.focus();
						return false;
					}
			}
		
			return true;
		}
			
	/*
	  Purpose	 : function to trim a string
	  Parameters :
				   
	  Returns	 :
					boolean
					true if valid selection is selected in the drop down
					false if invalid selection is selected in the drop down
				
	*/
		function trimchar(pstr)
		{
			var lenstr = pstr.length;
			for(var i = 0 ; pstr.charAt(i) == " "; i++);
			for(var j = pstr.length - 1; pstr.charAt(j)== " "; j--);
			j++;
			if (i > j)
				pstr = "";
			else
				pstr = pstr.substring(i,j);
			return pstr;
		}
			/*
	  Purpose	 : to check a string is null 
	  Parameters :
				   
					String
	  Returns	 :
				  true or false;
				  true if null else false
	*/
	function isNull(aStr)
	{
		var index;
		aStr=trimchar(aStr);
		for (index=0; index < aStr.length; index++)
			if (aStr.charAt(index) != ' ')
			return false;
		return true;
	}
	/*
	  Purpose	 : to check a maximum length of the string 
	  Parameters :
				   
					String
	  Returns	 :
				  true or false;
				  true if equal to maximum length  else false
	*/
	function isMaxLen(aStr,pinMaxLength)
	{
		if(trimchar(aStr)!="")
			{
				if(trimchar(aStr).length > pinMaxLength)
					{
					
					return false;
					}
			}
		else
		{
		return true;
		}	
	}

function isdefined(variable){
   return (typeof(variable) == "undefined")?  false: true;
}  
/*
====================================================================
Added:			2nd June, 2006 [DPI-GSM: BugFixing Section]
Name:			fileUploadExt()
Description: 	Return true/false 
Called From:	Admin Whole site
Parameters:		fieldname: eg.(document.formname.fieldname),  
				extAllowed: Extensions Allowed 
====================================================================
*/
function fileUploadExt(fieldname, extAllowed){

	msg = "We only accept (" + extAllowed + ") files";
//	strFileToUp1 = new String(window.document.ThisForm.image.value)
	strValue = fieldname.value;
	
	// if image box is empty, let it pass as it's an optional field.
	if(strValue.length == 0) return true;

	// if upload file is of type application type then upload it anyway.
	if(extAllowed == "any"){
		return true;
	}
		
	var sExt1 = parseInt(strValue.lastIndexOf("."));
	var stExt = "";
	if(sExt1 > 0){
		var stExt = strValue.substring(sExt1);
		stExt = stExt.toLowerCase();
	}

	if(stExt != ''){
		if(extAllowed.lastIndexOf(stExt) == -1){
			alert(msg);
			return false;
		}
	}
	return true;
}	
/*
-------------------------------------------------------------------------------------------------------------	
	Description	:	This functions validates a value against a validation pattern. If the pattern does not 
					matches the value it appends an error message to the error message string. This function 
					is to be used for form validation.
	Parameters 	:	
					pstrPtn -- validation pattern
					pstrVal -- value to be matched against the pattern 
					pstrMsg -- Constant for the error message 					
					pblnRqd	-- Required or not 						  
					pintMaxLen -- Maximum Length
-------------------------------------------------------------------------------------------------------------	
*/		
function ValidateField(pstrPtn, pstrVal, pstrMsg, pblnRqd, pintMaxLen)
	{
			
		var lblnVldFld = true 
		// check whether the field is mandatory
		if ( pblnRqd == true && pstrVal == "" )
		{
			lblnVldFld = false
		}
		// maxlength check
		if( pintMaxLen > -1 && pstrVal.length > pintMaxLen)
		{
			lblnVldFld = false	 		
		}
		// validate value against pattern
		if(!isNull(pstrVal)){
			if( eval(pstrPtn).test(pstrVal) == false )
			{
				lblnVldFld = false
			
			}
		}					
		// generate error message if invalid
		if ( lblnVldFld == false )
		{
			/*pstrErrMsg = eval(pstrMsg);*/
			alert(pstrMsg);
			return false;
		}
		return true;
	}