function checkRequired(p_obj,p_msg) {
   if (isNull(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      return false;
   }
   else {
      return true;
   }
}

function checkNumber(p_obj,p_msg) {
   if (!isNumber(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkPosNumber(p_obj,p_msg) {
   if (!isPosNumber(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkYear(p_obj, p_msg) {
   if (!isYear(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkInteger(p_obj,p_msg) {
   if (!isInteger(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }

   return true;
}

function checkDate(p_obj, p_msg) {
   if (!isDate(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkShortDate(p_obj, p_msg) {
   if (!isShortDate(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkUSDate(p_obj, p_msg) {
   if (!isUSDate(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkDateTime(p_obj, p_msg) {
   if (!isDate(p_obj.value.substring(0,10)) || 
       !isTime(p_obj.value.substring(11,19)) ||
       p_obj.value.substring(10,11) != ' ') {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkTime(p_obj, p_msg) {
   if (!isTime(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkShortTime(p_obj, p_msg) {
   if (!isShortTime(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkPercent(p_obj,p_msg) {
   if (!isNumber(p_obj.value) || !isPercent(p_obj.value)) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkSize(p_obj,p_count_target,p_msg) {
   var ptexto = trim(p_obj.value);
   if (ptexto.length < p_count_target) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }
   else {
      return true;
   }
}

function checkMaxValue(p_obj,p_msg) {
   if (p_obj.max_value &&
       ((p_obj.v_type=="integer" && parseInt(p_obj.value,10) > parseInt(p_obj.max_value,10)) ||
        (p_obj.v_type=="number" && parseFloat(p_obj.value) > parseFloat(p_obj.max_value)) ||
        (p_obj.v_type=="date" && parseInt(DateSerial(p_obj.value),10) > parseInt(DateSerial(p_obj.max_value),10)) ||
        (p_obj.v_type=="time" && parseInt(TimeSerial(p_obj.value),10) > parseInt(TimeSerial(p_obj.max_value),10)) ||
        (p_obj.v_type=="shorttime" && parseInt(ShortTimeSerial(p_obj.value),10) > parseInt(ShortTimeSerial(p_obj.max_value),10)) ||
        (p_obj.v_type=="shortdate" && parseInt(ShortDateSerial(p_obj.value),10) > parseInt(ShortDateSerial(p_obj.max_value),10)) ||
        (p_obj.v_type=="usdate" && parseInt(USDateSerial(p_obj.value),10) > parseInt(USDateSerial(p_obj.max_value),10))
        ) ) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }

   return true;
}

function checkMinValue(p_obj,p_msg) {
   if (p_obj.min_value &&
       ((p_obj.v_type=="integer" && parseInt(p_obj.value,10) < parseInt(p_obj.min_value,10)) ||
        (p_obj.v_type=="number" && parseFloat(p_obj.value) < parseFloat(p_obj.min_value)) ||
        (p_obj.v_type=="date" && parseInt(DateSerial(p_obj.value),10) < parseInt(DateSerial(p_obj.min_value),10)) ||
        (p_obj.v_type=="time" && parseInt(TimeSerial(p_obj.value),10) < parseInt(TimeSerial(p_obj.min_value),10)) ||
        (p_obj.v_type=="shorttime" && parseInt(ShortTimeSerial(p_obj.value),10) < parseInt(ShortTimeSerial(p_obj.min_value),10)) ||
        (p_obj.v_type=="shortdate" && parseInt(ShortDateSerial(p_obj.value),10) < parseInt(ShortDateSerial(p_obj.min_value),10)) ||
        (p_obj.v_type=="usdate" && parseInt(USDateSerial(p_obj.value),10) < parseInt(USDateSerial(p_obj.min_value),10))
        ) ) {
      alert(p_msg);
      p_obj.focus();
      p_obj.select();
      return false;
   }

   return true;
}

function isDigit(pchar) {
   return ("0123456789.".indexOf(pchar)>=0);
}

function isNumeric(ptexto) {
   var numeric = true;
   
   ptexto = trim(ptexto);
   
   if (ptexto.length==0)
      return false;
      
   for (i=0; i < ptexto.length; i++) {
      if (!isDigit(ptexto.charAt(i)) ) {
         numeric = false;
         break;
      }
   }
   
   return numeric;
}

function isNull(valor) {
   return (valor==null || valor=='null' || valor==(void 0) || valor=='' || trim(valor)=="");
}

function isPosNumber(ptexto) {
   ptexto = trim(ptexto);
   var ppos = ptexto.lastIndexOf(".");
   
   if (ppos<0) {
      ppos = ptexto.lastIndexOf(",");
   }
   
   if (ppos>=0) {
      var vantes = ptexto.substring(0,ppos);
      var vdepois = ptexto.substring(ppos+1,ptexto.length);
      
      if (vantes=="" && vdepois=="")
         return false
      else   
         return (isNumeric(nvl(vantes,"0")) && isNumeric(nvl(vdepois,"0")));
   }
   else {
      return (isNumeric(ptexto));
   }
}

function parseNumber(ptexto) {
   ptexto=trim(ptexto);
   
   var ppos = ptexto.lastIndexOf(",");

   if (ppos>=0) {
      vantes = ptexto.substring(0,ppos).replace(/\./g,"");
      vdepois = ptexto.substring(ppos+1,ptexto.length);
      
      return (vantes==""?"0":vantes) + "." + vdepois;
   }
   else {
      return ptexto.replace(/\./g,"");
   }
}

function parseNumberUSD(ptexto) {
   ptexto=trim(ptexto);
   
   var ppos = ptexto.lastIndexOf(".");

   if (ppos>=0) {
      vantes = ptexto.substring(0,ppos).replace(/\,/g,"");
      vdepois = ptexto.substring(ppos+1,ptexto.length);
      
      return (vantes==""?"0":vantes) + "." + vdepois;
   }
   else {
      return ptexto.replace(/\,/g,"");
   }
}

function isInteger(ptexto) {
   ptexto = ltrim (ptexto);

   if (ptexto.charAt(0)=='-') {
      ptexto = ptexto.substring(1,ptexto.length);
   }

   return (isNumeric(ptexto));
}

function isNumber(ptexto) {
   ptexto = ltrim (ptexto);

   if (ptexto.charAt(0)=='-') {
      ptexto = ptexto.substring(1,ptexto.length);
   }
   
   return (isPosNumber(ptexto));
}

function isDate(ptexto) {
   ptexto = trim (ptexto);
   if (ptexto.length !=10) {
      return false;
   }
   
   var dia = parseInt(ptexto.substring(0,2),10);
   var mes = parseInt(ptexto.substring(3,5),10);
   var ano = parseInt(ptexto.substring(6,10),10);
   
   if (isNaN(dia) || isNaN(mes) || isNaN(ano)) {
      return false;
   }
   
   if (mes<1 || mes >12) return false;
   
   if (ptexto.substring(2,3) != "/") return false;
   
   if (dia<1 || dia > 31) return false;
   
   if (ptexto.substring(5,6) != "/") return false;
   
   if (ano<=1000) return false;
   
   if (mes==4 || mes==6 || mes==9 || mes==11) {
      if (dia==31) return false;
   }
   
   if (mes==2) {
      var g = parseInt(ano/4,10);
      if (isNaN(g)) {
         return false;
      }
      if (dia>29) return false;
      if (dia==29 && ((ano/4)!=parseInt(ano/4,10))) return false;
   }
   
   
   return true;
}

function isUSDate(ptexto) {
   ptexto = trim (ptexto);
   if (ptexto.length !=10) {
      return false;
   }
   
   var mes = parseInt(ptexto.substring(0,2),10);
   var dia = parseInt(ptexto.substring(3,5),10);
   var ano = parseInt(ptexto.substring(6,10),10);
   
   if (isNaN(dia) || isNaN(mes) || isNaN(ano)) {
      return false;
   }
   
   if (mes<1 || mes >12) return false;
   
   if (ptexto.substring(2,3) != "/") return false;
   
   if (dia<1 || dia > 31) return false;
   
   if (ptexto.substring(5,6) != "/") return false;
   
   if (ano<=1000) return false;
   
   if (mes==4 || mes==6 || mes==9 || mes==11) {
      if (dia==31) return false;
   }
   
   if (mes==2) {
      var g = parseInt(ano/4,10);
      if (isNaN(g)) {
         return false;
      }
      if (dia>29) return false;
      if (dia==29 && ((ano/4)!=parseInt(ano/4,10))) return false;
   }
   
   
   return true;
}

function isShortDate(ptexto) {
   ptexto = trim (ptexto);
   if (ptexto.length!=7) {
      return false;
   }
   
   var mes = parseInt(ptexto.substring(0,2),10);
   var ano = parseInt(ptexto.substring(3,7),10);
   
   if (isNaN(mes) || isNaN(ano)) 
      return false;
   
   if (mes<1 || mes >12) 
      return false;
   
   if (ptexto.substring(2,3) != "/") 
      return false;
   
   if (ano<=1000) 
      return false;
   
   return true;
}

