function CustomValidateFirstName(source, arguments)
{    
    var FirstName = getElementContact(source.controltovalidate);
    if (FirstName.value == "")
    {
	    source.errormessage = 'Please, enter your First Name.';	    
	    arguments.IsValid=false;
	    return;
    }
	else
	{
		if (!ValidateName(FirstName.value))
		{
			source.errormessage = 'Please, check your First Name.';    	   
			arguments.IsValid=false;
			return;
		}
		if(!ValidateConsonants(FirstName.value))
		{
			source.errormessage = 'Please, check your First Name.';
			arguments.IsValid=false;				
			return;							
		}
	}
    arguments.IsValid=true;
}


function CustomValidateLastName(source, arguments)
{
    var LastName = getElementContact(source.controltovalidate);
    if (LastName.value == "")
    {
	    source.errormessage = 'Please, enter your Last Name.';	    
	    arguments.IsValid=false;
	    return;
    }
	else
	{
		if (!ValidateName(LastName.value))
		{
			source.errormessage = 'Please, check your Last Name.';    	   
			arguments.IsValid=false;
			return;
		}
		if(!ValidateConsonants(LastName.value))
		{
			source.errormessage = 'Please, check your Last Name.';
			arguments.IsValid=false;				
			return;							
		}
	}
    arguments.IsValid=true;
}



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

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


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


function ValidateComments(source, arguments)
{

    var Comment = getElementContact(source.controltovalidate);
    if (Comment.value == "")
    {
	    source.errormessage = 'Please, enter your Comments.';
	    //Comment.focus();
	    arguments.IsValid=false;
	    return;
    }		
    arguments.IsValid=true;
}




function getElementContact(name)
{
    var object = null;
    var tbSmallContact=document.getElementById('tbContactLarge'); 
    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 IsNumber(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 LoadPhone(){

   
    if (Page_ClientValidate("LargeContact"))
    {
        var txtPhone=document.getElementById('txtPhone');     
        var txtPhone1=document.getElementById('txtPhone1'); 
        var txtPhone2=document.getElementById('txtPhone2'); 
        var txtPhone3=document.getElementById('txtPhone3'); 
        
        txtPhone.value="("+txtPhone1.value+") "+txtPhone2.value+"-"+txtPhone3.value;
        return true;
    }
    else return false;
   
    
   
}
function CustomHasNumbers(source, args) 
    {  
     	if (/[0-9]/.test(args.Value))
            args.IsValid = false;
        else
            args.IsValid = true;
    }
function CustomValidatePhone(source, arguments)
{
    var PhoneType = getElementContact('ddlPhoneType');
    var AreaCode = getElementContact('txtPhone1');
    var Prefix = getElementContact('txtPhone2');
    var Suffix = getElementContact('txtPhone3');
    var Phone = getElementContact('txtPhone');
    
    if(trim(AreaCode.value) != "" && trim(Prefix.value) != "" && trim(Suffix.value) != "" && PhoneType.value == "")
    {
        source.errormessage = 'Plese, select the Phone Type.';
        PhoneType.focus();
        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;
    }
	
	if (!IsNumber(AreaCode.value))
	{
		source.errormessage = 'Please, enter a valid Area Code.';
		AreaCode.focus();
		arguments.IsValid=false;
		return;
	}
	
	if (!IsNumber(Prefix.value))
	{
		source.errormessage = 'Please, enter a valid Prefix.';
		Prefix.focus();
		arguments.IsValid=false;
		return;
	}
	
	if (!IsNumber(Suffix.value))
	{
		source.errormessage = 'Please, enter a valid Suffix.';
		Suffix.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;
    }
    
    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;
}
