﻿$(document).ready(function() {

    //Set the default button
    var $form = $('#contact');
    $form.keypress(function(e){         
        if (e.which == 13 && e.target.type != 'textarea' && e.target.type != "") {                               
           sendMail();
        }
    });    

    $("#btnSubmit").click(function(){    
        sendMail(); 
    })
    
})

function sendMail(){

        var errors = "";
    
        if($("#txt_yourName").val() == ""){
            errors = "** Please enter your name **";
        }
        
        if($("#txt_email").val() == ""){
            if(errors != "") errors += "<br />";
            errors += "** Please enter a valid e-mail address **";
        }else{
            if(!isValidEmail($("#txt_email").val())){
                if(errors != "") errors += "<br />";
                errors += "** Please enter a valid e-mail address **";            
            }
        }
        
        if($("#txt_comments").val() == ""){
            if(errors != "") errors += "<br />";
            errors += "** Please enter a comment **";        
        }

        if(errors != ""){
            $("#registerErrors").html(errors);
            $("#registerErrors").slideDown();
        }else{        
            //Lets send an Email
            $.get("Handler/Handler.ashx", {"action": "SendContactEmail", "Name": $("#txt_yourName").val(), "Telephone": $("#txt_telephone").val(), "Email": $("#txt_email").val(), "Comments": $("#txt_comments").val(), "Refresh": new Date().getTime()}, function(data){            
                $("#contact").hide();
                $("#ContactDone").show();                        
            })        
        }

}

function isValidEmail(Email){

    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!(filter.test(Email))){
        return false;
    }else{
        return true;
    }       

}

