var iTimeoutID;
var RegexpArticleNumber = /^[1-9]\d{0,2}$/;
var GVarOnPage = {
    "catalog" : false,
    "search"  : false
}

function G_onkeydown(event) {
    var id = false;
    event = event || window.event;
    var key = event.keyCode;
    if(event.ctrlKey) {
        var tag = (event.target || event.srcElement).tagName;
        if(tag != 'INPUT' && tag != 'TEXTAREA') {
            if(key == 37) {
                id = document.getElementById('page_previous');
                if (id) {
                    if (GVarOnPage['catalog']) {
                        G_submit(GVarOnPage['catalog'] - 1);
                        return;
                    } else if (GVarOnPage['search']) {
                        Search_onsubmit(GVarOnPage['search'] - 1);
                        return;
                    }
                }
            }
            if(key == 39) {
                id = document.getElementById('page_next');
                if (id) {
                    if (GVarOnPage['catalog']) {
                        G_submit(GVarOnPage['catalog'] + 1);
                        return;
                    } else if (GVarOnPage['search']) {
                        Search_onsubmit(GVarOnPage['search'] + 1);
                        return;
                    }
                }
            }
            if(id) {
                location.href = id.href;
            }
        }
    }
}

function J_basket_in() {
    var i, j, aCrumb, aIndex, aIndex_options, oA, aValue;
    var rx = /^article_id_(\d+)$/;
    var rx_options = /^options_id_(\d+)$/;
    var rx_int = /^\d+$/;
    var aCookie = document.cookie.split('; ');
    for (i = 0; i < aCookie.length; i++) {
        aCrumb = aCookie[i].split('=');
        aIndex = rx.exec(aCrumb[0]);
        aIndex_options = rx_options.exec(aCrumb[0]);
        if (aIndex) {
            oA = document.getElementById('a_basket_' + aIndex[1]);
            aValue = aCrumb[1].split('-');
            if (oA) {
                oA.className = 'basket_in';
                oA.href = '/basket/';
                if (rx_int.test(aValue[3])) J_options(aIndex[1], aValue[3]);
            }
        } else if (aIndex_options) {
            oA = document.getElementById('a_basket_' + aIndex_options[1]);
            aValue = aCrumb[1].split(',');
            if (oA && aValue.length > 0) {
                J_options(aIndex_options[1], 'basket');
            }
        }
    }
}

function G_compare_in(art_id, a_to_compare) {
    var i;
    for (i = 0; i < a_to_compare.length; i++) {
        if (a_to_compare[i] == art_id) return i;
    }
    return false;
}

function G_compare(art_id) {
    var to_compare = GetCookie('to_compare');
    var section_id = (GetCookie('section_id')) ? GetCookie('section_id') : 1;
    var compare_section = (GetCookie('compare_section_' + section_id)) ? GetCookie('compare_section_' + section_id) : 0;
    var a_to_compare = (to_compare) ? to_compare.split(',') : [];
    var oA = document.getElementById('compare_a_' + art_id);
    var index = G_compare_in(art_id, a_to_compare);
    if (a_to_compare.length > 0 && index !== false) {
        document.cookie = (compare_section > 1)
            ? 'compare_section_' + section_id + '=' + (parseInt(compare_section) - 1) + '; path=/'
            : 'compare_section_' + section_id + '=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
		document.cookie = 'str' + art_id + '=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
		document.cookie = 'compare_section_id_' + art_id + "=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/";
        if (a_to_compare.length > 1) {
            a_to_compare.splice(index, 1);
    		document.cookie = 'to_compare=' + a_to_compare.toString() + '; path=/';
        } else {
    		document.cookie = "to_compare=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/";
        }
        if (oA) oA.innerHTML = 'в сравнение';
        G_selected();
    } else if (compare_section >= 4) {
        alert('Для сравнения можно выбрать не более четырех товаров из одного раздела.');
    } else {
        a_to_compare.push(art_id);
        document.cookie = 'to_compare=' + a_to_compare.toString() + '; path=/';
        document.cookie = 'str' + art_id + '=' + escape(document.getElementById('article_name_' + art_id).value) + '; path=/';
        document.cookie = 'compare_section_' + section_id + '=' + (parseInt(compare_section) + 1) + '; path=/';
        document.cookie = 'compare_section_id_' + art_id + '=' + section_id + '; path=/';
        if (oA) oA.innerHTML = 'удалить из сравнения';
        G_selected();
    }
}

function G_selected() {
    var i, oA, oDIV, section, name, oIMG, oSPAN;
    var top_compare_rows = document.getElementById('top_compare_rows');
    top_compare_rows.innerHTML = '';
    var feedback_div = document.getElementById('top_feedback_div');
    var to_compare = GetCookie('to_compare');
    var section_id = (GetCookie('section_id')) ? GetCookie('section_id') : 1;
    var compare_section = (GetCookie('compare_section_' + section_id)) ? GetCookie('compare_section_' + section_id) : 0;
    var a_to_compare = (to_compare) ? to_compare.split(',') : [];
    for (i = 0; i < a_to_compare.length; i++) {
        section = GetCookie('compare_section_id_' + a_to_compare[i]);
        name = GetCookie('str' + a_to_compare[i]);
        if (section && section == section_id && name) {
            oDIV = document.createElement('div');
            top_compare_rows.appendChild(oDIV);
            oSPAN = document.createElement('span');
            oSPAN.innerHTML = '- ';
            oDIV.appendChild(oSPAN);
            oA = document.createElement('a');
            oA.href = '/catalog/model-' + a_to_compare[i] + '.html';
            oA.innerHTML = name;
            oDIV.appendChild(oA);

            oA = document.createElement('a');
            oA.href = 'javascript:G_compare(' + a_to_compare[i] + ');';
            oA.style.margin = '2px 0 0 1em';
            oA.style.cssFloat = 'right';
            oA.style.styleFloat = 'right';
            oDIV.appendChild(oA);
            oIMG = document.createElement('img');
            oIMG.src = '/img/compare-del.gif';
            oIMG.border = '0';
            oIMG.alt = 'Удалить из сравнения';
            oIMG.title = 'Удалить из сравнения';
            oA.appendChild(oIMG);
        }
        oA = document.getElementById('compare_a_' + a_to_compare[i]);
        if (oA) oA.innerHTML = 'удалить из сравнения';
    }
    document.getElementById('top_compare_link').style.display = (compare_section > 1) ? 'block' : 'none';
    feedback_div.style.display = (compare_section > 0) ? 'none' : 'block';
    document.getElementById('top_compare_div').style.display = (compare_section > 0) ? 'block' : 'none';
}

function G_submit(page) {
    document.getElementById('page').value = page;
    document.getElementById('on_page').value = document.getElementById('top_items_on_page').value;
    document.getElementById('sort_by').value = document.getElementById('top_sort_by').value;
    document.getElementById('form_submit').submit();
}

function G_compare_submit() {
    document.cookie = 'compare_return=' + escape(window.location) + '; path=/';
    return true;
}

function Global_menu(sID) {
    var oEl = document.getElementById(sID);
    if (oEl) oEl.className = 'menunew_active';
}

function Global_SelectOption(value, select_id) {
    var oSelect = document.getElementById(select_id);
    var oOptions = oSelect.getElementsByTagName('option');
    for (var i = 0; i < oOptions.length; i++) {
        if (oOptions.item(i).getAttribute('value') == value) {
        	oOptions.item(i).selected = true;
			break;
		}
	}
}

function Search(sObj) {
	var oFind = document.getElementById(sObj);
	if (oFind.value == 'Поиск по каталогу' || oFind.value == 'Поиск по сайту') oFind.value = '';
	if (oFind.value.length < 3) {
		alert("Искомое слово не должно содержать менее трёх символов!");
		oFind.focus();
		return false;
	}
	return true;
}

function FindField() {
	var oFind = document.getElementById('find');
	if (oFind.value == 'Поиск по каталогу' || oFind.value == 'Поиск по сайту') oFind.value = '';
}

function GL_Into(sLabel, iNumber, iPrice) { //
	var oInto = document.getElementById('into-basket');
	oInto.style.top = document.body.scrollTop;
	oInto.innerHTML = '';

    var oH1 = document.createElement('h1');
    oInto.appendChild(oH1);
    oH1.innerHTML = 'Вы поместили в корзину';

    var oDIV = document.createElement('div');
    oInto.appendChild(oDIV);
    oDIV.innerHTML = sLabel;
    oDIV.style.fontWeight = 'bold';
    oDIV.style.marginBottom = '10px';

    var oDIV = document.createElement('div');
    oInto.appendChild(oDIV);
    oDIV.innerHTML = 'Выбрано сейфов: ' + iNumber;

    var oDIV = document.createElement('div');
    oInto.appendChild(oDIV);
    oDIV.innerHTML = 'На сумму: ' + iPrice + ' руб.';

    var oDIV = document.createElement('div');
    oInto.appendChild(oDIV);
    oDIV.style.marginTop = '10px';
    oDIV.style.textAlign = 'right';

    var oA = document.createElement('a');
    oDIV.appendChild(oA);
    oA.href = '/basket/';
    oA.style.fontWeight = 'bold';
    oA.innerHTML = 'Оформить заказ &#187;';

	oInto.style.visibility = 'visible';
	window.clearTimeout(iTimeoutID);
	iTimeoutID = window.setTimeout('document.getElementById("into-basket").style.visibility = "hidden"', 5000);
}

function G_basket(iArticleID, sPrice, sWeight, options) {
    var iNumber, iPrice, iInputArticlesNumber, oA;
    if (options) {
        document.cookie = 'options_id_' + iArticleID + "=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/";
    }
    oA = document.getElementById('a_basket_' + iArticleID);
    if (oA) {
        oA.className = 'basket_in';
        oA.href = '/basket/';
    }
	var oInputArticleNumber = document.getElementById('quantity_' + iArticleID);
	if (RegexpArticleNumber.test(oInputArticleNumber.value)) {
		iInputArticlesNumber =  parseInt(oInputArticleNumber.value);
	} else {
		iInputArticlesNumber =	1;
		oInputArticleNumber.value = 1;
	}
	var ArticlesFromCookie = GetCookie('articles_in_basket');
	var PriceFromCookie = GetCookie('price_in_basket');
	var sAtricleName = document.getElementById('article_name_' + iArticleID).value;
	if (ArticlesFromCookie != null && PriceFromCookie != null) {
		var ArticleInCookie = GetCookie('article_id_' + iArticleID);
		if (ArticleInCookie != null) {
			var aArticleData = ArticleInCookie.split('-');
    		iNumber = parseInt(ArticlesFromCookie) - parseInt(aArticleData[0]) + iInputArticlesNumber;
    		iPrice = parseFloat(PriceFromCookie) - parseFloat(aArticleData[1] * aArticleData[0]) + parseFloat(sPrice * iInputArticlesNumber);
			document.cookie = 'article_id_' + iArticleID + '=' + iInputArticlesNumber + '-' + sPrice + '-' + sWeight + '-' + options + '; path=/';
		} else {
            iNumber = parseInt(ArticlesFromCookie) + iInputArticlesNumber;
            iPrice = parseFloat(PriceFromCookie) + parseFloat(sPrice) * iInputArticlesNumber;
			document.cookie = 'article_id_' + iArticleID + '=' + iInputArticlesNumber + '-' + sPrice + '-' + sWeight + '-' + options + '; path=/';
		}
		document.cookie = 'articles_in_basket=' + iNumber + '; path=/';
		document.cookie = 'price_in_basket=' + iPrice + '; path=/';
	} else {
	    iNumber = iInputArticlesNumber;
		iPrice = iInputArticlesNumber * parseFloat(sPrice);
		document.cookie = 'articles_in_basket=' + iInputArticlesNumber + '; path=/';
		document.cookie = 'price_in_basket=' + iPrice + '; path=/';
		document.cookie = 'article_id_' + iArticleID + '=' + iInputArticlesNumber + '-' + sPrice + '-' + sWeight + '-' + options + '; path=/';
	}
	document.cookie = 'article_name_' + iArticleID + '=' + escape(sAtricleName) + '; path=/';
	ParseCookieBasket_new();
}

function GetCookie(sName) {
	var aCookie = document.cookie.split('; ');
	for (var i=0; i < aCookie.length; i++) {
		var aCrumb = aCookie[i].split('=');
		if (sName == aCrumb[0])
			return unescape(aCrumb[1]);
	}
  return null;
}

function ParseCookieBasket_new() {
    var oDIV, oSpan, oA;
	var basket_form = document.getElementById('basket_form');
	var ArticlesFromCookie = GetCookie('articles_in_basket');
	var PriceFromCookie = GetCookie('price_in_basket');
	var cookieOrderFrom = GetCookie('order_from');
	if (cookieOrderFrom == null && document.referrer) {
		document.cookie = 'order_from=' + escape(document.referrer) + '; path=/';
	}
	if (ArticlesFromCookie && PriceFromCookie) {
	    basket_form.innerHTML = '';

        oA = document.createElement('a');
        basket_form.appendChild(oA);
        oA.href = '/basket/';
        oA.style.margin = '0px 20px 0px 0px';
        oA.style.background = 'transparent url(/img/basket_sprite.gif) no-repeat scroll 0px -47px';
        oA.style.styleFloat = 'left';
        oA.style.cssFloat = 'left';
        oA.style.width = '55px';
        oA.style.height = '47px';

        oDIV = document.createElement('div');
        basket_form.appendChild(oDIV);
        oDIV.innerHTML = 'Выбрано товаров: ' + ArticlesFromCookie;

        oDIV = document.createElement('div');
        basket_form.appendChild(oDIV);
        oDIV.innerHTML = 'На сумму: ' + PriceFromCookie + ' руб.';

        oDIV = document.createElement('div');
        oDIV.style.textAlign = 'right';
        oDIV.style.paddingRight = '12px';
        basket_form.appendChild(oDIV);

        oA = document.createElement('a');
        oDIV.appendChild(oA);
        oA.href = '/basket/';
        oA.style.color = '#ce5d17';

        oSpan = document.createElement('span');
        oSpan.style.color = 'black';
        oSpan.innerHTML = 'Оформить заказ';
        oA.appendChild(oSpan);

        oSpan = document.createElement('span');
        oSpan.innerHTML = '&nbsp;&nbsp;&#187; ';
        oDIV.appendChild(oSpan);
	}
    G_selected();
}
