lastFilter = 0;

startList = function() 
{
    if (document.all && document.getElementById) 
    {
        navRoot = document.getElementById("topMenu");
        if(!navRoot) return;
        for (i=0; i<navRoot.childNodes.length; i++) 
        {
            node = navRoot.childNodes[i];
            if (node.nodeName=="LI") 
            {
                node.onmouseover=function() { this.className+=" over"; }
                node.onmouseout=function() { this.className=this.className.replace(" over", "");  }
            }
        }
    }
}
window.onload=startList;

function showProd(obj) {
    window.open(obj.href, null, 'width=720,height=650,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1');    
}

function add2Fav() 
{
    ie = document.all?true:false;
    if(ie) window.external.AddFavorite(document.location.href, document.title); 
    else window.sidebar.addPanel(document.title,location.href,''); 
}

function checkCanCompare(frm)
{
    chkBoxesChecked = 0;
    tbl = document.getElementById('prodlist');
    if(tbl==null) return false;
    chkBoxes = tbl.getElementsByTagName('input');
    chkBoxesCount = chkBoxes.length;
    for(i = 0; i < chkBoxesCount; i++)
        if(chkBoxes[i].checked)
            if(++chkBoxesChecked > 1) return true;
    alert('Необходимо выбрать хотя бы 2 продукта!')
    return false;
}

function filterClick(filterId) 
{
   var filter = document.getElementById(filterId);
	if (lastFilter == filter ) {
		filter.style.display = (filter.style.display == 'block' ? 'none' : 'block');
	} else {	 
		if (lastFilter) lastFilter.style.display = 'none';
		filter.style.display = 'block';
	}
	lastFilter = filter;
}

function markCheckbox(formName, nameSubstr, flag) {
    if (flag == null) flag = true;
    var cForm = document.forms[formName];
    if (cForm != null) {
        for (i = 0; i < cForm.elements.length; i++) {
            var elem = cForm.elements[i];
            if (elem.nodeName.toLowerCase() == 'input' && elem.type == 'checkbox' && elem.name.indexOf(nameSubstr) >= 0) {
                elem.checked = flag;
            }
        }
    }
}

function umarkCheckbox(formName, nameSubstr) {
    markCheckbox(formName, nameSubstr, false);
}

function allCheckboxMarked(formName, nameSubstr) {
    var countMark = countCheckbox = 0;
    var cForm = document.forms[formName];
    if (cForm != null) {
        for (i = 0; i < cForm.elements.length; i++) {
            var elem = cForm.elements[i];
            if (elem.nodeName.toLowerCase() == 'input' && elem.type == 'checkbox' && elem.name.indexOf(nameSubstr) >= 0) {
                if (elem.checked == true) countMark++;
                countCheckbox++;
            }
        }
    }
    
    if (countMark == countCheckbox) return true;
    return false;
}

function quikFilterSubmit(formName, checkboxNameSubstr) {
    var allMarked = allCheckboxMarked(formName, checkboxNameSubstr);
    if (allMarked == true) {
        var hiddenFlag = document.createElement('input');
        hiddenFlag.type = 'hidden';
        hiddenFlag.name = 'all_marked';
        hiddenFlag.value = 1;
        document.forms[formName].appendChild(hiddenFlag);
    }
    document.forms[formName].submit();
}

function addPicture(message) {
    var url = prompt(message, '');
    if (url != null)
      document.getElementById('text').value = document.getElementById('text').value+'<img src="'+url+'"/>';
}

function submitTo(formNode, toUri) {
    if (formNode != null && formNode.tagName.toLowerCase() == 'form') {
        formNode.action = toUri;
        formNode.submit();
    }
}

function openW(url, name, w, h, noScroll) {
	if(noScroll==1) window.open(url,window.name+'qq','toolbar=no,location=no,status=no,menubar=no,resizable=no,directories=no,scrollbars=no,width='+w+',height='+h+'');
	else  window.open(url,window.name+'qq','toolbar=no,location=no,status=no,menubar=no,resizable=no,directories=no,scrollbars=yes,width='+w+',height='+h+'');
}

function checkEqual() {
    frm = document.forms['personal'];
    pass1 = document.forms['personal']['password'];
    pass2 = document.forms['personal']['password2'];
    msg = document.getElementById('passEqual');
    if(pass1.value == pass2.value && pass1.value != '') msg.style.visibility = 'visible';
    else msg.style.visibility = 'hidden';
}

function addProducts2ToCart(formName, nameSubstr, toUri, errorStr) {
	var result = checkMark(formName, nameSubstr);
	if (result == false) alert(errorStr);
	else submitTo(document.forms[formName], toUri);
}

function checkMark(formName, nameSubstr, need) {
    var countMark = 0;
    var needMark = (need == null ? 1 : need);
    var cForm = document.forms[formName];
    if (cForm != null) {
        for (i = 0; i < cForm.elements.length; i++) {
            var elem = cForm.elements[i];
            if (elem.nodeName.toLowerCase() == 'input' && elem.type == 'checkbox' && elem.name.indexOf(nameSubstr) >= 0) {
                if (elem.checked == true) countMark++;
            }
        }
    }
    
    if (countMark >= needMark) return true;
    return false;
}
