	function prTxt(val) {
		if (val > 4 && val < 21)	return "баллов";
		val = new String(val);
		val = new Number(val.substr(val.length - 1));
		if (val > 1 && val < 5)	return "балла";
		if (val == 1)	return "балл";
		if (val == 0 || val > 4)	return "баллов";
		return "балл"
	}

function getSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
	
function offsetx(e) {
	var	tmp = e.parentNode;
	var scr = 0;
	while (tmp != undefined) {
		if (tmp.scrollLeft != undefined && tmp.nodeName == 'DIV')
			scr += tmp.scrollLeft;
		tmp = tmp.parentNode;
	}
	tmp = e;
  var x = 0;
  while (tmp) {
    x += tmp.offsetLeft;
    tmp = tmp.offsetParent;
	}
	return x - scr;
}

function offsety(e) {
	var	tmp = e.parentNode;
	var scr = 0;
	while (tmp != undefined) {
		if (tmp.scrollTop != undefined && tmp.nodeName == 'DIV')
			scr += tmp.scrollTop;
		tmp = tmp.parentNode;
	}
	tmp = e;
  var y = 0;
  while (tmp) {
    y += tmp.offsetTop;
    tmp = tmp.offsetParent;
	}
	return y - scr;
}
/**
	* OBJECTS
*/

/**
	* LOADER
*/
function loader(frame_id) {
	this.state = 0;
	this.frame = document.createElement("iframe");
	this.frame.className = "pframe";
	this.frame._loader = this;
	this.frame.onload = this.onload;
	this.frame.onerror = this.onerror;
	this.frame.onabort = this.onerror;
  document.body.appendChild(this.frame);
}

loader.prototype = {
	onload: function () {
		this._loader.state = 0;
	},

	send: function (request) {
		if (this.state) return false;
		this.state = 1;
		try {
			this.frame.src = request;
		}
		catch (e) {
			alert(e);
			this.state = 0;
		}
		return true;
	},

	onerror: function () {
		alert('error');
	}
}

/**
	* HTML_OBJECT
*/
function htmlObj (tag) {
  this.tag = tag;
  this.style = '';
}

htmlObj.prototype = {
  fetch: function() {
    var res = '<'+this.tag;
    if (this.width) this.style += 'width:'+this.width+'px;';
    if (this.height) this.style += 'height:'+this.height+'px;';
    if (this.src) res += ' src="'+this.src+'"';
    if(this.onMouseOver) res += ' onMouseOver="'+this.onMouseOver+'"';
    if(this.onMouseOut) res += ' onMouseOut="'+this.onMouseOut+'"';
    if(this.onClick) res += ' onClick="'+this.onClick+'"';
    if(this.title) res += ' title="'+this.title+'"';
    if (this.style) res += ' style="'+this.style+'"';

    return res + '>';
  },

  display: function (doc) {
    if (doc == undefined) doc = document;
    doc.write(this.fetch());
  },

  setSize: function (width, height) {
    this.width = width;
    this.height = height;
  },

  setImg: function (singl, roll) {
    this.src = this.imgDir + singl;
    if (roll == undefined) return;
    this.onMouseOver = 'this.src=\''+this.imgDir + roll+'\'';
    this.onMouseOut = 'this.src=\''+this.imgDir + singl+'\'';
  },

  setOnClick: function(clk) {
    this.onClick = clk;
  },

  setTitle: function(title) {
    this.title= title;
  },

  setStyle: function(stl) {
    this.style += stl;
  }
}

/**
	* HTML_DOC
*/
function htmlDoc (selfName) {
  this.name = selfName;
  if (!this.name) this.name = 'bar';
  this.doc = document;
  this.imgDir;
  this.butCnt = 0;
}

htmlDoc.prototype = {

  getEl: function(el, doc) {
    if (doc == undefined) doc = this.doc;
    return doc.getElementById(el);
  },

  getVal: function (el, doc) {
    var el = this.getEl(el, doc);
    if (el == undefined) return false;
    return el.value;
  },

  setVal: function(id, val, doc) {
    var el = this.getEl(id, doc);
    if (el != undefined) el.value = val;
  },

  setHtml: function(id, val, doc) {
    var el = this.getEl(id, doc);
    if (el != undefined) el.innerHTML = val;
  },

  appendHtml: function(id, val, doc) {
    var el = this.getEl(id, doc);
    if (el != undefined) el.innerHTML += val;
  },

  getHtml: function(id, doc) {
    var el = this.getEl(id, doc);
    if (el == undefined) return false;
    return el.innerHTML;
  },

  img: function (width, height, img1, img2) {
    var el_img = new htmlObj('img');
    el_img.imgDir = this.imgDir;
    el_img.setSize(width, height);
    el_img.setImg(img1, img2);
    return el_img;
  },

  button: function (cap, click) {
    var el = new htmlObj('img');
    el.imgDir = 'images/butt/';
    el.setImg('button_left.gif');
    var res = '<table cellpadding="0" cellcpaceing="0"><tr style="cursor: pointer;"'+
              ' onMouseOver="'+this.name+'.getEl(\'main_b'+this.butCnt+'\').style.backgroundImage=\'url('+el.imgDir+'button_center-Roll.gif)\'"'+
              ' onMouseOut="'+this.name+'.getEl(\'main_b'+this.butCnt+'\').style.backgroundImage=\'url('+el.imgDir+'button_center.gif)\'"'+
              ' onClick="'+click+'">'+
              '<td>'+el.fetch()+'</td><td id="main_b'+
              this.butCnt+'" style=" background-image: url('+el.imgDir+'button_center.gif); background-repeat: repeat-x;"'+
              '>'+ cap + '</td><td>';
    el.setImg('button_right.gif');
    res += el.fetch() + '</td></tr></table>';
    this.butCnt++;
    return res;
  },

  strech: function (id, doc) {
    var el = this.getEl(id, doc);
    if (el == undefined) return false;
    el.style.left = 0+'px';
    el.style.top = 0+'px';
    sz = getSize();
    el.style.width = sz[0]+'px';
    el.style.height = sz[1]+'px';
  },

  center: function(id, doc) {
    var el = this.getEl(id, doc);
    if (el == undefined) return false;
    sz = getSize();
    el.style.left = (sz[0] - el.clientWidth) / 2 + 'px';
    el.style.top = (sz[1] - el.clientHeight) / 2 + 'px';
  }
}

prev_pn = 1;
/**
	* листание фоток
*/
function pgSel(el, last) {
	if (!el || el.id == "pn" + prev_pn) return;
 	if (last == undefined) last = 10;
 	doc.getEl("pn"+prev_pn).className = "pn";

  if (el.id == "pn_first") prev_pn--;  else if (el.id == "pn_last") prev_pn++;
  else prev_pn = el.id.substr(2);

  doc.getEl("pn_first").style.display = (prev_pn > 1) ? "" : "none";
  doc.getEl("pn_last").style.display = (prev_pn < last) ? "" : "none";
 	doc.getEl("pn"+prev_pn).className = "pnc";
}

function selPic() {
	var f = doc.getEl("foto");
	if (!f) return;
	f.src = "photo/s"+fotoz[prev_pn - 1];
}

/**
 * увеличение фоток
 */
function zoom() {
	var f = doc.getEl("foto");
	if (!f) return;
  var bod = document.getElementsByTagName("body").item(0);
	var div = document.createElement("div");
	div.id = 'bd_lock';
	var img = document.createElement("img");
	img.src = "photo/"+fotoz[prev_pn - 1];
	img.className = "foto";
	var div2 = document.createElement("div");
	div2.id = "lock_f";
	div2.onclick = function () {bod.removeChild(div); bod.removeChild(div2);bod.className = "";}
	div2.title = "Закрыть";
	div2.appendChild(img);
  bod.appendChild(div);
  bod.appendChild(div2);
  bod.className = "noScroll";
}

function chkFile() {
	var files = document.getElementsByTagName("input");
	for (var i=0; i<files.length; i++) {
		if (files[i].type != "file") continue;
		files[i].onchange = files[i].onmouseout = function () {this.relatedElement.value = this.value;}
		var fakeFileUpload = document.createElement('div');
		fakeFileUpload.className = 'fakefile';
		var tab1 = document.createElement("table");
		var row = tab1.insertRow(-1);

		var y = row.insertCell(-1);
		y.appendChild(document.createElement('input'));
		y = row.insertCell(-1);

		var image = document.createElement('img');
		image.src='img/sel.gif';
		y.appendChild(image);
		fakeFileUpload.appendChild(tab1);
		files[i].relatedElement = fakeFileUpload.getElementsByTagName('input')[0];
		files[i].parentNode.appendChild(fakeFileUpload);
	}
}

function getHTTPObject() {
	if (typeof XMLHttpRequest != 'undefined') return new XMLHttpRequest();
	 try {
		return new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
	return false;
}

//popUpForm
var popUpLevel = 0;

function popUpForm (cap, form_id, copy, doc) {
	this.bod = document.getElementsByTagName("body").item(0);
	this.bandiv = document.createElement("div");
	this.cap = cap;
	this.form_id = form_id;
	this.copy = copy;
	this.bandiv.id = 'bd_'+form_id;
	this.bandiv.className = 'cs_bann';
	this.bod.appendChild(this.bandiv);
	this.doc = doc;
	if (this.doc == undefined) this.doc = new htmlDoc();
	this.show_footer = false;
	this.onDontDisp;
}

popUpForm.prototype = {
	init: function () {
		if (this.copy) {
			if (!this.doc.getEl(this.form_id)) {
				alert("popupForm: Не найден источник!");
				return false;
			}
			this.form = document.createElement("div");
			
			var cap_div = document.createElement("div");
			cap_div.className = "cap";
			
			var tmp = document.createElement("span");
			
			tmp.appendChild(document.createTextNode("Закрыть"));
			
			tmp._popup = this;
			tmp.onclick = function(){this._popup.hide();};
	
			var tmp2 = document.createElement("div");
			tmp2.className = "close";
			tmp2.appendChild(tmp);
			
			cap_div.appendChild(document.createTextNode(this.cap));
			cap_div.appendChild(tmp2);
			
			
			this.form.className = 'popup_form';
			this.form.appendChild(cap_div);
			
			this.form.id = 'id_'+this.form_id;
			var ih = doc.getHtml(this.form_id);
			tmp = document.createElement("div");
			tmp.innerHTML = ih.substr(4, ih.length - 7);
			tmp.className = "cont";
			this.form.appendChild(tmp);
			
			if (this.show_footer) {
				tmp = document.createElement("div");
				tmp.className = "opt";
				
				tmp2 = document.createElement("input");
				tmp2.type = "checkbox";
				tmp2.id = "hint_not_show";
				tmp2._popup = this;
				if (this.onDontDisp)
					tmp2.onclick = function() {this._popup.onDontDisp(this.checked);};
				tmp.appendChild(tmp2);
				
				tmp2 = document.createElement("label");
				tmp2.htmlFor = "hint_not_show";
				tmp2.appendChild(document.createTextNode("Больше не показывать это сообщение"));
				tmp.appendChild(tmp2);
				
				this.form.appendChild(tmp);
			}
			//this.form.insertBefore(this.form.firstChild, cap_div);
			this.bod.appendChild(this.form);
		}
		else {
			this.form = html.getEl(form_id);
			if (this.form) alert("popupForm: Не найден источник!");
		}
	},

    display: function () {
		if (!this.form) return false;
		
		this.bandiv.style.zIndex = 100 + popUpLevel;
		this.form.style.zIndex = 100 + popUpLevel + 1;
		popUpLevel += 2;
//      html.strech(this.bandiv.id);
		this.doc.center(this.form.id);
		this.bandiv.style.visibility = 'visible';
		this.form.style.visibility = 'visible';
		this.bod.className = "noScroll";
    },

    hide : function () {
		if (!this.form) return false;
		popUpLevel -= 2;
		if (popUpLevel < 0) popUpLevel = 0;
		this.form.style.visibility = 'hidden';
		this.bandiv.style.visibility = 'hidden';
		this.bod.className = "";
    }
}

function dropDown(el, link, onsel) {
	this.el = document.getElementById(el);
	this.el._dd = this; 
	this.link = link;
	this.onSelect = onsel;
	this.el.onclick = this.display;
}

dropDown.prototype = {
	_init: function() {
		if (this._box) return;
		this._box = document.createElement('div');
		this._box._dd = this;
		this._box.style.left = offsetx(this.el)+'px';
		this._box.style.top = (offsety(this.el) + this.el.offsetHeight + 2) +'px';
		this._box.className = "dropDown";
		
		var div = document.createElement('div');
		div.style.padding = "0 2px";
		div.style.borderBottom = "1px solid #c0c0c0";
		div.style.marginBottom = "5px";
		div.style.backgroundColor = "#abc3cb";
		var d2 = document.createElement('span');
		d2.appendChild(document.createTextNode('[закрыть]'));
		d2.style.cssFloat = "right";
		d2.style.cursor = "pointer";
		d2._dd = this;
		d2.onclick = function() {this._dd.hide();};
		div.appendChild(d2);
		
		d2 = document.createElement('div');
		d2.style.clear = "both";
		div.appendChild(d2);
		this._box.appendChild(div);
		
		
		
		var cont = document.createElement('div');
		cont.className = "ddCont";
		this._box.appendChild(cont);
		bod = document.getElementsByTagName("body").item(0);
		bod.appendChild(this._box);	
		this._load(this.link);
	},
	
	_load: function(link) {
		if (this._loading) return;
		if (this._data) return;
		this._loading = 1;
		var loader = getHTTPObject();
		loader._dd = this;
		loader.onreadystatechange = function() {
			if (loader.readyState == 4) this._dd._updateList(loader.responseText);
		}
		loader.open("POST", "http://"+document.domain+"/index.php", true);
		loader.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		loader.send(link);
	},
	
	_updateList: function (data) {
		var dta = this._box.childNodes[1];
		data = data.split("\n");
		if (data[0].substr(0, 3) != '#OK') {
			alert(data[0]);
			return;
		}
		
		for (var i=1; i<data.length; i++) {
			var line = data[i].split(';');
			if (line.length < 2) continue;
			
			var div = document.createElement('div');
			div.className = "row";
			div._line = line;
			div.onmouseover = function() {this._color = this.style.backgroundColor; this.style.backgroundColor = "#00a0cc"};
			div.onmouseout = function() {this.style.backgroundColor = this._color};
			div.appendChild(document.createTextNode(line[1]));
			if (this.onSelect)
				div.onclick = function() {
					this.parentNode.parentNode._dd.onSelect(this._line);
					this.parentNode.parentNode._dd.hide();
			};
			else div.onclick = function() {this.parentNode.parentNode._dd.hide();};
			dta.appendChild(div);
		}
		
	},
	
	display: function() {
		this._dd._init();
		this._dd._box.style.visibility = 'visible';
	},
	
	hide: function() {
		this._box.style.visibility = 'hidden';
	}		
}
