
/*
 The functions in this validation file are
 1. Trim		--  removes spaces from left & right
 2. LTrim		--	remove spaces from left
 3. RTrim		--	remove spaces from right
 4. isEmpty		--  checks whether the control is blank or not
 5. isNumber	--	can have numbers, decimal & negative
 6. isPosNumber	--	can have numbers, decimal & no negative
 7. isNegNumber	--	can have numbers, decimal & must be negative
 8. isDigit		--	can have numbers & negative, no decimal
 9. isPosDigit	--	can have only numbers
10. isNegDigit	--	can have numbers and must be -ve
11. isDate		--  checks for a valid US date ( mm/dd/yy[yy])
12. isTime		--  checks for valid time ( hh:mm {AM/PM} )
13. isDateTime	--	checks for DateTime  ( mm/dd/yy[yy] hh:mm {AM/PM} )
14. isZip		--	checks for US Zip ( 99999 or 99999-9999 )
15. isSSN		--  checks for SS# ( 999-99-9999 )
16. isEmail		--	validates Email ( name@domain.{com/net/edu/org})
18. isIP		--  validates IP address ( 999.999.999.999 )
17. isAlpha		--	checks for alphabetic characters ( a-z & A-Z )
18. isAlphaNum	--	checks for alpha numeric characters ( a-z & A-Z & 0-9)
19. isNumeric	--	checks for numeric characters ( 0-9 ) like isPosDigit


// all the functions use regular expressions for validations
// as regular expressions are faster
*/



//This function removes all the spaces in the front and end
function Trim(sValue)
{
	var ichar, icount;
	ichar = sValue.length - 1;
	icount = -1;
	// finding out how spaces at the end
	while( sValue.charAt(ichar) == ' ' && ichar > icount )
		--ichar;
	// if there are spaces then removing them
	if( ichar != (sValue.length - 1))
		sValue = sValue.slice(0, ichar+1);
	
	ichar = 0;
	icount = sValue.length - 1;
	// finding out the spaces at the begging
	while( sValue.charAt(ichar) == ' ' && ichar < icount )
		++ichar;
	// if there are spaces at front then removing them
	if(ichar != 0 )
		sValue = sValue.slice(ichar, sValue.length);	
	return sValue;
}

//This function removes all the spaces in the left
function LTrim(sValue)
{
	var ichar, icount;
	ichar = 0;
	icount = sValue.length - 1;
	// finding out the spaces at the begging
	while( sValue.charAt(ichar) == ' ' && ichar < icount )
		++ichar;
	// if there are spaces at front then removing them
	if(ichar != 0 )
		sValue = sValue.slice(ichar, sValue.length);	
	return sValue;
}

//This function removes all the spaces in the Right
function RTrim(sValue)
{
	var ichar, icount;
	ichar = sValue.length - 1;
	icount = -1;
	// finding out how spaces at the end or Right
	while( sValue.charAt(ichar) == ' ' && ichar > icount )
		--ichar;
	// if there are spaces then removing them
	if( ichar != (sValue.length - 1))
		sValue = sValue.slice(0, ichar+1);

	return sValue;
}

// checks wheter the text field is empty or not
function isEmpty(item)
{
	var sValue = Trim( item.value );
	if (sValue.length == 0 )
		return false;
	return true;
}

// checks whether the text entered is a number or not
// a number can has digits, a decimal and - ( if negative ) at beginning
// and cannot have period at the end and no spaces or commas in between
function isNumber(item, required)
{
	var sValue = Trim( item.value );

	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//can have one or more numbers and can have period and cannot have period at end
	if ( !(/^-*[\d\.]+\d$/.test(sValue)) )
		return false;

	// checking if there are more than one period
	if( sValue.indexOf(".") != sValue.lastIndexOf("."))
		return false;
	
	// checking if there are more than one '-'
	if( sValue.indexOf("-") != sValue.lastIndexOf("-"))
		return false;

	return true;
}

// checks whether the text entered is a positive number or not
// a number can has digits, a decimal and no -ve  and cannot have
// period at the end
function isPosNumber(item, required)
{
	var sValue = Trim( item.value );

	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//can have one or more numbers and can have period and cannot have period at end
	if ( !(/^[\d\.]+$/.test(sValue)) )
		return false;

	// checking if there are more than one period
	if( sValue.indexOf(".") != sValue.lastIndexOf("."))
		return false;
	
	return true;
}

// checks whether the text entered is a negative number or not
// a number can has digits, a decimal and must have -ve at beginning
// and cannot have period at end
function isNegNumber(item, required)
{
	var sValue = Trim( item.value );

	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//can have a '-' and  one or more numbers and can have period and cannot have period at end
	if ( !(/^-[\d\.]*$/.test(sValue)) )
		return false;

	// checking if there are more than one period
	if( sValue.indexOf(".") != sValue.lastIndexOf("."))
		return false;
	
	return true;
}

// checks whether the text entered is a whole number or not
// a number can has digits and - ( if negative ) at beginning
function isDigit(item, required)
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	// this means there can be zero or more '-' and one or more decimal numbers
	if ( !(/^-*[\d]+$/.test(sValue)) )
		return false;

	// checking if there are more than one '-'
	if( sValue.indexOf("-") != sValue.lastIndexOf("-"))
		return false;

	return true;
}

// checks whether the text entered is a positive whole number or not
// a number can only have digits
function isPosDigit(item, required)
{
	var sValue = Trim( item.value );

	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}
	
	//means one or more numbers
	if ( !(/^\d+$/.test(sValue)) )
		return false;

	return true;
}

// checks whether the text entered is a whole number or not
// a number can has digits and must have -ve  at beginning
function isNegDigit(item, required)
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}
	
	//means a '-' and one or more numbers
	if ( !(/^-\d+$/.test(sValue)) )
		return false;

	return true;
}

//checks for a valid date or not
// it should be an American Date  mm/dd/yy only and no time
function isDate(item, required)
{
	var sValue = Trim( item.value );

	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}
	
	// checking if entered in proper way using Reg Exp
	if (!(/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(sValue)))
		return false;
	
	// getting the day, month and year into variables so as to check for a valid date

	var idd, imm, iyy;
	var sDate = sValue.split("/")
	
	if( sDate[2].length == 3 )		//entered 3 digit year
		return false;
	
	//getting the day, month & year into different variables
	imm = eval(sDate[0]);
	idd = eval(sDate[1]);
	iyy = eval(sDate[2]);
	

	// if the user entered 2 digit year then checking whether it belongs to 2000 or 1900
	if( sDate[2].length == 2 )
	{
		if( iyy > 50 ) 
			iyy += 1900;
		else
			iyy += 2000;
	}

	//checking for a valid month
	if( imm < 1 || imm > 12 )
		return false;

	// days should be from 1 to 31
	if( idd < 1 || idd > 31 )
		return false;
	
	//checking for months with 30 days
	if( imm == 4 || imm == 6 || imm == 9 || imm == 11)
	{
		if( idd == 31 )
			return false;
	}
	else
	{
		// checking for Feburary
		if( imm == 2 )
		{
			// for February checking for leap year
			if( (iyy % 4) == 0 ) 
			{
				if( iyy % 100 == 0)
				{
					if( iyy % 400 == 0 )
					{
						if ( idd > 29 )
							return false;
					}
					else
					{
						if( idd > 28 )
							return false;
					}
				}
				else
				{
					if ( idd > 29 )
						return false;
				}
			}
			else
			{
				if( idd > 28 )
					return false;
			}
		}
	}
	
	return true;
}

//checks for a valid Time or not
// it should be an in the following pattern hh:mm {AM/PM}
function isTime( item, required )
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//checking whether the user entered in a proper way
	if (!(/^\d{1,2}\:\d{2}\ [AaPp][Mm]$/.test(sValue)))
		return false;
	
	//getting the hours & min into variables
	sValue = sValue.slice( 0, sValue.length - 3 );
	var sTime = sValue.split(":");
	var sHour, sMin;
	sHour = eval( sTime[0] );
	sMin = eval( sTime[1] );
	//checking for hour
	if( sHour < 1 || sHour > 12 )
		return false;
	//checking for minutes
	if( sMin < 0 || sMin > 59 )
		return false;
	
	return true;
}

//checks for a valid DateTime or not
// it should be an in the following pattern MM/DD/YY[YY] hh:mm {AM/PM}
function isDateTime( item, required )
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//checking whether the user entered in a proper way
	if (!(/^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}\:\d{2}\ [AaPp][Mm]$/.test(sValue)))
			return false;

	// getting date & time seperately from Date Time and validating them seperately
	var iSpaceLoc = sValue.indexOf(' ');
	// declaing two objects which holds Date & Time, this is done because
	// they will be passed to isDate & isTime function to validate the
	// Date & Time as both the functions take Objects as input
	var oDate = new Object; 
	oDate.value = sValue.slice(0, iSpaceLoc);
	var oTime = new Object; 
	oTime.value = sValue.slice(iSpaceLoc+1, sValue.length);

	//checking for valid date using isDate function
	if( !isDate(oDate, true) )
		return false;
	// checking for valid Time using isTime function
	if( !isTime(oTime, true) )
		return false;
		
	return true;
}

//checks for a valid US Zip 
// it can be a 5 digits(99999) 0r 5-4(99999-9999) pattern
function isZip( item, required )
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//checking whether the user entered in a proper way
	if ( !(/^\d{5}$/.test(sValue) || /^\d{5}-\d{4}$/.test(sValue)) )
		return false;
		
	return true;
}

//checks for a valid Social Security Number 
// it should of the pattern 999-99-9999
function isSSN( item, required )
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//checking whether the user entered in a proper way
	if ( !(/^\d{3}-\d{2}-\d{4}$/.test(sValue)) )
		return false;
		
	return true;
}

//checks for a valid Email address
// name@domain.com
// the following assumtions are made
// 1) the name should be atleast 2 characters can have letters, periods or underscore
//    should start with a character and cannot have underscore at the end
// 2) the domain also should have 2 characters should have character or periods
//    it should start and end with only character
function isEmail( item, required )
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//checking whether the user entered in a proper way
	if ( !(/^[a-zA-Z][\w\.]*[a-zA-Z0-9]@[a-zA-Z][a-zA-Z\.]*[a-zA-Z]\.(com|COM|net|NET|org|ORG|edu|EDU)$/.test(sValue)) )
		return false;
		
	return true;
}

// checks whether the user valid IP Address
// user can only enter in the following order 999.999.999.999
function isIP( item, required )
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//checking whether the user entered alphabets
	if ( !(/^\d{3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(sValue)) )
		return false;
		
	return true;
}

// checks whether the user entered only alphabets
// user can only enter a-z & A-Z
function isAlpha( item, required )
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//checking whether the user entered alphabets
	if ( !(/^[a-zA-Z,'./!@#$%^&*()\[\]`~ {}?\|+=:;"<> ]+$/.test(sValue)) )
		return false;
		
	return true;
}

// checks whether the user entered alphabets numeric characters
// user can only enter a-z & A-Z & 0-9
function isAlphaNum( item, required )
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//checking whether the user entered alphabets numeric characters
	if ( !(/^[a-zA-Z0-9,'./!@#$%^&*()\[\]`~ {}?\|+=:;"<> ]+$/.test(sValue)) )
		return false;
		
	return true;
}

// checks whether the user entered numeric characters
// user can only enter 0-9 and no period
// this is similar to isPosDigit
function isNumeric( item, required )
{
	var sValue = Trim( item.value );
	
	if (!isEmpty(item))
	{
		if (required)
			return false; // return error it is required
		else
			return true;  // return if the item is empty
	}

	//checking whether the user entered numbers
	if ( !(/^\d+$/.test(sValue)) )
		return false;
		
	return true;
}

