function replace(texto,s1,s2)
{
    return texto.split(s1).join(s2);
}

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

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

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

function ValidatePhone(valor)
{
    var ValidChars = "0123456789.()- ";
    var IsCorrect=true;
    var Char;
    var IsValid=false;
    
    for (cont = 0; cont < valor.length && IsCorrect == true; cont++) 
    { 
        Char = valor.charAt(cont); 
        if (ValidChars.indexOf(Char) == -1) {
             return false;
        }
    }
    
    return true;
}

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

function ValidateDataSmall(FName,LName,Email,Phone,Comment,contactDate)
{
    FName.value=trim(FName.value);
    LName.value=trim(LName.value);
    Email.value=trim(Email.value);
    Phone.value=trim(Phone.value)
    Comment.value=trim(Comment.value);
    
	if (contactDate.value != "")
	{						
		return (false);
	}
	
	if (FName.value == "")
	{
		alert('Please, enter your first name.');
		FName.focus();
		return (false);
	}
	else
	{
		if(HasNumbers(FName.value))
		{
			alert('First name contains numbers, please remove them.');
			FName.focus();
			return (false);
		}
	}
	
	if (LName.value == "")
	{
		alert('Please, enter your last name.');
		LName.focus();
		return (false);
	}
	else
	{
		if(HasNumbers(LName.value))
		{
			alert('Last name contains numbers, please remove them.');
			LName.focus();
			return (false);
		}
	}
	
	if (Email.value == "")
	{
		alert('Please, enter your email.');
		Email.focus();
		return (false);
	}
	else
	{
		if(!ValidateEmail(Email.value))
		{
			alert("Please check the email address.");
			Email.focus();
			return false;
		}
	}
	
	if (Phone.value == "")
	{
		alert('Please, enter your phone.');
		Phone.focus();
		return (false);
	}
	else
	{
		if(!ValidatePhone(Phone.value))
		{
			alert("Please check your phone.");
			Phone.focus();
			return false;
		}
	}
		
	if (Comment.value == "")
	{
		alert('Please, enter your comments.');
		Comment.focus();
		return (false);
	}
	
	return true;
}

function OnSubmit()
{
    var FName=getSmallElement('txtFirstName');
	var LName=getSmallElement('txtLastName');
	var Email=getSmallElement('txtEmail');
	var Phone=getSmallElement('txtPhone');
	var Comment=getSmallElement('txtComments');	
	var contactDate=getSmallElement('contactDate');
	var hdnContactFormID=getSmallElement('hdnContactFormID');
	var hdnContactFormType=getSmallElement('hdnContactFormType');
	
	
	if (ValidateDataSmall(FName,LName,Email,Phone,Comment,contactDate)==true)
	{
		window.location.href="savesmallform.aspx?txtFirstName="+FName.value+"&txtLastName="+LName.value+"&txtEmail="+Email.value+"&txtPhone="+Phone.value+"&txtComments="+Comment.value+"&hdnContactFormID="+hdnContactFormID.value+"&hdnContactFormType="+hdnContactFormType.value;
	}
}

function getSmallElement(name)
{
    var object = null;
    var tbContact=document.getElementById('tbSmallContact'); 
    return findElement(tbContact, name)
}

function findElement(container, name)
{

    for (var i=0; i<container.childNodes.length; i++)
    {
        child = container.childNodes[i];
        if(child.id == name)     
           return child;
       
        object=findElement(child, name);
        if (object!=null)
            return object;      
    }  
    return null;  
}

function HasNumbers(valor) 
{
	if (/[0-9]/.test(valor))
			return true;
	else
			return false;
}
