/*
 * is numeric?
 */
function IsNumeric(str) {
   if (str.match(/^[0-9]+$/)) {
	   return true;
   } else {
	   return false;
   }
}
/*
 * is alphanumeric?
 */
function IsAlphaNumeric(str) {
   if (str.match(/^[\sa-zA-Z0-9\u00C0-\u00ff]*$/)) {
	   return true;
   } else {
	   return false;
   }
}
/*
 * is separator?
 */
function IsSeparator(str) {
   if (str.match(/^[a-zA-Z0-9\/\-\u00C0-\u00ff]+$/)) {
	   return true;
   } else {
	   return false;
   }
}
/*
 * is array?
 */
function isArray(obj) {
    return(typeof(obj.length)=="undefined")?false:true;
}
/*
 * limit to max. characters
 */
function limitChar(fieldName, objText, maxnum) {
    if (objText.value.length > maxnum) {
        var answer = confirm("Atenção o texto " + fieldName + " ultrapassou o limite de " + maxnum + " characteres. Você autoriza corte automático dos últimos caracteres?");
        if (answer) {
            alert("O texto foi ajustado ao limite permitido, por favor confirme antes de reenviar");
            objText.value = objText.value.substring(0, maxnum);
            return false;
        } else{
            alert("Por favor, ajuste o texto " + fieldName + " para o limite de " + maxnum + " characteres");
            return false;
        }
    }
    return true;
}
/*
 * verify numeric digits
 */
function numDigits(obj, max) {
	// initialization
	var newNum = '';
	for (var i = 0; i < obj.value.length; i++) {
		if (IsNumeric(obj.value.charAt(i))) {
			newNum += obj.value.charAt(i);
		}
	}
	if (max != null) {
		if (newNum.length > max) {
			newNum = newNum.substring(0, max);
		}
	}
	obj.value = newNum;
	return newNum;
}
/*
 * verify alphanumeric digits
 */
function alphaNumDigits(obj, max) {
	// initialization
	var newAlphaNum = '';
	for (var i = 0; i < obj.value.length; i++) {
		if (IsAlphaNumeric(obj.value.charAt(i))) {
			newAlphaNum += obj.value.charAt(i);
		}
	}
	if (max != null) {
		if (newAlphaNum.length > max) {
			newAlphaNum = newNum.substring(0, max);
		}
	}
	obj.value = newAlphaNum;
	return newAlphaNum;
}
/*
 * verify separator digits
 */
function separatorDigits(obj, max) {
	// initialization
	var newSeparator = '';
	for (var i = 0; i < obj.value.length; i++) {
		if (IsSeparator(obj.value.charAt(i))) {
			newSeparator += obj.value.charAt(i);
		}
	}
	if (max != null) {
		if (newAlphaNum.length > max) {
			newSeparator = newNum.substring(0, max);
		}
	}
	obj.value = newSeparator;
	return newSeparator;
}
/*
 * verify cep code
 */
function verCode(what, prefix) {
	// initialization
	var objCode1 = document.getElementById(prefix+'code1');
	var objCode2 = document.getElementById(prefix+'code2');
	var tmp = '';
	if (what == 'code1') {
		tmp = numDigits(objCode1, 5);
		if (tmp.length == 5) {
			objCode2.focus();
			objCode2.select();
		}
	}
	if (what == 'code2') {
		numDigits(objCode2, 3);
	}
}

