
function formata(strField, sMask, evtKeyPress){
	//chamada:  onKeyPress="return formata(this, '99.999.999/9999-99', event);"
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	if(document.all) {
		nTecla = evtKeyPress.keyCode;
	}else if(document.layers){
		nTecla = evtKeyPress.which;
	}
	sValue = strField.value;
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;
	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;
	while (i <= mskLen){
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++;
		}else{
			sCod += sValue.charAt(nCount);
			nCount++;
		}
		i++;
	}
	strField.value = sCod;
	if (nTecla != 8){
		if (sMask.charAt(i-1) == "9"){
			return ((nTecla > 47) && (nTecla < 58));
		}else{
			return true;
		}
	}else{
		return true;
	}
}



function validaEmail(emailad){
    var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
    var check=/@[\w\-]+\./;
    var checkend=/\.[a-zA-Z]{2,3}$/;
    if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1)){
        return false;
    }
    else {
        return true;
    }
}

function isDate(dateStr) {
	if(dateStr=='//'){
		return true;
	}
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
	}
	day= matchArray[1]; // p@rse date into variables
	month = matchArray[3];
	year = matchArray[5];

	if(year < 1900){
		return false;
	}
	
	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn`t have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}

function valIntData(teclapres,campo){
//vai formatando o campo como data ##/##/####
// chamada da função: onKeyPress="return valInt(event,this);"
	var tecla = teclapres.keyCode;
	if (tecla > 31 && (tecla < 48 || tecla > 57)){ 
		return false;
	}
	if((campo.value.length == 2)||(campo.value.length == 5)){
		campo.value = campo.value+'/'
	}
	return true;
}

function valInt(teclapres,campo){
// chamada da função: onKeyPress="return valInt(event,this);"
	var tecla = teclapres.keyCode;
	//alert(tecla)
	if (tecla > 31 && (tecla < 48 || tecla > 57)){ 
		return false;
	}
	return true;
}

function abreFecha(param){
	if(param.style.display=='none'){
		param.style.display='block'
	}else{
		param.style.display='none'
	}
}

function formataVlr(campo){
	texto = campo.value.replace(',','')
	tam = texto.length
	cent = texto.substring(tam-2)
	if(tam>2){
		texto = texto.substring(0,tam-2)+','+cent
	}
	campo.value=texto;
}

function formatar_mascara(src, mascara) {
	var campo = src.value.length;
	var saida = mascara.substring(0,1);
	var texto = mascara.substring(campo);
	if(texto.substring(0,1) != saida) {
		src.value += texto.substring(0,1);
	}
}
