﻿
/*******************************************************************
Created By: Dharminder Singh
Created Date: 07 Sep 2007

* All the validations are checked on IE and Mozrilla Firefox 

* A <Validation.CSS> Style Sheet is used for displaying the style of 
  validation Message 
  To change  'style of validation message' or 'Images of successful and error'
  we can change into style sheeet  

* Every function of javascript file we used two input parameters >...
  First Parameter is 'object' of any element
  Second parameter is 'Id' of any element 
  Basically Second Parameter is used for displaying error message into any label
  control or span
  
* for check validtaion on onkeyup ,onblur  events of any control we use validation method 
  such as e.g. onblur="OnlyInteger(this,'integerstatus')"
   here  'this' is element object and 'integerstatus' is span Id
   
* To check validations on any control button ,firstly you set onkeyup property of every object 
   so the all validation error message display on the page 
   e.g.   var obj1=document.getElementById('<%=txtNumeric.ClientID%>');
          var obj2=document.getElementById('<%=txtfirstname.ClientID%>');
          obj1.onkeyup();
          obj2.onkeyup();
          if (OnlyInteger(obj1,'integerstatus')&&OnlyNumeric(obj7,'NumericStatus'))
          {return true}else{return false}}
          
* To display error style of textbox,Please add CssClass <inboxSuccess> to every input Types 
  e.g. 	<asp:TextBox CssClass =" inboxSuccess" runat ="server"  id="txtpassword" ></asp:TextBox>

  
*******************************************************************/
// JScript File
/*******************************************************************
Function to Check for Empty value 
           Output: function give error message return false if input is not valid
                    else it return successful message
Here em >>Any textbox object
     id >>Any span Id or label Id for Display Validation Message  
********************************************************************/
function IsEmpty(em,id)
{
    var obj2 = document.getElementById(id);
    if( em.value.trim().length==0 )
        {
            setErrorColor(em);
            setError(obj2,"Required");
            return false;
        }
    else
        {
            setSuccessColor(em);
            setSuccess(obj2);
            return true;
        }
  }
function IsAvailable(em,id)
{
    var obj2 = document.getElementById(id);
      //if( em.value.trim().length==0 )
       // {
       //  ido.innerHTML="<span class=\"success_msg\"></span>";
       //  o.className="success";
        //    setErrorColor(em);
        //    setError(obj2,em.value + " is not available");
        //    return false;
      // }
   // else
    //{
            setErrorColor(em);
            setError(obj2,em.value + " is not available");
            return false;
     
  }
  
  function InvalidUserName(em,id)
{
     var obj2 = document.getElementById(id);   
     setErrorColor(em);
     setError(obj2, "inavlidUsername");
     return false;         
  }
    function userIsLocked(em,id)
{
     var obj2 = document.getElementById(id);   
     setErrorColor(em);
     setError(obj2, " userIsLocked ");
     return false;         
  }
  
  function InvalidPassword(em,id)
{
     var obj2 = document.getElementById(id);   
     setErrorColor(em);
     setError(obj2, "inavlidPassword");
     return false;         
  }
 function  emailAlreadyExist(em,id)
  {
      var obj2 = document.getElementById(id);
      setErrorColor(em);
      setError(obj2,"E-mail already exists");
      return false;            
  }
  
function NotAvailable(em,id)
{
    var obj2 = document.getElementById(id);
      //if( em.value.trim().length==0 )
       // {
        setSuccessColor(em);
        setSuccessIsAvailable(obj2,em);
        return true;
        //}
    //else
   // {
     //   setSuccessColor(em);
      //  setSuccess(obj2);
       // return true;
   // }
  }
  
/*****************************************************************
Function  Check for valid Email or not 
          Output: function give error message return false if input is not valid
                    else it return successful message
Here emailID >>Any textbox object
          id >>Any span Id or label Id for Display Validation Message  
******************************************************************/
function ValidateEmailAddress(emailID,id)
 {
    var obj2 = document.getElementById(id);
   if(emailID.value!='' &&  checkMail(emailID.value))
    {
       setSuccessColor(emailID);
       setSuccess(obj2);
       return true;
     }
   else
     {
         if(emailID.value.trim().length==0)
           {
                  setErrorColor(emailID);
                 setError(obj2,"Required");
                 return false;
                }
               else
                {
                 setErrorColor(emailID);
                setError(obj2,("Not Valid"));
                return false;
                }
                return true;
                
             }
}

/*****************************************************************
Function  Check for selecting item in DropDown list 
          Output: function give error message return false if input is not valid
                    else it return successful message
Here em >>Any DropDown object
     id >>Any span Id or label Id for Display Validation Message
*******************************************************************/  
 function ValidateDropDown(em,id)
       {
         var obj2= document.getElementById(id);
           if (em.selectedIndex == 0||em.value =='00'||em.value == ''||em.value=='-- Please Select --'||em.value=='All') 
           {
              setErrorColor(em);
              setError(obj2,"Required");
              return false;              
           }
           else
           {
                 setSuccessColor(em);
                setSuccess(obj2);
                window.focus();
                return true;
           }
       }

/******************************************************************
Function  Check for  password value in DropDown list
          Output: function give error message return false if input is not valid
                    else it return successful message 
Here em >>Any textbox object
     id >>Any span Id or label Id for Display Validation Message  
here password length is minimum 5 character
********************************************************************/
function checkFirstPassword(em,id)
{
        var obj2 = document.getElementById(id);
        if(em.value.trim().length==0)
        {
         setErrorColor(em);
        setError(obj2,"Required");
        return false;
        }
        else
        {
            if(em.value.trim().length<5)
            {
             setErrorColor(em);
            setError(obj2,"Minimum is 5 characters");
            return false;
            }
            else
            {
            setSuccessColor(em);
            setSuccess(obj2);
            return true;
            }
        }
} 
/*********************************************************************************************
Function  Check for match password value of two elements 
          Output: function give error message return false if input is not valid
                  else it return successful message
Here em1 >>first password textbox object
     em2 >>second password textbox object
     id1 >>Any span Id or label Id for Display Validation Message for first password textbox
     id2 >>Any span Id or label Id for Display Validation Message for second password textbox   
here password length is minimum 5 character
***********************************************************************************************/
function checkPassword(em1,em2,id1,id2)
{

    var em =document.getElementById(em2);
    checkFirstPassword(em,id1);
    var obj1 = document.getElementById(id1); 
    var obj3 = document.getElementById(id2); 
    if(em1.value.trim().length==0)
    {
        setErrorColor(em1);
        setError(obj3,"Required");
        return false;
    }
    else
    {
      
        if(em1.value.trim().length<5)
        {
            setErrorColor(em1);
            setError(obj3,"Minimum is 5 characters");
            return false;
        }
    }
    if(em.value==em1.value)
    {
        setSuccessColor(em);
        setSuccessColor(em1);
        setSuccess(obj1);
        setSuccess(obj3);
        return true;
    }
    else
    {
         setErrorColor(em);
        setError(obj3,"Passwords do not match");
        return false;
    }
}


/********************************************************************
Function  Check for  integer value  
          Output: function give error message return false if input is not valid
                    else it return successful message
Here em >>Any textbox object
     id >>Any span Id or label Id for Display Validation Message  
*********************************************************************/
function OnlyInteger(em,id)
{
    var obj1 = document.getElementById(id);
     var strValidChars = "0123456789"; 
               var strChar;
               var blnResult = true;
                if(em.value.trim().length==0)
                {
                    setErrorColor(em);
                    setError(obj1,"Only Numeric");
                    return false;
                }
                else
                {
               for (i = 0; i < em.value.length && blnResult == true; i++)
                { 
                 strChar = em.value.charAt(i);
                  if (strValidChars.indexOf(strChar) == -1)
                    {
                       
                       blnResult = false; 
                     }
                }
               if(blnResult==false)
                {
                 setErrorColor(em);
                 setError(obj1,"Not numeric Value");
                 return true;
                }
                else
                {
                    setSuccessColor(em);
                    setSuccess(obj1); 
                    return true;
                  }
                  }
  }
/******************************************************************   
 Function  Check for Numeric value
           Output: function give error message return false if input is not valid
                    else it return successful message
 Here em >>Any textbox object
      id >>Any span Id or label Id for Display Validation Message  
*******************************************************************/
function OnlyNumeric(em,id)
{
    var obj1 = document.getElementById(id);
     var strValidChars = "0123456789."; 
               var strChar;
               var blnResult = true;
             if(em.value.trim().length==0)
                {
                    setErrorColor(em);
                    setError(obj1,"Numeric Value");
                    return false;
                }
                else
                {
                   for (i = 0; i < em.value.length && blnResult == true; i++)
                    { 
                     strChar = em.value.charAt(i);
                      if (strValidChars.indexOf(strChar) == -1)
                        {
                           blnResult = false; 
                         }
                    }
                   if(blnResult==false)
                    {
                     setErrorColor(em);
                     setError(obj1,"Value is not numeric");
                     return false;
                    }
                    else
                    {
                        setSuccessColor(em);
                        setSuccess(obj1); 
                        return true;
                    }
              }
  }
/*******************************************************************    
  Function  Check for  Alphabets value
            Output: function give error message return false if input is not valid
                    else it return successful message
  Here em >>Any textbox object
       id >>Any span Id or label Id for Display Validation Message 
********************************************************************/  
function OnlyAlphabets(em,id)
{
   
    var obj1 = document.getElementById(id);
     var strValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "; 
               var strChar;
               var blnResult = true;
             if(em.value.trim().length==0)
                {
                    setErrorColor(em);
                    setError(obj1,"Insert alphabetic value");
                    return false;
                }
                else
                {
                   for (i = 0; i < em.value.length && blnResult == true; i++)
                    { 
                     strChar = em.value.charAt(i);
                      if (strValidChars.indexOf(strChar) == -1)
                        {
                           blnResult = false; 
                         }
                    }
                   if(blnResult==false)
                    {
                     setErrorColor(em);
                     setError(obj1,"Value is not alphabetic");
                     return true;
                    }
                    else
                    {
                        setSuccessColor(em);
                        setSuccess(obj1); 
                        return true;
                    }
              }
  }   
/*******************************************************************         
  Function  Check for  AlphaNumeric value
            This function accepts s as any control element object and verifies the
            Alphanumeric value 
            Output: function give error message return false if input is not valid
                    else it return successful message
  Here em >>Any textbox object
       id >>Any span Id or label Id for Display Validation Message
********************************************************************/    
function OnlyAlphaNumeric(em,id)
{
   
    var obj1 = document.getElementById(id);
     var strValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .";
               var strChar;
               var blnResult = true;
             if(em.value.trim().length==0)
                {
                    setErrorColor(em);
                    setError(obj1,"Insert alphanumeric value");
                    return false;
                }
                else
                {
                   for (i = 0; i < em.value.length && blnResult == true; i++)
                    { 
                     strChar = em.value.charAt(i);
                      if (strValidChars.indexOf(strChar) == -1)
                        {
                           blnResult = false; 
                         }
                    }
                   if(blnResult==false)
                    {
                     setErrorColor(em);
                     setError(obj1,"Value is not alphanumeric");
                     
                     return false;
                    }
                    else
                    {
                        setSuccessColor(em);
                        setSuccess(obj1); 
                        return true;
                    }
              }
  }     
  /****************************************************************
   Function  Check for  Phone number value of USA like <123 456 456456>
             This function accepts s as any control element object and verifies the
              phone number.
              Output: function give error message return false if input is not valid
                    else it return successful message 
   Here em >>Any textbox object
        id >>Any span Id or label Id for Display Validation Message  
  ******************************************************************/
function checkphoneformat(s,id)
{
   
    var obj1 = document.getElementById(id);
     if(s.value.trim().length==0)
        {
    
            setErrorColor(s);
            setError(obj1,"Insert phone number");
            return false;
        }
	if(s.value.trim().length<12)
	{
	
		 setErrorColor(s);
		 setError(obj1,"111-111-1234");
		 return false;
	}
	if ((s.value.charAt(3)!="-")||(s.value.charAt(7)!="-"))
	{
	    
	  setErrorColor(s);
	 setError(obj1,"111-111-1234");
	  return false;
	}
	if ( isNaN(s.value.charAt(0))==true || isNaN(s.value.charAt(1))==true || isNaN(s.value.charAt(2))==true || isNaN(s.value.charAt(4))==true || isNaN(s.value.charAt(5))==true || isNaN(s.value.charAt(6))==true || isNaN(s.value.charAt(8))==true || isNaN(s.value.charAt(9))==true || isNaN(s.value.charAt(10))==true || isNaN(s.value.charAt(11))==true )
	{
	
		 setErrorColor(s);
	     setError(obj1,"111-111-1234");
	     return false;
	}
	   setSuccessColor(s);
	   setSuccess(obj1); 
       return true;
}

/****************************************************************
   Function  Check the File Extensions
              This function accepts s as any control element object and verifies the
              extension of file.
               Output: function give error message return false if input is not valid
                    else it return successful message
   Here s >>Any textbox object
        id >>Any span Id or label Id for Display Validation Message  
******************************************************************/

	function ValidExtension(s,id) 
	{
	    var obj1 = document.getElementById(id);
	    var item =s.value
	    if(item.trim().length==0)
        {
            setErrorColor(s);
            setError(obj1,"Insert  value");
            return false;
        }
		else if (item.indexOf ('.jpg', 0)>0 || item.indexOf ('.JPG', 0)>0)
		{
		   setSuccessColor(s);
		   setSuccess(obj1); 
		   return true;
		}
		else if (item.indexOf ('.jpeg', 0) >0 || item.indexOf ('.JPEG', 0) >0)
		{
		   setSuccessColor(s);
		   setSuccess(obj1); 
           return true;
      
		}
		else if (item.indexOf ('.png', 0) >0 || item.indexOf ('.PNG', 0) >0)
		{
		   setSuccessColor(s);
		   setSuccess(obj1); 
           return true;
       
		}
		
		else if (item.indexOf ('.gif', 0) >0 || item.indexOf ('.GIF', 0) >0)
		{
		   setSuccessColor(s);
		   setSuccess(obj1); 
           return true;
		}

		else
		{
		setErrorColor(s);
		setError(obj1,"Invalid file extension");
		return false;
		}
	}
/****************************************************************
   Function  Check the  date format <dd/mm/yyyy> 
             This function accepts a string variable and verifies if it is a
             proper date or not. It validates format matching either
             mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
             has the proper number of days, based on which month it is.
            Output: function give error message return false if input is not valid
                    else it return successful message     
   Here s >>Any textbox object
        id >>Any span Id or label Id for Display Validation Message  
******************************************************************/
    function isDate(s,id) 
	{
	    dateStr=s.value.trim();
	    var obj1 = document.getElementById(id);
	    var datePat = /^(\d{1,2})(\/|)(\d{1,2})(\/|)(\d{4})$/;
	    var matchArray = dateStr.match(datePat); // is the format ok?
		months= new Array(12);
		months[0]="Jan";
		months[1]="Feb";
		months[2]="Mar";
		months[3]="Apr";
		months[4]="May";
		months[5]="Jun";
		months[6]="Jul";
		months[7]="Aug";
		months[8]="Sep";
		months[9]="Oct";
		months[10]="Nov";
		months[11]="Dec";	
		 if(dateStr.length==0)
        {
            setErrorColor(s);
            setError(obj1,"Insert date");
            return false;
        }
	    if (matchArray == null) 
	    {
		    setErrorColor(s);
		    setError(obj1,"Please enter date as either dd/mm/yyyy");
            return false;
	    }
		day = matchArray[1];
	    month = matchArray[3];
	    year = matchArray[5];
	    if (day < 1 || day > 31) 
	    {
	        setErrorColor(s);
		    setError(obj1,"Day must be between 1 and 31.");
		    return false;
	    }
		if (month < 1 || month > 12) // check month range
	    { 
		    setErrorColor(s);
		    setError(obj1,"Month must be between 1 and 12.");
            return false;

	    }
	    if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	    {
	        setErrorColor(s);
	        setError(obj1,"Month "+ months[month-1]+" doesn't have 31 days!");
            return false;
	    }
		if (year < 1900) 
	    {
	        setErrorColor(s);
	        setError(obj1,"Year must be after 1900");
            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)) 
		    {
		        setErrorColor(s);
		        setError(obj1,"February " + year + " doesn't have " + day + " days!");
                return false;
    		    
		    }
		 }
		setSuccessColor(s);
	    setSuccess(obj1); 
	    return true; // date is valid
	}
	
/****************************************************************
   Function  verify the IPAddress
              This function accepts s as any control element object and verifies the
               IPAddress.
               Output: function give error message return false if input is not valid
                    else it return successful message
   Here s >>Any textbox object
        id >>Any span Id or label Id for Display Validation Message  
******************************************************************/

	
	function verifyIP(s,id) 
    {
  	    var errorString = "";
	    var theName = "IPaddress";
	    var IPvalue= s.value;
        var obj1 = document.getElementById(id);
	    var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	    var ipArray = IPvalue.match(ipPattern); 
         if(IPvalue.trim().length==0)
        {
            setErrorColor(s);
            setError(obj1,"Insert IP address");
            return false;
        }
	    if (IPvalue == "0.0.0.0")
	    errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	    else if (IPvalue == "255.255.255.255")
	    errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	    if (ipArray == null)
	    errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
	    else {
	            for (i = 0; i < 4; i++) {
	            thisSegment = ipArray[i];
	            if (thisSegment > 255) {
	                errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
	                i = 4;
	                }
	                if ((i == 0) && (thisSegment > 255)) {
	                errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	                i = 4;
		                  }
	             }
	         }
	    extensionLength = 3;
	    if (errorString == "")
	    {
		     setSuccessColor(s);
		     setSuccess(obj1); 
	         return true; 
	    }
	    else
	    {
	          setErrorColor(s);
	          setError(obj1,errorString);
	          return false;
        }
    }	
	
	
	
	
/****************************************************************
   Function  for returning object of any element
   Here n >>Any textbox Id
         
******************************************************************/
function de(n)
{
    return document.getElementById(n);
}

/****************************************************************
   Function  for confirmation about deletion of any item
             OUTPUT: return true if confirmed else false
   
         
******************************************************************/
function ConfirmDelete()
{
    if(confirm("Are You Sure To Delete the Record"))
        return true;
    else
        return false;
}







//************************************************************************************
//*************************Inner function********************************************
// function for trim
String.prototype.trim=new Function("return this.replace(/^\\s+|\\s+$/g,'')");
//function for replacing string
String.prototype.replaceStr=function(find,replace)
{return this.split(find).join(replace);};
//function for check name
function checkName(name){return true;}
//function for validation successful message
function setSuccess(o,m)
{
o.innerHTML="<span class=\"success_msg\">"+(m==null?"&nbsp;":m)+"</span>";
o.className="success";
}

//function for search successful message ajax search
function setSuccessIsAvailable(o,m)
{
o.innerHTML="<span class=\"success_msg\">"+ m.value +" is available</span>";
o.className="success";
//alert(m.value);
}

//function for validation Error Message
function setError(o,m)
{
o.innerHTML="<span class=\"error_msg\">"+(m==null?"&nbsp;":m)+"</span>";
o.className="error";
}
// function foe set background color for Error Message
function setSuccessColor(inpElem)
{
 //inpElem.className="inboxSuccess";                                    
}	     
// function foe set background color for success Message
function setErrorColor(inpElem)
{
 //inpElem.className="inboxError";
}	     
//Inner function for Email check
function checkMail(email)
{
    var filter=/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if(filter.test(email))
    {return true;}
    else{return false;}
}
//************************************************************************************
//************************************************************************************

