var focus = false;

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

function ValidateFirstName(source, arguments)
{
    focus = false;
    var FirstName = getElementContact(source.controltovalidate);
    if (FirstName.value == "")
    {
	    source.errormessage = 'Please, enter your First Name.';
	    FirstName.focus();
	    focus = true;
	    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.';
           if(!focus)
		{
		LastName.focus();
		focus = true;
		}
	    arguments.IsValid=false;		
	    return;
    }		
    arguments.IsValid=true;
}

function ValidateEvents(source, arguments)
{    
    var ddlEvent = getElementContact(source.controltovalidate);
        
	if (ddlEvent.selectedIndex <= 0)
	{
		source.errormessage = "Please select 'How did you hear events?' option.";
		arguments.IsValid=false;		
           	if(!focus)
		{
		ddlEvent.focus();
		focus = true;
		}
	    return;
	}    
    arguments.IsValid=true;
}

function ValidateComments(source, arguments)
{

    var Comments = getElementContact(source.controltovalidate);
    if (Comments.value == "")
    {
	    source.errormessage = 'Please, enter your Comments.';
           if(!focus)
		{
		Comments.focus();
		focus = true;
		}
	    arguments.IsValid=false;		
	    return;
    }		
    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 ValidateEmail(valor) 
    {
	    if (/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(valor))
		    return true;
	    else
		    return false;
    }  

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

		        arguments.IsValid=false;				
		        return;
	        }
        }
        arguments.IsValid=true;
    }    
}

function ValidatePhone(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;
	             return;
	        }
	    }
	 }
	 arguments.IsValid= true;
}