/*******************************************************************************************
	Author					: Atul Phirke
	version					: 1.3 
	created Date			: 15/09/2007
	Last Modified By		: Abhijit Bhattacharya
	Last Modified Date		: 27/10/2009
	Explanation				: This Script is used for validations 
	version history         : 1.0 - created                    - Atul Phirke
	                          1.1 - added 'isCreditCardValid'  - Rohan K.
							  1.2 - added 'isValidLength'      - Rohan K.
							  1.3 - added 'isStringAllowSpace' - Rohan K.
 *******************************************************************************************/

var validator=new Validator();

function Validator()
{
	
	this.isEmpty=checkEmpty;
	this.isNumber=checkValidNumber;
	this.isNumberOnly=checkValidNumberOnly;
	this.isPhone=checkValidPhone;
	this.isPhone2=checkValidPhone2;
	this.isUrl=checkValidUrl;
	this.isEmail=checkValidEmail;
	this.isAlphaNumericString=checkValidAlphaNumericString;
	this.isAlphaOnly=checkValidAlphaString;
	this.isString=checkValidString;
	this.isStringAllowSpace=checkValidStringAllowSpace;
	this.isIpAddress=checkValidIpAddress;
	this.isImage=checkValidImage;
	this.isDate=checkValidDate;
	this.compareNumber=compareNumber;
	this.compareDate=compareDate;
	this.isCreditCardValid=Mod10;
	this.isValidLength=checkLength;
	this.allowOnlyNumeric =allowOnlyNumeric;
	this.allowNumericAndDot =allowNumericAndDot;
	this.allowNumericDotAndPlusMinus =allowNumericDotAndPlusMinus;

	
	/*******************************************************************************************
		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			:	checkValidAlphaString
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks weather given input is alphabet string or not.
									Ex	:	anmsoft
	*******************************************************************************************/
	
	function checkValidAlphaString(input)
	{
		var pattern=/^[a-zA-Z\s]+$/;
		
		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\s]+$/;
		
		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			:	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
												022 - 24465464
												(022)-2455644
	*******************************************************************************************/
	
	function checkValidPhone2(input)
	{
		var pattern=/^[0-9\s\+\-\(\)]+$/;
		
		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})$/;
		
		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][\w\.-]*[a-zA-Z0-9]*[0-9A-Za-z]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
		
		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			:	checkValidStringWithSpace
		Input				:	input : String 
		Return				:   boolean
		Scope				:   Private
		Description	:	It checks weather given input is string with or without space.
									Ex	:	Scott Thomas
	*******************************************************************************************/
    function checkValidStringAllowSpace(input)
	{
		var pattern=/^[a-zA-Z\s]+$/;
		
		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];
	}
	
	
	/*******************************************************************************************
		Function			:	Mod10
		Input				:	ccNumb : String 
		Return				:   boolean
		Scope				:   Private
		Description	        :	Validates the Credit Card No.
		Author              :   David Leppek :: https://www.azcode.com/Mod10
	*******************************************************************************************/
	
	/* 
	Basically, the alorithum takes each digit, from right to left and muliplies each second
	digit by two. If the multiple is two-digits long (i.e.: 6 * 2 = 12) the two digits of
	the multiple are then added together for a new number (1 + 2 = 3). You then add up the 
	string of numbers, both unaltered and new values and get a total sum. This sum is then
	divided by 10 and the remainder should be zero if it is a valid credit card. Hense the
	name Mod 10 or Modulus 10. */
	function Mod10(ccNumb) {  // v2.0
	var valid = "0123456789"  // Valid digits in a credit card number
	var len = ccNumb.length;  // The length of the submitted cc number
	var iCCN = parseInt(ccNumb);  // integer of ccNumb
	var sCCN = ccNumb.toString();  // string of ccNumb
	sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
	var iTotal = 0;  // integer total set at zero
	var bNum = true;  // by default assume it is a number
	var bResult = false;  // by default assume it is NOT a valid cc
	var temp;  // temp variable for parsing string
	var calc;  // used for calculation of each digit
	
	// Determine if the ccNumb is in fact all numbers
	for (var j=0; j<len; j++) {
	  temp = "" + sCCN.substring(j, j+1);
	  if (valid.indexOf(temp) == "-1"){bNum = false;}
	}
	
	// if it is NOT a number, you can either alert to the fact, or just pass a failure
	if(!bNum){
	  /*alert("Not a Number");*/bResult = false;
	}
	
	// Determine if it is the proper length 
	if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
	  bResult = false;
	} else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
	  if(len >= 15){  // 15 or 16 for Amex or V/MC
	    for(var i=len;i>0;i--){  // LOOP throught the digits of the card
	      calc = parseInt(iCCN) % 10;  // right most digit
	      calc = parseInt(calc);  // assure it is an integer
	      iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
	      i--;  // decrement the count - move to the next digit in the card
	      iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
	      calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
	      calc = calc *2;                                 // multiply the digit by two
	      // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
	      // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
	      switch(calc){
	        case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
	        case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
	        case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
	        case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
	        case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
	        default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
	      }                                               
	    iCCN = iCCN / 10;  // subtracts right most digit from ccNum
	    iTotal += calc;  // running total of the card number as we loop
	  }  // END OF LOOP
	  if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
	    bResult = true;  // This IS (or could be) a valid credit card number.
	  } else {
	    bResult = false;  // This could NOT be a valid credit card number
	    }
	  }
	}
	// change alert to on-page display or other indication as needed.
	/*if(bResult) {
	  alert("This IS a valid Credit Card Number!");
	}
	if(!bResult){
	  alert("Please Enter A Valid Credit Card Number!");
	}*/
	  return bResult; // Return the results
	}


	/*******************************************************************************************
		Function			:	checkLength
		Input				:	input : String,  validLength : int
		Return				:   boolean
		Scope				:   Private
		Description			:	returns true if the 'input' length 
								is equal to the number 'validLength' 
	*******************************************************************************************/
	function checkLength(input,validLength)
	{
		return (input.length == validLength)?true:false;
	}

	
	function allowOnlyNumeric(evt)
	{  
	   try{
	   var charCode = (evt.which) ? evt.which : event.keyCode
       if (charCode > 31 && (charCode < 46 || charCode==47 || charCode > 57))
           return false;
       }catch(e){
	   
	   }
        return true;
      
	}


	function allowNumericAndDot(evt)
	{  
	   try{
	   var charCode = (evt.which) ? evt.which : event.keyCode
       if (charCode > 31 && (charCode < 46 || charCode==47 || charCode > 57))
           return false;
       }catch(e){
	   
	   }
        return true;
      
	}

	function allowNumericDotAndPlusMinus(evt)
	{  
	   try{
	   var charCode = (evt.which) ? evt.which : event.keyCode
       if (charCode > 31 && (charCode < 43 || charCode==44 || charCode==47 || charCode > 57))
           return false;
       }catch(e){
	   
	   }
        return true;
      
	}

}

