	String.prototype.trim = function()
	{
		return this.replace(/^\s*/,"").replace(/\s*$/,"");
	}

	function doubleConverter(campo){
		var valor = campo.value;
		valor = valor.replace(/\./g,"");
		valor = valor.replace(",", ".");
		campo.value = valor;
	}

	function eString(str){
	    return (str != null && trimString(str) != "");
	}

	function trimString(str) {
	    return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
	}

	function validarData(campo)
	{

		if (isDate(campo.value)) {
		  return true;
		}
		else
		{
		  campo.focus();
		  return false;
		}
	}

	function validarNumero(campo)
	{
		var data = /^\d+$/;
		var valor = campo.value;

		if (data.test(valor)) {
		  return true;
		}
		else
		{
		  campo.focus();
		  return false;
		}
	}

	function validarMoeda(campo){
		var moeda = /^\d{1,3}(\.\d{3})*(\,\d{0,2}){0,1}$/;
		var valor = campo.value;

		if (moeda.test(valor)) {
			return true;
		}else {
			campo.focus();
			return false;
		}
	}


	function validarCampo(campo) {
		var valor = campo.value;

		if (eString(valor)) {
			campo.focus();
			return false;
		} else {
			campo.value = 0;
			return true;
		}
	}


	//Formata numero tipo moeda usando o evento onKeyDown
	function Formata(campo,tammax,teclapres,decimal) {
		var tecla = teclapres.keyCode;
		vr = Limpar(campo.value,"0123456789");
		vr = Number(vr).toString();
		tam = vr.length;
		dec=decimal

		if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

		if (tecla == 8 )
		{ tam = tam - 1 ; }

		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
		{

		if ( tam <= dec )
		{ campo.value = vr;
		  campo.value = "0,"
	          for (i = 0 ; i < (tam-dec) ; i++)
			campo.value += "0";
	          campo.value += vr;
		}

		if ( (tam > dec) && (tam <= 5) ){
			campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ;
		}
		if ( (tam >= 6) && (tam <= 8) ){
			campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
		}
		if ( (tam >= 9) && (tam <= 11) ){
			campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
		}
		if ( (tam >= 12) && (tam <= 14) ){
			campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
		}
		if ( (tam >= 15) && (tam <= 17) ){
			campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
		}

	}

	//Formata data usando o evento onKeyDown
	function FormataData(campo,teclapres) {
		var tecla = teclapres.keyCode;
		vr = Limpar(campo.value,"0123456789");


		if (vr.length > 7 & tecla != 8)
		{
		  vr = vr.substr(vr.length - 7, 7);
		}

		tam = vr.length;

		if (tecla != 8) { tam = vr.length + 1;}

		if (tecla == 8 )
		{ tam = tam - 1 ; }


		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
		{


			if ( (tam <= 4) ) {
				campo.value = "/ /" + vr;
			}

			if ( (tam >= 5) && (tam <= 6) ){
				campo.value = "/" + vr.substr( 0, tam-4 ) + "/" + vr.substr( tam - 4, 5 );
			}

			if ( (tam >= 7) && (tam <= 8) ){
				campo.value = vr.substr( 0, tam-6 ) + "/" + vr.substr( tam-6, 2 ) + "/" + vr.substr( tam-4, 5 );
			}
		}
	}

	function Limpar(valor, validos) {
		// Retira caracteres invalidos da string
		var result = "";
		var aux;
		for (var i=0; i < valor.length; i++) {
				aux = validos.indexOf(valor.substring(i, i+1));
			if (aux>=0) {
				result += aux;
			}
		}
		return result;
	}



var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(0,pos1)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

function apenasNumeros(myfield, e, dec)
{
         var key;
         var keychar;

         if (window.event)
                 key = window.event.keyCode;
         else if (e)
                 key = e.which;
         else{
                 return true;
         }

         keychar = String.fromCharCode(key);

         // control keys
         if ((key==null) || (key==0) || (key==8) ||
                 (key==9) || (key==13) || (key==27) )
                 return true;


         // numbers
         else if ((("1234567890").indexOf(keychar) > -1))
                 return true;


         // decimal point jump
         else if (dec && (keychar == "."))
         {
                 myfield.form.elements[dec].focus();
                 return false;
         }
         else
                 return false;
}

function isAlpha(keyChar) {
	return (("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").indexOf(keyChar) > -1);
}

function isNumber(keyChar) {
	return (("0123456789").indexOf(keyChar) > -1);
}

function   isPrintable(keyCode) {
   return ( keyCode >= 48 );
}

function StopEvent(event) {
	    if (event.preventDefault) {
	  	  event.preventDefault();
	      event.stopPropagation();
	    } else {
      	  event.returnValue = false;
     	  event.cancelBubble = true;
        }
}

function checkEmail(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
		return true
	}
	return false;
}

