/*
Purpose : Returns a copy of a string without leading and trailing spaces.
Parameters :
	1.	String that needs to be trim
Returns : String without leading and trailing spaces.
*/
function trim(st)
{
	if(st.length > 0)
	{
		re = / +$/g; 
		newval = st.replace(re,"")
		re = /^ +/g;
		newvala = newval.replace(re,"")
		return newvala;
	}
	return ""
}

/*
Purpose : To check whether the control is blank or not.
Parameters :
	1.	Object Refernce to the control.
Returns : Boolean Variable.
*/
function isBlank(cntrl)
{
	cntrl.value = trim(cntrl.value);
	if (cntrl.value=="")
		return true;
	else
		return false;
}

/*
Purpose : To check the length for textarea cntrl.
Parameters :
	1.	Object Refernce to the control.
	2.	Boolean value to insure that field is mandatory.
	3.	Numeric value for maxlength allowed.
	4.	String message for the control i.e., "Mailing address".
Returns : Boolean Variable.
*/
function isProperLength(cntrl,mandatory,maxlength,refmsg)
{
	if (isBlank(cntrl))
	{
		if (mandatory)
		{
			alert("Please enter " + refmsg + " !!!");
			return false;
		}		
	}
	else if(cntrl.value.length > maxlength)
	{
		alert("Please enter maximum of " + maxlength + " characters in " + refmsg);
		return false;
	}
	return true;
}

/*
Purpose : To check the whether the object contains numeric value or not.
Parameters :
	1.	Object Refernce to the control.
	2.	Boolean value to insure that field is mandatory.
	3.	String message for the control i.e., "Salary Expected".
Returns : Boolean Variable after throughing a proper message.
*/
function isNumeric(cntrl,mandatory,refmsg,zeroallowed)
{
	if (isBlank(cntrl))
	{
		if (mandatory)
		{
			alert("Please enter " + refmsg + " !!!");
			return false;
		}		
	}
	else
	{
		val = cntrl.value;
		if ((!zeroallowed) && (parseFloat(val)==0))
		{
			alert("Zero is not applicable for " + refmsg);
			return false;			
		}
		
		if(parseFloat(val,10)!=(val*1))
		{
			alert("Please enter only numeric value for " + refmsg);
			return false;
		}
	}
	return true;
}

/*
Purpose : To check the email structure.
Parameters :
	1.	Object Refernce to the control.
	2.	Boolean value to insure that field is mandatory.
Returns : Boolean Variable after throughing a proper message.
*/
function isEmail(cntrl,mandatory)
{
	if (isBlank(cntrl))
	{
		if (mandatory)
		{
			alert("Please enter E-mail Address !!!");
			return false;
		}
	}
	else
	{
		emailid = cntrl.value;
		if (emailid.indexOf(' ') > 0)
		{
			alert("Space is not allowed in Email Address !!!");
			return false;
		}
		if ((emailid.indexOf('@') == 0) || (emailid.indexOf('.') == 0))
		{
			alert("'.' or '@'  not allowed as first character in Email Address !!!");
			return false;		
		}
		if ((emailid.indexOf('@') == -1) || (emailid.indexOf('.') == -1))
		{
			alert("Please enter valid E-mail Address");
			return false;
		}
		var Dot= emailid.split('.');
		var Atr= emailid.split('@');
		if (Atr.length>2)
		{
			alert("Invalid Position Of '@' in E-mail Address");
			return false;
		}
		var AtPos=emailid.indexOf('@');
		var DotPos=emailid.indexOf('.');
		var DotLPos=emailid.lastIndexOf('.');

		if (Dot.length>2)
		{
			if (emailid.indexOf('@')>emailid.lastIndexOf('.'))
			{
				alert("Invalid Position Of '.' in E-mail Address");
				return false;
			}
			if (AtPos>DotPos) //---------- .@
			{
				if ((AtPos-DotPos)<2)
				{
					alert("Invalid Position Of '.' in E-mail Address");
					return false;
				}
			}
			else
			{
				if ((DotPos-AtPos)<2)
				{
					alert("Invalid Position Of '.' in E-mail Address");
					return false;
				}
			}
			if ((DotLPos-AtPos)<2)
			{
				alert("Invalid Position Of '.' in E-mail Address");
				return false;
			}
		}
		else
		{
			if (AtPos>DotPos) //---------- .@
			{
				if ((AtPos-DotPos)<2)
				{
					alert("Invalid Position Of '.' in E-mail Address");
					return false;
				}
			}
			else
			{
				if ((DotPos-AtPos)<2)
				{
					alert("Invalid Position Of '.' in E-mail Address");
					return false;
				}
			}				
		}
		if (DotLPos>(emailid.length-3))
		{
			alert("Invalid Length after '.'  !!!");
			return false;
		}
	}
	return true;
}
//---------------- checking for whos is string
//----- dash allowd, numbers, alpha
function isWhoisURL(nString)
{
	var mAlpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789"; nString=nString.toUpperCase(); for(i=0;i<nString.length;i++){if(mAlpha.indexOf(nString.charAt(i))==-1){return false;}}
	if (i==nString.length)return true;
}

