    var foto_windows = new Array();
    function open_foto(url, winname, width, height){
        var n = 0;
        for(i = 0; foto_windows.length > i; i++){
                if(!foto_windows[i].closed)
                        if(foto_windows[i].location == 'http://www.tema-sport.ru/'+url){
                                foto_windows[i].focus();
                                n = 1;
                        }
        }
        if(n == 0){
                foto_windows[foto_windows.length] = window.open(url,winname,'height='+height+',width='+width+',resizable=0,scrollbars=no,menubar=no,status=no,top=300,left=300');
                foto_windows[foto_windows.length-1].focus();
        }
}
    function notav(){
	alert('К сожалению, этот товар сейчас отсутствует...');
    }
    function setCookie(name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "");
                document.cookie = curCookie;
    }
    function getCookie(name) {
        var prefix = name + "=";
        var cookieStartIndex = document.cookie.indexOf(prefix);
        if (cookieStartIndex == -1)
                return null
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length;
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
    }
    function addCallback(res,text){
	if(!res){
            alert(text);
        }else{
//	    setCookie('basket',res.cookie,res.expires,'/','tema-sport.ru');
	    document.getElementById('basket_count').innerHTML=res.count;
	    document.getElementById('basket_total').innerHTML=res.total;
	    document.getElementById('basket_suff').innerHTML=(res.suff==0?'ов':(res.suff==1?'':'а'));
	    document.getElementById('basket').style.display='block';
	    alert(res.name+' добавлен в Вашу корзину');
	}
    }
    function add(id){
	ajax_call('add',id,addCallback);
    }
    function brandsShowAll(res,text){
	if(!res){
            alert(text);
        }else{
	    var t = '';
	    for(var i = 0;i<res.length;i++){
		t += '<a href="/catalog/0/'+res[i].id+'" title="'+res[i].name+'">'+res[i].name+'</a>';
		if(i != res.length-1){
		    t += '&nbsp;&nbsp; ';
		}
	    }
	    document.getElementById("for_brands").innerHTML = t;
	}
    }
    function removeCallback(res,text){
	if(!res){
            alert(text);
        }else{
            if (res.total == 0 && res.count == 0) {
                alert('Корзина очищена!');
                location.replace('/');
            } else {
    	        document.getElementById(res.id+'_tr').style.display='none';
    	        document.getElementById(res.id+'_count').value=0;
                document.getElementById('basket_count').innerHTML=res.count;
	        document.getElementById('total_order').innerHTML=res.total;
                document.getElementById('basket_total').innerHTML=res.total;
	        document.getElementById('basket_suff').innerHTML=(res.suff==0?'ов':(res.suff==1?'':'а'));
                document.getElementById('basket').style.display='block';
	        enable_btns();
            }
        }
    }
    function remove(id){
	disable_btns();
	ajax_call('remove',id,removeCallback);
    }
    function drop_basket(){
	drop();
	alert('Корзина очищена!');
	location.replace('/');
    }
    function dropCallback() {
	if(location.href.indexOf('basket') != -1){
            location.replace('/');
        }else{
            document.getElementById('basket').style.display='none';
        }
    }
    function drop(){
	ajax_call('drop',dropCallback);
    }
    function recalcCallback(res,total,count,suff){
	if(!res){
            alert(total);
        }else{
            if (total == 0 && count == 0) {
                alert('Корзина очищена!');
                location.replace('/');
            } else {
    	        for(var i = 0;i<res.length;i++){
    		    if(res[i].count == 0){
		        document.getElementById(res[i].id+'_tr').style.display = 'none';
		    }
		    document.getElementById(res[i].id+'_count').value = res[i].count;
		    document.getElementById(res[i].id+'_price').innerHTML = res[i].price;
	        }
                document.getElementById('basket_count').innerHTML=count;
                document.getElementById('basket_total').innerHTML=total;
	        document.getElementById('total_order').innerHTML=total;
	        document.getElementById('basket_suff').innerHTML=(suff==0?'ов':(suff==1?'':'а'));
                document.getElementById('basket').style.display='block';
	        enable_btns();
            }
        }
    }
    function recalc_it(){
	disable_btns();
	var obj = new Object;
	for(var i = 0;i<goods.length;i++){
	    obj[goods[i]] = document.getElementById(goods[i]+'_count').value;
	}
	ajax_call('recalc',obj,recalcCallback);
    }
    function sendOrderCallback(res){
	if(res){
	    alert('Ваш заказ принят! В ближайшее время с Вами свяжется наш менеджер');
	    location.replace('/');
	}
    }
    function send_order(){
	recalc_it();
	var obj = new Object;
	obj['name'] = document.getElementById('firstname').value;
	obj['phone'] = document.getElementById('phone').value;
	obj['email'] = document.getElementById('email').value;
	obj['adress'] = document.getElementById('adress').value;
	obj['comments'] = document.getElementById('comments').value;
	if(!(obj['name'].length && obj['phone'].length && obj['adress'].length)){
	    alert('Заполните пожалуйста все обязательные поля!');
	    return false;
	}
	ajax_call('send_order',obj,sendOrderCallback);
    }
    function loadBrand(group){
	document.getElementById('brand').disabled=1;
	ajax_call('load_brand',group,loadBrandCallback);
    }
    function loadGroup(brand){
        document.getElementById('group').disabled=1;
        ajax_call('load_group',brand,loadGroupCallback);
    }
    function loadBrandCallback(res,text){
	if(!res){
	    alert(text);
	}else{
//	    var t = '<option value=""> </option>';
	    var obj = document.getElementById('brand');
		var preSel = obj.options[obj.selectedIndex].value;
		obj.options.length = 1;
	    for(var i=0;i<res.length;i++){
			var wasSelected = (res[i].id==preSel);
			addOption(obj, res[i].name, res[i].id, false, wasSelected);
//			t += '<option value="'+res[i].id+'"'+(res[i].id==obj.options[obj.selectedIndex].value?' selected="1"':'')+'>'+res[i].name+'</option>';
	    }
//	    obj.innerHTML=t;
		obj.disabled=0;
	}
    }
    function loadGroupCallback(res,text){
        if(!res){
            alert(text);
        }else{
//            var t = '<option value=""> </option>';
			var obj = document.getElementById('group');
			var preSel = obj.options[obj.selectedIndex].value;
			obj.options.length = 1;
            for(var i=0;i<res.length;i++){
				var style = true;
				var wasSelected = (res[i].id==preSel); 
				var name = res[i].name;
				if(res[i].c_group) {
					name = '-- '+name;
					style = false;
				}
				addOption(obj, name, res[i].id, false, wasSelected, style);
//				addOption(obj, res[i].name, res[i].id, false, (res[i].id==obj.options[obj.selectedIndex].value), (res[i].c_group?'':' style="font-weight:bold;"'));
//                t += '<option value="'+res[i].id+'"'+(res[i].id==obj.options[obj.selectedIndex].value?' selected="1"':'')+(res[i].c_group?'':' style="font-weight:bold;"')+'>'+(res[i].c_group?'-- ':'')+res[i].name+'</option>';
            }
//            obj.innerHTML=t;
			obj.disabled=0;
        }
    }
    function filter(f,id){
	var link = '/catalog/';
	if(f.brand){
	    link += id+'/'+f.brand.value;
	}else if(f.group){
	    link += f.group.value+'/'+id;
	}
	location.replace(link);
    }
    function show(){
	var group = document.getElementById('group').value;
	var brand = document.getElementById('brand').value;
	if(!group && !brand){
	    return false;
	}
	var link = '/catalog/';
	if(group){
	    link += group+'/';
	}else{
	    link += '0/';
	}
	if(brand){
            link += brand+'/';
        }
	location.replace(link);
    }
    function testCallback(a,b){
	if(!a){
	    alert(b);
	}else{
	    alert(a);
	}
    }
function initCallback(res){
    if(res && res.count){
	document.getElementById('basket_count').innerHTML=res.count;
        document.getElementById('basket_total').innerHTML=res.total;
	document.getElementById('basket_suff').innerHTML=(res.suff==0?'ов':(res.suff==1?'':'а'));
        document.getElementById('basket').style.display='block';
    }
}
function _init(){
    ajax_call('check_basket',initCallback);
}

function disable_btns(){
    var btns = ["recalc_btn","drop_btn","sbm_btn"];
    for (var i = 0;i<btns.length;i++){
	document.getElementById(btns[i]).disabled = 1;
    }
}

function enable_btns(){
    var btns = ["recalc_btn","drop_btn","order_btn","sbm_btn"];
    for (var i = 0;i<btns.length;i++){
        document.getElementById(btns[i]).disabled = 0;
    }
}

function addOption (oListbox, text, value, isDefaultSelected, isSelected, style)
{
	var oOption = document.createElement("option");
	oOption.appendChild(document.createTextNode(text));
	oOption.setAttribute("value", value);

	if (isDefaultSelected) oOption.defaultSelected = true;
	else if (isSelected) oOption.selected = true;

	if (style) oOption.style.fontWeight = 'bold';

	oListbox.appendChild(oOption);
}


