var focus = false;

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

function HearOther(control)
{
	var txtOther = document.getElementById("txtOther");
	if(control.value == "Other")
	{
		txtOther.style.display = "";
		txtOther.focus();
	}
	else
		txtOther.style.display = "none";
}

function ValidatePhone(source, arguments)
{
var Phone = getElementContact(source.controltovalidate);
    if (Phone.value == "")
    {
	    source.errormessage = 'Please, enter your Phone.';
	    arguments.IsValid=false;
		if(!focus)
		{
			Phone.focus();
			focus = true;
		}
	    return;
    }		
    else
    {
	    var ValidChars = "0123456789.()- ";
	    var IsCorrect=true;
	    var Char;
		var incoming=Phone.value;
        var IsValid=false;
        
	    for (cont = 0; cont < incoming.length && IsCorrect == true; cont++) 
	    { 
	        Char = incoming.charAt(cont); 
	        if (ValidChars.indexOf(Char) == -1) {
		     source.errormessage = 'Please, enter a valid phone number.';
	         arguments.IsValid= false;
			if(!focus)
			{
				Phone.focus();
				focus = true;
			}
	             return;
	        }
	    }
	 }
	 arguments.IsValid= true;
}

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

function ValidateFullName(source, arguments)
{

    var FullName = getElementContact(source.controltovalidate);
    if (FullName.value == "")
    {
	    source.errormessage = 'Please, enter your Name.';
	   
	    arguments.IsValid=false;
		FullName.focus();
		focus = true;
	    return;
    }
   else
    {
		if (!ValidateCharacter(FullName.value))
		{
		 source.errormessage = "Please, enter a valid name.";
		FullName.focus();
	        arguments.IsValid=false;
	        return;
		}
		if(!ValidateConsonants(FullName.value))
		{
		 source.errormessage = "Please, enter a valid name.";
		FullName.focus();
	        arguments.IsValid=false;
	        return;
		}		
    }
    arguments.IsValid=true;
}

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

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

function ValidateZipCode(source, arguments)
{
    var ZipCode = getElementContact(source.controltovalidate);
    if (ZipCode.value == "")
    {
	    source.errormessage = 'Please, enter your ZipCode.';
	   
	    arguments.IsValid=false;
		if(!focus)
		{
			ZipCode.focus();
			focus = true;
		}
	    return;
    }		
   else
    {
	    var ValidChars = "0123456789";
	    var IsCorrect=true;
	    var Char;
		var incoming=ZipCode.value;
        var IsValid=false;
        
	    for (cont = 0; cont < incoming.length && IsCorrect == true; cont++) 
	    { 
	        Char = incoming.charAt(cont); 
	        if (ValidChars.indexOf(Char) == -1) {
		     source.errormessage = 'Please, enter a valid ZipCode.';
	         arguments.IsValid= false;
			if(!focus)
			{
				ZipCode.focus();
				focus = true;
			}
	             return;
	        }
	    }
	 }
    arguments.IsValid=true;
}

function ValidateHearAboutUs(source, arguments)
{

    var ddlHearAboutUs = getElementContact(source.controltovalidate);
        
	if (ddlHearAboutUs.selectedIndex <= 0)
	{
		source.errormessage = "Please select 'How did you hear about us?' option.";
		arguments.IsValid=false;
		if(!focus)
		{
			ddlHearAboutUs.focus();
			focus = true;
		}
	    return;
	}
	else
	{
		if(ddlHearAboutUs.value == "Other")
		{
			var txtOther = getElementContact("txtOther");
			if(trim(txtOther.value) == "")
			{
				source.errormessage = "Please enter your 'How did you hear about us?'.";
				arguments.IsValid=false;
				if(!focus)
				{
					txtOther.focus();
					focus = true;
				}
			    return;
			}
		}
	}
    arguments.IsValid=true;
}

function ConfirmOther()
{
	var ddlHearAboutUs = getElementContact("ddlHearAboutUs");
	var txtOther = getElementContact("txtOther");
	
	if(ddlHearAboutUs.value == "Other")
	{
		var opt = document.createElement("option");
		ddlHearAboutUs.options.add(opt);
		opt.text = txtOther.value;
        opt.value = txtOther.value;
		ddlHearAboutUs.value = txtOther.value;
	}
}

function ValidateComments(source, arguments)
{

    var Comments = getElementContact(source.controltovalidate);
    if (Comments.value == "")
    {
	    source.errormessage = 'Please, enter your Comments.';
	   
	    arguments.IsValid=false;
		if(!focus)
		{
			Comments.focus();
			focus = true;
		}
	    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.';
    	   
	        arguments.IsValid=false;
			if(!focus)
			{
				Email.focus();
				focus = true;
			}
	        return;
        }
        else
        {
        if(!ValidateEmail(Email.value))
	        {
		        source.errormessage = "Please check the emails address.";
    		 
		        arguments.IsValid=false;
				if(!focus)
				{
					Email.focus();
					focus = true;
				}
		        return;
	        }
        }
        arguments.IsValid=true;

    }    
}

function ValidateCharacter(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;
}
