/*
exemple: addformactionrule("meusaldo", "cpf", "cpf", null, "if(this.value=='Digite seu CPF')this.value='';", "if(this.value=='')this.value='Digite seu CPF';");
*/
isIE = navigator.appName.indexOf("Explorer") >= 0;
formatrules = new Array();
jumprules = new Array();
currentform = null;
currentfield = null;

function addformactionrule() {
	NewRule = new Array(arguments[0], arguments[1], arguments[2], (arguments[3]) ? arguments[3] : null, (arguments[4]) ? arguments[4] : null, (arguments[5]) ? arguments[5] : null, (arguments[6]) ? arguments[6] : null);
	if (arguments[2] == "jump") {
		jumprules[jumprules.length] = NewRule;
	} else
		formatrules[formatrules.length] = NewRule;
	return(NewRule);
}

function myonkeyup(e) {
	if (currentform != null && currentfield != null){
		key = (isIE) ? event.keyCode : e.which;
		if (key != 16 && key != 9 && key != 8 && key != 37 && key != 39 && key != 40 && key != 46)
			for (i = 0; i < formatrules.length; i++) {
				if (formatrules[i][0] == currentform && formatrules[i][1] == currentfield)
					document[currentform].elements[currentfield].value = eval("format" + formatrules[i][2] + "('" + document[currentform].elements[currentfield].value + "');");
			}
		if (key != 9 && key != 16 && key != 109 && key != 188 && key != 110 && key != 111 && key != 223 && key != 108 && key != 37 && key != 39) {
			bfind = false;
			for (i = 0; i < jumprules.length; i++) {
				if (jumprules[i][0] == currentform && jumprules[i][1] == currentfield)
					if (document[currentform].elements[currentfield].value.length == jumprules[i][3])
						for(i = 0; i < document[currentform].elements.length; i++) {
							if (bfind == true && document[currentform].elements[i].type != 'hidden' && document[currentform].elements[i].disabled != true) {
								document[currentform].elements[i].focus();
								i = document[currentform].elements.length;
							}
							if (document[currentform].elements[i])
								if (document[currentform].elements[i].name == currentfield) bfind = true;
						}
			}
		}
	}
}

function removeinvalidchar(value, validchars) {
	var valuetmp = "";
	for (j = 0; j < value.length; j++) {
		thischar=value.substr(j,1);
		if (validchars.indexOf(thischar)!=-1) {
			valuetmp = valuetmp + thischar;
		}
	}
	return(valuetmp);
}

function formatcpf(value) {
	value = removeinvalidchar(value, "0123456789");
	if (value.length > 2 && value.length < 7)
		return(value.substr(0, 3) + '.' + value.substr(3, value.length));
	if (value.length >= 6 && value.length <10)
		return(value.substr(0,3) + '.' + value.substr(3,3) + '.' + value.substr(6, value.length-6));
	if (value.length >= 9 && value.length < 12)
		return(value.substr(0,3) + '.' + value.substr(3,3) + '.' + value.substr(6,3) + '-' + value.substr(9,value.length-9));
	else
		return(value);
}

function formatcnpj(value) {
	value = removeinvalidchar(value, "0123456789");
	if (value.length > 2 && value.length < 6)
		return(value.substr(0, 2) + '.' + value.substr(2, value.length));
	if (value.length >= 6 && value.length < 9)
		return(value.substr(0,2) + '.' + value.substr(2,3) + '.' + value.substr(5,value.length-5));
	if (value.length >= 9 && value.length < 13)
		return(value.substr(0,2) + '.' + value.substr(2,3) + '.' + value.substr(5,3) + '/' + value.substr(8,value.length-8));
	if (value.length >= 13 && value.length < 15)
		return(value.substr(0,2) + '.' + value.substr(2,3) + '.' + value.substr(5,3) + '/' + value.substr(8,4)+ '-' + value.substr(12,value.length-12));
	else
		return(value);
}

function formatid(value) {
	value = removeinvalidchar(value, "0123456789Xx");
	if (value.length > 1 && value.length < 5)
		return(value.substr(0, value.length-1) + '-' + value.substr(value.length-1, 1));
	if (value.length >= 5 && value.length < 8)
		return(value.substr(0,value.length-4) + '.' + value.substr(value.length-4,3) + '-' + value.substr(value.length-1, 1));
	if (value.length >= 8 && value.length < 11)
		return(value.substr(0,value.length-7) + '.' + value.substr(value.length-7,3) + '.' + value.substr(value.length-4,3) + '-' + value.substr(value.length-1, 1));
	if (value.length >= 11)
		return(value.substr(0, value.length-10) + '.' + value.substr(value.length-10,3) + '.' + value.substr(value.length-7,3) + '.' + value.substr(value.length-4,3) + '-' + value.substr(value.length-1, 1));
	else
		return(value);
}

function formatzipcode(value) {
	value = removeinvalidchar(value, "0123456789");
	if (value.length > 4)
		return(value.substr(0, 5) + '-' + value.substr(5, value.length));
	else
		return(value);
}

function formatfone(value) {
	value = removeinvalidchar(value, "0123456789");
	if (value.length > 8)
		value = value.substr(0, value.length - (value.length - 8));
	if (value.length > 4)
		return(value.substr(0,value.length-4) + ' ' + value.substr(value.length-4, 4));
	else
		return(value);
}

function formatcurrency(value) {
	value = removeinvalidchar(value, "0123456789");
	if (value.length >= 3 && value.length < 6)
		return(value.substr(0, value.length-2) + ',' + value.substr(value.length-2, value.length));
	if (value.length >= 6 && value.length < 9)
		return(value.substr(0,value.length-5) + '.' + value.substr(value.length-5, 3) + ',' + value.substr(value.length-2, value.length));
	if (value.length >= 9)
		return(value.substr(0,value.length-8) + '.' + value.substr(value.length-8, 3) + '.' + value.substr(value.length-5, 3) + ',' + value.substr(value.length-2, value.length));
	else
		return(value);
}

function formatnumber(value) {
	value = removeinvalidchar(value, "0123456789");
	if (value.length >= 4 && value.length < 8)
		return(value.substr(0, value.length-3) + '.' + value.substr(value.length-3, value.length));
	return(value);
}

function formatcpf(value) {
	value = removeinvalidchar(value, "0123456789");
	if (value.length > 2 && value.length < 6)
		return(value.substr(0, 3) + '.' + value.substr(3, value.length));
	if (value.length >= 6 && value.length < 9)
		return(value.substr(0,3) + '.' + value.substr(3,3) + '.' + value.substr(6, value.length-6));
	if (value.length >= 9)
		return(value.substr(0,3) + '.' + value.substr(3,3) + '.' + value.substr(6,3) + '-' + value.substr(9,value.length-9));
	else
		return(value);
}

function formatcnpj(value) {
	value = removeinvalidchar(value, "0123456789");
	if (value.length > 1 && value.length < 5)
		return(value.substr(0, 2) + '.' + value.substr(2, value.length));
	if (value.length >= 5 && value.length < 8)
		return(value.substr(0,2) + '.' + value.substr(2,3) + '.' + value.substr(5,value.length-5));
	if (value.length >= 8 && value.length < 12)
		return(value.substr(0,2) + '.' + value.substr(2,3) + '.' + value.substr(5,3) + '/' + value.substr(8,value.length-8));
	if (value.length >= 12)
		return(value.substr(0,2) + '.' + value.substr(2,3) + '.' + value.substr(5,3) + '/' + value.substr(8,4)+ '-' + value.substr(12,value.length-12));
	else
		return(value);
}

function makeEvents() {
	if (document[arguments[0]].elements[arguments[1]].onkeyup == null) {
		if (arguments[2] != null)
			eval("document." + arguments[0] + ".elements['" + arguments[1] + "'].onkeyup = myonkeyup;" + arguments[2]);
		else
			eval("document." + arguments[0] + ".elements['" + arguments[1] + "'].onkeyup = myonkeyup;");
		if (arguments[3] != null)
			eval("document." + arguments[0] + ".elements['" + arguments[1] + "'].onfocus = function(e) {currentform = '" + arguments[0] + "'; currentfield = '" + arguments[1] + "';" + arguments[3] + "};");
		else
			eval("document." + arguments[0] + ".elements['" + arguments[1] + "'].onfocus = function(e) {currentform = '" + arguments[0] + "'; currentfield = '" + arguments[1] + "';};");
		if (arguments[4] != null)
			eval("document." + arguments[0] + ".elements['" + arguments[1] + "'].onblur = function(e) {currentform = null; currentfield = null;" + arguments[4] + "};");
		else
			eval("document." + arguments[0] + ".elements['" + arguments[1] + "'].onblur = function(e) {currentform = null; currentfield = null;};");
	}
}

onload = function() {
	for (i = 0; i < formatrules.length; i++)
		makeEvents(formatrules[i][0], formatrules[i][1]);
	for (i = 0; i < jumprules.length; i++)
		makeEvents(jumprules[i][0], jumprules[i][1]);
}
