function CustomValidateEmail(source, arguments)
{
    if(arguments)
    {
        var Email = getElementContact(source.controltovalidate);
        if (Email.value == "")
	    {
		    source.errormessage = 'Please, enter your Email.';
		    Email.focus();
		    arguments.IsValid=false;
		    return;
	    }
	    else
        {
			if(!ValidateEmailCharacters(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
				if(!focus)
				{
					Email.focus();
					focus=true;
				}
			 	arguments.IsValid=false;
				return;
			}	
			
			if(!ValidateEmail(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
				if(!focus)
				{
					Email.focus();
					focus=true;
				}
			 	arguments.IsValid=false;
				return;
			}
			
			if(!ValidateEmailDomain(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
				if(!focus)
				{
					Email.focus();
					focus=true;
				}
			 	arguments.IsValid=false;
				return;
			}
			
			if(!ValidateConsonants(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
				if(!focus)
				{
					Email.focus();
					focus=true;
				}
			 	arguments.IsValid=false;
				return;
			}
		}
        arguments.IsValid=true;    
    }    
}

function trim(myString)
{
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}

function CustomValidateName(source, arguments)
{
    if(arguments)
    {
        var Name = getElementContact(source.controltovalidate);
        var NamePrefix = getElementContact('ddlPrefixName');
        var FullName = getElementContact('txtFullName');
        if (trim(Name.value) == "")
	    {
		    source.errormessage = 'Please, enter your Name.';
		    Name.focus();
		    FullName.value = "";
		    arguments.IsValid=false;
		    return;
	    }
	    else
	    {
			if (!ValidateName(Name.value))
			{
				source.errormessage = 'Please, check your Name.';    	   
				Name.focus();															
				arguments.IsValid=false;
				return;
			}
			if(!ValidateConsonants(Name.value))
			{
				source.errormessage = 'Please, check your Name.';
				Name.focus();				
				arguments.IsValid=false;				
				return;							
			}			
	    }
        arguments.IsValid=true;    
    }    
}

function CustomValidateAddress(source, arguments)
{
    if(arguments)
    {
        var Address = getElementContact(source.controltovalidate);
        if (trim(Address.value) == "")
	    {
		    source.errormessage = 'Please, enter your Address.';
		    Address.focus();
		    Address.value = "";
		    arguments.IsValid=false;
		    return;
	    }
	    
        arguments.IsValid=true;
    
    }    
}
function CustomValidateZipCode(source, arguments)
{
    if(arguments)
    {
        var ZipCode = getElementContact(source.controltovalidate);
        if (trim(ZipCode.value) == "")
	    {
		    source.errormessage = 'Please, enter your Zip Code.';
		    ZipCode.focus();		    
		    arguments.IsValid=false;
		    return;
	    }	   
        arguments.IsValid=true;    
    }    
}

function CustomValidateCity(source, arguments)
{
    if(arguments)
    {
        var City = getElementContact(source.controltovalidate);
        if (trim(City.value) == "")
	    {
		    source.errormessage = 'Please, enter your City.';
		    City.focus();		    
		    arguments.IsValid=false;
		    return;
	    }
		else
		{	   	   
			if (!ValidateName(City.value))
			{
				source.errormessage = 'Please, check your City.';    	   
				City.focus();				
				focus=true;
				arguments.IsValid=false;
				return;
			}
			if(!ValidateConsonants(City.value))
			{
				source.errormessage = 'Please, check your City.';
				City.focus();
				focus=true;
				arguments.IsValid=false;				
				return;
			}
		}	    
        arguments.IsValid=true;    
    }    
}
function CustomValidateState(source, arguments)
{
    if(arguments)
    {
        var State = getElementContact(source.controltovalidate);
        if (trim(State.value) == "")
	    {
		    source.errormessage = 'Please, enter your State.';
		    State.focus();
		    State.value = "";
		    arguments.IsValid=false;
		    return;
	    }
		else
		{	   	   
			if (!ValidateName(State.value))
			{
				source.errormessage = 'Please, check your State.';    	   
				State.focus();
				State.value = "";
				focus=true;
				arguments.IsValid=false;
				return;
			}
			if(!ValidateConsonants(State.value))
			{
				source.errormessage = 'Please, check your State.';
				State.focus();
				State.value = "";
				focus=true;
				arguments.IsValid=false;				
				return;
			}
		}	    
        arguments.IsValid=true;    
    }    
}

function CustomValidateLargeEmail(source, arguments)
{
    if(arguments)
    {
        var Email = getElementContact(source.controltovalidate);
        if (Email.value == "")
	    {
		    source.errormessage = 'Please, enter your Email.';
		    Email.focus();
		    arguments.IsValid=false;
		    return;
	    }
	    else
	    {
	        if(!ValidateEmail(Email.value))
		    {
			    source.errormessage = "Please, check your Email.";
			    Email.focus();
			    arguments.IsValid=false;
			    return;
		    }
			
			if(!ValidateEmailCharacters(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
				Email.focus();
			 	arguments.IsValid=false;
				return;
			}	
			
			if(!ValidateEmail(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
				Email.focus();
			 	arguments.IsValid=false;
				return;
			}
			
			if(!ValidateEmailDomain(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
				Email.focus();
			 	arguments.IsValid=false;
				return;
			}
			
			if(!ValidateConsonants(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
				Email.focus();
			 	arguments.IsValid=false;
				return;
			}
	    }
        arguments.IsValid=true;   
    }    
}


function ValidateName(valor) 
{
		valor = valor.toLowerCase();
	if (/^[a-z ]+$/i.test(valor))
		return true;
	else
		return false;
}

function ValidateConsonants(valor) 
{
	   valor = valor.toLowerCase();
	if (/[bcdfghjklmnpqrstvwxyz]{7}/.test(valor))
		return false;
	else
		return true;
}

function ValidateEmail(valor) 
{    
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]{2,60}(\.[a-zA-Z]{2,4}){1,2}$/;  
    return emailPattern.test(valor); 
}


function ValidateEmailCharacters(valor)
{
	valor = valor.toLowerCase();
	var ValidChars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";   
	var Char;
	var IsCorrect=true;
	
	for (cont = 0; cont < valor.length && IsCorrect == true; cont++) 
	{ 
		Char = valor.charAt(cont); 
		if (ValidChars.indexOf(Char) == -1) {
			return false;
		}
	}
	return true;
}

function ValidateEmailDomain(valor) 
{
	valor = valor.toLowerCase();
	if (/.ru$|.sk$|.ua$/i.test(valor))
		return false;	
	else
		return true;
} 

function ValidateAreaCode(incoming)
{
    var ValidChars = "0123456789";
    var IsCorrect=true;
    var Char;
    var IsValid=false;
    for (cont = 0; cont < incoming.length && IsCorrect == true; cont++) 
    { 
        Char = incoming.charAt(cont); 
        if (ValidChars.indexOf(Char) == -1) {
             return false;
        }
    }
    return true;
}



function getElementContact(name)
{
    var object = null;
    var tbSmallContact=document.getElementById('tbContact'); 
    for (var i=0; i<tbSmallContact.rows.length; i++)
    {
        for (var j=0; j<tbSmallContact.rows[i].cells.length; j++)
        {
            for (var k=0; k<tbSmallContact.rows[i].cells[j].childNodes.length; k++)
            {                
                if(tbSmallContact.rows[i].cells[j].childNodes[k].id == name)
                {
                   object = tbSmallContact.rows[i].cells[j].childNodes[k];
                   return object;
                }
            }
        }
    }    
}

function trim(myString)
{
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}

function CustomValidatePhone(source, arguments)
{
    var PhoneType = getElementContact('ddlPhoneType');
    var AreaCode = getElementContact('txtAreaCode');
    var Prefix = getElementContact('txtPrefix');
    var Suffix = getElementContact('txtSuffix');
    var Phone = getElementContact('txtPhone');
    
    if(trim(AreaCode.value) != "" && trim(Prefix.value) != "" && trim(Suffix.value) != "" && PhoneType.value == "")
    {
        source.errormessage = 'Please, select the phone type.';
        PhoneType.focus();
        arguments.IsValid=false;
        return;
    }
    
    if(trim(AreaCode.value) != "" && trim(Prefix.value) != "" && trim(Suffix.value) != "" && PhoneType.value != "")
    {
        Phone.value = '(' + AreaCode.value + ')-' + Prefix.value + '-' + Suffix.value;
		
		var ValidChars = "0123456789.()- ";
	    var IsCorrect=true;
	    var Char;
        var IsValid=false;
        incoming= Phone.value;
	    for (cont = 0; cont < incoming.length && IsCorrect == true; cont++) 
	    { 
	        Char = incoming.charAt(cont); 
	        if (ValidChars.indexOf(Char) == -1) {
				 source.errormessage = 'Please, check the Phone.';
	             arguments.IsValid= false;
	             return;
	        }
	    }
    }
    
    if(trim(AreaCode.value) == "" || trim(Prefix.value) == "" || trim(Suffix.value) == "")
    {
        source.errormessage = 'Please, enter a valid Phone Number.';
        PhoneType.focus();
        arguments.IsValid=false;
        return;
    }
    
    arguments.IsValid=true;
}

function ValidateDataContact()
{
	var contactDate=document.getElementById('txtContactcontactDate');
	
	if (contactDate.value != "")
	{						
		return (false);
	}
	
	return true;
}

function OnlyNumbers()
{	
	if(window.event.keyCode < 48 || window.event.keyCode > 57)
	{
		window.event.keyCode = 0;
	}
}

function Error(sender, args) 
{ 
    alert ("Enter a Valid Date in MM/DD/YYYY Format");
    sender.focus();
}
