﻿﻿function clone(obj){if(obj==null||typeof(obj)!='object')return obj;var temp=new obj.constructor();for(var key in obj)temp[key]=clone(obj[key]);return temp;}
function nolink(){return false;}
function OnSubmit(f,t){_f=new Frm(f,t);return _f.Validate();}
function Frm(f,t)
{this._frm=f;this._type=t;if(this._type==0){this._reEmail=/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;}
this.Validate=function()
{switch(this._type)
{case 0:result=$("#fio").val().length<3;$("#fio").attr('class',result?'error':'');if(result)return false;result=!$("#email").val().match(this._reEmail);$("#email").attr('class',result?'error':'');if(result)return false;result=$("#question").val().length<=0;$("#question").attr('class',result?'error':'');if(result)return false;break;case 1:result=$("#fio").val().length<3;$("#fio").attr('class',result?'error':'');if(result){$("#fio").focus();return false};result=$("#phone").val().length<7;$("#phone").attr('class',result?'error':'');if(result)return false;result=$("#address").val().length<3;$("#address").attr('class',result?'error':'');if(result)return false;break;}
return true;}}
function tel_clicked()
{if($("#tel1").attr('rel')==1)
{$("#tel2").animate({marginTop:"0px"},{duration:700,complete:function(){$("#tel1").attr('rel','0');}});$("#firsttip").animate({marginTop:"78px"},700);}
else
{$("#tel2").animate({marginTop:"40px"},{duration:700,complete:function(){$("#tel1").attr('rel','1');}});$("#firsttip").animate({marginTop:"121px"},700);}}
function Room(id)
{this._itemid=0;this.Items={Count:0};this.name="";this.id=id;this.Tout=0;this.Tin=0;this.h=0;this.s=0;this._recalcT=function(){this.Tin=0;for(i=0;i<this._itemid;i++){var o=this.Items['e_'+i];if(o!=null){var t=0;if(o.t70!=0)t=o.t70;else if(o.t60!=0)t=o.t60;else if(o.t50!=0)t=o.t50;this.Tin+=(o['sectionNumber']!=null)?parseFloat(t)*o['sectionNumber']:parseFloat(t);}}
var s=this.Tout>this.Tin?this.Tout+'Вт > '+this.Tin+' Вт':this.Tout+'Вт < '+this.Tin+' Вт';$('#T_'+this.id).html(s);}
this.DoCalc=function()
{i=this.id;this.name=$("#Name"+i).val();h=$("#Height"+i).val();s=$("#Square"+i).val();this.h=h.replace(this.rePoint,'.');this.s=s.replace(this.rePoint,'.');this.Tout=Math.round(h*s*40);$('#croom_'+i+'>h3').html('Комната «'+this.name+'»');this._recalcT();}
this._getItemDescr=function(e)
{var s=e.description;if(e['sectionNumber']!=null)s+='<br />Кол-во секций: '+e['sectionNumber']+' (<a class="inner-link" title="Увеличить количество секций" onclick="calc.ChangeSections(1,'+this.id+','+e['_id']+')">+увеличить</a>/<a class="inner-link" title="Уменьнить количество секций" onclick="calc.ChangeSections(-1,'+this.id+','+e['_id']+')">-уменьшить</a>) шт.';s+='   <a class="inner-link" title="Удалить теплоприбор" onclick="calc.RemoveEquipment('+this.id+','+e['_id']+')">удалить</a>';return s;}
this.AddItem=function(e){this.Items['e_'+this._itemid]=e;this.Items.Count++;e['_id']=this._itemid;var s=$('#croom_'+this.id+'>ol').html();s+='<li id="lie_'+this.id+'_'+this._itemid+'">'+this._getItemDescr(e)+'</li>';$('#croom_'+this.id+'>ol').html(s);this._itemid++;$('#croom_'+this.id+'>p.ui-state-error').remove();this._recalcT();};this.RemoveItem=function(rID,eID){this.Items['e_'+eID]=null;$('#lie_'+rID+'_'+eID).remove();this.Items.Count--;if(this.Items.Count<=0){$('#croom_'+this.id+'>ol').after('<p class="ui-state-error ui-corner-all">Не выбрано ни одного теплового прибора</p>');}
this._recalcT();};this.ChangeSections=function(i,rID,eID){var o=this.Items['e_'+eID];if((o!=null)&&(o['sectionNumber']!=null)){o['sectionNumber']=o['sectionNumber']+i;$('#lie_'+rID+'_'+eID).html(this._getItemDescr(o));this._recalcT();}}}
function Rooms()
{this.RoomsCount=1;this.RoomsID=0;this['room_'+this.RoomsID]=new Room(this.RoomsID);this.RoomsID++;this._html_tpl='<div class="room" id="room_{#num#}"><label>Название комнаты:</label><input name="Name{#num#}" id="Name{#num#}"/><span>(например: Кухня)</span>    <span><a class="inner-link" onclick="return calc.Rooms.DeleteRoom({#num#})" title="Удалить комнату из данного расчета">Удалить комнату</a></span><br /><label>Высота потолка (м):</label><input name="Height{#num#}" id="Height{#num#}" onchange="calc.ValidateField(0,this);" /><span>(например: 2.6)</span><br /><label>Площадь комнаты (м²):</label><input name="Square{#num#}" id="Square{#num#}" onchange="calc.ValidateField(0,this);"/><span>(например: 10)</span></div>';this._html_tpl2='<div class="room" id="croom_{#num#}"><h3></h3><p>Теплопотери: <label id="T_{#num#}">1531 Вт > 0 Вт</label> — общая мощность теплоприборов</p><ol></ol><p class="ui-state-error ui-corner-all">Не выбрано ни одного теплового прибора</p><p> <button class="ui-state-default ui-corner-all" onclick="calc.AddEquipment({#num#});" title="Выбор и добавление теплового прибора">+Добавить теплоприбор</button></p></div>';this.AddRoom=function(){re=/\{#num#\}/g;var str=this._html_tpl.replace(re,this.RoomsID);$("#AddRoomBtn").before(str);str=this._html_tpl2.replace(re,this.RoomsID);$("#ReturnBtn").before(str);this['room_'+this.RoomsID]=new Room(this.RoomsID);this.RoomsID++;this.RoomsCount++;}
this.DeleteRoom=function(id){$("#room_"+id).remove();$('#croom_'+id).remove();this['room_'+id]=null;this.RoomsCount--;}
this.DoCalc=function()
{var str="";var name="";var t=0;for(var i=0;i<this.RoomsID;i++)
{if(this['room_'+i]!=null)
{this['room_'+i].DoCalc();}}}}
function Calc()
{this.Rooms=new Rooms();this.reInt=/^[0-9]+$/;this.reDigit=/^[0-9]+[\,\.]*[0-9]*$/;this.rePoint=/\,/;this.Items=null;this.selectedIndex=-1;this.currentRoom=-1;this.ValidateField=function(type,o)
{var str=$(o).val();var valid=str.match(type==0?this.reDigit:this.reInt);if(valid==null)$(o).addClass("error");else $(o).removeClass("error");return valid;}
this.ValidateRoomForm=function()
{var str="";var result=true;for(i=0;i<this.Rooms.RoomsID;i++)
if(this.Rooms['room_'+i]!=null)
{str=$("#Height"+i).val();if(!str.match(this.reDigit)){$("#Height"+i).addClass("error");result=false;}else $("#Height"+i).removeClass("error");str=$("#Square"+i).val();if(!str.match(this.reDigit)){$("#Square"+i).addClass("error");result=false;}else $("#Square"+i).removeClass("error");}
return result;}
this.AddEquipment=function(roomID)
{this.currentRoom=roomID;$('#dlgSelectItem').dialog('open');return false;}
this.RemoveEquipment=function(roomID,eID)
{if(this.Rooms['room_'+roomID]!=null)this.Rooms['room_'+roomID].RemoveItem(roomID,eID);}
this.ChangeSections=function(i,roomID,eID)
{if(this.Rooms['room_'+roomID]!=null)this.Rooms['room_'+roomID].ChangeSections(i,roomID,eID);}
this.ResetEquipment=function(){this.Items=null;this.selectedIndex=-1;}
$("#dlgSelectItem").dialog({autoOpen:false,bgiframe:true,width:450,height:500,modal:true,buttons:{'Отмена':function(){$(this).dialog("close");},'Выбрать':function(){gr=$("#group").val();if(((1!=gr)||calc.ValidateField(1,document.getElementById('secNum')))&&(calc.Rooms['room_'+calc.currentRoom]!=null))
{var item=clone(calc.Items[calc.selectedIndex]);if(1==gr)item['sectionNumber']=parseInt($('#secNum').val());calc.Rooms['room_'+calc.currentRoom].AddItem(item);$(this).dialog("close");}}}});}
function on_grChanged()
{var gr=$("#group").val();$("#company").attr('disabled','disabled');$("#item").attr('disabled','disabled');$("#secNum").attr('disabled','disabled');if(gr!=0)$.ajax({type:"POST",url:"/calc/r/",dataType:"json",cache:true,data:{json:1,gid:gr},success:function(o,status){var str='<option value="0" selected="selected">---Выберите фабрику---</option>';for(i=0;i<o.length;i++){str+='<option value="'+o[i].id+'">'+o[i].name+'</option>';}
calc.items=o;$('#company').html(str);$('#company').removeAttr("disabled");}});}
function on_cmpChanged()
{var gr=$("#group").val();$("#item").attr('disabled','disabled');$("#secNum").attr('disabled','disabled');var cmp=$("#company").val();if(cmp!=0)$.ajax({type:"POST",url:"/calc/r/",dataType:"json",cache:true,data:{json:1,gid:gr,cid:cmp},success:function(o,status){var str='<option value="0" selected="selected">---Выберите товар---</option>';calc.Items=o;for(i=0;i<o.length;i++){str+='<option value="'+o[i].id+'">'+o[i].description+'</option>';}
$('#item').html(str);$('#item').removeAttr("disabled");}});}
function on_itemChanged(o)
{gr=$("#group").val();if(1==gr)$('#secNum').removeAttr('disabled');else $('#secNum').attr('disabled','disabled');var item=calc.Items[o.selectedIndex-1];calc.selectedIndex=o.selectedIndex-1;str='<h4>Параметры отопительного прибора</h4>';str+='<p>'+item.description+'</p>';str+='<ul>';str+='<li>Артикул: '+item.article+'</li>';if(item.t70!=0)str+='<li>Теплоотдача (Вт): '+item.t70+'</li>';else if(item.t60!=0)str+='<li>Теплоотдача (Вт): '+item.t60+'</li>';else if(item.t50!=0)str+='<li>Теплоотдача (Вт): '+item.t50+'</li>';if(item.height!=0)str+='<li>Высота (мм): '+item.height+'</li>';if(item.width!=0)str+='<li>Ширина (мм): '+item.width+'</li>';if(item.deep!=0)str+='<li>Глубина (мм): '+item.deep+'</li>';if(item.length!=0)str+='<li>Длина (мм): '+item.length+'</li>';if(item.intercenterlen!=0)str+='<li>Межцентровое расстояние (мм): '+item.intercenterlen+'</li>';if(item.P!=0)str+='<li>Эл. мощность (Вт): '+item.P+'</li>';if(item.I!=0)str+='<li>Ток (А): '+item.I+'</li>';str+='</ul>';$('#itemInfo').html(str);}