var focus = false;
function CustomValidatePhone(source, arguments)
{
var Phone = getElementContact(source.controltovalidate);
    if (Phone.value != "")
    {	    
	    var ValidChars = "0123456789.()- ";
	    var IsCorrect=true;
	    var Char;
        var IsValid=false;
        incoming= document.getElementById(source.controltovalidate).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;
				 if(!focus)
				{
				 Phone.focus();
				 focus = true;
				}
	             return;
	        }
	    }
	 }
	 arguments.IsValid= true;
}

function validateBestTimeCall(source, arguments)
{
    var radioButtons = document.getElementsByName(source.controltovalidate);
    var txtOther = document.getElementById('txtOther');
    var rbtn;
    for (var x = 0; x < radioButtons.length; x ++) {
        if (radioButtons[x].checked) {
            rbtn = radioButtons[x];
            //alert("You checked " + radioButtons[x].id + " which has the value " + radioButtons[x].value);
        }
    }    
	
    if(arguments.Value == "Other" &&  txtOther.value == "")
    {
        arguments.IsValid= false;
		if(!focus)
		{
		 txtOther.focus();
		 focus = true;
		}        
        return;
    }
    else if (arguments.Value == "Other" &&  txtOther.value != "")
    {
        rbtn.value = txtOther.value;
    }
	focus = false;
    arguments.IsValid= true;
}


function useOtherContacts(value)
{
    var hdnContactFormType = document.getElementById("hdnContactFormType");    
    hdnContactFormType.value = value;
}

function ValidateFirstName(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 ValidateLastName(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;
		}		
	}
	
	focus = false;
    arguments.IsValid=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 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;
			}
        }
		focus = false;
        arguments.IsValid=true;
    }    
}

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

function CustomValidateAreaCode(source, arguments)
{
	var IsValid=false;
	incoming= document.getElementById(source.controltovalidate).value;
	if (incoming!="")
	{
		if (!ValidateAreaCode(incoming))
		{
			source.errormessage = 'Please, check your Zip Code.';
			arguments.IsValid= false;
            return;
		}
	}
	arguments.IsValid= true;
}

function CustomValidateState(source, arguments)
{
	var IsValid=false;
	incoming= document.getElementById(source.controltovalidate).value;
	if (incoming!="")
	{
		if (!ValidateName(incoming))
		{
			source.errormessage = 'Please, check your State.';			
			arguments.IsValid= false;
            return;
		}
	}
	arguments.IsValid= true;
}

function ValidateBirthDate(source, arguments) {

 var datePicker = $find("rdpBirthDate"); 
 var Birth = datePicker.get_textBox();  
    if (Birth.value != "")
	{
	var start = new Date(Birth.value);
	var end = new Date();
		if (end < start) 
		{
			source.errormessage = 'Please, enter valid BirthDate.';
			if(!focus)
				{
				 Birth.focus();
				 focus = true;
				}
		    arguments.IsValid=false;
		    return;
		}
		focus = false;
        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 ValidateEmailDomain(valor) 
{
	valor = valor.toLowerCase();
	if (/.ru$|.sk$|.ua$/i.test(valor))
		return false;	
	else
		return true;
} 

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 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 ValidateAreaCode(incoming)
{
	var ValidChars = "0123456789";			
	var Char;
	var IsCorrect=true;
	
	for (cont = 0; cont < incoming.length && IsCorrect == true; cont++) 
	{ 
		Char = incoming.charAt(cont); 
		if (ValidChars.indexOf(Char) == -1) {
			 
			 return false;
		}
	}
	return true;
}


