
//------------------- for clearing and replacing text in form input fields and textareas -------------------//
function clearText(thefield) {
  if (thefield.defaultValue==thefield.value) { thefield.value = "" }
} 
function replaceText(thefield) {
  if (thefield.value=="") { thefield.value = thefield.defaultValue }
}

function ValidateDataSmall(FirstName, LastName, Email, Comments, contactDate)
{
	if (contactDate.value != "")
	{
		return false;
	}
	
	if ((FirstName.value == "") || (FirstName.value == '* First Name'))
	{
		alert('Please, enter your First Name.');
		FirstName.focus();
		return false;
	}
	else
	{
		if(HasNumbers(FirstName.value))
		{
			alert('Name contains numbers, please remove them.');
			FirstName.focus();
			return (false);
		}
	}
	
	if ((LastName.value == "") || (LastName.value == '* Last Name'))
	{
		alert('Please, enter your Last Name.');
		LastName.focus();
		return false;
	}
	else
	{
		if(HasNumbers(LastName.value))
		{
			alert('Name contains numbers, please remove them.');
			LastName.focus();
			return (false);
		}
	}
	
	if ((Email.value == "") || (Email.value == '* Email'))
	{
		alert('Please, enter your email.');
		Email.focus();
		return false;
	}
	else
	{
		if(!ValidateEmail(Email.value))
		{
			alert("Please check the emails address");
			Email.focus();
			return false;
		}
	}
		
	if ((Comments.value == "") || (Comments.value == '* Comment'))
	{
		alert('Please, enter your comments.');
		Comments.focus();
		return false;
	}
	
	if  ( 
			hasSpamStrings(FirstName) ||						
			hasSpamStrings(LastName)  ||				
			hasSpamStrings(Comments) 			
		)
	{
		return false;
	}
	
	return true;
}

function hasSpamStrings(textField)
{

	var fieldValue = textField.value;
	
	if (	
			fieldValue.indexOf('@')			!=	-1 	|| 
			fieldValue.indexOf('http://')	!=	-1
		)
	{
			alert('Please, enter a valid value.');
			textField.focus();
			return true;
	}	
	else
	{
			return false;
	}

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

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 HasNumbers(valor) 
{
    if (/[0-9]/.test(valor))
            return true;
    else
            return false;
}
    
function getElement(name)
{
	var object = null;
	var tbSmallContact=document.getElementById('tbSmallContact'); 
	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;
                }
            }
        }
        
    }    
}
