 $(document).ready(function() {
   // do stuff when DOM is ready

   sBasePath = '/products/';
   nPathItems = 0;
   aPath = [];

/*
$.post( sBasePath + "wizard.php", 
   { sec: 0 }, 
   function(xml)
   {
      alert('TEST');
   }
);
return ;
*/

   $("#wizard_path").empty();

   $("#vendor_id").empty();
   $("#type_id").empty();
   $("#model_id").empty();
   $("#supp_id").empty();

   FillSelect( 0, 'vendor_id', '' );

   // Set Up Events
   $("#vendor_id").change( function(e){
      FillSelect( this, 'type_id', 'vendor_id' );
   } );

   $("#type_id").change( function(e){
      FillSelect( this, 'model_id', 'type_id' );
   } );

   $("#model_id").change( function(e){
      FillSelect( this, 'supp_id', 'model_id' );
   } );
   $("#model_id").click( function(e){
      FillSelect( this, 'supp_id', 'model_id' );
   } );

   $("#supp_id").change( function(e){
      // Some other func
   } );

 });

function FillSelect( oCur, sNextSel, sCurId )
{
   var aIds = [ 'vendor_id', 'type_id', 'model_id', 'supp_id' ];

   var bClear  = false;
   var bNext   = false;

   var nId  = 0;
   var sPth = '';

   if ( oCur )
   {
      nId = oCur.value;
      oS = document.getElementById( sCurId );
      sPth = oS.item( oS.selectedIndex ).text;
   }

   var sFarSel = '';

   if ( sCurId == 'vendor_id' )
   {
      aPath[0] = sPth; ClearPath(0);
   }
   else if ( sCurId == 'type_id' )
   {
      aPath[1] = sPth; ClearPath(1);
   }
   else if ( sCurId == 'model_id' )
   {
      aPath[2] = sPth; ClearPath(2);
   }

   // Build path
   sPath = aPath.join(' &gt; ');
   $("#wizard_path").empty();
   $("#wizard_path").append( sPath );

   for ( i in aIds )
   {
      if ( aIds[i] == sNextSel ) bClear = true;
      if ( bClear )
      {
         $( "#"+aIds[i] ).empty();

         if ( aIds[i] == 'supp_id' )
         {
            document.getElementById('supp_id').style.display = 'none'; // IE fix
         }
      }
   }

   if ( sNextSel == 'supp_id' )
      oParams = { sec: nId, pt: sCurId, elems: 1 };
   else
   {
      oParams = { sec: nId, pt: sCurId };
      if ( sCurId == 'type_id' )
         oParams.vnd = document.getElementById('vendor_id').value;
   }

   if ( sCurId == 'model_id' )
   {
      document.getElementById('td_vendor').style.display   = 'none';
      document.getElementById('td_type').style.display     = 'none';
      document.getElementById('td_model').style.display    = 'none';
      document.getElementById('td_supp').style.display     = '';
      document.getElementById('supp_id').style.display     = ''; // IE fix

      document.getElementById('wizard_path').style.display = '';
   }

   document.getElementById('load_ind').style.display = '';

   // var rnd = Math.round( 10000*Math.random() );

   $.post( sBasePath + "wizard.php", 
           oParams, 
           function(xml)
           {
              aIds = ( $("ids", xml).text() ).split(',');
              aTxt = ( $("txt", xml).text() ).split('^#');
              aExt = ( $("ext", xml).text() ).split('^#');

              AddPrintParam( aExt[0] );
              // Make sure
              $( "#"+sNextSel ).empty();

              document.getElementById('load_ind').style.display = 'none';

              // Add options
              var oSel = $("#"+sNextSel).get(0);
              if ( sNextSel != 'supp_id' )
              {
                 for( var i = 0; i < aIds.length; i++ )
                 {
                    if ( document.createElement )
                    {
                       var newOpt = document.createElement( "OPTION" );
                       newOpt.text = aTxt[i];
                       newOpt.value = aIds[i];
                       ( oSel.options.add ) ? oSel.options.add(newOpt) : oSel.add(newOpt, null);
                    }
                    else
                    {
                       oSel.options[i] = new Option( aTxt[i], aIds[i] );
                    }
                 }
              }
              else 
              {
                 var sTxt = '';
                 oSel.text  = sTxt;
                 oSel.value = sTxt;

                 for( var i = 0; i < aIds.length; i++ )
                 {
                    sTxt += ( aTxt[i] + '\n' );
                 }

                 oSel.text  = sTxt;
                 oSel.value = sTxt;
              }
           }
   );
}

function ClearPath( nFrom )
{
   nLen = aPath.length;
   for ( i=0; i < nLen; i++ )
      if ( i > nFrom ) aPath[i] = null;
}

function ReturnBtnClick()
{
   document.getElementById('td_supp').style.display = 'none';

   document.getElementById('td_vendor').style.display = '';
   document.getElementById('td_type').style.display = '';
   document.getElementById('td_model').style.display = '';
   document.getElementById('wizard_path').style.display = 'none';
}

function AddPrintParam( sId )
{
   if ( sId != '' )
   {
      sHref = document.getElementById('prn_ver_a_id').href;

      nI = sHref.indexOf( '&spage=', 0 );
      if ( nI > -1 )
         sHref = sHref.substr( 0, nI );

      document.getElementById('prn_ver_a_id').href = sHref + '&spage='+sId;
   }
}

