var inputClass = '';
function gC(e){ return e.all ? e.all : e.getElementsByTagName('*'); }
function getParent(e, t){ if(e == null) return null; else if(e.nodeType == 1 && e.tagName.toLowerCase() == t.toLowerCase())	return e; else return getParent(e.parentNode, t); }
function gebt(t, p){
	var list = new Array(); t = t || '*'; p = p || document;
	if(typeof(p.getElementsByTagName) != 'undefined'){
		list = p.getElementsByTagName(t);
		if(t == '*' && !list.length) list = p.all;
	}else{
		if(t == '*') list = p.all; else if(p.all && p.all.tags) list = p.all.tags(t);
	}
return(list);
}
function resetField(p){
	for(var i = 0, t = ['input','select']; i < t.length; i++){
		for(var j = 0, o = gebt(t[i], p); j < o.length; j++){
			if(o[j].type.indexOf('select') > -1)
				o[j].selectedIndex = -1;
			else
				o[j].value = '';
		}
	}
}
function clone(o, t){ p = getParent(o, t); var i, c; for(i = 0; i < p.childNodes.length; i++) if(p.childNodes[i].tagName && p.childNodes[i].tagName.toUpperCase() == 'DIV') c = i; var clone = p.childNodes[c].cloneNode(true); p.insertBefore(clone, o.parentNode); resetField(clone); }
function findObj(n, d){
	if(typeof(n) == 'object') return(n);
	var p, i , x; if(!d) d = document; if((p = n.indexOf('?')) > 0 && parent.frames.length){ d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p); }
  if(!(x = d[n]) && document.all) x = d.all[n];
	for(i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
  for(i = 0; !x && d.layers && i < d.layers.length; i++) x = findObj(n, d.layers[i].document);
	if(!x && document.getElementById) x = document.getElementById(n);
return(x);
}
function in_array(v, a){
	for(var i = 0; i < a.length; i++)
		if(a[i] == v) return(i);
return (i < a.length) ? i : -1;
}
function getIndex(o){ var i = -1; while(i < o.form.length) if(o.form[++i] == o) return(i); return false; }
function nextField(o, fw){
	if(o.jump){
		var i = (getIndex(o) + (fw ? fw : 1)) % o.form.length;
		while(i < o.form.length && in_array(o.form[i].type, ['a','image','hidden']) != -1){ i++; }
		try{ setFocus(o.form[i]); }catch(e){};
	return false;
	}
}
function getSuffix(alias){ return(alias.replace(/(a|m|Opção)$/, '') == alias ? 'o' : 'a'); }
function isOk(i, a, e){ if(i.value.length == 0) return setLabel(i, a + ' é obrigatóri' + getSuffix(a)); else if(e) return setLabel(i, a + ' é inválid' + getSuffix(a)); nextField(i); return setLabel(i, ''); }
function isEmpty(i, a){ return setLabel(i, i.value.length == 0 ? a + ' é obrigatóri' + getSuffix(a) : ''); }
function isSelected(i, a){ return setLabel(i, i.selectedIndex <= 0 ? a + ' é obrigatóri' + getSuffix(a) : '');  }
// Mask
function getKey(e){ return(document.all ? event.keyCode : e.which); }
function allowedKeys(k){ return(k == '' || k == 0 || k == 8 || k == 9 || k == 13 || k == 27); }
function setMask(e, m){ var k = getKey(e), c = String.fromCharCode(k); return(allowedKeys(k) || (c >= '0' && c <= '9')); }
function isValidSize(f, key, s){ if(f.value.length <= s) return true; setLabel(f, 'Tamanho máximo de ' + s + ' caracteres'); var k = getKey(key), a = k == 0 || k == 8; if(allow) setLabel(f, ''); return(a); }
function onlyNumbers(e){ e.onkeypress = function(e){ return(setMask(e, 'N')); }; }
// Validation
function isMail(i){ return(i.value.length >= 6 && i.value.search(new RegExp('^([a-z0-9_]|\-|\\\.)+@(([a-z0-9_]|\-)+\\\.)+[a-z]{2,4}$','gi')) != -1); }
function isCPF(input){
	var i, j, s, m, d1, d2, c = input.value.replace(/[^0-9]/, '').replace(/([0-9]{2})$/, '-$1');
	if(c.length != 12 || c == '000000000-00' || c == '999999999-99' || c.search(/[0-9]{9}\-[0-9]{2}/g) < 0) return false;
	for(s = 0, i = 0, j = 10; i < 9; i++, j--) s += (parseInt(c.substring(i, i + 1)) * j); m = s % 11; d1 = (m < 2) ? 0 : 11 - m;
	for(s = 0, i = 0, j = 11; i < 11; i++, j--){ if(i == 9) i++; s += (parseInt(c.substring(i, i + 1)) * j); } m = s % 11; d2 = (m < 2) ? 0 : 11 - m;
return(c.substring(10,11) == d1 && c.substring(11,12) == d2);
}
function inRange(n, x, y){ return(n >= x && n <= y); }
function daysIn(m, y){ return(m == 2 ? ((y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) ? 29 : 28) : (m & 1 ? (m < 8 ? 30 : 31) : (m > 7 ? 30 : 31))); }
function isValidDate(y, m, d){
	var strY = y[y.selectedIndex].value, intM = parseInt(m[m.selectedIndex].value), intD = parseInt(d[d.selectedIndex].value);
return((strY.length == 4 && inRange(intM, 1, 12) && inRange(intD, 1, 31)) && (intD <= daysIn(intM, parseInt(strY))));
}
function isDate(d, m, y, a){ return setLabel(y, !isValidDate(y, m, d) ? a + ' é obrigatória' : ''); }
function isCEP(c, d, s){
	var e = 0;
	if(c.value.length != 5) e++;
	if(d.value.length != 3) e++;
return setLabel(c, e == 0 ? '' : 'CEP');
}
// Callbacks
function cbCEP(o, f){
	var i, c = ['endereco','numero','complemento','bairro','cidade'], u = c.length; for(i = 0; i < c.length; i++){ var x = f[0][c[i] + f[1]]; setLabel(x, ''); x.className = inputClass; x.disabled = false; if(x.value != o[i] && o[i] != '') x.value = o[i]; }
	c = f[0]['estado' + f[1]]; setLabel(c, ''); c.className = inputClass; c.disabled = false; c.selectedIndex = -1; for(i = 1; i < c.length; i++){ if(c[i].value == o[u]){ c.selectedIndex = i; break; } }
return setLabel(f[3], '');
}
function doCEP(c, d, s, e){
	if(c.value.length == 5 && d.value.length == 3){ ajax_do_call_uri('cep', 'php/ajax.php', [c.value, d.value,{callback:cbCEP,extra_data:[c.form,s,c,d]}]); return setLabel(d, 'Consultando CEP', 'busca'); }
return setLabel(c, '');
}
document.getElementsBySelector = function(s){
	if(!document.getElementsByTagName) return new Array();
	var tS = s.split(' '), cC = new Array(document);
	for(var i = 0; i < tS.length; i++){
		t = tS[i].replace(/^\s+/,'').replace(/\s+$/,'');;
		if(t.indexOf('#') > -1){ var bits = t.split('#'), tN = bits[0], id = bits[1], e = document.getElementById(id); if(!e || (tN && e.nodeName.toLowerCase() != tN)) return new Array(); cC = new Array(e); continue; }
		if(t.indexOf('.') > -1){
			var bits = t.split('.'), tN = bits[0], className = bits[1]; if(!tN) tN = '*';
			var f = new Array(), fC = 0; for(var h = 0; h < cC.length; h++){ var eS = tN == '*' ? gC(cC[h]) : cC[h].getElementsByTagName(tN); for(var j = 0; j < eS.length; j++) f[fC++] = eS[j]; }
			cC = new Array; var cI = 0; for(var k = 0; k < f.length; k++) if(f[k].className && f[k].className.match(new RegExp('\\b' + className + '\\b'))) cC[cI++] = f[k]; continue;
		}
		if(t.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)){
			var tN = RegExp.$1, aN = RegExp.$2, aO = RegExp.$3, aV = RegExp.$4; if(!tN) tN = '*'; var f = new Array(), fC = 0;
			for(var h = 0; h < cC.length; h++){ var	eS = tN == '*' ? gC(cC[h]) : cC[h].getElementsByTagName(tN); for(var j = 0; j < eS.length; j++) f[fC++] = eS[j]; }
			cC = new Array(); var cI = 0, cF;
			switch(aO){
				case '=': cF = function(e){ return(e.getAttribute(aN) == aV); }; break;
				case '~': cF = function(e){ return(e.getAttribute(aN).match(new RegExp('\\b' + aV + '\\b'))); }; break;
				case '|': cF = function(e){ return(e.getAttribute(aN).match(new RegExp('^' + aV + '-?'))); }; break;
				case '^': cF = function(e){ return(e.getAttribute(aN).indexOf(aV) == 0); }; break;
				case '$': cF = function(e){ return(e.getAttribute(aN).lastIndexOf(aV) == e.getAttribute(aN).length - aV.length); }; break;
				case '*': cF = function(e){ return(e.getAttribute(aN).indexOf(aV) > -1); }; break;
				default : cF = function(e){ return e.getAttribute(aN); };
			}
			cC = new Array(); var cI = 0; for(var k = 0; k < f.length; k++) if(cF(f[k])) cC[cI++] = f[k]; continue;
		}
		if(!cC[0]) return; tN = t; var f = new Array(), fC = 0; for(var h = 0; h < cC.length; h++){ var eS = cC[h].getElementsByTagName(tN); for(var j = 0; j < eS.length; j++) f[fC++] = eS[j]; } cC = f;
	}
return cC;
}
var Behaviour = {
	list: new Array,
	register: function(s){ Behaviour.list.push(s); },
	start: function(){ Behaviour.addEvent(window, 'load', function(){ Behaviour.set(); }, false); },
	set: function(){ var h, l, s, list, e; for(h = 0; l = Behaviour.list[h]; h++){ for(s in l){ list = document.getElementsBySelector(s); if(!list) continue; for(i = 0; e = list[i]; i++) l[s](e); } } },
	addEvent: function(e, eT, f, c){ if(e.addEventListener){ e.addEventListener(eT, f, c); return true; }else if(e.attachEvent){ var r = e.attachEvent('on' + eT, f); return r; }else{ var oldFunction = e['on' + eT]; e['on' + eT] = typeof(oldFunction) == 'function' ? function(){ oldFunction(); f(); } : f; return true; } }
};
Behaviour.start();