﻿// ############### 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;
    if (findElement('GiftCertPriceTextBox') != null)
    {
        valid = isPrice(findElement('GiftCertPriceTextBox').value)

        if (valid == false)
        {
            alert('You must enter a valid amount for the Gift Certificate');
            return false;
        }
        
        var errorText = '';

        if (findElement('GiftCertificateEmailTextBox') != null)
        {
            // validate email gc

            var emailObj = findElement('GiftCertificateEmailTextBox')
            
            if (emailObj != null)
            {
                var email = emailObj.value;
                
                if (email == '')
                {
                    errorText = errorText + '<li>Email is required</li>';
                    findItem('EmailRequired').style.visibility = 'visible';
                    findItem('EmailExpressionRequired').style.visibility = 'hidden';
                }
                else
                {
                  var objEmailRequired = findItem('EmailRequired');
                  
                  if (objEmailRequired != null)
                    objEmailRequired.style.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>';
                    findItem('EmailExpressionRequired').style.visibility = 'visible';
                  }
                  else
                    findItem('EmailExpressionRequired').style.visibility = 'hidden';
                }

                if (findItem('GiftCertificateFromTextBox').value == '')
                {
                    errorText = errorText + '<li>From Name is required</li>';
                    findItem('EmailFromRequired').style.visibility = 'visible';                
                }
                else
                    findItem('EmailFromRequired').style.visibility = 'hidden'; 

                if (findItem('GiftCertificateToTextBox').value == '')
                {
                    errorText = errorText + '<li>To Name is required</li>';
                    findItem('EmailToRequired').style.visibility = 'visible';
                }
                else
                    findItem('EmailToRequired').style.visibility = 'hidden'; 

            }
        }

        var mailRadioObj = findElement('GiftCertificateMailingRadioList');

        if (mailRadioObj != null)
        {
            if (findItem('GiftCertificateAddressLine1') != null)
            {

                if (findItem('GiftCertificateAddressLine1').value == '')
                {
                    errorText = errorText + '<li>Address Line 1 is required.</li>';
                    findItem('AddressLine1Required').style.visibility = 'visible';
                }
                else
                    findItem('AddressLine1Required').style.visibility = 'hidden';


                if (findItem('GiftCertificateCity').value == '')
                {
                    errorText = errorText + '<li>City is required.</li>';
                    findItem('CityRequired').style.visibility = 'visible';
                }
                else
                    findItem('CityRequired').style.visibility = 'hidden';


                ddl = findItem('GiftCertificateState')
                if (ddl.options[ddl.selectedIndex].value == '' || ddl.options[ddl.selectedIndex].value == '0')
                {
                    errorText = errorText + '<li>State is required.</li>';
                    findItem('StateRequired').style.visibility = 'visible';
                }
                else
                    findItem('StateRequired').style.visibility = 'hidden';


                if (findItem('GiftCertificateZipcode').value == '')
                {
                    errorText = errorText + '<li>ZipCode is required.</li>';
                    findItem('ZipcodeRequired').style.visibility = 'visible';
                }
                else
                    findItem('ZipcodeRequired').style.visibility = 'hidden';
                    
                ddl = findItem('GiftCertificateCountry')
                if (ddl.options[ddl.selectedIndex].value == '' || ddl.options[ddl.selectedIndex].value == '0')
                {
                    errorText = errorText + '<li>Country is required.</li>';
                    findItem('CountryRequired').style.visibility = 'visible';
                }
                else
                    findItem('CountryRequired').style.visibility = 'hidden';
            }

            // selected use address from order
            if (findItem('GiftCertificateFrom2TextBox').value == '')
            {
                errorText = errorText + '<li>From Name is required</li>';
                findItem('EmailFromRequired2').style.visibility = 'visible';
            }

            if (findItem('GiftCertificateTo2TextBox').value == '')
            {
                errorText = errorText + '<li>To Name is required</li>';
                findItem('EmailToRequired2').style.visibility = 'visible';
            }
        }

        findItem('GCErrorSpan').innerHTML = errorText;

        if (errorText == '')
        {
            findItem('GCErrorSpan').innerHTML = '';
            valid = true;
        }
        else
        {
            findItem('GCErrorSpan').innerHTML = '<br/>' + errorText;
            valid = false;
        }
    }
    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';

    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;
}