﻿// JScript File contains the functions for String Validations
// creator : MFS_SKS
 
		//function to check the first character for white spaces or any special characters
		function checkFirstChar(value){
			var leftChar;
			var bag = new Array();
			bag = ["*","%","#","@","!","$","^","(",")","<",">"]
			leftChar = value.charAt(0);
			// checks for blank spaces
			if ((leftChar == " ") || (leftChar == "\n") || (leftChar == "\t") || (leftChar == "\r")){
				alert("First character cannot be a space");
				return true;
			} 
			
			//checks for special characters
			for(var i=0; i < bag.length ;i++){
				if(leftChar == bag[i]){
					alert("First character cannnot be a special character " + leftChar);
					return true;
				}
			}
			return false;
		}
		
		// function that checks for invalid characters such as < > in the entire string
		function checkInvalidCharacter(s){
			var i;
			for (i = 0; i < s.length; i++)
			{   
				// Check that current character for < >.
				var c = s.charAt(i);
				if((c == "<") || (c == ">") || (c == "!")){
					alert("You have entered invalid characters !, <, Or >");
					return true;
				} 
			}
			return false;
		}
		
		// function trims the Null string
        function trim(strText){ 
            return strText.replace(/^\s*|\s*$/g,"");
        } 
        
        // function that checks for invalid characters such as < > in the entire string
		function checkInvalidCharacters(inputText)
		{		    
		    if (inputText.match(/[<>!]/))  
            {
				alert("You have entered invalid characters !, <, or >");
				return true;				
			}
			return false;
		}
		/*
		   AddedBy:MFS_TD
		   Date: May 22 2008
		   Description: It gives error if the input string has special charcters like <,>,!,'&#'
		*/ 		
		function validateString(sender, args)
	        {   
	            var inputText = args.Value;  	   
	            if (inputText.match(/[<>!]/))  
                    {
				         args.IsValid = false;	
				         //sender.innerText =  "Invalid Characters (<,>,!)";
				         sender.innerHTML =  "Invalid Characters (<,>,!)";
				         return;			
			        }
		         if (inputText.match(/(&#)/))  
                    {
				         args.IsValid = false;	
				         //sender.innerText =  "Invalid Characters (&#)";
				         sender.innerHTML =  "Invalid Characters (&#)";
				         return;			
			        }	
		         if (inputText.length > 500)
		 	         {
				         args.IsValid = false;	
				         //sender.innerText =  "No of characters must be less than 500";
				         sender.innerHTML =  "No of characters must be less than 500";
				         return;			
			        }
			        args.IsValid= true;
			        return;
        			
	        }
	        /*
		       AddedBy:MFS_RK
		       Date: Aug 13 2008
		       Description: It validate for whole number.If decimal, negative is entered then it is invalid
		    */	        
            function IsNumber(sender, args)
            {
                var input = args.Value;
                try
                {
                    if(trim(input) == ""){ // do not require to validate for blank, if blank use required field validator
                    }
                    else {
                        var pattern = /^[0-9]+$/; 
                        if (input.match(pattern)) {
                            args.IsValid = true;                             
                        } 
                        else {                               
                               //sender.innerText =  "Enter Number only!";
                               sender.innerHTML =  "Enter Number only!";
                               args.IsValid = false;
                        }
                    }
                }
                catch (e)
                {
                }
             }
             
             function checkScriptTag(sender, args)
             {
                var input = args.Value;
                try
                {
                    if(trim(input) == "" || input == "http://"){ // do not require to validate for blank, if blank use required field validator
                        args.IsValid = true;
                        
                    }
                    else {
                           var pattern = /[<>!]/;
                           if (input.match(pattern)) {
                                //sender.innerText =  "Invalid Characters (<,>,!)";
                                sender.innerHTML =  "Invalid Characters (<,>,!)";
                                args.IsValid = false;
                           }
                           else {                                
                                args.IsValid = true;
                           }                             
                    
                    }
                }
                catch (e)
                {
                }
             }
             /*
                Created By:MFS_RK
                Date      :September 15 2008
                Purpose   :To Validate a URL entered in input textbox
                           In order to validate the prefix with http:// or https://
                           the commented line for pattern would work.
             */
             function validateURL(sender , args)
             {
                var input = args.Value;
                //var pattern = /([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?/;
                var pattern = /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,3}(([0-9]{1,3})?\/.*)?$/;
                //var pattern = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/;
                
                try
                {
                    if (input.match(pattern)) {
                        // do nothing as it is a valid url entry
                    }
                    else {
                        args.IsValid = false;
                    }
                    
                }
                catch (e)
                {
                    args.IsValid = false;
                }
             }            
             
             //created by MFS_RK, September 30 2008 to center popup window
		    function centeredWindow(url, popW, popH, features, returnWindowHandle) {
    	
	            if(!features)
	            {
		            features = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1";
	            }	
	            var w = 800, h = 600;
            		
	            if (document.all || document.layers) {
  		            w = screen.availWidth;
  		            h = screen.availHeight;
	            }

	            var leftPos = (w-popW)/2;
	            var topPos = (h-popH)/2;
	            features += ",width=" + popW + ",height=" + popH + ",top=" + topPos + ",left=" + leftPos;	
	            var link = window.open(url, "link", features);
	            try
	            {
	            link.focus();
	            }
	            catch(ex)
	            {
	            }
            	
	            if(returnWindowHandle)
	            {
		            return link;
	            }
            }
            
            //Created by MFS_RJ , October 10 to validateEmailAddress
            function validateEmailAddress(sender, args)
            {
                var input = args.Value;
                var pattern = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
                
                try
                {
                    if(input != "")
                    {
                        if (input.match(pattern)) {
                            // do nothing as it is a valid email address entry
                        }
                        else {
                                //sender.innerText = "The email address is not in correct format!"
                                sender.innerHTML = "The email address is not in correct format!"
                                args.IsValid = false;
                        }
                    }
                }
                catch (ex)
                {                    
                    sender.innerHTML = "The email address is not in correct format!"
                    args.IsValid = false;
                }
            }
            //created by MFS_RK, Date Nov 6 2008
            function isValidDate(mydate)
            {
                var input = mydate;
                
                var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
                
                var matchArray = input.match(datePat); // is the format ok?
                
                if (matchArray == null) {                    
                    return false;
                }
                
                month = matchArray[1]; 
                day = matchArray[3];
                year = matchArray[4];
                
                if (month < 1 || month > 12) {                     
                    return false;
                }
                if (day < 1 || day > 31) {                    
                    return false;
                }
                if ((month==4 || month==6 || month==9 || month==11) && day==31) {                    
                    return false
                }
                if (month == 2) { // check for february 29th
                    
                    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
                    
                    if (day>29 || (day==29 && !isleap)) {                        
                        return false;
                    }
                }
                
                return true;
            }
