function CatalogDeleteField(id) {
    var i;
    var oTable = document.getElementById('compare_table');
    var iRows = oTable.rows.length;
    var iCell = document.getElementById('catalog_cell_' + id).cellIndex;
    var sToCompare = GetCookie('to_compare');
    if (sToCompare) aCounter = GetCookie('to_compare').split(',');
    if (aCounter.length > 1) {
        for (i = 0; i < iRows; i++) {
            oTable.rows[i].deleteCell(iCell);
        }
        aCounter.splice(IndexInArray(id), 1);
        document.cookie = 'to_compare=' + aCounter + '; path=/';
        document.cookie = 'str' + id + '=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
    } else {
        for (i = 0; i < aCounter.length; i++) {
            document.cookie = 'str' + aCounter[i] + '=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
        }
        document.cookie = 'to_compare=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
	    window.location = (document.referrer) ? document.referrer : '/catalog/';
	}
	L_compare_elements();
}

function L_cell_color(oCells, bDiff) {
    for (var i = 0; i < oCells.length; i++) {
        if (bDiff) {
            oCells[i].className = (i == 0) ? 'compare_head_diff' : 'compare_diff';
        } else {
            oCells[i].className = (i == 0) ? 'compare_head_same' : 'compare_same';
        }
    }
}

function L_compare_elements() {
    var oTable = document.getElementById('compare_table');
    var oRows = oTable.rows;
    for (var i = 0; i < oRows.length; i++) {
        if (oRows[i].className != 'compare_continue') {
            var oCells = oRows[i].cells;
            L_cell_color(oCells, true);
            if (oCells.lenght <= 2) break;
            var sData = oCells[1].getElementsByTagName('input')[0].value;
            for (var j = 2; j < oCells.length; j++) {
                if (oCells[j].getElementsByTagName('input')[0].value == sData) {
                    if (oCells.length == j + 1) L_cell_color(oCells, false);
                    continue;
                } else break;
            }
        }
    }
}

function ParentChecked(id, checked) {
    var oDIV, oA, oIMG, oSpan;
	var oLabel = document.getElementById('label-' + id);
    var oList_td = document.getElementById('compare_list_td');
    var oList_div = document.getElementById('compare_list_div_' + id);
    var oDIV_checked = document.getElementById('div_checked_' + id);
    oDIV_checked.innerHTML = '';

	if (checked) {
		if (aCounter.length >= 4) {
			alert('Для сравнения можно выбрать не более четырех товаров.');
		} else {
            oDIV = document.createElement('div');
            oDIV_checked.appendChild(oDIV);
            oA = document.createElement('a');
            oA.href = '/catalog/compare.php';
            oA.innerHTML = 'Сравнить';
            oDIV.appendChild(oA);

            oDIV = document.createElement('div');
            oDIV_checked.appendChild(oDIV);
            oA = document.createElement('a');
            oA.href = 'javascript:ParentChecked(' + id + ', 0);';
            oA.innerHTML = '(отменить)';
            oDIV.appendChild(oA);

            document.getElementById('compare_list').style.display = 'block';
			aCounter.push(id);
			document.cookie = 'to_compare=' + aCounter + '; path=/';
			document.cookie = 'str' + id + '=' + escape(oLabel.alt) + '; path=/';

            oDIV = document.createElement('div');
            oList_td.appendChild(oDIV);
            oDIV.id = 'compare_list_div_' + id;
            oSpan = document.createElement('span');
            oDIV.appendChild(oSpan);
            oSpan.innerHTML = '- ';
            oA = document.createElement('a');
            oDIV.appendChild(oA);
            oA.href = '/catalog/gd-' + id + '.html';
            oA.innerHTML = oLabel.alt[0].toLowerCase() + oLabel.alt.substring(1);

            oA = document.createElement('a');
            oDIV.appendChild(oA);
            oA.href = 'javascript:G_compare_delete(' + id + ');';
            oA.style.margin = '1px 0 0 1em';
            oA.style.cssFloat = 'right';
            oA.style.styleFloat = 'right';
            oIMG = document.createElement('img');
            oA.appendChild(oIMG);
            oIMG.src = '/img/delet-compare.gif';
            oIMG.alt = 'Удалить';
            oIMG.title = 'Удалить';
		}
	} else if (!checked && aCounter.length > 1) {
        oDIV = document.createElement('div');
        oDIV_checked.appendChild(oDIV);
        oA = document.createElement('a');
        oA.href = 'javascript:ParentChecked(' + id + ', 1);';
        oA.innerHTML = 'В сравнение';
        oDIV.appendChild(oA);

        oList_td.removeChild(oList_div);
		aCounter.splice(IndexInArray(id), 1);
		document.cookie = 'to_compare=' + aCounter + '; path=/';
		document.cookie = 'str' + id + '=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
	} else {
        oDIV = document.createElement('div');
        oDIV_checked.appendChild(oDIV);
        oA = document.createElement('a');
        oA.href = 'javascript:ParentChecked(' + id + ', 1);';
        oA.innerHTML = 'В сравнение';
        oDIV.appendChild(oA);

        oList_td.removeChild(oList_div);
        document.getElementById('compare_list').style.display = 'none';
		aCounter = new Array();
		document.cookie = 'to_compare=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
		document.cookie = 'str' + id + '=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
	}
	if (aCounter.length > 1) {
        document.getElementById('compare_list_div').style.display = 'block';
	} else {
        document.getElementById('compare_list_div').style.display = 'none';
	}
}

