//All Functions in util.js by Lucas Ferreira - www.lucasferreira.com

function isIE(){
	return (window.ActiveXObject && document.all && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1) ? true : false;
}

function isOpera(){
	return (window.ActiveXObject == undefined && navigator.userAgent.toLowerCase().indexOf("opera") > -1) ? true : false;
}

function Erro(msg, a){
	if(a) alert(msg);
	debbug(msg, true);
	throw new Error(msg);
}

if(Ajax == undefined){
	var Ajax = new Object();
}
Ajax.getTransport = function(){
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	for(var i=0; i<prefixes.length; i++){
		try {
			return new ActiveXObject(prefixes[i] + ".XmlHttp");
		} catch(e) { continue; }
	}
	try {
		return new XMLHttpRequest();
	} catch(e){
		throw new Erro("Can't find XMLHttpRequest Object in you browser, please verify your system\nNão foi possível encontrar o objeto XMLHttpRequest em seu navegador, favor verificar seu sistema.\n" + e, true);
		return false;
	}
}

if(Key == undefined){
	var Key = {
		version: "1.0"
	}
}
Key.TAB = 9;
Key.LINEFEED = 10;
Key.CARRIAGE = 13;
Key.SPACE = 32;

String.prototype.trim = function() {
	
	var TAB = Key.TAB;
	var LINEFEED = Key.LINEFEED;
	var CARRIAGE = Key.CARRIAGE; 
	var SPACE = Key.SPACE; 
	var s = this.toString(), r = "", i = 0;   
	for (i=0; i <= s.length; i++){
		if(!(s.charCodeAt(i) == SPACE || s.charCodeAt(i) == CARRIAGE || s.charCodeAt(i) == LINEFEED || s.charCodeAt(i) == TAB)){
			r = r + s.charAt(i);
		}
	}  
	r = r.replace(/\+/g, " ");
	r = unescape(r);
	return r;
	
}

String.prototype.replaceAll = function(f, r){
	
	var s = this.toString();
	while(s.indexOf(f) > -1){
		s = s.replace(f, r);
	}
	
	return s;
	
}

function sleep(s){
	var st = (new Date()).getSeconds();
	var s = new Number(s);
	while(1){
		var at = (new Date()).getSeconds();
		if((at-st) >= s) break;
	}
}

function getTargetByEvent(e){
	if(typeof(e) == "undefined") var e = window.event;
	source = e.target ? e.target : e.srcElement;
	return source;
}

function $(){
	
	var errorMsg = "Can't find object (Não foi possível encontrar o Objeto)";
	var elements = new Array();
	var args = $.arguments;
	for(var i=0; i<args.length; i++){
		var o = args[i];
		try {
			if(typeof document.getElementById(o) != "undefined" && document.getElementById(o) != null && document.getElementById){
				elements.push(document.getElementById(o));
			} else if(isIE() && document.all && typeof document.all[o] != "undefined") {
				elements.push(document.all[o]);
			} else if(document.layers && typeof document[o] != "undefined") {
				elements.push(document[o]);
			} else {
				new Erro(errorMsg);
				return false;
			}
			if(args.length <= 1) return elements[0];
		} catch(e) {
			throw new Erro(errorMsg + "\n" + e);
		}
	}
	
	return elements
	
}

function $t(t, p){
	if(typeof p == "undefined" && p == null && !p) {
		var p = document;
	}
	return p.getElementsByTagName(t).length > 0 ? p.getElementsByTagName(t) : null;
}

function $tc(c, t, p){
	var $tags = $t(t, p);
	var elements = new Array();
	var args = $tc.arguments;
	for(var i=0; i<$tags.length; i++){
		if($tags[i].className == c){
			elements.push($tags[i]);
		}
	}
	return (elements.length > 0 ? elements : null);
}

function $v(o, form){
	if(form != undefined && form) {
		var o = document[form][o];
		if(o.length && o.length > 0
				&& o.type == undefined && !o.type){
			o.type = o[0].type;
		}
	} else var o = $(o);
	if(o == undefined || !o) return false;
	if(o.type && o.type.toString().indexOf("select") > -1){
		if(o.type.toString().indexOf("mult") < 0){
			return o.options[o.selectedIndex].value;
		} else {
			var values = new Array();
			for(var i=0; i<o.options.length; i++){
				if(o.options[i].selected){
					values.push(o.options[i].value);
				}
			}
			return values;
		}
	} else if(o.type && o.type.toString().indexOf("radio") > -1){
			for(var i=0; i<o.length; i++){
				if(o[i].checked){
					return o[i].value;
				}
			}
	} else if(o.type && o.type.toString().indexOf("check") > -1){
			var values = new Array();
			for(var i=0; i<o.length; i++){
				if(o[i].checked){
					values.push(o[i].value);
				}
			}
			return values;
	} else if(o.type) {
		return o.value.toString();
	} else {
		return o.innerHTML;
	}
}

StatusBar = {
	version: "1.0"
}
StatusBar.msg = new String();
StatusBar.set = function(msg){
	
	if(msg != undefined && msg.length > 0){
		window.defaultStatus = msg;
		StatusBar.msg = window.defaultStatus.toString();
	}

}
StatusBar.get = function(){
	return window.defaultStatus.toString();
}

var PopUp = {
	version: "1.1b"
}
PopUp.windows = new Object();
PopUp.reservedProps = new Array("center", "msgBlock", "");
PopUp.open = function(src, name, props){
	
	this.props = props;
	this.props["width"] = this.props["width"] != undefined && parseInt(this.props["width"]) > 0 ? parseInt(this.props["width"]) : 100;
	this.props["height"] = this.props["height"] != undefined && parseInt(this.props["height"]) > 0 ? parseInt(this.props["height"]) : 100;
	
	PopUp.windows[name] = window.open(src, name + "_" + (new Date()).getSeconds(), PopUp.getProps(this.props));
	
	if((PopUp.windows[name] == null || PopUp.windows[name] == false)
				&& this.props["msgBlock"] != undefined && this.props["msgBlock"].length > 0){
		alert(this.props["msgBlock"]);
		PopUp.killPop(name);
		return false;
	} else {
		if(this.props["center"] != undefined && this.props["center"] == true){
			swidth  = !screen.width 	 ? 	screenLeft 	: 	screen.width;
			sheight = !screen.height	 ? 	screenTop 	: 	screen.height;
			PopUp.windows[name].moveTo((swidth-this.props["width"])/2, (sheight-this.props["height"])/2);
		}
		PopUp.windows[name].focus();
	}
}
PopUp.getProps = function(props){
	var propsConf = new String();
	for(var i in props){
		var isReserved = false;
		for(var j=0; j<PopUp.reservedProps.length; j++){
			if(i.toString().toLowerCase() == PopUp.reservedProps[j].toString().toLowerCase()){
				isReserved = true;
				break;
			}
		}
		if(isReserved == false){
			propsConf += i.toString() + "=" + props[i] + ",";
		}
	}
	return propsConf.substr(0, propsConf.length-1);
}
PopUp.killPop = function(name){
	if(PopUp.windows[name] != undefined){
		if(PopUp.windows[name] != null){
			PopUp.windows[name].close();
		}
		delete PopUp.windows[name];
		return true;
	}
	return false;
}
PopUp.focus = function(name){
	PopUp.windows[name].focus();
}
PopUp.blur = function(name){
	PopUp.windows[name].blur();
}
PopUp.close = function(name){
	return PopUp.killPop(name);
}

function debbug(v, a){
	if(document.getElementById("debbuger") != undefined){
		if(!a || a == undefined) $("debbuger").innerHTML = "";
		$("debbuger").innerHTML += v + (a ? "<BR />" + String.fromCharCode(13) : "");
	}
}

debbug.createObject = function(){
	if(document.getElementById("debbuger") == undefined){
		this.htmlString = new String();
		this.htmlString += '<span id="debbuger" style="padding: 10px; border: 1px solid #000000; display: block; width: 70%;"></span>';
		document.write(this.htmlString);
	}
}

var bodyLoad = {
	version: "1.0"
}
bodyLoad.onloads = new Array();
bodyLoad.add = function(f){
	if(typeof f == "function"){
		bodyLoad.onloads.push(f);
	}
}
bodyLoad.onLoad = function(){
	for(var i=0; i<bodyLoad.onloads.length; i++){
		bodyLoad.onloads[i].call(this);
	}
}
bodyLoad.init = function(){
	if(window) window.onload = bodyLoad.onLoad;
}

Delegate = {
	version: "1.0"
}
Delegate.create = function(obj, func, args){
	var f = function(){
		var target = arguments.callee.target;
		var func = arguments.callee.func;
		var args = arguments.callee.args;
		return func.apply(target, (args.length < 1 ? arguments : args));
	};
	f.args = (args != undefined && args.length > 0 ? args : new Array());
	f.target = obj;
	f.func = func;
	return f;
}
