/***************************************************************************
 * Copyright (C) 2006 by Korgault Studios
 * http://www.kgstudios.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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **************************************************************************/

/**
 * Métodos agregados a objetos existentes de Javascript
 *
 * @package phppr0.Shared.js
 * @author Korvus
 * @date 01-01-2007
 * @version 1.0
 */

/**
 * Rellena una cadena
 * @function Array.prototype.pad
 * @param sPadding ( String ) Relleno
 * @param nAmmount ( String ) Tamaño máximo de la cadena resultante
 * @param [ bInsertBefore ] ( Boolean )
 * @return ( string )
 */

String.prototype.pad = function( sPadding, nAmmount, bInsertBefore )
{
 for( var i = 0, sReturnValue = ""; i < nAmmount - this.toString().length; i++ )
  sReturnValue = sReturnValue.concat( sPadding );
  
 return( bInsertBefore ? ( this + sReturnValue ) : sReturnValue + this );
}

/**
 * Elimina los espacios a principio y fin de cadena
 *
 * @function Array.prototype.trim
 * @return ( string )
 */
 
String.prototype.trim = function()
{ return( this.toString().replace( /^\s*|\s*$/g, "" ) ); }

/**
 * Devuelve la posición del array donde se ha hayado una coincidencia
 *
 * @function Array.prototype.indexOf
 * @param oSearchFor ( mixed )
 * @return ( bool ) Si no hay coincidencias devuelve -1
 */

Array.prototype.indexOf = function( sPattern, bIsRegularExpression )
{
 var nReturnValue = -1;
 var oRegExp = new RegExp( sPattern );

 for( var i = 0; i < this.length; i++ )
 {
  if( !bIsRegularExpression && this[ i ] == sPattern || oRegExp.test( this[ i ] ) == true )
   nReturnValue = i;
 }
 
 return( nReturnValue );
}

/**
 * Une un array asociativo en una sola cadena
 *
 * @function Array.prototype.ajoin
 * @param sGlue1 ( string ) Nexo entre la clave y el valor
 * @param sGlue2 ( string ) Nexo entre cada elemento del array
 * @return ( string )
 */

Array.prototype.ajoin = function( sGlue1, sGlue2 )
{
 var sReturnValue = new String;
 
 for( var i in this )
 {
  if( typeof( this[ i ] ) != "function" )
   sReturnValue = sReturnValue.concat( i + sGlue1 + this[ i ] + sGlue2 );
 }
  
 return( sReturnValue );
}