// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (xmlHttp)
  {
    return xmlHttp;
  }
}

// make asynchronous HTTP request using the XMLHttpRequest object
function add_to_basket_handler(url_pars)
{
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    xmlHttp.open("GET", "add_to_basket.php?" + url_pars, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }else{
    setTimeout('add_to_basket_handler(url_pars)', 1000);
  }
}

// executed automatically when a message is received from the server
function handleServerResponse()
{
  if (xmlHttp.readyState == 4)
  {
    if (xmlHttp.status == 200)
    {
      xmlResponse = xmlHttp.responseXML;
      xmlDocumentElement = xmlResponse.documentElement;
      ResultMessage = xmlDocumentElement.firstChild.data;
      if (ResultMessage=='1')
      {      	if(confirm('Товар успешно добавлен в корзину! Перейти к оформлению заказа?'))
      	{
      	  window.location=url_go_to_basket;      	}      }else{
        alert('При добавлении товара в корзину произошла ошибка!');      }
    }
  }
}

function add_to_basket(url_add_to_basket)
{
    url_add_to_basket=url_add_to_basket;
    add_to_basket_handler(url_add_to_basket);	return false;}

/*========================*/

function put_in_basket(id_item, u_code)
{	$.ajax({
	    url: '/add_to_basket.php?id_add=' + id_item + '&u=' + u_code,
	    cache: false,
	    dataType: "xml",
	    success: function(xml){

            var items_num = $(xml).find('response').text();

			if (items_num.length==0)
			{
			  var temp2='';
			  var temp1='';
			}else if(items_num.length==1){
			  var temp2='';
			  var temp1=items_num;
			}else{
			  var temp2=items_num.charAt(items_num.length-2) + '' + items_num.charAt(items_num.length-1);
			  var temp1=items_num.charAt(items_num.length-1);
			}

			var a_text = items_num + ' ';

			if (temp2==11 || temp2==12 || temp2==13 || temp2==14 ||
			    temp1==0 || temp1==5 || temp1==6 || temp1==7 || temp1==8 || temp1==9)
			{
			  a_text = a_text + 'товаров';
			}else if(temp1==1){
			  a_text = a_text + 'товар';
			}else if(temp1==2 || temp1==3 || temp1==4){
			  a_text = a_text + 'товара';
			}else{
			  a_text = a_text + 'товаров';
			}

	        $("#basket_link_top").text(a_text);
	        $("#basket_link_bottom").text(a_text);

			if(items_num > 0)
			{
				alert('Товар был успешно добавлен!');
			}else{
				alert('При добавлении товара возникла ошибка!');			}
	    }
	});
	return false;
}
