//
// SRC = \inca\prog\taxspooler
//
$(function(){
   $(document).keydown(function(evt) {
      if (evt.which == 27) // ESC
      {
         goTo(72);
      }
   });

   $(document).focus();

   // UI
   $('.usri').click(function(e) {
      var ro = $(this).find('input').first().prop('readonly');

      if (ro) {
         alert( 'model: ' + $(this).closest("tr").data("model") );
      }
   });

   // JQUERY: Assign click events to elements of class 'b-add' and 'b-del' (buttons 'Add' and 'Del')
   $('.b-save').on('click', b_save);
   $('.b-can').on('click', function(){ goTo(72); });

   // if (element.hasOwnProperty('customProperty')) {

   function read_level($otr2)
   {
      var x = $otr2.data('level');

      if (x === undefined)
         return null;
      else
         return parseInt(x);
   }

   function apply_attr($otr, show)
   {
      var trLevel = read_level($otr);

      if (trLevel !== null) {
         $otr.nextAll('tr').each(function(index, element){
            var trLevel2 = read_level($(element));

            if (trLevel2 <= trLevel)
               return false; // This breaks the loop
            else {
               if (show)
                  $(element).show(); //'slow');
               else
                  $(element).hide(); //'slow');
            }
         });
      }
   }

   $("tr:has(td:eq(1):empty)").addClass('isOpen');

   $(document).on('click', '.isOpen td:first-child', function(){
      $otr = $(this).parent();
      $otr.removeClass('isOpen').addClass('isClosed');
      apply_attr($otr, false);
   });
   $(document).on('click', '.isClosed td:first-child', function(){
      $otr = $(this).parent();
      $otr.removeClass('isClosed').addClass('isOpen');
      apply_attr($otr, true);
   });
});

/*
function prepDropDown()
{
   $('tbody tr').click(function(){
      var ar1 = new Array();
      $(this).find('> td').each(function(){ ar1.push( $(this).text() ); });
      var url='http://taxspooler/go.php?val=' + encodeURIComponent(ar1.join(' '));
      location.href = url;
   });
   var w = $(window);
   var row = $('.selected');
   if (row.length){ w.scrollTop( row.offset().top - (w.height()/2) ); }
}
   */

function b_save()
{
   var lastModel = new Array();
   $('tr').each(function(index, element){
      var $otr = $(element);
      var a1 = $otr.data("level");
      var a2 = $otr.find('td').eq(2).clone().children().remove().end().text().trim();
      if (a1!==undefined && a2!==undefined) {
         var ii = parseInt(a1); // level
         if (ii in lastModel) {
            var k2 = lastModel[ii].split('.');
            if (a2 === k2[0]) { // αν έχουμε ίδιο κλειδί
               var idx2 = 0;
               if (k2.length == 2) {
                  idx2 = parseInt(k2[1]) + 1;
               }
               else {
                  idx2 = 2;
               }
               a2 += '.' + idx2;
            }
         }

         lastModel[ii] = a2;

         $input = $otr.find('input');
         if ($input.length == 1) {
            var k1;
            var key = new Array();
            for (var i=0; i<=parseInt(a1); i++) {
               k1 = lastModel[i];
               if (k1.length > 0) {
                  key.push(k1);
               }
            }
            $input.attr('name', key.join('/'));

            if ($otr.data('model') !== 'xs:string') {
               var ar1 = $input.val().split(' ');
               while (ar1.length > 0 && ar1[0] === '') {
                  ar1.shift();
               }
               $input.val(ar1.length > 0 ? ar1[0] : '');
            }
         }
      }
   });

   $('#SERIALIZEDINPUT').val($('#userinput').serialize());
   $('#mainform').submit();
}