// Function Name - Parameters - Return Value - Funcationality or allowed fomat of value

// CheckFloatValue - Field,Label,ValueAfterFraction  - True/False - Checks Floating value with number of decimal places
// CheckLength - Field,Label,MinimumLength,MaximumLength - True/False - Checks Minimum and Maximum Length of field
// CheckOnlyAlpha - Field,Label - True/False - a-z,A-Z  
// CheckPhoneNumber - Field,Label - True/False - 0-9, '-' ','
// CheckForAlpha - Field,Label - True/False - a-z,A-Z,' '  '.'
// CheckForEmpty - Field,Label,True/False - True/False
// CheckWithCurrentDate - DateField,MonthField,YearField - True/False - True when the Date Greater than or equal to Current date
// CheckForNumeric - Field,Label, Allowed Length of Field - True/False - Allow only numeric of exact length length
// trim - FieldValue - FieldValue - Remove the extra space in value
// emailCheck - FieldValue - True/False - Check ffff@ff.co format of email id


// check float value with number of decimal places
function CheckFloatValue(objField,strLabel,intFractionLength)
{
	var intDotCount=0
	var intDotIndex=0
	var intValueAfterDot
	var intLength
	var intValue
	var intLoop
	var ch
	
	intValue=objField.value;

	intLength=intValue.length
	
	for(intLoop=0;intLoop<intLength;intLoop++)
	{
		ch= intValue.charAt(intLoop);
		if(!((ch>='0' && ch<='9') || (ch=='.')))
		{
			alert("Enter only Numeric values in "+strLabel)
			objField.focus();
			return false
		}
		if(ch=='.')
		{
			if(intDotCount<1)
			{
				intDotCount=intDotCount+1
				intDotIndex=intLoop
				intValueAfterDot=intValue.substring(intDotIndex,intLength)
				if(intFractionLength!="")
				{
					if(!(intValueAfterDot.length>intFractionLength))
					{
						alert("Only "+intFractionLength+" decimal places are allowed in "+strLabel)
						objField.focus();
						return false;
					}
				}
			}
			else
			{	
				alert("Float Value can have only one . in "+strLabel)
				objField.focus();
				return false;
			}
		}
	}
	return true
}


// Check Minimum and Maximum length of field
function CheckLength(objField,strLabel,intMinLen,intMaxLen)
{
	var strValue
	var intLength
	
	strValue=objField.value
	intLength=strValue.length
	if (intMinLen!="")
	{
		if(intLength<intMinLen)
		{
			alert("Minimum Length of "+strLabel+" must be "+intMinLen)
			objField.focus();
			return false;
		}
	}
	if (intMaxLen!="")
	{
		if(intLength>intMaxLen)
		{
			alert("Maximum Length allowed for "+strLabel+" is "+intMaxLen)
			objField.focus();
			return false;
		}
	}
	return true;
}

// Check for only alphabet
function CheckOnlyAlpha(objField,strLabel)
{
	// Allows only alphabets
	var strValue
	var intLength
	var Loop
	
	strValue	=	objField.value;
	intLength	=	strValue.length;

	for(Loop=0;Loop<intLength;Loop++)
	{
		ch	=	strValue.charAt(Loop)
		if(!((ch>="a" && ch<="z") || (ch>="A" && ch<="Z")))
		{
			alert("The Characters allowed in "+strLabel+" are A-Z, a-z")
			objField.focus();
			return false;
		}
	}	
	return true;
}


// Check Phonenumber and Fax Number
function CheckPhoneNumber(objField,strLabel)
{
	// check for numeric and the allowed special characters are '-',','
	var strValue
	var intLength
	var Loop
	
	strValue	=	objField.value;
	intLength	=	strValue.length;
	
	ch=strValue.charAt(0)
	if (!(ch>="0" && ch<="9"))
	{
		alert("The First charcter of "+strLabel+" must be number")
		objField.focus();
		return false
	}
	ch	=	strValue.charAt(intLength-1)
	if(!(ch>="0" && ch<="9"))
	{
		alert("The Last charcter of "+strLabel+" must be number")
		objField.focus();
		return false
	}
	for(Loop=1;Loop<intLength-1;Loop++)
	{
		ch	=	strValue.charAt(Loop)
		if(!((ch>="0" && ch<="9") || (ch==",") || (ch=="-")))
		{
			alert("The Characters allowed in "+strLabel+"are 0-9,-,','")
			objField.focus();
			return false;
		}
	}
	return true;	
}



// Check for Non numeric fields
function CheckForAlpha(objField,strLabel)
{
 
  // This funcation allow only a-z, A-Z,.," " 
	var strValue		
	var intLength
	var Loop
	
	strValue	=	objField.value;
	intLength	=	strValue.length;

	for(Loop=0;Loop<intLength;Loop++)
	{
		ch	=	strValue.charAt(Loop)
		if(!((ch>="a" && ch<="z") || (ch>="A" && ch<="Z") || (ch==" ") || (ch==".")))
		{
			alert("The Characters allowed in "+strLabel+" are A-Z, a-z, . ' '")
			objField.focus();
			return false;
		}
	}	
	return true;
}


//Check for empty
function CheckForEmpty(objField,strLabel,bMandatory)
{
	var Value;
	Value	=	objField.value;
	
	if (bMandatory==true)
	{
		if(Value=="")
		{
			alert("Please Enter the "+strLabel)
			objField.focus();
			return false;
		}
	}
	return true;
}

// Checking the date with current date
function CheckWithCurrentDate(objDate,objMonth,objYear)
{
	
	// receives the controls
	// Only the greaterthan or equal value is allowed not the lesser date
	// Checks the year, month and date separately	
	
	var strDate		=	objDate.value
	var strMonth	=	objMonth.value
	var strYear		=	objYear.value
	var sysDate
	var sysMonth
	var sysYear
	var CurrentDate	=	new Date();
	var bFlag				=	true;
	sysDate					=	CurrentDate.getDate();
	sysMonth				=	CurrentDate.getMonth()+1;
	sysYear					=	CurrentDate.getYear();
	
	// Check whether the year is less the current year or not
			if (strYear<sysYear)
			{
				bFlag	=	false;
				alert("Enter Year greater that currentYear");
				objYear.focus();
			}
	// check whether the month is less the current month or not		
			if(bFlag==true)
			{
				if(strMonth<sysMonth)
				{
					bFlag	=	false;
					alert("Enter month greater than current month")
					objMonth.focus();
				}
			}
	// check whether the date is less than the current date or not 
			if(bFlag==true)
			{
				if(strDate<sysDate)
				{
					bFlag	=	false;
					alert("Enter date greater than current date")
					objDate.focus();
				}
			}
			if(bFlag==true)
				return true;
			else
				return false;
	
}


//Checking numeric field
function CheckForNumeric(objField,strLabel,intExactSize)
{
	
	// Check for the numeric value
	// The exact length of the value is got as parameter
	// whent the exact size parameter is empty then the zero length is allowed
	
	var intLength;
	var intValue;
	var bFlag	=	true;
	var intLoop
	var ch
	intValue	=	objField.value;
	intLength	=	intValue.length;
	if (intExactSize!="")
	{
		if (intLength!=intExactSize)
		{
			alert("The Length of "+strLabel+" value must be "+intExactSize)
			objField.focus();
			return false;
		}
	}
	
	for (intLoop=0;intLoop<intLength;intLoop++)
	{
		ch	=	intValue.charAt(intLoop)
		if(!(ch>='0' && ch<='9'))
		{
			alert("Enter numbers in "+strLabel);
			objField.focus();
			return false;
		}
	}
	return true;
}

//Checking Null Value
function trim(inputString)
 {
   
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch				= retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue	=	retValue.substring(1, retValue.length);
      ch				=	retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}


//Email Validation

function emailCheck (emailStr)
  {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */

    var emailPat=/^(.+)@(.+)$/

/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
   var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"

/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
   var validChars="\[^\\s" + specialChars + "\]"

/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
   var quotedUser="(\"[^\"]*\")"

/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
   var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

/* The following string represents an atom (basically a series of
   non-special characters.) */
   var atom=validChars + '+'

/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
   var word="(" + atom + "|" + quotedUser + ")"

// The following pattern describes the structure of the user

   var userPat=new RegExp("^" + word + "(\\." + word + ")*$")

/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
   var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */

   var matchArray=emailStr.match(emailPat)
   if (matchArray==null) {
    /* Too many/few @'s or something; basically, this address doesn't
        even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}

var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The Email id doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
    return false
}


var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
 {
   // the address must end in a two letter or three letter word.
   alert("The address must end in a three-letter domain, or two letter country.")
   return false
 }

// Make sure there's a host name preceding the domain.
if (len<2)
 {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
 }

// If we've gotten this far, everything's valid!

return true;
}


	
