function isValidIPAddress(ipaddr) {
   var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
   if (re.test(ipaddr)) {
      var parts = ipaddr.split(".");
   if (parseInt(parseFloat(parts[0])) == 0) {
       return false;
   }
   for (var i=0; i<parts.length; i++) {
      if (parseInt(parseFloat(parts[i])) > 255) {
        return false;
      }
   }
   return false;
   } else { return true; }
}

function isFieldEmpty(text) {
   if (text !=undefined) {
//      text = chopExtraSpaces (text);
      return !(text.length > 0);
   }
}

function isValidEmail(field) {
   with (field) {
      apos=value.indexOf("@");
      dotpos=value.lastIndexOf(".");
         if (apos<1||dotpos-apos<2) {
            return true;
         }
         else {return false;}
   }
}

function hasSpaces(text) {
   return (text.search (/\s/) > -1);
}
function isZero(num) {
   return (num == 0);
}
function isInteger(num) {
   return (num == parseInt(num));
}
function isFloat(num) {
   return (num == parseFloat(num));
}
function isPositive(num) {
   return (num > 0);
}
function isPositiveInteger(num) {
   return (isInteger (num) && isPositive (num));
}
function isWhole(num) {
   return (isInteger (num) && (isPositive (num) || isZero (num)));
}
function isSelected(selectbox) {
   return (selectbox.selectedIndex == 0);
}

function chopLeadingSpaces(text) {
   return text.replace (/^\s+/, "");
}
function chopTrailingSpaces (text) {
   return text.replace (/\s+$/, "");
}
function chopExtraSpaces (text) {
   return chopTrailingSpaces (chopLeadingSpaces (text));
}
function chopAllSpaces (text) {
   while (text.search (/\s/) >= 0) {text = text.replace (/\s/g, "");}
   return chopTrailingSpaces (chopLeadingSpaces (text));
}
function chopExtraLineBreaks (text) {
   return text.replace (/(\n\s*){2,}/g, "\n");
}


function checkNumberField(field,fieldname,errors) {
   if (isFieldEmpty(field.value)) {
      errors[errors.length] = "The field <strong>" + fieldname + "</strong> cannot be blank.";
      showFieldError(field);
   } else if (!isInteger(field.value)) {
      errors[errors.length] = "The field <strong>" + fieldname + "</strong> must be a whole number.";
      showFieldError(field);
   }
   return errors;
}
function checkForValidEmail(field,fieldname,errors) {
   field = getLayer(field);
   if (isValidEmail(field)) {
      errors[errors.length] = "The field <strong>" + fieldname + "</strong> is not valid.";
      showFieldError(field);
   } else { clearFieldError(field); }
   return errors;
}
function checkForEmptyField(field,fieldname,errors) {
   field = getLayer(field);
   if (isFieldEmpty(field.value)) {
      errors[errors.length] = "The field <strong>" + fieldname + "</strong> cannot be blank.";
      showFieldError(field);
   } else { clearFieldError(field); }
   return errors;
}
function checkForValidIP(field,fieldname,errors) {
   field = getLayer(field);
   if (isValidIPAddress(field.value)) {
      errors[errors.length] = "The IP Address entered in <strong>" + fieldname + "</strong> is not valid.";
      showFieldError(field);
   } else { clearFieldError(field); }
   return errors;
}
function checkForSelection(field,fieldname,errors) {
   field = getLayer(field);
   if ( field.selectedIndex == 0 ) {
  // if (field.options.length == 0){
      errors[errors.length] = "Make a selection in the <strong>" + fieldname + "</strong> field.";
      showFieldError(field);
   } else { clearFieldError(field); }
   return errors;
}
function showFieldError(field) {
   field.className = 'textfield fixThis';
//   field.focus();
}
function clearFieldError(field) {
   field.className = 'textfield';
}
function handleServerError(error_list, e) {
   if (error_list[e]) {
      alert(error_list[e]);
      showErrors(new Array(error_list[e]),"operation failed.");
   }
   else {
      checkLastOp();
   }
}
function showErrors(errors,conclusionText) {
   var opStatus = getLayer('opStatus');
   var opInfo = getLayer('opInfo');

   var summaryText = "There " + ( (errors.length > 1) ? "were " : "was " )
       + errors.length + ( (errors.length > 1) ? " errors. " : " error. " );

   if (conclusionText)  {summaryText += conclusionText;}
      var errorText = "<ol>";
      for (var i=0; i<errors.length; i++) {
         errorText += "<li>" + errors[i] + "</li>";
      }
      errorText += "</ol>";

   opInfo.innerHTML = "<p class=\"errors\">" + summaryText + "</p><div id=\"errorList\">" + errorText + "</div>";
   opInfo.className = "error";
   opStatus.className = "displayBlock";
}

function ismaxedout(obj){
   var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
   if (obj.getAttribute && obj.value.length>mlength)
   obj.value=obj.value.substring(0,mlength)
}
