﻿
function ArrayIndexOf(arr, obj) {
  var i = arr.length;
  while (i--) {
    if (arr[i] === obj) {
      return i;
    }
  }
  return -1;
}

function ValidateDropdowns()
{
    var selects = document.getElementsByClassName('VerifySelection');
    var selectContainers = document.getElementsByClassName('VerifySelectionContainer');
    
    var customizationDropdowns = document.getElementsByClassName('CustomizationDropdown');
    var customizationTexts = document.getElementsByClassName('CustomizationText');
    
    var i;
    var allClear;
    
    var returnValue = 1;
    var bLongOrSweat = 0;
    var bGreaterThan3x = 0;
    
    var ErrorSpan = document.getElementById('ErrorText');
    ErrorSpan.innerHTML = '';
    
    for(i=0;i < selects.length;i++)
    {
        if(selects[i].selectedIndex == 0)
        {
            selectContainers[i].style.cssText='border: 2px solid red;'
            returnValue = 0;
        }
        
        if(selects[i].value.indexOf('Long Sleeve') > -1 || selects[i].value.indexOf('Sweatshirt') > -1)
        {
            bLongOrSweat = 1;
        }
        
        if(selects[i].value.indexOf('4X') > -1 || selects[i].value.indexOf('5X') > -1)
        {
            bGreaterThan3x = 1;
        }
    }
    
    if (returnValue == 0)
    {
        ErrorSpan.innerHTML = '* Please make a selection for all required product options.';
    }
    
    for(i=0;i < customizationDropdowns.length; i++)
    {
        if(customizationDropdowns[i].value.indexOf('No') != -1 && customizationTexts[i].value.length > 0)
        {
            if(ErrorSpan.innerHTML.length > 0){
                ErrorSpan.innerHTML += '<br />';
            }
            ErrorSpan.innerHTML += '* Customizations must be selected as &quot;Yes&quot; from the dropdowns';
            customizationDropdowns[i].style.cssText='border: 2px solid red;';
            returnValue = 0;
        }
    }
    
    if(bLongOrSweat == 1 && bGreaterThan3x == 1)
    {
        if (ErrorSpan.innerHTML == '')
        {
            ErrorSpan.innerHTML += '* ';
        }
        else
        {
            ErrorSpan.innerHTML += '<br />* ';
        }
        
        ErrorSpan.innerHTML += 'Long Sleeve or Sweatshirts not available larger than 3X.';
        returnValue = 0;
    }
    
    
    
    if(returnValue == 0)
    {
        ErrorSpan.style.cssText='color:Red;';
        
        var j;
        //for(j=0;j < selectContainers.length;j++)
        //{
        //    selectContainers[j].style.cssText='border: 2px solid red;'
        //}
    }
    else
    {
        //clear all the red borders
        var j;
        for(j=0;j < selectContainers.length;j++)
        {
            selectContainers[j].style.cssText='border: none;'
        }
    }
    
    return (returnValue == 1);
}

function RestrictLongSleeveMto2X()
{
    var selects = document.getElementsByClassName('VerifySelection');
    var selectContainers = document.getElementsByClassName('VerifySelectionContainer');
    var i;
    var allClear;
    
    var bLongSleeve = 0;
    var bRestrictedSizes = 0;
    
    var returnValue = true;
    
    var ErrorSpan = document.getElementById('ErrorText');
    
    for(i=0;i < selects.length;i++)
    {
        if(selects[i].value.indexOf('Long Sleeve') > -1)
        {
            bLongSleeve = 1;
        }
        
        if(selects[i].value.indexOf('S|') > -1 || selects[i].value.indexOf('3X') > -1)
        {
            bRestrictedSizes = 1;
        }
    }
    
    returnValue = !((bLongSleeve == 1) && (bRestrictedSizes == 1));
    
    if (!returnValue)
    {
        if(ErrorSpan.innerHTML.indexOf('Long Sleeve sizes limited to') == -1)    //only add this if it's not already there
        {
            //line break if there's already messaging
            if(ErrorSpan.innerHTML.length > 0)
            {
                ErrorSpan.innerHTML += '<br />';
            }
            
            ErrorSpan.innerHTML += '* Long Sleeve sizes limited to M - 2X';
        }
        
        ErrorSpan.style.cssText='color:Red;';
        
        var j;
        for(j=0;j < selectContainers.length;j++)
        {
            selectContainers[j].style.cssText='border: 2px solid red;'
        }
    }
    
    
    
    return returnValue;
}

//restrict the sizes that are not available for a product.
//use by declaring a javascript variable with the restricted size and sleeve length combos
//and adding this method to the onclick using an AND (&&)
//and also adding the "SleeveLength" class to the sleeve selector, and the "Size" class to the size selector
//<script type="text/javascript">var restrictSizeSleeveCombos='Long Sleeve=L';</script>
//onclick="return (ValidateDropdowns() && RestrictSpecificSizeSleeveCombos(restrictSizeSleeveCombos));" /> 

function RestrictSpecificSizeSleeveCombos(restrictedCombos)
{
    var returnValue = true;
    
    //parse the restricted  sleeve length and size combos, and make a lookup table
    //length and size combos will be pipe delimited, with length and size delimited by equals sign
    //example: Long Sleeve=S|Long Sleeve=3X|Hoodie=S 
   
    var splitCombos = restrictedCombos.split('|');
    var comboMap = {};
    for(i=0;i < splitCombos.length;i++)
    {
        var splitCombo = splitCombos[i].split('=');
        var sleeve = splitCombo[0];
        var size = splitCombo[1];
        
        if(typeof comboMap[sleeve] == 'undefined')  //check to see if this key exists in our map
        {
            //nope, add it with the size as a single member of a new array of restricted sizes
            comboMap[sleeve] = new Array();
            comboMap[sleeve][0] = size;
        }
        else
        {
            //this sleeve length exists, push the size onto its list of restricted values
            comboMap[sleeve].push(size);
        }
    }
    
    var sleeveSelect = document.getElementsByClassName('SleeveLength');
    var sizeSelect = document.getElementsByClassName('Size');
    
    //clear the red borders before we look for a match with our restricted list
    //so we don't persist an error that no longer exists
    sleeveSelect[0].style.cssText='border: solid 1px #878787;';
    sizeSelect[0].style.cssText='border: solid 1px #878787;';
    
    var selectedSleeve = sleeveSelect[0].value.split('|')[0];
    var selectedSize = sizeSelect[0].value.split('|')[0];
    
    //check to see if the selected sleeve length is in our restricted combo map
    if(typeof comboMap[selectedSleeve] != 'undefined')
    {
        //this sleeve length has a restricted size, lets see if 
        //the selected one is in our list
        if(-1 != ArrayIndexOf(comboMap[selectedSleeve],selectedSize))
        {
            //we have a match, this is a restricted combo
            //set the alert text, color, and make the sleeve 
            //and size select dropdowns red outlined
            var ErrorSpan = document.getElementById('ErrorText');
            var restrictedComboTextAlertStart = ErrorSpan.innerHTML.indexOf('* Sorry, this item is not available in');
            if(restrictedComboTextAlertStart != -1)
            {
                //if this message was already there, remove it, so we can re-add it below
                var restrictedComboTextAlertEnd = ErrorSpan.innerHTML.indexOf('.',restrictedComboTextAlertStart);
                ErrorSpan.innerHTML.replace(ErrorSpan.innerHTML.substring(restrictedComboTextAlertStart,restrictedComboTextAlertEnd),'');
            }
            
            //line break if there's already messaging
            if(ErrorSpan.innerHTML.length > 0)
            {
                ErrorSpan.innerHTML += '<br />';
            }
            
            ErrorSpan.innerHTML += '* Sorry, this item is not available in ' + selectedSleeve + ' - ' + selectedSize + '.';;
            
            
            ErrorSpan.style.cssText='color:Red;';
            sleeveSelect[0].style.cssText='border: 2px solid red;';
            sizeSelect[0].style.cssText='border: 2px solid red;';
            
            //finally, set the return value to false so we don't 
            //allow the user to add this item to the cart
            returnValue = false;
        }
    }
    
    
    return returnValue;
}





