﻿

$(document).ready(function() {  

                           

    // Contact Form

    var cForm = $("#contactForm");  

    var cinputName = cForm.find("#name"); 

    var cinputEmail = cForm.find("#email");  

    var cinputMessage = cForm.find("#message");

    var cloadingImage = cForm.find('#loading'); 

    var cresponseText = cForm.find("#response");



    // On Submitting    

    cForm.bind("submit", function(e){

        if(validateName(e, cinputName) & validateEmail(e, cinputEmail) & validateMessage(e, cinputMessage)) { 

            ajaxSend(cForm, cresponseText, cloadingImage);

        };

        return false;

    });

        

    // On key press  

    cinputName.bind("keyup", function(e){

        validateName(e,  cinputName);

    });

    cinputEmail.bind("keyup", function(e){

        validateEmail(e,  cinputEmail);

    });

    cinputMessage.bind("keyup", function(e){

        validateMessage(e,  cinputMessage);

    });

    

});



// Helper functions requierd by the contact form

function validateName(event, input){  

    if( input.val().length < 4 || input.val() == "Name" ){

        if(event.type != "keyup") {

            input.addClass("error"); 

            input.nextAll('.errorText').slideDown(); 

        }

        return false;

    } else { 

        input.removeClass("error"); 

        input.nextAll('.errorText').slideUp(); 

        return true;

    }  

}



function validateEmail(event, input){

    var a = input.val();

    var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;

    

    if( filter.test(a) ){

        input.removeClass("error"); 

        input.nextAll('.errorText').slideUp(); 

        return true;

    }else{

        if(event.type != "keyup") {

            input.addClass("error"); 

            input.nextAll('.errorText').slideDown(); 

        }

        return false;

    }

} 



function validateMessage(event, input){  

    if( input.val().length < 10 || input.val() == "your message" ){

        if(event.type != "keyup") {

            input.addClass("error"); 

            input.nextAll('.errorText').slideDown(); 

        }

        return false;

    }else{

        input.removeClass("error"); 

        input.nextAll('.errorText').slideUp(); 

        return true;

    } 

} 



function ajaxSend(form, response, loading){ 

    loading.show();

    

    response.slideUp().animate({X:""} , 200, "linear", function(){

        response.html('<small>Please wait, your message is being processed.</small><br />').slideDown();        

    });

    

    // Make AJAX request        

    $.post('sendMail.php', form.serialize(), function(data){

        loading.hide(200);

        response.slideUp().animate({X:""} , 200, "linear", function(){ response.html(data).slideDown(); });

    });



    //Cancel default action

    return false;

};

