String.prototype.formatNumber = function () {
	var tmpValue = this;
	var virgula = tmpValue.indexOf('.');
	var afterComma = tmpValue.substr(virgula+1, 2);
	var beforeComma = tmpValue.substr(0, virgula);
	var pieces = Array();
	for(var i=beforeComma.length; i>0; i-=3) { pieces.push(beforeComma.substring(i-3, i)); }
	return(pieces.reverse().join(".") + "," + afterComma)
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

function is_function( key ){
		return (key == 9) || (key == 8) || (key == 13) || ( key == 35 ) || ( key == 36 ) || ( key == 37 ) || ( key == 39 );
}


function findNextFormElement( obj ) {
	var form = obj.form;
	for( i = 0; i < form.elements.length; i++)
		if ( obj.id == form.elements[ i ].id )	break;

	for( y = i+1; y < form.elements.length; y++)
	  	if ( form.elements[ y ].id ) {
				return form.elements[ y ];
				break;
		}
}

function currency_keypress(e, obj, decimals ){
		if ( document.all ) {
				var tr = document.selection.createRange();
				var text = obj.value;
				if ( tr.text == text ) {
						obj.value = "";
				}
		}

		if ( !decimals ){
			decimals = 2;
		}



		if (obj.value.length < 18) {
				var key;
				if(window.event || !e.which){ // IE
						key = e.keyCode; // for IE, same as window.event.keyCode
				} else if(e) {// netscape
						key = e.which;
				} 	else {
						return true;
				}

				if( (key > 47) && (key < 58)  ){
						NumDig = obj.value;
   						TamDig = NumDig.length;
   						Contador = 0;
						if (TamDig > 1) {
								numer = "";
								for (i = TamDig; (i >= 0); i--){
          								if ((parseInt(NumDig.substr(i,1))>=0) && (parseInt(NumDig.substr(i, 1))<=9)) {
												Contador++;
             									if ((Contador == decimals) && ((TamDig -i) < decimals+1)) {
														numer = ","+numer;
														Contador = 0;
												} else if (Contador == 3) {
														if ( numer.indexOf(",") > 0 ){
															numer = "."+numer;
															Contador = 0;
														}
												}
             									numer = NumDig.substr(i, 1)+numer;
            							} //fecha if #3
           						} //fecha for
      							obj.value = numer;
      					}; //fecha if #2
						return true;
				}  else if ( is_function( key ) || ( key == 46 ) ) {
					return true;
				} else
					return false;
		}
}

function integer_keypress(e, ConteudoCampo){
				var key;
				if(window.event || !e.which){ // IE
						key = e.keyCode; // for IE, same as window.event.keyCode
				} else if(e) {// netscape
						key = e.which;
				} 	else {
						return true;
				}
				return (  (key > 47) && (key < 58)  ) || ( is_function( key ) );
}

function remove_left_zero(dado, lookup){
		NumDig = dado.value;
		numer = dado.value;

		pos = NumDig.indexOf(",");

		if( ( pos == -1 ) || ( pos > 1 ) ) {
				for (var i = 0; i < NumDig.length - 1; i = i + 1) {
						if (NumDig.charAt(i) == "0" ) {
								numer = NumDig.substring(i+1, NumDig.length);
						}  else {
			 					break;
	  					}
	  			}
   				dado.value = numer;
		}
		if ( lookup == 1 ) onlookup( dado );
}


function mask_format_data(e, obj){
	var keyPressed;
	var valida_key = true;
	if(window.event || !e.which){ // IE
			keyPressed = e.keyCode; // for IE, same as window.event.keyCode
	} else if(e) {// netscape
			keyPressed = e.which;
	} 	else {
			return true;
	}

	//keyPressed = event.keyCode;
	valida_key = (  ((keyPressed > 47) && (keyPressed < 58)) || (is_function( keyPressed ))  );
	if((obj.value.length == 2 || obj.value.length == 5) && (keyPressed != 8 || keyPressed !=37 || keyPressed != 39)) obj.value += "/";
	if(   ( obj.value.length == 9 ) && ((keyPressed > 47) && (keyPressed < 58))   ) {
		obj.value += String.fromCharCode( keyPressed );
		valida_key = false;
		Focus( findNextFormElement(obj) );
	}
	return valida_key;
}

function mask_format_cep(e, obj){
		var key;
		if(window.event || !e.which){ // IE
				key = e.keyCode; // for IE, same as window.event.keyCode
		} else if(e) {// netscape
				key = e.which;
		} 	else {
				return true;
		}

		valida_traco = ( (String.fromCharCode( key ) == "-") && (obj.value.length == 5) );
		valida_key = (  ((key > 47) && (key < 58)) || (is_function( key )) || (valida_traco) );
		if (obj.value.length == 5){
				if ( "-" != String.fromCharCode( key ) ) {
						obj.value += "-";
				}
		}

		if(   ( obj.value.length == 8 ) && ((key > 47) && (key < 58))   ) {
			obj.value += String.fromCharCode( key );
			valida_key = false;
			Focus( findNextFormElement(obj) );
		}

		return   valida_key;
}

function mask_format_cpf(e, obj){
		var key;
		if(window.event || !e.which){ // IE
				key = e.keyCode; // for IE, same as window.event.keyCode
		} else if(e) {// netscape
				key = e.which;
		} 	else {
				return true;
		}

		valida_traco = ( (String.fromCharCode( key ) == "-") && (obj.value.length == 11) );
		valida_ponto = ( (String.fromCharCode( key ) == ".") && ((obj.value.length == 3) || (obj.value.length == 7) ));
		valida_key = (  ((key > 47) && (key < 58)) || (is_function( key )) || (valida_traco) || (valida_ponto) );

		//pontos
		if(  (obj.value.length == 3) ||(obj.value.length == 7)  ) {
				if ( "." != String.fromCharCode( key ) ) {
						obj.value += ".";
				}
		}
		//traco
		if(  (obj.value.length == 11)  ) {
				if ( "-" != String.fromCharCode( key ) ) {
						obj.value += "-";
				}
		}

		if(   ( obj.value.length == 13 ) && ((key > 47) && (key < 58))   ) {
			obj.value += String.fromCharCode( key );
			valida_key = false;
			Focus( findNextFormElement(obj) );
		}
		return   valida_key;
}

function parseDec(val,places,sep) {

	// This function takes two arguments:
	//   (string || number)  val
	//            (integer)  places
	//             (string)  sep
	// val is the numeric string or number to parse
	// places represents the number of decimal
	// places to return at the end of the parse.
	// sep is an optional string to be used to separate
	// the whole units from the decimal units (default: '.')

	val = '' + val;
		// Implicitly cast val to (string)

	if (!sep) {
		sep = '.';
		// If separator isn't specified, then use a decimal point '.'
	}

	if (!places) { places = 0; }
	places = parseInt(places);
		// Make sure places is an integer

	if (!parseInt(val)) {
		// If val is null, zero, NaN, or not specified, then
		// assume val to be zero.  Add 'places' number of zeros after
		// the separator 'sep', and then return the value.  We're done here.
		val = '0';
		if (places > 0) {
			val += sep;
			while (val.substring((val.indexOf(sep))).length <= places) {
				val += '0';
			}
		}
		return val;
	}

	if ((val.indexOf('.') > -1) && (sep != '.')) {
		val = val.substring(0,val.indexOf('.')) + sep + val.substring(val.indexOf('.')+1);
			// If we're using a separator other than '.' then convert now.
	}

	if (val.indexOf(sep) > -1) {
		// If our val has a separator, then cut our value
		// into pre and post 'decimal' based upon the separator.
		pre = val.substring(0,val.indexOf(sep));
		post = val.substring(val.indexOf(sep)+1);
	} else {
		// Otherwise pre gets everything and post gets nothing.
		pre = val;
		post = '';
	}

	if (places > 0) {
		// If we're dealing with a decimal then...

		post = post.substring(0,(places+1));
			// We care most about the digit after 'places'

		if (post.length > places) {
			// If we have trailing decimal places then...

			//alert (parseInt(post.substring(post.length - 1)));

			if ( parseInt(post.substring(post.length - 1)) > 4 ) {
				post = '' + Math.round(parseInt(post) / 10);
				//post = '' + post.substring(0,post.length - 2) + (1/Math.pow(10,places));
				//post = ('' + post.substring(0,post.length - 2)) + (parseInt(post.substring(post.length - 1)) + 1);
			} else {
				post = '' + Math.round(parseInt(post));
			}
		}

		if (post.length > places) {
			post = '' + Math.round(parseInt(post.substring(0,places)));
		} else if (post.length < places) {
			while (post.length < places) {
				post += '0';
			}
		}

	} else {

		if (parseInt((post.substring(0,1))) > 4) {
			pre = '' + (parseInt(pre) + 1);
		} else {
			pre = '' + (parseInt(pre));
		}
		post = '';
	}

	sep = (post.length > 0) ? sep : '';
		// Should we use a separator?

	val = pre + sep + post;
		// Rebuild val

	return val;
}
