//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;
}

var Browser = new Object();
Browser.isIE = isIE;
Browser.isOpera = isOpera;

function Erro(msg, a){
	if(a) alert(msg);
	debbug(msg, true);
	throw new Error(msg);
}

function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;
    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }
    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }
    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;
    var x = Math.max(x1, Math.max(x2, x3)), y = Math.max(y1, Math.max(y2, y3));
    window.scrollTo(Math.floor(x / 1.5), Math.floor(y / 1.5));
    if (x > 0 || y > 0)  window.setTimeout("backToTop()", 30);
}
window.backToTop = backToTop;

function addEvent(obj, evType, fn){
	if(obj.addEventListener) obj.addEventListener(evType, fn, true);
	if(obj.attachEvent)	obj.attachEvent("on" + evType, fn);
}

function initValue(o){
	if(o.target != undefined) var o = o.target;
	else if(o.srcElement) var o = o.srcElement;
	if(o.initVal == undefined) o.initVal = o.value;
	if(typeof o.onblur != "function") o.onblur = function(){ if(this.value.length < 1) this.value = this.initVal; }
	if(o.value == o.initVal) o.value = "";
}

function alternateRows(o, a, st){
	var iColor = 0, l = o.childNodes.length, oc = o.childNodes;
	for(var i=0; i<l; i++){
		if(oc[i].nodeType == 1){
			var obj = oc[i];
			if(st != undefined){
				obj = $t("a", oc[i])[0];
			}
			obj.style.backgroundColor = a[iColor];
			iColor = iColor == 0 ? 1 : 0;
		}
	}
}

function setClass(obj, nClass){
	if(nClass != undefined){
		if(obj.lastClass == undefined){
			obj.lastClass = null;
		}
		obj.lastClass = obj.className || "";
		obj.className = nClass;
	} else {
		obj.className = obj.lastClass;
	}
}

function changeSelects(){
	var s = $t("SELECT", document);
	if(s != null){
		for(var i=0; i<s.length; i++){
			s[i].style.visibility = s[i].style.visibility.toString().indexOf("hid") > -1 ? "visible" : "hidden";
		}
	}
}

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){
		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 s = this.toString().replace(/^\s*|\s*$/g, ""), r = "", i = 0;   
	for (i=0; i <= s.length; i++){
		if(!(s.charCodeAt(i) == Key.SPACE ||
		  s.charCodeAt(i) == Key.CARRIAGE ||
		  s.charCodeAt(i) == Key.LINEFEED ||
		  s.charCodeAt(i) == Key.TAB)) r = r + s.charAt(i);
	}  
	return unescape(r.replace(/\+/g, " "));
}
String.prototype.replaceAll = function(f, r){
	var s = this.toString();
	while(s.indexOf(f) > -1) s = s.replace(f, r);
	return s;
}

function chr(n){
	return String.fromCharCode(n) || null;
}

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(document.getElementById && document.getElementById(o) != undefined && document.getElementById(o) != null){
				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 $e(el, t, i){
	if(!document.createElement) return false;
	var nEl = document.createElement(el.toString());
	if(t != undefined && t.length > 0 && !i) nEl.appendChild(document.createTextNode(t));
	else if(i == true) nEl.innerHTML = t;
	return nEl;
}

function $t(t, p){
	if(typeof p == "undefined" && p == null && !p) var p = document;
	if(typeof p == "string") var p = $(p);
	return p.getElementsByTagName(t).length > 0 ? p.getElementsByTagName(t) : null;
}

function $tc(c, t, p){
	var $tags = $t(t, p), elements = new Array(), 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);
}

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('<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) {
		if(typeof window.onload != "function"){
			window.onload = bodyLoad.onLoad;
		} else {
			window.oldLoad = window.onload;
			window.onload = function(){
				Delegate.create(window, bodyLoad.onLoad)();
				Delegate.create(window, window.oldLoad)();
			}
		}
	}
}
var BodyLoad = bodyLoad;

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;
}


// Máscara de telefone, onkeypress="return mMascaraTelefone(this, event);" 
function mMascaraTelefone(Campo, e) {
    var key = "";
    var len = 0;
    var strCheck = "0123456789";
    var aux = "";
    var whichCode = (window.Event) ? e.which : e.keyCode;
    
    if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true; 
    key = String.fromCharCode(whichCode); 
    if (strCheck.indexOf(key) == -1) return false;
    
    aux =  Telefone_Remove_Format(Campo.value); 
    
    len = aux.length; 
    if(len >= 10) return false;
    aux += key; 
     
    Campo.value = Telefone_Mont_Format(aux); 
    return false; 
} 
// Utilizada pela mascara telefone
function Telefone_Mont_Format(Telefone){ 
    var aux = len = ""; 
    len = Telefone.length; 
    
    if(len <= 9) tmp = 6; 
    else tmp = 6;
    
    aux = ""; 
    for(i = 0; i < len; i++){ 
        if(i == 0)aux = "("; 
        aux += Telefone.charAt(i); 
        if(i+1 == 2) aux += ") "; 
        
        if(i+1==tmp) aux += "-";
    } 
    return aux ; 
} 
// Utilizado pela mascara telefone
function Telefone_Remove_Format(Telefone){ 
    var strCheck = "0123456789"; 
    var len = i = aux = ""; 
    len = Telefone.length; 
    for(i = 0; i < len; i++){ 
        if (strCheck.indexOf(Telefone.charAt(i))!= -1) aux += Telefone.charAt(i);
    } 
    return aux; 
}
