var strAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var strAlphaNew = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '-";
var strNumeric = "0123456789";
var strSpecialChar = "@#$%-.\"'()+:[]_";
var strInvalidChar = "=\"~";

/*************************************************************************
	function : trimtxt(formObj)
	Input Parameter : textbox object or string
	Output parameter : trimed text
	purpose : Trim text removes all the preceeding & trailing spaces  from the  text
	Remark  : This function takes either textbox object or string as input.

**************************************************************************/
function trimtxt(objForm)
{
	if (typeof objForm == 'object')
		var strTemp  = objForm.value;
	else
		var strTemp  = new String(objForm);

	if (strTemp.search(/^\s+/) != -1)
		strTemp = strTemp.substring(strTemp.search(/\S+/),strTemp.length);

	if (strTemp.search(/(\s+)$/) != -1)
		strTemp = strTemp.substring(0,strTemp.search(/(\s+)$/));

	return strTemp;
}

/**************************************************************************
function 			: isQuotes(objForm, strLabel)
Input Parameter		: textbox object, label of the text box.
Output parameter	: true/false
Purpose				: To validate the text to check the entry of "double quotes"
**************************************************************************/

function isQuotes(objForm, strLabel, iMsgId)
{
	if (typeof objForm == 'object')
	{
		strTextString  = objForm.value
	}
	else
		strTextString =  new String(objForm)
	
	var regObj = eval("/" + strSpecialChar.charAt(6) + "/")

	if ((regObj.test(strTextString)) && (arguments.length != 1))
	{
		switch(parseInt(iMsgId,10))
		{
			case 1:
				alert(strLabel + " cannot contain double quotes.");
				break;
			case 2:
				alert(strLabel + " can contain only alphabets, apostrophes, hyphens, spaces and dots.");
				break;	
		}

		objForm.select();
		objForm.focus();
	}

	return regObj.test(strTextString)
}
/**************************************************************************
function 			:	isName(objForm, strLabel)
Input Parameter		:	1. ObjForm - Element object
						2. strLabel - Text appearing in the alert message
Output parameter	:	True - Value in the text box contains valid characters 
							(Alphabets, Apostrophe, Hyphen, Space and Dot)
						False - Value in the text box contains invalid characters

Purpose				:	To validate whether the data inputed is valid characters i.e
						Alphabets, numbers, hyphen, underscores and period entered
**************************************************************************/

function isName(objForm, strLabel)
{
	var bRet = true;

	if (typeof objForm == 'object')
	{
		objForm.value = trimtxt(objForm.value)
		strTextString  = objForm.value
	}
	else
		strTextString =  new String(objForm)

	if (strTextString.length == 0)
		bRet = false;

	if (bRet)
	{
		var regObj = /[^\w\-\_\,\.\s]/

		for (var i=0; i < strTextString.length; i++)
		{
			strTestChar = strTextString.charAt(i);

			if (regObj.test(strTestChar))
			{
				bRet = false;
				break;
			}
		}
	}

	if (!bRet)
		setFocus(objForm, strLabel + " can contain only alphabets, numbers, hyphen, underscores and period.\nPlease enter a valid " +  strLabel + ".");

	return bRet;
}

/*************************************************************************
function			: setFocus(formObj)
Input Parameter		: object, optional error message
Output parameter	: None
Purpose				: Sets the focus and shows an error message if applicable
Remark				:
**************************************************************************/

function setFocus(objFormX, strErrMessageX)
{
	objFormX.select();
	objFormX.focus();
	if (arguments.length != 1)
		alert(strErrMessageX);

	return;
}

/*************************************************************************
function 			:	isEmail(objForm, strLabel)
Input Parameter		:	1. ObjForm - Element object
						2. strLabel - Text appearing in the alert message
Output parameter	:	true/false
Purpose				:	This function checks whether characters entered in text box
							is valid email address's part before '@' sign.
*************************************************************************/

function isEmail(objForm, strLabel,strErrorMessage)
{
	bRet = true;
	if (typeof objForm == 'object')
	{
		objForm.value = trimtxt(objForm.value)
		strTextString  = objForm.value
	}
	else
		strTextString =  new String(objForm)
	if (strTextString.length > 4) 
	  {
		strInfo = strTextString.substring(strTextString.length-4);
		if (strInfo.toUpperCase() == 'INFO')
		{
			strTextString = strTextString.substring(0,strTextString.length-1);
		}
	 }
	
	var objReg1 = eval("/[\\s]|(" + strSpecialChar.charAt(0) + strSpecialChar.charAt(5) + "*" + strSpecialChar.charAt(0) + ")|(\\" +  strSpecialChar.charAt(5) + "\\" +  
	strSpecialChar.charAt(5) + ")|(" + strSpecialChar.charAt(0) + "\\" + strSpecialChar.charAt(5) + ")|(\\" + strSpecialChar.charAt(5) + strSpecialChar.charAt(0) + ")|(^\\" + strSpecialChar.charAt(5) + ")/")
	var objReg2 = eval("/^" + strSpecialChar.charAt(5) + "+\\" + strSpecialChar.charAt(0) + "(\\" + strSpecialChar.charAt(12) + "?)[" +  strAlpha + strNumeric + "\\" + strSpecialChar.charAt(4) + "\\" + strSpecialChar.charAt(5) + "]+\\" + strSpecialChar.charAt(5) + "([" + strAlpha + "]{" +  strNumeric.charAt(2) + "," + strNumeric.charAt(3) + "}|[" + strNumeric + "]{" + strNumeric.charAt(1) + "," + strNumeric.charAt(3) + "})(\\" + strSpecialChar.charAt(13) + "?)$/")
	var objReg3 = eval("/["+ strAlpha + strNumeric + "\\" + strSpecialChar.charAt(5)+ "\\" + strSpecialChar.charAt(4) + strSpecialChar.charAt(14) +"]/")
	if (!objReg1.test(strTextString) && objReg2.test(strTextString))
	{
		var arrTemp = strTextString.split('@')
		for (var i=0; i < arrTemp[0].length; i++)
		{
			strTestChar = arrTemp[0].charAt(i);
			if(strTestChar == "'")
			{
				if (typeof objForm == 'object')
				{
					if(strErrorMessage!=null)
					{
						setFocus(objForm,strErrorMessage);
					}
					else
					{
						setFocus(objForm, "Please enter a valid " + strLabel + ".");
					}
				}
				else
				{
					if(strErrorMessage!=null)
					{
						alert(strErrorMessage);
					}
					else
					{
						alert("Please enter a valid " + strLabel + ".")
					}
				}
				return false;
				break;
			}
			if (!objReg3.test(strTestChar))
			{
				bRet = false;
				break;
			}
		}
		bRet = true;
	}
	else
		bRet = false;
	
	if (!bRet)			
	{
		if (typeof objForm == 'object')
		{
			if(strErrorMessage!=null)
			{
				setFocus(objForm, strErrorMessage);
			}
			else
			{
				setFocus(objForm, "Please enter a valid " + strLabel + ".");
			}
		}
		else
		{
			if(strErrorMessage!=null)
			{
				alert(strErrorMessage);
			}
			else
			{
				alert("Please enter a valid " + strLabel + ".")
			}
		}
			
	}
		return bRet;
}
/*************************************************************************
function 			:	CheckLength(objForm, iLength, strLabel)
Input Parameter		:	1. ObjForm - Element object
						2. strLabel - Text appearing in the alert message
Output parameter	:	true/false
*************************************************************************/

function checkLength(objForm, iLength, strLabel)
{
	var objFormVal = trimtxt(objForm.value)
	
	if (objFormVal != "")
	{
		if(objForm.value.length > iLength)
		{
			alert("The maximum characters that can be entered for " + strLabel + " is " + iLength + ".\nPlease limit the entry to " + iLength + " characters.");
			objForm.focus();
			return false;
		}
	}
	return true;
}


function isTelePhone(objForm, strLabel,strErrorMessage)
{
	bRet = true;

	if (typeof objForm == 'object')
	{
		objForm.value = trimtxt(objForm.value)
		strTextString  = objForm.value
	}
	else
		strTextString =  new String(objForm)

	if (strTextString.length == 0)
		bRet = false;

	if (bRet)
	{
		var regObj = eval("/[^" + strNumeric + "\\s" + strSpecialChar.charAt(8) + "\\" + 
		strSpecialChar.charAt(9) + "\\" + strSpecialChar.charAt(10) + "\\" + strSpecialChar.charAt(4) + "\\,]/g")

		for (var i=0; i < strTextString.length; i++)
		{
			strTestChar = strTextString.charAt(i);

			if (regObj.test(strTestChar))
			{
				bRet = false;
				break;
			}
		}
	}

	if (!bRet)
	{
		if(strErrorMessage!=null)
		{
			setFocus(objForm,strErrorMessage);
		}
		else
		{
			setFocus(objForm, "Please enter a valid " + strLabel + ".");
		}		
	}
	return bRet;
}

function isAlpha(objForm, strLabel,strAllowQuote,strErrorMessage)
{
	bRet = true;

	if (typeof objForm == 'object')
	{
		objForm.value = trimtxt(objForm.value)
		strTextString  = objForm.value
	}
	else
		strTextString =  new String(objForm)

	if (strTextString.length == 0)
		bRet = false;
	else
	{
		if (strAllowQuote == '1')
		{
			var regObj = eval("/[^" + strAlphaNew + "]/")
		}
		else
		{
			var regObj = eval("/[^" + strAlpha + "]/")
		}
		
		if (regObj.test(strTextString))
			bRet = false;
	}	

	if (!bRet)
	{
		if(strErrorMessage!=null)
		{
			setFocus(objForm, strErrorMessage);
		}
		else
		{
			setFocus(objForm, strLabel + " can contain only alphabets and space.");
		}	
	}

	return bRet
}

/***********************************************************************
function 9		:	isBlank(formObj)
return value	:	true - text box is blank (contant zero length value or
					contents white spaces.
					false - if text box value is not zero length value or
					is non white space value
Purpose			:	To check if text box is left blank or has only white spaces.
Remark			:	Function will return false if value contains leading and trailing
					space.
***********************************************************************/
function isBlank(objForm, strLabel,strErrorMessage)
{
	if (typeof objForm == 'object')
	{
		objForm.value = trimtxt(objForm.value)
		strTextString  = objForm.value
		iValLength = strTextString.length
	}
	else
		strTextString =  new String(objForm)

	if ((arguments.length != 1) && (iValLength == 0))
	{
		if(strErrorMessage!=null)
		{
			setFocus(objForm,strErrorMessage);
		}
		else
		{
			setFocus(objForm, strLabel + " is required.\nPlease enter a " + strLabel + ".");
		}
	}

	return (iValLength == 0)
}

//Function to check the Presence of #,>,< and & .//
/*function isSearchField(objForm, strLabel,strMode)
{ 
 if (typeof objForm == 'object')
 { 
  strTextString  = objForm.value
 }
 else
 { 
  strTextString =  new String(objForm)
 }
  var regObj ='';

if(strMode=='1')
	 regObj = eval("/#|<|>/")
else
	regObj = eval("/&|#|<|>/")

 if ((regObj.test(strTextString)) && (arguments.length != 1))
 { 
  if(strMode=='1')
	 alert(strLabel + " cannot contain '<,>' or '#'");
else
	 alert(strLabel + " cannot contain '&,<,>' or '#'");
  
  objForm.select();
  objForm.focus();
 }
 return !regObj.test(strTextString)
}*/
//Function For Verifying the IP Address.
//Added by Sasi.
/*function verifyIP(IPvalue) 
		{
			errorString = "";
			theName = "FTP Server IP";
			var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
			var ipArray = IPvalue.match(ipPattern); 
			
			if (IPvalue == "0.0.0.0")
				errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
			else if (IPvalue == "255.255.255.255")
				errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
			if (ipArray == null)
				errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
			else {
			for (i = 0; i <= 4; i++) {
				thisSegment = ipArray[i];
				if (thisSegment > 255 || thisSegment < 0) {
					errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
					i = 4;
				}
				if ((i == 0) && (thisSegment > 255)) {
					errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
					i = 4;
				}
			}
		}
		extensionLength = 3;
		if (errorString == "")
			return true;
		else{
			alert (errorString);
			document.forms[0].txtFTPAddress.select();
			return false;
		}
	}*/
	
function isAlphaNumeric(objForm, strLabel)
{
	var bRet = true;
	
	if (typeof objForm == 'object')
	{
		objForm.value = trimtxt(objForm.value)
		strTextString  = objForm.value
	}
	else
		strTextString =  new String(objForm)

	
	if (bRet)
	{	
		strRegExpString = strAlpha + strNumeric + ",-."
		var regObj = eval("/[^" + strRegExpString + "]/")
		if (regObj.test(strTextString))
		{
			bRet = false;
		}
	}

	if (!bRet)
	{
		setFocus(objForm, strLabel + " can contain only alphabets and numbers.\nPlease enter a valid " +  strLabel + ".");
	}
	
	return bRet;
}


