
var Base=function(){if(arguments.length){if(this==window){Base.prototype.extend.call(arguments[0],arguments.callee.prototype);}else{this.extend(arguments[0]);}}};Base.version="1.0.2";Base.prototype={extend:function(source,value){var extend=Base.prototype.extend;if(arguments.length==2){var ancestor=this[source];if((ancestor instanceof Function)&&(value instanceof Function)&&ancestor.valueOf()!=value.valueOf()&&/\bbase\b/.test(value)){var method=value;value=function(){var previous=this.base;this.base=ancestor;var returnValue=method.apply(this,arguments);this.base=previous;return returnValue;};value.valueOf=function(){return method;};value.toString=function(){return String(method);};}
return this[source]=value;}else if(source){var _prototype={toSource:null};var _protected=["toString","valueOf"];if(Base._prototyping)_protected[2]="constructor";for(var i=0;(name=_protected[i]);i++){if(source[name]!=_prototype[name]){extend.call(this,name,source[name]);}}
for(var name in source){if(!_prototype[name]){extend.call(this,name,source[name]);}}}
return this;},base:function(){}};Base.extend=function(_instance,_static){var extend=Base.prototype.extend;if(!_instance)_instance={};Base._prototyping=true;var _prototype=new this;extend.call(_prototype,_instance);var constructor=_prototype.constructor;_prototype.constructor=this;delete Base._prototyping;var klass=function(){if(!Base._prototyping)constructor.apply(this,arguments);this.constructor=klass;};klass.prototype=_prototype;klass.extend=this.extend;klass.implement=this.implement;klass.toString=function(){return String(constructor);};extend.call(klass,_static);var object=constructor?klass:_prototype;if(object.init instanceof Function)object.init();return object;};Base.implement=function(_interface){if(_interface instanceof Function)_interface=_interface.prototype;this.prototype.extend(_interface);};var WebAjax=Base.extend({constructor:null,rjsparams:{},next_rjsparam:1,to_list:function(object){var val=this.to_list_impl(object).value;if(!val||val=="")
return val;return val.substring(1,val.length-1);},get_jscript_base_url:function(jscript_tag){var col=document.getElementsByTagName("script");if(!col)
return"";for(var i=0;i<col.length;i++){if(col[i].src.match(jscript_tag))
return this.get_host(col[i].src);}
return"";},get_host:function(url){var spos=-1;if(url.slice(0,4)!="http")
url=window.location.href;for(var j=0;j<3;j++){spos=url.indexOf("/",spos+1);if(spos==-1)
return"";}
return url.slice(0,spos);},exec_service:function(url,parameters,callback,timeout){if(!timeout||null===timeout)
timeout=60000;if(parameters){if(typeof parameters!="string")
parameters=WebAjax.serialize_params(parameters);}else
parameters="";if(this.get_host(window.location.href)!=this.get_host(url)){var rjsparam=this.next_rjsparam++;var script;var ontimeout=function(){log_text("RJS timeout");if(typeof WebAjax.rjsparams[rjsparam]=="undefined")
return;if(WebAjax.rjsparams[rjsparam].ival){clearTimeout(WebAjax.rjsparams[rjsparam].ival);WebAjax.rjsparams[rjsparam].ival=null;}
if(!WebAjax.rjsparams[rjsparam].completed){WebAjax.rjsparams[rjsparam].completed=true;WebAjax.rjsparams[rjsparam].callback(false,"error");}
delete WebAjax.rjsparams[rjsparam];};var obj=new Object;obj.completed=false;obj.callback=callback;obj.ival=setTimeout(ontimeout,timeout);this.rjsparams[rjsparam]=obj;script=document.createElement("script");script.type="text/javascript";script.src=url+"?"+"rjsfunc=WebAjax.handle_remote_result&rjsparam="+rjsparam+(parameters!=""?("&"+parameters):"")+"&rjscookie="+Math.floor(Math.random()*100000);var head=document.getElementsByTagName("head")[0];var done=false;script.onload=script.onreadystatechange=function(){if(!done&&(!this.readyState||this.readyState=="loaded"||this.readyState=="complete")){done=true;try{head.removeChild(script);}catch(err){log_text(err);}
if(typeof WebAjax.rjsparams[rjsparam]!="undefined")
delete WebAjax.rjsparams[rjsparam];}};head.appendChild(script);return 1;}
var xml;var requestDone=false;try{xml=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("MSXML.XMLHTTP");xml.open("post",url,true);}catch(err){log_text(err);if(xml)
delete xml;return false;}
xml.setRequestHeader("Content-Type","application/x-www-form-urlencoded");xml.setRequestHeader("X-Requested-With","XMLHttpRequest");if(xml.overrideMimeType)
xml.setRequestHeader("Connection","close");var ival=null;var onreadystatechange=function(isTimeout){if(typeof WebAjax=="undefined")
return;if(xml&&(xml.readyState==4||isTimeout=="timeout")){requestDone=true;if(ival){clearTimeout(ival);ival=null;}
var status;try{status=WebAjax.http_success(xml)&&isTimeout!="timeout"?"success":"error";if(status!="error"){var params="";var result=xml.responseText||"";var reply=result.slice(0,2);if(reply=="OK")
WebAjax.callback(callback,true,result.slice(2));else{if(result.slice(0,5)=="ERROR")
WebAjax.callback(callback,false,result.slice(5));else
WebAjax.callback(callback,false,result);}}else
WebAjax.callback(callback,false,"error");}catch(e){if(typeof WebAjax=="undefined")
return;WebAjax.callback(callback,false,"error");if(typeof log_text!="undefined")
log_text(sprintf("ajax error: %s",e));}
xml=null;}};xml.onreadystatechange=onreadystatechange;ival=setTimeout(function(){if(xml){log_text("AJAX timeout");xml.abort();if(!requestDone)
onreadystatechange("timeout");xml=null;}},timeout);try{xml.send(parameters);}catch(e){log_text(sprintf("ajax send error: %s",e));onreadystatechange("timeout");}
return true;},callback:function(callback,result,text)
{setTimeout(function(){callback(result,text);},1);},http_success:function(r){if(!r)
return false;try{return!r.status||(r.status>=200&&r.status<300)||r.status==undefined;}catch(e){}
return false;},serialize_params:function(a){var s=[];for(var j in a)
s.push(encodeURIComponent(j)+"="+encodeURIComponent(a[j]));return s.join("&");},to_list_impl:function(object){var type=typeof object;switch(type){case'undefined':case'function':case'unknown':return;case'number':case'boolean':return{type:"",value:object.toString()};case'string':return{type:"",value:this.prop_to_list(object)};}
if(object===null)return{type:"obj_",value:"null"};var results=[];if(object.length!=null){var arr_type="arr_";for(var i=0;i<object.length;i++){var value=this.to_list_impl(object[i]);if(value!==undefined){if(value.type!="")
arr_type="objarr_";results.push(value.value);}}
return{type:arr_type,value:"{"+results.join(" ")+"}"};}
for(var property in object){var value=this.to_list_impl(object[property]);if(value!=undefined){results.push(value.type+property+" "+value.value);}}
return{type:"obj_",value:"{"+results.join(" ")+"}"};},prop_to_list:function(object){if(object==""||object.indexOf(" ")!=-1||object.indexOf("\t")!=-1||object.indexOf("\r")!=-1||object.indexOf("\n")!=-1||object.indexOf("{")!=-1||object.indexOf("}")!=-1)
return"{"+object+"}";return object;},handle_remote_result:function(rjsparam,result){rjsparam=parseInt(rjsparam);if(typeof WebAjax.rjsparams[rjsparam]=="undefined")
return;if(WebAjax.rjsparams[rjsparam].completed)
return;WebAjax.rjsparams[rjsparam].completed=true;if(WebAjax.rjsparams[rjsparam].ival){clearInterval(WebAjax.rjsparams[rjsparam].ival);WebAjax.rjsparams[rjsparam].ival=null;}
var params="";var reply=result.slice(0,2);if(reply=="OK")
WebAjax.rjsparams[rjsparam].callback(true,result.slice(2));else{if(result.slice(0,5)=="ERROR")
WebAjax.rjsparams[rjsparam].callback(false,result.slice(5));else
WebAjax.rjsparams[rjsparam].callback(false,result);}
delete WebAjax.rjsparams[rjsparam];}});function check_time(i){if(i<10)
i="0"+i;return i;}
function format_time(abs_val,allow_short){if(!abs_val)
return"";if(typeof allow_short=="undefined")
allow_short=true;var d=new Date(abs_val*1000);var cd=new Date();var res=check_time(d.getHours())+":"+check_time(d.getMinutes())+":"+check_time(d.getSeconds());if(allow_short&&cd.getDate()==d.getDate()&&cd.getMonth()==d.getMonth()&&cd.getYear()==d.getYear())
return res;return d.getFullYear()+"-"+check_time(d.getMonth()+1)+"-"+check_time(d.getDate())+" "+res;}
function format_date(abs_val){if(!abs_val)
return"";var d=new Date(abs_val*1000);return d.getFullYear()+"-"+check_time(d.getMonth()+1)+"-"+check_time(d.getDate());}
function get_abs_time(year,month,day,hour,minute,second){var d=new Date(year,month-1,day,hour,minute,second,0);return parseInt(d.getTime()/1000);}
function get_curr_abs_time(){var cd=new Date();return parseInt(cd.getTime()/1000);}
function get_local_timezone(){var rightNow=new Date();var jan1=new Date(rightNow.getFullYear(),0,1,0,0,0,0);var june1=new Date(rightNow.getFullYear(),6,1,0,0,0,0);var temp=jan1.toGMTString();var jan2=new Date(temp.substring(0,temp.lastIndexOf(" ")-1));temp=june1.toGMTString();var june2=new Date(temp.substring(0,temp.lastIndexOf(" ")-1));var std_time_offset=((jan1-jan2)/(1000*60*60));var daylight_time_offset=((june1-june2)/(1000*60*60));var dst;if(std_time_offset==daylight_time_offset){dst="0";}else{var hemisphere=std_time_offset-daylight_time_offset;if(hemisphere>=0)
std_time_offset=daylight_time_offset;dst="1";}
return parseInt(std_time_offset*3600);}
function parse_time(value)
{var time=0;if(value=="")
return-1;var pos=value.search(/:/);if(pos<1)
return-1;var hour=Number(value.substr(0,pos));if(isNaN(hour)||hour<0||hour>23)
return-1;time=hour*3600;value=value.substr(pos+1,value.length);pos=value.search(/:/);var minute=0;if(pos==-1)
minute=Number(value);else
minute=Number(value.substr(0,pos));if(isNaN(minute)||minute<0||minute>59)
return-1;time=time+minute*60;value=value.substr(pos+1,value.length);pos=value.search(/:/);if(pos>=1){var second=Number(value.substr(0,pos));if(isNaN(second)||second<0||second>59)
return-1;time=time+second;}
return time;}
function set_cookie(c_name,value,expiredays)
{var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);document.cookie=c_name+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString());}
function get_cookie(c_name)
{if(document.cookie.length>0){c_start=document.cookie.indexOf(c_name+"=");if(c_start!=-1){c_start=c_start+c_name.length+1;c_end=document.cookie.indexOf(";",c_start);if(c_end==-1)
c_end=document.cookie.length;return unescape(document.cookie.substring(c_start,c_end));}}
return""}
function is_valid_email(email)
{return(/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);}
function log_text(txt){if(jQuery.browser.mozilla&&typeof console=='object'&&typeof console.log!='undefined'){var dt=new Date();var abs_time=get_abs_time(dt.getFullYear(),dt.getMonth()+1,dt.getDate(),dt.getHours(),dt.getMinutes(),dt.getSeconds());console.log("%d:%d:%d.%d: %s",dt.getHours(),dt.getMinutes(),dt.getSeconds(),dt.getMilliseconds(),txt);}}
test=log_text;function atoi(s){var n=parseInt(s,10);if(isNaN(n)||Math.abs(n)>0x8FFFFFFF)
return 0;return n;}
function atoui(s){var n=atoi(s);if(n<0)
return-n;return n;}
function atof(s){var x=parseFloat(s);if(isNaN(x)||Math.abs(x)>0xFFFFFFFF)
return 0.0;return x;}
function atouf(s){var x=atof(s);if(x<0.0)
return-x;return x;}
function string_trim(s){return s.replace(/^\s+|\s+$/g,"");}
var sprintfWrapper={init:function(){if(typeof arguments=="undefined"){return null;}
if(arguments.length<1){return null;}
if(typeof arguments[0]!="string"){return null;}
if(typeof RegExp=="undefined"){return null;}
var string=arguments[0];var exp=new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);var matches=new Array();var strings=new Array();var convCount=0;var stringPosStart=0;var stringPosEnd=0;var matchPosEnd=0;var newString='';var match=null;while(match=exp.exec(string)){if(match[9]){convCount+=1;}
stringPosStart=matchPosEnd;stringPosEnd=exp.lastIndex-match[0].length;strings[strings.length]=string.substring(stringPosStart,stringPosEnd);matchPosEnd=exp.lastIndex;matches[matches.length]={match:match[0],left:match[3]?true:false,sign:match[4]||'',pad:match[5]||' ',min:match[6]||0,precision:match[8],code:match[9]||'%',negative:parseInt(arguments[convCount])<0?true:false,argument:String(arguments[convCount])};}
strings[strings.length]=string.substring(matchPosEnd);if(matches.length==0){return string;}
if((arguments.length-1)<convCount){return null;}
var code=null;var match=null;var i=null;for(i=0;i<matches.length;i++){if(matches[i].code=='%'){substitution='%'}
else if(matches[i].code=='b'){matches[i].argument=String(Math.abs(parseInt(matches[i].argument)).toString(2));substitution=sprintfWrapper.convert(matches[i],true);}
else if(matches[i].code=='c'){matches[i].argument=String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));substitution=sprintfWrapper.convert(matches[i],true);}
else if(matches[i].code=='d'){matches[i].argument=String(Math.abs(parseInt(matches[i].argument)));substitution=sprintfWrapper.convert(matches[i]);}
else if(matches[i].code=='f'){matches[i].argument=String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision?matches[i].precision:6));substitution=sprintfWrapper.convert(matches[i]);}
else if(matches[i].code=='o'){matches[i].argument=String(Math.abs(parseInt(matches[i].argument)).toString(8));substitution=sprintfWrapper.convert(matches[i]);}
else if(matches[i].code=='s'){matches[i].argument=matches[i].argument.substring(0,matches[i].precision?matches[i].precision:matches[i].argument.length)
substitution=sprintfWrapper.convert(matches[i],true);}
else if(matches[i].code=='x'){matches[i].argument=String(Math.abs(parseInt(matches[i].argument)).toString(16));substitution=sprintfWrapper.convert(matches[i]);}
else if(matches[i].code=='X'){matches[i].argument=String(Math.abs(parseInt(matches[i].argument)).toString(16));substitution=sprintfWrapper.convert(matches[i]).toUpperCase();}
else{substitution=matches[i].match;}
newString+=strings[i];newString+=substitution;}
newString+=strings[i];return newString;},convert:function(match,nosign){if(nosign){match.sign='';}else{match.sign=match.negative?'-':match.sign;}
var l=match.min-match.argument.length+1-match.sign.length;var pad=new Array(l<0?0:l).join(match.pad);if(!match.left){if(match.pad=="0"||nosign){return match.sign+pad+match.argument;}else{return pad+match.sign+match.argument;}}else{if(match.pad=="0"||nosign){return match.sign+match.argument+pad.replace(/0/g,' ');}else{return match.sign+match.argument+pad;}}}}
sprintf=sprintfWrapper.init;var Wialon=Base.extend({constructor:null,items:{},item_guids:{},item_types:{},classes:{},user_id:0,curr_user:null,events_poll_interval:0,events_cookie:0,event_handlers:[],server_time:0,item_constructors:{},msg_constructors:{},params_constructors:{},item_plugins:{},get_current_user:function(){if(this.curr_user)
return this.curr_user;return this.get_item(this.user_id);},get_server_time:function()
{return this.server_time;},can_create_item:function(){var user=this.get_current_user();if(!user)
return false;return user.can_create_item();},login:function(user,password,store_cookie,callback,cparam){if(this.user_id!=0)
return false;return this.exec_service({svc:"login",user:user,password:password,store_cookie:store_cookie?1:0},function(result,reply){if(!result)
callback(false,reply,cparam);else{Wialon.init_state(reply);callback(true,"",cparam);}});},logout:function(callback,cparam){if(this.user_id==0)
return false;this.exec_service({svc:"logout"},function(result,text){callback(result,text,cparam);});this.reset();return true;},init_state:function(state_json){if(state_json=="")
return false;this.reset();var state;eval("state = "+state_json);var res=Wialon.init_state_impl(state);delete state;return res;},init_state_remote:function(poll_interval,callback){return Wialon.exec_service({svc:"init_state",poll_interval:poll_interval},function(result,text){if(!result||text==""){callback(false);return;}
var data;eval("data = "+text);Wialon.init_state_impl(data);delete data;callback(true);});},init_state_impl:function(state){if(typeof state!="object")
return false;this.reset();this.user_id=state.uid;this.events_poll_interval=state.pi;this.events_cookie=state.eid;this.server_time=state.tm;for(cls in state.classes)
this.classes[state.classes[cls]]=cls;for(var i=0;i<state.items.length;i++){var item=this.construct_item(state.items[i]);if(item){this.items[item.get_id()]=item;this.item_guids[item.get_guid()]=item;var icol=this.item_types[item.get_type()];if(typeof icol=="undefined"){this.item_types[item.get_type()]=new Array;icol=this.item_types[item.get_type()];}
icol.push(item);}}
this.curr_user=this.get_item(this.user_id);if(this.events_poll_interval&&this.events_cookie)
window.self.setTimeout("Wialon.check_events();",10000);Wialon.fire_event("wialon_ready",0,null);return true;},fool_callback:function(result,text){},bind_event_handler:function(event_name,event_id,callback,cparam){if(event_name==null||event_id==null||callback==null)
return false;var col1=this.event_handlers[event_name];if(typeof col1=='undefined'){col1=new Object;this.event_handlers[event_name]=col1;}
var col2=col1[event_id];if(typeof col2=='undefined'){col2=new Array;col1[event_id]=col2;}
var handler=new Object;handler.callback=callback;if(typeof cparam=="undefined")
cparam="";handler.cparam=cparam;col2.push(handler);return true;},unbind_event_handler:function(event_name,event_id,callback,cparam){if(event_name==null||callback==null)
return false;if(typeof cparam=="undefined")
cparam="";var col1=this.event_handlers[event_name];if(typeof col1=='undefined')
return false;var col2=col1[event_id];if(typeof col2=='undefined')
return false;for(var i=0;i<col2.length;i++){var handler=col2[i];if(handler.callback==callback&&handler.cparam==cparam){col2.splice(i,1);return true;}}
return false;},access_level:function(name){if(name=="none")
return 0;else if(name=="view"||name=="any")
return 0x1;else if(name=="exec_cmd")
return 0x7;else if(name=="edit")
return 0x000FFFFF;else if(name=="manage")
return 0x7FFFFFFF;return 0;},access_name:function(level){switch(parseInt(level)){case 0:return"none";case 0x1:return"view";case 0x7:return"exec_cmd";case 0x000FFFFF:return"edit";case 0x7FFFFFFF:return"manage";}
return"";},delete_items:function(col,callback,cparam){var dcol=new Array;var idcol=new Array;for(var i=0;i<col.length;i++){if(col[i].get_access_level()<Wialon.access_level("manage"))
continue;dcol.push(col[i]);idcol.push(col[i].get_id());}
if(!dcol.length)
return false;return Wialon.exec_service({svc:"delete_items",items:WebAjax.to_list(idcol)},function(result,text){if(!result||text==""){var col=new Array;col.push(text);callback(false,col,cparam);return;}
var data;eval("data = "+text);var arr=data.result;var col=new Array;for(var i=0;i<arr.length;i++){if(arr[i]!="")
col.push(arr[i]);else
Wialon.on_delete_item(idcol[i]);}
delete data;callback(true,col,cparam);});},serialize_items:function(col,callback,cparam){var icol=new Array;for(var i=0;i<col.length;i++){if(col[i].get_access_level()<Wialon.access_level("edit")||col[i].modifications.length==0)
continue;var item=new Object;item.id=col[i].get_id();item.mods=col[i].modifications;col[i].modifications=new Array;icol.push(item);}
if(!icol.length)
return false;return Wialon.exec_service({svc:"serialize_items",items:WebAjax.to_list(icol)},function(result,text){if(!result||text==""){var col=new Array;for(var i=0;i<icol.length;i++){for(var j=0;j<icol[i].mods.length;j++)
icol[i].mods[j].revert();var res=new Object;res.id=icol[i].id;res.result=new Array;res.result.push(text);col.push(res);}
callback(false,col,cparam);return;}
var data;eval("data = "+text);var arr=data.result;var col=new Array;for(var i=0;i<arr.length;i++){var mods=icol[i].mods;var res=new Object;res.item=Wialon.get_item(icol[i].id);if(!res.item)
continue;var rcol=new Array;if(typeof arr[i].error!="undefined"){for(var j=0;j<mods.length;j++)
mods[j].revert();rcol.push(arr[i].error);}else{for(var j=0;j<arr[i].result.length;j++){if(arr[i].result[j]!=""){rcol.push(arr[i].result[j]);mods[j].revert();}}}
res.result=rcol;col.push(res);}
delete data;callback(true,col,cparam);});},get_item:function(id){id=parseInt(id);var item=this.items[id];if(typeof item!='undefined')
return item;return null;},get_item_by_guid:function(guid){var item=this.item_guids[guid];if(typeof item!='undefined')
return item;return null;},reset:function(){this.items=new Object;this.item_guids=new Object;this.user_id=0;this.events_poll_interval=0;this.events_cookie=0;this.server_time=0;},exec_service:function(parameters,callback,timeout){var url=WebAjax.get_jscript_base_url("wialon_sdk=1")+"/webavl_sdk/wialon_service.html";if(parameters&&typeof parameters.svc!="undefined"&&url.split("?").length==1)
url+="?svc="+parameters.svc;return WebAjax.exec_service(url,parameters,callback,timeout);},check_events:function(){if(!this.events_poll_interval||!this.events_cookie||!this.user_id)
return true;var url=WebAjax.get_jscript_base_url("wialon_sdk=1")+"/avl_events";return WebAjax.exec_service(url,{sid:this.events_cookie},Wialon.events_callback);},events_callback:function(result,text){if(result&&text!=""&&Wialon.user_id){try{var event_json;eval("event_json = "+text);var events=event_json.events;Wialon.server_time=event_json.tm;delete event_json;for(var i=0;i<events.length;i++){var evt_data=events[i];var evt={};if(evt_data.t=="c")
evt.type="create";else if(evt_data.t=="d")
evt.type="delete";else if(evt_data.t=="u")
evt.type="update";else if(evt_data.t=="m")
evt.type="msg_update";if(typeof evt_data.d!="undefined")
evt.data=evt_data.d;var item=Wialon.get_item(evt_data.i);if(item!=null){item.handle_event(evt);continue;}else if(evt.type=="create"){item=Wialon.construct_item(evt.data);if(item&&item.get_guid()!=""&&typeof Wialon.items[item.get_id()]=="undefined")
Wialon.on_item_created(item);}
continue;}}catch(err){log_text(sprintf("wialon events poll error: %s. %s",err,text));}
Wialon.fire_event("wialon_events_poll",0,null);}else{if(text!=""){if(text!="error"){Wialon.events_poll_interval=0;}}}
if(!Wialon.events_poll_interval||!Wialon.user_id)
return;window.self.setTimeout("Wialon.check_events();",Wialon.events_poll_interval*1000);},fire_event:function(event_name,event_id,data){try{var col1=this.event_handlers[event_name];if(typeof col1!='undefined'){var col2=col1[event_id];if(typeof col2!='undefined'){for(var i=0;i<col2.length;i++){var handler=col2[i];handler.callback(event_name,event_id,data,handler.cparam);}}
col2=col1[0];if(typeof col2!='undefined'&&event_id!=0){for(var i=0;i<col2.length;i++){var handler=col2[i];handler.callback(event_name,event_id,data,handler.cparam);}}}
var col1=this.event_handlers[""];if(typeof col1!='undefined'){var col2=col1[event_id];if(typeof col2!='undefined'){for(var i=0;i<col2.length;i++){var handler=col2[i];handler.callback(event_name,event_id,data,handler.cparam);}}
col2=col1[0];if(typeof col2!='undefined'&&event_id!=0){for(var i=0;i<col2.length;i++){var handler=col2[i];handler.callback(event_name,event_id,data,handler.cparam);}}}}catch(err){log_text(sprintf("wialon event[%s,%s] error: %s",event_name,event_id,err));}},get_items:function(item_type){if(typeof item_type=="undefined"||item_type==""){var col=new Array;for(var id in this.items)
col.push(this.items[id]);return col;}else{var icol=this.item_types[item_type];if(typeof icol!="undefined")
return icol;}
return new Array;},filter_own_items:function(col){var ret_col=new Array;var user=this.get_current_user();for(var i=0;i<col.length;i++){var item=col[i];if(item.creator==user.id)
ret_col.push(item);}
return ret_col;},filter_items:function(col,acces_name){var ret_col=new Array;var user=this.get_current_user();var lvl=this.access_level(acces_name);for(var i=0;i<col.length;i++){var item=col[i];if(item.get_access_level()>=lvl)
ret_col.push(item);}
return ret_col;},sort_items:function(col){if(!col)
return null;return col.sort(Wialon.sort_items_func);},sort_items_func:function(a,b){if(a.name==b.name)
return 0;return a.name.toLowerCase()>b.name.toLowerCase()?1:-1;},get_items_from_id_col:function(idcol){var col=new Array;for(var i=0;i<idcol.length;i++){var item=this.get_item(idcol[i]);if(item)
col.push(item);}
return col;},get_items_from_guid_col:function(idcol){var col=new Array;for(var i=0;i<idcol.length;i++){var item=this.get_item_by_guid(idcol[i]);if(item)
col.push(item);}
return col;},on_delete_item:function(item_id){if(!item_id)
return;var item=this.items[item_id];if(typeof item!='undefined'){if(typeof this.item_guids[item.get_guid()]!='undefined')
delete this.item_guids[item.get_guid()];delete this.items[item_id];var icol=this.item_types[item.get_type()];if(typeof icol!="undefined"){for(var i=0;i<icol.length;i++){if(icol[i].id==item_id){icol.splice(i,1);break;}}}
Wialon.fire_event("item_deleted",item_id,item);}
return;},construct_item:function(state){if(typeof state!="object"||typeof state.cls=="undefined"||typeof state.id=='undefined')
return null;var cls=this.classes[state.cls];if(cls=="undefined")
return null;state.cls=cls;var func=this.item_constructors[state.cls];if(typeof func=='undefined')
return null;var pcol=new Object;var pls=this.item_plugins[state.cls];if(typeof pls!='undefined'){for(var tp in pls){var plugin=new pls[tp];if(plugin!=null)
pcol[tp]=plugin;}}
return new func(state,pcol);},construct_msg:function(json){if(typeof json!="object"||typeof json.tp!="string")
return null;var func=this.msg_constructors[json.tp];if(typeof func!='undefined')
return new func(json);return null;},construct_param_object:function(name,val){if(typeof val!="object"||typeof name!="string")
return val;var func=this.params_constructors[name];if(typeof func!='undefined')
return func(val);return val;},add_item_constructor:function(cls,func){this.item_constructors[cls]=func;},add_item_plugin:function(cls,plugin_type,func){var pls=this.item_plugins[cls];if(typeof pls=='undefined'){pls=new Object;this.item_plugins[cls]=pls;}
var chk=pls[plugin_type];if(typeof chk!='undefined')
return;pls[plugin_type]=func;},add_msg_constructor:function(tp,func){this.msg_constructors[tp]=func;},add_params_constructor:function(nm,func){this.params_constructors[nm]=func;},on_item_created:function(item){if(!item)
return;var pitem=this.get_item(item.get_id());if(pitem)
return;this.items[item.get_id()]=item;this.item_guids[item.get_guid()]=item;var icol=this.item_types[item.get_type()];if(typeof icol=="undefined"){this.item_types[item.get_type()]=new Array;icol=this.item_types[item.get_type()];}
icol.push(item);this.fire_event("item_created",item.get_id(),item);},set_user_access_for_item:function(item,user,access_level){if(!user||!item)
return;user.set_id_access_level(item.get_id(),access_level);Wialon.set_user_access_for_item(item,user.get_user_creator(),access_level);}});var WialonMsg=Base.extend({constructor:function(state){this.init_state(state);},time:0,params:{},type:"",flags:0,get_type:function(){return this.type;},get_time:function(){return this.time;},get_flags:function(){return this.flags;},get_params:function(){return this.params;},get_param:function(name,def_val){if(null===this.params)
return def_val;var val=this.params[name];if(typeof val!='undefined')
return val;if(typeof def_val=='undefined')
def_val="";return def_val;},init_state:function(state){this.time=state.t;this.params=state.p;this.type=state.tp;this.flags=state.f;return true;}});var WialonUnitMsg=WialonMsg.extend({constructor:function(state){this.init_state(state);},inputs:0,outputs:0,driver:"",get_inputs:function(){return this.inputs;},get_outputs:function(){return this.outputs;},get_driver_code:function(){return this.driver;},is_position:function(){return false;},init_state:function(state){this.base(state);if(typeof state.i!="undefined")
this.inputs=state.i;if(typeof state.o!="undefined")
this.outputs=state.o;if(typeof state.dr!="undefined")
this.driver=state.dr;return true;}});var WialonUnitPosMsg=WialonUnitMsg.extend({constructor:function(state){this.init_state(state);},location:"",lat:0.0,lon:0.0,road_lat:0.0,road_lon:0.0,alt:0,course:0,speed:0,sat_count:0,get_location:function(){return this.location;},get_lat:function(){return this.lat;},get_lon:function(){return this.lon;},get_road_lat:function(){return this.road_lat?this.road_lat:this.lat;},get_road_lon:function(){return this.road_lon?this.road_lon:this.lon;},get_alt:function(){return this.alt;},get_speed:function(){return this.speed;},get_course:function(){return this.course;},get_sat_count:function(){return this.sat_count;},is_position:function(){return true;},init_state:function(state){this.base(state);this.lat=state.y;this.lon=state.x;this.road_lat=state.y2;this.road_lon=state.x2;this.alt=state.z;this.speed=state.s;this.course=state.c;this.location=state.l;this.sat_count=state.sc;return true;}});var WialonUnitCmdResMsg=WialonMsg.extend({constructor:function(state){this.init_state(state);},cmd_name:"",cmd_param:"",user_guid:"",link_name:"",link_type:"",exec_time:0,get_cmd_name:function(){return this.cmd_name;},get_cmd_param:function(){return this.cmd_param;},get_user:function(){return Wialon.get_item(this.user_guid);},get_link_name:function(){return this.link_name;},get_link_type:function(){return this.link_type;},get_exec_time:function(){return this.exec_time;},init_state:function(state){this.base(state);this.cmd_name=state.cn;this.cmd_param=state.cp;this.user_guid=state.ug;this.link_name=state.ln;this.link_type=state.lt;this.exec_time=state.et;return true;}});var WialonUnitSMSMsg=WialonMsg.extend({constructor:function(state){this.init_state(state);},sms_text:"",modem_phone:"",user_guid:"",link_name:"",link_type:"",exec_time:0,get_sms_text:function(){return this.sms_text;},get_modem_phone:function(){return this.modem_phone;},init_state:function(state){this.base(state);this.sms_text=state.st;this.modem_phone=state.mp;return true;}});var WialonUnitEventMsg=WialonMsg.extend({constructor:function(state){this.init_state(state);},text:"",lat:0.0,lon:0.0,get_event_text:function(){return this.sms_text;},get_lat:function(){return this.lat;},get_lon:function(){return this.lon;},init_state:function(state){this.base(state);this.text=state.et;this.lon=state.x;this.lat=state.y;return true;}});Wialon.add_msg_constructor("udp",WialonUnitPosMsg);Wialon.add_msg_constructor("ud",WialonUnitMsg);Wialon.add_msg_constructor("ucr",WialonUnitCmdResMsg);Wialon.add_msg_constructor("us",WialonUnitSMSMsg);Wialon.add_msg_constructor("evt",WialonUnitEventMsg);Wialon.add_msg_constructor("xx",WialonMsg);Wialon.add_params_constructor("msg",function(state){return new Wialon.construct_msg(state);});var WialonItemPlugin=Base.extend({id:0,item:null,get_type:function(){return"unknown";},init_state:function(state,item){if(!state)
return;if(typeof state.id!="undefined")
this.id=state.id;if(item)
this.item=item;},populate_access_propogation_items:function(cset){},handle_event:function(evt){}});var WialonItem=Base.extend({constructor:function(state,plugins){this.plugins=plugins;this.init_state(state);},id:0,guid:"",name:"",public_props:{},private_props:{},superclass:"",plugins:[],creator:0,account:0,get_type:function(){return this.superclass;},get_plugin:function(plugin_type){var pl=this.plugins[plugin_type];if(typeof pl!='undefined'&&pl.get_type()==plugin_type)
return pl;return null;},get_name:function(){return this.name;},get_id:function(){return this.id;},get_guid:function(){return this.guid;},get_user_creator:function(){return Wialon.get_item(this.creator);},get_account:function(){return Wialon.get_item(this.account);},get_access_level:function(){return this.get_user_access(Wialon.get_current_user(),true);},get_public_property:function(prop_name,default_value){return this.get_prop_by_name(this.public_props,prop_name,default_value);},get_private_property:function(prop_name,default_value){return this.get_prop_by_name(this.private_props,prop_name,default_value);},get_user_access:function(user,check_groups){if(user==null)
return 0;var acc_col=new Array;var group_col=user.get_user_groups();acc_col.push(user);for(var i=0;i<group_col.length;i++)
acc_col.push(group_col[i]);var ids=new Object;this.populate_access_propogation_items(ids);var lvl=0;for(var i=0;i<acc_col.length;i++){var acc=acc_col[i];for(var id in ids){var id_lvl=acc.get_id_access_level(id);if(id_lvl>lvl)
lvl=id_lvl;}}
return lvl;},set_public_property:function(prop_name,prop_value){return this.set_property_impl(prop_name,prop_value,this.public_props,"public");},set_private_property:function(prop_name,prop_value){return this.set_property_impl(prop_name,prop_value,this.private_props,"private");},set_name:function(new_name){if(typeof new_name!="string"||this.get_access_level()<Wialon.access_level("edit"))
return false;if(this.get_name()==new_name)
return true;this.remove_modification("item_set_name",null);var item=this;var old_value=this.name;this.add_modification("item_set_name",new_name,function(){item.name=old_value;});this.name=new_name;return true;},serialize:function(callback,cparam){var mods=this.modifications;this.modifications=new Array();var item=this;if(mods.length==0){callback(true,new Array,cparam);return true;}
return Wialon.exec_service({svc:"serialize_item",item_id:this.id,mods:WebAjax.to_list(mods)},function(result,text){if(!result||text==""){for(var i=0;i<mods.length;i++)
mods[i].revert();var col=new Array;col.push(text);callback(false,col,cparam);return;}
var data;eval("data = "+text);var arr=data.result;var col=new Array;for(var i=0;i<arr.length;i++){if(arr[i]!=""){col.push(arr[i]);mods[i].revert();}}
delete data;callback(true,col,cparam);});},delete_item:function(callback,cparam){if(this.get_access_level()<Wialon.access_level("manage"))
return false;var item_id=this.get_id();return Wialon.exec_service({svc:"delete_"+this.get_type(),item_id:this.id},function(result,text){if(!result){callback(false,text,cparam);return;}
Wialon.on_delete_item(item_id);callback(true,"",cparam);});},revert:function(){for(var i=0;i<this.modifications.length;i++)
this.modifications[i].revert();this.modifications=new Array();return true;},get_prop_by_name:function(col,prop_name,default_value){if(typeof default_value=="undefined")
default_value="";var pval=col[prop_name];if(typeof pval=="undefined")
return default_value;return pval;},init_state:function(state){if(typeof this.modifications=="undefined")
this.modifications=new Array;if(typeof state.id!="undefined")
this.id=state.id;if(typeof state.gd!="undefined")
this.guid=state.gd;if(typeof state.nm!="undefined")
this.name=state.nm;if(typeof state.pup!="undefined")
this.public_props=state.pup;if(typeof state.prp!="undefined")
this.private_props=state.prp;if(typeof state.cls!="undefined")
this.superclass=state.cls;if(typeof state.crt!="undefined")
this.creator=state.crt;if(typeof state.bact!="undefined")
this.account=state.bact;for(var i in this.plugins)
this.plugins[i].init_state(state,this);return true;},populate_access_propogation_items:function(set){set[this.get_id()]=1;for(var i in this.plugins)
this.plugins[i].populate_access_propogation_items(set);},handle_event:function(evt){if(evt.type=="update"){this.init_state(evt.data);Wialon.fire_event("item_updated",this.get_id(),evt.data);}else if(evt.type=="delete"){Wialon.on_delete_item(this.get_id());}else{for(var i in this.plugins)
this.plugins[i].handle_event(evt);}},add_modification:function(mod_type,data,revert_func){this.modifications.push({type:mod_type,data:data,revert:revert_func});},remove_modification:function(type,func){for(var i=0;i<this.modifications.length;i++){var mod=this.modifications[i];if(mod.type==type&&(func==null||func(mod.data))){mod.revert();this.modifications.splice(i,1);break;}}},set_property_impl:function(prop_name,prop_value,col,prop_type){prop_value=prop_value.toString();if(typeof prop_name!="string"||this.get_access_level()<Wialon.access_level("edit"))
return false;if(this.get_prop_by_name(col,prop_name)==prop_value)
return true;this.remove_modification("item_set_property",function(data){if(data.type==prop_type&&data.name==prop_name)
return true;return false;});var old_value=this.get_prop_by_name(col,prop_name);this.add_modification("item_set_property",{name:prop_name,value:prop_value,type:prop_type},function(){col[prop_name]=old_value;});col[prop_name]=prop_value;return true;},check_item_in_id_col:function(col,item_id){for(var i=0;i<col.length;i++){if(col[i]==item_id)
return true;}
return false;},add_item_to_id_col:function(mod_type,col,id){if(this.get_access_level()<Wialon.access_level("edit"))
return false;if(this.check_item_in_id_col(col,id))
return true;this.remove_modification(mod_type,function(data){if(data==id)
return true;return false;});col.push(id);this.add_modification(mod_type,id,function(){for(var i=0;i<col.length;i++){if(col[i]==id){col.splice(i,1);return true;}}});return true;},remove_item_from_id_col:function(mod_type,col,id){if(this.get_access_level()<Wialon.access_level("edit"))
return false;if(!this.check_item_in_id_col(col,id))
return true;this.remove_modification(mod_type,function(data){if(data==id)
return true;return false;});for(var i=0;i<col.length;i++){if(col[i]==id){col.splice(i,1);break;}}
this.add_modification(mod_type,id,function(){col.push(id);});return true;}});Wialon.extend({get_avl_server:function(){var col=this.get_items("avl_server");if(!col||col.length!=1)
return null;return col[0];}});Wialon.add_item_constructor("avl_server",WialonItem);var WialonParamsItem=Base.extend({constructor:function(params){if(typeof params=='object')
this.params=param;else
this.params={};},params:{},get_params:function(){return this.params;},get_param:function(name,def_val){var val=this.params[name];if(typeof val!='undefined'){if(typeof val!='object')
return val;return Wialon.construct_param_object(name,val);}
if(typeof def_val=='undefined')
def_val="";return def_val;}});var WialonGeozonePoint=Base.extend({constructor:function(state){this.lat=state.y;this.lon=state.x;this.radius=state.r;},lat:0.0,lon:0.0,radius:0,get_lat:function(){return this.lat;},get_lon:function(){return this.lon;},get_radius:function(){return this.radius;}});var WialonControlPoint=Base.extend({constructor:function(state){if(state){this.arrive=state.at;this.arrive_offset=state.atd;this.departure=state.dt;this.departure_offset=state.dtd;this.name=state.n;}},arrive:0,arrive_offset:0,departure:0,departure_offset:0,name:"",get_name:function(){return this.name;},get_arrive_time:function(){return this.arrive;},get_arrive_time_deviation:function(){return this.arrive_offset;},get_departure_time:function(){return this.departure;},get_departure_time_deviation:function(){return this.departure_offset;}});var WialonGeozone=Base.extend({constructor:function(state){this.id=state.i;this.type=state.t;this.deviation=state.d;this.description=state.dr;this.name=state.n;this.points=new Array;for(var i=0;i<state.p.length;i++)
this.points.push(new WialonGeozonePoint(state.p[i]));if(state.cp instanceof Array&&state.cp.length){this.control_points=new Array;for(var i=0;i<state.cp.length;i++)
this.control_points.push(new WialonControlPoint(state.cp[i]));}},type:2,deviation:100,name:"",description:"",points:[],id:0,get_type:function(){return this.type;},get_id:function(){return this.id;},get_deviation:function(){return this.deviation;},get_description:function(){return this.description;},get_name:function(){return this.name;},get_points:function(){return this.points;},get_control_points:function(){if(this.control_points instanceof Array)
return this.control_points;return null;},copy:function(){var zone=new WialonGeozoneConstructor();zone.set_type(this.get_type());zone.set_deviation(this.get_deviation());zone.set_name(this.get_name());zone.set_description(this.get_description());zone.set_points(this.get_points());var cp=this.get_control_points();for(var i=0;cp&&i<cp.length;i++)
zone.set_control_point(i,cp[i]);return zone;},point_in_zone:function(lon,lat){function distance_to_line(pnt1_lat,pnt1_lon,pnt2_lat,pnt2_lon,pnt_from_lat,pnt_from_lon,check_intersect){var res={};if(pnt1_lat==pnt2_lat&&pnt1_lon==pnt2_lon)
return calc_distance(pnt1_lat,pnt1_lon,pnt_from_lat,pnt_from_lon);var lon_int=0;var lat_int=0;if(pnt1_lon!=pnt2_lon){var a=(pnt1_lat-pnt2_lat)/(pnt1_lon-pnt2_lon);var b=pnt1_lat-pnt1_lon*a;lon_int=(pnt_from_lon+a*pnt_from_lat-a*b)/(a*a+1.0);lat_int=lon_int*a+b;}else{var a=(pnt1_lon-pnt2_lon)/(pnt1_lat-pnt2_lat);var b=pnt1_lon-pnt1_lat*a;lat_int=(pnt_from_lat+a*pnt_from_lon-a*b)/(a*a+1.0);lon_int=lat_int*a+b;}
if(!check_intersect)
return calc_distance(lat_int,lon_int,pnt_from_lat,pnt_from_lon);if(lon_int<pnt1_lon&&lon_int<pnt2_lon||lon_int>pnt1_lon&&lon_int>pnt2_lon||lat_int<pnt1_lat&&lat_int<pnt2_lat||lat_int>pnt1_lat&&lat_int>pnt2_lat)
return-1;else
return calc_distance(lat_int,lon_int,pnt_from_lat,pnt_from_lon);}
if(!this.points.length)
return;var points=this.points;var pt_count=points.length;if(this.type==2){var xnew=0;var ynew=0;var xold=0;var yold=0;var x1=0;var y1=0;var x2=0;var y2=0;var inside=false;if(pt_count<3)
return false;xold=points[pt_count-1].lon;yold=points[pt_count-1].lat;for(var i=0;i<pt_count;i++){xnew=points[i].lon;ynew=points[i].lat;if(xnew>xold){x1=xold;x2=xnew;y1=yold;y2=ynew;}else{x1=xnew;x2=xold;y1=ynew;y2=yold;}
if((xnew<lon)==(lon<=xold)&&(lat-y1)*(x2-x1)<(y2-y1)*(lon-x1)){inside=!inside;}
xold=xnew;yold=ynew;}
return inside;}else if(this.type==1){var prev_lat=0;var prev_lon=0;for(var i=0;i<pt_count;i++){var range=calc_distance(points[i].lat,points[i].lon,lat,lon);if(points[i].radius&&range!=-1&&range<=points[i].radius)
return true;if(this.deviation){if(range!=-1&&range<=this.deviation/2)
return true;if(i>0){var range=distance_to_line(points[i].lat,points[i].lon,prev_lat,prev_lon,lat,lon,true);if(range!=-1&&range<=this.deviation/2)
return true;}}
prev_lat=points[i].lat;prev_lon=points[i].lon;}}else if(this.type==3){var p=points[0];range=calc_distance(p.lat,p.lon,lat,lon);if(range!=-1&&range<=p.radius)
return true;}
return false;}});var WialonGeozoneConstructor=WialonGeozone.extend({constructor:function(base){this.points=new Array;if(typeof base=="object"&&base!=null){for(var i=0;i<base.points.length;i++)
this.points.push(base.points[i]);this.id=base.id;this.name=base.name;this.type=base.type;this.deviation=base.deviation;this.description=base.description;if(base.control_points instanceof Array&&base.control_points.length){this.control_points=new Array;for(var i=0;i<base.control_points.length;i++)
this.control_points.push(base.control_points[i]);}}else{this.id=0;this.name="";this.type=1;this.deviation=0;this.description="";}},set_type:function(type){if(type==1||type==2||type==3){this.type=type;return true;}
return false;},set_deviation:function(val){this.deviation=val;return true;},set_name:function(val){this.name=val;return true;},set_description:function(val){this.description=val;return true;},insert_point:function(index,lat,lon,radius){if(typeof radius=='undefined')
radius=0;var data={y:lat,x:lon,r:radius};if(index<0||index>=this.points.length){this.points.push(new WialonGeozonePoint(data));if(this.control_points instanceof Array)
this.control_points.push(new WialonControlPoint(null));}else{this.points.splice(index,0,new WialonGeozonePoint(data));if(this.control_points instanceof Array)
this.control_points.splice(index,0,new WialonControlPoint(null));}},delete_point:function(index){if(index>=this.points.length)
return false;this.points.splice(index,1);if(this.control_points instanceof Array)
this.control_points.splice(index,1);return true;},set_control_point:function(index,data){if(typeof data!='object'||index>=this.points.length)
return;if(!(this.control_points instanceof Array)){this.control_points=new Array;for(var i=0;i<this.points.length;i++)
this.control_points.push(new WialonControlPoint(null));}
data.arrive=atoui(data.arrive);data.arrive_offset=atoui(data.arrive_offset);data.departure=atoui(data.departure);data.departure_offset=atoui(data.departure_offset);this.control_points[index]=data;},delete_control_point:function(index){if(!(this.control_points instanceof Array)||index>=this.points.length)
return;this.control_points[index]=new WialonControlPoint(null);},delete_points:function(index){this.points=new Array;if(this.control_points instanceof Array){this.control_points=new Array;delete this.control_points;}},set_points:function(col){this.delete_points();for(var i=0;i<col.length;i++)
this.points.push(col[i]);}});Wialon.add_params_constructor("geozone",function(state){return new WialonGeozone(state);});var WialonUnitSensorsPlugin=WialonItemPlugin.extend({sensors:{},sensor_limit:-1,get_type:function(){return"avl_unit_sensors";},init_state:function(state,item){this.base(state,item);if(typeof state.sens!="undefined"){this.sensors={};for(var id in state.sens)
this.sensors[id]=new WialonUnitSensor(state.sens[id]);}
if(typeof state.sensmax!="undefined")
this.sensor_limit=state.sensmax;if(typeof state.sensu!="undefined"&&state.sensu.length==2){var id=state.sensu[0];var udata=state.sensu[1];if(!udata){if(typeof this.sensors[id]!="undefined")
delete this.sensors[id];}else
this.sensors[id]=new WialonUnitSensor(udata);}},get_sensors:function(sens_type){var col=new Array;if(typeof sens_type=="undefined")
sens_type="";for(var id in this.sensors){var sensor=this.sensors[id];if(sens_type==""||sens_type==sensor.get_type())
col.push(sensor);}
return col;},get_sensor:function(id){var n=this.sensors[id];if(typeof n!="undefined")
return n;return null;},get_sensors_limit:function(){return this.sensor_limit;},create_sensor:function(sensor,callback,cparam){if(!sensor||this.item.get_access_level()<Wialon.access_level("edit")||sensor.get_type()==""||sensor.get_name()==""||sensor.get_param_name()=="")
return false;var item_plugin=this;return Wialon.exec_service({svc:"create_unit_sensor",item_id:this.item.get_id(),sensor_data:WebAjax.to_list(sensor)},function(result,text){if(!result){callback(0,text,cparam);return;}
var sensor_id=parseInt(text);callback(sensor_id,"",cparam);});},update_sensor:function(sensor_id,sensor,callback,cparam){if(!sensor||this.item.get_access_level()<Wialon.access_level("edit")||sensor.get_type()==""||sensor.get_name()==""||sensor.get_param_name()==""||typeof this.sensors[sensor_id]=="undefined")
return false;var item_plugin=this;return Wialon.exec_service({svc:"update_unit_sensor",item_id:this.item.get_id(),sensor_data:WebAjax.to_list(sensor),sensor_id:sensor_id},function(result,text){if(!result){callback(false,text,cparam);return;}
callback(true,"",cparam);});},delete_sensor:function(sensor,callback,cparam){if(!sensor||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"delete_unit_sensor",item_id:this.item.get_id(),sensor_id:sensor.get_id()},function(result,text){if(!result){callback(false,text,cparam);return;}
if(typeof item_plugin.sensors[sensor.id]!="undefined")
delete item_plugin.sensors[sensor.id];callback(true,"",cparam);});}});var WialonUnitSensor=Base.extend({constructor:function(state){this.id=state.id;this.type=state.tp;this.name=state.nm;this.descr=state.de;this.measure=state.me;this.param_name=state.pn;this.flags=state.fl;this.calc_table=state.tbl;},name:"",type:"",id:0,descr:"",measure:"",param_name:"",flags:0,calc_table:[],get_id:function(){return this.id;},get_type:function(){return this.type;},get_name:function(){return this.name;},get_param_name:function(){return this.param_name;},get_flags:function(){return this.flags;},get_description:function(){return this.descr;},get_measurement:function(){return this.measure;},get_calc_table:function(){return this.calc_table;},calculate:function(msg,inv_value){var param=this.get_param_name();if(!msg)
return inv_value;var param_val=0;if(param.substr(0,2)=="in"){if(!(msg.get_flags()&0x2))
return inv_value;var idx=parseInt(param.substr(2));if(idx<1||idx>32||isNaN(idx))
return this.calculate_val(msg.get_inputs(),inv_value);var bit=1<<(idx-1);param_val=(msg.get_inputs()&bit)?1:0;}else if(param.substr(0,3)=="out"){if(!(msg.get_flags()&0x4))
return inv_value;var idx=parseInt(param.substr(3));if(idx<1||idx>32||isNaN(idx))
return this.calculate_val(msg.get_outputs(),inv_value);var bit=1<<(idx-1);param_val=(msg.get_outputs()&bit)?1:0;}else{var msg_params=msg.get_params();if(msg_params&&typeof msg_params[param]!="undefined")
param_val=msg_params[param];else
return inv_value;}
return this.calculate_val(param_val,inv_value);},calculate_val:function(param_val,inv_value){if(isNaN(param_val))
return inv_value;var val=param_val;for(var i=0;i<this.calc_table.length;i++){if(i!=0&&this.calc_table[i][0]>param_val)
return val;val=parseFloat(this.calc_table[i][1])*parseFloat(param_val)+parseFloat(this.calc_table[i][2]);}
return val;}});var WialonUnitSensorConstructor=WialonUnitSensor.extend({constructor:function(){this.calc_table=[];},set_type:function(val){this.type=val;},set_name:function(val){this.name=val;},set_param_name:function(val){this.param_name=val;},set_flags:function(val){this.flags=val;},set_description:function(val){this.descr=val;},set_measurement:function(val){this.measure=val;},set_calc_table:function(tbl){this.calc_table=[];for(var i=0;i<tbl.length;i++){var row=tbl[i];if(typeof row.x=="undefined"||typeof row.a=="undefined"||typeof row.b=="undefined")
continue;this.calc_table.push([row.x,row.a,row.b]);}}});Wialon.add_item_plugin("avl_unit","avl_unit_sensors",WialonUnitSensorsPlugin);var WialonItemFieldsPlugin=WialonItemPlugin.extend({fields:{},fields_names:{},fields_limit:-1,get_type:function(){return"avl_item_fields";},init_state:function(state,item){this.base(state,item);if(typeof state.flds!="undefined"){this.fields={};this.fields_names={};for(var id in state.flds){this.fields[id]=new WialonItemField(state.flds[id]);this.fields_names[this.fields[id].get_name()]=this.fields[id];}}
if(typeof state.fldsmax!="undefined")
this.fields_limit=state.fldsmax;if(typeof state.fldsu!="undefined"&&state.fldsu.length==2){var id=state.fldsu[0];var udata=state.fldsu[1];if(typeof this.fields[id]!="undefined")
delete this.fields_names[this.fields[id].get_name()];if(!udata){if(typeof this.fields[id]!="undefined")
delete this.fields[id];}else{this.fields[id]=new WialonItemField(udata);this.fields_names[this.fields[id].get_name()]=this.fields[id];}}},get_fields:function(){var col=new Array;for(var id in this.fields){col.push(this.fields[id]);}
return col;},get_field:function(id){var n=this.fields[id];if(typeof n!="undefined")
return n;return null;},get_field_by_name:function(name){var n=this.fields_names[name];if(typeof n!="undefined")
return n;return null;},get_fields_limit:function(){return this.fields_limit;},create_field:function(field,callback,cparam){if(!field||this.item.get_access_level()<Wialon.access_level("edit")||field.get_name()=="")
return false;var item_plugin=this;return Wialon.exec_service({svc:"create_item_field",item_id:this.item.get_id(),field_data:WebAjax.to_list(field)},function(result,text){if(!result){callback(0,text,cparam);return;}
var field_id=parseInt(text);callback(field_id,"",cparam);});},update_field:function(field_id,field,callback,cparam){if(!field||this.item.get_access_level()<Wialon.access_level("edit")||field.get_name()=="")
return false;var item_plugin=this;return Wialon.exec_service({svc:"update_item_field",item_id:this.item.get_id(),field_data:WebAjax.to_list(field),field_id:field_id},function(result,text){if(!result){callback(false,text,cparam);return;}
callback(true,"",cparam);});},delete_field:function(field,callback,cparam){if(!field||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"delete_item_field",item_id:this.item.get_id(),field_id:field.get_id()},function(result,text){if(!result){callback(false,text,cparam);return;}
callback(true,"",cparam);});}});var WialonItemField=Base.extend({constructor:function(state){this.id=state.id;this.name=state.nm;this.value=state.vl;},name:"",value:"",id:0,get_id:function(){return this.id;},get_value:function(){return this.value;},get_name:function(){return this.name;}});var WialonItemFieldConstructor=WialonItemField.extend({constructor:function(){},set_value:function(val){this.value=val;},set_name:function(val){this.name=val;}});Wialon.add_item_plugin("avl_unit","avl_item_fields",WialonItemFieldsPlugin);Wialon.add_item_plugin("user","avl_item_fields",WialonItemFieldsPlugin);var WialonUnitGroup=WialonItem.extend({constructor:function(state,plugins){this.plugins=plugins;this.init_state(state);},units:[],child_groups:[],acl_propagated:0,get_units:function(){return Wialon.get_items_from_id_col(this.units);},check_unit:function(unit){return this.check_item_in_id_col(this.units,unit.get_id());},get_child_groups:function(){return Wialon.get_items_from_id_col(this.child_groups);},check_child_group:function(group){return this.check_item_in_id_col(this.child_groups,group.get_id());},is_acl_propogated:function(){return this.acl_propagated;},add_unit:function(unit){if(this.get_access_level()<Wialon.access_level("edit")||!unit)
return false;if(this.is_acl_propogated()&&unit.get_access_level()<Wialon.access_level("manage"))
return false;return this.add_item_to_id_col("unit_group_add_unit",this.units,unit.get_id());},remove_unit:function(unit){if(this.get_access_level()<Wialon.access_level("edit")||!unit)
return false;if(this.is_acl_propogated()&&unit.get_access_level()<Wialon.access_level("manage"))
return false;return this.remove_item_from_id_col("unit_group_remove_unit",this.units,unit.get_id());},set_units:function(units){if(this.get_access_level()<Wialon.access_level("edit")||!units||!units.length)
return false;for(var i=0;i<units.length;i++)
if(this.is_acl_propogated()&&units[i].get_access_level()<Wialon.access_level("manage"))
return false;var ids="";for(var i=0;i<units.length;i++)
ids+=(i>0?",":"")+units[i].get_id();this.remove_modification("unit_group_set_units",function(s){return s==ids;});this.add_modification("unit_group_set_units",ids);return true;},init_state:function(state){this.base(state);if(typeof state.u!="undefined")
this.units=state.u;if(typeof state.g!="undefined")
this.child_groups=state.g;if(typeof state.acl_ppg!="undefined")
this.acl_propagated=state.acl_ppg;return true;}});var WialonUnitGroupPlugin=WialonItemPlugin.extend({groups:[],get_type:function(){return"avl_unit_groups";},init_state:function(state,item){this.base(state,item);if(typeof state.ugs!="undefined")
this.groups=state.ugs;},populate_access_propogation_items:function(cset){this.base(cset);for(var i=0;i<this.groups.length;i++)
cset[this.groups[i]]=1;},handle_event:function(evt){}});Wialon.extend({get_unit_groups:function(){return this.get_items("avl_unit_group");},create_unit_group:function(name,propogate_acl,callback,cparam,creator){if(!this.can_create_item()){return false;};var creator_id=0;if(creator&&typeof creator=="object")
creator_id=creator.get_id();if(name=="")
return false;return this.exec_service({svc:"create_unit_group",name:name,creator_id:creator_id,propogate_acl:propogate_acl?1:0},function(result,text){if(!result){callback(null,text,cparam);return;}
var data;eval("data ="+text);var item=Wialon.construct_item(data);delete data;var user=creator;if(!user)
user=Wialon.get_current_user();Wialon.set_user_access_for_item(item,user,Wialon.access_level("manage"));callback(item,"",cparam);if(item)
Wialon.on_item_created(item);});}});Wialon.add_item_constructor("avl_unit_group",WialonUnitGroup);Wialon.add_item_plugin("avl_unit","avl_unit_groups",WialonUnitGroupPlugin);var WialonZonesLibraryPlugin=WialonItemPlugin.extend({zones:{},zones_limit:-1,get_type:function(){return"avl_zones_library";},init_state:function(state,item){this.base(state,item);if(typeof state.zl!="undefined"){this.zones={};for(var id in state.zl)
this.zones[id]=new WialonGeozone(state.zl[id]);}
if(typeof state.zlmax!="undefined")
this.zones_limit=state.zlmax;if(typeof state.zlu!="undefined"&&state.zlu.length==2){var id=state.zlu[0];var zdata=state.zlu[1];if(!zdata){if(typeof this.zones[id]!="undefined")
delete this.zones[id];}else
this.zones[id]=new WialonGeozone(zdata);}},get_zones:function(){var col=new Array;for(var id in this.zones)
col.push(this.zones[id]);return col;},get_zone:function(id){var zone=this.zones[id];if(typeof zone!="undefined")
return zone;return null;},get_zones_limit:function(){return this.zones_limit;},create_zone:function(zone,callback,cparam){if(!zone||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"create_library_zone",item_id:this.item.get_id(),zone_data:WebAjax.to_list(zone)},function(result,text){if(!result){callback(0,text,cparam);return;}
var zone_id=parseInt(text);if(zone_id){var gz=new WialonGeozoneConstructor(zone);gz.id=zone_id;item_plugin.zones[zone_id]=gz;}
callback(zone_id,"",cparam);});},update_zone:function(zone,callback,cparam){if(!zone||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"update_library_zone",item_id:this.item.get_id(),zone_data:WebAjax.to_list(zone)},function(result,text){if(!result){callback(false,text,cparam);return;}
item_plugin.zones[zone.id]=new WialonGeozoneConstructor(zone);callback(true,"",cparam);});},delete_zone:function(zone,callback,cparam){if(!zone||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"delete_library_zone",item_id:this.item.get_id(),zone_id:zone.get_id()},function(result,text){if(!result){callback(false,text,cparam);return;}
if(typeof item_plugin.zones[zone.id]!="undefined")
delete item_plugin.zones[zone.id];callback(true,"",cparam);});}});Wialon.add_item_plugin("avl_resource","avl_zones_library",WialonZonesLibraryPlugin);var WialonTimeSchedule=Base.extend({constructor:function(state){if(typeof state=='object'&&state){if(typeof state.time_from1!="undefined")
this.time_from1=state.time_from1;else
this.time_from1=state.f1;if(typeof state.time_from2!="undefined")
this.time_from2=state.time_from2;else
this.time_from2=state.f2;if(typeof state.time_to1!="undefined")
this.time_to1=state.time_to1;else
this.time_to1=state.t1;if(typeof state.time_to2!="undefined")
this.time_to2=state.time_to2;else
this.time_to2=state.t2;if(typeof state.month_day_mask!="undefined")
this.month_day_mask=state.month_day_mask;else
this.month_day_mask=state.mdm;if(typeof state.year_month_mask!="undefined")
this.year_month_mask=state.year_month_mask;else
this.year_month_mask=state.ymm;if(typeof state.week_day_mask!="undefined")
this.week_day_mask=state.week_day_mask;else
this.week_day_mask=state.wdm;}},time_from1:0,time_from2:0,time_to1:0,time_to2:0,month_day_mask:0,year_month_mask:0,week_day_mask:0,get_time_from1:function(){return this.time_from1;},get_time_from2:function(){return this.time_from2;},get_time_to1:function(){return this.time_to1;},get_time_to2:function(){return this.time_to2;},get_month_day_mask:function(){return this.month_day_mask;},get_year_month_mask:function(){return this.year_month_mask;},get_week_day_mask:function(){return this.week_day_mask;},set_time_from1:function(val){this.time_from1=val;},set_time_to1:function(val){this.time_to1=val;},set_time_from2:function(val){this.time_from2=val;},set_time_to2:function(val){this.time_to2=val;},set_month_day_mask:function(val){this.month_day_mask=val;},set_year_month_mask2:function(val){this.year_month_mask=val;},set_week_day_mask:function(val){this.week_day_mask=val;}});var WialonJobsPlugin=WialonItemPlugin.extend({jobs:{},job_limit:-1,get_type:function(){return"avl_jobs";},init_state:function(state,item){this.base(state,item);if(typeof state.ujb!="undefined"){this.jobs={};for(var id in state.ujb)
this.jobs[id]=new WialonJob(state.ujb[id]);}
if(typeof state.ujbmax!="undefined")
this.jobs_limit=state.ujbmax;if(typeof state.ujbu!="undefined"&&state.ujbu.length==2){var id=state.ujbu[0];var udata=state.ujbu[1];if(!udata){if(typeof this.jobs[id]!="undefined")
delete this.jobs[id];}else
this.jobs[id]=new WialonJob(udata);}},get_jobs:function(){var col=new Array;for(var id in this.jobs)
col.push(this.jobs[id]);return col;},get_job:function(id){var n=this.jobs[id];if(typeof n!="undefined")
return n;return null;},get_jobs_limit:function(){return this.jobs_limit;},create_job:function(job,callback,cparam){if(!job||this.item.get_access_level()<Wialon.access_level("edit")||typeof job.get_action()!="object")
return false;var item_plugin=this;return Wialon.exec_service({svc:"create_job",item_id:this.item.get_id(),job_data:WebAjax.to_list(job)},function(result,text){if(!result){if(callback)
callback(0,text,cparam);return;}
var job_id=parseInt(text);if(job_id){var n=new WialonJobConstructor(job);n.id=job_id;item_plugin.jobs[job_id]=n;}
if(callback)
callback(job_id,"",cparam);});},update_job:function(job_id,job,callback,cparam){if(!job||this.item.get_access_level()<Wialon.access_level("edit")||typeof job.get_action()!="object")
return false;var item_plugin=this;return Wialon.exec_service({svc:"update_job",item_id:this.item.get_id(),job_data:WebAjax.to_list(job),job_id:job_id},function(result,text){if(!result){if(callback)
callback(false,text,cparam);return;}
if(callback)
callback(true,"",cparam);});},delete_job:function(job,callback,cparam){if(!job||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"delete_job",item_id:this.item.get_id(),job_id:job.get_id()},function(result,text){if(!result){if(callback)
callback(false,text,cparam);return;}
if(typeof item_plugin.jobs[job.id]!="undefined")
delete item_plugin.jobs[job.id];if(callback)
callback(true,"",cparam);});},enable_job:function(job,enabled,callback,cparam){if(!job||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"enable_job",item_id:this.item.get_id(),job_id:job.get_id(),job_enabled:enabled},function(result,text){if(!result){if(callback)
callback(false,text,cparam);return;}
if(callback)
callback(true,"",cparam);});}});var WialonJob=Base.extend({constructor:function(state){this.id=state.id;this.descr=state.de;this.name=state.nm;this.reg_schedule=state.rsch;this.act_time=state.at;this.max_times=state.mt;this.tz=state.tz;this.lang=state.la;this.job_enabled=state.je;this.done_times=state.dt;this.last_done_time=state.ldt;this.schedule=new WialonTimeSchedule(state.sch);this.action=new WialonJobAction(state.act);},id:0,descr:"",reg_schedule:"",name:"",act_time:0,max_times:0,tz:0,lang:"",job_enabled:false,done_times:0,last_done_time:0,schedule:null,action:null,get_id:function(){return this.id;},get_description:function(){return this.descr;},get_name:function(){return this.name;},get_reg_schedule:function(){return this.reg_schedule;},get_activation_time:function(){return this.act_time;},get_max_times:function(){return this.max_times;},get_tz_offset:function(){return this.tz;},get_lang:function(){return this.lang;},is_enabled:function(){return this.job_enabled;},get_done_times:function(){return this.done_times;},get_last_done_time:function(){return this.last_done_time;},get_schedule:function(){return this.schedule;},get_action:function(){return this.action;}});var WialonJobAction=WialonParamsItem.extend({constructor:function(state){this.params=state.p;this.type=state.t;},type:"",get_type:function(){return this.type;},get_name:function(){return this.get_param("name");}});var WialonJobConstructor=WialonJob.extend({constructor:function(){},set_description:function(val){this.descr=val;},set_name:function(val){this.name=val;},set_reg_schedule:function(val){this.reg_schedule=val;},set_activation_time:function(val){this.act_time=val;},set_max_times:function(val){this.max_times=val;},set_tz_offset:function(val){this.tz=val;},set_lang:function(val){this.lang=val;},set_schedule:function(sch){if(typeof sch=='object'&&sch)
this.schedule=sch;},set_action:function(act){if(typeof act=='object'&&act)
this.action=act;}});var WialonJobs=Base.extend({constructor:null,create_exec_unit_cmd_action:function(units,cmd_name,cmd_param,link_type,timeout){if(typeof units!='string'||typeof cmd_name!='string')
return null;params=[{t:"text",n:"units",v:units},{t:"text",n:"cmd_name",v:cmd_name},{t:"text",n:"cmd_param",v:cmd_param},{t:"text",n:"link_type",v:link_type},{t:"number",n:"timeout",v:timeout}];return{type:"exec_unit_cmd",params:params};},create_send_email_report_action:function(report_type,units,time_from,time_to,flags,report_id,file_type,email_to,pack,params){var curr_user=Wialon.get_current_user();if(typeof email_to!='string'||!curr_user)
return null;params=[{t:"text",n:"user",v:curr_user.get_guid()},{t:"text",n:"report_type",v:report_type},{t:"text",n:"units",v:units},{t:"number",n:"time_from",v:time_from},{t:"number",n:"time_to",v:time_to},{t:"number",n:"flags",v:flags},{t:"text",n:"report_id",v:report_id},{t:"text",n:"file_type",v:file_type},{t:"text",n:"email_to",v:email_to},{t:"number",n:"pack",v:pack},{t:"text",n:"params",v:params}];return{type:"send_email_report",params:params};},create_send_email_report2_action:function(report_objects,report_guid,report_id,time_from,time_to,flags,file_type,email_to,pack,params){if(typeof email_to!='string')
return null;params=[{t:"text",n:"report_objects",v:report_objects},{t:"text",n:"report_guid",v:report_guid},{t:"number",n:"report_id",v:report_id},{t:"number",n:"time_from",v:time_from},{t:"number",n:"time_to",v:time_to},{t:"number",n:"flags",v:flags},{t:"text",n:"file_type",v:file_type},{t:"text",n:"email_to",v:email_to},{t:"number",n:"pack",v:pack},{t:"text",n:"params",v:params}];return{type:"send_email_report",params:params};},create_send_email_sms_fuel_action:function(units,email_to,phone_to,flags,time_offset){if(typeof email_to!='string'||typeof phone_to!='string')
return null;params=[{t:"text",n:"units",v:units},{t:"text",n:"email_to",v:email_to},{t:"text",n:"phone_to",v:phone_to},{t:"number",n:"flags",v:flags},{t:"number",n:"time_offset",v:time_offset}];return{type:"send_email_sms_fuel",params:params};},create_reset_units_bytes_counter_action:function(units,store_bytes){if(typeof units!='string')
return null;params=[{t:"text",n:"units",v:units},{t:"number",n:"store_bytes",v:store_bytes}];return{type:"reset_unit_bytes_counter",params:params};},create_assign_route_action:function(units,name,description,zone,flags){if(typeof units!="string"||typeof name!="string"||typeof description!="string"||typeof zone!="object"||typeof flags!="number")
return null;params=[{t:"text",n:"units",v:units},{t:"text",n:"name",v:name},{t:"text",n:"description",v:description},{t:"number",n:"zone",v:zone.get_id()},{t:"number",n:"flags",v:flags}];return{type:"assign_route",params:params};},create_remove_route_action:function(units,finished){if(typeof units!="string")
return null;params=[{t:"text",n:"units",v:units},{t:"number",n:"finished",v:0+finished}];return{type:"remove_route",params:params};},create_change_access_user:function(users,acc_level,units){if(typeof users!='string')
return null;params=[{t:"text",n:"users",v:users},{t:"number",n:"acc_level",v:acc_level},{t:"text",n:"units",v:units}];return{type:"change_access_user",params:params};}});Wialon.add_item_plugin("avl_resource","avl_jobs",WialonJobsPlugin);var WialonNotificationsPlugin=WialonItemPlugin.extend({notifications:{},notify_limit:-1,get_type:function(){return"avl_notifications";},init_state:function(state,item){this.base(state,item);if(typeof state.unf!="undefined"){this.notifications={};for(var id in state.unf)
this.notifications[id]=new WialonNotification(state.unf[id]);}
if(typeof state.unfmax!="undefined")
this.notify_limit=state.unfmax;if(typeof state.unfu!="undefined"&&state.unfu.length==2){var id=state.unfu[0];var udata=state.unfu[1];if(!udata){if(typeof this.notifications[id]!="undefined")
delete this.notifications[id];}else
this.notifications[id]=new WialonNotification(udata);}},get_notifications:function(){var col=new Array;for(var id in this.notifications)
col.push(this.notifications[id]);return col;},get_notification:function(id){var n=this.notifications[id];if(typeof n!="undefined")
return n;return null;},get_notifications_limit:function(){return this.notify_limit;},create_notification:function(notification,callback,cparam){if(!notification||this.item.get_access_level()<Wialon.access_level("edit")||notification.get_units()<1||notification.get_actions()<1||!notification.get_trigger())
return false;var item_plugin=this;return Wialon.exec_service({svc:"create_notification",item_id:this.item.get_id(),notification_data:WebAjax.to_list(notification)},function(result,text){if(!result){callback(0,text,cparam);return;}
var notification_id=parseInt(text);callback(notification_id,"",cparam);});},update_notification:function(notification_id,notification,callback,cparam){if(!notification||this.item.get_access_level()<Wialon.access_level("edit")||notification.get_units()<1||notification.get_actions()<1||!notification.get_trigger()||typeof this.notifications[notification_id]=="undefined")
return false;var item_plugin=this;return Wialon.exec_service({svc:"update_notification",item_id:this.item.get_id(),notification_data:WebAjax.to_list(notification),notification_id:notification_id},function(result,text){if(!result){callback(false,text,cparam);return;}
callback(true,"",cparam);});},enable_notification:function(notification_id,notification,enable,callback,cparam){if(!notification||this.item.get_access_level()<Wialon.access_level("edit"))
return false;if(typeof enable=="undefined")
return false;var item_plugin=this;return Wialon.exec_service({svc:"enable_notification",item_id:this.item.get_id(),notification_id:notification.get_id(),notification_enabled:enable,},function(result,text){if(!result){callback(false,text,cparam);return;}
callback(true,"",cparam);});},delete_notification:function(notification,callback,cparam){if(!notification||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"delete_notification",item_id:this.item.get_id(),notification_id:notification.get_id()},function(result,text){if(!result){callback(false,text,cparam);return;}
if(typeof item_plugin.notifications[notification.id]!="undefined")
delete item_plugin.notifications[notification.id];callback(true,"",cparam);});}});var WialonNotification=Base.extend({constructor:function(state){this.id=state.id;this.text=state.txt;this.name=state.nm;this.act_time=state.ta;this.deact_time=state.td;this.max_alarms=state.ma;this.flags=state.fl;this.tz=state.tz;this.lang=state.la;this.max_msgs_time_diff=state.mmtd;this.check_delay_time=state.cdt;this.alarms_count=state.ac;this.min_alarm_state_time=state.mast;this.min_prev_state_time=state.mpst;this.control_period=state.cp;this.schedule=new WialonTimeSchedule(state.sch);this.units=state.un;this.trigger=new WialonNotifyItem(state.trg);this.actions=new Array;for(var i=0;i<state.act.length;i++)
this.actions.push(new WialonNotifyItem(state.act[i]));},text:"",name:"",id:0,act_time:0,deact_time:0,max_alarms:0,max_msgs_time_diff:0,check_delay_time:0,alarms_count:0,min_alarm_state_time:0,min_prev_state_time:0,control_period:0,flags:0,tz:0,lang:"",schedule:null,units:[],trigger:null,actions:[],get_id:function(){return this.id;},get_text:function(){return this.text;},get_name:function(){return this.name;},get_activation_time:function(){return this.act_time;},get_deactivation_time:function(){return this.deact_time;},get_tz_offset:function(){return this.tz;},get_flags:function(){return this.flags;},get_lang:function(){return this.lang;},get_schedule:function(){return this.schedule;},get_max_alarms:function(){return this.max_alarms;},get_alarms_count:function(){return this.alarms_count;},get_max_msgs_time_diff:function(){return this.max_msgs_time_diff;},get_check_delay_time:function(){return this.check_delay_time;},get_min_alarm_state_time:function(){return this.min_alarm_state_time;},get_min_prev_state_time:function(){return this.min_prev_state_time;},get_control_period:function(){return this.control_period;},get_units:function(){return Wialon.get_items_from_id_col(this.units);},get_trigger:function(){return this.trigger;},get_actions:function(){return this.actions;}});var WialonNotifyItem=WialonParamsItem.extend({constructor:function(state){this.params=(state.p&&typeof state.p!="undefined")?state.p:{};this.type=state.t;},type:"",get_type:function(){return this.type;}});var WialonNotificationConstructor=WialonNotification.extend({constructor:function(){this.units=[];this.trigger=null;this.actions=[];},set_text:function(val){this.text=val;},set_name:function(val){this.name=val;},set_activation_time:function(val){this.act_time=val;},set_deactivation_time:function(val){this.deact_time=val;},set_tz_offset:function(val){this.tz=val;},set_max_alarms:function(val){this.max_alarms=val;},set_flags:function(val){this.flags=val;},set_max_msgs_time_diff:function(val){this.max_msgs_time_diff=val;},set_check_delay_time:function(val){this.check_delay_time=val;},set_min_alarm_state_time:function(val){this.min_alarm_state_time=val;},set_min_prev_state_time:function(val){this.min_prev_state_time=val;},set_control_period:function(val){this.control_period=val;},set_lang:function(val){this.lang=val;},set_schedule:function(sch){if(typeof sch=='object'&&sch&&typeof sch.time_from1!='undefined')
this.schedule=sch;},add_unit:function(unit){if(!unit)
return;this.units.push(unit.get_id());},set_trigger:function(trg){if(!trg||!trg.type||!trg.params)
return;this.trigger=trg;},add_action:function(act){if(!act||!act.type||!act.params)
return;this.actions.push(act);}});var WialonNotifications=Base.extend({constructor:null,create_action_sms:function(phones){if(typeof phones!='string')
return null;params=[{t:"text",n:"phones",v:phones}];return{type:"sms",params:params};},create_action_email:function(emails,subject,is_html){if(typeof emails!='string'||typeof subject!='string'||subject==""||(is_html!=0&&is_html!=1))
return null;params=[{t:"text",n:"email_to",v:emails},{t:"text",n:"subj",v:subject},{t:"number",n:"html",v:is_html?1:0}];return{type:"email",params:params};},create_action_message:function(name){if(typeof name!='string'||name=="")
return null;params=[{t:"text",n:"name",v:name}];return{type:"message",params:params};},create_action_unit_alarm:function(name){if(typeof name!='string'||name=="")
return null;params=[{t:"text",n:"name",v:name}];return{type:"alarm",params:params};},create_action_unit_event:function(flags){params=[{t:"number",n:"flags",v:flags}];return{type:"event",params:params};},create_action_exec_cmd:function(name,param,link){if(typeof name!='string')
return null;params=[{t:"text",n:"name",v:name},{t:"text",n:"param",v:param},{t:"text",n:"link",v:link}];return{type:"exec_cmd",params:params};},create_action_status:function(ui_text){params=[{t:"text",n:"ui_text",v:ui_text}];return{type:"status",params:params};},create_trigger_speed:function(min_speed,max_speed,sens_type,sens_mask,lower_bound,upper_bound,sens_merge,trigger_inout){if(typeof min_speed!='number'||typeof max_speed!='number'||min_speed>max_speed)
return null;params=[{t:"number",n:"min_speed",v:min_speed},{t:"number",n:"max_speed",v:max_speed},{t:"text",n:"sensor_type",v:sens_type},{t:"text",n:"sensor_name_mask",v:sens_mask},{t:"double",n:"lower_bound",v:lower_bound},{t:"double",n:"upper_bound",v:upper_bound},{t:"number",n:"merge",v:0+sens_merge},{t:"number",n:"reversed",v:0+trigger_inout}];return{type:"speed",params:params};},create_trigger_sms:function(mask){params=[{t:"text",n:"mask",v:mask}];return{type:"sms",params:params};},create_trigger_idle:function(max_speed,min_idle_time,sens_type,sens_mask,lower_bound,upper_bound,sens_merge,trigger_inout){if(typeof max_speed!='number'||max_speed>=10000)
return null;params=[{t:"number",n:"min_speed",v:max_speed},{t:"number",n:"max_speed",v:10000},{t:"number",n:"min_idle_time",v:min_idle_time},{t:"text",n:"sensor_type",v:sens_type},{t:"text",n:"sensor_name_mask",v:sens_mask},{t:"double",n:"lower_bound",v:lower_bound},{t:"double",n:"upper_bound",v:upper_bound},{t:"number",n:"merge",v:0+sens_merge},{t:"number",n:"reversed",v:0+trigger_inout}];return{type:"speed",params:params};},create_trigger_alarm:function(){params=[];return{type:"alarm",params:params};},create_trigger_zone:function(zone,type,zone_by_ref,min_speed,max_speed,sensor_type,sensor_name_mask,lower,upper,merge,reversed){if((type!=0&&type!=1)||typeof zone!='object')
return null;if(!min_speed||isNaN(min_speed))
min_speed=0;if(!max_speed||isNaN(max_speed))
max_speed=0;if((min_speed||max_speed)&&min_speed>max_speed)
return null;if(typeof zone_by_ref=="undefined")
zone_by_ref=false;var params=[{t:"text",n:"geozone_ids",v:zone.join(",")},{t:"long",n:"geozone_id",v:zone[0]},{t:"number",n:"type",v:type},{t:"number",n:"min_speed",v:min_speed},{t:"number",n:"max_speed",v:max_speed},{t:"text",n:"sensor_type",v:sensor_type},{t:"text",n:"sensor_name_mask",v:sensor_name_mask},{t:"double",n:"lower_bound",v:lower},{t:"double",n:"upper_bound",v:upper},{t:"number",n:"merge",v:merge},{t:"number",n:"reversed",v:reversed}];return{type:"geozone",params:params};},create_trigger_digital_input:function(input_index,reverse){if(typeof input_index!='number'||input_index<1||input_index>32)
return null;params=[{t:"number",n:"input_index",v:input_index},{t:"number",n:"type",v:reverse?1:0}];return{type:"digital_input",params:params};},create_trigger_msg_param:function(param,lower_bound,upper_bound,reverse){if(typeof lower_bound!='number'||typeof upper_bound!='number'||lower_bound>upper_bound||typeof param!='string'||param=="")
return null;params=[{t:"text",n:"param",v:param},{t:"double",n:"lower_bound",v:lower_bound},{t:"double",n:"upper_bound",v:upper_bound},{t:"number",n:"type",v:reverse?1:0}];return{type:"msg_param",params:params};},create_trigger_sensor_value:function(sensor_type,sensor_name_mask,lower_bound,upper_bound,merge,reverse){if(typeof lower_bound!='number'||typeof upper_bound!='number'||lower_bound>upper_bound||((sensor_type==""||sensor_type=="undefined")&&(sensor_name_mask==""||sensor_name_mask=="undefined")))
return null;params=[{t:"text",n:"sensor_type",v:sensor_type},{t:"text",n:"sensor_name_mask",v:sensor_name_mask},{t:"double",n:"lower_bound",v:lower_bound},{t:"double",n:"upper_bound",v:upper_bound},{t:"number",n:"merge",v:merge?1:0},{t:"number",n:"type",v:reverse?1:0}];return{type:"sensor_value",params:params};},create_trigger_outage:function(otype,time){if(typeof otype!='number'||typeof time!='number'||time<=0)
return null;params=[{t:"number",n:"type",v:otype},{t:"number",n:"time",v:time}];return{type:"outage",params:params};},create_action_user_access:function(users,acc_level){if(typeof users!='string')
return null;params=[{t:"text",n:"users",v:users},{t:"number",n:"acc_level",v:acc_level}];return{type:"user_access",params:params};},create_action_set_counter:function(mileage,engine_hours,traffic,flags){params=[{t:"double",n:"mileage",v:mileage},{t:"number",n:"engine_hours",v:engine_hours},{t:"number",n:"traffic",v:traffic},{t:"number",n:"flags",v:flags}];return{type:"counter",params:params};}});var WialonNotificationMsg=WialonMsg.extend({constructor:function(state){this.init_state(state);this.text=this.text.replace(/\n/g,"<br>");},name:"",text:"",unit_guid:"",lon:0,lat:0,get_unit:function(){return Wialon.get_item_by_guid(this.unit_guid);},get_name:function(){return this.name;},get_text:function(){return this.text;},get_lat:function(){return this.lat;},get_lon:function(){return this.lon;},init_state:function(state){this.base(state);this.unit_guid=state.unit;this.name=state.name;this.text=state.txt;this.lat=state.y;this.lon=state.x;return true;}});Wialon.add_msg_constructor("unm",WialonNotificationMsg);Wialon.add_item_plugin("avl_resource","avl_notifications",WialonNotificationsPlugin);var WialonDriversPlugin=WialonItemPlugin.extend({drivers:{},drivers_limit:-1,get_type:function(){return"avl_drivers";},init_state:function(state,item){this.base(state,item);if(typeof state.drvrs!="undefined"){this.drivers={};for(var id in state.drvrs)
this.drivers[id]=new WialonDriver(state.drvrs[id],this.item.get_id());}
if(typeof state.drvrsmax!="undefined")
this.drivers_limit=state.drvrsmax;if(typeof state.drvrsu!="undefined"&&state.drvrsu.length==2){var id=state.drvrsu[0];var udata=state.drvrsu[1];if(!udata){if(typeof this.drivers[id]!="undefined")
delete this.drivers[id];}else
this.drivers[id]=new WialonDriver(udata,this.item.get_id());}},get_drivers:function(){var col=new Array;for(var id in this.drivers)
col.push(this.drivers[id]);return col;},get_driver:function(id){var n=this.drivers[id];if(typeof n!="undefined")
return n;return null;},get_drivers_limit:function(){return this.drivers_limit;},create_driver:function(name,code,descr,phone,callback,cparam){if(this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"create_driver",item_id:this.item.get_id(),name:name,code:code,descr:descr,phone:phone},function(result,text){if(!result){callback(0,text,cparam);return;}
var driver_id=parseInt(text);callback(driver_id,"",cparam);});},update_driver:function(driver_id,name,code,descr,phone,callback,cparam){if(this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"update_driver",item_id:this.item.get_id(),driver_id:driver_id,name:name,code:code,descr:descr,phone:phone},function(result,text){if(!result){callback(false,text,cparam);return;}
callback(true,"",cparam);});},delete_driver:function(driver,callback,cparam){if(!driver||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"delete_driver",item_id:this.item.get_id(),driver_id:driver.get_id()},function(result,text){if(!result){callback(false,text,cparam);return;}
if(typeof item_plugin.drivers[driver.id]!="undefined")
delete item_plugin.drivers[driver.id];callback(true,"",cparam);});},bind_driver:function(driver_id,unit_id,date,callback,cparam){var item_plugin=this;return Wialon.exec_service({svc:"bind_driver",item_id:this.item.get_id(),driver_id:driver_id,unit_id:unit_id,date:date},function(result,text){if(!result){callback(false,text,cparam);return;}
callback(true,"",cparam);});}});var WialonDriver=Base.extend({constructor:function(state,item_id){this.id=state.id;this.name=state.nm;this.code=state.c;this.description=state.ds;this.ratio=state.r;this.cookie=state.ck;if(state.p)
this.phone=state.p;this.item_id=item_id;},name:"",code:0,description:"",id:0,ratio:0,cookie:0,phone:"",item_id:0,get_id:function(){return this.id;},get_name:function(){return this.name;},get_code:function(){return this.code;},get_description:function(){return this.description;},get_ratio:function(){return this.ratio;},get_image_url:function(border){if(!this.cookie)
return"";if(typeof border=='undefined'||!border)
border=32;return"/avl_driver/get.png?rid="+this.item_id+"&did="+this.id+"&c="+this.cookie+"&b="+border;},get_phone_number:function(){return this.phone;}});var WialonDriverConstructor=WialonDriver.extend({constructor:function(){},set_name:function(val){this.name=val;},set_code:function(code){this.code=code;},set_description:function(description){this.description=description;},set_phone_number:function(phone){this.phone=phone;}});Wialon.add_item_plugin("avl_resource","avl_drivers",WialonDriversPlugin);var WialonItemIconPlugin=WialonItemPlugin.extend({cookie:0,get_type:function(){return"avl_item_icon";},get_icon_url:function(border){if(typeof border=='undefined'||!border)
border=32;return"/avl_icon/get.png?i="+this.id+"&c="+this.cookie+"&b="+border;},init_state:function(state,item){this.base(state,item);if(typeof state.ugi!="undefined")
this.cookie=state.ugi;}});Wialon.add_item_plugin("avl_unit","avl_item_icon",WialonItemIconPlugin);Wialon.add_item_plugin("avl_unit_group","avl_item_icon",WialonItemIconPlugin);var WialonPOIManagerPlugin=WialonItemPlugin.extend({pois:{},poi_limit:-1,get_type:function(){return"avl_poi_manager";},init_state:function(state,item){this.base(state,item);if(typeof state.poi!="undefined"){this.pois={};for(var id in state.poi)
this.pois[id]=new WialonPOI(state.poi[id],this.item.get_id());}
if(typeof state.poimax!="undefined")
this.poi_limit=state.poimax;if(typeof state.poiu!="undefined"&&state.poiu.length==2){var id=state.poiu[0];var udata=state.poiu[1];if(!udata){if(typeof this.pois[id]!="undefined")
delete this.pois[id];}else
this.pois[id]=new WialonPOI(udata,this.item.get_id());}},get_pois:function(){var col=new Array;for(var id in this.pois)
col.push(this.pois[id]);return col;},get_poi:function(id){var n=this.pois[id];if(typeof n!="undefined")
return n;return null;},get_pois_limit:function(){return this.poi_limit;},create_poi:function(name,descr,lat,lon,radius,callback,cparam){if(this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"create_poi",item_id:this.item.get_id(),name:name,descr:descr,lat:lat,lon:lon,radius:radius},function(result,text){if(!result){callback(0,text,cparam);return;}
var poi_id=parseInt(text);if(poi_id){var n=new WialonPOI({id:poi_id,de:descr,nm:name,y:lat,x:lon,r:1,d:100,c:0},item_plugin.item.get_id());item_plugin.pois[poi_id]=n;}
callback(poi_id,"",cparam);});},update_poi:function(poi_id,name,descr,lat,lon,radius,callback,cparam){if(this.item.get_access_level()<Wialon.access_level("edit")||typeof this.pois[poi_id]=="undefined")
return false;var item_plugin=this;return Wialon.exec_service({svc:"update_poi",item_id:this.item.get_id(),poi_id:poi_id,name:name,descr:descr,lat:lat,lon:lon,radius:radius,update_image:"no"},function(result,text){if(!result){callback(false,text,cparam);return;}
var n=new WialonPOI({id:poi_id,de:descr,nm:name,y:lat,x:lon,r:1,d:100,c:0},item_plugin.item.get_id());item_plugin.pois[poi_id]=n;callback(true,"",cparam);});},delete_poi:function(poi,callback,cparam){if(!poi||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"delete_poi",item_id:this.item.get_id(),poi_id:poi.get_id()},function(result,text){if(!result){callback(false,text,cparam);return;}
if(typeof item_plugin.pois[poi.id]!="undefined")
delete item_plugin.pois[poi.id];callback(true,"",cparam);});}});var WialonPOI=Base.extend({constructor:function(state,item_id){this.id=state.id;this.descr=state.de;this.name=state.nm;this.lat=state.y;this.lon=state.x;this.ratio=state.r;this.radius=state.d;this.cookie=state.c;this.item_id=item_id;},descr:"",name:"",id:0,lat:0.0,lon:0.0,ratio:0.0,radius:100,cookie:0,item_id:0,get_id:function(){return this.id;},get_description:function(){return this.descr;},get_name:function(){return this.name;},get_lat:function(){return this.lat;},get_lon:function(){return this.lon;},get_radius:function(){return this.radius;},get_ratio:function(){return this.ratio;},get_image_url:function(border){if(!this.cookie)
return"";if(typeof border=='undefined'||!border)
border=32;return"/avl_poi/get.png?rid="+this.item_id+"&pid="+this.id+"&c="+this.cookie+"&b="+border;}});Wialon.add_item_plugin("avl_resource","avl_poi_manager",WialonPOIManagerPlugin);var WialonReportTemplatesPlugin=WialonItemPlugin.extend({report_templates:{},report_templates_limit:-1,get_type:function(){return"avl_report_templates";},init_state:function(state,item){this.base(state,item);if(typeof state.rep!="undefined"){this.report_templates={};for(var id in state.rep)
this.report_templates[id]=new WialonReportTemplate(state.rep[id]);}
if(typeof state.repmax!="undefined")
this.report_templates_limit=state.repmax;if(typeof state.repu!="undefined"&&state.repu.length==2){var id=state.repu[0];var udata=state.repu[1];if(!udata){if(typeof this.report_templates[id]!="undefined")
delete this.report_templates[id];}else
this.report_templates[id]=new WialonReportTemplate(udata);}},get_report_templates:function(){var col=new Array;for(var id in this.report_templates)
col.push(this.report_templates[id]);return col;},get_report_template:function(id){var n=this.report_templates[id];if(typeof n!="undefined")
return n;return null;},get_report_templates_limit:function(){return this.report_templates_limit;},create_report_template:function(report_template,callback,cparam){if(!report_template||this.item.get_access_level()<Wialon.access_level("view"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"create_report_template",item_id:this.item.get_id(),report_template:WebAjax.to_list(report_template)},function(result,text){if(!result){callback(0,text,cparam);return;}
var report_template_id=parseInt(text);callback(report_template_id,"",cparam);});},update_report_template:function(report_template_id,report_template,callback,cparam){if(!report_template||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"update_report_template",item_id:this.item.get_id(),report_template:WebAjax.to_list(report_template),report_template_id:report_template_id},function(result,text){if(!result){callback(false,text,cparam);return;}
callback(true,"",cparam);});},delete_report_template:function(report_template,callback,cparam){if(!report_template||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"delete_report_template",item_id:this.item.get_id(),report_template_id:report_template.get_id()},function(result,text){if(!result){callback(false,text,cparam);return;}
if(typeof item_plugin.report_templates[report_template.id]!="undefined")
delete item_plugin.report_templates[report_template.id];callback(true,"",cparam);});},generate_report:function(report_template,object_id,object_prop_id,interval,lang,tz_offset,callback,cparam){if(!report_template||!interval)
return false;var item_plugin=this;return Wialon.exec_service({svc:"generate_report",item_id:this.item.get_id(),report_template_id:report_template.get_id(),from:interval.time_from,to:interval.time_to,flags:interval.flags,object_id:object_id,object_prop_id:object_prop_id,lang:lang,tz_offset:tz_offset},function(result,text){if(!result||text==""){callback(false,text,null,cparam);return;}
var data;eval("data = "+text);var rres;if(typeof data.rres!='undefined')
rres=data.rres;else
rres=[];var rweb;if(typeof data.rweb!='undefined')
rweb=data.rweb;else
rweb="report_template_result";var ren=null;if(typeof data.renjson!='undefined')
ren=data.renjson;delete data;callback(true,rres,ren,rweb,cparam);},180000);},get_report_result_rows:function(rres_name,table_index,missing_intervals,callback,cparam){if(!rres_name.length||!missing_intervals)
return false;var item_plugin=this;return Wialon.exec_service({svc:"report_result_rows",rres_name:rres_name,table_index:table_index,missing_intervals:WebAjax.to_list(missing_intervals)},function(result,text){if(!result||text==""){callback(false,text,null,cparam);return;}
var data;eval("data = "+text);var r;if(typeof data.r!='undefined')
r=data.r;else
r=[];delete data;callback(true,r,cparam);});},get_report_result_subrows:function(rres_name,table_index,row_index,callback,cparam){if(!rres_name.length)
return false;var item_plugin=this;return Wialon.exec_service({svc:"report_result_rows",rres_name:rres_name,table_index:table_index,row_index:row_index},function(result,text){if(!result||text==""){callback(false,text,null,cparam);return;}
var data;eval("data = "+text);delete data;callback(true,data,cparam);});},get_row_trip:function(rres_name,msg_from,msg_to,callback,cparam){if(!rres_name.length)
return false;var item_plugin=this;return Wialon.exec_service({svc:"report_result_trip",rres_name:rres_name,msg_from:msg_from,msg_to:msg_to},function(result,text){if(!result||text==""){callback(false,null,cparam);return;}
var data;eval("data = "+text);callback(true,data,cparam);});}});var WialonReportTemplate=Base.extend({constructor:function(state){this.id=state.id;this.name=state.nm;this.calc_type=state.ct;},id:0,name:"",calc_type:"",get_id:function(){return this.id;},get_name:function(){return this.name;},get_calc_type:function(){return this.calc_type;}});Wialon.add_item_plugin("avl_resource","avl_report_templates",WialonReportTemplatesPlugin);var WialonRoutesControlPlugin=WialonItemPlugin.extend({routes:{},routes_limit:-1,get_type:function(){return"avl_route_control";},init_state:function(state,item){this.base(state,item);if(typeof state.rc!="undefined"){this.routes={};for(var id in state.rc)
this.routes[id]=new WialonUnitRoute(state.rc[id],this.item);}
if(typeof state.rcmax!="undefined")
this.routes_limit=state.rcmax;if(typeof state.rcu!="undefined"&&state.rcu.length==2){var id=state.rcu[0];var udata=state.rcu[1];if(!udata){if(typeof this.routes[id]!="undefined")
delete this.routes[id];}else
this.routes[id]=new WialonUnitRoute(udata,this.item);}},get_route:function(id){var n=this.routes[id];if(typeof n!="undefined")
return n;return null;},get_routes:function(){var col=new Array;for(var id in this.routes)
col.push(this.routes[id]);return col;},get_routes_limit:function(){return this.routes_limit;},create_route:function(route,callback,cparam){if(this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"create_route_control",item_id:this.item.get_id(),route:WebAjax.to_list(route),lang:WebCMS.get_lang()},function(result,text){if(!result){if(callback)
callback(false,text,cparam);return;}
var rc_id=parseInt(text);if(rc_id){item_plugin.routes[rc_id]=route;item_plugin.id=rc_id;route.id=rc_id;}
if(callback)
callback(rc_id,"",cparam);});},update_route:function(route_id,route,callback,cparam){if(this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"update_route_control",item_id:this.item.get_id(),route_id:route_id,route:WebAjax.to_list(route),lang:WebCMS.get_lang()},function(result,text){if(!result){callback(false,text,cparam);return;}
if(route_id){item_plugin.routes[route_id]=route;item_plugin.routes[route_id].id=route_id;}
callback(true,"",cparam);});},delete_route:function(route,callback,cparam){if(this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"delete_route_control",item_id:this.item.get_id(),route_id:route.get_id()},function(result,text){if(!result){callback(false,text,cparam);return;}
if(typeof item_plugin.routes[route.get_id()]!="undefined")
delete item_plugin.routes[route.get_id()];callback(true,"",cparam);});},enable_route:function(route,enabled,callback,cparam){if(!route||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"enable_route_control",item_id:this.item.get_id(),route_id:route.get_id(),route_enabled:enabled},function(result,text){if(!result){if(callback)
callback(false,text,cparam);return;}
if(callback)
callback(true,"",cparam);});}});var WialonUnitRoute=Base.extend({constructor:function(state,resource){this.id=state.i;this.name=state.nm;this.descr=state.de;this.activation_time=state.at;this.flags=state.fl;this.status=state.st;this.point_index=state.pt;this.update_time=state.tm;if(state.z){var pl=resource.get_plugin("avl_zones_library");if(pl)
this.zone=pl.get_zone(state.z);}else
this.zone=null;this.unit_id=state.ui;},id:0,name:"",descr:"",activation_time:0,unit_id:0,flags:0,zone:null,status:0,point_index:0,update_time:0,get_id:function(){return this.id;},get_name:function(){return this.name;},get_description:function(){return this.descr;},get_activation_time:function(){return this.activation_time;},get_unit:function(){if(this.unit_id)
return Wialon.get_item(this.unit_id);else
return null;},get_zone:function(){return this.zone;},get_status:function(){return this.status;},get_update_time:function(){return this.update_time;},get_point_index:function(){return this.point_index;},get_flags:function(){return this.flags;},is_enabled:function(){return!(this.status&0x80);}});var WialonUnitRouteConstructor=WialonUnitRoute.extend({constructor:function(){},set_name:function(name){this.name=name;},set_description:function(descr){this.descr=descr;},set_activation_time:function(time){this.activation_time=time;},set_unit:function(unit){this.unit_id=unit.get_id();},set_zone:function(zone){this.zone=zone;this.flags|=0x1;},set_flags:function(flags){this.flags=(flags&0xFFFFFFFE)|(this.flags&0x1);},set_status:function(status){this.status=status;}});Wialon.add_item_plugin("avl_resource","avl_route_control",WialonRoutesControlPlugin);var WialonUnitServiceIntervalPlugin=WialonItemPlugin.extend({service_intervals:{},service_interval_limit:-1,get_type:function(){return"avl_service_intervals";},init_state:function(state,item){this.base(state,item);if(typeof state.si!="undefined"){this.service_intervals={};for(var id in state.si)
this.service_intervals[id]=new WialonServiceInterval(state.si[id]);}
if(typeof state.simax!="undefined")
this.service_interval_limit=state.simax;if(typeof state.unfu!="undefined"&&state.unfu.length==2){var id=state.unfu[0];var udata=state.unfu[1];if(!udata){if(typeof this.service_intervals[id]!="undefined")
delete this.service_intervals[id];}else
this.service_intervals[id]=new WialonServiceInterval(udata);}},get_service_intervals:function(){var col=new Array;for(var id in this.service_intervals)
col.push(this.service_intervals[id]);return col;},get_service_interval:function(id){var n=this.service_intervals[id];if(typeof n!="undefined")
return n;return null;},get_service_intervals_limit:function(){return this.service_interval_limit;},create_service_interval:function(service_interval,callback,cparam){if(!service_interval||this.item.get_access_level()<Wialon.access_level("edit")||service_interval.get_name()=="")
return false;var item_plugin=this;return Wialon.exec_service({svc:"create_service_interval",item_id:this.item.get_id(),si_data:WebAjax.to_list(service_interval)},function(result,text){if(!result){callback(0,text,cparam);return;}
var service_interval_id=parseInt(text);callback(service_interval_id,"",cparam);});},update_service_interval:function(service_interval_id,service_interval,callback,cparam){if(!service_interval||this.item.get_access_level()<Wialon.access_level("edit")||service_interval.get_name()==""||typeof this.service_intervals[service_interval_id]=="undefined")
return false;var item_plugin=this;return Wialon.exec_service({svc:"update_service_interval",item_id:this.item.get_id(),si_data:WebAjax.to_list(service_interval),si_id:service_interval_id},function(result,text){if(!result){callback(false,text,cparam);return;}
callback(true,"",cparam);});},delete_service_interval:function(service_interval,callback,cparam){if(!service_interval||this.item.get_access_level()<Wialon.access_level("edit"))
return false;var item_plugin=this;return Wialon.exec_service({svc:"delete_service_interval",item_id:this.item.get_id(),si_id:service_interval.get_id()},function(result,text){if(!result){callback(false,text,cparam);return;}
if(typeof item_plugin.service_intervals[service_interval.id]!="undefined")
delete item_plugin.service_intervals[service_interval.id];callback(true,"",cparam);});}});var WialonServiceInterval=Base.extend({constructor:function(state){this.id=state.id;this.name=state.nm;this.text=state.txt;this.interval_mileage=state.im;this.interval_days=state.ids;this.interval_engine_hours=state.ie;this.prev_service_mileage=state.pm;this.prev_service_time=state.pd;this.prev_service_engine_hours=state.pe;this.services_count=state.sc;},name:"",text:"",id:0,interval_mileage:0,interval_days:0,interval_engine_hours:0,prev_service_mileage:0,prev_service_time:0,prev_service_engine_hours:0,services_count:0,get_id:function(){return this.id;},get_name:function(){return this.name;},get_text:function(){return this.text;},get_interval_mileage:function(){return this.interval_mileage;},get_interval_days:function(){return this.interval_days;},get_interval_engine_hours:function(){return this.interval_engine_hours;},get_prev_service_mileage:function(){return this.prev_service_mileage;},get_prev_service_time:function(){return this.prev_service_time;},get_prev_service_engine_hours:function(){return this.prev_service_engine_hours;},get_services_count:function(){return this.services_count;}});var WialonServiceIntervalConstructor=WialonServiceInterval.extend({constructor:function(){},set_name:function(name){this.name=name;},set_text:function(text){this.text=text;},set_interval_mileage:function(interval_mileage){this.interval_mileage=interval_mileage;},set_interval_days:function(interval_days){this.interval_days=interval_days;},set_interval_engine_hours:function(interval_engine_hours){this.interval_engine_hours=interval_engine_hours;},set_prev_service_mileage:function(prev_service_mileage){this.prev_service_mileage=prev_service_mileage;},set_prev_service_time:function(prev_service_time){this.prev_service_time=prev_service_time;},set_prev_service_engine_hours:function(prev_service_engine_hours){this.prev_service_engine_hours=prev_service_engine_hours;},set_services_count:function(services_count){this.services_count=services_count;}});Wialon.add_item_plugin("avl_unit","avl_service_intervals",WialonUnitServiceIntervalPlugin);var WialonSMSManagerPlugin=WialonItemPlugin.extend({phones:"",get_type:function(){return"avl_sms_manager";},init_state:function(state,item){this.base(state,item);if(typeof state.smsp!="undefined")
this.phones=state.smsp;},get_phones:function(){return this.phones;},set_phones:function(phones){if(this.item.get_access_level()<Wialon.access_level("edit"))
return false;if(this.get_phones()==phones)
return true;this.item.remove_modification("user_set_phones",null);var item=this;var old_value=item.phones;this.item.add_modification("user_set_phones",phones,function(){item.phones=old_value;});this.phones=phones;return true;}});Wialon.add_item_plugin("user","avl_sms_manager",WialonSMSManagerPlugin);var WialonMsgsManager={get_messages:function(type,ival1,ival2,flags,flags_mask,load_locations,callback,cparam){return Wialon.exec_service({svc:"get_msgs",item_id:this.id,type:type,ival1:ival1,ival2:ival2,flags:flags,flags_mask:flags_mask,load_locations:load_locations?1:0},function(result,text){if(!result||text==""){callback(false,null,cparam);return;}
var msgs;eval("msgs = "+text);callback(true,msgs,cparam);delete msgs;});}};var WialonAccessor=WialonItem.extend({constructor:function(state,plugins){this.plugins=plugins;this.init_state(state);},acl:{},is_admin_group:false,get_avaible_items:function(){if(this.is_admin_group)
return Wialon.get_items();var col=new Array;for(var id in this.acl){var item=Wialon.get_item(id);if(item)
col.push(item);}
return col;},set_access:function(item,access_level){if(item==null||typeof item!="object"||typeof access_level!="number"||this.get_access_level()<Wialon.access_level("manage")||Wialon.access_name(access_level)==""||item.get_access_level()<Wialon.access_level("manage"))
return false;if(this.get_id_access_level(item.get_id())==access_level)
return true;var item_id=item.get_id();this.remove_modification("item_set_access",function(data){if(data.item==item_id)
return true;return false;});var user=this;var old_level=this.get_id_access_level(item_id);this.add_modification("item_set_access",{level:access_level,item:item_id},function(){user.acl[item_id]=old_level;});this.acl[item_id]=access_level;return true;},get_id_access_level:function(id){if(this.is_admin_group)
return Wialon.access_level("manage");if(typeof this.acl[id]!='undefined')
return this.acl[id];return 0;},set_id_access_level:function(id,lvl){if(this.is_admin_group)
return;this.acl[id]=lvl;},init_state:function(state){this.base(state);if(typeof state.acl!="undefined"){if(typeof state.acl=="string"&&state.acl=="admin_group")
this.is_admin_group=true;else
this.acl=state.acl;}
return true;}});var WialonUserGroup=WialonAccessor.extend({constructor:function(state,plugins){this.plugins=plugins;this.init_state(state);},users:[],all_users_group:false,get_users:function(){if(this.all_users_group)
return Wialon.get_users();return Wialon.get_items_from_id_col(this.users);},check_user:function(user){if(this.all_users_group)
return true;return this.check_item_in_id_col(this.users,user.get_id());},init_state:function(state){this.base(state);if(typeof state.u!="undefined"){if(typeof state.u=="string"&&state.u=="all_users_group"){this.all_users_group=true;this.users=new Array;}else
this.users=state.u;}
return true;}});Wialon.extend({get_user_groups:function(){return this.get_items("users_group");}});Wialon.add_item_constructor("users_group",WialonUserGroup);var WialonResource=WialonItem.extend({constructor:function(state,plugins){this.plugins=plugins;this.init_state(state);},init_state:function(state){if(!state)
return;this.base(state);return true;},handle_event:function(evt){this.base(evt);if(evt.type=="msg_update"){var lmsg=this.last_msg;var pos=this.position;var msg=Wialon.construct_msg(evt.data);if(!msg)
return;Wialon.fire_event("msg_registered",this.get_id(),msg);}}});WialonResource=WialonResource.extend(WialonMsgsManager);Wialon.extend({get_resources:function(){return this.get_items("avl_resource");}});Wialon.add_item_constructor("avl_resource",WialonResource);var WialonUser=WialonAccessor.extend({constructor:function(state,plugins){this.plugins=plugins;this.init_state(state);},flags:0,triggers:[],is_enabled:function(){return!(this.flags&0x1)?true:false;},password_changeable:function(){return(this.flags&0x2)?false:true;},can_create_item:function(){return(this.flags&0x4)?true:false;},get_user_groups:function(){var col=Wialon.get_items("users_group");var rcol=new Array;for(var i=0;i<col.length;i++){if(col[i].check_user(this))
rcol.push(col[i]);}
return rcol;},set_flags:function(flags,mask){if(this.get_access_level()<Wialon.access_level("manage")||!mask||(mask&0xFFFF000))
return false;if((this.get_flags()&mask)==flags)
return true;this.remove_modification("user_set_flags",null);var item=this;var old_value=item.get_flags();this.add_modification("user_set_flags",{flags:flags,mask:mask},function(){item.flags=old_value;});var s=0;var check=1;for(var i=0;i<32;i++,check<<1){if(mask&check){if(flags&check)
s|=check;}else if(this.flags&check)
s|=check;}
this.flags=s;return true;},get_flags:function(){return this.flags;},set_password:function(old_password,new_password){if(this.get_access_level()<Wialon.access_level("edit"))
return false;this.remove_modification("user_set_password",null);this.add_modification("user_set_password",{old_passwd:old_password,new_passwd:new_password},function(){});return true;},reset_password:function(callback,cparam){if(this.get_access_level()<Wialon.access_level("manage"))
return false;return Wialon.exec_service({svc:"reset_user_password",user_id:this.id},function(result,text){callback(result,text,cparam);});},init_state:function(state){this.base(state);if(typeof state.fl!="undefined"){this.flags=state.fl;}
return true;},handle_event:function(evt){this.base(evt);if(evt.type=="msg_update"){var msg=Wialon.construct_msg(evt.data);if(!msg)
return;Wialon.fire_event("msg_registered",this.get_id(),msg);}},set_hosts_mask:function(mask){if(this.get_access_level()<Wialon.access_level("manage"))
return false;var user=this;var old_mask=this.mask;this.remove_modification("user_set_hosts_mask",null);this.add_modification("user_set_hosts_mask",{hosts_mask:mask},function(){});}});WialonUser=WialonUser.extend(WialonMsgsManager);Wialon.extend({get_users:function(){return this.get_items("user");},create_user:function(name,password,callback,cparam,creator){if(!this.can_create_item())
return false;var creator_id=0;if(creator&&typeof creator=="object")
creator_id=creator.get_id();if(name=="")
return false;return this.exec_service({svc:"create_user",name:name,password:password,creator_id:creator_id},function(result,text){if(!result){callback(null,text,cparam);return;}
var data;eval("data ="+text);var item=Wialon.construct_item(data);delete data;if(item){var user=creator;if(!user)
user=Wialon.get_current_user();Wialon.set_user_access_for_item(item,user,Wialon.access_level("manage"));callback(item,"",cparam);Wialon.on_item_created(item);}});}});Wialon.add_item_constructor("user",WialonUser);var WialonUnit=WialonItem.extend({constructor:function(state,plugins){this.plugins=plugins;this.init_state(state);},unique_id:0,hw_type:0,phone_number:"",password:"",commands:[],last_msg:null,position:null,inet:[],params:null,driver:"",calc_flags:0,mileage_counter:0,engine_hours_counter:0,get_unique_id:function(){return this.unique_id;},get_hardware_type:function(){var hw=Wialon.get_item(this.hw_type);if(hw)
return hw.get_name();return this.hw_type;},get_phone_number:function(){return this.phone_number;},get_password:function(){return this.password;},get_driver_code:function(){return this.driver;},get_commands:function(comm_type){if(typeof comm_type!="undefined"&&comm_type!=""){var cmds=this.commands[comm_type];if(typeof cmds!="undefined")
return cmds;return[];}
var checker={};var rcmds=new Array;for(var ctype in this.commands){var cmds=this.commands[ctype];for(var i=0;i<cmds.length;i++){var cmd=cmds[i];if(typeof checker[cmd]!="undefined")
continue;checker[cmd]=1;rcmds.push(cmd);}}
checker=null;return rcmds;},get_last_msg:function(){return this.last_msg;},get_position:function(){return this.position;},get_inet_connection:function(){return this.inet;},get_last_param:function(param_name){if(!this.params){this.params={};this.update_params();}
if(typeof this.params[param_name]!="undefined")
return this.params[param_name];else
return"N/A";},get_last_params:function(){if(!this.params){this.params={};this.update_params();}
var ret_params={};for(var param_name in this.params)
ret_params[param_name]=this.params[param_name];return ret_params;},get_calc_flags:function(){return this.calc_flags;},get_mileage_counter:function(){return this.mileage_counter;},get_engine_hours_counter:function(){return this.engine_hours_counter;},update_params:function(reg_msg){if(!this.params)
this.params={};var msgs=[];if(reg_msg&&typeof reg_msg!="undefined")
msgs.push(reg_msg);else{var pos=this.get_position();var data=this.get_last_msg();if((pos&&data)&&(pos.get_time()<data.get_time())){msgs.push(pos);msgs.push(data);}else{msgs.push(data);msgs.push(pos);}}
for(var m=0;m<msgs.length;m++){var msg=msgs[m];if(!msg)
continue;if(msg.get_flags()&0x2){this.params["in"]=msg.get_inputs();for(var i=0;i<32;i++)
this.params["in"+(i+1)]=(msg.get_inputs()&(1<<i))?1:0;}
if(msg.get_flags()&0x4){this.params["out"]=msg.get_outputs();for(var i=0;i<32;i++)
this.params["out"+(i+1)]=(msg.get_outputs()&(1<<i))?1:0;}
var msg_params=msg.get_params();for(var param_name in msg_params)
this.params[param_name]=msg_params[param_name];}},is_command_avaible:function(cmd_name,link_type){var cmds=this.get_commands(link_type);for(var i=0;i<cmds.length;i++){if(cmds[i]==cmd_name)
return true;}
return false;},exec_cmd:function(cmd_name,cmd_param,link_type,timeout,callback,cparam){if(!this.is_command_avaible(cmd_name,link_type)||this.get_access_level()<Wialon.access_level("exec_cmd"))
return false;return Wialon.exec_service({svc:"exec_unit_cmd",unit_id:this.id,cmd_name:cmd_name,cmd_param:cmd_param,link_type:link_type,timeout:timeout},function(result,text){callback(result,text,cparam);});},set_password:function(passwd){if(this.get_access_level()<Wialon.access_level("edit"))
return false;if(this.get_password()==passwd)
return true;this.remove_modification("unit_set_password",null);var item=this;var old_value=item.password;this.add_modification("unit_set_password",passwd,function(){item.password=old_value;});this.password=passwd;return true;},set_phone_number:function(phone){if(this.get_access_level()<Wialon.access_level("edit"))
return false;if(this.get_phone_number()==phone)
return true;this.remove_modification("unit_set_phone",null);var item=this;var old_value=item.phone_number;this.add_modification("unit_set_phone",phone,function(){item.phone_number=old_value;});this.phone_number=phone;return true;},set_calc_flags:function(calc_flags){if(this.get_access_level()<Wialon.access_level("edit"))
return false;if(this.get_calc_flags()==calc_flags)
return true;this.remove_modification("unit_set_calc_flags",null);var item=this;var old_value=item.calc_flags;this.add_modification("unit_set_calc_flags",calc_flags,function(){item.calc_flags=old_value;});this.calc_flags=calc_flags;return true;},set_mileage_counter:function(mileage_counter){if(this.get_access_level()<Wialon.access_level("edit"))
return false;if(this.get_mileage_counter()==mileage_counter)
return true;this.remove_modification("unit_set_mileage_counter",null);var item=this;var old_value=item.mileage_counter;this.add_modification("unit_set_mileage_counter",mileage_counter,function(){item.mileage_counter=mileage_counter;});this.mileage_counter=mileage_counter;return true;},set_engine_hours_counter:function(engine_hours_counter){if(this.get_access_level()<Wialon.access_level("edit"))
return false;if(this.get_engine_hours_counter()==engine_hours_counter)
return true;this.remove_modification("unit_set_engine_hours_counter",null);var item=this;var old_value=item.engine_hours_counter;this.add_modification("unit_set_engine_hours_counter",engine_hours_counter,function(){item.engine_hours_counter=engine_hours_counter;});this.engine_hours_counter=engine_hours_counter;return true;},set_bytes_counter:function(bytes_counter,reset){if(this.get_access_level()<Wialon.access_level("edit"))
return false;this.add_modification("unit_set_bytes_counter",WebAjax.to_list([bytes_counter,reset]),function(){});return true;},set_unique_id:function(unq_id){if(this.get_access_level()<Wialon.access_level("edit"))
return false;if(this.get_unique_id()==unq_id)
return true;this.remove_modification("unit_set_unique_id",null);var item=this;var old_value=item.unique_id;this.add_modification("unit_set_unique_id",unq_id,function(){item.unique_id=old_value;});this.unique_id=unq_id;return true;},set_hardware_type:function(type){if(this.get_access_level()<Wialon.access_level("edit"))
return false;if(this.hw_type==type)
return true;this.remove_modification("unit_set_hw_type",null);var item=this;var old_value=item.hw_type;this.add_modification("unit_set_hw_type",type,function(){item.hw_type=old_value;});this.hw_type=type;return true;},init_state:function(state){this.base(state);if(typeof state.uid!="undefined")
this.unique_id=state.uid;if(typeof state.hw!="undefined")
this.hw_type=state.hw;if(typeof state.ph!="undefined")
this.phone_number=state.ph;if(typeof state.psw!="undefined")
this.password=state.psw;if(typeof state.cmds!="undefined")
this.commands=state.cmds;if(typeof state.inet!="undefined")
this.inet=state.inet;if(typeof state.drv!="undefined")
this.driver=state.drv;if(typeof state.cfl!="undefined")
this.calc_flags=state.cfl;if(typeof state.cneh!="undefined")
this.engine_hours_counter=state.cneh;if(typeof state.cnm!="undefined")
this.mileage_counter=state.cnm;if(typeof state.pos!="undefined"){if(state.pos)
this.position=Wialon.construct_msg(state.pos);else
this.position=null;}
if(typeof state.lmsg!="undefined"){if(state.lmsg){if(state.lmsg=="dup")
this.last_msg=this.position;else
this.last_msg=Wialon.construct_msg(state.lmsg);}else
this.last_msg=null;}
return true;},handle_event:function(evt){this.base(evt);if(evt.type=="msg_update"){var lmsg=this.last_msg;var pos=this.position;var msg=Wialon.construct_msg(evt.data);if(!msg)
return;var pos_updated=false;var data_updated=false;if(msg.get_type()=="ud"&&(!this.last_msg||msg.get_time()>this.last_msg.get_time())){this.last_msg=msg;data_updated=true;this.update_params(msg);}else if(msg.get_type()=="udp"&&(!this.position||msg.get_time()>this.position.get_time())){this.position=msg;pos_updated=true;data_updated=true;if(!this.last_msg||msg.get_time()>this.last_msg.get_time())
this.last_msg=msg;this.update_params(msg);}
msg.pos_updated=pos_updated;msg.data_updated=data_updated;Wialon.fire_event("msg_registered",this.get_id(),msg);if(pos_updated){if(!this.pos_update_timeout||typeof this.pos_update_timeout=="undefined"){var unit_id=this.get_id();this.pos_update_timeout=setTimeout(function(){var unit=Wialon.get_item(unit_id);if(!unit)
return;unit.pos_update_timeout=null;Wialon.fire_event("unit_pos_updated",unit_id,unit.position);},1000);}}}}});WialonUnit=WialonUnit.extend(WialonMsgsManager);var WialonHWType=WialonItem.extend({constructor:function(state,plugins){this.plugins=plugins;this.init_state(state);},init_state:function(state){if(!state)
return;this.base(state);return true;}});Wialon.extend({get_units:function(){return this.get_items("avl_unit");},get_hw_types:function(){return this.get_items("avl_hw");},create_unit:function(name,hw_type,callback,cparam,creator){if(!Wialon.get_current_user().can_create_item())
return false;var creator_id=0;if(creator&&typeof creator=="object")
creator_id=creator.get_id();if(name=="")
return false;return this.exec_service({svc:"create_unit",name:name,hw_type:hw_type,creator_id:creator_id},function(result,text){if(!result){callback(null,text,cparam);return;}
var data;eval("data ="+text);var item=Wialon.construct_item(data);delete data;if(item){var user=creator;if(!user)
user=Wialon.get_current_user();Wialon.set_user_access_for_item(item,user,Wialon.access_level("manage"));callback(item,"",cparam);Wialon.on_item_created(item);}});}});Wialon.add_item_constructor("avl_hw",WialonHWType);Wialon.add_item_constructor("avl_unit",WialonUnit);var WialonWebObjects=Base.extend({constructor:null,interval_id:-1,interval_ok:30000,interval_err:10000,interval_curr:0,object_names:{},deleted_names:{},add_name:function(name){if(typeof this.object_names[name]!="undefined")
return;this.object_names[name]=1;if(typeof this.deleted_names[name]!="undefined")
delete this.deleted_names[name];if(WialonWebObjects.interval_id==-1){WialonWebObjects.interval_curr=WialonWebObjects.interval_ok;WialonWebObjects.interval_id=setInterval(function(){WialonWebObjects.update_names();},WialonWebObjects.interval_curr);}},delete_name:function(name){if(typeof this.object_names[name]=="undefined")
return;delete this.object_names[name];if(typeof this.deleted_names[name]!="undefined")
return;this.deleted_names[name]=1;},update_names:function(){var arr=[];for(name in this.object_names)
arr.push(name);var darr=[];for(name in this.deleted_names)
darr.push(name);if(!arr.length&&!darr.length)
return;if(darr.length)
this.deleted_names={};Wialon.exec_service({svc:"update_web_objects",names:arr.length?WebAjax.to_list(arr):"",dnames:darr.length?WebAjax.to_list(darr):""},function(result,text){var new_interval=0;if(!result){var obj_res=text.split(";");for(var i=0;i<obj_res.length;i++){if(obj_res[i]=="0")
WialonWebObjects.delete_name(arr[i]);}
if(WialonWebObjects.interval_curr!=WialonWebObjects.interval_err)
new_interval=WialonWebObjects.interval_err;}else if(WialonWebObjects.interval_curr!=WialonWebObjects.interval_ok)
new_interval=WialonWebObjects.interval_ok;if(new_interval){WialonWebObjects.interval_curr=new_interval;clearInterval(WialonWebObjects.interval_id);WialonWebObjects.interval_id=setInterval(function(){WialonWebObjects.update_names();},WialonWebObjects.interval_curr);}});}});var WialonImageRenderer=Base.extend({constructor:function(){this.tz_offset=get_local_timezone();this.layers=[];this.deleted_layers=[];},tz_offset:0,renderer_name:"",layers:[],deleted_layers:[],lupdated:false,version:1,add_layer:function(layer){if(!layer)
return false;layer.enabled=1;if(typeof this.layers[layer.name]!="undefined")
delete this.layers[layer.name];this.layers[layer.name]=layer;Wialon.fire_event("msgs_layer_added",layer,this);return true;},remove_layer:function(layer){if(!layer||typeof this.layers[layer.name]=="undefined")
return false;this.lupdated=true;this.deleted_layers.push(layer.name);Wialon.fire_event("msgs_layer_removed",layer,this);delete this.layers[layer.name];return true;},enable_layer:function(layer,enable){if(!layer||typeof this.layers[layer.name]=="undefined")
return false;if(layer.enabled!=(enable?1:0))
this.lupdated=true;layer.enabled=enable?1:0;if(layer.enabled)
Wialon.fire_event("msgs_layer_enabled",layer,this);else
Wialon.fire_event("msgs_layer_disabled",layer,this);return true;},is_layer_enabled:function(layer){if(!layer||typeof this.layers[layer.name]=="undefined")
return;return layer.enabled;},get_layer:function(layer_name){if(typeof this.layers[layer_name]=="undefined")
return null;return this.layers[layer_name];},get_layers:function(only_enabled){if(typeof only_enabled=="undefined")
only_enabled=false;var col=new Array;for(name in this.layers){if(!only_enabled||this.layers[name].enabled)
col.push(this.layers[name]);}
return col;},set_tz_offset:function(tz_offset){this.tz_offset=tz_offset;return true;},create_renderer:function(callback,cparam){if(this.renderer_name!="")
return false;var item=this;return Wialon.exec_service({svc:"create_image_renderer",tz_offset:this.tz_offset},function(result,text){if(!result||text==""){callback(false,null,cparam);return;}
var data;eval("data = "+text);item.renderer_name=data.name;item.bounds=data.bounds;delete data;WialonWebObjects.add_name(item.renderer_name);callback(true,item,cparam);});},update_renderer:function(callback,cparam){if(this.renderer_name=="")
return;if(!this.lupdated){this.version++;callback(true,cparam);return;}
var l1=[];var l2=[];for(name in this.layers){if(this.layers[name].enabled)
l1.push(name);else
l2.push(name);}
var item=this;return Wialon.exec_service({svc:"update_image_layers",enabled_layers:l1.join("|"),disabled_layers:l2.join("|"),deleted_layers:this.deleted_layers.join("|"),tz_offset:this.tz_offset,rname:this.renderer_name},function(result,text){if(!result){if(callback)
callback(false,cparam);return;}
item.lupdated=false;item.deleted_layers=[];item.version++;if(callback)
callback(true,cparam);});},attach_renderer:function(name){if(this.renderer_name!="")
return false;this.renderer_name=name;WialonWebObjects.add_name(this.renderer_name);},destroy_renderer:function(){var layers=this.get_layers();for(var i=0;i<layers.length;i++)
this.remove_layer(layers[i]);WialonWebObjects.delete_name(this.renderer_name);this.renderer_name="";this.update_renderer(null);},hit_test:function(lat,lon,radius,callback,cparam){if(this.renderer_name==""||!lat||!lon)
return false;var item=this;var url=WebAjax.get_jscript_base_url("wialon_sdk=1")+"/avl_hittest";return WebAjax.exec_service(url,{rname:this.renderer_name,lat:lat,lon:lon,radius:radius},function(result,text){if(!result||text==""){callback(false,null,cparam);return;}
var data;eval("data="+text);if(data.type==1){var result={type:1,lat:data.lat,lon:data.lon,header:data.header,text:data.text};}else if(data.type==2){var msg=Wialon.construct_msg(data.msg);if(!msg){callback(false,null,cparam);return;}
var result={type:2,unit:Wialon.get_item(data.unit_id),lat:msg.get_lat(),lon:msg.get_lon(),msg:msg,imsg:data.imsg};}else{callback(false,null,cparam);return;}
callback(true,result,cparam);});},hit_test_time:function(time,callback,cparam){if(this.renderer_name=="")
return false;var item=this;var url=WebAjax.get_jscript_base_url("wialon_sdk=1")+"/avl_hittest_time";return WebAjax.exec_service(url,{rname:this.renderer_name,time:time},function(result,text){if(!result||text==""){callback(false,null,cparam);return;}
var data;eval("data="+text);var msg=Wialon.construct_msg(data.msg);if(!msg){callback(false,null,cparam);return;}
var result={type:2,unit:Wialon.get_item(data.unit_id),lat:msg.get_lat(),lon:msg.get_lon(),msg:msg,imsg:data.imsg};callback(true,result,cparam);});}});var WialonImageLayer=Base.extend({name:"",enabled:1});var WialonUnitMessagesLayer=WialonImageLayer.extend({constructor:function(){this.units=[];this.bounds=[];},border_style:null,label_font_name:"",annot_font_name:"",non_valid_dash:"",units:[],curr_unit:null,curr_msg_style:null,name:"",renderer_name:"",add_unit:function(unit,type,ival1,ival2,trip_detect){this.finish_unit();this.curr_unit=new Object;this.curr_unit.id=unit.get_id();this.curr_unit.msgs_type=type;this.curr_unit.msgs_ival1=ival1;this.curr_unit.msgs_ival2=ival2;this.curr_unit.td=trip_detect;this.curr_unit.styles=new Array;},finish_unit:function(){if(!this.curr_unit)
return;this.finish_msg_style();this.units.push(this.curr_unit);this.curr_unit=null;},set_unit_tracks_config:function(width,color,dir_offset,dir_size,speed_config){if(!this.curr_unit)
return;this.curr_unit.track=new Object;this.curr_unit.track.width=width;this.curr_unit.track.color=sprintf("%08x",color);this.curr_unit.track.dir_offset=dir_offset;this.curr_unit.track.dir_size=dir_size;this.curr_unit.track.speed_conf=speed_config;},set_unit_split_config:function(split_interval,split_distance){if(!this.curr_unit)
return;this.curr_unit.split=new Object;this.curr_unit.split.split_interval=split_interval;this.curr_unit.split.split_distance=split_distance;},set_unit_label_config:function(label,font_size,spacing,color,halo_size,halo_color,min_scale,max_scale){if(!this.curr_unit)
return;this.curr_unit.label=new Object;this.curr_unit.label.text=label;this.curr_unit.label.font_size=font_size;this.curr_unit.label.spacing=spacing;this.curr_unit.label.color=sprintf("%08x",color);this.curr_unit.label.halo_size=halo_size;this.curr_unit.label.halo_color=sprintf("%08x",halo_color);this.curr_unit.label.min_scale=min_scale;this.curr_unit.label.max_scale=max_scale;this.curr_unit.label.text=label;},set_unit_road_lock:function(road_lock){if(!this.curr_unit)
return;this.curr_unit.road_lock=road_lock;return true;},add_msg_style:function(is_moving,radius,color,check_placement,min_scale,max_scale){if(!this.curr_unit)
return;this.finish_msg_style();this.curr_msg_style=new Object;this.curr_msg_style.is_moving=is_moving;this.curr_msg_style.radius=radius;this.curr_msg_style.color=sprintf("%08x",color);if(typeof check_placement!="undefined")
this.curr_msg_style.check_placement=check_placement?1:0;else
this.curr_msg_style.check_placement=1;if(typeof min_scale!="undefined")
this.curr_msg_style.min_scale=min_scale;else
this.curr_msg_style.min_scale=0;if(typeof max_scale!="undefined")
this.curr_msg_style.max_scale=max_scale;else
this.curr_msg_style.max_scale=0;this.curr_msg_style.flags=0;},set_msg_style_annot_config:function(annot_format,annot_font_size,annot_font_color,annot_bg_color,annot_border_color,annot_border_width,annot_min_scale,annot_max_scale){if(!this.curr_msg_style)
return;this.curr_msg_style.annot_format=annot_format;this.curr_msg_style.annot_font_size=annot_font_size;this.curr_msg_style.annot_font_color=sprintf("%08x",annot_font_color);this.curr_msg_style.annot_bg_color=sprintf("%08x",annot_bg_color);this.curr_msg_style.annot_border_color=sprintf("%08x",annot_border_color);this.curr_msg_style.annot_border_width=annot_border_width;if(typeof annot_min_scale!="undefined")
this.curr_msg_style.annot_min_scale=annot_min_scale;else
this.curr_msg_style.annot_min_scale=0;if(typeof annot_max_scale!="undefined")
this.curr_msg_style.annot_max_scale=annot_max_scale;else
this.curr_msg_style.annot_max_scale=0;this.curr_msg_style.flags|=1;},set_msg_style_index_config:function(index_font_size,index_font_color,index_halo_radius,index_halo_color){if(!this.curr_msg_style)
return;this.curr_msg_style.index_font_size=index_font_size;this.curr_msg_style.index_font_color=sprintf("%08x",index_font_color);this.curr_msg_style.index_halo_radius=index_halo_radius;this.curr_msg_style.index_halo_color=sprintf("%08x",index_halo_color);if(typeof index_halo_radius!="undefined")
this.curr_msg_style.index_halo_radius=index_halo_radius;else
this.curr_msg_style.index_halo_radius=0;if(typeof index_halo_color!="undefined")
this.curr_msg_style.index_halo_color=sprintf("%08x",index_halo_color);else
this.curr_msg_style.index_halo_color=0;this.curr_msg_style.flags|=2;},finish_msg_style:function(){if(!this.curr_msg_style||!this.curr_unit)
return;this.curr_unit.styles.push(this.curr_msg_style);this.curr_msg_style=null;},set_border_style:function(color,width){this.border_style=new Object;this.border_style.width=width;this.border_style.color=sprintf("%08x",color);return true;},set_label_font_name:function(font_name){this.label_font_name=font_name;return true;},set_annot_font_name:function(font_name){this.annot_font_name=font_name;return true;},set_non_valid_dash:function(dash){this.non_valid_dash=dash;return true;},create_layer:function(renderer,name,callback,cparam){if(this.name!=""||!renderer||renderer.renderer_name==""||name=="")
return false;this.finish_unit();var item=this;return Wialon.exec_service({svc:"create_msgs_layer",border_style:this.border_style?WebAjax.to_list(this.border_style):"",label_font_name:this.label_font_name,annot_font_name:this.annot_font_name,non_valid_dash:this.non_valid_dash,units:WebAjax.to_list(this.units),rname:renderer.renderer_name,lname:name,tz_offset:renderer.tz_offset},function(result,text){if(!result||text==""){callback(false,null,null,cparam);return;}
var data;eval("data = "+text);item.name=data.name;var units;if(typeof data.units!='undefined')
units=data.units;else
units=[];item.units=units;item.bounds=data.bounds;delete data;if(renderer.add_layer(item))
item.renderer_name=renderer.renderer_name;callback(true,item,renderer,cparam);});},attach_layer:function(renderer,layer_data){if(this.renderer_name!="")
return false;this.name=layer_data.name;var units;if(typeof layer_data.units!='undefined')
units=layer_data.units;else
units=[];this.units=units;this.bounds=layer_data.bounds;if(renderer.add_layer(this))
this.renderer_name=renderer.renderer_name;},get_unit_msgs:function(unit_id,index_from,index_to,callback,cparam){if(this.renderer_name==""||index_from>index_to)
return false;var item=this;return Wialon.exec_service({svc:"get_unit_msgs_from_layer",lname:this.name,rname:this.renderer_name,from:index_from,to:index_to,unit_id:unit_id},function(result,text){if(!result||text==""){callback(false,null,cparam);return;}
var msgs;eval("msgs = "+text);var col=new Array;for(var i=0;i<msgs.length;i++){var msg=Wialon.construct_msg(msgs[i]);if(msg)
col.push(msg);}
delete msgs;callback(true,col,cparam);});},hit_test:function(unit_id,lat,lon,calc_mileage,callback,cparam){if(this.renderer_name==""||!lat||!lon)
return false;var item=this;return Wialon.exec_service({svc:"hit_test_layer",lname:this.name,rname:this.renderer_name,lat:lat,lon:lon,unit_id:unit_id,calc_mileage:calc_mileage?1:0},function(result,text){if(!result||text==""){callback(false,null,cparam);return;}
var data;eval("data="+text);var msg=Wialon.construct_msg(data.msg);callback(true,{msg:msg,mileage:data.mileage,index:data.index},cparam);});},get_units_data:function(){return this.units;},get_bounds:function(){return this.bounds;}});