// JavaScript Document

function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
}

function getSourceElement(event)
{
	if (browser.isIE)
		el = window.event.srcElement;
	else
		el = (event.target.tagName ? event.target : event.target.parentNode);
}

function getChildNodes(element, tag) {
  var r = new Array();
  var nodes = element.childNodes;
  for (var i = 0; i<nodes.length; i++) {
    if (nodes[i].tagName == tag) {
      r.push(nodes[i]);
    }
  }
  return r;
}
function getFirstChild(element, tag ) {
	if( element ) {
		var nodes = element.childNodes;
		for (var i = 0; i<nodes.length; i++) {
			if (nodes[i].tagName == tag)
				return nodes[i];
		}
	} else {
		return null;
	}
}

// finds first parent Element with TagName == "DIV"
// returns NULL if not found such element
function parentDiv( obj ) {
	var srcDiv = obj;
	while( srcDiv!=null && srcDiv.tagName!="DIV" ) {
		srcDiv = srcDiv.parentNode;
	}
	return srcDiv;
}


var browser = new Browser();

if (browser.isIE)
	document.onmousedown = getSourceElement;
else
    document.addEventListener("mousedown", getSourceElement, true);



/* Advanced search switcher */
window.onload = function()
{
    fixHeights();
	var switcherControl = document.getElementById("advancedSwitcher");
	if(!switcherControl) return false;
	
	switcherControl.onclick = function()
	{
		var advSearchPane = document.getElementById("advancedSearchPane");
		if(!advSearchPane) return false;
		advSearchPane.style.display = ( advSearchPane.style.display == "block" ) ? "none" : "block";
		this.className = (this.className.indexOf("current") > - 1) ? "" : "current";
	}
}

function fixHeights()
{
    var container = document.getElementById("container");
    if(!container) return false;

    for(var i = 0; i < container.childNodes.length; i++)
    {
        //alert(container.childNodes[i].tagName);
        if(container.childNodes[i].tagName && container.childNodes[i].className.indexOf("left-col") > -1)
            var leftCol = container.childNodes[i];
        if(container.childNodes[i].tagName && container.childNodes[i].className.indexOf("content") > -1)
            var rightCol = container.childNodes[i];
    }
    // Do nothing if no loaft col exist
    if(!leftCol) return false;
    // Fix height of left col if it more than content pane
    if(leftCol && rightCol)
        rightCol.style.height = (rightCol.offsetHeight < leftCol.offsetHeight) ? leftCol.offsetHeight+"px" : "auto";
}

function GotoList(selected)
{
    window.location.href="search.php?okved="+selected;
}

function GotoCCI(selected)
{
    window.location.href="?CCI="+selected;
}

function OnOK()
{
}

var CurrentFrameSection=0;

function ShowFrameSection(id)
{
    document.getElementById('section'+CurrentFrameSection).style.display='none';
    document.getElementById('lm'+CurrentFrameSection).className='notcurrent';
    document.getElementById('section'+id).style.display='block';
    document.getElementById('lm'+id).className='current';
    CurrentFrameSection=id;
    fixHeights();
}

function fnShowProps(obj, objName){
    var result = "";
    for (var i in obj) // обращение к свойствам объекта по индексу
        result += objName + "." + i + " = " + obj[i] + "<br />\n";
    window.alert(result);
    //document.write(result);
}

function voteframe(ID, FrameID)
        {
        var val = '' + document.getElementById('vote').value;
        var req = new Subsys_JsHttpRequest_Js();
        req.onreadystatechange = function() {
                if (req.readyState == 4) {
                //window.alert(req);
                        if (req.responseJS) {
                                  var stars = document.getElementById('stars');
                                  stars.style.background.left = '0px';
                                  stars.style.background.top = '-198px';
                                  stars.style.background_position = '0px -198px;'
                                  stars.innerHTML = req.responseJS.rating;
                                  document.getElementById('rating').style.display = 'none';
//                                        document.getElementById('votediv').innerHTML =
//                                                '<h2>Рейтинги</h2>' +
//                                                '<ul>' +
//                                                '        <li>Рейтинг: ' + (req.responseJS.rating) + '</li>' +
//                                                '        <li>Голосов: ' + (req.responseJS.vote) + '</li>' +
//                                                '</ul>'
                                }
                        }
                }
        req.caching = false;
        req.open('POST', '/voteframe.php', true);
        req.send({ FrameID: FrameID, StatID: ID, vote: val });
        }
