function checkGoal(obj)
{
	var cont = obj.parentNode.parentNode; // див в котором все лежит
	var label = obj.parentNode; // label
	if (obj.checked) {
		cont.style.background = '#0A96D1';
		cont.style.color = '#FEFEFE';
		label.style.borderColor = '#FEFEFE';
	} else {
		cont.style.background = 'none';
		cont.style.color = '#333';
		label.style.borderColor = '#333';
	}
}

function inCmp(id, el)
{
	var cookie = getCookie('cmp');
	if (cookie) {
		var cookieList = cookie.split('+');
		if ( ! inArray(id, cookieList)) {
			setCookie('cmp', cookie + '+' + id, 0, '/');
		}
	} else {
		setCookie('cmp', id, 0, '/');
	}
	var img = document.createElement("img");
	img.src = '/img/compare/tick.gif';
	img.alt = 'Добавлено для сравнения';
	el.parentNode.replaceChild(img, el);
}

function inArray(needle, arr)
{
	var i;
	for (i=0; i < arr.length; i++) {
		if (arr[i] === needle) {
			return true;
		}
	}
	return false;
}

function goCompare()
{
	var cookie = getCookie('cmp');
	if (! cookie) {
		alert('Пожалуйста, выберите нужные Вам ноутбуки.');
	} else {
		location.href = 'http://www.comprus.ru/compare/?list='+cookie;
	}
}

function switchCompare(type)
{
	var set = [
	'chip', 'chipset', 'l2', 'bus', 'fsb', 'nbrige', 'sbrige',
	'm_type', 'm_bus',
	'hdd_rpm',
	'd_type',
	'v_bus', 'v_memory_type',
	'sound', 's_processor', 's_codec', 's_channel',
	'n_speed', 'network', 'n_controller',
	'bt_controller', 'wf_controller',
	'b_capacity', 'b',
	'dim_w', 'dim_h', 'dim_d',
	'p_ps2', 'p_irda', 'p_line_in', 'p_line_out', 'p_microphone_in', 'p_spdif', 'p_rj11', 'p_rj45', 'p_vga', 'p_svideo', 'p_hdmi',];
	for(var prop in set){
		toggle('id'+set[prop]);
	}

	if (type=='long'){
		document.getElementById('switch').innerHTML = '<a href="#" onclick="switchCompare(\'short\');return false;">кратко</a><span>подробно</span>';
	} else {
		document.getElementById('switch').innerHTML = '<span>кратко</span><a href="#" onclick="switchCompare(\'long\');return false;">подробно</a>';
	}
}

function nbMenuClick(obj)
{
	document.getElementById('nb_1').className='nbP'
	document.getElementById('nb_2').className='nbP'
	document.getElementById('nb_3').className='nbP'
	obj.parentNode.className='nbA';
}

function actionRefresh()
{
	var cont = document.getElementById('actionRot');
	var rnd  = Math.floor(Math.random()*2) + 1;
	cleanNode(cont);
	cont.appendChild(document.getElementById('action' + rnd).cloneNode(true));
}

function cleanNode(dest) {
	var child;
	while (child = dest.firstChild) {
		dest.removeChild(child);
	}
}

function showUr()
{
	var fiz = document.getElementById('fizface');
	var ur = document.getElementById('urface');
	fiz.style.display = 'none';
	ur.style.display = '';
}

function showFiz()
{
	var fiz = document.getElementById('fizface');
	var ur = document.getElementById('urface');
	ur.style.display = 'none';
	fiz.style.display = '';
}

function menuOver(obj)
{
	obj.style.background='url(/img/cont/header_blue.gif) repeat-x';
	obj.style.color='#FFF';
	obj.firstChild.style.color='#FFF';
}

function menuOut(obj)
{
	obj.style.background='';
	obj.style.color='#535353';
	obj.firstChild.style.color='#535353';
}

function rT(path, arr, cont)
{
	for(var i = 0; i < arr.length; i++)
	{
		var l = document.getElementById('thumb' + arr[i]);
		l.className = '';
	}
	document.getElementById('imgCont').innerHTML = '<img src="' + path + '" />';
	document.getElementById('imgBigLink').href = path.replace(/.jpg/, '_raw.jpg');
	cont.parentNode.className = 'aThumb';
}

function setElementOpacity(oElem, nOpacity)
{
	var p = getOpacityProperty();
	(setElementOpacity = p=="filter"?new Function('oElem', 'nOpacity', 'nOpacity *= 100;	var oAlpha = oElem.filters["DXImageTransform.Microsoft.alpha"] || oElem.filters.alpha;	if (oAlpha) oAlpha.opacity = nOpacity; else oElem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";'):p?new Function('oElem', 'nOpacity', 'oElem.style.'+p+' = nOpacity;'):new Function)(oElem, nOpacity);
}

function delLast()
{
	var cont = document.getElementById('newsRefresh');
	cont.removeChild(cont.lastChild);
}

function addFirst()
{
	var cont = document.getElementById('newsRefresh');
	var el = document.createElement('div');
	el.innerHTML = 'news refresh test';
	cont.insertBefore(el, cont.firstChild);
}

function icq_submit(event){
	if (event.keyCode==13 && event.ctrlKey) {
		document.getElementById('compose').submit();
	}
}

function toggle(obj)
{
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
		return false;
	} else {
		el.style.display = '';
		return true;
	}
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
	( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
	( ( path ) ? ';path=' + path : '' ) +
	( ( domain ) ? ';domain=' + domain : '' ) +
	( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
	( ( path ) ? ';path=' + path : '') +
	( ( domain ) ? ';domain=' + domain : '' ) +
	';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	if ( node == null )
	node = document;
	if ( tag == null )
	tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

function deleteColumn(tblId, colNum)
{
	var allRows = document.getElementById(tblId).rows;
	for (var i=0; i<allRows.length; i++) {
		if (allRows[i].cells.length > 1) {
			allRows[i].deleteCell(colNum);
		}
	}
}

/**
*
*  Sortable HTML table
*  http://www.webtoolkit.info/
*
**/

function SortableTable (tableEl) {

    this.tbody = tableEl.getElementsByTagName('tbody');
    this.thead = tableEl.getElementsByTagName('thead');
    this.tfoot = tableEl.getElementsByTagName('tfoot');

    this.getInnerText = function (el) {
        if (typeof(el.textContent) != 'undefined') return el.textContent;
        if (typeof(el.innerText) != 'undefined') return el.innerText;
        if (typeof(el.innerHTML) == 'string') return el.innerHTML.replace(/<[^<>]+>/g,'');
    }

    this.getParent = function (el, pTagName) {
        if (el == null) return null;
        else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())
            return el;
        else
            return this.getParent(el.parentNode, pTagName);
    }

    this.sort = function (cell) {

        var column = cell.cellIndex;
        var itm = this.getInnerText(this.tbody[0].rows[1].cells[column]);
        var sortfn = this.sortCaseInsensitive;

        if (itm.match(/\d\d[-]+\d\d[-]+\d\d\d\d/)) sortfn = this.sortDate; // date format mm-dd-yyyy
        if (itm.replace(/^\s+|\s+$/g,"").match(/^[\d\.]+$/)) sortfn = this.sortNumeric;

        this.sortColumnIndex = column;

        var newRows = new Array();
        for (j = 0; j < this.tbody[0].rows.length; j++) {
            newRows[j] = this.tbody[0].rows[j];
        }

        newRows.sort(sortfn);

        if (cell.getAttribute("sortdir") == 'down') {
            newRows.reverse();
            cell.setAttribute('sortdir','up');
        } else {
            cell.setAttribute('sortdir','down');
        }

        for (i=0;i<newRows.length;i++) {
            this.tbody[0].appendChild(newRows[i]);
        }

    }

    this.sortCaseInsensitive = function(a,b) {
        aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]).toLowerCase();
        bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]).toLowerCase();
        if (aa==bb) return 0;
        if (aa<bb) return -1;
        return 1;
    }

    this.sortDate = function(a,b) {
        aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]);
        bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]);
        date1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
        date2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
        if (date1==date2) return 0;
        if (date1<date2) return -1;
        return 1;
    }

    this.sortNumeric = function(a,b) {
        aa = parseFloat(thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]));
        if (isNaN(aa)) aa = 0;
        bb = parseFloat(thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]));
        if (isNaN(bb)) bb = 0;
        return aa-bb;
    }

    // define variables
    var thisObject = this;
    var sortSection = this.thead;

    // constructor actions
    if (!(this.tbody && this.tbody[0].rows && this.tbody[0].rows.length > 0)) return;

    if (sortSection && sortSection[0].rows && sortSection[0].rows.length > 0) {
        var sortRow = sortSection[0].rows[0];
    } else {
        return;
    }

    for (var i=0; i<sortRow.cells.length; i++) {
        sortRow.cells[i].sTable = this;
        sortRow.cells[i].onclick = function () {
            this.sTable.sort(this);
            return false;
        }
    }

}