function login_callback(data) {

  if (data != 1) {
    alert('Incorrect login or password!');
  } else {
    cms_page_refresh();
  }  

}

function do_login() {

  cms_do_ajax_login($('#login_login').val(), $('#login_password').val(), 0, 'login_callback');

}

function do_login2(e) {

  e = e || window.event || {};

  if (e.keyCode == 13) {
    do_login();
  }  

}

function add_to_cart(caller, amount, productId) {
  
  $(caller).fadeOut();
  $('#cart_items_sign').fadeOut();
  $('#total_items_sign').fadeOut();
  
  var a = $.get( "?__ajaxMethod=add_to_cart"
               , { amount:    amount
                 , productId: productId
                 }
               , function(data, textStatus) {
                   $(caller).fadeIn();
                   $('#cart_items_sign').html(data);
                   $('#cart_items_sign').fadeIn();
                   $('#total_items_sign').html(data);
                   $('#total_items_sign').fadeIn();
                 }
               );

}

function update_cart(caller, amount, productId) {
  
  $(caller).fadeOut();
  $('#cart_items_sign').fadeOut();
  $('#total_items_sign').fadeOut();
  
  var a = $.get( "?__ajaxMethod=update_cart"
               , { amount:    amount
                 , productId: productId
                 }
               , function(data, textStatus) {
                   if (data == -1) {
                     cms_page_refresh();
                   } else {                 
                     $(caller).fadeIn();
                     $('#cart_items_sign').html(data);
                     $('#cart_items_sign').fadeIn();
                     $('#total_items_sign').html(data);
                     $('#total_items_sign').fadeIn();
                   }
                 }
               );

}

function empty_cart(caller) {
  
  $(caller).fadeOut();
  $('#cart_items_sign').fadeOut();
  $('#total_items_sign').fadeOut();
  
  var a = $.get( "?__ajaxMethod=empty_cart"
               , { }
               , function(data, textStatus) {
                   cms_page_refresh();
                 }
               );

}


function remove_from_cart(caller, productId) {
  
  $(caller).fadeOut();
  $('#cart_items_sign').fadeOut();
  $('#total_items_sign').fadeOut();
  
  var a = $.get( "?__ajaxMethod=remove_from_cart"
               , { productId: productId
                 }
               , function(data, textStatus) {
                   $('#products_row_' + productId).fadeOut();
                   $('#products_total_sum').html(data);
                   $('#cart_items_sign').html(data);
                   $('#cart_items_sign').fadeIn();
                   $('#total_items_sign').html(data);
                   $('#total_items_sign').fadeIn();
                 }
               );

}

function trim(string) {
return string.replace(/(\s+$)|(^\s+)/g, "");
}

function do_search(submit) {
    var searchText = document.getElementById('search_text');
    var searchForm = document.getElementById('search_form');
    if (searchText.value == "search.." || trim(searchText.value) == "") {
        searchText.focus();
        alert('Please enter search text');
        return false;
    } else {
        var searchValue = trim(searchText.value);
            searchValue = searchValue.replace(/[^A-Za-z0-9 +-]/g, "");
            searchValue = searchValue.replace(/(\s+)/g, "-");
        searchForm.action += searchValue;
        searchForm.submit();
    }
}
