﻿// ############### CART VALIDATION #########################
var cartErrMsg = '';
var maxItemsForCurrentProduct = -1;
var noItemsForCurrentProduct = 0;



// ######################## SIZE VALIDATION ##############################
function sizePageValid(sender, args)
{
  var nextBtn = findElement('NextStepButton');
  if (nextBtn != null)
    nextBtn.disabled = true;

  var finishBtn = findElement('FinishButton');
  if (finishBtn != null)
    finishBtn.disabled = true;

  var valid = true;

  valid = validateCustomPrice();

  if (valid == true)
    valid = validateSize();

  if (valid == true)
    valid = validateQuantity();

  if (valid == true)
    valid = validateDogTag();

  if (valid == true)
    valid = validateGiftCert();

  if (valid == false)
  {
    if (cartErrMsg != "")
      alert(cartErrMsg)

    valid = false;
  }

  if (nextBtn != null)
    nextBtn.disabled = false;
  if (finishBtn != null)
    finishBtn.disabled = false;

  return valid;
}


function validateSize()
{
  var obj = findElement('SizeDropDownList');

  if (obj != null)
  {
    if (obj.value == 0)
    {
      cartErrMsg = 'You must enter a size';
      return false;
    }
    else
      return true;
  }
  else
    return true;
}

function validateDogTag()
{
  var valid = true;

  if (findElement('DogTagLengthDropDownList') != null)
  {
    var dtDDL = findElement('DogTagLengthDropDownList');

    if (dtDDL.value == '')
    {
      cartErrMsg = 'You have not chosen your chain length.  Please choose your chain length.';
      valid = false;
    }
  }

  return valid;
}

function validateQuantity()
{
  var qtyTB = null;
  var qty = 0;

  qtyTB = findElement('QuantityTextBox');

  qty = qtyTB.value;

  if (qty == '' || qty == '0')
  {
    cartErrMsg = 'You must enter a quantity';
    return false;
  }
  else if (isInteger(qty) == false)
  {
    cartErrMsg = 'You must enter a valid quantity';
    return false;
  }
  else
  {
    if (findElement('MIHiddenField') != null)
      maxItemsForCurrentProduct = parseInt(findElement('MIHiddenField').value)

    // check to make sure the user did not go over the limit for this product
    if (maxItemsForCurrentProduct != -1)
    {
      if (findElement('NIHiddenField') != null)
        noItemsForCurrentProduct = parseInt(findElement('NIHiddenField').value);

      if (parseInt(qty) + noItemsForCurrentProduct > maxItemsForCurrentProduct)
      {
        var totalItems = parseInt(qty) + noItemsForCurrentProduct;
        cartErrMsg = 'The maximum you can order for this product is ' + maxItemsForCurrentProduct + '.';
        return false;
      }
      else
        cartErrMsg = '';
    }
    else
      cartErrMsg = '';
  }

  return true;
}

function validateGiftCert()
{
  var valid = true;
  var gcPriceTextBox = $('input[id*=GiftCertPriceTextBox]');
  var gcEmailTextBox = $('input[id*=GiftCertificateEmailTextBox]');

  if (gcPriceTextBox != null && gcPriceTextBox.length > 0)
  {
    valid = isPrice(gcPriceTextBox.val())

    if (valid == false)
    {
      alert('You must enter a valid amount for the Gift Certificate');
      return false;
    }

    var errorText = '';

    if (gcEmailTextBox != null && gcEmailTextBox.length > 0)
    {
      var email = gcEmailTextBox.val();

      if (email == '')
      {
        errorText = errorText + '<li>Email is required</li>';
        $('input[id*=EmailRequired]').css('visibility', 'visible');
        $('input[id*=EmailExpressionRequired]').css('visibility', 'hidden');
      }
      else
      {
        $('input[id*=EmailRequired]').css('visibility', 'hidden');

        re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        if (re.test(email) == false)
        {
          errorText = errorText + '<li>Email is invalid</li>';
          $('input[id*=EmailExpressionRequired]').css('visibility', 'visible');
        }
        else
          $('input[id*=EmailExpressionRequired]').css('visibility', 'hidden');
      }

      if ($('input[id*=GiftCertificateFromTextBox]').val() == '')
      {
        errorText = errorText + '<li>From Name is required</li>';
        $('input[id*=EmailFromRequired]').css('visibility', 'visible');
      }
      else
        $('input[id*=EmailFromRequired]').css('visibility', 'hidden');

      if ($('input[id*=GiftCertificateToTextBox]').val() == '')
      {
        errorText = errorText + '<li>To Name is required</li>';
        $('input[id*=EmailToRequired]').css('visibility', 'visible');
      }
      else
        $('input[id*=EmailToRequired]').css('visibility', 'hidden');

    }

    var mailRadioObj = $('input[id*=GiftCertificateMailingRadioList]');

    if (mailRadioObj != null && mailRadioObj.length > 0)
    {
      var addr1 = $('input[id*=GiftCertificateAddressLine1]');
      if (addr1 != null && addr1.length > 0)
      {

        if (addr1.val() == '')
        {
          errorText = errorText + '<li>Address Line 1 is required.</li>';
        $('input[id*=AddressLine1Required]').css('visibility', 'visible');
        }
        else
        $('input[id*=AddressLine1Required]').css('visibility', 'hidden');


        if ($('input[id*=GiftCertificateCity]').val() == '')
        {
          errorText = errorText + '<li>City is required.</li>';
        $('input[id*=CityRequired]').css('visibility', 'visible');
        }
        else
        $('input[id*=CityRequired]').css('visibility', 'hidden');


        ddl = $('select[id*=GiftCertificateState]');
        if (ddl.val() == '' || ddl.val() == '0')
        {
          errorText = errorText + '<li>State is required.</li>';
        $('input[id*=StateRequired]').css('visibility', 'visible');
        }
        else
        $('input[id*=StateRequired]').css('visibility', 'hidden');


        if ($('input[id*=GiftCertificateZipcode]').val() == '')
        {
          errorText = errorText + '<li>ZipCode is required.</li>';
        $('input[id*=ZipcodeRequired]').css('visibility', 'visible');
        }
        else
        $('input[id*=ZipcodeRequired]').css('visibility', 'hidden');

      ddl = $('select[id*=GiftCertificateCountry]');
        if (ddl.val() == '' || ddl.val() == '0')
        {
          errorText = errorText + '<li>Country is required.</li>';
        $('input[id*=CountryRequired]').css('visibility', 'visible');
        }
        else
        $('input[id*=CountryRequired]').css('visibility', 'hidden');
      }

      // selected use address from order
      if ($('input[id*=GiftCertificateFrom2TextBox]').val() == '')
      {
        errorText = errorText + '<li>From Name is required</li>';
        $('input[id*=EmailFromRequired2]').css('visibility', 'visible');
      }

      if ($('input[id*=GiftCertificateTo2TextBox]').val() == '')
      {
        errorText = errorText + '<li>To Name is required</li>';
        $('input[id*=EmailToRequired2]').css('visibility', 'visible');
      }
    }

    if (errorText == '')
    {
      $('#GCErrorSpan').html('');
      valid = true;
    }
    else
    {
      $('#GCErrorSpan').html('<ul>' + errorText + '</ul>');
      valid = false;
    }

    // because the heights are now set we need to make sure to adjust our heights again
    // to do this we will set our main area height to nothing so it auto stretches
    // then match the heights again
    $('#mainArea').css('height', '');
    adjustCategorySize();
  }
  return valid;
}



function validateCustomPrice()
{
  var obj = findElement('ProductPriceTextBox');

  if (obj == null)
    return true;
  else
    return isPrice(findElement('ProductPriceTextBox').value)
}

function validateDogTag()
{
  var valid = true;

  if (findElement('DogTagLengthDropDownList') != null)
  {
    var dtDDL = findElement('DogTagLengthDropDownList');

    if (dtDDL.value == '')
    {
      cartErrMsg = 'You have not chosen your chain length.  Please choose your chain length.';
      valid = false;
    }
  }

  return valid;
}

// ######################## END SIZE VALIDATION ##############################



// ######################## ENGRAVINGS VALIDATION ##############################
function engravingPageValid(sender, args)
{
  var nextBtn = findElement('NextStepButton');
  if (nextBtn != null)
    nextBtn.disabled = true;

  var finishBtn = findElement('FinishButton');
  if (finishBtn != null)
    finishBtn.disabled = true;

  var valid = true;

  valid = validateEngraving();

  if (valid == true)
    valid = validateStrand();

  if (valid == false)
  {
    if (cartErrMsg != "")
      alert(cartErrMsg)

    valid = false;
  }

  if (nextBtn != null)
    nextBtn.disabled = false;
  if (finishBtn != null)
    finishBtn.disabled = false;

  return valid;
}

function validateEngraving()
{
  var valid = true;

  // first see if we have any engravings

  if (findElement('EngravingItem1') != null)
  {
    var engravingNo = 0;

    // find how many engravings
    var formObj = document.forms[0];
    for (var i = 0; i < formObj.length; i++)
    {
      fldObj = formObj.elements[i];

      if (fldObj.id.search('EngravingItem') != -1)
        engravingNo += 1;
    }

    // first make sure at least one is filled out
    // or that the blank tag is selected
    var blankObj = findElement('BlankTagCheckBox')
    var blankChecked = false;
    if (blankObj != null)
      blankChecked = blankObj.checked

    var filled = false;
    for (var i = engravingNo; i > 0; i--)
    {
      var txt = findElement('EngravingItem' + i).value;

      if (txt != '')
      {
        filled = true;
        break;
      }
    }

    if (filled == false && blankChecked == false)
    {
      cartErrMsg = 'You\'ve selected an item with Customized Lettering. \n However you have ' +
                          'not filled out all the lettering fields.'
      return false;
    }

    // we will do two loops.  We will make sure that they did not enter info
    // on a higher strand and not a lower strand
    for (var i = engravingNo; i > 0; i--)
    {
      var txt = findElement('EngravingItem' + i).value;

      if (txt != '')
      {
        for (var j = i - 1; j > 0; j--)
        {
          var txt2 = findElement('EngravingItem' + j).value;

          if (txt2 == '')
          {
            cartErrMsg = 'Engraving lines must be filled out in the order they appear.'
            valid = false;
            return valid;
          }
        }
      }
    }
  }

  return valid;
}

function validateStrand()
{
  var valid = true;

  // first see if we have any strands

  if (findElement('StrandItem1') != null)
  {
    var strandNo = 0;

    // find how many strands
    var formObj = document.forms[0];
    for (var i = 0; i < formObj.length; i++)
    {
      fldObj = formObj.elements[i];
      if (fldObj.id.search('StrandItem') != -1)
        strandNo += 1;
    }

    // first make sure at least one is filled out
    var filled = false;
    for (var i = strandNo; i > 0; i--)
    {
      var txt = findElement('StrandItem' + i).value;

      if (txt != '')
      {
        filled = true;
        break;
      }
    }

    if (filled == false)
    {
      cartErrMsg = 'You\'ve selected an item with Customized Lettering. \n However you have ' +
                          'not filled out all the lettering fields.'
      return false;
    }

    // we will do two loops.  We will make sure that they did not enter info
    // on a higher strand and not a lower strand
    for (var i = strandNo; i > 0; i--)
    {
      var txt = findElement('StrandItem' + i).value;

      if (txt != '')
      {
        for (var j = i - 1; j > 0; j--)
        {
          var txt2 = findElement('StrandItem' + j).value;

          if (txt2 == '')
          {
            cartErrMsg = 'You\'ve selected an item with Customized Lettering. \n However you have ' +
                          'not filled out all the lettering fields in the correct order.'
            valid = false;
            return valid;
          }
        }
      }
    }
  }

  return valid;
}


// ######################## END ENGRAVINGS VALIDATION ##############################


// ######################## SPECIFIC OPTIONS VALIDATION ##############################

// ######################## END SPECIFIC OPTIONS VALIDATION ##############################


// ######################## EXTRA OPTIONS VALIDATION ################################

function sizeExtraOptionsValid(sender, args)
{
  var finishBtn = findElement('FinishButton');
  finishBtn.disabled = true;

  var valid = true;

  valid = validateGift();

  if (valid == false)
  {
    if (cartErrMsg != "")
      alert(cartErrMsg)

    valid = false;
  }

  finishBtn.disabled = false;

  return valid;
}

var defGift = 'Enter your gift card message here!'
function validateGift()
{
  var valid = true;

  if (findElement('GiftWrapDropDownList') != null)
  {
    var giftWrapDDL = findElement('GiftWrapDropDownList');
    var giftDDL = findElement('GiftCardDropDownList');
    var giftMsg = findElement('GiftMessageTextBox');

    if (giftWrapDDL.value != '')
    {
      if (giftDDL.value != '' && (giftMsg.value == defGift))
      {
        cartErrMsg = 'You have selected a Gift Card but have not\nentered a message to put on the card.' +
                    '\n\nIf you do not want a Gift Card, please \nreturn the Gift Card field to - ' +
                    'select - and \ndelete all text from the Message field.';
        valid = false;
      }
      else if (giftDDL.value == '' && giftMsg.length > 0)
      {
        cartErrMsg = 'Please select the type of Gift Card \nyou want sent with your order.' +
                    '\n\nIf you do not want a Gift Card, please \ndelete all text from the message field.';
        valid = false;
      }
      else if (giftDDL.value != '' && giftMsg.length == 0)
      {
        cartErrMsg = 'You have selected a Gift Card but have not\nentered a message to put on the card.' +
                    '\n\nIf you do not want a Gift Card, please \nreturn the Gift Card field to - ' +
                    'select - and \ndelete all text from the Message field.';
        valid = false;
      }
      else if (CountWords(giftMsg, false, false) > 15)
      {
        cartErrMsg = 'You can only have a gift card message with 15 words or less.';
        valid = false;
      }
    }
    else
    {
      var confirmMsg = 'You have selected Gift Wrap, however you have not chosen your color bag or gift card.' +
                                '\n\n Would you still like Gift Wrap';

      var answer = confirm(confirmMsg)

      if (answer)
        valid = false;
    }
  }

  return valid;
}
// ######################## END EXTRA OPTIONS VALIDATION ################################




function specificOptionsPageValid(sender, args)
{
  var nextBtn = findElement('NextStepButton');
  if (nextBtn != null)
    nextBtn.disabled = true;

  var finishBtn = findElement('FinishButton');
  if (finishBtn != null)
    finishBtn.disabled = true;

  var valid = true;

  valid = validateTag();

  if (valid == false)
  {
    if (cartErrMsg != "")
      alert(cartErrMsg)

    valid = false;
  }

  if (nextBtn != null)
    nextBtn.disabled = false;
  if (finishBtn != null)
    finishBtn.disabled = false;

  return valid;
}

function validateTag()
{
  var valid = false;

  var defaultObj = null;
  var ringsObj = null;

  var checkedVal = $('input:radio:checked').val();

  if (checkedVal == null || checkedVal == '')
    valid = false;
  else
    valid = true;

  if (valid == false)
    cartErrMsg = 'You must enter a tag type';

  if (checkedVal == 'custom')
  {
    var tagVal = $('.customTagSize').val();

    if (tagVal == '')
    {
      cartErrMsg = 'Please enter your custom tag size';
      valid = false;
    }
  }

  return valid;
}


function changeTagImage()
{
  ddl = findElement('TagListDropDownList')
  img = findImage('TagListImage')

  if (ddl == null || img == null)
    return;

  img.src = 'images/tags/' + tagImg[ddl.selectedIndex]
  img.style.visibility = 'visible';
}


function removeEngravingText()
{
  var obj = null;
  var blankTagObj = findElement('BlankTagCheckBox')

  if (blankTagObj.checked == true)
  {
    for (var i = 1; i <= 5; i++)
    {
      obj = findElement('EngravingItem' + i);

      if (obj != null)
        obj.value = '';
    }
  }
}



// ######################## Snowmen VALIDATION ##############################
function snowmenValid(sender, args)
{
  var nextBtn = findElement('NextStepButton');
  if (nextBtn != null)
    nextBtn.disabled = true;

  var finishBtn = findElement('FinishButton');
  if (finishBtn != null)
    finishBtn.disabled = true;

  var valid = true;

  if (noSnowmen == 0)
  {
    valid = false;
    cartErrMsg = 'You must select at least 1 snowman.';
  }

  if (valid == false)
  {
    if (cartErrMsg != "")
      alert(cartErrMsg)

    valid = false;
  }

  if (nextBtn != null)
    nextBtn.disabled = false;
  if (finishBtn != null)
    finishBtn.disabled = false;

  return valid;
}
