/*
 * Miscellaneous helper functions.

 * Note that this src file must be called AFTER jQuery is called,
 * otherwise JavaScript errors will result.
 */



// **************
// MESSAGING 
// **************

// prediv parameter is a string containing
//  jquery-style name of element that the
//  error box will be prepended into.
function addUserError(msg, prediv, clear) {
  if( $("#userErrors").is("div") ) {
    if(clear) $("#userErrors p").remove();
    $("#userErrors").append('<p>' + msg + '</p>').fadeIn('slow');
  } else {
    $(prediv).prepend(
      '<div id="userErrors">'     +
        '<p>' + msg + '</p>'      +
      '</div>'
    );
  }
}

// prediv parameter is a string containing
//  jquery-style name of element that the
//  message box will be prepended into.
function addUserMessageInset(msg, prediv) {
  if( $("#userMessages").is("div") ) {
    $("#userMessages").append('<p>' + msg + '</p>');
  } else {
    $(prediv).prepend(
      '<div id="userMessages">'     +
        '<p>' + msg + '</p>'      +
      '</div>'
    );
  }
}

function addMessageWindow(wtype, msg) {
  var cl = 'window-body-usermsg';
  if(wtype == 'error') cl = 'window-body-error';

  $('body').append(
    '<div class="messageWindow">' +
      '<div class="window-body ' + cl + '">' +
        '<div class="window-close">x</div>' + 
        '<p>' +
          msg +
        '</p>' +
      '</div>' +
    '</div>'
  );
  $('.messageWindow').Resizable({
    minWidth: 200,
    minHeight: 60,
    maxWidth: 700,
    maxHeight: 400,
    dragHandle: '.messageWindow',
    handlers: {
      se: '#windowResize'
    }
  });
  $('.window-close').click(function() {
    $('.messageWindow').remove();
  });
}

function cleanUserErrors() {
  if( $("#userErrors").is("div") ) {
    $("#userErrors").remove();
  }
}

function cleanUserMessages() {
  if( $("#userMessages").is("div") ) {
    $("#userMessages").remove();
  }
}

/* viewportCenter plugin provided by Brian Reindel
 * http://blog.reindel.com/src/jquery_viewport_center/
 *
 * Originally made avialable publicly on jQuery forums
 * at http://www.nabble.com/viewportCenter()-plugin-now-in-beta...-t3576725s15494.html
 */
jQuery.fn.viewportCenter = function(){
  return this.each(function(){
    d = {};
    if (self.innerHeight) {
      d.pageYOffset = self.pageYOffset;
      d.pageXOffset = self.pageXOffset;
      d.innerHeight = self.innerHeight;
      d.innerWidth = self.innerWidth;
    } else if (document.documentElement && document.documentElement.clientHeight) {
      d.pageYOffset = document.documentElement.scrollTop;
      d.pageXOffset = document.documentElement.scrollLeft;
      d.innerHeight = document.documentElement.clientHeight;
      d.innerWidth = document.documentElement.clientWidth;
    } else if (document.body) {
      d.pageYOffset = document.body.scrollTop;
      d.pageXOffset = document.body.scrollLeft;
      d.innerHeight = document.body.clientHeight;
      d.innerWidth = document.body.clientWidth;
    }
    $(this).remove().appendTo("body").css("position","absolute");
    $(this).css("top",Math.round(d.innerHeight/2) + d.pageYOffset - Math.round($(this).height()/2));
    $(this).css("left",Math.round(d.innerWidth/2) + d.pageXOffset - Math.round($(this).width()/2));
    d = null;
  });
};


// ***************************
// PHP-LIKE HELPER FUNCTIONS
// ***************************

/*
function htmlEntities(texto){
  //by Micox - elmicoxcodes.blogspot.com - www.ievolutionweb.com
  var i;
  var carac;
  var letra;
  var novo = '';

  for(i = 0; i < texto.length; i++) {
alert("t" + texto);
    carac = texto[i].charCodeAt(0);
    if( (carac > 47 && carac < 58) || (carac > 62 && carac < 127) ){
      //se for numero ou letra normal
      novo += texto[i];
    } else {
      novo += "&#" + texto[i].charCodeAt(0) + ";";
    }
  }
alert("n" + novo);
  return novo;
}
*/

String.prototype.htmlEntities = function () {
   return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
};

function addslashes(str) {
  str=str.replace(/\\/g,'\\\\');
  str=str.replace(/\'/g,'\\\'');
  str=str.replace(/\"/g,'\\"');
  str=str.replace(/\0/g,'\\0');
  return str;
}

function isset(varname){
  if(typeof(varname) === 'undefined') {
    return false;
  } else {
    return true;
  }
}


/*
 * Prepares textarea input for html output by
 *  converting \n\n to <p> wrapper and \n
 *  to <br /> when needed.
 * Also runs text through c() html cleansing
 *  function before wrapping and returning.
 */
function textareaOutput(intext) {
  var p_chunks = intext.split("\n\n");
  var new_p_chunks = new Array();
  var cpl = p_chunks.length;
  for(j = 0; j < cpl; j++) {
    var br_chunks = p_chunks[j].split("\n");
    var new_br_chunks = new Array();
    var l = br_chunks.length;
    for(i = 0; i < l; i++) {
      if(typeof(br_chunks[i+1]) == 'string') {
        new_br_chunks[i] = br_chunks[i].htmlEntities() + '<br />';
      } else {
        new_br_chunks[i] = br_chunks[i].htmlEntities();
      }
    }
    new_p_chunks[j] = '<p>' + new_br_chunks.join(' ') + '</p>';
  }
  var newtext = new_p_chunks.join(' ');
  return newtext;
}

