    var miniBasketTimeout;
    var isJustHideMiniBasket = false; //tungnt fix minibasket show/hide continuous
    function getId(str){
            var str = str.split("_");
            
            return str[1].toUpperCase();
    }

    function dimScreenCheckout(div_id, opac){
        var wW = $(window).width();
        var wH = $(document).height();
        
        if(div_id != "" && $("#"+div_id)){
            wH = Math.max($(document).height(),pY+$("#"+div_id).height());
        }
        
        if(opac == "")opac = "0.7";
        
        if(!$("#dim-bg").is(":visible")){
            $('body').prepend("<div id='dim-bg'></div>");
            $('#dim-bg').css({display:'block',opacity: opac, 'width':wW+'px','height':wH+'px'});
        }
    }

    function dimScreen(div_id, opac){
        var wW = $(window).width();
        var wH = $(document).height();
        
        if(div_id != "" && $("#"+div_id)){
            wH = Math.max($(document).height(),pY+$("#"+div_id).height());
        }
        
        if(opac== "")opac = "0.7";
        
        if(!$("#dim-bg").is(":visible")){
            $('body').prepend("<div id='dim-bg'></div>");
            $('#dim-bg').css({display:'block',opacity: opac, 'width':wW+'px','height':wH+'px','position':'absolute','z-index':9998,'background':'#000'});            
            $("#dim-bg").click(function(){
                hideMiniBasket();
            })
        }
    }
    
    function removeDim(){

        if($("#cvc_popup") && $("#cvc_popup").is(":visible")){
            $("#cvc_popup").slideUp();
        }

        if($("#what_is_this_cardtype") && $("#what_is_this_cardtype").is(":visible")){
            $("#what_is_this_cardtype").slideUp();
        }

        if($("#newsletter_what_popup") && $("#newsletter_what_popup").is(":visible")){
            $("#newsletter_what_popup").slideUp();
        }

        $("#dim-bg").fadeOut("fast", function(){
            $("#dim-bg").remove();        
        });

    }

    function backToTop() {
        var x1 = x2 = x3 = 0;
        var y1 = y2 = y3 = 0;
    
        if (document.documentElement) {
            x1 = document.documentElement.scrollLeft || 0;
            y1 = document.documentElement.scrollTop || 0;
        }
    
        if (document.body) {
            x2 = document.body.scrollLeft || 0;
            y2 = document.body.scrollTop || 0;
        }
    
        x3 = window.scrollX || 0;
        y3 = window.scrollY || 0;
    
        var x = Math.max(x1, Math.max(x2, x3));
        var y = Math.max(y1, Math.max(y2, y3));
    
        window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));
    
        if (x > 0 || y > 0) {
            window.setTimeout("backToTop()", 25);
        }
    }
    
    
    function removeItemMini(input_id){
		
        item_id = input_id;
        updateItemActionP(item_id, 0, "addTo");
        
    }
    
    function updateItemMini(input_id){
        item_id = input_id; 
        quantity = $("#inputmini_"+item_id).val();
        updateItemActionP(item_id, quantity, "addTo");

    }
    
    function updateMainBasket(){ // use if change quantity in mini basket @ basket page - temp
        var url = site_url + "ajax/main_basket";

           $.ajax({
            type:"POST",
            url:url,
            dataType:"text",
            success: function(html){
                $("#shopcart_content").html(html)
            }
        })           
    }

    function addItemToBasketAll(input_id, quantity){
        item_id = getId(input_id);

        updateItemActionP(item_id, quantity, "addMore");

    }
    
    function update_minisum(){
        total_item = $("#total_item_p").val(); 
        total_price = $("#total_price_p").val();
        $("#header_items").html(total_item);
        $("#header_price").html(total_price);
    }
    function showMiniBasket(){
        
        dimScreen("", 0.7);
        $("#mini_basket").show("blind", { direction: "vertical" }, 1000);
        
        miniBasketTimeout=setTimeout(hideMiniBasket,5000);
         
		bind_event_update_minibasket();
        isJustHideMiniBasket = false;
    }
    
    function hideMiniBasket(){
        
        clearTimeout(miniBasketTimeout);
        
        removeDim();

        $("#mini_basket").hide("blind", { direction: "vertical" }, 1000);
        
        if(miniBasketTimeout){
            clearTimeout(miniBasketTimeout);
        }
        isJustHideMiniBasket = true;
    }
    
    function updateItemActionP(item_id, quantity, action){ 
		//var url = site_url + "ajax/add_from_product/"+item_id + "/" + quantity + "/" + action;
        //var total_item = 0;
        //var total_price = 0;
        //var mini_basket_sum = "";
        //var item = msg_item;
           $.ajax({
            type:"POST",
            url:site_url + "ajax/add-product-to-basket",
            data:({attribute_id:item_id,quantity:quantity,action:action}),
            dataType:"json",
            success: function(data){
                if(data.success == 1)
                {
                    basketdata = data.cart;
                    parse_basket(basketdata);
                    if(show_mini_basket_after_change)
                    {
                        showMiniBasket(true);
                    }
                }
                if(data.show_msg == 1)
                {
                    show_popup(data.msg_title, data.msg_content);
                }
            }
        })    
    }
    
    function show_popup(title,text,popupY){
        //alert("Hi");
    	if($("#popup-box").css('display') != 'none') return false;
        //alert("Hi");  
        if($("#c_subject")){
            $("#c_subject").hide();
        }
        $('html, body').animate({scrollTop:0}, 'fast');//scroll to top
        var wW = $(window).width();
        var wH = $(window).height();
        var pW = $("#newsletter-box").width();        //Width of the popup
        var pX = (wW-pW)/2;    //X coodinate of the popup
        var pY = 117;

        //var pY = ((popupY!=null)&&(parseInt(popupY)+''!='NaN'))?parseInt(popupY):250;    //Y coodinate of the popup
        var wH = Math.max($(document).height(),pY+$("#popup-box").height());
                  
      

        $("#popup-title").html(title);
        $("#popup-text").html(text);
        $('body').prepend("<div id='popup-bg'></div>");
        $('#popup-bg').css({display:'block',opacity: 0.7, 'width':wW+'px','height':wH+'px'});
        //$("#popup-box").css({display:'block',top:pY+'px',left:pX+'px',width:pW+'px'});
        $("#popup-box").css({display:'block'});
        set_popup_at_center("#popup-box",99999);
        $("#close-link, #close-image, #popup_close").click(function(){
            hide_popup();
        })
    }
    
    function hide_popup() {
        if($("#c_subject")){
            $("#c_subject").show();
        }
		
        if($("#error_msg_popup")){
            $("#error_msg_popup").show();
        }

        $("#popup-box").css('display','none');
        $("#popup-bg").css('display','none');
        $("#popup-bg").remove();        
    }
    
    /*
    Set an element HTML at the center of the screen
    Params: string of class or id; OR an jquery object of element
    */
    function set_popup_at_center(elementPopup, zindex){
        var wW = $(window).width();
        var wH = $(window).height();
        var pW = $(elementPopup).width();
        var pH = $(elementPopup).height();
        var pX = (pW >= wW) ? 0 : (wW-pW)/2;    //X coodinate of the popup
        var pY = (pH >= wH) ? 0 : (wH-pH)/2;    //Y coodinate of the popup
        $(elementPopup).css({'left':pX,'top':pY,'z-index':zindex});        
    }
    
    function format_price(price,currency){
        html = '';
        price = isNaN(price) ? 0 : price;
        currency_decimal_point = (typeof currency.currency_decimal_point != 'undefined') ? currency.currency_decimal_point : 0; 
        currency_sign_before = (typeof currency.currency_sign_before != 'undefined' && currency.currency_sign_before !='') ? currency.currency_sign_before+'&nbsp;' : '';
        currency_sign_after = (typeof currency.currency_sign_after != 'undefined' && currency.currency_sign_after !='') ? '&nbsp;'+currency.currency_sign_after : '';
        currency_float_point = typeof currency.currency_float_point != 'undefined' ? currency.currency_float_point : '';
        new_price = number_format(price, currency_decimal_point);        
        int_part = Math.floor(new_price);
        float_part = number_format(new_price - int_part,currency_decimal_point);
        float_part = float_part.substring(2);        
        float_part = float_part.length>0 ? '<sup>'+float_part+'</sup>' : '';
        new_value = currency_sign_before + int_part + currency_float_point + float_part + currency_sign_after;        
        return new_value;
    }

    function price_helper(price,sale_price,is_active,currency,sale_price_tag,sale_price_class,price_tag,price_class){
        html = '';
        if(is_active == 1 && sale_price > 0){
            html += '<' + price_tag + ' class="'+price_class+'">';
            html += currency.currency_sign_before != '' ? currency.currency_sign_before + '&nbsp;' : '';
            html += get_currency_format(price,currency);
            html += '</' + price_tag + '>';
            html += '<' + sale_price_tag + ' class="'+sale_price_class+'">';
            html += get_currency_format(sale_price,currency);
            html += currency.currency_sign_after != '' ? '&nbsp;' + currency.currency_sign_after : '';
            html += '</'+ sale_price_tag + '>';
        } else {
            html += '<' + price_tag + '>';
            html += currency.currency_sign_before != '' ? currency.currency_sign_before + '&nbsp;' : '';
            html += get_currency_format(price,currency);
            html += currency.currency_sign_after != '' ? '&nbsp;' + currency.currency_sign_after : '';
            html += '</' + price_tag + '>';
        }
        return html;
    }
 
     function format_price_only(price,currency){
        html = '';
        price = isNaN(price) ? 0 : price;
        currency_decimal_point = (typeof currency.currency_decimal_point != 'undefined') ? currency.currency_decimal_point : 0; 
        currency_sign_before = (typeof currency.currency_sign_before != 'undefined' && currency.currency_sign_before !='') ? currency.currency_sign_before+'&nbsp;' : '';
        currency_sign_after = (typeof currency.currency_sign_after != 'undefined' && currency.currency_sign_after !='') ? '&nbsp;'+currency.currency_sign_after : '';
        //currency_float_point = typeof currency.currency_float_point != 'undefined' ? currency.currency_float_point : '';
        new_price = number_format(price, currency_decimal_point);        
        int_part = Math.floor(new_price);
        float_part = number_format(new_price - int_part,currency_decimal_point);
        float_part = float_part.substring(2);        
        float_part = float_part.length>0 ? '<sup>'+float_part+'</sup>' : '';
        return int_part + float_part;        
    }

    function price_helper_only(price,sale_price,is_active,currency,sale_price_tag,sale_price_class,price_tag,price_class){
        html = '';
        if(is_active == 1 && sale_price > 0){
            html += '<' + price_tag + ' class="'+price_class+'">';
            html += currency.currency_sign_before != '' ? currency.currency_sign_before + '&nbsp;' : '';
            html += format_price_only(price,currency);
            html += '</' + price_tag + '>';
            html += '<' + sale_price_tag + ' class="'+sale_price_class+'">';
            html += format_price_only(sale_price,currency);
            html += currency.currency_sign_after != '' ? '&nbsp;' + currency.currency_sign_after : '';
            html += '</'+ sale_price_tag + '>';
        } else {
            html += '<' + price_tag + '>';
            html += currency.currency_sign_before != '' ? currency.currency_sign_before + '&nbsp;' : '';
            html += format_price_only(price,currency);
            html += currency.currency_sign_after != '' ? '&nbsp;' + currency.currency_sign_after : '';
            html += '</' + price_tag + '>';
        }
        return html;
    }
    
    function get_currency_format(amount,obj_currency){
        currency_sign_before = obj_currency.currency_sign_before;
        currency_sign_after = obj_currency.currency_sign_after;
        currency_decimal_point = obj_currency.currency_decimal_point;
        formated_price = '';
        //formated_price = currency_sign_before;
        //formated_price += (currency_sign_before != '') ? " " : "";
        formated_price +=  number_format(amount,currency_decimal_point);
        //formated_price +=  (currency_sign_after != '') ? " " : "";
        //formated_price +=  currency_sign_after;
        return formated_price;
        
    }

function number_format (number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands
    //
    // version: 906.1806
    // discuss at: http://phpjs.org/functions/number_format    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival    // +     input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +     improved by: davook
    // +     improved by: Brett Zamir (http://brett-zamir.me)
    // +     input by: Jay Klehr
    // +     improved by: Brett Zamir (http://brett-zamir.me)    // +     input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'    // *     example 10: number_format('1.20', 2);
    // *     returns 10: '1.20'
    // *     example 11: number_format('1.20', 4);
    // *     returns 11: '1.2000'
    // *     example 12: number_format('1.2000', 3);    // *     returns 12: '1.200'
    var n = number, prec = decimals;
 
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);        return (Math.round(n*k)/k).toString();
    };
 
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
 
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
     var abs = toFixedFix(Math.abs(n), prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);    } else {
        s = s.replace('.', dec);
    }
 
    var decPos = s.indexOf(dec);    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';    }
    return s;
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}          

/*--start functions process newsletter---*/
    function show_newsletter(){        
        if($("#newsletter-box").css('display') != 'none') return false;
        var wW = $(window).width();
        var wH = $(window).height();
        var pW = $("#newsletter-box").width();
        var pY = 117;        //Y coodinate of the popup
        var pX = (wW-pW)/2;    //X coodinate of the popup
        

        //var pY = ((popupY!=null)&&(parseInt(popupY)+''!='NaN'))?parseInt(popupY):250;    //Y coodinate of the popup
        var wH = Math.max($(document).height(),pY+$("#newsletter-box").height());
        
        $('body').prepend("<div id='newsletter-bg'></div>");
        $('#newsletter-bg').css({display:'block',opacity: 0.7, 'width':wW+'px','height':wH+'px'});
        $("#newsletter-box").css({display:'block',top:pY+'px',left:pX+'px'});
        $("#strFirstName").val('');
        $("#strLastName").val('');
        $("#strCity").val('');
        $("#strZip").val('');
        $("#radio_m").removeAttr('checked');
        $("#radio_f").attr('checked','checked');
        if(!$('#newsletter-box form').hasClass('jNice')) $('#newsletter-box form').addClass('jNice');
        $('#newsletter-box form.jNice').jNice();
    }

    function hide_newsletter() {
        $("#newsletter-box").css('display','none');
        $("#newsletter-bg").css('display','none');
        $("#newsletter-bg").remove();
        if($('#newsletter-box form').hasClass('jNice')) $('#newsletter-box form').removeClass('jNice');
        $("#nMsg").html('');
    }
/*--end function process newsletter--*/
    
    function validEmail_home(str){  
        var emailRegEx = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(str.match(emailRegEx)) return true;    
        return false;
    } 
    
        
    $(document).ready(function() {     
        $('#newsletter-email').keypress(function(e) {
            if(!e) e = window.event;
            key_code = (e.keyCode ? e.keyCode : e.which);
            if(key_code == 13) {
                email = $.trim($(this).val());
                if (email != ''){
                    if(!validEmail_home(email)){  
                        show_popup(msg_error,gettext.msg_email_invalid);
                    } else {
                        $("#strEmail").val(email);
                        scroll(0,0);
                        show_newsletter();
                    }
                }
                return false;
            }
        })
        
        //this move from function showMiniBasket
        miniBasketTimeout = null;        
        $("#mini_basket").bind("mouseenter",function(){                   
            clearTimeout(miniBasketTimeout);            
        }).bind("mouseleave",function(){            
            if(!isJustHideMiniBasket){
                miniBasketTimeout=setTimeout(hideMiniBasket,5000);
            }             
        });
    });

    
function bind_event_update_minibasket(){
    $("#close-mini2").click(hideMiniBasket);
    $("#mini_to_basket").click(function(){window.location = site_url+'basket';});
    // $("#mini_to_checkout").click(function(){window.location = shop_secure_site_url+'/checkout';});
    $("#mini_to_checkout").click(function(){window.location = site_url+'checkout';});
}

//MinhBL: Value validation ->
function is_email(str,trim_spaces) {
    trim_spaces = typeof trim_spaces != 'undefined' ? trim_spaces : true;
    if(trim_spaces) str = $.trim(str);
    email_pattern = /^[a-zA-Z]+([+_.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)*\.[a-zA-Z]{2,4}$/;
    return email_pattern.test(str);
}
function is_number(str,trim_spaces) {
    trim_spaces = typeof trim_spaces != 'undefined' ? trim_spaces : true;
    if(trim_spaces) str = $.trim(str);
    if(str != $.trim(str)) return false;
    return !isNaN(str);
}
function is_integer(str,trim_spaces) {
    trim_spaces = typeof trim_spaces != 'undefined' ? trim_spaces : true;
    if(!is_number(str,trim_spaces)) return false;
    return str == parseInt(str,10);
}
//MinhBL: Value validation <-

//hongtt change country notice popup
function change_country_process(selector) {
    if(basketdata.total.items > 0) {
        show_country_notice(gettext.Notice, gettext.change_country_msg);
        $("#confirm_yes").click(function() {
             //window.location.replace(url);

            $.ajax({
                url:site_url+'ajax/clear_basket',
                success: function(html){
                     selector.form.submit();
                     return false;
                }
            })           
    
        });
    }
    else{
        //window.location.replace(url);
        selector.form.submit();
        return false;
    }
    return true;
}

function show_country_notice(title,msg) {
    $('html, body').animate({scrollTop:0}, 'fast');//scroll to top
    var wW = $(window).width();
    var wH = $(window).height();
    var pW = $("#error-container").width();        //Width of the popup
    var pX = (wW-pW)/2;    //X coodinate of the popup
    var pY = 117;
    
    var wH = Math.max($(document).height(),pY+$("#error-container").height());
    
    $("#error-title").html(title);
    $("#notice-text").html(msg);
    $('body').prepend("<div id='popup-bg'></div>");
    $('#popup-bg').css({display:'block',opacity: 0.7, 'width':wW+'px','height':wH+'px'});
    $("#error-container").css({display:'block'});
    set_popup_at_center("#error-container",9999);
    $("#close-notice, #confirm_no").click(function(){
        parse_change_country();
        hide_country_notice();
    })
}

function parse_change_country()
{
    try{
        var change_country_html = '';
        $.each(change_country_data, function(i, country){
            if(country.id == store.id)
            {
                change_country_html += '<option value='+country.url+' selected="selected" >'+country.name+'</option>';
            }
            else
            {
                change_country_html += '<option value='+country.url+'>'+country.name+'</option>';
            }
        })
        $('#dr_country').html(change_country_html);
        $.jNice.SelectUpdate($('#dr_country'));
    }
    catch(err){}
}

function hide_country_notice() {
    $("#error-container").css('display','none');
    $("#popup-bg").css('display','none');
    $("#popup-bg").remove();        
}
//hongtt end change country notice

/*---hongtt refactor function subcribe of signup and thankyou and move here.--*/
function subcribeNewsletter(firstName, lastName, city, country, day, month, year, email, gender, msg_place)
{
    var dayExp = /^\d{1,2}$/;
    var monthExp = /^\d{1,2}$/;
    var yearExp = /^\d{4}$/;
    var errorFields = new Array();
    
    if(firstName == '') errorFields.push(gettext.err_first_name);
    if(lastName == '') errorFields.push(gettext.err_last_name);
    if(city == '') errorFields.push(gettext.err_city);
    if(country=='') errorFields.push(gettext.err_country);
    if(dayExp.test(day) == false || 0 > parseInt(day) || 31 < parseInt(day)) errorFields.push(gettext.err_day);
    if(monthExp.test(month) == false || 0 > parseInt(month) || 12 < parseInt(month)) errorFields.push(gettext.err_month);
    if(yearExp.test(year) == false || 1900 > parseInt(year) || 2050 < parseInt(year)) errorFields.push(gettext.err_year);
    if(!validEmail_home(email)) errorFields.push(gettext.err_email); 
    
    var err_fields = errorFields.join(', ');
    
    if(err_fields!='') {
        show_popup(msg_error, gettext.field_msg+'<br/><br/>'+ err_fields);
        return false;
    }

   // backToTop();

    img = cdn_url + "images/ajax_loader.gif";

    $(msg_place).html(gettext.processing_msg);

    $.ajax({
        type: "POST",
        url: site_url+'ajax/subscribe-to-emv',
        dataType: "json",
        data: {billing_email : email, billing_firstname : firstName, billing_lastname : lastName, billing_city : city, billing_country : country, day : day, month : month, year : year, gender : gender },
        success: function(response_html){
            $(msg_place).html(response_html.data);
        }
    });
}
/*--end of function subcribeNewsletter--*/