function getSelectText(objSelect) {
	var currItem;
	for(var i = 0; i < objSelect.childNodes.length; i++) {
		currItem = objSelect.childNodes[i];
		if(currItem.nodeName == "OPTION" && currItem.selected) {
			return currItem.text;
		}
	}
}

// vDate
// USO: vDate(ano, mes, dia)
function vDate(y, m, d) {
	var vDate = false;
	var date = new Date();
	var betweenDays = 0;
	var betweenMonths = 0;
	var betweenYears = 0;
	
    with (D = new Date(y, --m, d)) {
        vDate = (getMonth() == m && getDate() == d) ? true : false;
    }
	if(vDate) {
		betweenDays = Math.floor(Math.abs(Math.round(date - D)) / (24 * 60 * 60 * 1000));
		betweenMonths = Math.floor(betweenDays / 30);
		betweenYears = Math.floor(betweenDays / 365);
	}
	return {
		valid : vDate,
		days : betweenDays,
		months : betweenMonths,
		years : betweenYears
	}
}

// numeros e barra
// USO: onkeypress="return setDate(event)"
function setdate(e) {
	var obj = getTarget(e);
	var key = '';
	var strCheck = "0123456789";
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if(!whichCode) whichCode = e.keyCode ? e.keyCode : e.charCode;
	var ins = false;

	if(whichCode == "\t" || whichCode == "\n" || whichCode == "\b" || whichCode == 8) return true; // Tab, Enter e backspace
	key = String.fromCharCode(whichCode);  // Pega o codigo da tecla precionada

	if(strCheck.indexOf(key) != -1){ // tecla valida
		if(!obj.format) {
			if((obj.value.length == 1) || (obj.value.length == 4)) {
				ins = true;
			}
		}
		else {
			if(obj.value.length == 1) {
				ins = true;
			}
		}
		if(ins) {
			obj.value += key + "/";
			return abort(e);
		}
	}
	else {
		return abort(e);
	}
}

function abort(e) {
	var w3DOM = window.addEventListener;
	if(w3DOM){
		e.preventDefault();
	}
	else{
		return false;
	}
}


// email
// USO: campo.value.isEmail
String.prototype.isEmail = function(){
	var er = /^[a-z0-9._-]+@([a-z0-9]+[a-z0-9_-]*)+(\.[a-z0-9]+)+$/;
	var value = this.toLowerCase();
	return er.test(value);
}

// CNPJ
// USO: campo.value.isCNPJ
String.prototype.isCNPJ = function() {
	if(this.replace(/\D/g, "").split(this.charAt(0)).join("")=="") return false;
	var d = this.replace(/\D/g, "").split("");
	var m1 = [d[0]*5,d[1]*4,d[2]*3,d[3]*2,d[4]*9,d[5]*8,d[6]*7,d[7]*6,d[8]*5,d[9]*4,d[10]*3,d[11]*2];
	var s1 = 0;
	for(i in m1){ s1 += m1[i]; }
	var d1 = (s1%11<2)?0:11-(s1%11);
	var m2 = [d[0]*6,d[1]*5,d[2]*4,d[3]*3,d[4]*2,d[5]*9,d[6]*8,d[7]*7,d[8]*6,d[9]*5,d[10]*4,d[11]*3,d1*2];
	var s2 = 0;
	for(i in m2){ s2 += m2[i]; }
	var d2 = (s2%11<2)?0:11-(s2%11);
	return (d1 == d[12] && d2 == d[13])?true:false;
} 

// CPF 
// USO: campo.value.isCPF
String.prototype.isCPF = function(){
	if(this.replace(/\D/g, "").split(this.charAt(0)).join("")=="") return false;
	var d = this.replace(/\D/g, "").split("");
	var m1 = [d[0]*10,d[1]*9,d[2]*8,d[3]*7,d[4]*6,d[5]*5,d[6]*4,d[7]*3,d[8]*2];
	var s1 = 0;
	for(i in m1){ s1 += m1[i]; }
	var d1 = (s1%11<2)?0:11-(s1%11);
	var m2 = [d[0]*11,d[1]*10,d[2]*9,d[3]*8,d[4]*7,d[5]*6,d[6]*5,d[7]*4,d[8]*3,d1*2];
	var s2 = 0;
	for(i in m2){ s2 += m2[i]; }
	var d2 = (s2%11<2)?0:11-(s2%11);
	return (d1 == d[9] && d2 == d[10])?true:false;
}

// numeros
// USO: onkeypress="return setNumeric(event)"
function setNumeric(e) {
	var key = '';
	var strCheck = "0123456789";
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if(!whichCode) whichCode = e.keyCode ? e.keyCode : e.charCode;
	if (whichCode == "\t" || whichCode == "\n" || whichCode == "\b" || whichCode == 8) return true; // Tab, Enter e backspace
	key = String.fromCharCode(whichCode);  // Pega o codigo da tecla precionada
	if (strCheck.indexOf(key) == -1){ // tecla não valida
		return abort(e);
	}
}

function setCurrency(e) {
	var strValue = "";
	var strLength = 0;
	var obj = getTarget(e);
	var key = '';
	var strCheck = "0123456789";
	var floatPoint = 2;
	var decimalSep = ',';
	var thousandsSep = '.';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if(!whichCode) whichCode = e.keyCode ? e.keyCode : e.charCode;
	if(trim(obj.value) == "" && obj.stringValue) {
		var objStr = document.getElementById(obj.stringValue);
		objStr.innerHTML = "";
	}
	if (whichCode == "\t" || whichCode == "\n" || whichCode == "\b" || whichCode == 8) return true; // Tab, Enter e backspace
	key = String.fromCharCode(whichCode);  // Pega o codigo da tecla precionada
	if (strCheck.indexOf(key) != -1){ // tecla valida
		var obj, strValue, strLength = (
					   strValue = (
							obj.value.replace(/^0+/g, "") + key
							).replace(/\D/g, "")
					   ).length, floatPoint;
		if(obj.maxLength + 1 && strLength >= obj.maxLength) {
			return abort(e);
		}
		strLength <= (floatPoint) && (strValue = new Array(floatPoint - strLength + 2).join("0") + strValue);
		for(var i = (strLength = (strValue = strValue.split("")).length) - floatPoint; (i -= 3) > 0; strValue[i - 1] += thousandsSep);
		floatPoint && floatPoint < strLength && (strValue[strLength - ++floatPoint] += decimalSep);
		obj.value = strValue.join("");
		if(obj.stringValue) {
			var objStr = document.getElementById(obj.stringValue);
			objStr.innerHTML = obj.value.strValue();
		}
	}
	return abort(e);
}

String.prototype.strValue = function(){
	var c = 1;
	var ex = [
		["zero", "um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove", "dez", "onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezessete", "dezoito", "dezenove"],
		["dez", "vinte", "trinta", "quarenta", "cinqüenta", "sessenta", "setenta", "oitenta", "noventa"],
		["cem", "cento", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos"],
		["mil", "milhão", "bilhão", "trilhão", "quadrilhão", "quintilhão", "sextilhão", "setilhão", "octilhão", "nonilhão", "decilhão", "undecilhão", "dodecilhão", "tredecilhão", "quatrodecilhão", "quindecilhão", "sedecilhão", "septendecilhão", "octencilhão", "nonencilhão"]
	];
	var a, n, v, i, n = this.replace(c ? /[^,\d]/g : /\D/g, "").split(","), e = " e ", $ = "real", d = "centavo";
	for(var f = n.length - 1, l, j = -1, r = [], s = [], t = ""; ++j <= f; s = []){
		j && (n[j] = (("." + n[j]) * 1).toFixed(2).slice(2));
		if(!(a = (v = n[j]).slice((l = v.length) % 3).match(/\d{3}/g), v = l % 3 ? [v.slice(0, l % 3)] : [], v = a ? v.concat(a) : v).length) continue;
		for(a = -1, l = v.length; ++a < l; t = ""){
			if(!(i = v[a] * 1)) continue;
			i % 100 < 20 && (t += ex[0][i % 100]) ||
			i % 100 + 1 && (t += ex[1][(i % 100 / 10 >> 0) - 1] + (i % 10 ? e + ex[0][i % 10] : ""));
			s.push((i < 100 ? t : !(i % 100) ? ex[2][i == 100 ? 0 : i / 100 >> 0] : (ex[2][i / 100 >> 0] + e + t)) +
			((t = l - a - 2) > -1 ? " " + (i > 1 && t > 0 ? ex[3][t].replace("ão", "ões") : ex[3][t]) : ""));
		}
		a = (s.length > 1 ? (a = s.pop(), s.join(" ") + " e " + a) : s.join("") || ((!j && (n[j + 1] * 1 > 0) || r.length) ? "" : ex[0][0]));
		a && r.push(a + (c ? (" " + (v.join("") * 1 > 1 ? j ? d + "s" : $.replace("l", "is") : j ? d : $)) : ""));
	}
	return r.join(e);
}

function hasDuplicate(A) {
	var i, j, n;
	n = A.length;
	for(i = 0; i < n; i++) {
		for( j = i + 1; j < n; j++) {
			if(A[i] == A[j]) return true;
		}
	}
	return false;
}