function init()
{
	var tds = document.getElementsByTagName("td");

	process_copyright('copyright_link', {'href':'http://www.arealsoft.ru','title':'Разработка www.arealsoft.ru'});

	for(var i=0; i<tds.length; i++)
	{
		if (tds[i].className == "wh")
		{
			tds[i].onmouseover = function(){this.parentNode.className='active';}
			tds[i].onmouseout  = function(){this.parentNode.className='';}
		}	
	}
	showform();
	setMailBoxes();	
	if(document.getElementById('formfeedbacka'))
		window.location.hash='#form';
}

function showform(flg)
{
	var elem = document.getElementById('formfeedback');
	if(!elem) return;
	if(flg)
	{
		elem.style.display='';
		document.getElementById('formmessage').style.display='none';
	}
	else
		elem.innerHTML = form;
}


function setMailBoxes()
{
	var as=document.getElementsByTagName('a'), dmn, nm;
	for(var i=0;i<as.length;i++)
		if(as[i].className=='e-mail')
			{				
				dmn=as[i].href.substr(as[i].href.search('#')+1);
				nm=as[i].title;				
				as[i].href='mailto:'+nm+'@'+dmn;
				as[i].title='Написать письмо';
				if(!as[i].innerHTML) as[i].innerHTML=nm+'@'+dmn;
			}
}

function process_copyright (id, data)
{
	var elem = document.getElementById (id);
	if(!elem) return;
	data['rel'] = 'nofollow';
	data['target'] = data['target'] ? data['target'] : '_blank';
	
	var text = '<' + 'a';
	for (var i in data)
		text += ' ' + i + '="' + data[i] + '"';
	text += '>' + elem.innerHTML + '<' + '/' + 'a>';
	elem.innerHTML = text;
}

function showBigImage(href,obj,w,h,ih)
{
	var d = document.getElementById('bigImage');
	w = w?w:900;
	h = h?h:700;
	if(!d) 
	{
		d = document.body.appendChild(document.createElement('div'));
		d.id='bigImage';
		d.style.display='none';
	}	
	d.innerHTML = '<img src="'+href+'" '+(ih?'height="'+ih+'" ':'')+'/>';
	wins.open({fromdivid:'bigImage', width:w, height:h,		
		beforeclose:function(){ wins.clearContent();},
		title:obj.title});
	return false;
}

function initflash(id, url, w, h)
{
	var axo, a = document.getElementById(id);
	w = w?w:167;
	h = h?h:223;
	if(navigator.plugins && navigator.mimeTypes.length && navigator.plugins["Shockwave Flash"] && navigator.plugins["Shockwave Flash"].description)
		axo = true;
	else
		try{axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");} catch(e){}
	if(axo)
		a.innerHTML='<embed src="/images/mediaplayer.swf?file='+url+'&autostart=false" wmode="opaque" allowfullscreen="true" width="'+w+'" height="'+h+'" type="application/x-shockwave-flash"></embed>';
}

/* Калькулятор */
	function show_inputs()
	{      
		rooms_inp = document.getElementById("rooms");
		rooms_lbl = document.getElementById("rooms_label")                            
		count = Number(rooms_inp.value);  
		error_div = document.getElementById("calc_error"); 
		rooms_inp.disabled = false; 
		if(!count || count <= 0) 
		{
			error_div.innerHTML = '<p align="center" class="red" id="calc_error"><b>Введите число комнат корректно</b></p>';
			rooms_inp.className = "calc red";
			rooms_lbl.className = "label_rooms red";
			return;
		}
		else
		{
			error_div.innerHTML = '';  
			rooms_inp.className = "calc";
			rooms_lbl.className = "label_rooms";
		}
		if(count > rooms) add_rooms (count - rooms);
		if(count < rooms) del_rooms (rooms - count);
		rooms = count;
	}

	function add_rooms (n)
	{
		for(i = rooms+1; i <= rooms+n; i++)
		{         
			list = document.getElementById("calc_list");   
			inputs = document.getElementById("calc_input"); 
			newinput =  document.createElement('div');
			newinput.innerHTML = '<div class="room">Комната '+i+':</div>'+inputs.innerHTML.replace("[0]", "["+(i-1)+"]");
            newinput.innerHTML = newinput.innerHTML.replace("thermostat_0", "thermostat_"+(i-1)).replace("thermostat_0", "thermostat_"+(i-1));

			fields = newinput.getElementsByTagName("input");
			fields[0].value = '';
            fields[1].checked = false;
			list.appendChild(newinput);   
		}
	}

	function del_rooms (n)
	{
		list = document.getElementById("calc_list");  
		inputs = list.getElementsByTagName("input");

		for(i = inputs.length-1; i >= 0 && n>0; i -= 2)
		{
			list.removeChild(inputs[i].parentNode);
			n--;
		}
	}

	function check_key(event, type) 
	{    
		if(!event) event = window.event;
		if(event.keyCode)
			key = event.keyCode;
		else
        	key = event.charCode;
		if(key == 13)
		{	
			show_inputs();
			return;
		}
		if(type !=  "int" && key == 46)
			return;
		/*–Ёдал, del, tab*/	
		if (((key < 48) || (key > 57)) && key != 8 && key != 9)
  			return false;
	} 

	function reset_calc ()
	{
		list = document.getElementById("calc_list");  
		fields = list.getElementsByTagName("input");
		    
		for(i = 0; i < fields.length; i ++)
		{
			if(fields[i].type == "text")
				fields[i].value = '';
			if(fields[i].type == "checkbox")
            	fields[i].checked = false;
		}
		resalt = document.getElementById("resalt_calc");
		resalt.innerHTML = '';  
	}

	function hide (name)
	{
		obj = document.getElementById(name);
		obj.style.display = 'none';
	}
