/*
  Title: javascript\common.js
*/

/*
  Function: DebugCall

  Logs a function and parameters into the console

  Parameters:

  First parameter - The name of the function
  Subsequent parameters - The parameters of the function

  Author:
  
  Tony Mattsson (tony.mattsson@ltdalarna.se)
*/

/*
  Prototype: StrNum
  
  Remove everything except for numbers

  Author:
  
  Tony Mattsson (tony.mattsson@ltdalarna.se)
*/
function StrNum() {
  return (parseInt(this.replace(/[^\d\.-]/g, '')));
}
String.prototype.strnum = StrNum;

/*
  Function: LimitToNumbers

  Limits an input field so that it only accepts numbers

  Parameters:

  Target - Target id, for example #MuInput

  Author:
  
  Tony Mattsson (tony.mattsson@ltdalarna.se)
*/
function LimitToNumbers(Target) {
  $(Target).keydown(function(Event) {
    if(Event.keyCode == 13) $(Target).change();
    if(Event.keyCode == 46 || Event.keyCode == 8 || Event.keyCode == 190 || Event.keyCode == 9) {
    } else {
      if ((Event.keyCode < 48 || Event.keyCode > 57) && (Event.keyCode < 96 || Event.keyCode > 105 ) || Event.shiftKey || Event.altKey || Event.ctrlKey)
      Event.preventDefault(); 
    }
  });
}

function DebugCall() {
  if (!Settings.MaterioDevelopmentMode) return;
  var Values = [];
  for (var i = 1; i < arguments.length; i++) {
    switch (typeof arguments[i]) {
    case 'boolean':
      (arguments[i]) ? Values[i - 1] = 'true' : Values[i - 1] = 'false';
      break;
    case 'string':
      Values[i - 1] = "'" + arguments[i] + "'";
      break;
    case 'object':
      Values[i - 1] = "object";
      break;
    case 'undefined':
      Values[i - 1] = "undefined";
      break;
    default:
      Values[i - 1] = arguments[i];
    }
  }
  console.log(arguments[0] + '(' + Values.join(', ') + ');');
}

/*
  Function: Log

  Outputs log information into the console if MaterioDevelopmentMode is set

  Parameters:

  Content - Content to be logged

  Author:
  
  Tony Mattsson (tony.mattsson@ltdalarna.se)
*/
function Log(Content) {
  if (Settings.MaterioDevelopmentMode)
    console.log(Content);
  else
    return;
}

/*
  Function: MaterioDisplayBackButton

  Display a button so that users can back to the main Materio application conveniently

  Parameters:

  X - The X-axis coordinate
  Y - The Y-axis coordinate
  BackTraversal - Traverse back to find index.php. Default = 2
  HiddenOpacity - A value from 0 (invisible) to 1 (visible)

  Author:
  
  Tony Mattsson (tony.mattsson@ltdalarna.se)
*/
function MaterioDisplayBackButton(X, Y, BackTraversal, HiddenOpacity) {
  if(!X) var X = 2;
  if(!Y) var Y = 2;
  if(!BackTraversal) var BackTraversal = 2;
  if(!HiddenOpacity) var HiddenOpacity = 0.2;
  var HTML = new Array();
  var BackString = '';
  for(i=0; i < (BackTraversal); i++)
    BackString += '../';
  HTML.push('<div id="MaterioDisplayBackButton" style="position:absolute;top:' + Y + 'px;left:' + X + 'px;">');
  HTML.push('<a style="border:0" href="../../index.php"><img style="border:0" src="' + BackString + 'pix/gotomaterio_lb.png" alt=""></a></div>');
  $('body').append(HTML.join(''));
  $('#MaterioDisplayBackButton img').fadeTo(0, HiddenOpacity);
  $('#MaterioDisplayBackButton img').mouseover(function(){
    $(this).fadeTo('fast', 1);
  }).mouseout(function(){
    $(this).fadeTo('fast', HiddenOpacity);
  });
}

/*
  Function: MaterioGetUserData

  Gets data about the logged-in user

  Parameters:

  SuccessFunction - A functio to handle the response
  BackTraversal - Traverse back to find index.php. Default = 2
  HiddenOpacity - A value from 0 (invisible) to 1 (visible)

  Returns:

  Returs the data in an array, or false if error is thrown

  Author:
  
  Tony Mattsson (tony.mattsson@ltdalarna.se)
*/
function MaterioGetUserData(SuccessFunction, BackTraversal) {
  var BackString = '';
  if(!BackTraversal) var BackTraversal = 2;
  for(i=0; i < (BackTraversal); i++)
    BackString += '../';
  $.ajax({
    url: BackString + 'index.php',
    cache: false,
    dataType: 'json',
    data: {'Operation':'MaterioGetUserData'},
    success: function(data) {
        SuccessFunction(data);
      },
    error: function (xhr, ajaxOptions, thrownError) {
      return(false);
    }
  });
}

/*
  Function: ConsoleEcho

  Echoes logs from the *ConsoleEcho* command (misc.php) to the javascript console

  Author:
  
  Tony Mattsson (tony.mattsson@ltdalarna.se)
*/
function ConsoleEcho() {
  $('.ConsoleEcho').each(function() {
  var Header = $('.ConsoleEchoHeader', this).text();
  var Echo = $('.ConsoleEchoText', this).text();
  if(Header.length > 0) {
    var Time = $('.ConsoleEchoTime', this).text();
    var Line = $('.ConsoleEchoLine', this).text();
    console.log(Header + ' / Time: ' + Time + ' / Line: ' + Line);
  }
  console.log(Echo);
  });
}

/*
  Function: UCFirst

  First character becomes uppercase

  Derived from:
  
  <StackOverflow at http://stackoverflow.com/questions/1026069/capitalize-first-letter-of-string-in-javascript>
*/
function UCFirst(String) {
  return(String.charAt(0).toUpperCase() + String.slice(1));
}







