// Ajaxian 
function xhttpreq(dest) {
 try { xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"); }
 catch (e) { 
 	// browser doesn't support ajax. handle however you want 
 }
 xmlhttp.onreadystatechange = triggered;
 xmlhttp.open("GET", dest+'&ajax=1');
 xmlhttp.send(null);
}

function triggered() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		//alert(xmlhttp.responseText);
		resp = xmlhttp.responseText;
		if (isNaN(resp)) {
			if (resp.substring(0,9)=='clickstat') triggered_clickstat(resp);
			else {
				var resp = eval('(' + resp + ')'); 
				if (resp.count) {
					//alert('added');
					var ob = doc.getElementById("cart_"+resp.id);
					var carta = doc.getElementById("cartinside");
					if (ob) {
						ob = doc.getElementById("cartprice_"+resp.id);
						if (ob.innerHTML) ob.innerHTML = resp.price;
						ob = doc.getElementById("cartcount_"+resp.id);
						if (ob.innerHTML) ob.innerHTML = 'x'+resp.count;
					} else {
						//alert(resp.id);
						if (resp.id.indexOf('frescoes')!=-1) imgname = 'frescoes/frescoes/small/'+str_replace('/','-',resp.ref.toLowerCase())+'.jpg'; else imgname = resp.img+'s.jpg';
						var item = '<p id="cart_'+resp.id+'"><a href="/search/?text='+resp.id+'"><img src="/i/'+imgname+'" /></a> № '+resp.ref+' (<span id="cartcount_'+resp.id+'" class="count">x'+resp.count+'</span>) <b id="cartprice_'+resp.id+'">'+resp.price+'</b> руб. <a class="remove" href="/cart/server.php?action=remove&id='+resp.id+'" onclick="removecart(\''+resp.id+'\'); return false;"><img src="/i/del.gif" width="13" height="13" /></a></p>';
						//alert(item);
						carta.innerHTML += item;
						//alert(3);
					}
				} else {			
					//alert('removed');
					var ob = doc.getElementById("cart_"+resp.id);
					if (ob) {
						ob.style.visibility = 'hidden'; 
						ob.style.position = 'absolute';
						ob.id = 'deleted';
					}	
				}	
				var summa = doc.getElementById("summ");
				//alert(summa);
				summa.innerHTML = resp.summ;
				hideshowcart();
			}	
		} else {
			// 
		}
   }
}

function hideshowcart() {
	var cart = doc.getElementById("cart");
	var summa = doc.getElementById("summ");
	var summaval = 0;
	if (summa) summaval = summa.innerHTML;
	
	if (summaval>0) {
		cart.style.visibility = 'visible'; 
		cart.style.width = '100%'; 
		cart.style.position = 'relative'; 
	} else {
		cart.style.visibility = 'hidden';
		cart.style.width = '200px'; 
		cart.style.position = 'absolute'; 
	}	
}

function addcart(id)
{
		xhttpreq("/cart/server.php?action=add&id="+id);
}

function removecart(id)
{
		xhttpreq("/cart/server.php?action=remove&id="+id);
}

function changecart(id,count)
{
		xhttpreq("/cart/server.php?action=change&id="+id+"&count="+count);
}

function addcurrency(price) {
	return price + ' ' + currencytext;
}

function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

if (document.getElementById('summ')) doc = document; else doc = window.opener.document;
removecart('');
