// CONTAINS COMMONLY USED FUNCTIONS

// Function to check the maximum and 0 number of characters for any field...  
// (Kapil - 20/11)
function fnCheckLength(strMsg,strVal,inMin,inMax)
{
	stItemValue = strVal.value;
	inItemLength = stItemValue.length;
	
	// Validate the control's minimum value.
	if (inMin != 0)
	{
		if (inItemLength < 1) 
		{
			alert(strMsg + " cannot be Blank!. Please retry.");
			strVal.focus();
			strVal.select();
			return(false);
		}
	}
	if  (inItemLength > inMax) 	// Validate the control's maximum value.
	{
		alert(strMsg + " cannot be of more than " + inMax + " characters. Please retry.");
		strVal.focus();
		strVal.select();
		return(false);
	}
	return(true);
}


// Function to check that only numbers are entered
// (Kapil - 20/11)
function fnCheckNumeric(strMsg,strVal,inCheckNeg)
{
	//alert(strVal);
	stItemValue = strVal.value;
	//alert(stItemValue);
	//stItemValue="432df"
		
	if (isNaN(stItemValue) == true)
	{
			alert(strMsg + " can only have numeric data!. Please retry.");
			strVal.focus();
			strVal.select();
			return(false);
	}
	
	if (inCheckNeg == 1)
	{
		if (!(fnCheckNeg(strMsg,strVal)))
			return (false);
	}
	
	return(true);

}


// Function to check that number is not negative
// (Kapil - 31/01/2001)
function fnCheckNeg(strMsg,strVal)
{
	stItemValue = strVal.value;
		
	if (parseInt(stItemValue) < 0)
	{
			alert(strMsg + " cannot be negative!. Please retry.");
			strVal.focus();
			strVal.select();
			return(false);
	}
	return(true);

}



function fnIsValidAlphaNumeric(strMsg,varCtrl)   // Not in use. But good for reference.       
{
	var a,b
	var Field
	Field = varCtrl
	
	//if (!Field) // Validate the Form element
	//{
	//	alert (" Element is not Defined !");
	//	return(-1);
	//}

	Field.value = fnTrim(Field); // remove spaces from both side of the string/number
    var length = Field.value.length;

    if (!fnIsCharacter(Field))
    {
    	alert("First letter should be an alphabet in " + strMsg)
    	return false;
    }	
    a ="0";	
    for (var inCounter = 0; inCounter <= (Field.value.length - 1); inCounter++)
    {
		var Validchar = Field.value.substring(inCounter,inCounter+1);  // Check one Character at a
							    // time
		var character = 0;  // Set character as invalid alphanumeric value
		
		if (((Validchar >= "A") && (Validchar <= "Z")) || ((Validchar >= "a") && (Validchar <= "z")) || ((Validchar >= "0") && (Validchar <= "9")) || (Validchar == "-") || (Validchar == "_") || (Validchar == "."))
		{		
       		character = 1; //valid character
       		b = a;
       		a = Validchar;

       		if ((a == b) && (a==".") && (b=="."))
       		{
       			alert("Please do not enter two dots (..) in " + strMsg);
       			return false;
       		}	
	  	}
		else
		{
		
			character = 0; //Invalid character
			alert("Must have valid characters in " + strMsg);
			
			return false;
		}
	  	
	  	
	    if (character == 0)// generate error message if not valid Character
				    // value
        {
			alert("Must have valid characters in " + strMsg);
			
			return false; //If not valid character
	    }  
	}
	
	return true; //If valid character
}

// End of fnIsValidAlphaNumeric


// Checks for existance of white spaces in any given string
function fnCheckWhiteSpaces(strMesg,varCtrl)
{
		var blnOk = true;		
		var stItemValue = varCtrl.value;
		var inItemLength = stItemValue.length;
		
		//alert (inItemLength);
		//alert (stItemValue);
		
		for (i = 0; i<=inItemLength; i++)
		{
			if (stItemValue.charAt(i) == " ")
			{ 
				blnOk = false
				break;
			}
			else
			{
				continue;
			}
		}
		if (blnOk == false)
		{
			alert('The value entered for ' + strMesg + ' contains spaces.' + strMesg + ' cannot contain spaces.');		
			varCtrl.focus();
			varCtrl.select();
			return(false);
			}
		else{
			return true;				
		}
}



// Function to remove whitespaces
function fnTrim(varCtrl)
{
	var Field
	Field = varCtrl
	if (!Field) // Validate the Form element
	{
		alert(" Function : Element is not Defined !");
		return(-1);
	}
	var y=Field.value.length;
	var retval=Field.value;
	var m=0;
	while(m < y) //Start removing white spaces from left side
	{  
		if ((retval.substring(0,1)==" ")||(retval.substring(0,1)=="	"))
        	{ 
			retval=Field.value.substring(m+1,y); //remove left most white char
        	} 
		else 
		{ 
			break;
		}
		m++;
  	}
	y=retval.length;
  	m=y;
  	while (m >= 0) //Start removing white spaces from right side
  	{  
		if ((retval.substring(m-1,m)==" ")||(retval.substring(m-1,m)=="	"))
     		{  
			retval=retval.substring(0,m-1); //remove right most white char
	        } 
		else 
		{ 
			break;  //first non - white character encountered while 
			       //traversing from right to left. so break the loop
		}
     		m--;
  	}
  	return retval;
}

function fnIsCharacter(varCtrl)          
{
    	var character
		var Validchar = varCtrl.value.substring(0,1)  
		if (((Validchar >= "A") && (Validchar <= "Z")) || ((Validchar >= "a") && (Validchar <= "z")))
		{
			return true // Valid character      
	  	}
		else
		{
			return false //Invalid character
		}
}


//CHECK FOR CORRECT E-MAIL ADDRESS
function fnIsValidEmail(varCtrl)
{	
	var flag=0;
	var str = varCtrl.value;
	var email_ok = "Y";
	var emaillength = varCtrl.value.length - 1;
	if (emaillength > 0)
	{
		for (i = 0; i<=emaillength; i++)
		{
			if (str.charAt(i) == "@")
			{
				if((emaillength-i) < 4)
				{
					alert("There should be at least 2 characters after @.");
					varCtrl.focus();
					varCtrl.select();
					return(false);
				}
			   flag=1;
			}
			if(flag==1)
			{
				if(str.charAt(i) == ".")
				{
					if((emaillength - i) < 2)
					{
						alert("There should at least be 2 characters after '.'");
						varCtrl.focus();
						varCtrl.select();
						return(false);
						
					}
				  flag=2;
				}
			}
		}
		if(flag!=2)
		  {
			alert("Please enter your correct E-MailID");
			varCtrl.focus();
			varCtrl.select();
			return(false);
		  }
	}else{
		alert("Please enter you E-MailID");
		varCtrl.focus();
		varCtrl.select();
		return(false);
	}
	return(true);
}
