/*
 * WCM
 * Copyright (C) 2006-2010  Proformatique <technique@proformatique.com>
 * Copyright (C) 2010  Waycom <devs@waycom.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

function wcm_bool(bool)
{
  if (bool === null)
    return false;

  switch (typeof(bool))
  {
    case 'object':
      return true;
    case 'undefined':
      return false;
  }

  switch (bool.toString().toLowerCase())
  {
    case 'y':
    case '1':
    case 'on':
    case 'yes':
    case 'true':
      return true;
    case 'n':
    case '0':
    case 'off':
    case 'no':
    case 'false':
      return false;
  }

  return Boolean(bool);
}

function wcm_string(str)
{
  var r = '';

  if (typeof(str) === 'undefined')
    return r;

  switch (str)
  {
    case null:
    case false:
      return r;
    case true:
      r = 1;
      break;
    default:
      r = str;
  }

  return String(str);
}

function wcm_is_boolean(b)
{
  return (typeof(b) === 'boolean');
}

function wcm_is_string(s)
{
  return (typeof(s) === 'string');
}

function wcm_is_array(a)
{
  return (a instanceof Array);
}

function wcm_is_function(f)
{
  return (typeof(f) === 'function');
}

function wcm_type_object(o)
{
  return (o !== null && typeof(o) === 'object');
}

function wcm_is_object(o)
{
  return (wcm_type_object(o) === true && wcm_is_array(o) === false);
}

function wcm_is_undef(v)
{
  return (typeof(v) === 'undefined');
}


function wcm_is_number(n)
{
  return (wcm_is_undef(Number(n)));
}

function wcm_is_int(i)
{
  var y = parseInt(i);

  if (isNaN(y) === true)
    return false;

  return (i == y && i.toString() === y.toString());
}

function wcm_is_float(i)
{
  var y = parseFloat(i);

  if (isNaN(y) === true || i != y)
    return false;

  return (i.toString().replace(/\.00*$/, '') === y.toString());
}

function wcm_is_uint(i)
{
  return ((wcm_is_int(i) === true && i >= 0));
}

function wcm_is_ufloat(i)
{
  return ((wcm_is_float(i) === true && i >= 0));
}

function wcm_is_scalar(val)
{
  switch (typeof(val))
  {
    case 'object':
    case 'undefined':
      return false;
  }

  return true;
}

function wcm_is_empty(val)
{
  if (wcm_is_undef(val) === true)
    return true;
  else if (wcm_is_array(val) === true)
    return (val.length === 0);
  else if (wcm_is_object(val) === true)
  {
    for (var property in val)
      return false;
    return true;
  }

  switch (val)
  {
    case 0:
    case '0':
    case null:
    case false:
    case '':
      return true;
  }

  return false;
}

function wcm_parse_uint(i)
{
  if (wcm_is_scalar(i) === false)
    return NaN;

  i = parseInt(wcm_string(i).replace(/\s+/g, ''));

  if (i !== NaN && i < 0)
    i = parseInt(wcm_substr(wcm_string(i), 1));

  return i;
}

function wcm_parse_float(i)
{
  if (wcm_is_scalar(i) === false)
    return NaN;

  i = wcm_string(i).replace(/\s+/g, '');
  var pos = i.lastIndexOf(',');

  if (pos !== -1)
    i = wcm_substr(i, 0, pos) + '.' + wcm_substr(i, pos + 1);

  return parseFloat(i);
}

function wcm_nan(val, def)
{
  if (isNaN(val) === false)
    return val;

  return (wcm_is_undef(def) === true ? true : def);
}

function wcm_has_len(val, def)
{
  if (wcm_is_scalar(val) === false)
    return (wcm_is_undef(def) === true ? false : def);

  return (wcm_string(val).length > 0);
}

function wcm_object_has_len(val, key, def)
{
  if (wcm_type_object(val) === false
  || wcm_is_scalar(key) === false
  || wcm_is_scalar(val[key]) === false)
    return (wcm_is_undef(def) === true ? false : def);

  return (wcm_string(val).length > 0);
}

function wcm_object_flip(obj)
{
  var r = {};

  for (var property in obj)
  {
    if (wcm_is_scalar(obj[property]) === true)
      r[obj[property]] = property;
  }

  return r;
}

function wcm_strcmp(str1, str2, len)
{
	if (wcm_is_scalar(str1) === false
	|| wcm_is_scalar(str2) === false)
		return false;

	str1 = wcm_string(str1);
	str2 = wcm_string(str2);

	if (wcm_is_uint(len) === true)
	{
		str1 = str1.substring(0, len);
		str2 = str2.substring(0, len);
	}

	if (str1 > str2)
		return 1;
	else if (str1 === str2)
		return 0;

	return -1;
}

function wcm_strcasecmp(str1, str2, len)
{
	if (wcm_is_scalar(str1) === false
	|| wcm_is_scalar(str2) === false)
		return false;

	str1 = wcm_string(str1).toLowerCase();
	str2 = wcm_string(str2).toLowerCase();

	if (wcm_is_uint(len) === true)
	{
		str1 = str1.substring(0, len);
		str2 = str2.substring(0, len);
	}

	if (str1 > str2)
		return 1;
	else if (str1 === str2)
		return 0;

	return -1;
}

function wcm_clone(obj)
{
  if (wcm_is_array(obj) === true)
    var r = [];
  else if (wcm_is_object(obj) === true)
    var r = {};
  else
    return obj;

  for (var property in obj)
    r[property] = wcm_clone(obj[property]);

  return r;
}

function wcm_substr(str, beg, end)
{
  var r = '';

  if (wcm_is_scalar(str) === false)
    return r;

  var len = str.length;

  if (len === 0 || isNaN(beg) === true)
    return r;

  if (isNaN(end) === true)
    end = len;

  beg = Number(beg);
  end = Number(end);

  if (beg < 0 || end < 0)
  {
    if (beg < 0)
      beg = len + beg;

    if (end < 0)
    {
      end = len + end;

      if (beg !== 0 && str.substr(beg, len).length > end)
        return r;
    }

    return str.substring(beg, end);
  }

  return str.substr(beg, end);
}

function wcm_random(len)
{
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
  var     r = '';

  if (wcm_is_uint(len) === false)
    return r;

  for (var i = 0; i < len && i < 256; i++)
  {
    var rnum = Math.floor(Math.random() * chars.length);
    r       += chars.substring(rnum,rnum + 1);
  }

  return r;
}

