  function trim(Str)
  {
    //remove leading and trailing spaces
    return(Str.replace(/^[\s]*/, '').replace(/[\s]*$/, ''));
  } //end trim

  function AddAttrb(Index, Prompt, Value)
  {
    if (this.Children.length == 0)
    {
      this.Children[0] = new Attrb('', 'NA', 0);
    }
    
    if (this.Children.length <= Index)
    {
      this.Children[Index] = new Attrb(Prompt, Value, Index);
    }
    return(this.Children[Index]);
  }//end AddAttrb
  
  function GetChildren()
  {
    var i;
    
    for (i = 0; i < this.Children.length; i++)
    {
      alert(this.Children[i].Value);
      this.Children[i].GetChildren();
    }
  }//end GetChildren
  
  function PopulateNextOptions(OptionIndex, OptionCount)
  {
    var i;
    var OptionObj;

    for (i = OptionIndex; i <= OptionCount; i++)
    {
      OptionObj = eval('document.isc.Option' + i);
      OptionObj.options.length = 1;
      OptionObj.options[0].selected = true;
      OptionObj.options.selectedIndex = 0;
    }
    
    OptionObj = eval('document.isc.Option' + OptionIndex);
    for (i = 1; i < this.Children.length; i++)
    {
      OptionObj.options[i] = new Option(this.Children[i].Prompt, this.Children[i].Value);
    }
     
    if (OptionIndex == 2)
    {
      SetImage(this.Index);
    }
  }//end PopulateNextOptions

  function Attrb(Prompt, Value, Index)
  {
    //add methods to the class
    this.Add = AddAttrb;
    this.GetChildren = GetChildren;
    this.PopulateOptions = PopulateNextOptions;
    this.PopulateOptionsForMultiSkus = PopulateNextOptionsForMultiSkus;
    
    //add properties to the class
    this.Prompt = Prompt;
    this.Value = Value;
    this.Index = Index;
    this.Children = new Array();
  }//end Attrb

  function SetImage(ImageIndex)
  {
    var ThumbObj;
    var TxtObj = document.getElementById('Swatch');
    
    //check if swatching is active for this product
    //if active, change current image & swatch description
    if (TxtObj && ImageIndex != 0)
    {
      ThumbObj = document.getElementById('ThumbImg' + ImageIndex);
      document.DfltImg.src = DfltImg[ImageIndex].src;
      TxtObj.innerHTML = 'Shown: ' + RootAttrb.Children[ImageIndex].Prompt;
      ThumbObj.style.borderStyle = 'solid';
    
      if (CurrentImageIndex)
      {
        ThumbObj = document.getElementById('ThumbImg' + CurrentImageIndex);
        ThumbObj.style.borderStyle = 'none';
      }
    
      CurrentImageIndex = ImageIndex;
    }
  }//end SetImage
  
  function SetAttribute(AttrbIndex, NumberOfFamilies)
  {
      document.isc.Option1.options[0].selected = true;
      document.isc.Option1.selectedIndex = AttrbIndex;
      
      if (NumberOfFamilies > 1)
      {
        RootAttrb.Children[document.isc.Option1.options.selectedIndex].PopulateOptions(2, NumberOfFamilies)
      }
      else
      {
        SetImage(AttrbIndex);
      }
  }//end SetAttribute
  
  function SetNoThumbImage(Img)
  {
    Img.src = 'http://www.levimage.com/IMAGE/Navigation/Product/NoImage_CST.gif';
  }// end SetNoThumbImage
  
  function SetNoDfltImage(Img)
  {
    Img.src = 'http://www.levimage.com/IMAGE/Navigation/Product/NoImage_CSW.gif';
  }// end SetNoDfltImage
  
  function CheckDataEntered(rootsku)
  {
    var OptObj;
    var i = 1;
    
    document.isc.ITN.value = rootsku
    
    while ((OptObj = eval('document.isc.Option' + i++)))
    {
      if (!OptObj.selectedIndex)
      {
        alert("Selection Required");
        OptObj.focus();
        return (false);
      }
      else
      {
        document.isc.ITN.value = document.isc.ITN.value + ' ' + OptObj.options[OptObj.selectedIndex].value;
      }
    }

    // check if the user has selected the "Other" option without entering data in the shipping name field
    // note: document.isc.ShipTo.length-1 is the "Other" selection option
    
    document.isc.ShipToFirstName.value = trim(document.isc.ShipToFirstName.value);
    if (document.isc.ShipTo[document.isc.ShipTo.length-1].checked &&
           (document.isc.ShipToFirstName.value == ""))
    {
      // the user has selected the Other option without entering any data in the shipping name field
      alert("If you are shipping this to someone else, you must enter a name");
      document.isc.ShipToFirstName.focus();
      return (false);
    }
    
    document.isc.RootSkuImage.value = document.DfltImg.src;
    return(true);
  } //end function CheckDataEntered()

  function addDynamicImage()
  {	
    document.SubmitProductReview.imageURL.value = document.DfltImg.src;
    return true;
  } //end function addDynamicImage()

  function SubmitProductReviewForm()
  {
    if (addDynamicImage())
    {
      document.SubmitProductReview.submit();
    }
  } //end function SubmitProductReviewForm()
  
  function SubmitAddReviewForm()
  {
      document.SubmitProductReview.submit();
   
  } //end function SubmitProductReviewForm()
  
  function SetOtherSelected()
  {
    document.isc.ShipTo[document.isc.ShipTo.length-1].checked = true;
    document.getElementById("ShipToInstructions").style.display = "block";
  } //end SetOtherSelected
  
  function SetToOther()
  {
    document.getElementById("ShipToFirstName").focus();
    document.getElementById("ShipToInstructions").style.display = "block";
  } //end SetToOther
  
  function ClearOther()
  {
    document.isc.ShipToFirstName.value = ""
  } //end ClearOther
  
  function ChangeOptions()
  {
	if (document.isc.ShipTo[document.isc.ShipTo.selectedIndex].value=='Other')
    {
     return SetToOther();
    }
    else
    {
     return ClearOther();
    }
  } //end ChangeOptions
  

  var RootAttrb = new Attrb('','');
  var DfltImg = new Array();
  var CurrentImageIndex = 0;
  

//***********************MultiSku Functions****************************

  function PopulateNextOptionsForMultiSkus(OptionIndex, OptionCount, SkuCount)
  {
    var i;
    var OptionObj;
    
    for (i = OptionIndex; i <= OptionCount; i++)
    {
      OptionObj = eval('document.isc.Option' + i + '_' + SkuCount);
      OptionObj.options.length = 1;
      OptionObj.options[0].selected = true;
      OptionObj.options.selectedIndex = 0;
    }
    
    OptionObj = eval('document.isc.Option' + OptionIndex + '_' + SkuCount);
    for (i = 1; i < this.Children.length; i++)
    {
      OptionObj.options[i] = new Option(this.Children[i].Prompt, this.Children[i].Value);
    }
    if (OptionIndex == 2)
    {
      SetImageForMultiSkus(this.Index, SkuCount);
    }
  }//end PopulateNextOptionsForMultiSkus
  
  function SetImageForMultiSkus(ImageIndex, SkuCount)
  {
    var SwatchThumbObj;
    var SwatchObj = document.getElementById('Swatch'+ SkuCount);
    var AlternateObj = document.getElementById('AlternateImg'+ SkuCount);
    
    //check if swatching is active for this product
    //if active, change current image & swatch description
    if (SwatchObj && ImageIndex != 0)
    {
      SwatchThumbObj = document.getElementById('SwatchThumbImg' + ImageIndex + '_' + SkuCount);
      SwatchObj.innerHTML = eval('RootAttrb' + SkuCount).Children[ImageIndex].Prompt;
      AlternateObj.src = eval('DfltImg' + SkuCount + '[' + ImageIndex + ']').src;
      SwatchThumbObj.style.borderStyle = 'solid';
    
      if (CurrentImageIndex)
      {
        SwatchThumbObj = document.getElementById('SwatchThumbImg' + CurrentImageIndex + '_' + SkuCount);
        SwatchThumbObj.style.borderStyle = 'none';
      }
    
      CurrentImageIndex = ImageIndex;
    } 
  }//end SetImageForMultiSkus
  
  function SetImageandTextForMultiSkus(Index)
  {
    var ThumbObj = document.getElementById('ThumbImg' + Index);
    var TxtObj = document.getElementById('ProductCopy');
    var ZoomObj = document.getElementById('ZoomImg');
    var ThumbNailDescObj = document.getElementById('ThumbDesc')
    
   //Check if there is a thumbnail for this product, if there is, then set the default image, zoom image,
   // and copy.
   
    if (ThumbObj)
    {
     document.DfltImg.src = multiskudfltimagearr[Index];
      if (ZoomObj)
      {
        if (multiskuzoomimagearr[Index] == '')
        {
          ZoomObj.style.visibility = 'hidden';
        }
        else
        { 
          ZoomObj.style.visibility = 'visible';
          ZoomObj.onclick = function(){openWindow(multiskuzoomimagearr[Index])};
        }
      }
	  if (TxtObj)
	  {
		TxtObj.innerHTML = multiskucopyarr[Index];
	  }
	  if (ThumbNailDescObj)
      {
		ThumbNailDescObj.innerHTML = 'Shown: ' + multiskudescarr[Index];
	  }
	}
  }//end SetImageandTextForMultiSkus
  
  function SetAttributeForMultiSkus(AttrbIndex, NumberOfFamilies, SkuCount)
  {
      OptionObj = eval('document.isc.Option1_' + SkuCount);
      OptionObj.options[0].selected = true;
      OptionObj.selectedIndex = AttrbIndex;
      
      
      if (NumberOfFamilies > 1)
      {
        eval('RootAttrb'+ SkuCount).Children[OptionObj.options.selectedIndex].PopulateOptionsForMultiSkus(2, NumberOfFamilies, SkuCount)
      }
      else
      {
        SetImageForMultiSkus(AttrbIndex, SkuCount);
      }
  }//end SetAttributeForMultiSkus
  
  function CheckMultiSkuDataEntered()
  {
    var OptObj;
    var i, countsku, rootsku, formattedsku;
    
    countsku = 0;
    document.isc.skus.value = '';

    //Iterate through all the form elements untill 'txtqty' is found and is not equal to zero.
    for (i = 0; i < document.isc.elements.length; i++) 
    {
		if (document.isc.elements[i].type == 'text' && document.isc.elements[i].name.substring(0,6) == 'txtQTY') 
		{
		  countsku = countsku + 1;
		  if (document.isc.elements[i].value != 0)
		  { 
		    rootsku = document.getElementById('hdnSKU' + countsku).value;
		    formattedsku = rootsku;
			var j = 1;
		   // while ((OptObj = eval('document.isc.' + rootsku + '_Option' + j++)))
		    while ((OptObj = eval('document.isc.Option' + j++ + '_' + countsku)))
			{
				if (!OptObj.selectedIndex)
				{    
					alert("Selection Required");
					OptObj.focus();
					return (false);
				}
				else
				{   
					formattedsku = formattedsku + ' ' + OptObj.options[OptObj.selectedIndex].value;
				}
			}
			document.isc.skus.value = document.isc.skus.value + '|' + ZeroPad(document.isc.ShipTo.selectedIndex, 2) + ':' + formattedsku + ':' + ZeroPad(document.isc.elements[i].value, 2)
		  }
		}
	}
	
	// Remove the leading '|'
	document.isc.skus.value = document.isc.skus.value.substring(1)

	if (document.isc.ShipTo[document.isc.ShipTo.selectedIndex].value=='Other')
    {
		document.isc.ShipTos.value =  ZeroPad(document.isc.ShipTo.selectedIndex, 2) + ':' + document.isc.ShipToFirstName.value
	}
	else
	{
		document.isc.ShipTos.value =  ZeroPad(document.isc.ShipTo.selectedIndex, 2) + ':' + document.isc.ShipTo[document.isc.ShipTo.selectedIndex].text
	}
	
	if (document.isc.skus.value == '')
	{
	  alert('Please enter a quantity');
	  return(false);	
    }
    
    // check if the user has selected the "Other" option without entering data in the shipping name field
    // note: document.isc.ShipTo.length-1 is the "Other" selection option
    
    document.isc.ShipToFirstName.value = trim(document.isc.ShipToFirstName.value);
    if (document.isc.ShipTo[document.isc.ShipTo.length-1].checked &&
           (document.isc.ShipToFirstName.value == ""))
    {
      // the user has selected the Other option without entering any data in the shipping name field
      alert("If you are shipping this to someone else, you must enter a name");
      document.isc.ShipToFirstName.focus();
      return(false);
    }
    
    //document.isc.RootSkuImage.value = document.DfltImg.src;
    return(true);
  } //end function CheckMultiSkuDataEntered
  
  function SetOtherSelectedDD()
  {
    document.isc.ShipTo[document.isc.ShipTo.length-1].selected = true;
    document.getElementById("ShipToInstructions").style.display = "block";
  } //end SetOtherSelectedDD
  
  function ZeroPad(num,count)
  { 
	var numZeropad = num + '';
	while(numZeropad.length < count)
	{
      numZeropad = "0" + numZeropad; 
	}
	return numZeropad;
  } //end ZeroPad
  
  function SetNoThumbImageForMultiSkus(Img)
  {
    Img.src = 'http://www.levimage.com/IMAGE/Navigation/Product/NoImage_CST_57x57.jpg';
  }// end SetNoThumbImageForMultiSkus
  
  function SetNoSwatchImageForMultiSkus(Img)
  {
    Img.src = 'http://www.levimage.com/IMAGE/Navigation/Product/NoImage75x75.jpg';
  }// end SetNoSwatchImageForMultiSkus
  
  function SetNoDetailImageForMultiSkus(Img)
  {
    Img.src = 'http://www.levimage.com/IMAGE/Navigation/Product/NoImage152x250.jpg';
  }// end SetNoDetailImageForMultiSkus
  
  function OpenDetailPopUp(Index, RootSkuGroupID)
  {
    var AlternateObj = document.getElementById('AlternateImg'+ Index)
  
  //Store each of the RootskugroupID and Alternate Imagename in a cookie and open a popup window to view details
  
    document.cookie="RootSkuGroupID="+ RootSkuGroupID+";path=/;domain=.levenger.com;";
    document.cookie ="ImageName="+ AlternateObj.src+";path=/;domain=.levenger.com;";
    openWindow('/PopUps/Product/DetailImage.asp');
  } //end OpenDetailPopUp