function chgIco(o,on){ (o.hasChildNodes() ? o.firstChild : o).src = '/ico/check' + (on ? 2 : 0) + '.gif'; }
function tryZIP(a){
	var o = gebt('input', getParent(a, 'P'));
	console.log(o.value);
	if(o.value != ''){
		var p = getParent(o, 'DIV'), s = gebc('get', p, 'span')[0]; s.style.display = 'block';
		ajax_do_call_uri('','/webservice/ajax/cep.php?cep=' + o.value,[{'callback':setZIP,'extra_data':{'p':p,'s':s,'a':a.parentNode}}]);
	}else
		alert('Informe o CEP que deseja pesquisar o endereço.');
}
function skipCEP(o){ var p = getParent(o, 'DIV'); o.style.display = 'none'; gebc('set', p, 'span')[0].style.display = 'block'; }
function getZIP(o){
	if(o.value != ''){
		var p = getParent(o, 'DIV'), s = gebc('get', p, 'span')[0]; s.style.display = 'block';
		ajax_do_call_uri('','/webservice/ajax/cep.php?cep=' + o.value,[{'callback':setZIP,'extra_data':{'p':p,'s':s,'a':getParent(o, 'P')}}]);
	}
}
function setZIP(z,o){
	o.s.style.display = 'none';
	gebc('set', o.p, 'span')[0].style.display = 'block';
	gebc('unk', o.a, 'a')[0].style.display = 'none';
	function getFieldName(field){ return(field.id.replace(/\[.+/, '')); }
	var i, n, input = gebt('input', o.p), select = gebt('select', o.p);
	for(i = 0; i < input.length; i++) if(typeof(z[n = getFieldName(input[i])]) != 'undefined') input[i].value = z[n];
	for(i = 0; i < select.length; i++) if(typeof(z[n = getFieldName(select[i])]) != 'undefined') select[i].selectedIndex = z[n];
}
function addFlag(o, ok){ if(o.type == 'textarea') return(ok); if(o) o.className = ok ? 'cp' : 'err'; return(ok); }
function addFlagIco(o, ok, flag){ findObj(flag).src = '/ico/action_' + (ok ? 'success' : 'fail') + '2.gif'; return(addFlag(o, ok)); }
function addMsgErr(o, ok, msg, suf){ if(!ok) err_msg += '\n- ' + msg + ' é ' + (!is_null(suf) ? suf : 'obrigatório') + '.'; return(addFlag(o, ok)); }
function addMsgErrCustom(o, ok, msg){ if(!ok) err_msg += '\n- ' + msg; return(addFlag(o, ok)); }
function hasPrefix(o, l){
	if(!isEmpty(o, l))
		addMsgErrCustom(o, !/^([DS]rt?a?\.?|Doutora?|Senhora?|Seu|Dona) /i.test(o.value), 'Não é permitido prefixos no nome tais como: Sr, Sra, Dr, Dra, etc.');
}
function isEmpty(o, alias, size, suf){
	var flg = 0; var name = alias ? alias : o.name; var str = is_object(o) ? o.value : o;
	if(!size)
		size = 0;
	if(size > 0 && str.length < size){
		flg++; err_obs += '\n- ' + name + ' deve ser preenchido com ' + size + ' dígitos.';
	}else	if(size == 0 && str.length < (size + 1)){
		flg++; addErrMsg(name, suf);
	}
	addFlag(o, flg == 0);
return(flg != 0);
}
function isNumber(o, alias){
	var f = alias ? alias : o.name, n = o.value.replace(/[^0-9]/, '') == o.value;
	if(isEmpty(o, f)) return(addFlag(o, false));
	if(!n) err_msg += '\n- ' + f + ' deve conter apenas números.';
return(addFlag(o, n));
}
function isCurrency(o, alias){
	var f = alias ? alias : o.name, x = o.value.replace(/[^0-9,]/g, '').replace(/,/, '.');
	if(isEmpty(o, f)) return(addFlag(o, false));
	if(!is_numeric(x)) addErrMsg(f, 'inválido');
return(addFlag(o, is_numeric(x)));
}
function isIP(o, alias){
	if(o.value.search(new RegExp(/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(-(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))?$/)) < 0){
		addErrMsg((alias ? alias : o.name) + ' ' + o.value, 'inválido');
	return(addFlag(o, false));
	}
return(addFlag(o, true));
}
function isString(o, alias, addChr){
	var flg = 0;
	var str = spc = arw = tst = '';
	var name = alias ? alias : o.name;
	var chrValid = letter_list + (is_def(addChr) ? addChr : '');
	if(isEmpty(o, alias)) return(false);
	for(var i = 0; i < o.value.length; i++){
		tst = o.value.substring(i, i + 1);
		if(chrValid.indexOf(tst) < 0){ flg++; str += ' ' + tst; spc += tst; arw += '^'; }else arw += '-';
	}
	if(flg > 0){
		err_obs += '\n- ' + name + ' inválido\n' + o.value + '\n-' + arw;
		err_obs += '\nEste campo deve ser preenchido somente com letras.';
		err_obs += '\nExiste ' + flg + ' caractere(s) inválido(s): ' + str;
	}
return(addFlag(o, flg == 0));
}
function isEq(x, y){ var ok = x.value == y.value; addFlag(x, ok); addFlag(y, ok); return(ok); }
function isEqIco(x, y, a, b){ var ok = x.value == y.value; addFlagIco(x, ok, a); addFlagIco(y, ok, b); return(ok); }
function addErrMsg(msg, suf){ err_msg += '\n- ' + msg + ' é ' + (suf ? suf : 'obrigatório') + '.'; }
function inRange(n, x, y){ return(n >= x && n <= y); }
function cmpDate(dtBof, dtEof, aliasBof, aliasEof){
	var dB = formatDate(dtBof), dE = formatDate(dtEof);
	if(dB > dE){ err_msg += '\n- Data ' + aliasBof + ' deve ser menor ou igual a Data ' + aliasEof; return false; }
return true;
}
function isDay(d){ return inRange(d, 1, 31); }
function isMonth(m){ return inRange(m, 1, 12); }
function isYear(y){ var s = '' + y; return s.length == 2 || s.length == 4; }
function daysIn(m, y){ return((m == 2) ? (isLeapYear(y) ? 29 : 28) : (m % 2 == 0 ? m < 8 ? 30 : 31 : m > 7 ? 30 : 31)); }
function isLeapYear(y){ return(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)); }
function isValidDate(y, m, d){
	var intY = is_object(y) ? y.value : y; var intM = is_object(m) ? m.value : m; var intD = is_object(d) ? d.value : d;
return((isYear(intY) && isMonth(intM) && isDay(intD)) && (intD <= daysIn(intM, intY)));
}
function isDate(o, alias){
	var dt = o.value.split('/');
	if(!isValidDate(dt[2], dt[1], dt[0])){ addErrMsg(alias ? alias : o.name, 'inválida'); return(addFlag(o, false)); }
return(addFlag(o, true));
}
function isDateStr(o, alias){
	var str = is_object(o) ? o.value : o; var dt = str.split('/'); alias = (alias) ? ' ' + alias : '';
	if(!isValidDate(dt[2], dt[1], dt[0])){ addErrMsg('Data' + alias, 'inválida'); return(addFlag(o, false)); }
return(addFlag(o, true));
}
function isTime(o, alias){
	if(o.value.search(new RegExp(/^([01][0-9]|2[0-3]):?[0-5][0-9]$/)) < 0){
		addErrMsg(alias ? alias : o.name, 'inválida');
	return(addFlag(o, false));
	}
return(addFlag(o, true));
}
function isChecked(o, alias){
	if(o.type == 'checkbox'){ if(o.checked == true) return(true); }else{ for(var i = 0; i < o.length; i++) if(o[i].checked == true) return(true); }
	addErrMsg((alias) ? alias : o.name);
return(false);
}
function isSelected(o, alias){
	if(o && o.options){ for(var i = (o.options[0].value == 0) ? 1 : 0; i < o.length; i++) if(o.options[i].selected == true) return(true); }
	addErrMsg((alias) ? alias : o.name);
return(false);
}
function getStrPrefix(e, glue){ return(e.name.slice(0, e.name.lastIndexOf((glue) ? glue : '_')).toLowerCase()); }
function getStrSuffix(e, glue){ return(e.name.slice(e.name.lastIndexOf((glue) ? glue : '_') + 1)); }
function getDateSuffix(e, glue){ var d = (is_object(e)) ? getStrSuffix(e, glue) : e; return(d.slice(6) + '/' + d.slice(4, 6) + '/' + d.slice(0, 4)); }
function specialEntry(e){ return(e.type.search(new RegExp('^(hidden|button|submit|reset|image|select.+)$','gi')) == 0); }
function checkEntry(e, i, name){
	if(specialEntry(e[i])) return(true);
	if(e[i].type != 'radio' && e[i].type != 'checkbox') return(e[i].value != '');
	while(i < e.length && e[i].name == name) if(e[i++].checked) return(true);
return(false);
}
function checkSuffixEntry(e, i, sufix){
	function glue(ary){ var str = ary.join(', ').replace(/^(, )+|(, )+$/g, ''); return(str.replace(/(, )+/g, ', ')); }
	var j = 0, fld = new Array(new Array(prefix.length), new Array(prefix.length));
	while(i < e.length && getStrSuffix(e[i]) == sufix){
		if(!specialEntry(e[i])){
			if(prefix.length == 4){
				if(e[i].name == 'i_' + sufix){
					if((e[i].value == '' && e[i + 1].value != '') || (e[i].value != '' && e[i + 1].value == ''))
						fld[(e[i].value == '' ? 0 : 1)][j] = prefix[j];
				i++;
				j += 2;
				}else fld[(e[i].value == '' ? 0 : 1)][j] = prefix[j++];
			}else	if(e[i].name != 'i_' + sufix){ fld[(e[i].value == '' ? 0 : 1)][j] = prefix[j++];
			}else fld[(e[i].value == '' ? 0 : 1)][j] = prefix[j++];
		}
		i++;
	}
	if(fld[0].join('').length == 0 || fld[1].join('').length == 0) return(true);
	err_msg += '\n -' + glue(fld[1]) + ' de ' + getDateSuffix(sufix) + ', pois não foi informado: ' + glue(fld[0]);
return(false);
}
function isMail(o, alias, txt){
	var mail = txt && txt != '' ? txt : o.value;
	if(mail.length < 6 || mail.search(new RegExp('^([a-z0-9_]|\-|\\\.)+@(([a-z0-9_]|\-)+\\\.)+[a-z]{2,4}$','gi')) < 0){
		addErrMsg((alias) ? alias : o.name, 'inválido');
	return(addFlag(o, false));
	}
return(addFlag(o, true));
}
function isDomain(o, alias){
	var url = o.value;
	if(url.search(new RegExp('^(.{2,6}://)?(w{3}[0-9]?\\\.)?(([a-z0-9_]|\-)+\\\.)+[a-z]{2,4}(/)?$','gi')) < 0){
		err_msg += '\n- ' + ((alias) ? alias : o.name) + ' é inválido.';
	return(addFlag(o, false));
	}
return(addFlag(o, true));
}
function isCPF(o, suffix){
	var i, j, sum = 0, mod, dig1, dig2, doc = o.value, msg = '\n- O CPF' + (suffix ? suffix : '') + ': ' + doc + ' é inválido';
	var cpf = formatStr(doc, 'DOC');
	if(cpf.length != 12 || cpf == '000000000-00' || cpf == '999999999-99' || cpf.search(new RegExp('[0-9]{9}\-[0-9]{2}', 'g')) < 0){
		err_msg += msg;
	return false;
	}
	for(i = 0, j = 10; i < 9; i++, j--) sum += (parseInt(cpf.substring(i, i + 1), 10) * j);
	mod = sum % 11; dig1 = (mod < 2) ? 0 : 11 - mod; sum = 0;
	for(i = 0, j = 11; i < 11; i++, j--){ if(i == 9) i++; sum += (parseInt(cpf.substring(i, i + 1), 10) * j); }
	mod = sum % 11; dig2 = (mod < 2) ? 0 : 11 - mod;
	if(!(cpf.substring(10,11) == dig1 && cpf.substring(11,12) == dig2)){ err_msg += msg + doc + ' é inválido'; return false; }
return true;
}
function isCNPJ(o, suffix){
	var i, j, sum1 = sum2 = 0, mod1, mod2 = 2, dig1, dig2, doc = o.value, msg = '\n- O CNPJ' + (suffix ? suffix : '') + ': ' + doc + ' é inválido';
	var cnpj = formatStr(doc, 'DOC');
	if(cnpj.charAt(0) == '0' && cnpj.length == 16) cnpj = cnpj.substring(1);
	if(cnpj.length != 15 || cnpj.search(new RegExp('[0-9]{12}\-[0-9]{2}', 'g')) < 0 ||
		(cnpj == '000000000000-00') || (cnpj == '111111111111-11') ||
		(cnpj == '222222222222-22') || (cnpj == '333333333333-33') ||
		(cnpj == '444444444444-44') || (cnpj == '555555555555-55') ||
		(cnpj == '666666666666-66') || (cnpj == '777777777777-77') ||
		(cnpj == '888888888888-88') || (cnpj == '999999999999-99')){ err_msg += msg; return false; }
	for(i = 11; i >= 0; i--){ j = eval(cnpj.charAt(i)); sum1+= j * (mod1 = mod2); sum2+= j * (mod2 = mod2 < 9 ? mod2 + 1 : 2); }
	sum1%= 11; dig1 = (sum1 < 2) ? 0 : 11 - sum1; sum2 = (sum2 + (2 * dig1)) % 11; dig2 = (sum2 < 2) ? 0 : 11 - sum2;
	if(!(dig1 == cnpj.charAt(13) && dig2 == cnpj.charAt(14))){ err_msg += msg; return false; }
return true;
}
function isPIS(o){
	var pis = o.value.replace(/[^0-9]/g, ''), i, s = 0, m = 1, d;
	while(pis.length != 11) pis = '0' + pis;
	if(parseInt(pis, 10)){
		for(i = 2; i < 12; i++) s += (m = m < 9 ? m + 1 : 2) * parseInt(pis.substring(12 - i - 1, 12 - i), 10);
		d = 11 - (s % 11);
		if((d > 9 ? 0 : d) == parseInt(pis.substring(10, 11), 10)) return true;
	}
	err_msg += '\n- O PIS: ' + o.value + ' é inválido';
return false;
}
