function addToShoppingCart( what, id )
{ 
  setMessageText('Выполняется запрос...');
  showMessage(200, 30);
  
  quont = ( $('#id-'+ what + '_' + id + '-quont').val() ) ? $('#id-'+ what + '_' + id + '-quont').val() : 1;
    
  $.get(
        "/index-ajax.php", 
        { q: "assets/snippets/catalog/shoppingcart/addtoshoppingcart.processor.php", 
          addToShoppingCart: what, 
          item:  id,
          quont: quont},  
        refreshShoppingCart
  );
}

function delFromShoppingCart( what, id )
{ 
  setMessageText('Выполняется запрос...');
  showMessage(200, 30);
    
  $.get(
        "/index-ajax.php", 
        { q: "assets/snippets/catalog/shoppingcart/delfromshoppingcart.processor.php", 
          delFromShoppingCart: what, 
          item:  id},  
        function(){ delFromShoppingCartVisual( what,id ) }
  );
}

function delFromShoppingCartVisual( what, id )
{
  setMessageText('Запрос выполнен'); 
  $('#' + what + '_row_' + id).hide(2000,function(){
    $('#' + what + '_row_' + id).remove();
    refreshSum();
    hideMessage(); 
  }); 
}

function refreshSum() 
{
  var sum = 0;
  $('input.item_quont').each(function() {
    var item_sum = 0;
    item_sum = this.value*$('#'+this.id+'-price').val();
    $('#'+this.id+'-sum').html(item_sum);
    sum += item_sum;
  });
  $('#sc-sum').html('Общая сумма заказа:&nbsp;&nbsp;&nbsp;' + sum +  ' грн');
}

function updateShoppingCart()
{
  setMessageText('Выполняется запрос...');
  showMessage(200, 30);
  
  $.get( "/index-ajax.php?q=assets/snippets/catalog/shoppingcart/updateshoppingcart.processor.php", 
        $("form").serialize(),  
        refreshShoppingCart
  );
}

function refreshShoppingCart()
{   
 $.getJSON(
          "/index-ajax.php?q=assets/snippets/catalog/shoppingcart/getshoppingcartinfo.processor.php",
          refreshBasket);
}

function refreshBasket(request)
{
  $('#shoppingCartQuont').html(request.quont);
  $('#shoppingCartSum').html(request.sum);
  setMessageText('Запрос выполнен'); 
  hideMessage();
}

function showMessage( w, h )
{
  // width and height of the viewport
	var ar = getPageSize();
	var wScr = ar[2];
	var hScr = ar[3];
	
	// correct coords according to scroll bars position
	var scr = getScrollXY();
	var y = (hScr>h ? Math.floor(Math.abs((hScr - h)/2)) : 0) + scr[1];
	var x = (wScr>w ? Math.floor(Math.abs((wScr - w)/2)) : 0) + scr[0];
	
	params = {
	 width: w + "px",
	 height: h+ "px",
	 top: y+ "px",
	 left: x+ "px",
   visibility: 'visible',
   display: 'block',
   opacity: 1,
   backgroundColor: '#fff'
	};
	$('#message_window').css(params);
}

function hideMessage()
{
  $('#message_window').fadeOut(1000, function() {
    $('#message_window').css({display: 'block', visibility: 'hidden'});
    setMessageText('');   
  });
}

function setMessageText( text )
{
  $('#message_window').html("<div id='message_text'>" + text + "</div>");
}

function sendOrder() 
{
  tel = jQuery.trim(document.mail.tel.value);
  email = jQuery.trim(document.mail.email.value);
  if ( email=='' && tel=='' ) 
   alert('Введите пожалуйста контакнтую информацию, чтобы мы могли с Вами связаться.'); 
  else {
   if ( email !='' && ! /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(email) ) {
    alert('Введите пожалуйста правильный электронный адрес.');
    return false;
   }
   document.mail.send.click();
  }
}
