var dpDateSentFrom, dpDateSentTo;
var dpDateRecievedFrom, dpDateRecievedTo;

function AccountEdit(APKV, AAction)
{
  Popup(WSA_URLGet(SEMM_Account, AAction, ['PKV', APKV]), 450, 550, false, true);
}

function AccountSave(AForm)
{
  with (AForm)
  {
    if (FPassword.value != FPassword1.value)
    {
      alert('Passwords do not match');
      return;
    }
    if (
      FieldValidate(FName, fvtNotEmpty, 'Account Name', '', true)
      &&
      FieldValidate(FUserName, fvtNotEmpty, 'User Name', '', true)
      && 
      FieldValidate(FEMailAddress, fvtEMail, 'Email Address', '', true)
      &&
      FieldValidate(FReplyAddress, fvtEMail, 'Reply Address', '', false)
      &&
      FieldValidate(FPOP3Server, fvtNotEmpty, 'POP3 Server', '', true)
      &&
      FieldValidate(FSMTPServer, fvtNotEmpty, 'SMTP Server', '', true)
      &&
      FieldValidate(FPOP3Port, fvtIntGT0, 'POP3 port', '', true)
      &&
      FieldValidate(FSMTPPort, fvtIntGT0, 'SMTP port', '', true)
      &&
      FieldValidate(FServerUserName, fvtNotEmpty, 'User Name', '', true)
    )
      AForm.submit();
  }
}

function AccountDelete(AForm)
{
  if ((CB_CheckedValuesGet(AForm.PKV)).length == 0)
  {
    alert('No account(s) selected.');
    return;
  }
  
  if (AccountDeleteConfirm())
    AForm.submit();
}

function AccountDeleteConfirm()
{
  return confirm('Are you sure you want to permanently delete the selected account(s) and all letters related to them');
}

function AttachmentAdd()
{
  if (ElementGet('frm').submited)
    return;
  Popup(WSA_URLGet(SEMM_AttachmentUpload, 'E', [
    'PKV', ElementGet('PKV').value, 
    'UID', ElementGet('FUID').value]), 380, 150, false, true);
  if (ElementGet('PKV').value == '')
    LetterSave('AttachmentAdd', ElementGet('frm'));
}

function AttchmentDelete()
{
  var LObject = ElementGet('sAttachments');

  if (LObject.selectedIndex < 0)
  {
    alert('No attachment(s) selected');
    return false;
  }
  
  if (!confirm('Are you sure you want to delete the selected attachment(s)?'))
    return false;
  
  for (var i = LObject.options.length - 1; i >= 0 ; i--)
    if (LObject.options[i].selected == true)
    {
      LObject.options[i] = null;
      ElementGet('AttachmentDelete').value = 'Y';
    } 
}

function BlockedEMailAdd()
{
  with (ElementGet('frm'))
  {
    A.value="N";
    submit();
  }
}

function BlockedEMailDelete()
{
  if ((CB_CheckedValuesGet(ElementGet('frm'))).length == 0)
  {
    alert('No email(s) selected.');
    return;
  }
  if (confirm('Are you sure you want to permanently delete the selected email(s)'))
    ElementGet('frm').submit();
}

function BlockedEMailSave(AForm)
{
  var LElement;
  var  MailBoxes = new Array();
  with (ElementGet('frm'))
  {
    for (var i = 0; i < elements.length; i++)
    {
          LElement = elements[i];
        if (LElement.name.search('EMail') != -1)
      {
                if (!FieldValidate(LElement, fvtEMail, 'Email', '', true))
                return;
        MailBoxes[MailBoxes.length] = LElement.value;
      }
    }
    var MailBoxesSorted = MailBoxes.sort();

    for (var i = 0; i < MailBoxesSorted.length - 2; i++)
      if (MailBoxesSorted[i] == MailBoxesSorted[i + 1])
      {
        for (var j = 0; j < elements.length; j++)
        {  
          LElement = elements[j];
          if ((LElement.name.search('EMail') != -1) && (LElement.value == MailBoxesSorted[i]))
            break;
        }
        alert('Duplicate Emails');
        LElement.focus();
        return; 
      }
    A.value = 'S';
    submit();
  }
}

function  BlockedEMailsSearchSubmit()
{
  SubMenu_SectionFormOnSubmit('Search');
}

function BlockedEMailsSearchClear()
{
  with (ElementGet('frmFilter'))
  {
    AuxReverse[0].checked = true;
    AuxReverse[1].checked = false;
    EMail.value = '';
  }
}

function CheckMail()
{
  ElementGet('frmCheckMail').submit(); 
}

function ConfirmCodeApply()
{
  if (
    FieldValidate(ElementGet('ConfirmedEmail'), fvtEMail, 'email', '', true)
    &&
    FieldValidate(ElementGet('ConfirmCode'), fvtNotEmpty, 'confirmation code', '', true)
  )
  {
    if (frmCanSubmit(ElementGet('frm')))
    {
        ElementGet('frm').A.value = 'P';
        ElementGet('frm').submit();
    }
  }
}

function ConfirmCodeSend()
{
  if (FieldValidate(ElementGet('ConfirmCodeEmail'), fvtEMail, 'email address', '', true))
  {
    if (frmCanSubmit(ElementGet('frm')))
    {
        ElementGet('frm').A.value = 'M';
        ElementGet('frm').submit();
    }
  }
}

function ContactAddress_FieldValidate(AForm, APrefix, ACaptionPrefix)
{
  ACaptionPrefix = ACaptionPrefix ? ACaptionPrefix : '';
  APrefix = APrefix ? APrefix : '';
  with (AForm)
    return (
      FieldValidate(elements[APrefix + 'Address1'], fvtAddress, ACaptionPrefix + ' Address', '', false)
      &&
      FieldValidate(elements[APrefix + 'City'], fvtCity, ACaptionPrefix + ' City', '', false, '', elements[APrefix + 'CountryCode'].value)
      &&
      FieldValidate(elements[APrefix + 'StateProvince'], fvtNotEmpty, ACaptionPrefix + ' State/Province', '', false)
      &&
      FieldValidate(elements[APrefix + 'ZIP'], fvtZIP, ACaptionPrefix + ' ZIP', '', false)
      &&
      FieldValidate(elements[APrefix + 'CountryCode'], fvtNotEmpty, ACaptionPrefix + ' Country', '', false)
    );
}

function ContactEdit(APKV, AAction)
{
  Popup(WSA_URLGet(SEMM_Contact, AAction, ['PKV', APKV]), 380, 665, false, true);
}

function ContactSave(AForm)
{
  with (AForm)
  if (
    Name_FieldValidate(AForm, 'F', '')
    &&
    FieldValidate(FJobTitle, fvtJobTitle, 'Job Title', '', false)
    &&
    FieldValidate(FEMail, fvtEMail, 'Preferred e-mail address ', '', false)
    &&
    FieldValidate(EMail2, fvtEMail, 'Secondary e-mail address', '', false)
    &&
    FieldValidate(FPhoneWork, fvtPhone, 'Work Phone Number ', '', false, '', elements['FHomeCountryCode'].value)
    &&
    FieldValidate(FPhoneHome, fvtPhone, 'Home Phone Number ', '', false, '', elements['FHomeCountryCode'].value)
    &&
    FieldValidate(FPhoneMobil, fvtPhone, 'Mobile Phone Number', '', false, '', elements['FHomeCountryCode'].value)
    &&
    FieldValidate(FPager, fvtPhone, 'Pager', '', false, '', elements['FHomeCountryCode'].value)
    &&
    FieldValidate(FFax, fvtPhone, 'Fax', '', false, '', elements['FHomeCountryCode'].value)
    &&
    ContactAddress_FieldValidate(AForm, 'FHome', 'Home')
    &&
    ContactAddress_FieldValidate(AForm, 'FWork', 'Work')
  )
    AForm.submit()
  else
    CountryLookupRefresh();
}

function ContactsDelete(AForm)
{
  if ((CB_CheckedValuesGet(AForm.PKV)).length == 0)
  {
    alert('No contact(s) selected.');
    return;
  }
  if (confirm('Are you sure you want to permanently delete the selected contact(s)'))
    AForm.submit();
}

function ContactsInsert(ADestination)
{
  if (ADestination == '')  
    return;
  if ((CB_CheckedValuesGet(ElementGet('frm').PKV)).length == 0)
  {
    alert('No contact(s) selected.');
    return;
  }
  var LRes='';
  var i, j, LElement, LEMail, LEMailElement;
  for(i = 0; i < ElementGet('frm').elements.length; i++)
  {
    LElement = ElementGet('frm').elements[i];
    if(
      (LElement.type=='checkbox')
      &&
      (LElement.checked)
      &&
      (LElement.name != 'cbAll')
    )
    {
      LEMailElement = ElementGet(LElement.value + 'EMail');
      if (LEMailElement)
        LEMail = LEMailElement.value.trim();
      else
        LEMail = '';
      if (LEMail != '')
      {
        if(LRes != '')
          LRes += ', ';
        var LName = ElementGet('frm').elements[LElement.value + 'Name'].value;
        LName = LName.trim();
        if (LName != '')
          LName = '"' + LName + '" ';
        LRes += LName + '<' + LEMail + '>';
      }
      else
        alert('Contact "' + ElementGet(LElement.value + 'Name').value + '" doesn\'t have email');
    }
  }          
  if (opener)
  {
    if (LRes !='')
      if ((ElementGet(ADestination, opener).value.trim()) == '')
        ElementGet(ADestination, opener).value = LRes;
      else
        ElementGet(ADestination, opener).value += "," + LRes;
  }
  close();
}

function ContactsLookupProcess(ADestination)
{
  Popup(WSA_URLGet(SEMM_Contacts, '', ['Destination', ADestination]), 700, 660, false, true);
}

function DraftEdit(APKV)
{
  Popup(WSA_URLGet(SEMM_Letter, 'E', ['PKV', APKV, 'Action', '']), 700, 660, false, true);
}

function DownloadAttchment()
{
  with(ElementGet('frmAttachments'))
  {
    if (sAttachments.selectedIndex < 0)
      return;
    AttachmentID.value = sAttachments.options[sAttachments.selectedIndex].value;
    submit();
  }
}

function  ContactsSearchSubmit()
{
  SubMenu_SectionFormOnSubmit('Search');
}

function ContactsSearchClear()
{
  with (ElementGet('frmFilter')) 
  {
    AuxReverse[0].checked = true;
    AuxReverse[1].checked = false;
    NameFirst.value = '';
    NameLast.value = '';
    Phone.value = '';
    Company.value = '';
    EMail.value = '';
  }
}

function EAccountInformationControlsSet()
{
  with (ElementGet('frm'))
  {
    FUsePOPSettings.disabled = !FSMTPAuth.checked;
    FUsePOPSettings[0].disabled = !FSMTPAuth.checked;
    FUsePOPSettings[1].disabled = !FSMTPAuth.checked;
    FSMTPUserName.disabled = !FSMTPAuth.checked || !FUsePOPSettings[1].checked;
    FSMTPPassword.disabled = !FSMTPAuth.checked || !FUsePOPSettings[1].checked;
  }
}

function EEMailAccountInformationSave(AForm)
{
  with (AForm) 
    if (
      FieldValidate(FName, fvtNotEmpty, 'Account Name', '', true)
      &&
      FieldValidate(FEMailAddress, fvtEMail, 'EMail Address', '', true)
      &&
      FieldValidate(FUserName, fvtNotEmpty, 'User Name', '', true)
      &&
      FieldValidate(FPassword, fvtNotEmpty, 'Password', '', false)
      &&
      FieldValidate(FPOP3Server, fvtDomain, 'Incoming Server', '', true)
      &&
      FieldValidate(FSMTPServer, fvtDomain, 'Outgoing Server', '', true)
      &&
      FieldValidate(FSMTPUserName, fvtNotEmpty, 'SMTP User Name', '', false)
      &&
      FieldValidate(FSMTPPassword, fvtNotEmpty, 'SMTP Password', '', false)
      &&
      frmCanSubmit(AForm)
    )
      submit();
}

function EEMailAccountsSearchSubmit()
{
  SubMenu_SectionFormOnSubmit('Search');
}

function EEMailAccountsSearchClear()
{
  with (ElementGet('frmFilter'))
  {
    AuxReverse[0].checked = true;
    AuxReverse[1].checked = false;
    Name.value = '';
    EMailAddress.value = ''; 
    Active.selectedIndex = 0;
  }
}

function EEMailAccountEdit(AAction, APKV)
{
  var LPKVs = CB_CheckedValuesGet(ElementGet('frm').PKV);
  
  if (APKV)
    LPKVs[0] = APKV;
  else
  {
    if (AAction != 'New')
    {
      if (!CB_FieldValidate(ElementGet('frm').PKV, 'Email accounts', true))
        return;
    }
    else
      LPKVs[0] = '';
  }
  Popup(WSA_URLGet(SEMM_EEMailAccount, 'E', ['AccountID', LPKVs[0], 'Action', AAction]), 700, 500, false, true);
}

function EMailAccountsTabChange(ATabNo, ASection)
{
  var LURL = WSA_URLGet(ASection, '', ['TabNo', ATabNo]);
  if (top.frames.frMain)
  {
  top.SubMenu_Search = 'S=' + ASection + '&A=FF';
    top.frames.frMain.location = LURL;
  }
  else
    document.location = LURL;
}

function ForwardToActionProcess(AEnable)
{
  if (frmCanSubmit(ElementGet('frm')))
  {
    ElementGet('frm').A.value = AEnable ? 'B' : 'I';
    ElementGet('frm').submit();
  }
}

function frmSubmit(AAction)
{
  with (ElementGet('frm'))
  {    
    target = '';
    switch (AAction)
    {
    case 'S':    
      if (!frmCanSubmit(ElementGet('frm')) || !MessageValidateExt(ElementGet('frm')))
        return false;
    case 'F':
      break;
    case 'Preview':
      target = 'NewMessagePreviewWindow';
    }    
    A.value = AAction;
    submit();
    return true;
  }
}

function IAutoResponderSave(AForm)
{
  with (AForm)
    if (
      FEnableAutoReply.value == 0 ||
      FieldValidate(FReplyFrom, fvtEMail, 'Reply-from Address', '', true)
    )
      submit();
}

function IAccountAutoSetup(AAS_Program, AAS_Action, AAS_Mailbox)
{
  if (AAS_Action == 'I')
  {
    Popup(WSA_URLGet(SEMM_IEMailAccountAutoSetup, '', 
    [
      'AS_Mailbox', AAS_Mailbox,
      'AS_Program', AAS_Program
    ]), 570, 340);    
    return;
  }
  
  ElementGet('frm').AS_Program.value = AAS_Program;
  if (
    (ElementGet('frm').IsChanged.value == 'true')
    &&
    confirm('Account information was changed.\nDo you want to save it?')
  )
  {
    IAccountInformationPost(true);
  }
  else
  {
    ElementGet('frm').A.value = '';
    IAccountInformationPost(false);
  }
}

function IAccountInformationPost(AValidate)
{
  var LCanBeSubmited = true;
  if (AValidate)
    LCanBeSubmited = IAccountInformationValidate();
    
  LCanBeSubmited = (LCanBeSubmited && frmCanSubmit(ElementGet('frm')));
    
  if (LCanBeSubmited)
    ElementGet('frm').submit();
}

function IAccountInformationSave()
{
  IAccountInformationPost(true);
}

function IAccountInformationValidate()
{
  with (ElementGet('frm'))
  {
    return (
      FieldValidateEx(FFullName, FFullName.value != '', 'Account Name value is required; please specify it')
      &&
      FieldValidateEx(FFullName, FFullName.value.length <= 128, 'Account Name value value should be at most 128 characters long')
      &&
      FieldValidate(FMailbox, fvtMailBox, 'Email Address', '', true)
      &&
      (!ElementGet('FPassword') || FieldValidate(FPassword, fvtPassword, 'Password', '', false))
      &&
      (!ElementGet('FConfirmPassword') || FieldValidate(FConfirmPassword, fvtPassword, 'Confirm Password', '', false))
      &&
      (
        !ElementGet('FPassword')
        ||
        !ElementGet('FConfirmPassword')
        ||
        FieldValidateEx(FConfirmPassword, (FConfirmPassword.value == FPassword.value), 'Passwords do not match')
      )
      &&
      (
        !ElementGet('FForwardTo')
        ||
        FieldValidate(FForwardTo, fvtEMailList, 'Forward to', '', false)
      )
      &&
      FieldValidate(EMailAliases, fvtMailBoxList, 'Email Alias', '', false)
      &&
      (
        !ElementGet('FQuotaLimit') || 
        (
          FieldValidate(FQuotaLimit, fvtInt, 'Quota Limit', '', false) 
          && 
          FieldValidateEx(FQuotaLimit, ((FQuotaLimit.value == "") || (FQuotaLimit.value > 0)), '"' + FQuotaLimit.value + '" is not valid Quota Limit; value should be positive number or empty')
        )  
      )
    );
  }
}

function IAutoResponderControlsSet()
{
  with (ElementGet('frm'))
  {
    var LDisabled = !FEnableAutoReply[0].checked;
    FReplyFrom.disabled = LDisabled;
    FMessage.disabled = LDisabled;
    FReplyOnce.disabled = LDisabled;
  }
}

function IEMailAccountsSearchSubmit()
{
  SubMenu_SectionFormOnSubmit('Search');
}

function IEMailAccountsSearchClear()
{
  var LPartnerID = ElementGet('PartnerID');
  if (LPartnerID)
    LPartnerID.options[0].selected = true;
  with (ElementGet('frmFilter'))
  {
    AuxReverse[0].checked = true;
    AuxReverse[1].checked = false;
    Mailbox.value = '';
    ForwardTo.value = '';
    FullName.value = '';
    Active.selectedIndex = 0;
  }
}

function IEMailAccountsDelete(AForm)
{
  if (!CB_FieldValidate(ElementGet('frm').PKV, 'Email accounts', false))
    return;
  if (AccountDeleteConfirm())
    AForm.submit();
}

function IEMailAccountsAddMultiple(AMerchantID)
{
  Popup(WSA_URLGet(SEMM_AddMultiple, 'E', ['MerchantID', AMerchantID]), 800, 500, false, true);
}

function IEMailAccountsQuotaSet(AForm, AQuotaLimit)
{
  if (!CB_FieldValidate(ElementGet('frm').PKV, 'Email accounts', false))
    return;

  var LQuotaLimit = prompt('Please enter a new quota limit for selected accounts', AQuotaLimit);
  if (LQuotaLimit==null)
    return false;

  if (!LQuotaLimit || !LQuotaLimit.isInt())
  {
    alert('"' + LQuotaLimit + '" is not valid Quota Limit; value should be integer');
    return false;
  }

  if (LQuotaLimit <= 0)
  {
    alert('"' + LQuotaLimit + '" is not valid Quota Limit; value should be positive number');
    return false;
  }
  
  ElementGet('QuotaLimit').value = LQuotaLimit;
  ElementGet('A').value = 'Q';
  
  AForm.submit();
}

function IEAAddMultipleSave(AForm)
{
  function MailBoxValidate(APKV)
  {
    var LResult;
    with (AForm)
    {
      return (
        FieldValidateEx(elements['F' + APKV + 'FullName'], elements['F' + APKV + 'FullName'].value != '', 'Account Name value is required; please specifi it')
        &&
        FieldValidateEx(elements['F' + APKV + 'FullName'], elements['F' + APKV + 'FullName'].value.length <= 128, 'Account Name value value should be at most 128 characters long')
        &&
        FieldValidate(elements['F' + APKV + 'Mailbox'], fvtMailBox, 'Email Address', '', true)
        &&
        FieldValidate(elements['F' + APKV + 'Password'], fvtPassword, 'Password', '', true)
        &&
        FieldValidate(elements['F' + APKV + 'ConfirmPassword'], fvtPassword, 'Confirm Password', '', true)
        &&
        FieldValidateEx(elements['F' + APKV + 'ConfirmPassword'], (elements['F' + APKV + 'ConfirmPassword'].value == elements['F' + APKV + 'Password'].value), 'Passwords do not match')
        &&
        FieldValidate(elements['F' + APKV + 'ForwardTo'], fvtEMailList, 'Forward to', '', false)
      );
    }
  }

  var LCount = 0;
  with (AForm)
  {
    for (var i = 0; i < 10; i++)
    {
      if (
          elements['F' + i + 'QuotaLimit']
          && 
          (
            !FieldValidate(elements['F' + i + 'QuotaLimit'], fvtInt, 'Quota Limit', '', false) 
            || 
            !FieldValidateEx(elements['F' + i + 'QuotaLimit'], ((elements['F' + i + 'QuotaLimit'].value == "") || (elements['F' + i + 'QuotaLimit'].value > 0)), '"' + elements['F' + i + 'QuotaLimit'].value + '" is not valid Quota Limit; value should be positive number or empty')
          )  
      )
      {
        return;
      }
      
      if (elements['F' + i + 'FullName'])
      {
        if ( 
          (elements['F' + i + 'FullName'].value != '') 
          || 
          (elements['F' + i + 'Mailbox'].value != '') 
          || 
          (elements['F' + i + 'Password'].value != '') 
          || 
          (elements['F' + i + 'ForwardTo'].value != '')
          || 
          elements['F' + i + 'DontDeliver'].checked
          || 
          elements['F' + i + 'WebMail'].checked
        )
        {
          LCount++;
          if (!MailBoxValidate(i))
            return;
        }
      }
    }
      if (LCount == 0)
      {
        alert('No account information specified');
        return;    
      }
      submit();
  }  
}

function IEMailAccountEdit(AAction, APKV, AMerchantID, AIsDomainshield)
{
  var LPKVs = CB_CheckedValuesGet(ElementGet('frm').PKV);
  if (APKV)
    LPKVs[0] = APKV;
  else
  {
    if (AAction != 'New')
    {
      if (!CB_FieldValidate(ElementGet('frm').PKV, 'Email accounts', true))
        return;
    }
    else
      LPKVs[0] = '';
  }
  Popup(WSA_URLGet(SEMM_IEMailAccount, 'E', ['Mailbox', LPKVs[0], 'Action', AAction, 'MerchantID', AMerchantID, 'IsDomainshield', AIsDomainshield]), 785, 550, false, true);
}

function IGeneralSettingsSave()
{
  with (ElementGet('frm'))
  {
     if (RemoveDelay.disabled)
      FRemoveDelay.value = '';
     else
       FRemoveDelay.value = RemoveDelay.value;
     submit();
  }
}

function IGeneralSettingsLeaveMessagesClick(AOnload)
{
  with (ElementGet('frm'))
  {
    if (!FLeaveMessages.checked)
    {
      if (!AOnload)
      {
      }
      RemoveDelayEnabled.checked = false;
      RemoveDelayEnabled.disabled = true;
    }
    RemoveDelayEnabled.disabled = !FLeaveMessages.checked;
    IGeneralSettingsRemoveDelayClick();
  }
}

function IGeneralSettingsRemoveDelayClick()
{
  with (ElementGet('frm'))
  {
    RemoveDelay.disabled = !RemoveDelayEnabled.checked;
  }
}

function IReplyToSettingsSave()
{
  with (ElementGet('frm'))
    if (FieldValidate(FReplyAddress, fvtEMail, 'Reply-to Address', '', false))
      submit();
}

function LetterEdit(AAction)
{
  if (AAction != 'New')
    if (!CB_FieldValidate(ElementGet('frm').PKV, 'Letters', true))
      return;
  var LPKVs = CB_CheckedValuesGet(ElementGet('frm').PKV);
  Popup(WSA_URLGet(SEMM_Letter, 'E', ['PKV', LPKVs[0], 'Action', AAction]), 710, 660, false, true);
}

function LetterFormatChange()
{
  ElementGet('frm').A.value = 'E';
  ElementGet('frm').ChangeFormat.value = 'Yes';
  LetterSave('', ElementGet('frm'));
}

function LetterFromChange()
{
  ElementGet('frm').A.value = 'E';
  ElementGet('frm').ChangeFrom.value = 'Yes';
  LetterSave('', ElementGet('frm'));
}

function LetterDelete()
{
  if (confirm('Are you sure you want to permanently delete this message?'))
  {
      ElementGet('A').value = 'D';
      ElementGet('frm').submit();
  }
}

function LetterSave(AAction, Afrm)
{
  if (Afrm.submited)
    return;
  with (Afrm)
    if (
      (AAction == '')
      ||
      (AAction == 'AttachmentAdd') 
      ||
      (AAction == 'Save') 
      ||
      (  
      FieldValidate(FRecipients, fvtEMailListEx, 'To', '', true)
      &&
      FieldValidate(FCCList, fvtEMailListEx, 'CC', '', false)
      &&
      FieldValidate(FBCCList, fvtEMailListEx, 'BCC', '', false)))
    {

  	  try
      {
        if (FCKeditorAPI.GetInstance('LFCKeditor'))
        {
  	      var LSource = FCKeditorAPI.GetInstance('LFCKeditor').GetHTML();
          LSource = LSource.replace(/<~([^<~>]*)~ >/g, '<~$1~>');
          FBody.value = LSource;
        }  
      }
      catch(E){}
      if (Action.value != 'Edit')
        Action.value = AAction;

      AttachmentsPKVs.value = '';

      if (Afrm.sAttachments)
      { 
        for (var i = 0; i < sAttachments.options.length; i++)
          AttachmentsPKVs.value = AttachmentsPKVs.value + sAttachments.options[i].value + ', ';
        AttachmentsPKVs.value = AttachmentsPKVs.value.substr(0, AttachmentsPKVs.value.length - 2);
      }
      if (frmCanSubmit(Afrm))
        submit();
    }
}

function LettersDelete(AForm, AMessage)
{
  if ((CB_CheckedValuesGet(AForm.PKV)).length == 0)
  {
    alert('No letter(s) selected.');
    return;
  }
  if (confirm(AMessage))
    AForm.submit();
}

function LettersSearchDatePickersInit()
{
  dpDateSentFrom = DatePickerCreate(ElementGet('dvDateSentFrom'), ElementGet('DateSentFrom').value, dpoOptional | dpoDate);
  dpDateSentTo = DatePickerCreate(ElementGet('dvDateSentTo'), ElementGet('DateSentTo').value, dpoOptional | dpoDate);
  dpDateRecievedFrom = DatePickerCreate(ElementGet('dvDateRecievedFrom'), ElementGet('DateRecievedFrom').value, dpoOptional | dpoDate);
  dpDateRecievedTo = DatePickerCreate(ElementGet('dvDateRecievedTo'), ElementGet('DateRecievedTo').value, dpoOptional | dpoDate);
}

function  LettersSearchDatePickersSave()
{
  ElementGet('DateSentFrom').value = dpDateSentFrom.strDateGet();
  ElementGet('DateSentTo').value = dpDateSentTo.strDateGet();
  ElementGet('DateRecievedFrom').value = dpDateRecievedFrom.strDateGet();
  ElementGet('DateRecievedTo').value = dpDateRecievedTo.strDateGet();
}

function  LettersSearchSubmit()
{
  LettersSearchDatePickersSave();
  return(DateRange_FieldValidate(dpDateSentFrom, dpDateSentTo, 'Date Sent From', 'Date Sent To') 
    &&
    DateRange_FieldValidate(dpDateRecievedFrom, dpDateRecievedTo, 'Date Received From', 'Date Received To'));
  SubMenu_SectionFormOnSubmit('Search');
}

function LettersSearchClear()
{
  with (ElementGet('frmFilter')) 
  {
    AuxReverse[0].checked = true;
    AuxReverse[1].checked = false;
    From.value = '';
    To.value = '';
    Subject.value = '';
    Body.value = ''; 
    Viewed.selectedIndex = 0; 
    Priority.selectedIndex = 0; 
    LettersSearchDatePickersInit();
  }
  
  function LDateTimePickersClear(ADateTimePickers)
  {
    for (var i = 0; i < ADateTimePickers.length; i++)
    {
      var LDateTimePicker = ADateTimePickers[i]
      LDateTimePicker.dateSet((new Date()));
      LDateTimePicker.childGet('cbEnabled').checked = false;
      LDateTimePicker.enabledSet(false);
    }
  }
  
  LDateTimePickersClear([dpDateSentFrom, dpDateSentTo, dpDateRecievedFrom, dpDateRecievedTo]);
}

function LetterView(APKV)
{
  Popup(WSA_URLGet(SEMM_LetterView, 'E', ['PKV', APKV]), 710, 660, false, true);
}

function LetterViewActionProcess(AAction)
{
  with (ElementGet('frm'))
  {
    S.value = SEMM_LetterView;
    A.value = AAction;
    submit();
  }
}

function ListShow(AList)
{
  with (open('', '', 'resizable=yes, scrollbars=yes, width=300, height=400'))
  {
    with (document)
    {
      open();
      write(AList);
      close();
    }
    focus();
  }
}

function MenuRefresh()
{
   alert(top.frames.frMain.location);
   alert(top.frames.frSubMenu.location);
   top.frames.frMain.location
  //top.frames.frSubMenu.location = top.frames.frSubMenu.location;
}

function MessageValidate(AForm)
{
  with (AForm)
    return (
/*      FieldValidateEx(FMessageFrom,
        FMessageFrom.value.trim() != '' || FMessageFromEMail.value.trim() != '', 
        'Please specify either Message Sender Name or EMail.'
      )
      && */
      FieldValidate(FMessageFromEMail, fvtEMail, 'From EMail', '', true)
      &&
      FieldValidate(FMessageSubject, fvtNotEmpty, 'Message subject', '', true)
      &&
      FieldValidate(F1Template, fvtNotEmpty, 'Message body', '', true)
    );
}

function MessageValidateExt(AForm)
{
  with (AForm)
    return (MessageValidate(AForm)
      && 
      FieldValidate(FAdmins, fvtEMail, 'Report to EMail', '', false)
      &&
      FieldValidate(FReplyTo, fvtEMail, 'Reply to EMail', '', false)
      &&
      (FListID.selectedIndex != 0 || FieldValidate(FEMails, fvtEMailList, 'EMail list', '', true))
    );
}

function Navigate(AAction)
{
  LetterViewActionProcess(AAction);
}

function EMailFriendValidate(AForm)
{
  with (AForm)
    return (
      FieldValidate(FMessageFromEMail, fvtEMail, 'Your Email', '', true)
      &&
      FieldValidate(AForm.FEMails, fvtEMail, 'Send Email To', '', true)
      &&
      FieldValidate(FMessageSubject, fvtNotEmpty, 'Subject', '', true)
      &&
      FieldValidate(F1Template, fvtNotEmpty, 'Message body', '', true)
    );
}

function SchedulingEdit(ATaskID, APKV, AType)
{
  Popup(WSA_URLGet(SEMM_MessageScheduling, '', 
  [
    'TaskID',  ATaskID,
    'PKV',     (APKV ? APKV : '')
  ]), 400, 300, true, true, true);
}

function MessagePreview(AID)
{
  Popup('default.asp?' + SIDQ + 'S=' + SEMM_Message + '&A=Preview&PKV=' + AID,
    700, 550, true, true, true);
}

function MessagePreviewNew()
{
  frmSubmit('Preview');
}

function MessageNew()
{
  Popup('default.asp?' + SIDQ + 'S=' + SEMM_Message + '&A=N&' + RefererCatchParamsGet(), 735, 490, false, true, true);
}

function MessagesDelete(AForm)
{
  if (CB_CheckedValuesGet(AForm.PKV).length == 0)
  {
    alert('No message(s) selected');
    return;
  }
  if (confirm('Are you sure you want to permanently delete this message(s)?'))
    AForm.submit();
}

function NewsletterSubmit(AForm)
{
  with (AForm)
    if (
      FieldValidate(FEmail, fvtEMail, 'Email Address', '', true)
    )
      submit();
}

function ViewedLetterEdit(AAction)
{
  with (ElementGet('frm'))
  {
    Action.value = AAction;
    submit();
  }
}
