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 ValidateFirstName(source, arguments)
{

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

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

function ValidateAddress(source, arguments)
{    
    var Address = getElementContact(source.controltovalidate);    
    if ((Address.value == "") || (Address.value == "Street Address"))
    {
	    source.errormessage = 'Please, enter your Street Address.';
	   
	    arguments.IsValid=false;		
	    return;
    }		
    arguments.IsValid=true;
}

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

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

function ValidateZipCode(source, arguments)
{    
    var ZipCode = getElementContact(source.controltovalidate);
    if ((ZipCode.value == "") || (ZipCode.value == "ZIP/Postal Code"))
    {
	    source.errormessage = 'Please, enter your ZipCode.';
	    arguments.IsValid=false;		
	    return;
    }
	else		
	{
		if (!ValidateAreaCode(ZipCode.value))
		{
			source.errormessage = 'Please, check your ZipCode.';    	   
			ZipCode.focus();
			arguments.IsValid=false;
			return;
		}
	}	
    arguments.IsValid=true;
}

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

function ValidatePhone(source, arguments)
{
    var Phone = getElementContact(source.controltovalidate);
    if ((Phone.value == "") || (Phone.value == "Primary Phone"))
    {
	    source.errormessage = 'Please, enter your Phone.';
	   
	    arguments.IsValid=false;		
	    return;
    }
    else
    {
	    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;
	             return;
	        }
	    }
	 }
	 arguments.IsValid= true;
}

function ValidatePhone2(source, arguments)
{
    var Phone = getElementContact(source.controltovalidate);
    if ((Phone.value != "") && (Phone.value != "Secondary Phone"))
    {	    
	    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;
	             return;
	        }
	    }
	 }
	 arguments.IsValid= 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 CustomValidateEmail(source, arguments)
{
    if(arguments)
    {
        var Email = getElementContact(source.controltovalidate);
        if ((Email.value == "") || (Email.value == "email Address"))
        {
	        source.errormessage = 'Please, enter your Email Address.';
    	   
	        arguments.IsValid=false;			
	        return;
        }
        else
        {			
			if(!ValidateEmailCharacters(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
			 	arguments.IsValid=false;
				return;
			}	
			
			if(!ValidateEmail(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
			 	arguments.IsValid=false;
				return;
			}
			
			if(!ValidateEmailDomain(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
			 	arguments.IsValid=false;
				return;
			}
			
			if(!ValidateConsonants(Email.value))
			{
				source.errormessage = "Please check your Email Address.";
			 	arguments.IsValid=false;
				return;
			}
        }
        arguments.IsValid=true;
    }    
}

function ValidateContactMe(source, arguments)
{
    var cblContactMe = document.getElementById(source.controltovalidate);
    var length = 4;
    for (i = 0; i <length; i ++)
    {
        if (document.getElementById("cblHowContactYou_" + i ).checked)
        {
            var Phone = getElementContact("txtOtherPhone");
            if (Phone.value == Phone.defaultValue)
            {
                Phone.value = "";
            }
            arguments.IsValid=true;
            return;
        }
    }
    
    source.errormessage = 'Please, select any Contact Me.';   
    arguments.IsValid=false;		
    return;
}

function clearTextFirstName(thefield) {
  if ("First Name"==thefield.value) { thefield.value = "" }
}
function replaceTextFirstName(thefield) {
  if (thefield.value=="") { thefield.value = "First Name" }
}

function clearTextLastName(thefield) {
  if ("Last Name"==thefield.value) { thefield.value = "" }
}
function replaceTextLastName(thefield) {
  if (thefield.value=="") { thefield.value = "Last Name" }
}

function clearTextAddress(thefield) {
  if ("Street Address"==thefield.value) { thefield.value = "" }
} 
function replaceTextAddress(thefield) {
  if (thefield.value=="") { thefield.value = "Street Address" }
}

function clearTextCity(thefield) {
  if ("City"==thefield.value) { thefield.value = "" }
} 
function replaceTextCity(thefield) {
  if (thefield.value=="") { thefield.value = "City" }
}

function clearTextState(thefield) {
  if ("State/Province"==thefield.value) { thefield.value = "" }
} 
function replaceTextState(thefield) {
  if (thefield.value=="") { thefield.value = "State/Province" }
}

function clearTextZipCode(thefield) {
  if ("ZIP/Postal Code"==thefield.value) { thefield.value = "" }
} 
function replaceTextZipCode(thefield) {
  if (thefield.value=="") { thefield.value = "ZIP/Postal Code" }
}

function clearTextCountry(thefield) {
  if ("Country"==thefield.value) { thefield.value = "" }
} 
function replaceTextCountry(thefield) {
  if (thefield.value=="") { thefield.value = "Country" }
}

function clearTextPhone(thefield) {
  if ("Primary Phone"==thefield.value) { thefield.value = "" }
} 
function replaceTextPhone(thefield) {
  if (thefield.value=="") { thefield.value = "Primary Phone" }
}

function clearTextOtherPhone(thefield) {
  if ("Secondary Phone"==thefield.value) { thefield.value = "" }
} 
function replaceTextOtherPhone(thefield) {
  if (thefield.value=="") { thefield.value = "Secondary Phone" }
}

function clearTextEmail(thefield) {
  if ("email Address"==thefield.value) { thefield.value = "" }
} 
function replaceTextEmail(thefield) {
  if (thefield.value=="") { thefield.value = "email Address" }
}

function clearText(thefield) {
  if (thefield.defaultValue==thefield.value) { thefield.value = "" }
} 
function replaceText(thefield) {
  if (thefield.value=="") { thefield.value = thefield.defaultValue }
}



function trim(myString)
{
 return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}
 
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;
}

