
// Verifica se uma String corresponde a uma data
//----------------------------------------------
function isDate(dateStr) {
  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var matchArray = dateStr.match(datePat); // is the format ok?
  if (matchArray == null) {
    //alert("Entre uma data válida, no formato (dd/mm/yyyy).");
    return false;
  }
  month = matchArray[3]; // parse date into variables
  day   = matchArray[1];
  year  = matchArray[5];
  if (month < 1 || month > 12) { // check month range
    //alert("O mês deve estar entre 1 e 12.");
    return false;
  }
  if (day < 1 || day > 31) {
    //alert("O dia deve estar entre 1 e 31.");
    return false;
  }
  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    //alert("O mês "+month+" não tem 31 dias!")
    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("Fevereiro de " + year + " não tem " + day + " dias!");
      return false;
    }
  }
  return true; // date is valid
}


// Verifica se uma String corresponde a um numero 
//-----------------------------------------------
function isNumeric(val) {
  var dp = false;
  for (var i=0; i < val.length; i++) {
    if (!isDigit(val.charAt(i))) {
      if (val.charAt(i) == ',') { // padrão brasileiro...
        if (dp == true) { return false; } // already saw a decimal point
        else { dp = true; }
      }
      else if (val.charAt(i) == '.') {
      }
      else {
        return false;
      }
    }
  }
  return true;
}


// isDigit(value)
//   Returns true if value is a 1-character digit
//-----------------------------------------------
function isDigit(num) {
  var string="1234567890";
  if (string.indexOf(num) != -1) {
    return true;
  }
  return false;
}


// Efetua trim esquerdo
//---------------------
function trimL(str) {
var n=0;
var i=0;

  if (str=='') return '';

  for (i=0; i<str.length; i++) {
   n = str.charCodeAt(i);   
   if (n>32) break;   	
  }

  if (i<=str.length)
  	return str.substring(i,str.length);
  else return str;
}


// Efetua trim direito
//---------------------
function trimR(str) {
var n=0;
var i=0;

  if (str=='') return '';

  for (i=str.length-1; i>=0; i--) {
   n = str.charCodeAt(i);

   if (n>32) break;   	
  }

  if (i>=0)
  	return str.substring(0,i+1);
  else return str;
}


// Efetua trim de ambos os lados da string
//----------------------------------------
function trim(str) { 
  var r = trimR(str);
  var l= trimL(r);
  return l;
}


// Muda a visibilidade de um objeto
//---------------------------------
function changeVisibility(sID, sDisplay, checked)
{
		var oDet = document.all[sID];

		if (oDet.style.display != "" && checked==true)
    	oDet.style.display = sDisplay;
    else if (checked==false)
      oDet.style.display = "none";
}

// Valida um endereço de email
//----------------------------
function isEmail(email) {
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";

    // Check for null
    if (email == "") {
        return true;
    }

    // Check for invalid characters as defined above
    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }
    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }
    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    // There must be at least one @ symbol
    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    // But only ONE @ symbol
    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    // Also check for at least one period after the @ symbol
    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}
