
Utils   = function() {}


/*
 * @description Handles jQuery AJAX errors and outputs to javascript console.
 * @param object xhr The jQuery XmlHttpRequest object. 
 * @param string status Short status message about the error. 
 * @param string error More verbose error message. 
 * @param string url The URL involved in the attempted AJAX call.
 * @return null
 */
Utils.prototype.ajaxError  = function( xhr, status, error, url ) {
  if( typeof window.console != 'undefined' ) {
    console.log( ''
      + 'AJAX error: ' + error + '\n'
      + 'status: ' + status + '\n'
      + 'url: ' + url + '\n'
    );
  }
}


/*
 * @description Converts the hash into path by removing the #/ from the beginning
 * @param string hash 
 * @return string
 */
Utils.prototype.hashToPath  = function( hash ) {
  return hash.replace( '#/', '' );
}


/*
 * @description Converts the querystring (?pg=) as hash (#/).
 * @param string qs Querystring from location.search.
 * @return string Reformatted as location.hash.
 */
Utils.prototype.querystringToHash  = function( qs ) {
  var hash  = qs.replace( '?pg=', '#/' );
  return hash;
}

/*
 * @description Check the string for tags in plain text, html entity, url encode. Bounce to root if present.
 * @param string str The string to check for tags.
 * @return boolean
 */
Utils.prototype.sanitizeTags  = function( str ) {
  str           = String( str ).toLowerCase();
  var hasTags   = false;
  if( str.indexOf( '<' ) != -1 )      hasTags   = true;
  if( str.indexOf( '>' ) != -1 )      hasTags   = true;
  if( str.indexOf( '%3c' ) != -1 )    hasTags   = true;
  if( str.indexOf( '%3e' ) != -1 )    hasTags   = true;
  if( str.indexOf( '&lt;' ) != -1 )   hasTags   = true;
  if( str.indexOf( '&gt;' ) != -1 )   hasTags   = true;
  if( str.indexOf( '&#60;' ) != -1 )  hasTags   = true;
  if( str.indexOf( '&#62;' ) != -1 )  hasTags   = true;
  
  if( hasTags ) {
    top.location  = '/';
    return false;
  }
  return true;
}


/*
 * @description Turns any string into a URL-friendly slug.
 * @param string str 
 * @return string
 */
Utils.prototype.slugify  = function( str ) {
  str   = str.toLowerCase();
  str   = str.replace( /\s/gi, "-" );
  str   = str.replace( /\//gi, "-" );
  str   = str.replace( /[^-a-zA-Z0-9,&/\\s]+/ig, '' );
  
  // Trim dash from beginning, end.
  while( str.substr( 0, 1 ) == '-' )  str   = str.substr( 1 );
  while( str.substr( -1 ) == '-' )    str   = str.substr( 0, str.length -1 );
  
  return str;
}


/*
 * @description Removes whitespace from the beginning and end of string.
 * @param str string
 * @return string
 */
Utils.prototype.trim  = function( str ) {
  return str.replace( /^\s+|\s+$/g,'' );
}



