﻿function submitAdditionalSearch() {
    var text = $('input[name=txtAdditionalSearchText]').val();
    if (text != "") {
        $('input[name=searchtext]').val($('input[name=searchtext]').val() + ($('input[name=searchtext]').val() == "" ? "" : " ") + text);
        document.getElementById('additionalSearchForm').submit();
    }
}


$(document).ready(function() {

    var searchFieldDefault = $('input[name=txtAdditionalSearchText]').val();

    $('input[name=txtAdditionalSearchText]').focus(function() {
        if ($(this).val() == searchFieldDefault || $(this).val() == '') {
            $(this).val('');
        }
    }).blur(function() {
        if ($(this).val() == '') {
            $(this).val(searchFieldDefault);
        }
    });

    $("input[name=txtAdditionalSearchText]").keyup(function(event) {

        if (event.keyCode == 13) {
            event.preventDefault();
            submitAdditionalSearch();
        }
    });
});


var itemIdAddedToCart;
var globalCurrentLanguage = "";
var globalCurrentCultureCode = "";
var globalMsgProductsInYourBasket = "";

function AddItemToCartFromList(itemId) {
    
    $('#addToCart_' + itemId).hide();
    $('#ajaxloader_' + itemId).show();
    itemIdAddedToCart = itemId;
    PageMethod("AddItemToCartOnlyIfSimple", [
        "itemId", itemId,
        "languageCode", globalCurrentLanguage
            ],
                    AddItemToCartOnlyIfSimpleSucceeded,
                    AddItemToCartOnlyIfSimpleError);
}

function AddItemToCartOnlyIfSimpleSucceeded(result) {
    if (result.d == '') {
        GetHtmlProductListCart();
        RefreshQtyBasket();
        document.getElementById('frmAddToCart').src = itemIdAddedToCart.split("_")[0];
    }
    else {
        ReloadLocation(result.d);
    }
}

function AddItemToCartOnlyIfSimpleError()
{ }

function GetHtmlProductListCart() {
    PageMethod("GetHtmlProductListCart", [
        "currentCultureCode", globalCurrentCultureCode
            ],
                    GetHtmlProductListCartSucceeded,
                    GetHtmlProductListCartError);
}
function GetHtmlProductListCartSucceeded(result) {
    $('span[id=CartItems]').html(result.d.HtmlItems);
    $('div[id=CartItemsQty]').html(result.d.Total + "&nbsp;" + globalMsgProductsInYourBasket);
    openAkBox('addProduct');
    $('#addToCart_' + itemIdAddedToCart).show();
    $('#ajaxloader_' + itemIdAddedToCart).hide();
}
function GetHtmlProductListCartError() {
    $('#addToCart_' + itemIdAddedToCart).show();
    $('#ajaxloader_' + itemIdAddedToCart).hide();
}
