/**
 *
 */
function getElementsByClassName(node, classname) {

    var a   = [];
    var re  = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");

    for (var i=0, j=els.length; i<j; i++) {

        if (re.test(els[i].className)) {
            a.push(els[i]);
        } // if

    } // for

    return a;

} // getElementsByClassName



/**
 *
 */
function poistaVanhat(node, classname) {

    var poistettavat = getElementsByClassName(node, classname);

    if (poistettavat.length > 0) {

        for (var i=0; i<poistettavat.length; i++) {
            node.removeChild(poistettavat[i]);
        } // for

    } // if

} // poistaVanhat


var OR = function ()
{
  Builder.dump();

  return {

    toPaddedString: function (number, padding, length, radix)
    {
      var string = number.toString(radix || 10);
      return padding.times(length - string.length) + string;
    },

    makeDraggable: function ()
    {
      $$('#tulokset div.leimaukset').each(function (item)
      {
        new Draggable(item.id);
        
        var closeLink = $(A({ href: 'javascript:void(0)' }, [ 'Sulje ikkuna' ]));
        var bottomBar = P({ className: 'closeLink' });
        
        closeLink.observe('click', function () { item.hide(); });
        bottomBar.appendChild(closeLink);
        item.appendChild(bottomBar);
      });
    },

    createResultForm: function (resId)
    {
      var result = tulokset[resId];
      var row = $('tulos-' + resId);

      if (row.next() && row.next().hasClassName('newRow'))
      {
        row.next().toggle();
        return;
      }

      // make each split and control code 4 chars wide
      var splitTimes = $A(result[4].split(',')).map(function (item)
      {
        return OR.toPaddedString(item, ' ', 4);
      }).join(',');

      var codes = $A(result[5].split(',')).map(function (item)
      {
        return OR.toPaddedString(item, ' ', 4);
      }).join(',');
      
      var lstCourses = SELECT({ name: 'lstRadat' });

      $A(radat).each(function (course, index)
      {
        lstCourses.options[index] = new Option(course[1], course[0]);

        if (course[0] == result[6])
          lstCourses.options[index].selected = true;
      });

      var newRow = TR({ className: 'newRow' }, [
        TD({ colSpan: 7 }, [
          FORM({ method: 'post' }, [
            INPUT({ type: 'hidden', name: 'txtId', value: resId }),
            INPUT({ type: 'text', name: 'txtSukunimi', value: result[0] }),
            INPUT({ type: 'text', name: 'txtEtunimi', value: result[1] }),
            INPUT({ type: 'text', name: 'txtSeura', value: result[2] }),
            INPUT({ type: 'text', name: 'txtEmit', value: result[3] }),
            INPUT({ type: 'text', name: 'txtValiajat', value: splitTimes }),
            INPUT({ type: 'text', name: 'txtKoodit', value: codes }),
            lstCourses,
            INPUT({ type: 'submit', name: 'cmdMuokkaaTulos', value: 'Tallenna' })
          ])
        ])
      ]);

      row.up().insertBefore(newRow, row.next());
    }
  };
}();

/**
 *
 */
function varmista(kohde) {

    var kys;
    var ok;

    switch (kohde) {

        case "radanPoisto":
           kys = "Haluatko varmasti poistaa valitun radan?";
           break;

        case "tuloksenPoisto":
            kys = "Haluatko varmasti poistaa valitun tuloksen?";
            break;

    } // switch

    ok = confirm(kys);

    return (ok ? true : false);

} // varmista



/**
 *
 */
function toggle(id) {

    var e = document.getElementById(id);
    var t = e.className;

    if (t.match('invisible')) {
        t = t.replace(/invisible/gi, 'visible');
    } else {
        t = t.replace(/visible/gi, 'invisible');
    }

    e.className = t;

} // toggle

function linkki(formi,paikka) {
  var url = prompt('Anna linkin kohde:', 'http://');
  var teksti = prompt('Anna linkin teksti:','');
  eval("document."+formi+"."+paikka+".value += '[url="+url+"]"+teksti+"[/url]'");
}

function lihavoi(formi,paikka) {
  var teksti = prompt('Anna lihavoitava teksti:','');
  eval("document."+formi+"."+paikka+".value += '[b]"+teksti+"[/b]'");
}

function kursivoi(formi,paikka) {
  var teksti = prompt('Anna kursivoitava teksti:','');
  eval("document."+formi+"."+paikka+".value += '[i]"+teksti+"[/i]'");
}

function alleviivaa(formi,paikka) {
  var teksti = prompt('Anna alleviivattava teksti:','');
  eval("document."+formi+"."+paikka+".value += '[u]"+teksti+"[/u]'");
}

function popup(url, name, width, height) {

    settings = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width="+width+",height="+height;
    MyNewWindow = window.open(url, name, settings);

}