// This File contains some common javascript functions 
// used in form validation 
//************ Function to check for a blank field *********

var frmName;			// form name 
var fieldName;			// form field name

function chkBlank(arg1,arg2)
{
	var retVal;
	//frmName = arg1;
	//frmName		= eval(arg1+".name");
	arrFields	= arg2.split(",")
	//alert(arrFields.length);
	//alert(eval(arg1+"."+arrFields[1]+".value"));
	for (i=0;i<=arrFields.length-1;)
	{
		//if(eval("document."+frmName+"."+arrFields[i]+".value")=="")
		if(eval(arg1+"."+arrFields[i]+".value")=="")	
		{
			alert("The "+arrFields[i+1]+" field  can not be left blank.")
			//eval("document."+frmName+"."+arrFields[i]+".focus()");
			eval(arg1+"."+arrFields[i]+".focus()")
			retVal	= false;
//			return false;
			break;
		}
		i	= i+ 2;
		retVal	= true;
	}
	return retVal;
}

//********** Function to check for valid Email ***************

// Valid mail id must contain atleast one '@' and '.' 
// Valid mail id can contain any of the chars.
// Two @ are allowed,adjacent '@' and '.' are not allowed ,Two adjacent '.' are not allowed
// '@' or '.' as first character is not allowed.
// Space is not allowed
// Logic used is to check for valid chars and indexes using javascript function indexOf().

//************************************************************

function validEmail(arg1,arg2)
{
	frmName		= eval(arg1+".name");
	fieldName	= eval(arg2+".name");
	var strEmail;						// email id entered by the user
	var intindexOfDot;					// index of "." in the mail id
	var intindexOfAt;					// index of "@" in the mail id
	var chkChar;						// first character in the mail id 
	var chkLastChar;					// last character in the mail id
	var strStatus;						// string variable used as a flag
	var invalidChars;			
	strStatus		= "invalid";
	strEmail		= eval(arg2+".value")
	invalidChars	= "@.";
	intindexOfDot	=	strEmail.indexOf(".")
	intindexOfAt	=	strEmail.indexOf("@")
	intindexOfSpace	=	strEmail.indexOf(" ")
	chkChar			=	strEmail.charAt(0)
	chkLastChar		=	strEmail.charAt(strEmail.length-1)
	if(invalidChars.indexOf(chkChar)==-1)
	{	
		if(invalidChars.indexOf(chkLastChar)==-1)
		{	
			if ((intindexOfAt != -1) && (intindexOfDot!= -1)&&(intindexOfSpace == -1))
			{
				if((intindexOfDot)-(strEmail.indexOf(".",intindexOfDot+1))!=-1)
				{				
					if(strEmail.indexOf("@",intindexOfAt+1)==-1)
					{
						if((parseInt(intindexOfAt)-parseInt(intindexOfDot))!= -1)
						{
							strStatus	= "valid";
						}
					}
				}	
			}
		}	
	}
	if(strStatus == 'invalid')
	{
		alert("Email Id entered by You is Invalid");
		eval("document."+frmName+"."+fieldName+".value=''");
		eval("document."+frmName+"."+fieldName+".focus()");
		return false;	
	}
	else
	return true;
}

//************ Function to check for valid numeric values *****

//The logic used is to just check that whether the entered value is a number by using 
//javascript function isNaN.

//*************************************************************
function validNumeric(arg1,arg2)
{
	frmName		= eval(arg1+".name");
	fieldName	= eval(arg2+".name");
	var numValue;			// value of the numeric field to be checked
	numValue	= eval(arg2+".value")
	if(isNaN(numValue))
	{
		alert("This Field contains numeric values only!");
		eval("document."+frmName+"."+fieldName+".value=''");
		eval("document."+frmName+"."+fieldName+".focus()");
		return;
	}
}

//*********** Function to check for valid Telephone Number ******

// Valid Telephone number can contain any digit from 0-9,+,-.
// Rest of the characters are not allowed in the telephone number
// Two adjacent + are not allowed similarly Two adjacent - are not allowed
// + followed by - or vice versa is not allowed
// The Logic used is to check for valid chars,check the indexes of + and - if any using
// javascript function indexOf().

//***************************************************************

function validTelephone(arg1,arg2)
{
	frmName		= eval(arg1+".name");
	fieldName	= eval(arg2+".name");
	var numTel;						// value of the TElephone field to be checked
	var validChars;					// valid characters a telephone number can contain
	var indexofPlus;
	var indexofDash;
	numTel	= eval(arg2+".value")
	validChars	= "0123456789+-";
	if((numTel.indexOf("+")!=-1)||(numTel.indexOf("-")!=-1))
	{
		indexofPlus	= numTel.indexOf("+");
		indexofDash	= numTel.indexOf("-");
		if(Math.abs(indexofPlus-indexofDash)!=1)
		{
			if((Math.abs(indexofPlus-(numTel.indexOf("+",indexofPlus+1)))==1)||(Math.abs(indexofDash-(numTel.indexOf("-",indexofDash+1)))==1))
			{
				alert("Please enter a valid Telephone number");	
				eval("document."+frmName+"."+fieldName+".focus()");
				return false;
			}
		}
		else
		{
			alert("Please enter a valid Telephone number");	
			eval("document."+frmName+"."+fieldName+".focus()");
			return false;
		}
	}
	for(i=0;i<numTel.length;i++)
	{
		var numChar	= numTel.charAt(i)
		if(validChars.indexOf(numChar)==-1)
		{
			alert("Please enter a valid Telephone Number.");
			eval("document."+frmName+"."+fieldName+".focus()");
			return false;
			break;
		}
	}
	return true;
}
//**************** Function to check for valid Text*********

function validText(arg1,arg2)
{
	frmName		= eval(arg1+".name");
	fieldName	= eval(arg2+".name");
	var fieldValue;
	fieldValue	= eval("document."+frmName+"."+fieldName+".value");
	if(isNaN(fieldValue)!= true)	
	{
		alert("The value you entered is not valid Text.");
		eval("document."+frmName+"."+fieldName+".value=''");
//		eval("document."+frmName+"."+fieldName+".focus()");
		return false;
	}
}
