var MENU_HIGHT = 22;
var frHeaderHeight = 57;
var frFooterHeight = 20;
var frSubMenuWidth = 190;
var frSubMenuWidth_Support = 290;
var frSubMenuWidth_Document = 155;
var frSubMenuTabsHeight = 20;
var PartnerID = -1;
var RowOnMouseOutColor = '#ffffff';
var RowOnMouseOverColor = '#ffcc66';
var dpSurveyBirthdayDate; 
var TWWindow;

function  SurveyRecordDatePickerInit()
{
  if (ElementGet('dvBirthday'))
    dpSurveyBirthdayDate = DatePickerCreate(ElementGet('dvBirthday'), (ElementGet('Birthday').value ? ElementGet('Birthday').value : '1/1/1975'), dpoDate);
}

function ActiveXSmartInstall(AActiveXName, ACodeBase, ADummyClassid)
{
  var LLeft = window.screenLeft + (window.document.body.offsetWidth - 300) / 2;
  var LTop = window.screenTop + (window.document.body.offsetHeight - 100) / 2;
  var LWindow = window.open('about:blank', '_blank',
    'toolbar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=no,width=380,height=140,left=' + LLeft + ',top=' + LTop);
  LWindow.document.open();
  LWindow.document.write(
    '<head><title>Installing '+ AActiveXName + '</title></head>' +
    '<body border=0 bgcolor="#CCCCCC" onload="setInterval(\'LoadedCheck(objDummy)\', 500)">' +
    '<center><br><font face="verdana">Please wait while component is downloaded.<br>You will be asked to confirm installation</font></center>' +
    '<br>' +
    '<object id=objDummy width="0%" height="0%" classid="CLSID:' + ADummyClassid + '" codebase="' + ACodeBase + '"></object>' +
    '</body>' +
    '<' + 'script>' +
    'var IsLoaded = false;' +
    'function LoadedCheck(AObject)' +
    '{if (IsLoaded) return; try {if (AObject.State == 1) {IsLoaded = true; window.opener.location = window.opener.location; setTimeout(\'window.close();\', 2000) }} catch (E){}}' +
    '<' + '/script>'
  );
  LWindow.document.close();
}

function ComponentProcess(AComponentID, ASubMenuID, AfrMainParams)
{
  var LSection;
  switch(AComponentID)
  {
    case cAccountManager:
      LSection = SMSC_AccountManager;
      break;
    case cBusinessCenter:
      LSection = SMSC_BusinessCenter;
      break;
    case cDataManager:
      LSection = SMSC_DataManager;
      break;
    case cDomainManager:
      LSection = SMSC_DomainManager;
      break;
    case cEmailManager:
      LSection = SMSC_EMailManager;
      break;
    case cHelpContents:
      LSection = SMSC_HelpContents;
      break;
    case cHome:
      LSection = SMSC_Home;
      break;
    case ciAffiliateBuilder:
      LSection = SMSC_iAffiliateBuilder;
      break;
    case ciRevenueBuilder:
      LSection = SMSC_iRevenueBuilder;
      break;
    case ciSiteBuilder:
      LSection = SMSC_iSiteBuilder;
      break;
    case ciStoreBuilder:
      LSection = SMSC_iStoreBuilder;
      break;
    case ciTrafficBuilder:
      LSection = SMSC_iTrafficBuilder;
      break;
    case cReportManager:
      LSection = SMSC_ReportManager;
      break;
    case cTemplateWizard:
      LSection = SMSC_TemplateWizard;
      break;
    case cPartnerManager:
      LSection = SMSC_PartnerManager;
      break;
    case cEmployeeManager:
      LSection = SMSC_EmployeeManager;
      break;
    case cSalesContactsManager:
      LSection = SMSC_SalesContactsManager;
      break;
    case cNetworkManager:
      LSection = SMSC_NetworkManager;
      break;
    case cSupportManager:
      LSection = SMSC_SupportManager;
      break;
    case cMarketingManager:
      LSection = SMSC_MarketingManager;
      break;
    default:
      LSection = '';
  }
  AfrMainParams = AfrMainParams ? AfrMainParams : '';
  AfrMainParams = URLParamCheckAppend(AfrMainParams, 'S', LSection ? LSection : SMSC_Home);
  if (top.frFooter)
  {
    top.frFooter.location = WSA_URLGet(SMSC_Footer, '', ['C_', AComponentID]);
    top.frHeader.location = WSA_URLGet(SMSC_Header, '', ['C_', AComponentID]);
  }
  var LFrame = (top.frMain ? top.frMain : top);
  WaitMessageShow(LFrame);
  LFrame.location = WSA_URLGet2(AfrMainParams,
    [
      'C_',   AComponentID,
      'SMID', ASubMenuID
    ], true);

  if (top.frSubMenu)
  {
    if (AComponentID != cHome)
    {
      top.frSubMenu.location = 'default.asp?' + SIDQ + 'S=' + SMSC_SubMenu + '&C_=' + AComponentID + (ASubMenuID ? '&SMID=' + ASubMenuID : '') + '&TS=' + Date.now();
      if (top.frSubMenuTabs)
        top.frSubMenuTabs.location = WSA_URLGet(SMSC_SubMenuTabs, '', ['C_', AComponentID]);
    }
    SubMenu_GlobalParamsSet(ASubMenuID, '', '');
  }
/*
  // Footer is updated every time to support logic that show unregistered components
  if ((AComponentID == cHome)||(top.SubMenu_Component == cHome)||(!top.SubMenu_Component))
    top.frames.frFooter.location = WSA_URLGet(SMSC_Footer, '', ['C_', AComponentID]);
*/
}

function ConstantContactPopup()
{
  window.openNew('http://www.constantcontact.com/index.jsp?pn=succeed',
    window.top.document.body.clientWidth + 10, window.top.document.body.clientHeight + 100);
}

function DOC_SettingsPageShow(ASection, ASubMenuItemID)
{
  WaitMessageShow(top.frMain);
  top.frMain.location = WSA_URLGet(ASection, '', ['PKV', top.DocumentPKV]);
  SubMenu_Submit('S=' + ASection + '&A=FF', '', ASubMenuItemID);
}

function DocumentBackupManagerStart()
{
  Popup(WSA_URLGet(SDOC_Backup_Frameset, '', []), 784, 540, false, true, 1, 'DocumentBackupManagerStart');
}

function DownloadProcess()
{
  with (document.all.frmLink)
    if (
      FieldValidate(FNameFull, fvtNameFull, 'Full Name', '', true)
      &&
      FieldValidate(FPhone, fvtPhone, 'Phone', '', true)
      &&
      FieldValidate(FEMail, fvtEMail, 'Email', '', true)
    )
      submit();
}

function eBayAccountTokenGet(AReferer, ARefreshOpener, AAlternativeSection)
{
  var LSection = AAlternativeSection ? AAlternativeSection : SAB_AccountSetup;
  var LParams = ['RefOp', (ARefreshOpener ? 1 : 0)];
  if (AAlternativeSection)
    LParams = LParams.concat('Type', 'W');
  Popup(
    WSA_URLGet(
      LSection, '', 
      LParams.refererParamsAdd(
      [
        'S', AReferer
      ])
    ),
    800, 590, false, true
  );
}

function FileManagerStart(AMerchantID)
{
  if (navigator.userAgent.indexOf('MSIE') >= 0)
    FileManagerActiveXStart();
  else
    FileManagerJSStart(AMerchantID);
}

function FileManagerActiveXStart()
{
  Popup(WSA_URLGet(SMSC_FileManagerActiveX, '', []), 784, 540, false, true, 1, 'FileManagerActiveX');
}

function FileManagerJSStart(AMerchantID)
{
  Popup(WSA_URLGet(SMSC_FileManagerJS, '', ['MerchantID', AMerchantID]), 784, 540, false, true, 1, 'FileManagerJS');
}

function FramesResize()
{
  var LWindowHeight = window.document.body.clientHeight;
  var LWindowWidth = window.document.body.clientWidth;

  top.frHeader.width = LWindowWidth;

  top.frFooter.top = LWindowHeight - top.frFooter.height;
  top.frFooter.width = LWindowWidth;

  top.frSubMenu.top = top.frHeader.height + MENU_HIGHT;
  top.frSubMenu.height = LWindowHeight - top.frSubMenu.top - top.frSubMenuTabs.height - top.frFooter.height;

  top.frSubMenuTabs.top = top.frSubMenu.top + top.frSubMenu.height;

  top.frMain.top = top.frHeader.height + MENU_HIGHT;

  top.frMain.height = LWindowHeight - top.frMain.top - top.frFooter.height;
  top.frMain.width = LWindowWidth - top.frSubMenuTabs.width;
  top.frMain.left = top.frSubMenuTabs.width;
}

function FrameRefresh(AFrame)
{
  with (AFrame)
    location = location;
}

function Frames_ShowHide(AShow)
{
  top.document.getElementById('frsBody').cols = (AShow ? frSubMenuWidth : '0') + ',*';
  if (top.frsSubMenu)  // there are situations where there is no SubMenuTabs
    top.frsSubMenu.rows = '*,' + (AShow ? frSubMenuTabsHeight : '0');
}

function Frames_ShowHide_Support(AShow)
{
  top.document.getElementById('frsBody').cols = (AShow ? frSubMenuWidth_Support : '0') + ',*';
  if (top.frsSubMenu)  // there are situations where there is no SubMenuTabs
    top.frsSubMenu.rows = '*,' + (AShow ? frSubMenuTabsHeight : '0');
}

function frMain_Load(ASubMenuID, AParams)
{
  WaitMessageShow(top.frMain);
  top.frames.frMain.location = WSA_URLGet2('S=' + AParams, ['SMID_Node', ASubMenuID]);
}

function frMain_SubMenuLoad(ASubMenuID, AParams)
{
  top.frames.frSubMenu.location = 'default.asp?' + SIDQ + 'S=' + SMSC_SubMenu + (ASubMenuID ? '&SMID=' + ASubMenuID : '') + '&TS=' + Date.now();
  top.frames.frSubMenuTabs.location = WSA_URLGet(SMSC_SubMenuTabs, '');
  WaitMessageShow(top.frMain);
  top.frames.frMain.location = 'default.asp?' + SIDQ + 'S=' + AParams + (ASubMenuID ? '&SMID_Node=' + ASubMenuID : '')
}

function GraphicStudioStart()
{
  Popup(WSA_URLGet(SGS_Frameset, '', []), 784, 540, false, false, 1, 'GraphicTools');
}

function GoogleAnaliticsStatsOpen()
{
  if ((!top.frSubMenu.GoogleAnaliticsEMail || !top.frSubMenu.GoogleAnaliticsKey)
    && (!top.frMain.GoogleAnaliticsEMail || !top.frMain.GoogleAnaliticsKey))
  {
    ComponentProcess('6', 'GooglePackUpgrade');
    alert('For access to Google Analitics you should configure Google Pack Upgrade');
    return false;
  }
  else
  {
    var LEmail = (top.frSubMenu.GoogleAnaliticsEMail?top.frSubMenu.GoogleAnaliticsEMail:top.frMain.GoogleAnaliticsEMail);
    var LKey = (top.frSubMenu.GoogleAnaliticsKey?top.frSubMenu.GoogleAnaliticsKey:top.frMain.GoogleAnaliticsKey);
  }

  var LWND = window.open('', '_blank');
  LWND.document.write(
    '<html>' +
    '<body onLoad="window.document.frm.submit();">' +
    '  <base href="">' + 
    '  <form name="frm" method="post" action="https://www.google.com/accounts/ServiceLoginBoxAuth?continue=http%3A%2F%2Fwww.google.com%2Fanalytics%2Fhome%2F%3Fet%3Dreset%26amp%3Bhl%3D&service=analytics&nui=1&hl=en&PersistentCookie=yes&rmShown=1&null=Sign in">' +
    '    <input type="hidden" name="Email" value="' + LEmail + '">' +
    '    <input type="hidden" name="Passwd" value="' + LKey + '">' +
    '  </form>' +
    '</body>' +
    '</html>'
  );
  LWND.document.close();
/*
  if (top.frSubMenu)
  {
    top.frSubMenu.location = 'default.asp?' + SIDQ + 'S=' + SMSC_SubMenu + '&C_=11&SMID=GoogleAnalytics&TS=' + Date.now();
    SubMenu_GlobalParamsSet('GoogleAnalytics', '', '');
  }
*/
}

function iMallSetupWizardStart()
{
  Popup(WSA_URLGet(SRB_iMallSetupWizard, '', []), 830, 540, false, true, 1, 'IMallSetupWizard');
}

function ITraffixCOMPopup()
{
  window.openNew('http://itraffix.com/',
    window.top.document.body.clientWidth + 10, window.top.document.body.clientHeight + 100);
}

function PartnerIDChange(AValue)
{
  PartnerID = AValue;
  var LLength = top.frames.frMain.location.indexOf('&PartnerID=');
  var LLocation = top.frames.frMain.location;
  if (LLength != -1)
  {
    LLocation = LLocation.substring(0, LLength);
  }
  WaitMessageShow(top.frMain);
  top.frames.frMain.location = LLocation + (PartnerID ? '&PartnerID=' + PartnerID : '');
}

function RowOnMouseOver(ATR)
{
  ATR.bgColor = RowOnMouseOverColor;
}

function RowOnMouseOut(ATR)
{
  ATR.bgColor = RowOnMouseOutColor;
}

function TW_Popup(ASection)
{
//  Popup('default.asp?' + SIDQ + 'S=' + ASection, 800, 560);
  Popup('default.asp?' + SIDQ + 'S=' + ASection, 790, 546, false, true, true, 'TemplateWizardPopupMain');
}

function WSD_Popup(ASection)
{
//  Popup('default.asp?' + SIDQ + 'S=' + ASection, 800, 560);
  Popup('default.asp?' + SIDQ + 'S=' + ASection, 790, 546, false, true, true, 'WholeSalerDistributers');
}

function ShippingInfoProcess()
{
  Popup(WSA_URLGet(SMSC_ShippingInfo, 'E', []), 750, 550, true, true);
}

function SitePreview(ADesignNo, AShowCustomizableImages, AUpdateMenu, AShowCustomizableTextTags, AInPlaceEdit)
{
  WaitMessageShow(parent.frames.frMain);
  parent.frames.frMain.location = '../default.asp?' + SIDQ + 'DesignNo=' + ADesignNo +
    '&ImagesHighlight=' + (AShowCustomizableImages ? '1' : '0') + '&TextTagsHighlight=' + (AShowCustomizableTextTags ? '1' : '0') +
  '&InPlaceEdit=' + (AInPlaceEdit ? '1' : '0');
  if (AUpdateMenu != false)
  {
    SubMenu_Submit('S=' + SDOC_Documents + '&A=FF', '',
      (
        AInPlaceEdit ? 'InplaceEdit' :
        (
          'DesignView' +
          (AShowCustomizableImages ? 'Images' : '') +
          (AShowCustomizableTextTags ? 'TextTags' : '')
        )
      )
    );
  }
}

function SubMenu_IsShowing()
{
  return (top.SubMenu_IsShow);
}

function SubMenu_BtnUpdate(ABtn)
{
  ABtn.src = LSharedURL + 'Images/MenuHeader/' + (SubMenu_IsShowing() ? 'btn_SubMenuHide.gif' : 'btn_SubMenuShow.gif');
}

function SubMenu_ShowHide(ABtn)
{
  top.document.getElementById('frsBody').cols = (SubMenu_IsShowing() ? '0' : frSubMenuWidth) + ',*';
  if (top.frsSubMenu)
    top.frsSubMenu.rows = '*,' + (SubMenu_IsShowing() ? '0' : frSubMenuTabsHeight);
  top.SubMenu_IsShow = !top.SubMenu_IsShow;
  SubMenu_BtnUpdate(ABtn);
}

function SubMenu_frMainLoad(AParams)
{
  if (AParams.slice(0, 11).toLowerCase() == 'javascript:')
    /*top.frames.frMain.*/eval(AParams.slice(11));
  else
  {
    AParams = URLParamCheckAppend(AParams, 'TabNo', '0');
    AParams = URLParamCheckAppend(AParams, 'PageNo', '1');
    WaitMessageShow(top.frMain);
    top.frames.frMain.location = WSA_URLGet2(AParams, ['SMID_Node', top.SMID]);
  }
}

function SubMenu_GlobalParamsSet(ASubMenuItemID, ASearchParams, AHelpParams)
{
  top.SMID = ASubMenuItemID;
  top.SubMenu_Search = ASearchParams;
  top.SubMenu_Help = AHelpParams;
}

function SubMenu_Submit(ASearchParams, AHelpParams, ASubMenuItemID, ANodeClick)
{
  with(document.forms.frmSubMenu)
  {
    Component.value = top.SubMenu_Component;
    SMID.value = ASubMenuItemID;
    if (ANodeClick)
      SMID_Node.value = ASubMenuItemID;
    SubMenu_GlobalParamsSet(ASubMenuItemID, ASearchParams, AHelpParams);
    submit();
  }
}

function SubMenuClick(AParams, ASearchParams, AHelpParams, ASubMenuItemID, ANodeClick)
{
  if (AParams)
  {
    if (AParams.slice(0, 11).toLowerCase() == 'javascript:')
      eval(AParams.slice(11));
    else
    {
      SubMenu_Submit(ASearchParams, AHelpParams, ASubMenuItemID, ANodeClick);
      SubMenu_frMainLoad(AParams);
    }
  }
}

function SubMenu_QuickTipsPageLoad(ASubMenuID)
{
  top.count++;
  top.QuickTips_SMIDs[top.QuickTips_SMIDs.length] = '&SMID=' + SMID.value + '&C_=' + C_.value;
  top.frames.frSubMenu.location = 'default.asp?' + SIDQ + 'S=' + SMSC_SubMenu + '&A=H' + (ASubMenuID ? '&SMID=' + ASubMenuID : '');
}

function SubMenu_QuickTipsPageBack()
{
 if (top.count==1)
 {
   top.frames.frSubMenu.location = WSA_URLGet(SMSC_SubMenu, '', ['SMID', top.SMID]);
   top.frames.frSubMenuTabs.location = WSA_URLGet(SMSC_SubMenuTabs, 'Menu')
 }
 else
 {
   top.count--;
   top.frames.frSubMenu.location = 'default.asp?' + SIDQ + 'S=' + SMSC_SubMenu + '&A=H' + top.QuickTips_SMIDs[top.QuickTips_SMIDs.length - 1];
   top.QuickTips_SMIDs.length = top.QuickTips_SMIDs.length - 1;
 }
}

function SubMenuNodeClick(ASubMenuItemID)
{
  with(frmSubMenu)
  {
    Component.value = top.SubMenu_Component;
    SMID_Node.value = ASubMenuItemID;
    submit();
  }
}

function SubMenuReload()
{
  with(top.frames.frSubMenu)
  {
    var LURL = location.href;
    if (LURL.indexOf('.tmp') == -1)
      LURL = URLParamCheckAppend(LURL, 'SMID', top.SMID)
    location.href = LURL;
  }
}

function SubMenuTabsClick(AAction)
{
  switch (AAction)
  {
    case 'Menu':
      if (!top.SubMenu_Component)
        top.SubMenu_Component = SMSC_Home;
          top.frames.frSubMenu.location = WSA_URLGet(SMSC_SubMenu, '', ['Component', top.SubMenu_Component, 'SMID', top.SMID]);
      break;
    case 'Search':
      if (!top.SubMenu_Search)
        top.SubMenu_Search = 'S=' + SMSC_SubMenu + '&A=FF';
          top.frames.frSubMenu.location = 'default.asp?' + SIDQ + top.SubMenu_Search + '&SMID=' + top.SMID;
      break;
    case 'Help':
      top.count = 1;
      top.QuickTips_SMIDs = new Array();
    if (!top.SubMenu_Help)
        top.SubMenu_Help = 'S=' + SMSC_SubMenu + '&A=H';
      top.frames.frSubMenu.location = 'default.asp?' + SIDQ + top.SubMenu_Help + '&SMID=' + top.SMID;
      break;
  }
  location = WSA_URLGet(SMSC_SubMenuTabs, AAction);
}

function SubMenuToolbarClick(ATabName)
{
  if (ATabName == 'HelpTopics')
  {
    top.frames.frSubMenu.location = WSA_URLGet(SMSC_SubMenu, '', ['Component', top.SubMenu_Component, 'SMID', top.SMID]);
  }
  else
  if (ATabName == 'Glossary')
  {
    top.frames.frSubMenu.location = WSA_URLGet(SCRM_Glossary, '');
  }
  location = WSA_URLGet(SMSC_SubMenuToolBar, '', ['TabName', ATabName]);
}

function TabSelect(ATabName)
{
  if (ATabName == 'HelpTopics')
  {
    top.location = WSA_URLGet(SMSC_FrameSet);
  }
  else
  if (ATabName == 'ContactSupport')
  {
    top.location = WSA_URLGet(SCRM_ContactSupport, 'F');
  }
}

function TemplateWizard_ESWStart()
{
  try
  { 
    if (TWWindow)
      TWWindow.close();
    TWWindow = Popup(WSA_URLGet(STW_ExpressSetupWizard, '', []), 800, 540, false, true, 1, 'Template_Wizard');
  }
  catch(e)
  {
  }
}

function TemplateWizardStart()
{
  try
  { if (TWWindow)
      TWWindow.close();
    TWWindow = Popup(WSA_URLGet(STW_FrameSet, '', ['C_', ciSiteBuilder]), 784, 540, false, true, 1, 'Template_Wizard');
  }
  catch(e)
  {
  }
}

function ToolButton_Highlight(AButton, AActive)
{
  switch (AButton)
  {
    case ContentsButton:
      document.getElementById('imgContents').src = LTemplateURL + (AActive?'Images/ToolBar/btn_contents_over.gif':'Images/ToolBar/btn_contents.gif');
      break;
    case IndexButton:
      document.getElementById('imgIndex').src = LTemplateURL + (AActive?'Images/ToolBar/btn_index_over.gif':'Images/ToolBar/btn_index.gif');
      break;
    case GlossaryButton:
      document.getElementById('imgGlossary').src = LTemplateURL + (AActive?'Images/ToolBar/btn_glossary_over.gif':'Images/ToolBar/btn_glossary.gif');
      break;
    case SearchButton:
      document.getElementById('imgSearch').src = LTemplateURL + (AActive?'Images/ToolBar/btn_search_over.gif':'Images/ToolBar/btn_search.gif');
      break;
  }
}

function ToolButton_MouseOut(AButton)
{
  if (LActiveButton != AButton)
    ToolButton_Highlight(AButton, false);
}

function ToolButton_MouseOver(AButton, ASrc)
{
  if (LActiveButton != AButton)
    ToolButton_Highlight(AButton, true);
}

function  SOMOrderWindowOpen(AParams)
{
  Popup(WSA_URLGet(SSOM_Order, '', AParams), 850, 785, true);
}

function GoogleAnalyticScriptValidate()
{
  var LScript;
  var LFirstTab = (ElementGet('didTab1').style.display=='');
  if (LFirstTab)
    LScript = ElementGet('AccountNumber').value;
  else
    LScript = ElementGet('Script').value;

  if (!LScript)
  {
    if (LFirstTab)
      alert('Account Number should be specified');
    else
      alert('Google (TM) Account Number or Analytics JScript should be specified');
    return false;
  }
  
  var LAccount = LScript.match(/UA-\d{1,}-\d{1}/i);

  if (!LAccount)
  {
    if (LFirstTab)
      alert('Account number is incorrect');
    else
      alert('Google (TM) Analytics JScript include incorrect account number format or account number is absent');
    return false;
  }

  ElementGet('AccountNumber').value = LAccount[0].substr(0, LAccount[0].length);
  return true;
}

function GoogleAnalyticsOptionsSave()
{
  if (FieldValidate(ElementGet('EMail'), fvtEMail, 'Email', '', false)
    && GoogleAnalyticScriptValidate())
  {
  
    ElementGet('IsPasswordEncrypted').value = 
      ((ElementGet('PWD').value != '') && ElementGet('PWD').value == ElementGet('Password').value?'1':'0');

    if (ElementGet('IsPasswordEncrypted').value == '0') 
      if (!FieldValidate(ElementGet('Password'), fvtPassword, 'Password', '', false))
        return false;

    ElementGet('frmGoogleAlalytics').submit();
  }
}