/*******************************************************************************************
	Author					: Atul Phirke
	version					: 1.0 
	created Date			: 15/09/2007
	Last Modified By		: 
	Last Modified Date		: 
	Explanation				: This Script is used for validations 
 *******************************************************************************************/

var validator=new Validator();

function Validator()
{
	
	this.isEmpty=checkEmpty;
	this.isNumber=checkValidNumber;
	this.isNumberOnly=checkValidNumberOnly;
	this.isPhone=checkValidPhone;
	this.isUrl=checkValidUrl;
	this.isEmail=checkValidEmail;
	this.isAlphaNumericString=checkValidAlphaNumericString;
	this.isString=checkValidString;
	this.isIpAddress=checkValidIpAddress;
	this.isImage=checkValidImage;
	this.isDate=checkValidDate;
	this.compareNumber=compareNumber;
	this.compareDate=compareDate;
	this.checkTextAreaLength=limitTexLength;


	
	/*******************************************************************************************
		Function			:	checkEmpty
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	If String is blank then it returns true otherwise returns false
	*******************************************************************************************/
	function checkEmpty(input)
	{
		

		return input==""?true:false;

	}


	/*******************************************************************************************
		Function			:	checkValidNumber
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks weather input value is number or not.
									Ex:	12.50 ,1000
	*******************************************************************************************/
	function checkValidNumber(input)
	{
		var pattern=/^([0-9]+([.]?[0-9]+)?){1}$/;
		
		return pattern.test(input);

	}
	
	
	/*******************************************************************************************
		Function			:	checkValidNumberOnly
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks weather input value is pure number or not.
									Ex:	1000
	*******************************************************************************************/
	function checkValidNumberOnly(input)
	{
		var pattern=/^[0-9]+$/;
		
		return pattern.test(input);

	}
	
	/*******************************************************************************************
		Function			:	checkValidAlphaNumericString
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks weather given input is alphanumeric string or not.
									Ex	:	anmsoft123 or 12323
	*******************************************************************************************/
	
	function checkValidAlphaNumericString(input)
	{
		var pattern=/^[a-zA-Z0-9]+$/;
		
		return pattern.test(input);

	}
	

	/*******************************************************************************************
		Function			:	checkValidPhone
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks weather phone number is in specified format.
									Ex	:	+123 
												+(123)4567
												123
												(123)123123
	*******************************************************************************************/
	
	function checkValidPhone(input)
	{
		var pattern=/^[+]?([0-9]+|([(]{1}[0-9]+[)]{1}[0-9]+)|([0-9]+[-]{1}[0-9]+))$/;
		
		return pattern.test(input);

	}

	
	/*******************************************************************************************
		Function			:	checkValidUrl
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	 It checks weather input url is in proper format.
									Ex	:	www.ontra.com
		Note				:   other pattern="^((ht|f)tp(s?)\:\/\/|~/|/)?(([a-zA-Z]){3,})([.]{1}[a-zA-Z0-9]{2,}[-]?[a-zA-Z0-9]+)([.]{1}[a-zA-Z0-9]{2,3}){1,2}$";
									For  ftp://www.yahoo.co.in		http://www.ontra.com			 ftps://www.ontra.co.in
	*******************************************************************************************/
	function checkValidUrl(input)
	{
		//var pattern=/^(([a-zA-Z]){3,})([.]{1}[a-zA-Z0-9]{2,}[-]?[a-zA-Z0-9]+)([.]{1}[a-zA-Z0-9]{2,3})$/;
		var pattern=/^(.*)[.](.*)$/;
		return pattern.test(input);

	}
	
	/*******************************************************************************************
		Function			:	checkValidEmail
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	 It checks weather email is in proper format.
									Ex	: a@b.com
		
	*******************************************************************************************/
	function checkValidEmail(input)
	{
		//var pattern=/^([a-zA-Z0-9]+)([_.]?[a-zA-Z0-9]+)*@([a-zA-Z0-9_]+)([.][a-zA-Z0-9]{2,3}){1,2}$/;
		//var pattern=/^([a-zA-Z0-9]+)([_.]?[a-zA-Z0-9]+)*@([a-zA-Z0-9_]+)([.][a-zA-Z0-9]{2,10}){1,10}$/; it will not allow -char
		//var pattern=/^([a-zA-Z0-9]+)([_.]?[a-zA-Z0-9-]+)*@([a-zA-Z0-9_-]+)([.][a-zA-Z0-9]{2,10}){1,10}$/;
		var pattern=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		return pattern.test(input);

	}

	
	/*******************************************************************************************
		Function			:	checkValidString
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks weather given input is string or not.
									Ex	:	anmsof
	*******************************************************************************************/
	
	function checkValidString(input)
	{
		var pattern=/^[a-zA-Z]+$/;
		
		return pattern.test(input);

	}
	


	/*******************************************************************************************
		Function			:	checkValidIpAddress
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks weather given input is valid IP Address or not.
									Ex	:	192.168.57.14
	*******************************************************************************************/
	
	function  checkValidIpAddress(input)
	{
		var pattern=/^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/;
		
		return pattern.test(input);

	}
	
	
	/*******************************************************************************************
		Function			:	checkValidImage
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks weather given input is valid image file.
									Ex	:	extension of file may be jpg, bmp, gif ..........
	*******************************************************************************************/
	function  checkValidImage(input)
	{
		var pattern=/^(jpeg|gif|bmp|dib|jpg|jre|jfif||tif|fiff|png|ico)$/;
		var extension=input.substring(input.lastIndexOf(".")+1);
		return pattern.test(extension);

	}

	

	/*******************************************************************************************
		Function			:	checkValidDate
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks weather given input is valid date in mm-dd-yyyy format.
									Ex	:	01-10-2007
	*******************************************************************************************/
	
	function checkValidDate(input)
	{
		
		var pattern       =/^[0-9]{2}[-][0-9]{2}[-][0-9]{4}$/;
		
		return (pattern.test(input) && checkValidDateObject(input));
	}

	/*******************************************************************************************
		Function			:	compareNumber
		Input				:	input1 : String  , input2 : String 
		Return				:   -1 (if input1 < input2) , 0 (if input1 = input2) , 1 (if input1 > input2)
		Scope				:   Private
		Description	:	It compare two numbers.
		Note				:   Pass valid number string . i.e. before calling this function use isNumber Function
	*******************************************************************************************/

	function compareNumber(input1,input2)
	{
			var val1=parseInt(input1);
			var val2=parseInt(input2);

			if(val1 < val2)
				return -1;
			else if(val1>val2)
				return 1; 
			else
				return 0; // for both number are same
	
	}

	
	/*******************************************************************************************
		Function			:	compareDate
		Input				:	input1 : String  , input2 : String 
		Return				:   -1 (if input1 < input2) , 0 (if input1 = input2) , 1 (if input1 > input2)
		Scope				:   Private
		Description	:	It compare two dates.
		Note				:   Pass valid number string . i.e. before calling this function use isDate Function
	*******************************************************************************************/

	function compareDate(input1,input2)
	{
			var date_array = input1.split('-');

			 var day1 =date_array[0];
		
			var month1 =date_array[1];
		
			var year1=date_array[2];
		
			var date_array1 = input2.split('-');

			 var day2 =date_array1[0];
		
			var month2 =date_array1[1];
		
			var year2=date_array1[2];
			

				if(year1<year2)
					return -1;
				else if(year1>year2)
					return 1;
				else
				{
				
					if(month1<month2)
						return -1;
					else if(month1>month2)
						return 1;
					else
					{
						if(day1<day2)
							return -1;
						else if(day1>day2)
							return 1;
						else
							return 0;
					}
			
				}
    }

	
	/*******************************************************************************************
		Function			:	checkValidDateObject
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks for valid date.
	*******************************************************************************************/
	 function checkValidDateObject(input)
    {

		
		var date_array = input.split('-');

        var day =date_array[0];
		
        var month =date_array[1];
		
        var year=date_array[2];
		
		
        if (year < 1 || month < 1 || day < 1 || month > 12 || day > getMaxDays (month, year))
        {
            return false;
        }
		
        return true;
    }

	
	/*******************************************************************************************
		Function			:	isLeapYear
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	 It checks weather year is leap or not.
	*******************************************************************************************/
	
	 function isLeapYear (year)
    {
        return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? true : false);
    }




    /*******************************************************************************************
		Function			:	getMaxDays
		Input				:	month : String , year : String 
		Return				:   int
		Scope				:   Private
		Description	:	 It return maximum daty of the month.
	*******************************************************************************************/
	function getMaxDays (month, year)
    {
		var dayInFeb=isLeapYear (year) ? '29' : '28';
        var daysInMonth = new Array('31', dayInFeb , '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');

        return daysInMonth[month - 1];
	}
//to limit text Entered for textArea
	function limitTexLength(el,length){
		if (el.value.length > length){
			el.value = el.value.substr(0,length)
			return false;
		}
		return true;
	} 
}


