/*
 * Class :        SKUSpec
 *
 * Description :  Javascript Class to define possible SKU Specification/Values.
 *
 * Notes :
 *
 */
function SKUSpec( pstrItem, pstrSpecId, pstrSpecCode, pintSpecCodeOrder )
{

    function SpecValue( pstrSKUID, pstrSpecValue, pintSpecValueOrder, pintQty )
    {
      /*
       * Class :        Attribute
       *
       * Description :  Class to define an individual SKU Attribute.
       *
       * Notes :
       *
       */
      // Define public variables
      this.SKUId          = pstrSKUID;
      this.SpecValue      = pstrSpecValue;
      this.SpecValueOrder = pintSpecValueOrder;
      this.Qty            = pintQty;
    }


    this.AddSpecValue = function( pstrSKUID, pstrSpecValue, pintSpecValueOrder, pintQty ) {
      /*
       * Method :       AddSpecValue
       *
       * Description :  Method to add a SKU Spec Value.
       *
       * Notes :
       *
       */
      this.SpecValues[intSpecValueCtr++]  = new SpecValue( pstrSKUID, pstrSpecValue, pintSpecValueOrder, pintQty );

      return true;
    }


    this.SpecId         = pstrSpecId;
    this.SpecCode       = pstrSpecCode;
    this.SpecCodeOrder  = pintSpecCodeOrder;

    var intSpecValueCtr = 0;
    this.SpecValues     = new Array();

}



/*
 * Class :        SKUSpecHandler
 *
 * Description :  Javascript Class for handling Item SKU Specifications/Values.
 *
 * Notes :
 *
 */
function SKUSpecHandler( pobjSKUSpecs )
{

    this.BuildList = function( pobjDDL ) {
      /*
       * Method :       BuildList
       *
       * Description :  Entry point method to determining what data gets loaded in what drop
       *                Down List.
       *
       * Notes :
       *
       */
      var strSKUFilter    = "";
      var strCurSpecCode  = "";
      var intCounter      = 0;

      pobjSKUSpecs.SortByProperty("SpecCodeOrder");


      if ( !pobjDDL || pobjDDL == null || pobjDDL == "" )
      {
        objCurSKUSpec = this.SKUSpecs.GetAt(0);
      }
      else
      {
        strSKUFilter  = this.BuildSKUFilter(pobjDDL.name);
        objCurSKUSpec = this.SKUSpecs.GetAt(pobjDDL.name);
        objCurSKUSpec = this.SKUSpecs.GetNext();

        if ( objCurSKUSpec == null )
        {
          objSKUId = document.getElementById("skuid");
          objSKUId.value = strSKUFilter.TrimComma();
          return;
        }

      }


      /*
       * Remove all child drop down lists.
       */
      this.ClearLists(objCurSKUSpec.SpecCode);

      pobjDDL = document.getElementById(objCurSKUSpec.SpecCode);
      pobjDDL.options[intCounter++] = new Option("----","",true,true);

      objCurSKUSpec.SpecValues.SortByProperty("SpecValueOrder");
      for( specvalue in objCurSKUSpec.SpecValues )
      {
        if(typeof (objCurSKUSpec.SpecValues[specvalue]) == "object" )
        {

          if ( strSKUFilter == "" )
          {
            if ( objCurSKUSpec.SpecValues[specvalue].Qty > 0 )
            {
              if ( !ItemExists( objCurSKUSpec.SpecValues[specvalue].SpecValue, pobjDDL ) )
              {
                pobjDDL.options[intCounter++] = new Option(objCurSKUSpec.SpecValues[specvalue].SpecValue, objCurSKUSpec.SpecValues[specvalue].SpecValue);
              }
            }
          }
          else
          {
            if ( strSKUFilter.search( objCurSKUSpec.SpecValues[specvalue].SKUId ) >= 0 )
            {
              if ( objCurSKUSpec.SpecValues[specvalue].Qty > 0 )
              {
                if ( !ItemExists( objCurSKUSpec.SpecValues[specvalue].SpecValue, pobjDDL ) )
                {
                  pobjDDL.options[intCounter++] = new Option(objCurSKUSpec.SpecValues[specvalue].SpecValue, objCurSKUSpec.SpecValues[specvalue].SpecValue);
                }
              }
            }
          }
        }
      }

      //This is a catch for the case where there is only one attribute
      //list to select from.
      /*
      if ( this.SKUSpecs.GetNext() == null )
      {
        document.forms[0].skuid.value = strSKUFilter.TrimComma();
        alert( document.forms[0].skuid.value );
        return;
      }
      */

    }


    this.BuildSKUFilter = function(pstrSKUSpec)
    {
      var strSKUList    = [];
      var strTmpSKUList = [];
      var objCurDDL     = null;
      var intSKUListCtr = 0;


      var objSKUSpec = this.SKUSpecs.GetAt( pstrSKUSpec );
      for( specvalue in objSKUSpec.SpecValues )
      {
        if(typeof (objSKUSpec.SpecValues[specvalue]) == "object" )
        {
          objCurDDL = document.getElementById(objSKUSpec.SpecCode);

          if ( objCurDDL.options[objCurDDL.selectedIndex].value == objSKUSpec.SpecValues[specvalue].SpecValue )
          {
            strSKUList[intSKUListCtr++] = objSKUSpec.SpecValues[specvalue].SKUId;
          }
        }
      }

      /*
       * Need to subtract from the list as opposed to add to the list.  try making the filter list an array...
       */
      while ( objSKUSpec = this.SKUSpecs.GetPrevious() )
      {
        intSKUListCtr = 0;
        for( specvalue in objSKUSpec.SpecValues )
        {
          if(typeof (objSKUSpec.SpecValues[specvalue]) == "object" )
          {
            objCurDDL = document.getElementById(objSKUSpec.SpecCode);

            if ( objCurDDL.options[objCurDDL.selectedIndex].value == objSKUSpec.SpecValues[specvalue].SpecValue )
            {
              strTmpSKUList[intSKUListCtr++] = objSKUSpec.SpecValues[specvalue].SKUId;
            }
          }
        }

        strSKUList = DeltaSKUList( strSKUList, strTmpSKUList );
        strTmpSKUList = [];
      }


      return strSKUList.toString();
    }

    function DeltaSKUList( pstrSKUList, pstrTmpSKUList )
    {
      var strTmpSKUList = pstrTmpSKUList.toString();

      for( intX=0; intX<pstrSKUList.length;intX++)
      {
        if ( strTmpSKUList.search(pstrSKUList[intX]) < 0 )
        {
          pstrSKUList[intX] = "";
        }
      }

      return pstrSKUList;
    }



    this.ClearLists = function(pstrSpecCode)
    {
      objSKUSpec = this.SKUSpecs.GetAt( pstrSpecCode);
      for( specvalue in objSKUSpec.SpecValues )
      {
        if(typeof (objSKUSpec.SpecValues[specvalue]) == "object" )
        {
          objCurDDL = document.getElementById(objSKUSpec.SpecCode);

          for( intX=0; intX<objCurDDL.options.length; intX++)
          {
            objCurDDL.options[intX] = null;
          }
        }
      }

      while( objSKUSpec = this.SKUSpecs.GetNext() )
      {
        for( specvalue in objSKUSpec.SpecValues )
        {
          if(typeof (objSKUSpec.SpecValues[specvalue]) == "object" )
          {
            objCurDDL = document.getElementById(objSKUSpec.SpecCode);

            for( intX=0; intX<objCurDDL.options.length; intX++)
            {
              objCurDDL.options[intX] = null;
            }
          }
        }
      }
    }


    function ItemExists( pstrValue, objDDL )
    {
      for( intX = 0; intX < objDDL.options.length; intX++ )
      {
        if ( objDDL.options[intX].text == pstrValue )
        {
          return true;
        }
      }

      return false;
    }

    //Public Variables
    this.SKUSpecs = pobjSKUSpecs;
}
