function onEnter(event, func, obj)
{
	var code = event.keyCode;
    if(code == 13)
	{
		if(obj == undefined)func();
		else
		{
			func.apply(obj);
		}//else
	}//if enter
}//onEnter

function displayBox(id)
{
	$('#' + id).show(700);
}//displayBox

function hideBox(id)
{
	$('#' + id).hide(700);
}//hideBox

function switchBox(id, black_bg)
{
	var $box = $('#' + id);
	var css;

	if($box.css('display') == 'none')
	{
		$box.show(700);
		if(black_bg == true)
		{
			switchBlackLayer(true);
		}//if black_bg
	}//if !box
	else
	{
		$box.hide(700);
		switchBlackLayer(false);
	}//else
}//switchBox

function switchBlackLayer(on, hide_select)
{
	if(on != false)
	{
		$('#blackLayer').css({display:'block', height:$('html')[0].scrollHeight});
		if(hide_select != false)$('select').hide();
	}//if on
	else
	{
		$('#blackLayer').css({display:'none'});
		if(hide_select != false)$('select').show();
	}//else
}//switchBlackLayer

function swapBoxes(id1, id2, black_bg)
{
	switchBox(id1, black_bg);
	switchBox(id2, black_bg);
}//switchBox

function centerBox(id, offset_w, offset_h)
{
	var pos_l = $(document).scrollLeft() + (($(window).width() - $('#' + id).width()) / 2);
	var pos_t = $(document).scrollTop() + (($(window).height() - $('#' + id).height()) / 2);

	if(offset_w != undefined)pos_l += offset_w;
	if(offset_h != undefined)pos_t += offset_h;

	$('#' + id).css('left', pos_l);
	$('#' + id).css('top', pos_t);
}//centerBox

function setLoading(on)
{
	if(on)
	{
		centerBox('loadingBox');
		$('#loadingBox').show();
	}//if on
	else
	{
		$('#loadingBox').hide();
	}//else off
}//setLoading

function errorBox(msg, html_msg)
{
	var box_id = (html_msg != true)?('errorBox'):('errorBoxHtml');

	if(msg != undefined && msg != '' && msg != null)
	{
		if(html_msg != true)$('#' + box_id + 'Text').val(msg);
			else $('#' + box_id + 'Text').html(msg);

		centerBox(box_id);

		$('#' + box_id).show();
	}//if msg
	else
	{
		$('#' + box_id).hide();
	}//else
}//errorBox

var messageBoxTO = 666;
var messageBoxCallback = null;

function messageBox(msg, callback, time)
{
	if(messageBoxTO != 666)clearTimeout(messageBoxTO);
	if(callback != undefined)messageBoxCallback = callback;

	if(msg != undefined && msg != 0)
	{
		centerBox('messageBox');
		$('#messageBox').html('<div>' + msg + '</div>');
		$('#messageBox').show();
		messageBoxTO = setTimeout(function(){messageBox()}, (time == undefined)?(1000):(time));
	}//if msg
	else
	{
		$('#messageBox').hide();
		if(typeof(messageBoxCallback) == 'function')messageBoxCallback();
		messageBoxCallback = null;
	}//else hide
}//messageBox

function freeBox(content)
{
	if(content != undefined && content != '' && content != null)
	{
		$('#freeBox').html(content);
		centerBox('freeBox');
		$('#freeBox').show();
	}//if msg
	else $('#freeBox').hide();
}//freeBox

function fillContainer(html, id)
{
	$('#' + id).html(html);
}//fillContainer

function addAfter(html, sel)
{
	$(sel).after(html)
}//addAfter

function addBefore(html, sel)
{
	$(sel).before(html)
}//addBefore

function removeElement(sel)
{
	$(sel).remove();
}//removeElement

function prependTo(html, css_q)
{
	$(css_q).prepend(html);
}//prependTo

function setFieldValue(id, value)
{
	$('#' + id).val(value)
}//setFields

function setFieldAttr(id, attr, value)
{
	$('#' + id).attr(attr, value)
}//setFieldAttr

function setFields(obj)
{
	for(var field in obj)
	{
		$('#' + field).val(obj[field])
	}//for field in obj
}//setFields

function confirmSubmit(msg)
{
   var agree = confirm(msg);
   if(agree)return true;
	else return false;
}//confirmSubmit

function confirmAction(msg, action)
{
	$('#confirmBox').data('action', action);
	$('#confirmBox').children('.message').html(msg);

	centerBox('confirmBox');
	displayBox('confirmBox');
}//confirmSubmit

function gotoAnchor(name)
{
	window.location = '#' + name;
}//gotoAnchor

function getTagWords(tags_str)
{
	var closed_words = new Array();

	tags_str = tags_str.replace(',', '');

	if(tags_str.indexOf('"') > -1)
	{
		var word_i = -1;
		var tags_str_cp = tags_str;

		for(var i = 0; i < tags_str_cp.length; i++)
		{
			var _char = tags_str_cp.charAt(i);

			if(_char == '"')
			{
				if(word_i == -1)
				{
					word_i = i + 1;
				}//if word starting
				else
				{
					var word = tags_str_cp.substring(word_i, i);
					closed_words.push(word);

					tags_str = tags_str.replace('"' + word + '"', '');

					word_i = -1;
				}//word found
			}//if "
		}//for i
	}//if "

	var tags_w = closed_words.join(',');
	var tags_s = trim(tags_str);
	var tags_arr = tags_s.split(' ');

	var tags = tags_w;
	var sep = '';
	if(tags.length > 0)sep = ',';

	for(var ti = 0; ti < tags_arr.length; ti++)
	{
		if(tags_arr[ti].length > 0)tags += sep + tags_arr[ti];
		sep = ',';
	}//for ii

	return tags;
}//getTagWords

function getFlash(movieName)
{
    if(navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[movieName];
    }//if IE
    else
    {
    	if(document[movieName] == undefined)
    	{
    		alert('CANT FIND FLASH ' + movieName);
    		return null;
    	}//if !movie
    	if(document[movieName][1] == undefined)return document[movieName];
    		else return document[movieName][1];
    }//else others
}//getFlash

// USANDO VERSÃO MODIFICADA DO THICKBOX

function thickBox(title, width, height, html_str, modal)
{
	tb_showDirect(title, width, height, html_str, modal);
}//thickBox

function thickBoxClose()
{
	tb_remove();
}//thickBoxClose

// ALIENS!

function trim(str, chars)
{
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/**
* Function : dump()
* Arguments: The data - array,hash(associative array), object
*    		 The level - OPTIONAL
* Returns  : The textual representation of the array.
*
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*
* http://www.openjs.com/scripts/others/dump_function_php_print_r.php
*/
function dump(arr, level)
{
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}//dump

function utf8_encode(argString)
{
    // Encodes an ISO-8859-1 string to UTF-8
    //
    // version: 909.322
    // discuss at: http://phpjs.org/functions/utf8_encode

    var string = (argString+''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");

    var utftext = "";
    var start, end;
    var stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }

    if (end > start) {
        utftext += string.substring(start, string.length);
    }

    return utftext;
}//utf8_encode

function utf8_decode ( str_data ) {
    // Converts a UTF-8 encoded string to ISO-8859-1
    //
    // version: 909.322
    // discuss at: http://phpjs.org/functions/utf8_decode
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
    var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;

    str_data += '';

    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return tmp_arr.join('');
}


function serialize(mixed_value)
{
    // Returns a string representation of variable (which can later be unserialized)
    //
    // version: 910.813
    // discuss at: http://phpjs.org/functions/serialize
    var _getType = function (inp) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';

    switch (type) {
        case "function":
            val = "";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            //val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            //break;
        case "string":
            mixed_value = this.utf8_encode(mixed_value);
            val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            /*
            if (type == "object") {
                var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {
                    return;
                }
                objname[1] = this.serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }
            */
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") {
                    continue;
                }

                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += this.serialize(okey) +
                        this.serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
        case "undefined": // Fall-through
        default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
            val = "N";
            break;
    }
    if (type != "object" && type != "array") {
        val += ";";
    }
    return val;
}//serialize
