// Util2.js

function Util(){};

Util.isAny = Util.isOk =
function /* isOk */( o ) {
  return ( (typeof o != "undefined") && (o != null) );
}

Util.get_if_ok = Util.getIfOk =
function /* get_if_ok */(v,dv) {
  if (Util.isOk(v)) {
    return v;
  }
  else
  {
    if (Util.isOk(dv)) {
      return dv;
    }
    else {
      return null;
    }
  }
}

Util.getFunctionName =
function /* getFunctionName */( aFunction ) {
  if ( typeof aFunction != 'function' ) return "";
  aFunction = '' + aFunction;
  var fn = aFunction.slice( aFunction.indexOf( " " ) + 1, aFunction.indexOf( "(" ) );
  return fn;
}

Util.emptyString = '';
Util.emptyFunction = function /* emptyFunction */() {};
Util.emptyObject = {};
Util.emptyArray = [];

register( Util );

Util.isOkClean = Util.isAnyClean =
function /* isOkClean */( o, prop )
{
  if ( (o == null) || (typeof o == "undefined") )
    return false;
  if ( (o[prop] != null) && (typeof o[prop] != "undefined") )
    return true;
  else
    {
      delete o[prop];
      return false;
    }
}

Util.getConstructorName_JS14 =
function /* getConstructorName_JS14 */( c )
{
  if ( this.isAny( c ) && this.isAny( c.constructor ) )
    return c.constructor.name;
}

Util.getConstructorName_JS13 =
function /* getConstructorName_JS13 */( c )
{
  if ( this.isAny( c ) && this.isAny( c.constructor ) )
    {
      if ( typeof c.constructor == 'function' )
  {
    if ( c.constructor.toString )
      {
        var cStr = c.constructor.toString();
        return cStr.slice( 10, cStr.indexOf( "(" ) );
      }
  }
      return '' + c.constructor;
    }
  else
    return '--'
}

// Check whether constructor.name exists
if ( Util.isOk( Util.constructor.name ) ) {
    Util.getConstructorName = Util.getConstructorName_JS14;
} else {
    Util.getConstructorName = Util.getConstructorName_JS13;
}

Util.addURLParameter =
function /* addURLParameter */( s, para )
{
  var result = '' + s;
  var pos = result.indexOf( '?' );
  if ( pos != -1 )
  {
    result += "&" + para;
  }
  else
  {
    result += "?" + para;
  }
  return result;
}

/**
 * Returns an array of key value pairs
 * OJO: If no value exists for a key, the keywork 'true' is used.
 * @param   theUrl
 * @param   pBegin   [optional] Default: '?'
 * @param   pSep     [optional] Default: '&'
 * @param   pKV      [optional] Default: '='
 */
Util.getURLParameters =
function /* getURLParameters */( theUrl, pBegin, pSep, pKV ) {
  // Delimitadores por defecto
  if ( ! Util.isOk( pBegin ) ) pBegin = "?";
  if ( ! Util.isOk( pSep ) ) pSep = "&";
  if ( ! Util.isOk( pKV ) ) pKV = "=";

  var result = [];
  var queryPos = theUrl.lastIndexOf( pBegin );
  if ( queryPos >= 0 ) {
    theUrl = theUrl.substring( queryPos + pBegin.length );
    var params = theUrl.split( pSep );
    for ( var p=0; p<params.length; p++ ) {
      var key, value;
      var keyValue = params[p];
      var equal = keyValue.indexOf( pKV );
      if ( equal > 0 ) {
        key = unescape(keyValue.substring(0, equal));
        value = unescape(keyValue.substring(equal+pKV.length));
      } else {
        key = unescape( keyValue );
        value = 'true';
      }
      result[ key ] = value;
    }
  }
  return result;
}

Util.trim =
function /* trim */( s )
{
  //if ( ! Util.isOk( s ) ) return '';
  // [la interrogacion elimina el modo greedy
  // (feature no documentada de JavaScript) ]
  //s = (''+s).replace( /^\s*(.*?)\s*$/, "$1" );
  //return s;
  if ( !s ) return '';
  var ch;
  for ( var i=0; i < s.length; i++) {
      ch = s.charAt(i);
      if ( ch != " " ) break;
  }
  for ( var j = s.length-1; j>0; j--) {
      ch = s.charAt(j);
      if ( ch != " " ) break;
  }
  return s.substring(i, j+1);

}

Util.isException =
function /* isException */( o ) {
//debug(342);
var result = false;
  if ( this.isOk( o ) ) {
//  debug(345);
//  debug("ExceptionInterface=" + ExceptionInterface);
    result = OOJS.objectImplements( o, ExceptionInterface );
  }
//  debug("result=" + result);
  return result;
}

Util.arrayToURI =
function /* arrayToURI */ ( a ){
  var s="";
  for(var e in a)
    s += e +"="+a[e]+"&";
  return s.slice(0,s.length-1);
}

Util.arrayToList =
function /* arrayToList */( args ) {
  var result = '';

  for (i=0; i < args.length; i++) {
    if (i > 0) result += ', ';
    var a = args[i];
    result += ((typeof a == "string") ? '"' + a.replace(/"/g, '\\"') + '"' : a);
}
return result;
}
Util.insertProto =
function /* insertProto */( obj, newProto )
{
  if ( obj.__proto__ == newProto ) return;
  var oldProto = obj.__proto__;
  obj.__proto__ = newProto;
  while( newProto.__proto__.constructor != Object )
    newProto = newProto.__proto__;
  newProto.__proto__ = oldProto;
}

Util.concatProto =
function /* concatProto */( obj, newProto )
{
  if ( obj.__proto__ == newProto ) return;
  var pObj = obj;
  while( pObj.__proto__ != null )
    {
      obj = pObj;
      pObj = obj.__proto__;
      if ( pObj == Function.prototype ) {
  obj.__proto__ = newProto;
  //Util.concatProto( newProto, Function );
  return;
      }
      if ( pObj == newProto ) return;
    }
  obj.__proto__ = newProto;
}

Util.concatParent =
function /* concatParent */( obj, newParent )
{
  if ( obj.__parent__ == newParent ) return;
  var pObj = obj;
  while( pObj.__parent__ != null )
    {
      obj = pObj;
      pObj = obj.__parent__;
      if ( pObj == newParent ) return;
    }
  obj.__parent__ = newParent;
}

Util.isInInstance =
function /* isInInstance */( o, prop )
{
  // Otra implementacion -- tiene problemas con los arrays
  //dirty hack para arrays vacias
  var isEmptyArray = ( ( o.constructor == Array ) && ( o.length == 0 ) );

  if ( (o == null) || (typeof o == "undefined") )
    return false;
  var oProto = o.__proto__;
  o.__proto__ = null;
  var result = this.isOkClean( o, prop );
  o.__proto__ = oProto;

  //dirty hack para arrays vacias
  if ( isEmptyArray )
    o.pop();

  return result;
}

Util.instanceOf =
function /* instanceOf */( object, constructor )
{
  while ( object != null )
    {
      if ( object == constructor.prototype )
  return true;
      object = object.__proto__;
    }
  return false;
}

Util.getInstanceProperties =
function /* getInstanceProperties */( obj )
{
  var proto = obj.__proto__;
  var result = [];
  obj.__proto__ = null;
  for ( var i in obj )
    result[ result.length ] = i;
  obj.__proto__ = proto;
  return result;
}

Util.getInstancePropertiesAsHTML =
function /* getInstancePropertiesAsHTML */( obj )
{
  var props = this.getInstanceProperties( obj );
  var result = '';
  for ( var i = 0; i < props.length; i++ )
    result += props[ i] + '<br>\n';
  return result;
}

Util.objectClone =
function /* objectClone */( o ) {
  var o2 = {};
  var proto = o.__proto__;
  o2.__proto__ = proto;
  for ( var p in o ) {
    if ( this.isOkClean( proto, p ) ) continue;
    var pv = o[p];
    if ( pv == proto ) continue;
    o2[ p ] = pv;
  }
 return o2;
}

Util.displayObjectStruct =
function /* displayObjectStruct */( o, s, ind ) {
  if ( ! ind ) ind = "";
  var result = '';
  if ( s ) result += s + ": " + (typeof o) + " ";
  if ( Util.isAny(o) ) {
    if ( o.__proto__ == Object.prototype ) {
      result += "[" + "Object"+ "]" + "<br>\n";
    } else {
      result += "<br>\n";
      result += ind + "constructor: " + Util.getConstructorName(o) + "<br>\n";
      result += ind + "__proto__ -> " + arguments.callee( o.__proto__, s + '.__proto__', ind + "----" );
      result += ind + "__parent__ -> " + arguments.callee( o.__parent__, s + '.__parent__', ind + "----" );
    }
  } else {
    result += "null" + "<br>\n";
  }
  return result;
}

Util.clone =
function /* clone */( o )
{
   var cloned;
  if ( Util.isObject( o ) ) {
     return Util.object_clone( o );
  }
  if ( Util.isArray( o ) ) {
     return Util.array_clone( o );
  }
  if ( Util.isString( o ) ) {
     return new String( o );
  }
  return o;
}


Util.object_clone =
function /* object_clone */( o )
{
   if ( Util.isOk( o.length ) ) {
      return Util.array_clone( o );
   }
   var cloned = {};
   for( var p in o ) {
      cloned[p] = Util.clone(o[p]);
   }
   return cloned;
}

Util.array_clone =
function /* array_clone */( o )
{
   var cloned = [];
   for(var i=0; i<o.length; i++){
      cloned[i] = Util.clone(o[i]);
   }
   return cloned;
}

Util.isString =
function /* isString */( cadena ) {
  return this.isGivenObject( cadena, String );
}

Util.isNumber =
function /* isNumber */( numero ) {
  if ( isNaN( numero ) ) return false;
  return this.isGivenObject( numero, Number );
}

Util.isBoolean =
function /* isBoolean */( bBool ) {
  return this.isGivenObject( bBool, Boolean );
}

Util.isArray =
function /* isArray */( a ) {
  return this.isGivenObject( a, Array );
}

Util.isFunction =
function /* isFunction */( f ) {
  return this.isGivenType( f, "function" );
}

Util.isGivenObject =
function /* isGivenObject */( argument, objectConstructor ) {
  if ( (typeof argument == "undefined") ||
       (argument == null) ||
       (argument.constructor != objectConstructor) )
    return false;
  else
    return true;
}

Util.isGivenType =
function /* isGivenType */( argument, argType ) {
  if ( argument == null ) return false;
  return ( typeof argument == argType );
}

Util.isNull =
function /* isNull */( pData )
{
  if ( ( typeof pData == 'undefined' ) || ( pData == null ) )
    return true;
  else
    return false;
}

Util.isObject =
function /* isObject */( pData )
{
  if ( typeof pData == 'object' )
    return true;
  else
    return false;
}
