// hide/display the input value 'Search...' of the top right form
function focusTopSearch() {
    var value = $('input[name=s]').val();
    $("input[name=s]").focus(function(){
       if(this.value == value ) {
            this.value = '';
       }
       
        $("input[name=s]").blur(function(){
            if(this.value == '') {
                this.value = value;    
            }    
        });
    });    
}

// check to see if the search input has min 2 chars to make the search
function minInputTopSearch(params) {
    $('#topSearch').submit(function(){
        if($('input[name=s]').val().length <2 || $('input[name=s]').val() == params) {
            alert('Please enter minimum 2 characters.');
            return false;
        }   
    });    
}

// validate the post a comment form

//function to check valid email address
function isValidEmail(string){
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(string)){
        return true;
    }
    return false;
}

function commentformValidation() {
    $('#commentform').submit(function(){
        $('#jq_comment_errors').stop(true,true).slideUp(500);
        var message = '';
        if($('#author').val().length<3) {
            message += 'Complete your name.\n <br/>';
        }
        if(!isValidEmail($('#email').val())) {
            message += 'Complete your email.\n <br/>';
        }
        if($('#comment').val().length<2) {
            message += 'Complete your comment.\n <br/>';
        }
        if(message != '') {
            $('.post_comment h2').after('<div id="jq_comment_errors">'+message+'</div>');
            $('#jq_comment_errors').stop(true,true).slideDown(500);
            return false;
        }
         
    });
}


$(document).ready(function(){
    
    // get the default search input value
    var default_search_value = $('input[name=s]').val();
    minInputTopSearch(default_search_value);
    
    focusTopSearch();
    
    commentformValidation();
    
    
});