function lib_select_child_delete(selectObj) {
	while (selectObj.childNodes.length) {
		if (selectObj.firstChild.tagName == 'optgroup') {
			while (selectObj.firstChild.childNodes.length)
				selectObj.firstChild.removeChild(selectObj.firstChild.firstChild);
		}
		selectObj.removeChild(selectObj.firstChild);
	}
}

function lib_child_delete(selectObj) {
	while (selectObj.childNodes.length)
		selectObj.removeChild(selectObj.firstChild);
}

function lib_select_onchange(id, values) {
	var select = document.getElementById(id);
	cmf_select_child_delete(select);
	for (var i = 0; i < values.length; i++)
			select.options[i] = new Option(values[i].text, values[i].value, values[i].select, values[i].select);
}

function lib_select_option(parent, text, value, selected, selected2) {
	var opt1 = new Option(text, value, selected, selected2);
	parent.appendChild(opt1);
	opt1.text = text;
	opt1.value = value;
	return opt1;
}

function lib_select_option2(obj, value, text) {
	var opt = new Option(text, value);
	obj.appendChild(opt);
	opt.text = text;
	opt.value = value;
}

function lib_select(obj, data) {
	//lib_select_child_delete(obj);
	//alert(data);
	for (var i in data) {
        lib_select_option2(obj, i, data[i]);
	}

}

function lib_ajax_log(text) {
	document.getElementById('ajax_log').innerHTML = text;
}


function lib_innerHTML(id, value) {
	document.getElementById(id).innerHTML = value;
}

function lib_getId(id) {
	return document.getElementById(id);
}


function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}



function selectCompare(id) {
    var req = new JsHttpRequest();
    //req.caching = true;
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if(req.responseJS.init) {
            	CompareInit(req.responseJS.init);
	            var product = eval('(' + req.responseJS.product + ')');
	            alert(product[180]);
	        }
			lib_ajax_log(req.responseText);
        }
    }
	req.open(null, '/main_ajax/compare.php', true);
	req.send( { action: 'add', compare: id } );
}

function CompareInit(count) {
	document.getElementById('is_compare_button').disabled = false;
	document.getElementById('is_compare_count').innerHTML = 'Товаров в сравнении ('+ count +')';
}



function lib_viewImg(url, width, height) {
	if (url == '') return false;
	window_top =20+ (screen.height - height) / 2;
	window_left =20+ (screen.width - width) / 2;
	width += 16;
	height += 16;
	window.open(url, 'image', 'width='+width+', height='+height+', top='+window_top+', left='+window_left+', toolbar=0, statusbar=0, location=0, scrollbars=no, resizable=0');
}