function AddressValidate(AAddressType, AFormName)
{
  var LPrefix = 'F' + AAddressType;
  var LForm = undefined;
  if (AFormName != undefined)
    LForm = ElementGet(AFormName);
  if (LForm == undefined)
    LForm = document.OrderForm;

  with (LForm)
    return (
      Name_FieldValidate(LForm, LPrefix)
      &&
      Address_FieldValidate(LForm, LPrefix)
      &&
      FieldValidate(elements[LPrefix + 'EMail'], fvtEMail, 'EMail', '', true)
      &&
      FieldValidate(elements[LPrefix + 'Phone'], fvtPhoneCommon, 'Phone', '', true)
      &&
      (
        !elements[LPrefix + 'ConfirmEMail'] ||
        (
          !(AAddressType == patBilling) || FieldValidateEx(elements[LPrefix + 'ConfirmEMail'],
            elements[LPrefix + 'ConfirmEMail'].value == elements[LPrefix + 'EMail'].value, 'Valid EMail confirmation is required')
        )
      )
    );
}

function ValidateForm(ASubmitBtn, ANotDefaultLayout)
{
  if(window.hints)
    hints.onsubmit();
  var LPaymentTypes = CB_ArrayForce(document.OrderForm.FPPaymentTypeID);
  if (
    AddressValidate(patBilling, ANotDefaultLayout) && AddressValidate(patShipping)
    &&
    (
      LPaymentTypes.length == 0
      ||
      LPaymentTypes.length == 1
      ||
      FieldValidateEx(LPaymentTypes[0], CB_CheckedCountGet(LPaymentTypes) != 0, 'Payment Type is required')
    )
    &&
    ProductsValidate(document.OrderForm)
    &&
    frmCanSubmit(document.OrderForm, ASubmitBtn)
  )
    document.OrderForm.submit();
  else
  if (window.hints)
    hints.init();
}

function ProductsValidate()
{
  with (document.OrderForm)
  {
    for (var i = 0; i < elements.length; i++)
    {
      if (elements[i].name.indexOf('FQuantity') != -1)
      {
        if (!SC_ItemValidate(document.OrderForm, elements[i].name.substring('FQuantity'.length, elements[i].name.length)))
          return false;
      }
    }
  }
  return true;
}

function SC_ItemValidate(AForm, APKV)
{
  var LPrefix = 'F' + APKV;
  with (AForm)
    return (
      (
        !elements[LPrefix + 'Colors']
        ||
        FieldValidate(elements[LPrefix + 'Colors'], fvtNotEmpty, 'Color', '', true)
      )
      &&
      (
        !elements[LPrefix + 'Sizes']
        ||
        FieldValidate(elements[LPrefix + 'Sizes'], fvtNotEmpty, 'Size', '', true)
      )
    );
}

function ShippingCalculatorShow(ASection)
{
  Popup(
    WSA_URLGet(
      SECE_ShippingCalculator, '',
      [].refererParamsAdd(
      [
        'S', ASection
      ])
    ),
    420, 300, true, false, true, 'wndShippingCalculator'
  );
}

function frmParamAdd(AForm, AName, AValue)
{
  var LElement = document.createElement('input');
  LElement.type = 'hidden';
  LElement.name = AName;
  LElement.value = AValue;
  AForm.appendChild(LElement);
}

function ProfileClear(AForm)
{
  var LElementNames = [
    'NameTitle', 'NameFirst', 'NameMiddle', 'NameLast', 'NameSuffix',
    'Company', 'Address1', 'Address2', 'SuiteType', 'SuiteNo', 'City',
    'StateProvince', 'CountryCode',
    'ZIP', 'EMail', 'ConfirmEMail', 'Phone', 'PhoneWork', 'Fax'
  ];

  var LBillingPrefix = 'F' + patBilling;
  var LShippingPrefix = 'F' + patShipping;

  with (AForm)
    for (var i = 0, l = LElementNames.length; i < l; i++)
    {
      if (elements[LBillingPrefix + LElementNames[i]])
        with (elements[LBillingPrefix + LElementNames[i]])
          if (tagName == 'SELECT')
            selectedIndex = 0;
          else
            value = '';

      if (elements[LShippingPrefix + LElementNames[i]])
        with (elements[LShippingPrefix + LElementNames[i]])
          if (tagName == 'SELECT')
            selectedIndex = 0;
          else
            value = '';
    }
  AForm.elements[LBillingPrefix + 'CountryCode'].value = 'US';
  AForm.elements[LShippingPrefix + 'CountryCode'].value = 'US';
  if (JSCountriesList4)
    JSCountriesList4.LookupRefresh();
  if (JSCountriesList5)
    JSCountriesList5.LookupRefresh();

  frmParamAdd(AForm, 'ProfileClear', 'true');
}

function AddressReload(AForm)
{
  AForm.A.value = '';
  AForm.submit();
}

function OPWOrderValidate(AForm, ASubmitButton, APaymentType)
{
  if (
    PaymentValidate(AForm, APaymentType, 'FP', 'Payment ')
    &&
    frmCanSubmit(AForm, ASubmitButton)
  )
    return true;
  else
    return false;
}

function PoliciesShow()
{
  Popup(WSA_URLGet(SMSC_StaticPageCleanBody, '', ['Page', 'PopupPolicies.htm']), 640, 400, true);
}

function CheckIfRefundPolicyCheked(AForm)
{
  if (AForm.elements['RefundPolicy'])
    if (AForm.elements['RefundPolicy'].checked)
      return true;
    else
    {
      alert('Please check the Refund Policy to continue');
      return false;
    }
  else
    return true;
}

var CC_Types = ['VI', 'AE', 'MC', 'ND'];

function  CC_FirstCharTypeMatch(ACCNumber)
{
  var LCCNumber = ACCNumber.toString();
  var L = LCCNumber.length;
  if (L == 0)
    return '';
  var LCH_1 = LCCNumber.charAt(0);
  if (LCH_1 == '4')
    return 'VI';
  else
  if (LCH_1 == '5')
    return 'MC';
  else
  if (LCH_1 == '3')
    return 'AE';
  else
  if (LCH_1 == '6')
    return 'ND';
  else
    return '';
}

function CC_ImagesAdjust(ACardType)
{
  var LResult = true;
  for (var i = 0; i < CC_Types.length; i++)
  {
    var LImageSrc;
    if (CC_Types[i] == ACardType)
      LImageSrc = CC_Type_ImagePath + CC_Types[i] + '_normal.gif';
    else
      LImageSrc = CC_Type_ImagePath + CC_Types[i] + '_grey.gif';
    var LImageElement = document.getElementById(CC_Types[i]);
    if (!LImageElement)
    {
      if (CC_Types[i] == ACardType)
        LResult = false;
    }
    else
    if ((LImageElement.src) != LImageSrc)
      LImageElement.src = LImageSrc;
  }
  return LResult;
}

function CC_ImagesClear()
{
  for (var i = 0; i < CC_Types.length; i++)
  {
    var LImageSrc;
    LImageSrc = CC_Type_ImagePath + CC_Types[i] + '_normal.gif';
    var LImageElement = document.getElementById(CC_Types[i]);
    if (LImageElement && ((LImageElement.src) != LImageSrc))
      LImageElement.src = LImageSrc;
  }
}

function CC_Check(ACCNumber)
{
  if (ACCNumber.value == '')
  {
    CC_ImagesClear();
    return;
  }
  var LCardType = CC_FirstCharTypeMatch(ACCNumber.value);
  if (CC_ImagesAdjust(LCardType))
    ACCNumber.form.elements['FPCardType'].value = LCardType;
  else
    ACCNumber.form.elements['FPCardType'].value = '';
}

function PaymentTypeValidate(AForm, APaymentTypeID)
{
  if (APaymentTypeID != ptCreditCard)
    return true;

  return FieldValidateEx(AForm.elements['FPCardNumber'], AForm.elements['FPCardType'].value != '', 'Card type is not supported');
}

function CheckoutButtonDisable()
{
  ElementGet('SubmitBtn').disabled = true;
  ElementGet('SubmitBtn').style.display = 'none';
}

function PaymentReload(AForm)
{
  AForm.A.value = '';
  AForm.submit();
}

function cFloatFormEx2(AWidth, AHeight)
{
  var FfrmStub = new cForm(null, null, 2000);
  FfrmStub.left = -9999;
  var self = new cFloatForm(FfrmStub);

  self.cFloatFormEx_innerHTMLSet_I = self.innerHTMLSet;
  self.cFloatFormEx_show_I = self.show;
  self.cFloatFormEx_close_I = self.close;
  self.cFloatFormEx_EwindowOnResize_I = self.EwindowOnResize;

  self.baseClass = 'ffrm_base';
  self.popupLoadMsgBody = PopupLoadMsgBody;
  if (AWidth)
    self.width = AWidth;
  else
    self.width = 600;

  if (AHeight)
    self.height = AHeight;
  else
    self.height = 500;

  self.backgroundMaskGet = function()
  {
    return null;
  }

  self.close = function()
  {
    this.cFloatFormEx_close_I();
    this.elementsShow();
  }

  self.contentLoad = function()
  {
    (new cAJAX_Loader()).get(
      this.URLGet(), // abstract
      [this, 'contentLoadComplete'],//onLoadComplete
      {});//Context
  }

  self.contentLoadComplete = function(AResponse, AContext)
  {
    this.innerHTMLSet(AResponse.contentHTML);
  }

  self.elementsHide = function(ATagName, AWindow)
  {
    if (!MSIE || Opera)
      return;

    if (MSIE && (parseFloat(navigator.appVersion) >= 7))
      return;

    this.HiddenElements = [];
    var LElement,
      LCount = 0,
      LMargin = 14,
      LLeft = this.left,
      LTop = this.top,
      LWidth = this.width,
      LHeight = this.height,
      LWindow = AWindow ? AWindow : window;

    for (var Lx = LLeft; Lx < LLeft + LWidth + LMargin; Lx += LMargin)
      for (var Ly = LTop; Ly < LTop + LHeight + LMargin; Ly += LMargin)
      {
        LElement = LWindow.document.elementFromPoint(Lx, Ly);
        if (LElement && LElement.tagName == ATagName)
        {
          LElement.style.visibility = 'hidden';
          this.HiddenElements[LCount++] = LElement;
        }
      }
  }

  self.elementsShow = function(ATagName)
  {
    if (!MSIE || Opera)
      return;

    for (var i = 0; i < this.HiddenElements.length; i++)
      this.HiddenElements[i].style.visibility = 'visible';
    this.HiddenElements = [];
  }

  self.EwindowOnResize = function()
  {
    with (this.parentForm)
    {
      if (anchor.element)
      {
        divDisable.style.left = ElementAbsLeftGet(anchor.element) + anchor.offsetX;
        divDisable.style.top  = ElementAbsTopGet(anchor.element) + anchor.offsetY;
      }
    }
    this.cFloatFormEx_EwindowOnResize_I();
  }

  self.show = function()
  {
    this.elementsHide('SELECT');
    var LBackgroundMask = this.backgroundMaskGet();
    if (LBackgroundMask)
      with (this.parentForm)
      {
        left   = LBackgroundMask.left;
        top    = LBackgroundMask.top;
        width  = LBackgroundMask.width;
        height = LBackgroundMask.height;
        var LAnchor = window.document.elementFromPoint(left, top);
        anchor = {element: LAnchor, offsetX: left - ElementAbsLeftGet(LAnchor), offsetY: top - ElementAbsTopGet(LAnchor)};
      }
    this.cFloatFormEx_show_I(true, true);
    this.contentLoad();
  }

  self.innerHTMLSet = function(AHTML)
  {
    this.containerElement.innerHTML = [
      '<table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%" class="', this.baseClass, '">',
        '<tr id="ffHeader">',
          '<td>',
            '<table class="ffrm_header" border="0" cellspacing="0" cellpadding="0">',
              '<tr>',
                '<td width="100%" class="ffrm_header_text" align="left" valign="middle" nowrap>Information</td>',
                '<td valign="middle" nowrap>',
                  '<img name="ffClose" width="15" height="15" border="0" title="Close" src="', SharedURL, 'Images/AuctionBlast/ItemPreview/btn_Close.gif" onclick="ORI.objects[' + this.oriID + '].close(); return false;" ',
                    'onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'btnProductsCompareClose\', \'\', \'', SharedURL, 'Images/AuctionBlast/ItemPreview/btn_Close_over.gif\', 1)">',
                '</td>',
              '</tr>',
            '</table>',
          '</td>',
        '</tr>',
        '<tr>',
          '<td id="PopupContent">',
            '<table style="height: ', this.height - 20, 'px; overflow-y: auto; overflow-x: hidden;">',
                AHTML,
            '</table>',
          '</td>',
        '</tr>',
      '</table>'
    ].join('');

    this.moveHeaderElement = this.containerElement.firstChild.rows[0];
  }

  return self;
}

function cCouponInfoForm()
{
  var self = new cFloatFormEx2();

  self.contentLoad = function()
  {
    this.innerHTMLSet(ElementGet('CouponPopupContent').innerHTML);
    ElementGet('divCouponPopup').innerHTML = '';
  }

  self.width = 640;
  self.height = 200;

  return self;
}

var CouponInfoForm;

function CouponInfoFormShow()
{
  CouponInfoForm = new cCouponInfoForm();
  CouponInfoForm.show();
}

function CouponInfoFormClose()
{
  if (ElementGet('chbCouponApply').checked)
    CouponInfoForm.close();
  else
  {
    var LForm = ElementGet('PaymentForm');
    LForm.A.value = 'W';
    LForm.CouponCode.value = '';
    LForm.submit();
  }
}
