﻿var defaultText = 'Nhập từ khóa cần tìm';
function btnSearch_Click()
{
    var value = document.getElementById('txtSearchKeyWord').value;
    window.location.href = appPath + 'tim-kiem.html?q=' + encodeURIComponent(value);
    return false;
}

function processEnter(e) {
    
    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    var target = e.target.tagName.toLowerCase();
        if (key === 13 && target === 'input') {
            if ($('#txtSearchKeyWord').val() != '') {
                e.preventDefault();            
                $('#btnSearch').trigger('click');
            }
            else {
                e.preventDefault();
                alert('Vui lòng nhập từ khóa tìm kiếm');       
            }
    }
}

$(document).ready(function() {
    $('#txtSearchKeyWord').keypress(function(e){
        processEnter(e)
    });  
    
    if ($('#txtSearchKeyWord').val() == '')
        $('#txtSearchKeyWord').val(defaultText);  
    
    $('#txtSearchKeyWord').focus(function(){
       if ($('#txtSearchKeyWord').val() == defaultText)
       {
            $('#txtSearchKeyWord').val('')
       }
    });    
    
    $('#txtSearchKeyWord').blur(function(){
       if ($('#txtSearchKeyWord').val() == defaultText || $('#txtSearchKeyWord').val() == '')
       {
            $('#txtSearchKeyWord').val(defaultText)
       }
    });  
});
