﻿// JScript File
// Summary description for UserRegistration.js
// FileName                 : UserRegistration.js
// Descriptio               : Validating UserRegistration Fields 
// Developer                : Aruna , Swami
// Created Date             : 29-Sep-2007 
 function UserEditValidations()
    { 
        var IsValid=0;
        var Msg = "Required Information is Incorrect \n\n";
        var alphaCount = 0;

       
        //Validation for First Name
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtFirstName").value == "")
        {
            Msg = Msg + "First Name \n";
            IsValid =1;
        }
        else
        {
            var sizechar=document.getElementById("ctl00_cphAdmin_UserRegistration1_txtFirstName").value;
            var testName =/^([a-z]|[A-Z]| )*$/;
            if(!testName.test(sizechar))
            {
				Msg=Msg + "Name doesn't contain Digits \n";
				IsValid=1;
			}
        }
        
        //Validation For Last Name
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtLastName").value != "")
        {
            var sizechar=document.getElementById("ctl00_cphAdmin_UserRegistration1_txtLastName").value;
            var testName =/^([a-z]|[A-Z]| )*$/;
            if(!testName.test(sizechar))
            {
                Msg=Msg + "Last Name doesn't contain Digits \n";
				IsValid=1;
			}
        }
        
        //Validation for Date Of Birth
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtDob").value != "")
        {
            var today = new Date();
            
            var CurrentYear = today.getFullYear();
            
            var birthdate = document.getElementById("ctl00_cphAdmin_UserRegistration1_txtDob").value.split('-');
            var year = birthdate[2];
            
            if(year >= CurrentYear)
            {
                Msg=Msg+"-Invalid Date Of Birth \n";
                IsValid = 1;
            }
        }
        
        //Validation For Address
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtAddress").value == "")
        {
            Msg = Msg + "Address \n";
            IsValid = 1;
        }
        
        //Validation For City
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtCity").value == "")
        {
            Msg = Msg + "City \n";
            IsValid = 1;
        }
        else
        {
            var sizechar=document.getElementById("ctl00_cphAdmin_UserRegistration1_txtCity").value;
            var testName =/^([a-z]|[A-Z]| )*$/;
            if(!testName.test(sizechar))
            {
              	Msg=Msg + "City doesn't contain Digits \n";
				IsValid=1;
			}
        }
        
        //Validation For State
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtState").value != "")
        {
            var sizechar=document.getElementById("ctl00_cphAdmin_UserRegistration1_txtState").value;
            var testName =/^([a-z]|[A-Z]| )*$/;
            if(!testName.test(sizechar))
            {
                Msg = Msg + "State doesn't contain numbers \n";
                IsValid = 1;
            }
        }
        
        //Validation For Country
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_dlCountry").selectedIndex == 0)
        {
            Msg = Msg + "Select Country \n";
            IsValid = 1;
        }
       
        //Validation For Postal Code
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtPostalCode").value == "")
        {
            Msg = Msg + "Postal Code \n";
            IsValid = 1;
        }
        else
        {
            var PostalCode = document.getElementById("ctl00_cphAdmin_UserRegistration1_txtPostalCode").value;
            if(isNaN(PostalCode))
            {
                Msg = Msg + "Enter Only Numbers in Postal Code \n";
                IsValid = 1;
            } 
        }
        
        //Validation For EMail
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtEmail").value == "")
        {
            Msg = Msg + "EMail \n";
            IsValid = 1;
        }
        else
        {
            var email = document.getElementById("ctl00_cphAdmin_UserRegistration1_txtEmail");
            if(echeck(email.value) == false)
            {
                email.value = "";
                email.focus();
                Msg = Msg + "Invalid EMail Id \n";
                IsValid = 1;
            }
        }

        //Validation For Telephone Number
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtTelephone").value != "")
        {
            var Contact = document.getElementById("ctl00_cphAdmin_UserRegistration1_txtTelephone").value;
            var stripped = Contact.replace(/[\(\)\.\-\ ]/g,'');
            if(isNaN(stripped))
            {
                Msg = Msg + "Telephone Contains Characters \n";
                IsValid = 1;
            }
        }
        
        //Validation For Fax
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtFax").value != "")
        {
            var Fax = document.getElementById("ctl00_cphAdmin_UserRegistration1_txtFax");
            var stripped = Fax.value.replace(/[\(\)\.\-\ ]/g, '');
            if(isNaN(stripped))
            {
                Msg = Msg + "Fax contains characters \n";
                IsValid = 1;
            }
        }
        
        //Validation For Occupation
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtOccupation").value != "")
        {
           var sizechar=document.getElementById("ctl00_cphAdmin_UserRegistration1_txtOccupation").value;
            var testName =/^([a-z]|[A-Z]| )*$/;
            if(!testName.test(sizechar))
            {
                Msg = Msg + "Occupation doesn't contain numbers \n";
                IsValid = 1;
            }
        }
        
        //Validation For Login
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtLogin").value == "")
        {
            Msg = Msg + "Login \n";
            IsValid = 1;
        }
        else 
        {
            var alphabetcount = 0;
            var numcount = 0;
            
            var loginvalue = document.getElementById("ctl00_cphAdmin_UserRegistration1_txtLogin").value;
            var loginsize = document.getElementById("ctl00_cphAdmin_UserRegistration1_txtLogin").value.length;
            
            var num_valid = "0123456789";
            
            for(var i=0;i<loginsize;i++)
            {
                if(num_valid.indexOf(loginvalue.charAt(i)) < 0)
                {
                    numcount++;
                }
            }
            
            var alph_valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            
            for(var i=0;i<loginsize;i++)
            {
                if(alph_valid.indexOf(loginvalue.charAt(i)) < 0)
                {
                    alphabetcount++;
                }
            }
            
            if(((numcount == loginvalue.length)||(alphabetcount == loginvalue.length))&&((numcount!=loginvalue.length)||(alphabetcount == loginvalue.length)))
            {
                Msg = Msg + "Invalid Login \n";
                IsValid = 1;
            }
        }  
        //Validation For Password
        if(document.getElementById("ctl00_cphAdmin_UserRegistration1_txtPassword").value == "")
        {
            Msg = Msg + "Password \n";
            IsValid = 1;
        }
        else 
        {
            var alphabet = 0;
            var number = 0;
            
            var pwdvalue = document.getElementById("ctl00_cphAdmin_UserRegistration1_txtPassword").value;
            var pwdlength = document.getElementById("ctl00_cphAdmin_UserRegistration1_txtPassword").value.length;
            
            var number_valid = "0123456789";
            
            for(var i=0;i<pwdlength;i++)
            {
                if(number_valid.indexOf(pwdvalue.charAt(i)) < 0)
                {
                    number++;
                }
            }
            
            var alphabet_valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            
            for(var i=0;i<pwdlength;i++)
            {
                if(alphabet_valid.indexOf(pwdvalue.charAt(i)) < 0)
                {
                    alphabet++;
                }
            }
            
            if(((number == pwdvalue.length)||(alphabet == pwdvalue.length))&&((number != pwdvalue.length)||(alphabet == pwdvalue.length)))
            {
                Msg = Msg + "Invalid Password \n";
                IsValid = 1;
            }
        }
        
        if(IsValid == 1)
        {
            alert(Msg);
            document.getElementById("ctl00_cphAdmin_UserRegistration1_txtFirstName").focus();
            return false;
        }
    }
    
    
    
    
    ///This is for user side validation
    function UserRegisterValidations()
    {
    
        
       var IsValid=0;
        var Msg = "Required Information is Incorrect \n\n";
        var alphaCount = 0;

       
        //Validation for First Name
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtFirstName").value == "")
        {
            Msg = Msg + "First Name \n";
            IsValid =1;
        }
        else
        {
            var sizechar=document.getElementById("ctl00_cphUser_UserRegistration1_txtFirstName").value;
            var testName =/^([a-z]|[A-Z]| )*$/;
            if(!testName.test(sizechar))
            {
				Msg=Msg + "Name doesn't contain Digits \n";
				IsValid=1;
			}
        }
        
        //Validation For Last Name
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtLastName").value != "")
        {
            var sizechar=document.getElementById("ctl00_cphUser_UserRegistration1_txtLastName").value;
            var testName =/^([a-z]|[A-Z]| )*$/;
            if(!testName.test(sizechar))
            {
                Msg=Msg + "Last Name doesn't contain Digits \n";
				IsValid=1;
			}
        }
        
        //Validation for Date Of Birth
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtDob").value != "")
        {
           
            var today = new Date();
            
            var CurrentYear = today.getFullYear();
            
            var birthdate = document.getElementById("ctl00_cphUser_UserRegistration1_txtDob").value.split('-');
            var year = birthdate[2];
            
            if(year >= CurrentYear)
            {
                Msg=Msg+"-Invalid Date Of Birth \n";
                IsValid = 1;
            }
        }
        
        //Validation For Address
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtAddress").value == "")
        {
            Msg = Msg + "Address \n";
            IsValid = 1;
        }
        
        //Validation For City
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtCity").value == "")
        {
            Msg = Msg + "City \n";
            IsValid = 1;
        }
        else
        {
            var sizechar=document.getElementById("ctl00_cphUser_UserRegistration1_txtCity").value;
            var testName =/^([a-z]|[A-Z]| )*$/;
            if(!testName.test(sizechar))
            {
              	Msg=Msg + "City doesn't contain Digits \n";
				IsValid=1;
			}
        }
        
        //Validation For State
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtState").value != "")
        {
            var sizechar=document.getElementById("ctl00_cphUser_UserRegistration1_txtState").value;
            var testName =/^([a-z]|[A-Z]| )*$/;
            if(!testName.test(sizechar))
            {
                Msg = Msg + "State doesn't contain numbers \n";
                IsValid = 1;
            }
        }
        
        //Validation For Country
        if(document.getElementById("ctl00_cphUser_UserRegistration1_dlCountry").selectedIndex == 0)
        {
            Msg = Msg + "Select Country \n";
            IsValid = 1;
        }
        
        //Validation For Postal Code
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtPostalCode").value == "")
        {
            Msg = Msg + "Postal Code \n";
            IsValid = 1;
        }
        else
        {
            var PostalCode = document.getElementById("ctl00_cphUser_UserRegistration1_txtPostalCode").value;
            if(isNaN(PostalCode))
            {
                Msg = Msg + "Enter Only Numbers in Postal Code \n";
                IsValid = 1;
            } 
        }
        
        //Validation For EMail
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtEmail").value == "")
        {
            Msg = Msg + "EMail \n";
            IsValid = 1;
        }
        else
        {
            var email = document.getElementById("ctl00_cphUser_UserRegistration1_txtEmail");
            if(echeck(email.value) == false)
            {
                email.value = "";
                email.focus();
                Msg = Msg + "Invalid EMail Id \n";
                IsValid = 1;
            }
        }

        //Validation For Telephone Number
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtTelephone").value != "")
        {
            var Contact = document.getElementById("ctl00_cphUser_UserRegistration1_txtTelephone").value;
            var stripped = Contact.replace(/[\(\)\.\-\ ]/g,'');
            if(isNaN(stripped))
            {
                Msg = Msg + "Telephone Contains Characters \n";
                IsValid = 1;
            }
        }
        
        //Validation For Fax
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtFax").value != "")
        {
            var Fax = document.getElementById("ctl00_cphUser_UserRegistration1_txtFax");
            var stripped = Fax.value.replace(/[\(\)\.\-\ ]/g, '');
            if(isNaN(stripped))
            {
                Msg = Msg + "Fax contains characters \n";
                IsValid = 1;
            }
        }
        
        //Validation For Occupation
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtOccupation").value != "")
        {
           var sizechar=document.getElementById("ctl00_cphUser_UserRegistration1_txtOccupation").value;
            var testName =/^([a-z]|[A-Z]| )*$/;
            if(!testName.test(sizechar))
            {
                Msg = Msg + "Occupation doesn't contain numbers \n";
                IsValid = 1;
            }
        }
        
        //Validation For Login
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtLogin").value == "")
        {
            Msg = Msg + "Login \n";
            IsValid = 1;
        }
        else 
        {
             var alphacount = 0;
             var numcount = 0;
             
            var login = document.getElementById("ctl00_cphUser_UserRegistration1_txtLogin").value;
            var size = document.getElementById("ctl00_cphUser_UserRegistration1_txtLogin").value.length;
            var num_valid="0123456789";
            
            for(var i=0;i<size;i++)
            {
                if(num_valid.indexOf(login.charAt(i)) < 0)
                {
                    numcount = numcount + 1;
                }
            }
            
            
            var  alph_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

            for (var i=0; i<size; i++) 
            {
               if (alph_valid.indexOf(login.charAt(i)) < 0) 
               {
                   alphacount++;
               }
            }
               
            if(((numcount==login.length)||(alphacount==login.length))&&((numcount!=login.length)||(alphacount==login.length)))
            {
                 Msg = Msg + "Invalid Login \n";
                 IsValid = 1;
            }
        }
        
        
        //Validation For Password
        if(document.getElementById("ctl00_cphUser_UserRegistration1_txtPassword").value == "")
        {
            Msg = Msg + "Password \n";
            IsValid = 1;
        }
        else 
        {
             var alphacount = 0;
             var numcount = 0;
             
            var pwd = document.getElementById("ctl00_cphUser_UserRegistration1_txtPassword").value;
            var size = document.getElementById("ctl00_cphUser_UserRegistration1_txtPassword").value.length;
            var num_valid="0123456789";
            
            for(var i=0;i<size;i++)
            {
                if(num_valid.indexOf(pwd.charAt(i)) < 0)
                {
                    numcount = numcount + 1;
                }
            }
            
            
            var  alph_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

            for (var i=0; i<size; i++) 
            {
               if (alph_valid.indexOf(pwd.charAt(i)) < 0) 
               {
                   alphacount++;
               }
            }
                
            if(((numcount==pwd.length)||(alphacount==pwd.length))&&((numcount!=pwd.length)||(alphacount==pwd.length)))
            {
                 Msg = Msg + "Invalid Password \n";
                 IsValid = 1;
            }
        }
        
        
        if(IsValid == 1)
        {
            alert(Msg);
            document.getElementById("ctl00_cphUser_UserRegistration1_txtFirstName").focus();
            return false;
        }
    }
    
    function echeck(str)
    {
        var at="@";
        var dot=".";
        var lat=str.indexOf(at);
        var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if (str.indexOf(at)==-1)
		{
			return false;
	    }

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		return false;
		}

		if (str.indexOf(at,(lat+1))!=-1)
		{
		return false;
		}

		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		{
		return false;
		}
		
		if (str.indexOf(dot,(lat+2))==-1)
		{
		return false;
		}
			
		if (str.indexOf(" ")!=-1)
		{
		return false;	
		}
	return true;		
}
