// Сравнение
function getCookie(name) {
	var allcookies = document.cookie;
	if (allcookies == '') return false;
	
	var start = allcookies.indexOf(name + '='); if (start == -1) return false; start += name.length + 1;
	var end = allcookies.indexOf(';', start); if (end == -1) end = allcookies.length;
	var cookieval = unescape(allcookies.substring(start, end));
	
	return cookieval;
}

function AddCompare(redirect_to) {
    var data = document.getElementsByTagName('input');
    
    var out = '';
    for (var i = 0; i < data.length; i++) {
        if (data[i].type == 'checkbox' && data[i].checked) 
            out += data[i].value + ',';
    }

    location.href = '/compare.php?add=' + out.substring(0,out.length-1) + '&redirect=' + escape(redirect_to);
    
    return false;

//    return '/compare.php?add=' + out.substring(0,out.length-1) + '&redirect=' + escape(redirect_to);
}

function RemoveCompare(key) {
// Удаляем запись в cookie о данной моделе
	var models = ',' + getCookie('compare') + ',';
	models = models.replace(',' + key + ',', ',');
	
	if (models.indexOf(',') == 0) models = models.substring(1, models.length);
	if (models.lastIndexOf(',') == models.length-1) models = models.substring(0, models.length-1);
	
// Обновляем cookie
	dt = new Date(); dt.setHours(dt.getHours() + 2);
	document.cookie = 'compare=' + escape(models) + '; path=/; expires=' + dt.toGMTString();

// Удаляем модель со страницы
	document.getElementById('compare_'+key).style.display = 'none';
	
	if (models == '') { window.location.reload(true); }
    return false;
}

function dump(obj) {
	out = ''; for (prop in obj) out += 'obj[' + prop + '] = ' + obj[prop] + '\n';
	return out;
}                        


// Галерея
function OpenGallery(type, key) {
    var wnd = window.open("/info/catalog/gallery/?type=" + type + "&key=" + key, "gallery", "status,width=800,height=500");
    wnd.focus();
} 


// Каталог
function ChangeSort(type, url) {

    if (url.indexOf('sort=') > 0) {
		var pattern = /sort=[^&]*/i;
        window.location.href = url.replace(pattern, "sort=" + type);
	} else if (url.indexOf('/?') > 0)
		window.location.href = url + '&sort=' + type;
	else
		window.location.href = url + '?sort=' + type;
}

function ChangePageSize(count, url) {
	dt = new Date(); dt.setHours(dt.getHours() + 24*30);
	document.cookie = 'shop_page_size=' + count + '; path=/; expires=' + dt.toGMTString();

	window.location.href = url;
}

function OpenOrder(id) {
    var wnd = window.open("/order.php?id=" + id, "order", "status,width=240,height=430,left="+(window.screen.availWidth/2-120)+",top="+(window.screen.availHeight/2-200));
    wnd.focus();
}

function OpenForumReg() {
    var wnd = window.open("/forum_reg.php", "forum_reg", "status,width=240,height=330,left="+(window.screen.availWidth/2-120)+",top="+(window.screen.availHeight/2-165));
    wnd.focus();
}

function openFlashFile(newURL)
{
        window.open(newURL, "_blank",'width=624,height=464,resizable=no,location=no,directories=no,scrollbars=no,menubar=no,status=no,toolbar=no,titlebar=no,hotkeys=no');
}
