var fakeIMG;
function dce(tag) {
	return document.createElement(tag);
}
function dct(text) {
	return document.createTextNode(text);
}
function dge(id) {
	return document.getElementById(id);
}

function inputLimit(input, maxlen, left)
{
	inputstr = input.value;
	strlen = inputstr.length;
	if (strlen > maxlen)
		input.value = inputstr.substring(0, maxlen);
	if (left)
		left = document.getElementById(left);
	if (left) {
		if (left.tagName == 'INPUT')
			left.value = maxlen - input.value.length;
		else
			left.innerHTML = maxlen - input.value.length;
	}
	return true;
}

function showForm(id,show) {
  if (show) 
  {
    document.getElementById(id+'Link').style.display='none';
    document.getElementById(id+'Form').style.display='';
  }  
  else
  {
    document.getElementById(id+'Link').style.display='';
    document.getElementById(id+'Form').style.display='none';
  }
}
function toFriends(el, id, js) {
	if (!fakeIMG)
		fakeIMG = dce('IMG');
	var is = parseInt(el.getAttribute('is'));
	fakeIMG.src = js + '?itemID=' + id + '&set=' + (!is + 0) + '&_rand=' + Math.random();
	el.setAttribute('is', !is + 0);

	var tmp = el.innerHTML;
	el.innerHTML = el.getAttribute('text');
	el.setAttribute('text', tmp);

	var fclass = el.getAttribute('fclass');
	if (fclass) {
		var tmp = el.className;
		el.className = fclass;
		el.setAttribute('fclass', tmp);
	}
}
function toggleClass(obj,classN,togling){
	function test(clN,value){
		var myRe= new RegExp("\\b"+value+"\\b");
		return myRe.test(clN)?true:false;
		}
	function RMclass(clN,value){
		var rmclass = new RegExp ('(.*)(\\b'+value+'\\b)(.*)');
		return rmclass.exec(clN)[1]+rmclass.exec(clN)[3];
		}
	function rm(){
		var a=RMclass(obj.className,classN); 
		a.length==0 ? obj.removeAttribute($ifBrowser('MSIE') ? 'className' : 'class') : obj.className=a}
	function add(){obj.className += ' ' + classN}
	var k = test(obj.className,classN);
	switch (togling){
		case 'toggle':	if (k) 	{ rm() 	} else { add() }; 	break;
		case 'dell':	if (k) 	{ rm() 	}					break;
		case 'add':		if (!k) { add() }					break;
		}
}
/* voting */
function cvoting () {
	this.onmouseover = function(obj) {
		objs = obj.parentNode.getElementsByTagName('img');
		for (i=0;i<objs.length;i++) if (objs[i] == obj) curNr = i;
		for (i=0;i<objs.length;i++) 
		{
			if (i <= curNr ) objs[i].className='on';
			else objs[i].className='';
		}
	}
	this.onclick = function(obj, id, k, js) {
		if (!fakeIMG)
			fakeIMG = dce('IMG');

		fakeIMG.src = js + '?itemID=' + id + '&vote=' + k + '&_rand=' + Math.random();

		var elem = obj.parentNode.parentNode;
		elem.innerHTML = 'Рейтинг: ' + (parseInt(elem.getAttribute('rank')) + parseInt(k));
	}
	this.onmouseout = function (obj) {
		objs = obj.parentNode.getElementsByTagName('img');
		for (i=0;i<objs.length;i++) objs[i].className='';
	}
}
var voting = new cvoting();
function setStartPage(obj, site) {
	if (document.all)  {
		obj.style.behavior = 'url(#default#homepage)';
		obj.setHomePage(site);
		return false;
	}
	if (!document.layers) {
		netscape.security.PrivilegeManager.enablePrivilege-("UniversalPreferencesWrite");
		navigator.preference("browser.startup.homepage", site); 
	}
}

function browseIt()
{
	this.ver = navigator.appVersion.toLowerCase();
	this.verNum = parseInt(this.ver);
	this.agent = navigator.userAgent.toLowerCase();
	this.dom = (document.getElementById ? 1 : 0);
	this.opera = (this.agent.indexOf("opera") > -1 && this.dom ? 1 : 0);
	this.opera7 = (this.opera && this.verNum >= 7);
	this.opera9 = (this.opera && this.verNum >= 9);
	this.ie = (this.ver.indexOf("msie") > -1 && this.dom && !this.opera ? 1 : 0);
	this.webkit = this.ver.indexOf("applewebkit") > -1;
	this.chrome = this.ver.indexOf("chrome") > -1;
	this.safari = this.chrome ? false : this.ver.indexOf("safari") > -1;
	this.ieVer = 0;
	if (this.ie) {
		var pos = this.ver.indexOf("msie");
		if (pos != -1)
			this.ieVer = parseFloat(this.ver.substr(pos + 5));
	}
	this.ie6 = (this.ie && (this.ieVer >= 6));
	this.ie6only = (this.ie && (this.ieVer >= 6) && (this.ieVer < 7));
	this.ie7 = (this.ie && (this.ieVer >= 7));
	this.ie8 = (this.ie && (this.ieVer >= 8));
	this.macOS = (this.agent.indexOf("mac") > -1);
	this.mac = (this.macOS && this.verNum >= 7 ? 1 : 0);
	this.moz = (this.agent.indexOf("gecko") > -1);
	this.ns6 = (this.dom && this.agent.indexOf("netscape") > -1 && this.verNum >= 5 ? 1 : 0);
	this.ff = (this.agent.indexOf("firefox") > -1);
	this.b = (this.ie || this.ns6 || this.opera7 || this.mac || this.moz || this.dom);

	return this;
}

var b = new browseIt();
function reFillSelect(sel, opt, nulval)
{
	var nulval = nulval || false;
	while (sel.childNodes.length > 1) {
		sel.removeChild(sel.childNodes[sel.childNodes.length - 1]);
	}
	var o = null;
	var j, g;
	if (nulval) {
		o = dce('OPTION');
		sel.appendChild(o);
		o.innerHTML = nulval;
		o.value = 0;
	}
	for (var i in opt) {
		if (typeof(opt[i]) == "object") {
			g = dce('OPTGROUP');
			g.label = i;
			sel.appendChild(g);
			for (j in opt[i]) {
				o = dce('OPTION');
				g.appendChild(o);
				o.innerHTML = opt[i][j];
				o.value = j;
			}
		} else {
			o = dce('OPTION');
			sel.appendChild(o);
			o.innerHTML = opt[i];
			o.value = i;
		}
	}
}
function getCity(el, withlabel)
{
	var withlabel = withlabel || 0;
	if (typeof(cityreq) == "undefined")
		cityreq = new Subsys_JsHttpRequest_Js();
	el.form.fCity.disabled = true;
	var country = el.options[el.selectedIndex].value;
	if (country) {
		cityreq.city = el.form.fCity;
		cityreq.onreadystatechange = function() {
			if (cityreq.readyState == 4) {
				reFillSelect(cityreq.city, cityreq.responseJS.city, 'Выберите город');
				cityreq.city.disabled = false;
				if (typeof(cityreq.responseJS.labels) != "undefined") {
					reFillLabels(cityreq.responseJS.labels, cityreq.city.form.parentNode);
				}

			}
		};
		cityreq.caching = true;
		cityreq.open('GET', '/js/city.js', true);
		cityreq.send({ cid: country, withlabel: withlabel });
	} else {
		reFillSelect(el.form.fCity, [], true);
	} 
}
function reFillLabels(labels, el)
{
	var el = el.nextSibling;
	if (!el)
		return;
	while (el && el.tagName != 'DIV')
		el = el.nextSibling;
	if (el.tagName != 'DIV') 
		return;
	el.innerHTML = labels;
	if (labels == '')
		el.style.display = 'none';
	else
		el.style.display = '';

}
function clearFormError (form) {
	var tmp;
	for (var i=0; i < form.elements.length; i++) {
		tmp = dge(form.elements[i].name+'_errCtrl');
		if (tmp && typeof(tmp) != "undefined") {
			tmp.style.display='none';
			tmp.innerHTML = ''
		}
	}
}
