/*
  Displays the table containing the large image and thumbs
*/
  function showItemDetailImages(anId, onNumber, maxNumber, optionId, aPath, aZoomPath, aSelectNumber) {    
    for (a=0; a<maxNumber; a++) {
      e=document.getElementById(anId + "_" + a);
      if (e != null) {
        if (a == onNumber && onNumber >= 0) {          
          e=document.getElementById("mainImage" + a);
          e.className = "onlyHidden";
          switchItemDetailMainImage("mainImage" + a, aPath, aZoomPath);
          e=document.getElementById("mainImage" + a);
          e.className = "onlyHidden";
                  /*
                  // Set color variance
                  e=document.getElementById("colorOptionToBB");
                  e.value = optionId;
                  */
          e=document.getElementById("varianceColorImage" + onNumber);
          if (e != null) {
            e.className = "borderVarianceboxActive";
          }  
          e=document.getElementById("currentColorVariance" + onNumber);
          if (e != null) {
            e.className = "itemValues visible";
          } 
                  //e=document.getElementById("currentColorVarianceDisplay");
                  //e.innerHTML = optionId;
          e=document.getElementById("currentColorVarianceDisplay" + onNumber);
          if (e != null) {
            e.className = "itemValues visible";
          } 
          e=document.getElementById(anId + "_" + a);
          e.className = "visible"; 
          e=document.getElementById("mainImage" + a);
          e.className = "onlyVisible";                      
        } 
        else {
          if (onNumber >= 0) {
            e.className = "hidden";
          }
          e=document.getElementById("varianceColorImage" + a);
          if (e != null) {
            if (a != aSelectNumber) {
              e.className = "borderVariancebox";
            } else {
              e.className = "borderVarianceboxActive";
            }
          }
          if (onNumber > 0) {
            e=document.getElementById("currentColorVariance" + a);
            if (e != null) {
              e.className = "itemValues hidden";
            }
            e=document.getElementById("currentColorVarianceDisplay" + a);
            if (e != null) {
              e.className = "itemValues hidden";
            }    
            var theImageId = "mainImage" + a;
            if (mainImageId == theImageId) {
              e=document.getElementById("mainImage" + a);
              //e.src = "/images/spacer.gif";
              e.className = "onlyHidden";
              /*
              if (aPath == aZoomPath) {
                mainImageId = "mainImage";
              } 
              */                
            }   
            e=document.getElementById("mainImage" + a);        
            e.className = "onlyHidden";    
          }     
        }
      }
    }
    //alert("!");
  }

/*
  Path variable for the large image
*/
  var currentPath = new String();

/*
  Switches the large image over to the image provided
*/
  function switchItemDetailMainImage(anId, aPath, aZoomPath) {    
    e=document.getElementById(anId);    
    if (e != null) {
      //e.className = "onlyHidden";
      currentPath = aZoomPath;
      e.src = aPath;
      if (aPath != aZoomPath) {
        mainImageId = anId;
      }
      //e.className = "onlyVisible";
    }
    //alert("!");
  }
  
/*
  Saves the zoomImage path 
*/
  function changeCurrentPath(aZoomPath) {
    currentPath = aZoomPath;
  }
  
/*
  Popup Zoom window for the large image and view larger image button
*/  
  function openZoomWindow(categoryIn, itemIn, onNumber) {
    window.open('zoom.jsp?zoomPath=' + currentPath + '&category=' + categoryIn + '&item=' + itemIn,'_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=n0, width=800, height=900')
  }    
  
/*
  Show the sizes
*/
  function showItemDetailSizes(anId, onNumber, maxNumber, optionId, aCurrent, aSelectNumber) {
    //e=document.getElementById(optionId);       
      for (a=0; a<maxNumber; a++) {
        e=document.getElementById(anId + a);
        if (e != null) {
          if (a == onNumber) {
            e.className = "sizeVarianceClass borderVarianceboxActive";  
            /*
            // Set size
            e=document.getElementById("sizeOptionToBB");
            e.value = optionId;
            */   
            //e=document.getElementById("currentSizeVariance" + a);
            //e.className = "itemValues visible";
            //e=document.getElementById("currentSizeVarianceDisplay" + a);
            //e.className = "itemValues visible";     
            //e=document.getElementById(anId + a);                               
          }
          else {
            if (a != aSelectNumber) {
              e.className = "sizeVarianceClass borderVariancebox";
            } else {
              e.className = "sizeVarianceClass borderVarianceboxActive";
            }
            //e=document.getElementById("currentSizeVariance" + a);
            //e.className = "itemValues hidden";
            //e=document.getElementById("currentSizeVarianceDisplay" + a);
            //e.className = "itemValues hidden";                                  
          }
        }
     }  
  }
/*
  Show the scents
*/
  function showItemDetailScents(anId, onNumber, maxNumber, optionId, aCurrent, aSelectNumber) {
    //e=document.getElementById(optionId);       
      for (a=0; a<maxNumber; a++) {
        e=document.getElementById(anId + a);
        if (e != null) {
          if (a == onNumber) {
            e.className = "sizeVarianceClass borderVarianceboxActive";  
            /*
            // Set size
            e=document.getElementById("sizeOptionToBB");
            e.value = optionId;
            */   
            //e=document.getElementById("currentSizeVariance" + a);
            //e.className = "itemValues visible";
            //e=document.getElementById("currentSizeVarianceDisplay" + a);
            //e.className = "itemValues visible";     
            //e=document.getElementById(anId + a);                               
          }
          else {
            if (a != aSelectNumber) {
              e.className = "sizeVarianceClass borderVariancebox";
            } else {
              e.className = "sizeVarianceClass borderVarianceboxActive";
            }
            //e=document.getElementById("currentSizeVariance" + a);
            //e.className = "itemValues hidden";
            //e=document.getElementById("currentSizeVarianceDisplay" + a);
            //e.className = "itemValues hidden";                                  
          }
        }
     }  
  }
  
  function updateItemDetailVarianceLabel(anId, aString) {
    e=document.getElementById(anId);
    if (e != null) {
      e.innerHTML = aString;
    }
    e=document.getElementById(anId + "_");
    if (e != null) {
      e.innerHTML = aString;
    }    
    e=document.getElementById((anId + "Display"));
    if (e != null) {
      e.innerHTML = aString;
    }
  }
  
  function toggleCheckbox(anId, aTextArea) {
    e=document.getElementById(anId);
    if (e != null) {
      //e.checked = !e.checked;
      if (!e.checked) {  
        e=document.getElementById(aTextArea);  
        if (e != null) {
          e.className="hidden";
          e.value = "";          
        }  
      }
      else {
        e=document.getElementById(aTextArea);
        if (e != null) {  
          e.className="visible";
        }      
      }
    }
  }
  
  function toggleQuantity(anId, aQty, aCategory, aProductId, anAddnlText) {
    e=document.getElementById(anId);
    if (e != null) {
      //e.checked = !e.checked;
      if (!e.checked) {      
        e=document.getElementById(aQty);  
        if (e != null) {
          e.name = "someB1quantity";
          e.value = "0";
        }
        e=document.getElementById(aCategory);
        if (e != null) {
          e.name = "someB1quantity";
        }        
        e=document.getElementById(aCategory);
        if (e != null) {
          e.name = "someB1category";
        }        
        e=document.getElementById(aProductId);
        if (e != null) {
          e.name = "someB1productId";
        }        
        e=document.getElementById(anAddnlText);
        if (e != null) {
          e.name = "someB1addnlText";
        }          
      }
      else {
        e=document.getElementById(aQty);  
        if (e != null) {
          e.name = "B1quantity";
          e.value = "1";
        }
        e=document.getElementById(aCategory);
        if (e != null) {
          e.name = "B1quantity";
        }        
        e=document.getElementById(aCategory);
        if (e != null) {
          e.name = "B1category";
        }        
        e=document.getElementById(aProductId);
        if (e != null) {
          //e.name = "B1productId";
          e.name = "B1itemId";
        }        
        e=document.getElementById(anAddnlText);
        if (e != null) {
          e.name = "B1addnlText";
        }   
      }      
    } 
  }
  
  function checkTextArea(anId) {
    e=document.getElementById(anId);
    if (e != null) {
      if (e.value.length > 250) {
        alert("Sorry, 250 character limit.");
      }
    }    
  }
  
  /*
    Hides the first, shows the second
  */
  function hideShow(anId, anId2) {
    e=document.getElementById(anId);
      if (e != null) {
        e.className = "hidden";
        anId = anId.substring(0, anId.length - 3);
        e=document.getElementById(anId);    
        if (e != null) {
          if (anId == "state") {
            e.value = "--";
          }
          else {
            e.value = "";
          }
        }
      }
    e=document.getElementById(anId2);
      if (e != null) {
        e.className = "visible";
      }      
  }
  
  function countryChange() {
    e=document.getElementById("country");
    if ( e!= null) {
      if (e.value == "US") {
        hideShow("provinceTR", "stateTR");
        hideShow("provinceTR2", "stateTR2");
      }
      else {
        hideShow("stateTR", "provinceTR");
        hideShow("stateTR2", "provinceTR2");
      }
    }
  }
/*  
  function addItems(anItem) {
    if (document.getElementsByName("B1addnlText") != null) { 
      document.getElementById("B1addnlText").value = anItem + "|" + document.getElementById("addnlText").value;
    } 
    return true;
  }
*/  
  function addItemsWithVariance(anItem) {
    elements = document.getElementsByName("B1addnlText");
    text = elements[0];
    if (text != null) { 
      text.value = anItem;
      e=document.getElementById("currentSizeVariance");
      if (e != null) {
        text.value = text.value + "_" + e.innerHTML;
      }
      e=document.getElementById("currentColorVariance");
      if (e != null) {
        text.value = text.value + "_" + e.innerHTML;
      }
      text.value = text.value + "|" + document.getElementById("addnlText").value;
    } 
    return true;
  }   
  
  function addGiftWrapToAddnlData() {
    e=document.getElementById("addnlText");
    at=document.getElementById("addnlText").value;
    gw=document.getElementById("giftwrap").checked;
    if (gw) {
      e.value="Y-";
    } else {
      e.value="N-";
    }
    e.value += at;
    
    return true;
  } 
  
  function UpdateGiftWrapOnBasket(aBasketKey, aProductId, aMessage) {
    e=document.getElementById("giftWrap" + aBasketPos);
    if (e != null) {
      if (e.checked == true) {
        // add item -- Needs to have basket sorted
        // window.location.href="/servlet/com.beantree.catalog.BasketItemDeleteServlet?basketItem=" + aBasketKey + "&amp;path=" + aTargetPath;
      }
      else {
        //remove item
        window.location.href="/servlet/com.beantree.catalog.BasketItemDeleteServlet?basketItem=" + aBasketKey + "&amp;path=" + catSession.getSecurePath();
      }
    }
  }
  
  // allows 0-9 only
  function checkForNumericKey(e) { // e is an event  
    if (e != null) {
      var keynum
      var keychar
      var numcheck
      
      if(window.event) // IE
      {
      keynum = e.keyCode
      }
      else if(e.which) // Netscape/Firefox/Opera
      {
      keynum = e.which
      }
      keychar = String.fromCharCode(keynum)
      numcheck = /\d/
      return numcheck.test(keychar)
    }
  }  
  
  function showIconLegend() {
    window.open('icons.jsp','_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=300, height=565')
  }

/*
  keeps track of the large/zoom image path
*/
  var imageSrcString = "";
  
  function loadImages(anImageString) {
    imageSrcString = anImageString;
    var pos = 0;
    var imageSrc = "";
    while (pos != -1) {
      pos = anImageString.indexOf(",");
      if (pos != -1) {
        imageSrc = anImageString.substring(pos);
        var image = new Image();
        image.src = imageSrc;
        anImageString = anImageString.substring(pos + 1);
      }
    }
  }
  
/*
  returns the path of a single image  
*/  
  function getImage(aNumber) {
    var anImageString = imageSrcString;
    var aString = "";
    var pos = 0;
    var imageSrc = "/images/spacer.gif";
    var i = 0;
    while (pos != -1) {
      pos = anImageString.indexOf(",");
      if (pos != -1) {
        if (i == aNumber) {
          imageSrc = anImageString.substring(pos);
        }
        anImageString = anImageString.substring(pos + 1);
        i++;
      }
    }  
    return imageSrc;
  }
  
/*
  Which large/zoom image needs updating
*/
  var mainImageId = "mainImage";

/*
  Check Email
*/
  function checkEmail(anId) {
    var e = document.getElementById(anId);
    var ok = false;
    if (e != null) {
      if (e.value.indexOf(".") != -1 && e.value.indexOf("@") != -1) {
        ok =  true;
      }
    }
    return ok;
  }

/*
  Scrub Search
*/
  function checkSearch(anId) {
    ok = false;
    term = document.getElementById(anId);
    if (term != null && term.value.length > 0) {
      var stuff = new RegExp("[^a-zA-Z0-9_-]+", "g");
      term.value = term.value.replace(stuff," ");
      //term.value = term.value + "*";
      ok = true;
    }
    return ok;
  }

// load reviews
  function loadReviews(aURL) {    
    var div = $('loadReviews');
    if (div != null) {
      if (!div.hasClass('hasLoaded')) {
        try {
          div.load(aURL);
          new Request({
            url: aURL,
            noCache: 'true',
            onSuccess: function(data) {
              div.set('html', data);
            },
            onFailure: function(xhr) {
              div.set('html','<div>Error: '+xhr+'</div>');
            },
            onException: function(headerName, value) {
              div.set('html','<div>Error: '+headerName+' '+value+'</div>');
            }
          }).send();
        } catch (error) {
          div.set('html','An Error Has Occurred! ' + error);
        }
        div.setStyle('opacity',0);
        div.setStyle('display','block');
        div.addClass('hasLoaded');
        div.addEvent('click', function() {
          div.fade('out');
        });
      }
      div.fade('in');
      //new Fx.Scroll($('productReviewsAnchor')).toElement(div);
    }    
  }

function tabSwitcher(tab) {
  var tabId = tab.id;
  
  if (tabId == 'title1') {
    changeDivDisplay('content3', 'none');
    changeDivDisplay('content2', 'none');
    changeDivDisplay('content1', 'block');
    changeBackground('headerRow', 1);
    changeFont('title1', 'bold');
    changeFont('title2', 'normal');
    changeFont('title3', 'normal');
  } else if (tabId == 'title2') {
    changeDivDisplay('content1', 'none');
    changeDivDisplay('content3', 'none');
    changeDivDisplay('content2', 'block');
    changeBackground('headerRow', 2);
    changeFont('title1', 'normal');
    changeFont('title2', 'bold');
    changeFont('title3', 'normal');
  } else {
    changeDivDisplay('content1', 'none');
    changeDivDisplay('content2', 'none');
    changeDivDisplay('content3', 'block');
    changeBackground('headerRow', 3);
    changeFont('title1', 'normal');
    changeFont('title2', 'normal');
    changeFont('title3', 'bold');
  }  
}

function changeDivDisplay(theDiv, divChange) {
	var theStyle = getStyleObject(theDiv);
	if (theStyle != false) {
		theStyle.display = divChange;
	}
}

function changeBackground(theDiv, tabNumber) {
	var theStyle = getStyleObject(theDiv);
	if (theStyle != false) {
	  if (tabNumber == 1) {
      theStyle.backgroundImage = "url(/images/tab1of3.gif)";	
    } else if (tabNumber == 2) {
      theStyle.backgroundImage = "url(/images/tab2of3.gif)";	
    } else {
      theStyle.backgroundImage = "url(/images/tab3of3.gif)";	
    }
  }
}

function changeFont(theDiv, weight) {
	var theStyle = getStyleObject(theDiv);
	if (theStyle != false) {
    theStyle.fontWeight = weight;
    if (weight == 'bold') {
      theStyle.color = '#97597E';
    } else {
      theStyle.color = '#A09C6F';
    }
  }
}

function getStyleObject(objectId) {
	if (document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId).style;
	} else {
		return false;
	}
}

function moveContent (divMoved, amtMoved){

      var divId = document.getElementById(divMoved);
      var divTop = document.getElementById(divMoved).style.marginTop;

      divTop = divTop.substring(0,divTop.length - 2);

      var divShift = amtMoved;
      var divShiftNum = parseInt(divShift);

      divTop = parseInt(divTop) + divShiftNum;
      
      var divHeight = divId.offsetHeight;
      var divHeightNum = parseInt(divHeight);
      var negHeight = 74 - divHeightNum;
      //this number (74) comes from the height of the content area (same as height of its background image, area.gif)
      
      if (divHeightNum < 74){
      }
      
      else {
            
            if (divTop + 10 > 0) {
             if (divShiftNum = 10) {
             divId.style.marginTop = 0;
             }
            }
            
            else if (divTop - 10 < negHeight){
             if (divShiftNum = -10) {
             divId.style.marginTop = negHeight;
             }
            }
            
            else {
             divId.style.marginTop = divTop;
            }
            
      } 
      

}

var scrollBool = 0;

function startScroll (divMoved2, amtMoved2) {
	scrollBool = setInterval ( function() { moveContent(divMoved2, amtMoved2); }, 80 );
}

function stopScroll () {
	clearInterval ( scrollBool );
}    

