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

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

function CustomValidateName(source, arguments)
{
    if(arguments)
    {
        var Name = getElementContact(source.controltovalidate);
        var NamePrefix = getElementContact('ddlPrefixName');
        var FullName = getElementContact('txtFullName');
        if (trim(Name.value) == "")
	    {
		    source.errormessage = 'Please, enter your Name.';
		    Name.focus();
		    FullName.value = "";
		    arguments.IsValid=false;
		    return;
	    }
	    else
	    {
	        FullName.value = NamePrefix.value + ' ' + Name.value;
	    }
        arguments.IsValid=true;
    
    }    
}

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

function CustomValidateCity(source, arguments)
{
    if(arguments)
    {
        var City = getElementContact(source.controltovalidate);
        if (trim(City.value) == "")
	    {
		    source.errormessage = 'Please, enter your City.';
		    City.focus();
		    City.value = "";
		    arguments.IsValid=false;
		    return;
	    }
	    
        arguments.IsValid=true;
    
    }    
}
function CustomValidateState(source, arguments)
{
    if(arguments)
    {
        var State = getElementContact(source.controltovalidate);
        if (trim(State.value) == "")
	    {
		    source.errormessage = 'Please, enter your State.';
		    State.focus();
		    State.value = "";
		    arguments.IsValid=false;
		    return;
	    }
	    
        arguments.IsValid=true;
    
    }    
}

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

function ValidateEmail(valor) 
{
    if (/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(valor))
	    return true;
    else
	    return false;
}

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

function CustomValidatePhone(source, arguments)
{
    var PhoneType = getElementContact('ddlPhoneType');
    var AreaCode = getElementContact('txtAreaCode');
    var Prefix = getElementContact('txtPrefix');
    var Suffix = getElementContact('txtSuffix');
    var Phone = getElementContact('txtPhone');
    
    if(trim(AreaCode.value) != "" && trim(Prefix.value) != "" && trim(Suffix.value) != "" && PhoneType.value == "")
    {
        source.errormessage = 'Please, select the phone type.';
        PhoneType.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;
		
		var ValidChars = "0123456789.()- ";
	    var IsCorrect=true;
	    var Char;
        var IsValid=false;
        incoming= Phone.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;
	        }
	    }
    }
    
    if(trim(AreaCode.value) == "" || trim(Prefix.value) == "" || trim(Suffix.value) == "")
    {
        source.errormessage = 'Please, enter a valid Phone Number.';
        PhoneType.focus();
        arguments.IsValid=false;
        return;
    }
    
    arguments.IsValid=true;
}

function ValidateDataContact()
{
	var contactDate=document.getElementById('txtContactcontactDate');
	
	if (contactDate.value != "")
	{						
		return (false);
	}
	
	return true;
}

function OnlyNumbers()
{	
	if(window.event.keyCode < 48 || window.event.keyCode > 57)
	{
		window.event.keyCode = 0;
	}
}

function Error(sender, args) 
{ 
    alert ("Enter a Valid Date in MM/DD/YYYY Format");
    sender.focus();
}