// Removing unwanted whitespace before and after the string
function trim(str)
{
        var ichar, icount;
        var strValue = str;

         ichar = strValue.length - 1;
        icount = -1;
        while (strValue.charAt(ichar)==' ' && ichar > icount)
            --ichar;
        if (ichar!=(strValue.length-1))
            strValue = strValue.slice(0,ichar+1);
        ichar = 0;
        icount = strValue.length - 1;
        while (strValue.charAt(ichar)==' ' && ichar < icount)
            ++ichar;
        if (ichar!=0)
            strValue = strValue.slice(ichar,strValue.length);
        return strValue;
}

// Mandatory function -> To check if the field is empty or not
function nulValid(frm,elmtNam,msg)
{
	tmp = eval("document."+frm+"."+elmtNam+".value");
	if (trim(tmp) == "")
	{
		alert(msg);
		eval("document."+frm+"."+elmtNam+".focus()");
		return false;		
	}
	else
	{	
		return true;	
	}
}

// This function is to check if the user has entered alphabets only
function alphaOnly(frm,elmtNam,msg,bool)
{	
	if (bool == "1")	
	{
		tmp = eval("document."+frm+"."+elmtNam+".value");
		if (trim(tmp) == "")
		{
			alert("Enter the "+msg);
			eval("document."+frm+"."+elmtNam+".focus()");
			return false;		
		}
		else
		{	
			var arg=  /[^a-zA-Z. ]/g
			if (arg.test(trim(tmp))==true)
			{
				alert(msg+" Should Be Alphabet");
				eval("document."+frm+"."+elmtNam+".focus()");
				return false;
			}
			else
			{
				return true;
			}
		}
	}
	else
	{
		tmp = eval("document."+frm+"."+elmtNam+".value");
		if (trim(tmp) != "")
		{
			var arg=  /[^a-zA-Z. ]/g
			if (arg.test(trim(tmp))==true)
			{
				alert(msg+" Should Be Alphabet");
				eval("document."+frm+"."+elmtNam+".focus()");
				return false;
			}
			else
			{
				return true;
			}
		}		
	}
	return true;
}

// This is to check for number only
function numOnly(frm,elmtNam,msg,len,bool)
{
if (bool == "1")	
	{
		tmp = eval("document."+frm+"."+elmtNam+".value");		
		if (trim(tmp) == "")
		{			
			alert("Enter the "+msg);
			eval("document."+frm+"."+elmtNam+".focus()");
			return false;		
		}
		else
		{	
			var arg=  /[^0-9.]/g
			if (arg.test(trim(tmp))==true)
			{
				alert(msg+" Should Be Numeric");
				eval("document."+frm+"."+elmtNam+".focus()");
				return false;
			}
			else
			{
				if (tmp.length > len)
				{
					alert("Only "+len+" characters are allowed in the "+msg+" field");
					eval("document."+frm+"."+elmtNam+".focus()");
					return false;
				}
				if (tmp.length < len)
				{
						alert("Minimum of "+len+" characters should be there in the "+msg+" field");
						eval("document."+frm+"."+elmtNam+".focus()");
						return false;
				}
				if (tmp.length == len)
				{
					return true;
				}	
				return true;
			}
		}
	}
	else
	{
		tmp = eval("document."+frm+"."+elmtNam+".value");
		if (trim(tmp) != "")
		{
			var arg=  /[^0-9.]/g
			if (arg.test(trim(tmp))==true)
			{
				alert(msg+" Should Be Numeric");
				eval("document."+frm+"."+elmtNam+".focus()");
				return false;
			}
			else
			{
				if (tmp.length > len)
				{
					alert("Only "+len+" characters are allowed in the "+msg+" field");
					eval("document."+frm+"."+elmtNam+".focus()");
					return false;
				}
				if (tmp.length < len)
				{
						alert("Minimum of "+len+" characters should be there in the "+msg+" field");
						eval("document."+frm+"."+elmtNam+".focus()");
						return false;
				}
				if (tmp.length == len)
				{
					return true;
				}	
				return true;
			}
		}		
	}
	return true;
}
// This is also numeric only function without considering the length

function numOnly1(frm,elmtNam,msg,bool)
{
if (bool == "1")	
	{
		tmp = eval("document."+frm+"."+elmtNam+".value");		
		if (trim(tmp) == "")
		{			
			alert("Enter the "+msg);
			eval("document."+frm+"."+elmtNam+".focus()");
			return false;		
		}
		else
		{	
			var arg=  /[^0-9.]/g
			if (arg.test(trim(tmp))==true)
			{
				alert(msg+" Should Be Numeric");
				eval("document."+frm+"."+elmtNam+".focus()");
				return false;
			}
			else
			{								
				return true;
			}
		}
	}
	else
	{
		tmp = eval("document."+frm+"."+elmtNam+".value");
		if (trim(tmp) != "")
		{
			var arg=  /[^0-9.]/g
			if (arg.test(trim(tmp))==true)
			{
				alert(msg+" Should Be Numeric");
				eval("document."+frm+"."+elmtNam+".focus()");
				return false;
			}
			else
			{
				if (tmp.length > len)
				{
					alert("Only "+len+" characters are allowed in the "+msg+" field");
					eval("document."+frm+"."+elmtNam+".focus()");
					return false;
				}
				if (tmp.length < len)
				{
						alert("Minimum of "+len+" characters should be there in the "+msg+" field");
						eval("document."+frm+"."+elmtNam+".focus()");
						return false;
				}
				if (tmp.length == len)
				{
					return true;
				}	
				return true;
			}
		}		
	}
	return true;
}
//

// Email - Id validation
function isEmailAddr(frm,elmtNam,msg,bool)
{
	tmp = eval("document."+frm+"."+elmtNam+".value");
	email = trim(tmp);	
  if (bool == "1")
  {  
	  if (email == "")
	  {
		alert("Enter Your "+msg);
		eval("document."+frm+"."+elmtNam+".focus()");
		return false;		
	  }
	  else
	  {	
		  var oRegExp = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/g;
		  if (oRegExp.test(email))
   		  {
	 		return true;
   		  }
	  	  else
    	  {
  			alert("Invalid Mail-Id. eMail-Id should be as : yourName@yourdomain.com");
			eval("document."+frm+"."+elmtNam+".focus()");			
  			return false;
	   	  }
	  }
	  return true;
  }
  else
  {  	
	  if (email != "")
	  {
		  var oRegExp = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/g;
		  if (oRegExp.test(email))
   		  {
	 		return true;
   		  }
	  	  else
    	  {
		  	alert("Invalid MailId. eMailId should be YourName@Yourdomain.com");
			eval("document."+frm+"."+elmtNam+".focus()");			
  			return false;
	   	  }
	  }
   }	 
}