var PERIOD=900;    // period , must >= 900 ms, 900 ~ 5000 ms
var timeout_xml=20000;  // timeout, ms, 10000 ~ 30000 ms

var PERIOD_to_check=200;  // period to check xml 
var time_to_re_login=120;  //  second 
var time_to_re_login1=time_to_re_login;
var timeout1,timeout2;
var run_at_pc=0;
MAX_ELEMENT=32;
var bErr_cnt=0;
var server_error=0;
var scroll_msg;
var scroll_period;
PAGE_LEVEL=0;
var xml_path;
var To_logout=0;

// max Modbus number 1 ~ 1024 , 0: not used
var max_data_size = 1024;

var xml_moving_no=0;
var old_xml_moving_no=0;
var moving_no_cnt=0; 
var old_date_time;
var get_xml_path_cnt=0;

// for level.xml
var xmlDoc1 = new ActiveXObject("Microsoft.xmldom");

// for curve.xml
var xmlDoc2 = new ActiveXObject("Microsoft.xmldom");
curve_y = new Array(401);
var curve_min, curve_max;

//jerry, Nov 05, 2004
var m_size = 7;

m = new Array(m_size+1);
m_len = new Array(m_size+1);

// How many Modbus number 
var data_num;

// Controller type, '0':ISaGRAF , '1':Modbus server, '2':whmi
var controller_type;

data_type = new Array(max_data_size+1);

// Modbus data array
boolean_val = new Array(max_data_size+1);
word_val = new Array(max_data_size+1);
float_val = new Array(max_data_size+1);
string_val = new Array(max_data_size+1);

//jerry, Nov 05, 2004
timer_val = new Array(max_data_size+1);

// Dec.10,2004 by Chun
tab_1 = new Array(256);
tab_2 = new Array(256);

cr_buf = new Array(256);
var cr_1,cr_2;
var size_cr_buf;
cj_buf = new Array(10240);  // MAX_ELEMENT*320
var size_cj_buf;
var cj_1,cj_2;

function load_xml()
{
 if(To_logout==1) return;  // logout command is given
 if(parent.flag_get_xml==1) return;
 parent.flag_get_xml=1;
 get_xml_path();
 parent.xmlDoc.async = true; 
 parent.xmlDoc.load(xml_path); 
}

function get_data1()
{   
 var ii,jj;
 var boolean_num; 
 var word_num; 
 var float_num; 
 var string_num; 
 var timer_num;
 var len;
 var n1,n2,n3;
 var n4;
 var cookie=parent._cookie_v;
 var v1,v2,v3,w3,var1;
 var nnn1;

 n1 = parent.xmlDoc.documentElement;
 n2 = (n1.childNodes).nextNode;
 n3 = n2.childNodes;

 for(ii=0;ii<m_size;ii++)
 {
  m[ii+1] = n3.nextNode.text;
  m_len[ii+1] = m[ii+1].length;
 }

 // abort
 if(m[1].charCodeAt(0)==0x39) return;

 v1=cookie.substring(9,10);
 v2=cookie.substring(10,11);
 v3=cookie.substring(11,12);
 if(v1=="0" || v1=="1" ||
   v2=="0" || v2=="1" ||
   v3=="0" || v3=="1" ||
   v1==v2  || v1==v3 ||
   v2==v3 ) 
 {
  nnn1=v1.charCodeAt(0);
  if(nnn1<0x32) v1="2";
  nnn1=v1.charCodeAt(0)+1;
  if(nnn1>0x39) v2="2";
  else v2=String.fromCharCode(nnn1);
  nnn1=v2.charCodeAt(0)+1;
  if(nnn1>0x39) v3="2";
  else v3=String.fromCharCode(nnn1);
 }
 ii=3;
 if( (v2.charCodeAt(0)%2) ==1 )
 {
  var1="";
  for(jj=0;jj<m_len[ii];jj++)
  {
   w3=m[ii].substring(jj,jj+1);
   if(w3=="0") 
    w3="1";
   else if(w3=="1")
    w3="0";
   var1 += w3;
  }
  m[ii]=var1;
 }
 for(ii=4;ii<5;ii++)
 {
  var1="";
  for(jj=0;jj<m_len[ii];jj++)
  {
   w3=m[ii].substring(jj,jj+1);
   if(w3==v1) 
    w3=v3;
   else if(w3==v2)
    w3=v1;
   else if(w3==v3)
    w3=v2;
   var1 += w3;
  }
  m[ii]=var1;
 }  
  
 for(ii=m_size;ii<10;ii++)
 {
  n4=n3.nextNode;
 }
 n4=n4.childNodes;

 // '0':ISaGRAF , '1':Modbus server, '2': whmi
 controller_type = m[1].charAt(0);
  
 xml_moving_no=parseInt(m[1],10);
 if(xml_moving_no==old_xml_moving_no) { moving_no_cnt++; }
 old_xml_moving_no=xml_moving_no;

 // reset data_type to '0':None
 for(ii=0;ii<max_data_size;ii++)
 {
  data_type[ii+1]='0';
 }
  
 // get current data number
 data_num = m_len[2];
  
 if(data_num<=max_data_size+1)
 {
  // get current data_type
  for(ii=1;ii<=data_num;ii++)
  {
    data_type[ii]=m[2].charAt(ii-1);
  }
 }
 else
 {
  // too many data
  return;
 }
  
 boolean_num=0;
 word_num=0;
 float_num=0;
 string_num=0;
 timer_num=0;
  
 for(ii=1;ii<=data_num;ii++)
 {
  switch(data_type[ii])
  {
   case '0' :  // none
    break;

   case '1' :  // boolean
    if(boolean_num >= m_len[3])
    {
     // lack of boolean data
     break;
    }

    // get boolean
    boolean_val[ii]=parseInt(m[3].substring(boolean_num,boolean_num+1),10);
    boolean_num++;
    if(boolean_val[ii] != 0) 
    {
     boolean_val[ii]=1;
    }		
    break;

   case '2' :  // word
    if(word_num >= m_len[4])
    {
     // lack of word data
     break;
    }
    // get word
    word_val[ii]=parseInt(m[4].substring(word_num,word_num+4),16);
    word_num+=4;
    if(word_val[ii] == NaN) 
    {
     word_val[ii]=0;
    }
    else
    {
     // -32768 ~ +32767
     if(word_val[ii]>32767)
     {
      word_val[ii]= (0xFFFF0000 | word_val[ii]);
     }
    }
    break;
        
   case '3' :  // float
    if(float_num >= m_len[5])
    {
     // lack of float data
     break;
    }
    len=parseInt(m[5].substring(float_num,float_num+1),16);  // get len
    float_num++;
    if(len==NaN)
    {
     // len error
     break;
    }
    // get float
    float_val[ii]=parseFloat(m[5].substring(float_num,float_num+len));
    float_num+=len;
    if(float_val[ii] == NaN) 
    {
     float_val[ii]=0.0;
    }
    break;
        
   case '4' :  // string
    string_val[ii]=n4.nextNode.text
    break;

   case '5' :  // boolean & word
    break;
        
   case '6' :  // boolean & float
    break;
        
   case '7' :  // word & float
    if(word_num >= m_len[4])
    {
      // lack of word data
      break;
    }
    // get word
    word_val[ii]=parseInt(m[4].substring(word_num,word_num+4),16);
    word_num+=4;
    if(word_val[ii] == NaN) 
    {
      word_val[ii]=0;
    }
    else
    {
     // -32768 ~ +32767
     if(word_val[ii]>32767)
     {
      word_val[ii]= (0xFFFF0000 | word_val[ii]);
     }
    }     
       
    //get float
    if(float_num >= m_len[5])
    {
     // lack of float data
     break;
    }
    len=parseInt(m[5].substring(float_num,float_num+1),16);  // get len
    float_num++;
    if(len==NaN)
    {
     // len error
     break;
    }
    // get float
    float_val[ii]=parseFloat(m[5].substring(float_num,float_num+len));
    float_num+=len;
    if(float_val[ii] == NaN) 
    {
     float_val[ii]=0.0;
    }  
    break;
        
   case '8' :  // boolean & word & float
    break;
		
   case '9' :  // timer : Long Integer	
    if(timer_num >= m_len[7])
    {
     // lack of word data
     break;
    }
    // get 8 byte for timer 
    var LoVal=0, HiVal=0;
    LoVal=parseInt(m[7].substring(timer_num,timer_num+4),16);       
    HiVal=parseInt(m[7].substring(timer_num+4,timer_num+8),16);				
    timer_val[ii]=(LoVal&0x0000FFFF | (HiVal<<16));				
    timer_num+=8;	
		
    if(timer_val[ii] == NaN) 
    {
     timer_val[ii]=0;
    }		
     
    break;		
        
   default:   // wrong data_type
    break;
  }
 }
 refresh_data();   
 parent.frames.L_frame.refresh_data();
}

// get long val from word
function get_long_val(modbus_number)
{
 var val;
 if(controller_type=='2')
  val=timer_val[modbus_number];
 else
  val=(word_val[modbus_number]&0x0000FFFF) | (word_val[modbus_number+1]<<16);
 return val;
}

function search_path_()
{
 var re;
 var path_name_=location.pathname.substring(0,5);
 while(1)
 {
  re=/C:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/D:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/E:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/F:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/G:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/H:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/c:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/d:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/e:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/f:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/g:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  re=/h:\\/i;
  if(path_name_.search(re)!=-1) {run_at_pc=1; break;}
  break;
 }
}

function checkLogin()
{
 search_path_();
 if(run_at_pc!=1) 
 {
  lastVisit=parent._cookie_v;
  if(lastVisit==0 || lastVisit==null )
  {
   parent.location="login.htm";  
  }
 }
} 

function get_xml()
{
 if(bErr_cnt>6)
 {
  //timeout2=120000;
  timeout2=time_to_re_login*1000+10;
  bErr_cnt=0;
  server_error=1;
  parent.flag_get_xml=0;
 }
 if(timeout2>time_to_re_login*1000) // timeout too long, please login again
 {
  if(server_error==1)
   alert("Server error or Timeout, please close Internet Browser & open it again to re-login !");
  else
   alert("Communication break time exceed "+time_to_re_login1+" seconds, please close Internet Browser & open it again to re-login !");
  timeout2=0;
  time_to_re_login=10;  // reset to 10 second
 }
 else
 {
  if(parent.flag_get_xml==1)  // check timeout
  {
   if(timeout1>timeout_xml)
   {
    parent.COMM_state=0; // set as break
    refresh_data();   // function that user use to modify displayed data
    parent.frames.L_frame.refresh_data();
    if(parent.get_c_()==4)   // 4: successful or unsuccessful
    {
     timeout1=0;  
     parent.flag_get_xml=0;
     setTimeout("load_xml()", PERIOD); 
    }
   }
   else
   {
    if(parent.get_c_()==4)  // load COMPLETED
    {
     timeout1=0;  
     timeout2=0;
     parent.flag_get_xml=0;
     if(parent.get_b_()==0) // success
     {
      parent.COMM_state=1; // set as Ok
      bErr_cnt=0;
      get_data1();  
     }
     else
     {
      bErr_cnt++;
     }
     setTimeout("load_xml()", PERIOD); 
    }
    else
    {
     timeout1 += PERIOD_to_check;
    }
   }
  }
 }
 timeout2 += PERIOD_to_check;
 setTimeout("get_xml()", PERIOD_to_check); 
}

function GetUserID(obj)
{	
 var rightNow1 = new Date();  
 var str1,str2,ii;
 var jj,len,tmp;
 var cookie=parent._cookie_v;
 var OBJ=obj.elements;
 var find_CMD,find_UserID;

 if(OBJ.length>MAX_ELEMENT)
 {
  alert("The element number in one form should be under 32 !");
  return false;
 }
 if(parent.COMM_state==0)
 {
  alert("Communication is temporary break. Please try again later !");
  return false;
 }
 if(OBJ.length<2)
 {
  alert("The number of elements in one form should be at least 2.");
  return false;
 }
 find_CMD=0;
 find_UserID=0;
 for(ii=0;ii<OBJ.length;ii++)
 {
  if(find_CMD==0 && OBJ[ii].name=="END") 
  {
   find_CMD=1;
  }
  else if(find_UserID==0 && OBJ[ii].name=="BEGIN") 
  {
   find_UserID=1;
  }
  if(find_CMD==1 && find_UserID==1)
  {
   break;
  }
 }
 if(find_CMD==0 || find_UserID==0)
 {
  alert("Element name 'END' or 'BEGIN' not found !");
  return false;
 }
 if(run_at_pc==1) return false;
 old_date_time=rightNow1.getTime();
 str1 = "" + old_date_time;
 str2=str1;
 for(ii=0;ii<OBJ.length;ii++)
 {
  if(OBJ[ii].name=="BEGIN") {continue;}
  if(OBJ[ii].name=="END") {continue;}
  
  // by Chun on Oct.06,2010 for checkbox and radio in FORM
  // -----------------------------------------------------------------
  if( (OBJ[ii].type=="checkbox") || (OBJ[ii].type=="radio")  ) 
  {
    if(OBJ[ii].checked==false) {continue;}
  }
  // -----------------------------------------------------------------

  str2+=OBJ[ii].name;
  tmp=OBJ[ii].name.substring(0,1);
  if(tmp=="S" || tmp=="s")
  {
  }
  else
  {
   str2+=OBJ[ii].value;
  }
 }
 obj.BEGIN.value="" + str1 + cookie.substring(0,7)+"b"+set_2(str2);
 return true;
}

// Dec.10,2004
function A_11()
{
 var mas,bit,cr,mem;
 for(mas=0;mas<0x100;mas++)
 {
  cr=mas;
  for(bit=0;bit<8;bit++)
  {
   mem=cr & 0x1;
   cr/=2;
   if(mem!=0) cr^=parent.POLY;
  }
  tab_2[mas]=cr & 0xFF;
  tab_1[mas]=cr >>> 8;
 }
}

function A_22()
{
 var car,i;
 var c0=0xFF;
 var c1=0xFF;
 for(i=0;i<size_cr_buf;i++)
 {
  car=cr_buf[i];
  car ^= c0;
  c0=parent._get_px_1(c1,tab_2[car]);
  c1=parent._get_px_2(tab_1[car]);
 }
 cr_1=c0;
 cr_2=c1;
}
function A_33()
{
 var car,i;
 var c0=0xFF;
 var c1=0xFF;
 for(i=0;i<size_cj_buf;i++)
 {
  car=cj_buf[i];
  car ^= c0;
  c0=parent._get_px_1(c1,tab_2[car]);
  c1=parent._get_px_2(tab_1[car]);
 }
 cj_1=c0;
 cj_2=c1;
}
function set_1(T_val,mr)
{
 var n1=parent._code00;
 var n2=parent._code01;
 var n4=parent._code02;   
 var n5=parent._code03;
 var n6=""+parent._code04;   
 var n7=""+parent._code05;
 var n8=""+parent._code06;
 var n9=""+parent._code07;   
 var n10=""+parent._code08;
 var n11=""+parent._code09;
 var cookie=parent._cookie_v;
 var ii,jj,len,len1;
 var vip_1,vip_2;
 var ret,n0,n3;
 var o1,o2,o3,n4_,n5_,n6_,n7_,n8_;
 o1=T_val.charCodeAt(10)-0x30;
 o2=T_val.charCodeAt(11)-0x30;
 o3=T_val.charCodeAt(12)-0x30;
 len=n1.length;
 n0="";
 for(ii=0;ii<len-2;ii++)
 {
  if(o1==ii) 
  { 
   n0=n0+cookie.substring(7,8);
  }
  if(o2==ii) 
  { 
   n0=n0+cookie.substring(8,9);
  }
  if(o3==ii) 
  { 
   n0=n0+cookie.substring(9,10);
  }
  n0=n0+n1.substring(ii+2,ii+3);
 }  
 len=n2.length;
 n3="";
 for(ii=0;ii<len-4;ii++)
 {
  if(o1==ii) 
  { 
   n3=n3+cookie.substring(10,11);
  }
  if(o2==ii) 
  { 
   n3=n3+cookie.substring(11,12);
  }
  if(o3==ii) 
  { 
   n3=n3+cookie.substring(12,13);
  }
  n3=n3+n2.substring(ii+4,ii+5);
 }  
 n4_=n4;
 n5_=n5;
 if(mr==1)
 {
  n6_=n6.charCodeAt(0);
  n7_=n7.charCodeAt(0);
  n8_=n8.charCodeAt(0);
 }
 else
 {
  n6_=n9.charCodeAt(0);
  n7_=n10.charCodeAt(0);
  n8_=n11.charCodeAt(0);
 }
 n4_ = n4_ & 0xFFFFFF;
 n5_ = n5_ & 0xFFFFFF;
 for(ii=0;ii<9;ii++)
 {
  cr_buf[ii]=T_val.charCodeAt(ii);
 }
 cr_buf[9]=n4_>>>16;
 cr_buf[10]=(n4_ & 0xFF00) >>> 8;
 cr_buf[11]=n4_&0xFF;
 len=n0.length;
 if(len>0) 
 {
  cr_buf[12]=n0.charCodeAt(0);
 }
 cr_buf[13]=T_val.charCodeAt(9);
 cr_buf[14]=n6_;
 cr_buf[15]=n7_;
 cr_buf[16]=n8_;
 for(ii=0;ii<len-1;ii++)
 {
  cr_buf[17+ii]=n0.charCodeAt(1+ii);
 }
 cr_buf[len+16]=n5_>>>16;
 cr_buf[len+17]=(n5_ & 0xFF00) >>> 8;
 cr_buf[len+18]=n5_&0xFF;
 cr_buf[len+19]=T_val.charCodeAt(10);
 cr_buf[len+20]=T_val.charCodeAt(11);
 cr_buf[len+21]=T_val.charCodeAt(12);
 size_cr_buf=len+22;
 A_22();
 vip_1=(cr_1<<8) | cr_2;   
 for(ii=0;ii<10;ii++)
 {
  cr_buf[ii]=T_val.charCodeAt(ii);
 }
 cr_buf[10]=n4_>>>16;
 cr_buf[11]=(n4_ & 0xFF00) >>> 8;
 cr_buf[12]=n4_&0xFF;
 len=n3.length;
 if(len>0) 
 {
  cr_buf[13]=n3.charCodeAt(0);
 }
 cr_buf[14]=T_val.charCodeAt(10);
 cr_buf[15]=n6_;
 cr_buf[16]=n7_;
 cr_buf[17]=n8_;
 for(ii=0;ii<len-1;ii++)
 {
  cr_buf[18+ii]=n3.charCodeAt(1+ii);
 }
 cr_buf[len+17]=n5_>>>16;
 cr_buf[len+18]=(n5_ & 0xFF00) >>> 8;
 cr_buf[len+19]=n5_&0xFF;
 cr_buf[len+20]=T_val.charCodeAt(11);
 cr_buf[len+21]=T_val.charCodeAt(12);
 size_cr_buf=len+22;
 A_22();
 vip_2=(cr_1<<8) | cr_2;   
 ret="" + (((vip_1 & 0x7FFF) << 16) | vip_2);
 len1=ret.length;
 for(ii=0;ii<10-len1;ii++)
 {
  ret="0"+ret;
 }
 return(ret);
};

function set_2(s1)
{
 var n1=parent._code00;
 var n2=parent._code01;
 var n4=parent._code02;   
 var n5=parent._code03;
 var n6=""+parent._code04;   
 var n7=""+parent._code05;
 var n8=""+parent._code06;
 var n9=""+parent._code07;   
 var n10=""+parent._code08;
 var n11=""+parent._code09;
 var ii,jj,len,o1,o2;
 var ret,vip_1,vip_2;
 
 o1=n8.charCodeAt(0)-0x30;
 o2=n11.charCodeAt(0)-0x30;
 jj=0; 
 for(ii=0;ii<5;ii++)
 {
  if(ii==o1)
  {
   cj_buf[jj]=n1.charCodeAt(2);
   jj++;
  }
  cj_buf[jj]=s1.charCodeAt(ii);
  jj++;
 }
 cj_buf[jj]=n1.charCodeAt(2);
 jj++;
 cj_buf[jj]=n4>>>16;
 jj++;
 for(ii=5;ii<13;ii++)
 {
  if(ii==o1)
  {
   cj_buf[jj]=n1.charCodeAt(2);
   jj++;
  }
  cj_buf[jj]=s1.charCodeAt(ii);
  jj++;
 }
 cj_buf[jj]=n9.charCodeAt(0);
 jj++;
 cj_buf[jj]=n10.charCodeAt(0);
 jj++;
 cj_buf[jj]=n11.charCodeAt(0);
 jj++;
 len=s1.length;
 for(ii=13;ii<len;ii++)
 {
  cj_buf[jj]=s1.charCodeAt(ii);
  jj++;
 }
 len=n1.length;
 for(ii=3;ii<len;ii++)
 {
  cj_buf[jj]=n1.charCodeAt(ii);
  jj++;
 }
 size_cj_buf=jj;
 A_33();
 vip_1=(cj_1<<8) | cj_2;   
 jj=0; 
 for(ii=0;ii<6;ii++)
 {
  if(ii==o2)
  {
   cj_buf[jj]=n2.charCodeAt(4);
   jj++;
   }
   cj_buf[jj]=s1.charCodeAt(ii);
   jj++;
 }
 cj_buf[jj]=n2.charCodeAt(4);
 jj++;
 cj_buf[jj]=n5>>>16;
 jj++;
 for(ii=6;ii<13;ii++)
 {
  if(ii==o2)
  {
   cj_buf[jj]=n2.charCodeAt(4);
   jj++;
  }
  cj_buf[jj]=s1.charCodeAt(ii);
  jj++;
 }
 cj_buf[jj]=n6.charCodeAt(0);
 jj++;
 cj_buf[jj]=n7.charCodeAt(0);
 jj++;
 cj_buf[jj]=n8.charCodeAt(0);
 jj++;
 len=s1.length;
 for(ii=13;ii<len;ii++)
 {
  cj_buf[jj]=s1.charCodeAt(ii);
  jj++;
 }
 len=n2.length;
 for(ii=5;ii<len;ii++)
 {
  cj_buf[jj]=n2.charCodeAt(ii);
  jj++;
 }
 size_cj_buf=jj;
 A_33();
 vip_2=(cj_1<<8) | cj_2;   
  
 ret="" + (((vip_1 & 0x7FFF) << 16) | vip_2);
 len=ret.length;
 for(ii=0;ii<10-len;ii++)
 {
  ret="0"+ret;
 }
 return(ret);
}

function get_xml_path()
{	
 var rightNow1 = new Date();  
 var str1;
 var cookie=parent._cookie_v;

 if(moving_no_cnt>=1 || get_xml_path_cnt>1000)
 {
  moving_no_cnt=0;
  if(get_xml_path_cnt>50)
  {
   get_xml_path_cnt=0;
  }
  else
  {
   get_xml_path_cnt++;
  }
  old_date_time=rightNow1.getTime();
  str1 = "" + old_date_time;
 }
 else
 {
  str1 = "" + old_date_time;
  get_xml_path_cnt++;
 }
 xml_path="./" + str1 + cookie.substring(0,7)+ "b" +set_1(str1,1) + "wincon.xml";
}

function init()
{
 var rightNow1 = new Date();  
  
 if(run_at_pc!=1) 
 {
  goto_L_page("menu.htm");
  A_11();
  old_date_time=rightNow1.getTime();
  get_xml_path_cnt=151;
  moving_no_cnt=0;
  init_1();
 }  
}

function init_1()
{
 if(get_level()==1)
 {
 }
 else
 {
  history.back();
  return;
 }
 server_error=0;
 bErr_cnt=0;
 timeout1=0;
 timeout2=0;
 load_xml();
 get_xml();
}

function logout(obj)
{	
 var rightNow1;  
 var str1;
 var cookie=parent._cookie_v;

 flag = confirm("Are you sure to logout?");	
 if(flag)
 {	
  if(run_at_pc==1)
  {
   window.close();
   return false;
  }
  rightNow1 = new Date();  
  str1 = "" + rightNow1.getTime();
  obj.CMD.value="" + str1 + cookie.substring(0,7)+ "b" +set_1(str1,2);
  document.cookie="lastVisit=0";	
  To_logout=1;
  parent.frames.L_frame.location="_leftxx.htm";
  return true;	
 }
 else 
  return false;	
}

checkLogin();

function get_page_path(name)
{
 var str1;
 str1=parent._cookie_v;
 str1=str1.substring(0,7);
 return("abcdefghijklmnopqrst1357924680abcdefghijklmnopqrst1357924680abcdefghijklmnopqrst1357924680abcdefghijklmnopqrst0123456789" + str1 + "wxyz" + name);
}

function goto_R_page(name)
{	
 parent.frames.R_frame.location=get_page_path(name);
}

function goto_L_page(name)
{	
 parent.frames.L_frame.location=get_page_path(name);
}

function get_level()
{
 var str1;

 str1=parent._cookie_v;
 str1=str1.substring(0,7);
 str1="./DEF" + str1 + "level.xml";
 var ii,A1,A2,A3,A4,A5,A6,kk,R1,len;
  
 if( (parent._u_level>=1) && (parent._u_level<=3) && (PAGE_LEVEL>0) )
 {
  return 1;
 }
  
 xmlDoc1.async = false; 
 xmlDoc1.load(str1); 
  
 if(xmlDoc1.parseError!=0)
 {
  return 0;
 }
  
 A1 = xmlDoc1.documentElement;
 A2 = (A1.childNodes).nextNode;
 A3 = A2.childNodes;
 A4 = parseInt(A3.nextNode.text,10);
 A5 = parseInt(A3.nextNode.text,10);
 A6 = parseInt(A3.nextNode.text,10);
 len=parent._code01.length;
 R1="";
 for(ii=0;ii<len;ii++)
 {
  if(ii==A5+4)
  {
   kk=parent._code01.charCodeAt(ii)^A6;
   if(kk==0) kk=0x36;
   R1+=String.fromCharCode(kk);
  }
  else
  {
   R1+=parent._code01.substring(ii,ii+1);
  }
 }
 parent._code01=R1;
 if(A4>=1 && A4<=3 && PAGE_LEVEL>0 )
 {
  if(A4>=PAGE_LEVEL)
  {
   parent._u_level=A4;
   return 1;
  }
 }
 return 0;
}

// for trend curve
// pixel_img: for ex:  "red_pixel.bmp"
function draw_curve(img, win2)
{
 var ii,jj,kk,mm;
 var x0=70, y0=330, x1, y1;
 var old_curve_y, gap_y;
 var x_num=400;
 var dir;
 var img1;
 var tmp,p50,p10,p3,p2,p1;
 var s1 = ' style="position:absolute;left:';
 var s2 = '<img src=';
 var s3 = 'px;top:'

 old_curve_y=999;
 for(ii=0;ii<=x_num;ii++)
 {
  if(curve_y[ii] != 999)
  {
   x1=x0+ii;
   y1=y0-curve_y[ii]; 
   if( (old_curve_y == 999) || (old_curve_y == curve_y[ii]) )
   {
    gap_y=1;
   }
   else if(curve_y[ii]>old_curve_y)
   {
    gap_y=curve_y[ii]-old_curve_y;
   }
   else
   {
    gap_y=old_curve_y-curve_y[ii];
    y1=y0-old_curve_y+1;
   }
   tmp=gap_y%50;
   p50=(gap_y-tmp)/50;
   gap_y=tmp;
   tmp=tmp%10;
   p10=(gap_y-tmp)/10;
   gap_y=tmp;
   tmp=tmp%3;
   p3=(gap_y-tmp)/3;
   gap_y=tmp;
   tmp=tmp%2;
   p2=(gap_y-tmp)/2;
   p1=tmp;
      
   img1=img+"1y.bmp";
   for(kk=0;kk<p1;kk++)
   {
    win2.document.write(s2 + img1 + s1 + x1 + s3 + y1 + 'px;" >');
    y1++;
   }
   img1=img+"2y.bmp";
   for(kk=0;kk<p2;kk++)
   {
    win2.document.write(s2 + img1 + s1 + x1 + s3 + y1 + 'px;" >');
    y1+=2;
   }
   img1=img+"3y.bmp";
   for(kk=0;kk<p3;kk++)
   {
    win2.document.write(s2 + img1 + s1 + x1 + s3 + y1 + 'px;" >');
    y1+=3;
   }
   img1=img+"10y.bmp";
   for(kk=0;kk<p10;kk++)
   {
    win2.document.write(s2 + img1 + s1 + x1 + s3 + y1 + 'px;" >');
    y1+=10;
   }
   img1=img+"50y.bmp";
   for(kk=0;kk<p50;kk++)
   {
    win2.document.write(s2 + img1 + s1 + x1 + s3 + y1 + 'px;" >');
    y1+=50;
   }
  }
  old_curve_y=curve_y[ii];
 }
}

// for trend curve 1
function get_trend_curve_1(caption,unit,ID1,color1,msg1,
	start_year,start_month,start_day,start_hour,start_minute,start_second,
	end_year,  end_month,  end_day,  end_hour,  end_minute,  end_second)
{
  get_trend_(caption,unit,ID1,color1,msg1,-1,"R","",-1,"R","",
	start_year,start_month,start_day,start_hour,start_minute,start_second,
	end_year,  end_month,  end_day,  end_hour,  end_minute,  end_second);
}

// for trend curve 2
function get_trend_curve_2(caption,unit,ID1,color1,msg1,ID2,color2,msg2,
	start_year,start_month,start_day,start_hour,start_minute,start_second,
	end_year,  end_month,  end_day,  end_hour,  end_minute,  end_second)
{
  get_trend_(caption,unit,ID1,color1,msg1,ID2,color2,msg2,-1,"R","",
	start_year,start_month,start_day,start_hour,start_minute,start_second,
	end_year,  end_month,  end_day,  end_hour,  end_minute,  end_second);
}

// for trend curve 3
function get_trend_curve_3(caption,unit,ID1,color1,msg1,ID2,color2,msg2,ID3,color3,msg3,
	start_year,start_month,start_day,start_hour,start_minute,start_second,
	end_year,  end_month,  end_day,  end_hour,  end_minute,  end_second)
{
  get_trend_(caption,unit,ID1,color1,msg1,ID2,color2,msg2,ID3,color3,msg3,
	start_year,start_month,start_day,start_hour,start_minute,start_second,
	end_year,  end_month,  end_day,  end_hour,  end_minute,  end_second);
}

// for trend curve
// caption: Caption of this Trend curve
// ID1 to ID3:  curve ID: 0 to 199, set to -1 means no curve
// color1 to color3: accepts only "r", "R", "b", "B", "g", "G"
// msg 1 to msg3: a description of the curve, max 10 English letters or 5 letters for other language.
function get_trend_(caption,unit,ID1,color1,msg1,ID2,color2,msg2,ID3,color3,msg3,
	start_year,start_month,start_day,start_hour,start_minute,start_second,
	end_year,  end_month,  end_day,  end_hour,  end_minute,  end_second)
{
 var str1,str2,str3;
 var n1,n2,n3;
 A = new Array(10);
 A_len = new Array(10);
 temp = new Array(6);
 var ii,jj,kk,mm,LL;
 var s1 = ' style="position:absolute;left:';
 var s2 = '<img src=';
 var s3 = 'px;top:'
 var curve_val,curve_scale_val;
 curve_date = new Array(3);
 date_ms = new Array(4);
 var date_gap;
 ID_ = new Array(4);
 color_ = new Array(4);
 msg_ = new Array(4);
 pos_x = new Array(4);
 pos_y = new Array(4);
  
 var scan_period, backup_period, max_record; 

 pos_x[1]=80; pos_x[2]=220; pos_x[3]=360;    
 pos_y[1]=370; pos_y[2]=370; pos_y[3]=370;    
  
 ID_[1]=ID1; ID_[2]=ID2; ID_[3]=ID3; 
 color_[1]=color1; color_[2]=color2; color_[3]=color3; 
 msg_[1]=msg1; msg_[2]=msg2; msg_[3]=msg3; 

 for(ii=1;ii<4;ii++)
 {
  if( (ID_[ii]<0 && ID_[ii]!=-1) || ID_[ii]>199)
  {
   alert("Trend Curve ID should be in the range of [0,199] !");
   return;
  }
  if( color_[ii]!="R" && color_[ii]!="R" && 
	color_[ii]!="B" && color_[ii]!="b" && 
	color_[ii]!="G" && color_[ii]!="g" ) 
  {
   alert('color error! Should be one of "R" , "r" , "B" , "b" , "G" , "g" ');
   return;
  }
 }

 if( (start_year<2000 && start_year != 0) || (start_year>2200) || (end_year<2000) || (end_year>2200) ||
     (start_month<=0) || (start_month>12) || (end_month<=0) || (end_month>12) ||
     (start_day<=0) || (start_day>31) || (end_day<=0) || (end_day>31) ||
     (start_hour<0) || (start_hour>23) || (end_hour<0) || (end_hour>23) ||
     (start_minute<0) || (start_minute>59) || (end_minute<0) || (end_minute>59) ||
     (start_second<0) || (start_second>59) || (end_second<0) || (end_second>59) )
 {
  alert("Starting or Ending time is not correct !");
  return;
 }

 dat1=new Date(start_year,start_month,start_day,start_hour,start_minute,start_second);
 dat2=new Date(end_year,end_month,end_day,end_hour,end_minute,end_second);
 if(dat1.getTime() >= dat2.getTime())
 {
  alert("Starting or Ending time is not correct !");
  return;
 }
 date_gap= (dat2.getTime()-dat1.getTime())/5;
 n1=dat1.getTime();
 for(ii=0;ii<4;ii++)
 {
  date_ms[ii]=new Date(n1+(ii+1)*date_gap);
 }

 str1="";
 if(start_year==0) str1 = str1 + ",0000";
 else str1 = str1 + "," + start_year;
 if(start_month<10) str1 = str1 + ",0" + start_month;
 else str1 = str1 + "," + start_month;
 if(start_day<10) str1 = str1 + ",0" + start_day;
 else str1 = str1 + "," + start_day;
 if(start_hour<10) str1 = str1 + ",0" + start_hour;
 else str1 = str1 + "," + start_hour;
 if(start_minute<10) str1 = str1 + ",0" + start_minute;
 else str1 = str1 + "," + start_minute;
 if(start_second<10) str1 = str1 + ",0" + start_second;
 else str1 = str1 + "," + start_second;
  
 str1 = str1 + "," + end_year;
 if(end_month<10) str1 = str1 + ",0" + end_month;
 else str1 = str1 + "," + end_month;
 if(end_day<10) str1 = str1 + ",0" + end_day;
 else str1 = str1 + "," + end_day;
 if(end_hour<10) str1 = str1 + ",0" + end_hour;
 else str1 = str1 + "," + end_hour;
 if(end_minute<10) str1 = str1 + ",0" + end_minute;
 else str1 = str1 + "," + end_minute;
 if(end_second<10) str1 = str1 + ",0" + end_second;
 else str1 = str1 + "," + end_second;
  
 str2=parent._cookie_v;
 str2=str2.substring(0,7);
  
 str1 = str1+"DEF"+str2+"curve.xml";

for(LL=1;LL<4;LL++)
{
 if(ID_[LL] == -1) continue;
  
 str3="./ABC";
 if(ID_[LL]>=100) str3 = str3 + ID_[LL];
 else if(ID_[LL]>=10) str3 = str3 + "0" + ID_[LL];
 else str3 = str3 + "00" + ID_[LL];
  
 str3=str3+str1;

 xmlDoc2.async = false; 
 xmlDoc2.load(str3); 

 if(xmlDoc2.parseError!=0)
 {
  return;
 }

 n1 = xmlDoc2.documentElement;
 n2 = (n1.childNodes).nextNode;
 n3 = n2.childNodes;

 for(ii=0;ii<9;ii++)
 {
  A[ii+1] = n3.nextNode.text;
  A_len[ii+1] = A[ii+1].length;
 }

 // abort
 if(A[1]=="RRR")
 {
  alert("No such a trend curve (ID="+ID_[LL] +") !");
  return;
 }

 if(A_len[1]==1203)
 {
  for(ii=0;ii<401;ii++)
  {
   kk=3*ii;
   curve_y[ii]=parseInt(A[1].substring(kk,kk+3),16);
   if(curve_y[ii] == NaN) 
   {
    curve_y[ii]=999;
   }
  }
 }
 else
 {
  alert("trend curve data error at A_len[1] !");
  return;
 }
  
 curve_max=parseFloat(""+A[2]);
 if(curve_max == NaN) 
 {
  alert("trend curve data error at curve_max !");
  return;
 }

 curve_min=parseFloat(""+A[3]);
 if(curve_min == NaN) 
 {
  alert("trend curve data error at curve_min !");
  return;
 }
  
 for(jj=4;jj<7;jj++)
 {
  if(A_len[jj]==18)
  {
   for(ii=0;ii<6;ii++)
   {
    kk=3*ii;
    temp[ii]=parseInt(A[jj].substring(kk,kk+3),16);
    if(temp[ii]== NaN) 
    {
     alert("trend curve data error 0 at A["+jj+"] !");
     return;
    }
   }
   curve_date[jj-4]=new Date(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5]);
  }
  else
  {
   alert("trend curve data error 1 at A["+jj+"] !");
   return;
  }  
 }

 scan_period=parseInt(A[7],10);
 backup_period=parseInt(A[8],10);
 max_record=parseInt(A[9],10);

if(LL==1)
{
 var win2=window.open("", "", "width=525, height=405");
 win2.document.write('<html><head><title>Trend Curve</title></head><body>');
 win2.document.write('<font size="2">');
 win2.document.write('<b '+s1+200+s3+5+'px;" >'+caption+'</b>');
 win2.document.write('<b '+s1+50+s3+5+'px;" >'+unit+'</b>');
 win2.document.write('</font>');
 win2.document.write('<font size="1">');
 win2.document.write('<b '+s1+30+s3+335+'px;" >'+to_date_str(dat1,1)+'</b>');
 win2.document.write('<b '+s1+50+s3+350+'px;" >'+to_time_str(dat1)+'</b>');
 win2.document.write('<b '+s1+430+s3+335+'px;" >'+to_date_str(dat2,1)+'</b>');
 win2.document.write('<b '+s1+450+s3+350+'px;" >'+to_time_str(dat2)+'</b>');

 win2.document.write('<b '+s1+0+s3+390+'px;" >'+'Since '+to_date_str(curve_date[2],1)+' '+to_time_str(curve_date[2])+'</b>');
 win2.document.write('<b '+s1+170+s3+390+'px;" >Sampling: '+scan_period/10+'sec</b>');
 win2.document.write('<b '+s1+290+s3+390+'px;" >Backup: '+backup_period+'min</b>');
 win2.document.write('<b '+s1+395+s3+390+'px;" >Max '+max_record+' sample</b>');
 
 win2.document.write('<b '+s1+5+s3+20+'px;" >'+float_str1(curve_max,2)+'</b>');
 win2.document.write('<b '+s1+5+s3+320+'px;" >'+float_str1(curve_min,2)+'</b>');
 curve_scale_val=(curve_max-curve_min)/10.0;
 curve_val=curve_max;
 for(ii=0;ii<9;ii++)
 {
  jj=25+30*(ii+1);
  curve_val -= curve_scale_val;
  win2.document.write('<b '+s1+5+s3+jj+'px;" >'+float_str1(curve_val,2)+'</b>');
 }
 win2.document.write('</font>');

 jj=30;
 for(ii=0;ii<9;ii++)
 {
  jj += 30; 
  win2.document.write(s2 + "img/y_scale.jpg" + s1 + 70 + s3 + jj + 'px;" >');
 }
 kk=70;
 win2.document.write('<font size="1">');
 for(ii=0;ii<4;ii++)
 {
  kk += 40; 
  win2.document.write(s2 + "img/x_scale.jpg" + s1 + kk + s3 + 30 + 'px;" >');
  kk += 40; 
  win2.document.write(s2 + "img/x_scale.jpg" + s1 + kk + s3 + 30 + 'px;" >');
  jj=50+80*(ii+1); 
  win2.document.write('<b '+s1+jj+s3+335+'px;" >'+to_date_str(date_ms[ii],0)+'</b>');
  win2.document.write('<b '+s1+jj+s3+350+'px;" >'+to_time_str(date_ms[ii])+'</b>');
 }
 kk += 40; 
 win2.document.write(s2 + "img/x_scale.jpg" + s1 + kk + s3 + 30 + 'px;" >');
 win2.document.write('</font>');
  
 win2.document.write(s2 + "img/h_line.jpg" + s1 + 70 + s3 + 30 + 'px;" >');
 win2.document.write(s2 + "img/h_line.jpg" + s1 + 70 + s3 + 330 + 'px;" >');
 win2.document.write(s2 + "img/v_line.jpg" + s1 + 70 + s3 + 30 + 'px;" >');
 win2.document.write(s2 + "img/v_line.jpg" + s1 + 470 + s3 + 30 + 'px;" >');
 
}

 if(color_[LL]=="R" || color_[LL]=="r")
 {
  draw_curve("img/red",win2);
 }
 else if(color_[LL]=="B" || color_[LL]=="b")
 {
  draw_curve("img/blue",win2);
 }
 else if(color_[LL]=="G" || color_[LL]=="g")
 {
  draw_curve("img/green",win2);
 }
}

win2.document.write('<font size="1">');
for(LL=1;LL<4;LL++)
{
 if(ID_[LL] == -1) continue;
 jj=pos_x[LL]+15;
 win2.document.write('<b '+s1+jj+s3+pos_y[LL]+'px;" >'+msg_[LL]+'</b>');
 ii=pos_y[LL]+7;
 if(color_[LL]=="R" || color_[LL]=="r")
 {
  win2.document.write(s2 + "img/red10x.bmp"+s1+pos_x[LL]+s3+ii+'px;" >');
 }
 else if(color_[LL]=="B" || color_[LL]=="b")
 {
  win2.document.write(s2 + "img/blue10x.bmp"+s1+pos_x[LL]+s3+ii+'px;" >');
 }
 else if(color_[LL]=="G" || color_[LL]=="g")
 {
  win2.document.write(s2 + "img/green10x.bmp"+s1+pos_x[LL]+s3+ii+'px;" >');
 }
}
win2.document.write('</font>');
win2.document.write('</body></html>');
  
}

// flag: 1: with year,  0: No year
function to_date_str(dat,flag)
{
 var str1;
  
 switch(dat.getMonth())
 {
  case 1: str1="Jan/"; break;
  case 2: str1="Feb/"; break;
  case 3: str1="Mar/"; break;
  case 4: str1="Apr/"; break;
  case 5: str1="May/"; break;
  case 6: str1="Jun/"; break;
  case 7: str1="Jul/"; break;
  case 8: str1="Aug/"; break;
  case 9: str1="Sep/"; break;
  case 10: str1="Oct/"; break;
  case 11: str1="Nov/"; break;
  case 12: str1="Dec/"; break;
 }
 if(flag)
 {
  str1 = str1+ dat.getDate() + "/" + dat.getYear();
 }
 else
 {
  str1 = str1+ dat.getDate();
 }
 return str1;
}

function to_time_str(dat)
{
 var str1;
 var h;
  
 str1="";
 h = dat.getHours();
 if(h<10) str1=str1+"0";
 str1=str1+h+":";
 h = dat.getMinutes();
 if(h<10) str1=str1+"0";
 str1=str1+h+":";
 h = dat.getSeconds();
 if(h<10) str1=str1+"0";
 str1=str1+h;
 return str1;
}

// period is from 100 ms to 2000 ms
function show_scroll_word(period,msg)
{
 scroll_msg=msg;
 scroll_period=period;
 if(scroll_period<100) scroll_period=100;
 if(scroll_period>2000) scroll_period=2000;
 window.setTimeout("scroll_at_buttom(100)",300);
}

var COMM_step_=0;
function scroll_at_buttom(pt)
{
 var mv=" ";
 var ii=1;
 var act;

 COMM_msg1="                              Communication is temporary break now !";
 COMM_msg0="";

 if(run_at_pc!=1)
 {
  if(parent.COMM_state == 0)
  {
   if(COMM_step_)
   {
    window.status=COMM_msg1;
    COMM_step_=0;
   }
   else
   {
    window.status=COMM_msg0;
    COMM_step_=1;
   }
   act="scroll_at_buttom("+pt+")";
   window.setTimeout(act,scroll_period);
   return;
  }
 }
  
 if(pt > 200) 
 {
  pt--;
  act="scroll_at_buttom("+pt+")";
  window.setTimeout(act,scroll_period);
 }
 else if(pt <= 200 && pt > 0)
 {
  for(ii=0;ii<pt;ii++) 
  {
   mv+=" ";
  }
  pt--;
  mv+=scroll_msg;
  window.status=mv;
  act="scroll_at_buttom("+pt+")";
  window.setTimeout(act,scroll_period);
 }  
 else if(pt <= 0) 
 {
  if(-pt < scroll_msg.length) 
  {
   mv+=scroll_msg.substring(-pt,scroll_msg.length);
   pt--;
   window.status=mv;
   act="scroll_at_buttom("+pt+")";
   window.setTimeout(act,scroll_period);
  }
  else
  {
   window.status=" ";
   act="scroll_at_buttom(100)";
   window.setTimeout(act,scroll_period);
  }
 }
}

// convert float value to string with fixed digit number under dot "." symbol
// float_val: a float value, for ex, 1.234567
// dot_num: 0 ~ 6
// for ex,  1.234567 return 1.234 if dot_num=3
//                   return 1.23  if dot_num=2
function float_str1(float_val,dot_num)
{
 var ii,jj,pos;
 var str,str1;

 if( (float_val > 0) && (float_val < 0.000001))  float_val=0;
 if( (float_val < 0) && (float_val > -0.000001))  float_val=0;
 if( (float_val >= 1000000) || (float_val <= -1000000) ) return(""+float_val);
  
 if(dot_num<0) dot_num=0;
 else if(dot_num>6) dot_num=6;
  
 // search "."
 str=""+float_val;
 jj=str.length;
 pos=-1;
 for(ii=0;ii<jj;ii++)
 {
  if(str.substring(ii,ii+1)==".")
  {
   pos=ii;
   break;
  }
 }
    
 if(pos!=-1)  // "." found
 {
  if(pos<str.length-dot_num)
  {
   str1=str.substring(0,pos+1+dot_num);
  }
  else
  {
   jj=pos+1+dot_num-str.length;
   str1=str;
   for(ii=0;ii<jj;ii++)
   {
    str1 += "0";
   }
  }
 }
 else  // "." not found
 {
  str1=float_val+".";
  for(ii=0;ii<dot_num;ii++)
  {
    str1 += "0";
  }
 }
 return(str1);
}

