var photos = function(obj)
{
	this.obj = obj;
	var thisobj = this;
	var images = [];
	
	this.init = function()
	{
		var imgs = this.obj.getElementsByTagName('IMG');
		var count = imgs.length;
		for (var i=0; i<count; i++)
		{
			imgs[i].onclick = this.openwindow;
			imgs[i].style.cursor = 'pointer';
			var tmp = new Image();
			tmp.src = imgs[i].src.replace(/images\/photo\/100/, '/images/photo/500/');
			images[imgs[i].src] = tmp;
		}
	}
	
	this.openwindow = function()
	{
		var path = images[this.src].src;
		var width = images[this.src].width;
		var height = images[this.src].height;
		var windowobj = window.open(path, 'newWindow', 'width='+(width + 40)+'px, height='+(height + 30)+'px, status=yes, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=yes');
		windowobj.document.onclick = function(){windowobj.close();}
	}
}

var selecthandle = function(oSel1, oSel2, oFrame)
{
	this.msel = oSel1;
	this.orsel = oSel2;
	var self = this;
	var vSel1 = 0;
	var vSel2 = 0;
	this.frame = oFrame;

	this.changeFrame = function()
	{
		if (this.id == 'selectmetro')
		vSel1 = this.options[this.selectedIndex].value;
		else vSel2 = this.options[this.selectedIndex].value;
		if (!!self.frame)
		self.frame.src = '/templates/sellpoints/iframe.php?okrug='+vSel2+'&metro='+vSel1;
		else window.location = '/selling/?okrug='+vSel2+'&metro='+vSel1;
	}
	
	this.init = function()
	{
		this.msel.onchange = this.changeFrame;
		this.orsel.onchange = this.changeFrame;
		var curm = oSel1.options[this.msel.selectedIndex].value;
		var curo = oSel2.options[this.orsel.selectedIndex].value;
		if (curm != 0) vSel1 = curm;
		if (curo != 0) vSel2 = curo;
		if (vSel1 != 0 || vSel2 != 0)
		if (!!this.frame)
		this.frame.src = '/templates/sellpoints/iframe.php?okrug='+vSel2+'&metro='+vSel1;
	}
}

var checkform = function(obj)
{
	this.obj = obj;
	var thisobj = this;
	
	this.init = function()
	{
		this.obj.onsubmit = this.perform;
	}
	
	this.perform = function()
	{
		var count = thisobj.obj.length;
		for (var i=0; i<count; i++)
		{
			var name = thisobj.obj[i].nodeName;
			switch (name)
			{
				case 'SELECT':
				var co = thisobj.obj[i].selectedIndex;
				var value = thisobj.obj[i].options[co].value;
				if (value != 0) return true;
				break;
				case 'INPUT':
				if (thisobj.obj[i].type == 'text')
				{
					var value = thisobj.obj[i].value;
					if (value != '') return true;
				}
				break;
			}
		}
		alert('Необходимо выбрать хотя бы один параметр!');
		return false;
	}
}

var validform = function(objname, fields)
{
	this.obj = false;
	this.objname = objname;
	this.fields = fields;
	var thisobj = this;
	
	this.init = function()
	{
		this.obj = document.forms[this.objname];
		if (!this.obj || !fields || fields.length == 0) return false;
		this.obj.onsubmit = this.valid;
	}
	
	this.valid = function()
	{
		for (var i=0; i<thisobj.fields.length; i++)
		{
			var name = thisobj.obj[thisobj.fields[i]].nodeName;
			switch (name)
			{
				case 'SELECT':
				var co = thisobj.obj.elements[thisobj.fields[i]].selectedIndex;
				var value = thisobj.obj.elements[thisobj.fields[i]].options[co].value;
				if (value == 0 || value == '')
				{
					alert('Необходимо ввести обязательные параметры!');
					return false;
				}
				break;
				case 'INPUT':
				if (thisobj.obj[i].type == 'text')
				{
					var value = thisobj.obj.elements[thisobj.fields[i]].value;
					if (value == '')
					{
						alert('Необходимо ввести обязательные параметры!');
						thisobj.obj.elements[thisobj.fields[i]].focus();
						return false;
					}
				}
				break;
			}
		}
		return true;
	}
}

var reqobjfunc = false;
var reqobj = function()
{
	if (reqobjfunc) return reqobjfunc();
	else if (window.ActiveXObject)
	{
		reqobjfunc = function()
		{return new ActiveXObject("Microsoft.XMLHTTP");}
	}
	else
	{
		reqobjfunc = function()
		{return new XMLHttpRequest;}
	}
	return reqobj();
}

var sertificate = function()
{
	this.cselector = false;
	this.pselector = false;
	var thisobj = this;
	var req = false;
	
	this.init = function()
	{
		this.cselector = document.getElementById('cliniclist');
		this.pselector = document.getElementById('programmlist');
		if (!this.cselector || !this.pselector) return false;
		this.cselector.onchange = this.clchange;
	}
	
	this.clchange = function()
	{
		req = reqobj();
		var current = this.options[this.selectedIndex].value;
		req.open('get', '/programms/xml/'+current+'/');
		req.onreadystatechange = thisobj.refreshpsel;
		req.send(null);
	}
	
	this.refreshpsel = function()
	{
		if (req.readyState != 4 || req.status != 200) return false;
		clean(thisobj.pselector);
		thisobj.pselector.disabled = true;
		if (appendvalues(req.responseXML)) thisobj.pselector.disabled = false;
	}

	var clean = function(sel)
	{
		var opts = sel.getElementsByTagName('OPTION');
		var count = opts.length - 1;
		if (count == 0) return true;
		for (var i=count; i>=0; i--)
			sel.removeChild(opts[i]);
	}
	
	var appendvalues = function(xml)
	{
		var items = xml.getElementsByTagName('item');
		var count = items.length;
		if (count == 0) return false;
		for (var i=0; i<count; i++)
		{
			var opt = document.createElement('OPTION');
			opt.value = items[i].getAttribute('id');
			opt.innerHTML = items[i].getAttribute('value');
			thisobj.pselector.appendChild(opt);
		}
		return true;
	}
}

function G(selector, container)
{
	container = container || document;
	selector = selector.replace(/\s/, '');
	//смотрим, есть ли последовательность элтов
	var isSequence = selector.indexOf('>');
	var dotNum = selector.indexOf('.');
	var cellNum = selector.indexOf('#');
	if (isSequence != -1)
	{
		var parentSelector = selector.substr(0, isSequence);
		var childSelector = selector.substr(isSequence);
		var elements = G(parentSelector, container) || false;
		if (!elements) return false;
		var count = elements.length || false;
		if (!count) return false;
		for (var i=0; i<count; i++)
		{}
		return false;
	}
	if (/^(\*|[a-z0-9]+)$/i.test(selector))
	{
		return container.getElementsByTagName(selector.toUpperCase());
	}
	else if (dotNum != -1)
	{
		var el = selector.substr(0, dotNum);
		el = el == ''?'*':el;
		var clname = selector.substr(dotNum);
		var elements = container.getElementsByTagName(el.toUpperCase()) || false;
		if (!elements) return false;
		var count = elements.length || false;
		if (!count) return false;
		var array = [];
		for (var i=0; i<count; i++)
		{
			if (elements[i].className == clname) array.push(elements[i]);
		}
		return array;
	}
	else if (cellNum != -1)
	{
		return document.getElementById(selector.substr(cellNum + 1));
	}
}

var voting = function(container, module, place)
{
	this.container = container;
	this.place = place;
	this.imgcontainer = G('div', this.container)[0];
	this.activeImgSrc = '/images/voting/star1.gif';
	this.inactiveImgSrc = '/images/voting/star0.gif';
	this.module = module;
	
	this.activeImgObj = new Image();
	this.activeImgObj.src = this.activeImgSrc;
	
	this.inactiveImgObj = new Image();
	this.inactiveImgObj.src = this.inactiveImgSrc;
	
	this.cfg = null;
	var self = this;
	
	this.init = function()
	{
		var req = reqobj();
		req.open('get', '/modules/voting.php?module='+this.module+'&id='+this.place);
		req.onreadystatechange = function()
		{
			if (req.readyState != 4 || req.status != 200) return false;
			self.cfg = eval('(' + req.responseText + ')');
			req.abort();
			for (var i=0; i<5; i++)
			{
				var img = i < self.cfg.intBall ? self.activeImgObj.cloneNode(true) : self.inactiveImgObj.cloneNode(true);
				img.id = 'vote-' + self.module + '-' + i;
				if (self.cfg.disabled == 0)
				{
					img.onmouseover = self.reDraw;
					img.onmouseout = self.reStore;
					img.onclick = self.vote;
					img.style.cursor = 'pointer';
				}
				self.imgcontainer.appendChild(img);
			}
			var nums = G('span', self.container);
			nums[0].innerHTML = self.cfg.ball;
			nums[1].innerHTML = self.cfg.voters;
		};
		req.send(null);
	}
	
	this.reStore = function()
	{
		var num = self.cfg.intBall;
		self.fill(num);
	}
	
	this.reDraw = function()
	{
		var num = parseInt(this.id.replace(/.*?(\d)$/, '$1')) + 1;
		self.fill(num);
	}
	
	this.fill = function(num)
	{
		var img = G('img', self.imgcontainer);
		for (var i=0; i<5; i++)
			img[i].src = i < num? self.activeImgSrc : self.inactiveImgSrc;
	}
	
	this.vote = function()
	{
		var mark = parseInt(this.id.replace(/.*?(\d)$/, '$1')) + 1;
		var req = reqobj();
		req.open('get', '/modules/voting.php?module='+self.module+'&id='+self.place+'&mark='+mark);
		req.onreadystatechange = function()
		{
			if (req.readyState != 4 || req.status != 200) return false;
			self.cfg = eval('(' + req.responseText + ')');
			var img = G('img', self.imgcontainer);
			for (var i=0; i<5; i++)
			{
				img[i].src = i < self.cfg.intBall ? self.activeImgSrc : self.inactiveImgSrc;
				
				img[i].onmouseover = null;
				img[i].onmouseout = null;
				img[i].onclick = null;
				img[i].style.cursor = 'auto';
			}
			var nums = G('span', self.container);
			nums[0].innerHTML = self.cfg.ball;
			nums[1].innerHTML = self.cfg.voters;
		};
		req.send(null);
	}
}
