/**
 * Common JavaScript file
 */

/**
 * Cookies - based on Prototype
 *
 * @author Carlos Reche, mihau
 * @param {Object} name
 * @param {Object} value
 * @param {Object} daysToExpire
 * @param {Object} cookiePath
 */
var Cookie = {
  set: function(name, value, daysToExpire, cookiePath) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }

    var path = '';
    if (cookiePath != undefined)
    {
      path = '; path=' + cookiePath;
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire + path);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};

/**
 * Detekcja włączonego klawisza CAPSLOCK. Przydatna np. przy logowaniu się użytkownika.
 *
 * <code>
 * <input type="text" ... onkeypress="capsDetect(arguments[0]);">
 * </code>
 *
 * @author http://www.howtocreate.co.uk/
 */
function capsDetect( e , infoTarget, boxId) {

  //if the browser did not pass event information to the handler,
  //check in window.event
  if( !e ) { e = window.event; } if( !e ) { return; }
  //what (case sensitive in good browsers) key was pressed
  //this uses all three techniques for checking, just in case
  var theKey = 0;
  if( e.which ) { theKey = e.which; } //Netscape 4+, etc.
  else if( e.keyCode ) { theKey = e.keyCode; } //Internet Explorer, etc.
  else if( e.charCode ) { theKey = e.charCode } //Gecko - probably not needed
  //was the shift key was pressed
  var theShift = false;
  if( e.shiftKey ) { theShift = e.shiftKey; } //Internet Explorer, etc.
  else if( e.modifiers ) { //Netscape 4
    //check the third bit of the modifiers value (says if SHIFT is pressed)
    if( e.modifiers & 4 ) { //bitwise AND
      theShift = true;
    }
  }
  //if upper case, check if shift is not pressed
  if( theKey > 64 && theKey < 91 && !theShift ) {
    alert( 'Uwaga - w??czony jest CapsLock!' );
  }
  //if lower case, check if shift is pressed
  else if( theKey > 96 && theKey < 123 && theShift ) {
    alert( 'Uwaga - w??czony jest CapsLock!' );
  }
}

var bool_showDebug = true;
/**
 * Debbuger.
 *
 * Jezeli wlaczony pokazuje w okienku debug
 *
 * W locie tworzy okienko debuga
 *
 * @param content  mixed   Wiadomosc do pokazania
 * @param label    string  Etykieta dla wiadomosci. Predefiniowane: '@error' (kolorczerwony)
 * @param append   bool    Jesli 1 to dopisuj do juz istniejacej zawartosci okienka
 * @author mihau
 * @version 2008-01-10 10:10:10
 *
 * @todo    Dodać Header bar z możliwością chowania i zamknięcia
 */
function xsDebug(content, label, append) {
    if(!bool_showDebug) {
        return;
    }

    var debugBoxId = 'elDebugBox';
    var debugBoxHolderId = 'elDebugBoxHolder';
    var obj_debugBoxHolder = document.getElementById(debugBoxHolderId);

    // Sprawdz czy istnieje juz okienko debuga - jezeli nie to je dodaj
    if(!obj_debugBoxHolder) {
        obj_debugBoxHolder = document.createElement('OBJECT');
        obj_debugBoxHolder.id = debugBoxHolderId;
        obj_debugBoxHolder.style.position = 'fixed';
        obj_debugBoxHolder.style.right = '2px';
        obj_debugBoxHolder.style.bottom = '2px';
        obj_debugBoxHolder.style.width = '500px';
        obj_debugBoxHolder.style.height = '100px';
        obj_debugBoxHolder.style.backgroundColor = '#d0d0ff';
        obj_debugBoxHolder.style.fontFamily = 'terminus, courier new, serif';
        obj_debugBoxHolder.style.fontSize = '10px';
        obj_debugBoxHolder.style.border = '1px solid blue';
        obj_debugBoxHolder.style.overflow = 'auto';

        //obj_debugBoxHolder.innerHTML = '<textarea id="' + debugBoxId + '" cols="60" rows="20"  style="border:1px solid blue;padding:3px;background-color:#d0d0ff;font-family:terminus, \'courier new\',serif;font-size:10px;"></textarea></div>';

        document.getElementsByTagName('BODY')[0].appendChild(obj_debugBoxHolder);
    }


    // Formatowanie w zaleznosci od etykiety
    if(label == '@error') {
        content = '<span style="color:red;">' + content  + '</span>';
    }

    // Etykieta
    if(!label) {
        label = '[*] ';
    }
    else {
        label = '[' + label + ']<br/>\n';
    }


    var obj_debugBox = document.getElementById(debugBoxHolderId);
    if(!append || append !== 0) {
        obj_debugBox.innerHTML =  obj_debugBox.innerHTML + label + content + '<br>\n';
    }
    else {
        obj_debugBox.innerHTML =   content;
    }
}

