function makeAJAXRequest()
{
    var http_request = false;
    if (window.XMLHttpRequest) // Mozilla
    { 
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
        {
            http_request.overrideMimeType('text/xml; charset=windows-1251');
        }
    }
    else if (window.ActiveXObject) // IE
    { 
        try
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request)
    {
        return false; // Невозможно создать экземпляр класса XMLHTTP
    }
    else
    {
        return http_request;
    }
}

function add_to_basket(product_id)
{
	var link = document.getElementById('addto_bask_' + product_id);
	
	var mess = create_mess('Добавляем товар в корзину...');
	down=setTimeout( function() { document.getElementsByTagName("body")[0].removeChild(mess) },3000);
	
	var xURL = "?screen=aj_add_product_to_basket&id=" + product_id;
	
	var http_request = makeAJAXRequest();
    http_request.onreadystatechange = function(){ add_to_basket_mess(http_request, mess, link); };
    
    http_request.open('GET', xURL, true);
    http_request.send(null);
	
}

function add_to_basket_mess(http_request, mess, link)
{
	if (http_request.readyState == 4)
    {
        if (http_request.status == 200)
        {
            var xmlResp = http_request.responseXML;
			var xml_status = xmlResp.getElementsByTagName("status")[0].firstChild.nodeValue;
			
			if( xml_status == 'OK' )
			{
				var basket_amount = xmlResp.getElementsByTagName("basket_amount")[0].firstChild.nodeValue;
				var basket_sum    = xmlResp.getElementsByTagName("basket_sum")[0].firstChild.nodeValue;
				
				var out = "Товар добавлен в корзину";
					out = out + "<br />В корзине <b>"+basket_amount+"</b> товаров на сумму <b>"+basket_sum+"</b> руб.";
				update_mess(mess, out);
				
				var html_basket_amount = document.getElementById('basket_amount');
					html_basket_amount.innerHTML = basket_amount;
				var html_basket_sum    = document.getElementById('basket_sum');
					html_basket_sum.innerHTML = basket_sum;
					
				link.parentNode.removeChild(link);
				
			}
			else
			{
				update_mess(mess, 'Ошибка! Товар не добавлен в корзину!');
			}
		}
	}
}

function create_mess(mess)
{
	var scroll = document.documentElement.scrollTop || document.body.scrollTop;
	var div = document.createElement('div');
		div.innerHTML     = mess;
		div.style.position = 'absolute';
		div.style.border  = "6px solid #cccccc";
		div.style.padding = '5px';
		div.style.width   = '400px';
		div.style.top = scroll + 300 + 'px';
        div.style.left = Math.floor(document.body.clientWidth/2) - 225 + 'px';
		div.style.background = "#ffffff";
		
	document.getElementsByTagName("body")[0].appendChild(div);
	
	return div;
}

function update_mess(div, mess)
{
	div.innerHTML = mess;
}

function remove_mess(mess)
{
	document.getElementsByTagName("body")[0].removeChild(mess);
}


function empty_search(input)
{
	if( input.value == 'поиск')
	{
		input.value = '';
	}
}


