 
function addOption(oListbox, sName, sValue) 
{
	var oOption = document.createElement("option");				
	oOption.appendChild(document.createTextNode(sName));				
	if (arguments.length == 3) 
	{				
		oOption.setAttribute("value", sValue);				
	}
	oListbox.appendChild(oOption);				
}


function gup( name )
{			
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");			
  var regexS = "[\\?&]"+name+"=([^&#]*)";			
  var regex = new RegExp( regexS );			
  var results = regex.exec( window.location.href );			
  if( results == null )			
    return "";			
  else			
    return results[1];			
}


function in_array (needle, haystack, argStrict) 
{
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
 
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}


$(function()
{
  //ex: $('#xpto').replaceWith('<div></div>');
  $.fn.replaceWith = function(html) { return this.after(html).remove(); }; 

  //devolver a tagname de um elemento
  $.fn.tagName = function()
  {
    if(this.length == 1)
      return this[0].tagName.toLowerCase();
    else
    {
      var tagNames = [];
      this.each(function(i, el)
      {
              tagNames[i] = el.tagName.toLowerCase();
      });
      return tagNames;
    }
  };


  //adicionar um opção a um combobox
  $.fn.addOption=function(sName,sValue,select)  
  {
    if($(this).tagName()!="select")
      return false;
      
    option=$('<option value="'+sName+'">'+sValue+'</option>');
    
    if (arguments.length == 3 && select==true) 
	  {
      $(this).find('option:selected').removeAttr('selected');
      option.attr('selected','selected');
    }
    
    $(this).append(option);
  };
  
  $.fn.exists = function() { return (this.length > 0); };
  
  $.show_error_msg = function(id,type,error_msg)
  {
    if($('#'+id+'_ok').exists())
      $('#'+id+'_ok').remove();
    
    text=error_msg[id+'_'+type];
    if(!$('#'+id+'_msg').exists())
      $('<p id="'+id+'_msg" class="error_msg">'+text+'</p>').insertAfter('#'+id).show();
    else if(text!=$('#'+id+'_msg').text())
      $('#'+id+'_msg').text(text);
    
    if(!$('#'+id+'_error').exists())
      $('<span class="error_img" id="'+id+'_error"></span>').insertAfter('#'+id).show();
  
    if(!$.browser.msie)
      $('#'+id).focus();
  };
  
  $.hide_error_msg = function(id)
  {
    if($('#'+id+'_error').exists())
      $('#'+id+'_error').remove();
      
    if($('#'+id+'_msg').exists())
      $('#'+id+'_msg').hide('slow',function(){$(this).remove()});
    
    if(!$('#'+id+'_ok').exists())
      $('<span class="ok_img" id="'+id+'_ok"></span>').insertAfter('#'+id).show();
  };
  
  $.alert=function(msg,callback)
  {
    if($.facebox!="undefined")
    {
      alertHTML="<div class='facebox_alert'> \
                    <p class='msg_container'>"+msg+"</p> \
                    <p> <input class='input_dialog' type='button' value='OK'/> </p>\
                  </div>";
      $(document).trigger('close.facebox'); //fechar qq facebox aberta
      $(document).bind('afterReveal.facebox',function(){$('#facebox .footer').hide();});      
      $.facebox(alertHTML);       
      $('#facebox .input_dialog').click(function()
      {
        if(callback)callback();
        $(document).trigger('close.facebox');
        $('#facebox .input_dialog').unbind('click');
      });
      $(document).unbind('afterReveal.facebox');
      $('#facebox_overlay').unbind('click');            
    }
  };
  
  $.confirm=function(msg,yes_callback,no_callback)
  {
    if($.facebox!="undefined")
    {
      alertHTML="<div class='facebox_confirm'> \
                    <p class='msg_container'>"+msg+"</p> \
                    <p> \
                      <input class='input_dialog' type='button' value='Sim'/> \
                      <input class='input_dialog' type='button' value='Não'/> \
                    </p>\
                  </div>";
      $(document).trigger('close.facebox'); //fechar qq facebox aberta
      $(document).bind('afterReveal.facebox',function(){$('#facebox .footer').hide();});      
      $.facebox(alertHTML);      
      $('#facebox .input_dialog').click(function()
      {                  
        $(document).trigger('close.facebox');
        $('#facebox .input_dialog').unbind('click');
        if($(this).val()=="Sim" && yes_callback)window.setTimeout(yes_callback, 500);
        else if($(this).val()=="Não" && no_callback)window.setTimeout(no_callback, 500);
      });
      $(document).unbind('afterReveal.facebox');
      $('#facebox_overlay').unbind('click');
    }
  };

  var Loading=function()
  {
    var _show=function()
    {
      $('#loading_overlay').fadeIn("fast");
      $('#loading_overlay_box').fadeIn("fast");
    };

    return {
      _hide:function()
      {
        $('#loading_overlay_box').fadeOut("fast");
        $('#loading_overlay').fadeOut("fast");
      },
      _init:function(options)
      {
        options = $.extend(
    		{
    			msg:	"",
    			imgsrc:	"/media/images/ajax-loader.gif"
    		}, options);
      		
        if($('#loading_overlay_box').exists() && $('#loading_overlay').exists())
        {
          $('#loading_overlay_message').html(options.msg);
          $('#loading_overlay_img').attr('src',options.imgsrc);
          _show();
        }
        else
        {          
      		var overlayHeight = $(document).height();
          var overlayWidth = $(window).width();

      		var overlay=$('<div></div>');
      		overlay.attr('id','loading_overlay');
          overlay.css({'position':'fixed'});
          overlay.css({'height':overlayHeight,'width':overlayWidth});
          overlay.css({'left':0,'top':0,'z-index':1001,'opacity':0.5});
          overlay.css({'cursor':'wait','background-color':'#FFF'});
          overlay.hide();

      		var image=$("<img/>");
      		image.attr("src",options.imgsrc);
      		image.attr("id",'loading_overlay_img');
      		image.css({"width":"65px","height":"65px"});
      		
          if(options.msg!="")
      		{
        		var message = $("<div></div>");
        		message.attr('id','loading_overlay_message');
        		message.html(options.msg);
        	}

      		var box=$('<div></div>');
      		box.attr('id','loading_overlay_box');
      		box.append(image);
          if(options.msg!="")
      		  box.append(message);
          box.hide();

      		var boxLeft=$(window).width()/2-box.width()/2;
      		var boxTop=$(window).height()/2-box.height()/2;

      		box.css({'position':'fixed'});
      		box.css({'left':boxLeft,
                    'top':boxTop,
                    'z-index':1002});

          $('body').append(overlay);
          $('body').append(box);

          _show();
        }
      }
    };

  }();

  $.extend({showLoading:Loading._init,hideLoading:Loading._hide});
});