//funçoes de máscara
function cleanMask(val) {
	var strCheck = "'[](){}<>=+-*/_|\~`!?@#$%^&:;,.";
	var aux="";
	var i;

	for(i=0; i<val.length; i++) {
		if(strCheck.indexOf(val.charAt(i))==-1) {
			aux+=val.charAt(i);
		}
	}
	return aux;
}

function maskCurrency(val, milSep, decSep) {
	var aux="";
	var aux2="";

	var i,j;

	len = val.length;
	if (len == 0) {
		aux = '';
	} else if (len == 1) {
		aux = '0'+ decSep + '0' + val;
	} else if (len == 2) {
		aux = '0'+ decSep + val;
	} else if (len > 2) {
		aux2 = '';

		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += val.charAt(i);
			j++;
		}
		aux = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--) {
			aux += aux2.charAt(i);
		}
		aux += decSep + val.substr(len - 2, len);
	}
	return aux;
}
function mask(_mask, val) {
	var i, mki;
	var aux="";

	for(i=mki=0; i<val.length; i++, mki++) {
		if(_mask.charAt(mki)=='' || _mask.charAt(mki)=='#' || _mask.charAt(i)==val.charAt(i)) {
			aux+=val.charAt(i);
		} else {
			aux+=_mask.charAt(mki)+val.charAt(i);
			mki++;
		}
	}
	return aux;
}

function maskEvent(otherField, _mask, evento) {
	var otherKey ='';
	var aux='';
	var len=0;
	var i=0;
	var strCheck = '0123456789';
	
	var rcode = evento.keyCode ? evento.keyCode : evento.which ? evento.which : evento.charCode;
	
	if(rcode == 13 || rcode == 0 || rcode == 8 || otherField.value.length == _mask.length  )
	{
		//Enter
		otherKey=String.fromCharCode(rcode);

		if(rcode!=0 && rcode != 8 || rcode==13)
		{
			/*alert(rcode+"aqui1");*/
			return false;
		}
		return true;
	}

	//Get otherKey value from otherKey code
	otherKey=String.fromCharCode(rcode);
	
	if(strCheck.indexOf(otherKey)==-1) {
		
		//Not a valid otherKey
		return false;
	}

	aux=otherField.value+otherKey;
	//window.alert(aux);
	aux=mask(_mask,aux);
	//window.alert(aux);
	otherField.value=aux;
	return false;
}

function handleEnter(otherField, evento, f, m)
{
	var keyCode = evento.keyCode ? evento.keyCode : evento.which ? evento.which : evento.charCode;
	
	if (keyCode == 13)
	{
		var i;
		for (i = 0; i < otherField.form.elements.length; i++)
			if (otherField == otherField.form.elements[i])
				break;
		
		i = (i + 1) % otherField.form.elements.length;
		
		/* 
			Alterado por Andrey, pois quando chegava em um campo do tipo disabled e dava pau
			porque nao conseguia dar foco
		*/
		passou = 0;
		
		for(i2 = i; i2 <= otherField.form.elements.length; i++) {
			if(otherField.form.elements[i].disabled == true) {
				i++;
				passou = 1;
			}
			else {
				break;
			}
		}
		
		if(passou == 1) {
			i--;
		}
		/* Fim alterações Andrey */
		
		otherField.form.elements[i].focus();
		return false;
	}
	else if(f)
		{
			return maskEvent(otherField, m, evento)
		}
		else
		{
			return true;
		}
}      
function currencyFormat(fld, milSep, decSep,casas, change, e) {
	
	var sep = 0;
	var otherKey = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	if(change==0)
	{
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13 || whichCode == 0 || whichCode == 8 ) {		//Enter
		if(whichCode==8)
			{
			fld.value=fld.value.substr(0,fld.value.length-1);
			currencyFormat(fld, milSep, decSep,casas,1, e)
				return false;
			}
		else
			{
			return true;
			}
	}
	
	otherKey = String.fromCharCode(whichCode);  // Get otherKey value from otherKey code
	if (strCheck.indexOf(otherKey) == -1 && change == 0) {
		return false;  // Not a valid otherKey
	}
	}
	len = fld.value.length;
	for(i = 0; i < len; i++) {
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) {
			break;
		}
	}
	aux = '';
	for(; i < len; i++) {
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) {
			aux += fld.value.charAt(i);
		}
	}
	aux += otherKey;

	len = aux.length;
	if (len < casas) {
		retorno = '0'+ decSep;
		for(i=0;i<casas-len;i++)
			{
			retorno+='0';
			}
		retorno+=aux;
		fld.value=retorno;
	} else if (len == casas) {
		fld.value = '0'+ decSep + aux;
	} else if (len > casas) {
		aux2 = '';

		for (j = 0, i = len - (casas+1); i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		retorno = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--) {
			retorno += aux2.charAt(i);
		}
		retorno += decSep + aux.substr(len - casas, len);
		if(retorno.length > fld.getAttribute("maxlength"))
			{
			return false;
			}
		fld.value=retorno;
	}
return false;
}
//fim funções de máscara
