#include <WindowsAPI.js>

now_dt=new Date();
now_dt_s=""+now_dt.getFullYear();
now_dt_s+="_"+(1+now_dt.getMonth());
now_dt_s+="_"+now_dt.getDate();
now_dt_s+="_"+now_dt.getHours();
now_dt=null;

ziped_file_short_name="Data_"+now_dt_s+".zip";
ziped_file_name=System.GetAppPath()+ziped_file_short_name;

function ZipData()
{
  bjShell=new ActiveXObject("Wscript.Shell");

  DeleteFile(ziped_file_name);

  zip_ext="\""+System.GetAppPath()+"7-Zip\\7z.exe\"";
  zip_action=" a -tzip";
  zip_option_r=" -r";
  data_path=System.GetAppPath()+"Data\\*.*";
  
  cmd_line=zip_ext+zip_action+" \""+ziped_file_name+"\" \""+data_path+"\""+zip_option_r;

  bjShell.Run(cmd_line,0,true);
  
  if(GetFileAttributes(ziped_file_name)>0)
    return true;

  return false;
}

var form;
function OnSendButtonClick(Sender)
{
  if(LANG_CODE==2052)
    labelTip.Caption="正在压缩";
  else
    labelTip.Caption="Zipping";
  System.ProcessMessages();
  if(ZipData())
  {
    if(LANG_CODE==2052)
      labelTip.Caption="正在发送...";
    else
      labelTip.Caption="Sending...";
    System.ProcessMessages();
    mailer=new dobject("smtpsend");
    ret=mailer.sendMail(cbSmtpServer.Text,editUser.Text,editUser.Text,editUser.Text,editPwd.Text,editSubject.Text,memoBody.Text,"",editAttchment.Text);//attachment /must ';' of the end
    if(ret)
    {
      if(LANG_CODE==2052)
        labelTip.Caption="发送完毕";
      else
        labelTip.Caption="Sent";
      System.ProcessMessages();

      if(LANG_CODE==2052)
        alert("自发邮件备份完成:"+ziped_file_name);
      else
        alert("Email self send finish: "+ziped_file_name);
      form.Close();
    }else
    {
      if(LANG_CODE==2052)
        labelTip.Caption="发送出现错误";
      else
        labelTip.Caption="Error occur";
      System.ProcessMessages();

      if(LANG_CODE==2052)
        alert("未能发送 - 错误信息:\r\n"+mailer.ReturnString);
      else
        alert("Can't send - error infoation:\r\n"+mailer.ReturnString);
    }
    mailer=null;
  }else
  {
    if(LANG_CODE==2052)
      labelTip.Caption="压缩文件出现问题:"+ziped_file_name;
    else
      labelTip.Caption="Error occur while zip: "+ziped_file_name;
  }
}

function ShowInputMailAccount()
{
  vcl=new dobject("vcl");
  fn=System.GetAppPath()+"MailAcount.dfm";
  scope="SendMailApp";
  form=vcl.LoadFormFromDfmFile(fn,scope,true);
  form.OnClose=function(Sender,Action){Sender.operator_delete();};

  editUser=dvm.GetSharedObject(scope+".MailAcount.editUser");
  editPwd=dvm.GetSharedObject(scope+".MailAcount.editPwd");
  cbSmtpServer=dvm.GetSharedObject(scope+".MailAcount.cbSmtpServer");
  editAttchment=dvm.GetSharedObject(scope+".MailAcount.editAttchment");
  editSubject=dvm.GetSharedObject(scope+".MailAcount.editSubject");
  memoBody=dvm.GetSharedObject(scope+".MailAcount.memoBody");
  labelTip=dvm.GetSharedObject(scope+".MailAcount.labelTip");
  
  if(LANG_CODE!=2052)
  {
    form.Caption="Backup data through self email";
    dvm.GetSharedObject(scope+".MailAcount.labelUser").Caption="User";
    dvm.GetSharedObject(scope+".MailAcount.labelPwd").Caption="Password";
    dvm.GetSharedObject(scope+".MailAcount.labelSmtpServer").Caption="Server";
    dvm.GetSharedObject(scope+".MailAcount.labelAttchment").Caption="Attchment";
    dvm.GetSharedObject(scope+".MailAcount.labelSubject").Caption="Subject";
    dvm.GetSharedObject(scope+".MailAcount.labelBody").Caption="Body";
  }

  editUser.Text="";
  editPwd.Text="";
  editAttchment.Text=ziped_file_name+";";
  if(LANG_CODE==2052)
    memoBody.Text="Duceland Widgets 引擎数据备份 - "+ziped_file_short_name;
  else
    memoBody.Text="Data of Duceland Widgets Backup - "+ziped_file_short_name;
    
  if(LANG_CODE==2052)
    editSubject.Text="Duceland Widgets 引擎数据备份 - "+ziped_file_short_name;
  else
    editSubject.Text="Data of Duceland Widgets Backup - "+ziped_file_short_name;

  labelTip.Text="";

  dvm.GetSharedObject(scope+".MailAcount.btnSend").OnClick=OnSendButtonClick;

  form.Show();
}

ShowInputMailAccount();
