function order_list(){
	var bbk = new basket();
	bbk.order();
}
function order_complete(item){
	var lst = document.getElementById("order_list_table");
	var frm = document.getElementById("order_form_table");
	var sz = item.list.length;
	var n;
	var r_flag = 0;
	if(lst.firstChild.childNodes.length > 1) r_flag = lst.firstChild.childNodes.length - 1;
	var nodes = lst.firstChild.childNodes;
	for(var i=0; i<sz; i++){
		n = item.size - i;
		if(r_flag == 0) lst.firstChild.appendChild(createLstNode(item.list[i], n));
		else{
			var node = nodes[i+1];
			lst.firstChild.replaceChild(createLstNode(item.list[i], n), node);
			r_flag--;
		}
	}
	if(frm.firstChild.childNodes.length == 4) frm.firstChild.appendChild(createFrmNode(item));
	else{
		var node = frm.firstChild.lastChild;
		frm.firstChild.replaceChild(createFrmNode(item), node);
	}
}
function basket(){

	var req = false;
	var act;

	this.buy = function(code){
		var quantity;
		quantity = window.prompt("Количество:", "1");
		if((!quantity)||(quantity=='0')) return;
		if(!this.init_req()) return;
		act = "buy_item";
		var now = new Date();
		var hours = now.getHours();
		var minutes = now.getMinutes();
		var seconds = now.getSeconds();
		var timeStr = "" + hours;
		timeStr += ((minutes < 10) ? "0" : "") + minutes;
		timeStr += ((seconds < 10) ? "0" : "") + seconds;
		req.open("GET", "/catalog/buyItem.ajax?code="+code+"&quantity="+quantity+"&"+timeStr, true);
		req.send(null);
	}
	this.order = function(){
		if(!this.init_req()) return;
		act = "order_list";
		var now = new Date();
		var hours = now.getHours();
		var minutes = now.getMinutes();
		var seconds = now.getSeconds();
		var timeStr = "" + hours;
		timeStr += ((minutes < 10) ? "0" : "") + minutes;
		timeStr += ((seconds < 10) ? "0" : "") + seconds;
		req.open("GET", "/user/currBasket.ajax?"+timeStr, true);
		req.send(null);
	}
	this.init_req = function(){
//		if(req) return true;
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				if(window.XMLHttpRequest){
					req = new XMLHttpRequest();
				}
			}
		}
		if(!req){
			alert("init req error!");
			return false;
		}
		req.onreadystatechange = this.getDate;
		return true;
	}
	this.getDate = function(){
		if(req.readyState == 4){
			if(req.status == 200){
				eval(req.responseText);
				switch(act){
					case "buy_item":
						order_list();
						break;
					case "order_list":
						order_complete(currBasket);
						break;
				}
			}
			else{
				alert("AJAX error: "+req.status+ "\n"+ req.statusText);
			}
		}
	}
}
function createImgTdNode(width, height){
	td = document.createElement("td");
	td.setAttribute("style", "width: "+width+"px");
	var img = document.createElement("img");
	img.setAttribute("width", width);
	img.setAttribute("height", height);
	td.appendChild(img);
	return td;
}
function createLstNode(item, n){
	var className = (n % 2) ? "userCat_" : "userCat";
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	td.setAttribute("class", className);
	var text = document.createTextNode(n);
	td.appendChild(text);
	tr.appendChild(td);
	tr.appendChild(createImgTdNode(1,0));
	td = document.createElement("td");
	td.setAttribute("class", className);
	text = document.createTextNode(item.name.substr(0,24));
	td.appendChild(text);
	tr.appendChild(td);
	tr.appendChild(createImgTdNode(1,0));
	td = document.createElement("td");
	td.setAttribute("class", className);
	text = document.createTextNode(item.size);
	td.appendChild(text);
	tr.appendChild(td);
	return tr;
}
function createFrmNode(item){
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	td.setAttribute("colspan", "3");
	td.setAttribute("class", "userText");
	var text = document.createTextNode("Товаров в корзине: ");
	td.appendChild(text);
	var b = document.createElement("b");
	text = document.createTextNode(item.size);
	b.appendChild(text);
	td.appendChild(b);
	var br = document.createElement("br");
	td.appendChild(br);
	text = document.createTextNode("На сумму: ");
	td.appendChild(text);
	b = document.createElement("b");
	text = document.createTextNode(item.summ);
	b.appendChild(text);
	td.appendChild(b);
	text = document.createTextNode(" руб");
	td.appendChild(text);
	tr.appendChild(td);
	return tr;
}