$(function(){
	$("#logo").click(function() {
		document.location = "default.php";
	});
});

function setError(msg, scroll)
{
	closeMessage();
	if(scroll) $(window).scrollTop(0);
	$("#errortext").html(msg);
	$("#errorbox").show("fast");
}

function closeError()
{
	$("#errortext").html("");
	$("#errorbox").hide("fast");
}

function setMessage(msg, scroll)
{
	closeError();
	$("#msgtext").html(msg);
	$("#messagebox").show("fast", function() {
		if(scroll) $(window).scrollTop(0);
	});
	setTimeout("closeMessage()", 5000);
}

function setMessageTime(msg, scroll)
{
	closeError();
	$("#msgtext").html(msg);
	$("#messagebox").show("fast", function() {
		if(scroll) $(window).scrollTop(0);
	});
	// setTimeout("closeMessage()", 5000);
}

function closeMessage()
{
	$("#msgtext").html("");
	$("#messagebox").hide("fast");
}

function loadJCombo(slt, file, val, callback)
{
	alert("loadJCombo");
	slt.options.length = 0;
	slt.disabled = true;
	$.getJSON(file, function(data) {	
		$.each(data.items, function(i, item) {
			slt.options.add(new Option(item.name, item.id));
		});
		if(parseInt(val) > 0) {
			slt.value = val;
		}
		if(parseInt(slt.options.length) > 1) {
			slt.disabled = false;
		}
		if(typeof callback == "function") {
			callback.apply(this);
		}
	});
}

/*
 * Uso:
 * 
 * function foo(arg1, arg2, arg3, ...) {
 *     debugParamsFunction(arguments);
 *     ...
 * }
 * 
 * 
 */
function debugParamsFunction(args)
{
	var outFunc = args.callee.toString();
	outFunc = outFunc.substr('function '.length);
	outFunc = outFunc.substr(0, outFunc.indexOf('('));
	outFunc += " (";
	for(var n=0; n<args.length; n++) {
		if(typeof args[n] == "string") outFunc += "'";
		outFunc += args[n];
		if(typeof args[n] == "string") outFunc += "'";
		if(n < (args.length-1)) {
			outFunc += ", ";
		}
	}
	outFunc += ");";
	alert(outFunc);
}

function base64_encode(data)
{
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];

    if (!data) {
        return data;
    }

    //data = this.utf8_encode(data+'');

    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1<<16 | o2<<8 | o3;

        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);

    enc = tmp_arr.join('');

    switch (data.length % 3) {
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }

    return enc;
}
