/******************************************************************************
*
*  Copyright (c) 2008
*  cruiz10020@yahoo.com
*
*  JsHelper.js
*
*  Description:
*    Common js helper functions
*
*
*  Requires:
*    prototype.js - http://www.prototypejs.org/
*
*  Development History:
*    12-2-2008       - created
*
*******************************************************************************/


var JsHelper = 
{
  getEvent: function( e )
  {
    if( ( ! e ) && window.event )
    {
      return window.event;
    }

    return e;
  },
  
  getSourceElement: function( e )
  {
    var sourceElement = null;

    e = this.getEvent( e );

    if( e && e.srcElement )
    {
      sourceElement = e.srcElement;
    }
    else if( e && e.target )
    {
      sourceElement = e.target;
    }
    else
    {
      sourceElement = null;
    }

    return sourceElement;
  },
  
  getKeycode: function( e )
  {
    e = this.getEvent( e );
    if( e.keyCode )
    {
      return e.keyCode; 
    }
    
    return -1;
  },
  
  isReturnKey: function( e )
  {
    switch( this.getKeycode( e ) )
    {
      case 13: return true;
      
      default: return false;
    }
  },
  
  isTabKey: function( e )
  {
    switch( this.getKeycode( e ) )
    {
      case 9: return true;
      
      default: return false;
    }
  },
  
  isDeleteKey: function( e )
  {
    switch( this.getKeycode( e ) )
    {
      case 8: return true;
      
      default: return false;
    }
  },
  
  isEscKey: function( e )
  {
    switch( this.getKeycode( e ) )
    {
      case 27: return true;
      
      default: return false;
    }
  },
  
  isUpArrowKey: function( e )
  {
    switch( this.getKeycode( e ) )
    {
      case 38: return true;
      
      default: return false;
    }
  },
  
  isDownArrowKey: function( e )
  {
    switch( this.getKeycode( e ) )
    {
      case 40: return true;
      
      default: return false;
    }
  },
  
  truncate: function( value, maxLength, suffix )
  {
  	if( value )
  	{
  		if( value.length > maxLength )
  		{ 
  			if( suffix == null )
  			{
  				suffix = '&#133;';
  			}
  			
  			value = value.substring( 0, (maxLength - suffix.length) );
  			value += suffix;
  		}
  	}
  	
  	return value;
  },
  
  stripParameter: function( url, paramName )
	{
		if( url )
		{
		  // strip out old param value if it already exists
		  if ( url.indexOf( "?" + paramName + "=" ) > -1 || url.indexOf( "&" + paramName + "=" ) > -1 )
		  {
		    var delim = ( url.indexOf( "?" + paramName + "=" ) > -1 ) ? "?" : "&";
		
		    var s1 = url.substring( 0, url.indexOf( delim + paramName + "=" )+1 );
		    var s2 = url.substring( url.indexOf( delim + paramName + "=" ) + 1 );
		
		    if ( s2.indexOf( "&" ) > -1 )
		    {
		      // if there are more params, concatenate first part of url with second part (minus stripped param)
		      return s1 + s2.substring( s2.indexOf( "&" )+1 );
		    }
		    else
		    {
		      // if this was the last param, strip punctuation off
		      return s1.substring( 0, s1.length-1 );
		    }
		  }
		}
	
    return url;
	},
  
  
  appendParameter: function( url, paramName, paramValue, includeRandomParam )
	{
	  if( url )
	  {
	    // strip out old param value if it already exists
	    url = this.stripParameter( url, paramName );
	
	    if( url.indexOf( '?' ) >= 0 )
	    {
	      url += '&';
	    }
	    else
	    {
	      url += '?';
	    }
	
	    url += paramName + '=' + paramValue
	
	    if( includeRandomParam == true )
	    {
	      url = this.appendParameter( url, 'rand', this.simpleRound( Math.random(), 4 ) );
	    }
	  }
	
	  return url;
	},
	
	simpleRound: function( value, numberOfDecimalPlaces )
	{
	  if( value )
	  {
	    if( !numberOfDecimalPlaces )
	      numberOfDecimalPlaces = 1;
	
	    var precision = Math.pow( 10, numberOfDecimalPlaces );
	    value = Math.round( value *  precision ) / precision;
	  }
	
	  return value;
	},
	
	getWindowSize: function()
	{
	  var toReturn = $( new Array() );

	  if( document.clientHeight )
	  {
	    toReturn[0] = document.clientWidth;
	    toReturn[1] = document.clientHeight;
	  }
	  else if( document.documentElement && document.documentElement.clientHeight )
	  {
	    toReturn[0] = document.documentElement.clientWidth;
	    toReturn[1] = document.documentElement.clientHeight;
	  }
	  else if( document.body && document.body.clientHeight )
	  {
	    toReturn[0] = document.body.clientWidth;
	    toReturn[1] = document.body.clientHeight;
	  }


	  return toReturn;
	},
	
	evalJSON: function( str, maxRecursion )
	{
	  var obj = null;
	  try
	  {
	    obj = str.toString().evalJSON();
	  }
	  catch(e)
	  {
	    //debug( e );
	  }
	
	  //var obj = eval( '( ' + str + ' )' );
	  if( ( maxRecursion > 0 ) && obj )
	  {
	    // must be an array
	    if( obj.length )
	    {
	      // loop through all elements
	      for( var i = 0; i < obj.length; i++ )
	      {
	        // jsonfy each attribute that is json
	        for( var key in obj[i] )
	        {
	          if( obj[i][key].toString().isJSON() )
	          {
	            try
	            {
	              obj[i][key] = this.evalJSON( obj[i][key], maxRecursion - 1 );
	            }
	            catch(e)
	            {
	              // eat it!
	            }
	          }
	        }
	      }
	    }
	    else
	    {
	      // jsonfy each attribute that is json
	      for( var key in obj )
	      {
	        if( obj[key].toString().isJSON() )
	        {
	          obj[key] = this.evalJSON( obj[key], maxRecursion - 1 );
	        }
	      }
	    }
	  }
	
	  return obj;
  },
  
  appendDelimitedValue: function( str, toAdd, delimiter )
  {
  	if( toAdd && delimiter )
  	{
  		if( ! str.endsWith( delimiter ) )
  		{
  			str += delimiter;
  		}
  		
  		str += toAdd;
  	}
  	
  	return str;
  },
  
  removeDelimitedValue: function( str, toRemove, delimiter )
  {
    if( str && toRemove && delimiter )
    {
    	var split = str.split( delimiter );
    	str = [];
    	for( var i = 0; i < split.length; i++ )
    	{
    		if( split[i] != toRemove )
    		{
    			str.push( split[i] );
    		}
    	}
    	
    	str = str.join( delimiter );
    }  
  },
  
  containsDelimitedValue: function( str, value, delimiter )
  {
    if( str && value && delimiter )
    {
      var split = str.split( delimiter );
      str = [];
      for( var i = 0; i < split.length; i++ )
      {
        if( split[i] == value )
        {
          return true;
        }
      }
    }
    
    return false;
  },
  
  defaultEmpty: function( value, defaultValue, prefix, suffix )
  {
  	if( ! value || ( value.length == 0 ) )
  	{
  		if( ! defaultValue )
  		{
  			return '';
  		}
  		else
  		{
  		  return defaultValue;	
  		}
  	}
  	else
  	{
  		if( prefix )
  		  value = prefix + value;
  		  
  		if( suffix )
  		  value += suffix;
  		  
  		return value;
  	}
  }
}




