﻿
// Regular Expression Variables

var String_AlphaSpace = /^([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]+( )?)+$/;
var String_AlphaSpace_N = /^([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]+( )?)+$|^$/;
var String_AlphaNumeric = /^([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_])+$/;
var String_AlphaNumericSpaceSpecialCharecters = /^([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.()+-_,%@&]+( )?)+$/;
var String_AlphaNumericSpaceSpecialCharecters_N = /^([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.()+-_,%@&]+( )?)+$|^$/;
var String_Alpha = /^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]+$/;
var String_Alpha_N = /^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]+$|^$/;
var String_AlphaNumeric1 = /^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]+$/;
var String_AlphaNumeric1_N = /^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]+$|^$/;
var Number = /^[0123456789]+$/;
var Number_N = /^[0123456789]+$|^$/;
var PinCode = /^(\d{6})$/;
var PinCode_N = /^(\d{6})$|^$/;
var PhoneNumber = /^\d{8,}$/;
var PhoneNumber_N = /^\d{8,}$|^$/;
var Email_N = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$|^$/;
var Code_SingleAlpha = /^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]$/;
var Code_CapitalAlphaAndNumber = /^[ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]+$/;
var Code_Date = /^(\d{2})(\/)(\d{2})(\/)(\d{4})$/;
var Bit =/^[01]$/;



function SelectRegularExpression(type)
{
    if(type == "String_AlphaSpace")
    {
        return String_AlphaSpace;
    }
    
    else if(type == "String_AlphaSpace_N")
    {
        return String_AlphaSpace_N;
    }
    
    else if(type == "String_AlphaNumeric")
    {
        return String_AlphaNumeric;
    }
        
    else if(type == "String_AlphaNumericSpaceSpecialCharecters")
    {
        return String_AlphaNumericSpaceSpecialCharecters;
    }
    
    else if(type == "String_AlphaNumericSpaceSpecialCharecters_N")
    {
       return String_AlphaNumericSpaceSpecialCharecters_N; 
    }
    
    else if(type == "String_Alpha")
    {
        return String_Alpha;
    }
    
    else if(type == "String_Alpha_N")
    {
        return String_Alpha_N;
    }
    
    else if(type == "String_AlphaNumeric1")
    {
        return String_AlphaNumeric1;
    }
    
    else if(type == "String_AlphaNumeric1_N")
    {
        return String_AlphaNumeric1_N;
    }
    
    else if(type == "Number")
    {
        return Number;
    }
    else if(type == "Number_N")
    {
        return Number_N;
    }
    
    else if(type == "PinCode")
    {
        return PinCode;
    }
    
    else if(type == "PinCode_N")
    {
        return PinCode_N;
    }
    
    else if(type == "PhoneNumber")
    {
        return PhoneNumber;
    }
    
    else if(type == "PhoneNumber_N")
    {
        return PhoneNumber_N;
    }
            
    else if(type == "Email_N")
    {
        return Email_N;
    }
    
    else if(type == "Code_SingleAlpha")
    {
        return Code_SingleAlpha;
    }
    
    else if(type == "Code_CapitalAlphaAndNumber")
    {
        return Code_CapitalAlphaAndNumber;
    }
    
    else if(type == "Date")
    {
        return Code_Date;
    }
    
    else if(type == "Bit")
    {
        return Bit;
    }
          
}

//Javascript file
function ValidateTextBox(str,messagenull,message,type)
{
    type = SelectRegularExpression(type);
    if(messagenull == "" && document.getElementById(str).value=="")
    {
        return true;
    }
    else if(messagenull == "" && document.getElementById(str).value!="")
    {
        if (type.test(document.getElementById(str).value) == true)
        {
          return true;        
        }           
        else
        {
            alert(message);
            document.getElementById(str).select();
            return false;
        }    
    }
    else if((messagenull) != "" && (document.getElementById(str).value)=="")
    {
        alert(messagenull);
        document.getElementById(str).select();
        return false;
    }
    else if((messagenull) != "" && (document.getElementById(str).value)!="")
    {
        if (type.test(document.getElementById(str).value) == true)
        {
            return true;        
        }           
        else
        {
            alert(message);
            document.getElementById(str).select();
            return false;  
        } 
    }
}

//Validate Drop Down
function ValidateDropdown(str,message)
{
    if(document.getElementById(str).selectedIndex==0)
    {
        alert(message);
        document.getElementById(str).focus();
        return false;
    }
    return true;
}

function ValidateRadioButton(str,message,count)
{
    var i=0;
    for (i=0;i<count;i++)
    {
        if(document.getElementById(str+'_'+i).checked)
        {
            return true;
        }
    }
    alert(message);
    
    document.getElementById(str+'_'+0).focus();
    return false;
}

function ValidateBirthDate(strServerDate,strBirthDate,message)
{
   var serverDate=new Date(document.getElementById(strServerDate).value);
   var birthDate=new Date(document.getElementById(strBirthDate).value);
   var dateDiff=((serverDate - birthDate)/86400000);
   if(dateDiff<6574)
   {
     alert(message);
     document.getElementById(strBirthDate+'_Day_ID').focus();
     return false;
   }
   return true;

}

function ValidateJoiningDate(strJoiningDate,strBirthDate,strServerDate,message)
{
   var joiningDate=new Date(document.getElementById(strJoiningDate).value);
   var birthDate=new Date(document.getElementById(strBirthDate).value);
   var serverDate=new Date(document.getElementById(strServerDate).value);
   var dateDiff=((joiningDate - birthDate)/86400000);
   if((dateDiff<6574) || (joiningDate>serverDate))
    {
       alert(message);
       document.getElementById(strJoiningDate+'_Day_ID').focus();
       return false;
    }
   
      return true;
}

function ValidateCurrentOfficeDate(strCurrentOfficeDate,strJoiningDate,strServerDate,message)
{
   var currentOfficeDate=new Date(document.getElementById(strCurrentOfficeDate).value);
   var joiningDate=new Date(document.getElementById(strJoiningDate).value);
   var serverDate=new Date(document.getElementById(strServerDate).value);
   if((currentOfficeDate < joiningDate) || (currentOfficeDate > serverDate))
   {
    alert(message);
    document.getElementById(strCurrentOfficeDate+'_Day_ID').focus();
    return false;
   }
    return true;
    
}

function ValidateIncrementDate(strIncrementDate,strCurrentOfficeDate,strServerDate,message)
{
   var incrementDate=new Date(document.getElementById(strIncrementDate).value);
   var currentOfficeDate=new Date(document.getElementById(strCurrentOfficeDate).value);
   var serverDate=new Date(document.getElementById(strServerDate).value);
   var dateDiff=((incrementDate - currentOfficeDate)/86400000);
     
   if((dateDiff<365) || (incrementDate < serverDate))
   {
    alert(message);
    document.getElementById(strIncrementDate+'_Day_ID').focus();
    return false;
   }
    return true;
    
}

function ValidateFromDate(strFromDate,strJoiningDate,strServerDate,message)
{
   var fromDate=new Date(document.getElementById(strFromDate).value);
   var joiningDate=new Date(document.getElementById(strJoiningDate).value);
   var serverDate=new Date(document.getElementById(strServerDate).value);
   if((fromDate > serverDate) || (fromDate < joiningDate))
   {
    alert(message);
    document.getElementById(strFromDate+'_Day_ID').focus();
    return false;
   }
    return true;
}


 //Validation of From Date (should be > MAXPrevToDate or <= Todays Date)
function ValidateFromDateWithPrevToDate(strFromDate,strPrevToDate,strServerDate,message)
{
    
   var fromDate=new Date(document.getElementById(strFromDate).value);
   var prevToDate=new Date(document.getElementById(strPrevToDate).value);
   var serverDate=new Date(document.getElementById(strServerDate).value);
    
   if ((fromDate > serverDate) || (fromDate <= prevToDate))
   {
      alert(message + ' and MAXPrevToDate is ' + document.getElementById(strPrevToDate).value + ' (yyyy/mm/dd) ');
      document.getElementById(strFromDate+'_Day_ID').focus();
      return false;
   }
   
      return true;
   

}



