$(function() 
{
	
	$(".submit").click(function() 
	{
			
		var name = $("input#name").val();  
		var phone = $("input#phone").val(); 
		var email = $("input#email").val(); 
		var body = $("textarea#body").val(); 
		
		var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&body=' + body;
		
		$.ajax({
			type: "POST",
			url: "sendmail.php",
			data: dataString,
			
			success: function() {

				var message;							
								
				if((phone != " " && phone != "phone") && (email != " " && email != "email")){								
					if(validEmail(email)){
						message = "<div id='h3Send' name='h3Send'>Message sent!</div>";
					}
					else{
						message= "<div id='h3Send' name='h3Send'>Email Incorrect!</div>";
					}					
				}
				else{
					if((phone == " " || phone == "phone") && (email != " " && email != "email"))
					{
						message= "<div id='h3Send' name='h3Send'>Phone required!</div>";
					}
					else
					{
						if((phone != " " && phone != "phone") && (email == " " || email == "email"))
						{
							message= "<div id='h3Send' name='h3Send'>Email required!</div>";
						}
						else
						{
							message= "<div id='h3Send' name='h3Send'>Phone or email required!</div>";
						}
					}								
				}

													
				$('#contentmessages').html(message)
				
				.hide()
				.fadeIn(1500, function() {
					//$('#message').append("<img id='checkmark' src='image/check.png' />");
				});
				
			}
		});
		return false;
	});
	return false;
});

// Function for Validate email in JS
function validEmail(mail) {
    invalidChars = " /:,;"

    if (mail == "") { // cannot be empty
        return false
    }
    for (i=0; i<invalidChars.length; i++) { // does it contain any invalid characters?
        badChar = invalidChars.charAt(i)
        if (mail.indexOf(badChar,0) > -1) {
            return false
        }
    }
    atPos = mail.indexOf("@",1) // there must be one "@" symbol
    if (atPos == -1) {
        return false
    }
    if (mail.indexOf("@",atPos+1) != -1) { // and only one "@" symbol
       return false
    }
    periodPos = mail.indexOf(".",atPos)
    if (periodPos == -1) { // and at least one "." after the "@"
        return false
    }
    if (periodPos+3 > mail.length) { // must be at least 2 characters after the "."
        return false
    }
    return true
}





 

