// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
    var str = elem.value;
    var re = /.+/;
    if(!str.match(re)) {
        alert(elem.name.toUpperCase() + " is required.  Please enter a valid value.");
        setTimeout("call focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}

//validates that the entry is a positive or negative number
function isNumber(elem) {
    var str = elem.value;
    var re = /^[-]?\d*\.?\d*$/;
    str = str.toString();
    if (!str.match(re)) {
        alert("Enter only numbers into the " + elem.name.toUpperCase() + "field.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    }
    return true;
}


// validate that the user made a selection other than default
function isChosen(select) {
    if (select.selectedIndex == 0) {
        alert("Please make a choice from the list.");
        return false;
    } else {
        return true;
    }
}

// validate that the user has checked one of the radio buttons
function isValidRadio(radio) {
    var valid = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return true;
        }
    }
    alert("Make a choice from the radio buttons.");
    return false;
}

// set the focus to element that failed validation and select any text
function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus();
    elem.select();
}

// validate that entry is a valid date
function isDate(elem) {
    var mo, day, yr;
    var entry = elem.value;
    var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
    var valid = (reLong.test(entry)) || (reShort.test(entry));
    if (valid) {
        var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(0, delim1), 10);
        day = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        // handle two-digit year
        if (yr < 100) {
            var today = new Date();
            // get current century floor (e.g., 2000)
            var currCent = parseInt(today.getFullYear() / 100) * 100;
            // two digits up to this year + 15 expands to current century
            var threshold = (today.getFullYear() + 15) - currCent;
            if (yr > threshold) {
                yr += currCent - 100;
            } else {
                yr += currCent;
            }
        }
        var testDate = new Date(yr, mo-1, day);
        if (testDate.getDate() == day) {
            if (testDate.getMonth() + 1 == mo) {
                if (testDate.getFullYear() == yr) {
                    // fill field with database-friendly format
                    elem.value = mo + "/" + day + "/" + yr;
                    return true;
                } else {
                    alert("There is a problem with the year entry.");
                }
            } else {
                alert("There is a problem with the month entry.");
            }
        } else {
            alert("There is a problem with the date entry.");
        }
    } else {
        alert("Incorrect date format. Enter as mm/dd/yyyy.");
    }

    setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
    return false;
}


// validate that character entered is numeric
function numeralsOnly(evt)  {
      evt = (evt) ? evt : event;
      var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
        ((evt.which) ? evt.which : 0));
      if (charCode > 31 && (charCode < 48 || charCode > 57))  {
        alert("Enter numerals only in this field.");
        return false;
      }
      return true;
}

// alternate date validation routine using split but doesn't reformat date.  May want to merge it with isDate()
function validateDate (strDate) {
   var parsedDate = strDate.split ("/");
   if (parsedDate.length != 3) return false;
   var day, month, year;
   month = parsedDate[0]-1;
   day = parsedDate[1];
   year = parsedDate[2];

   var objDate = new Date (strDate);
   if (month != objDate.getMonth()) return false;
   if (day != objDate.getDate()) return false;
   if (year != objDate.getFullYear()) return false;

   return true;
} 


// this function assumes date is valid with "/" separator - use a date validation routine first

function compareDates(strDate1, strDate2) {

   var lowDate = strDate1.value;
   var highDate = strDate2.value;


   var parsedDate1 = lowDate.split ("/");
   var day1, month1, year1;
   month1 = parsedDate1[0]-1;
   day1 = parsedDate1[1];
   year1 = parsedDate1[2];

   var objDate1 = new Date();
   objDate1.setFullYear(year1, month1, day1);

   var parsedDate2 = highDate.split ("/");
   var day2, month2, year2;
   month2 = parsedDate2[0]-1;
   day2 = parsedDate2[1];
   year2 = parsedDate2[2];

   var objDate2 = new Date();
   objDate2.setFullYear(year2, month2, day2);

//   if (objDate1 > objDate2) {
//      alert(strDate1.name.toUpperCase() + " cannot be after " + strDate2.name.toUpperCase());
//      setTimeout("focusElement('" + strDate1.form.name + "', '" + strDate1.name + "')", 0);
//      return false;


      if (objDate1 < objDate2) {
         return -1;
      }
      else if (objDate1 == objDate2) {
         return 0;
      }
      else {
         return 1;
      }
//   }

}

function isDateSmaller(strDate1, strDate2) {

   answer = (compareDates(strDate1,strDate2))
   if (answer == -1) {
      return true;
   }
   else {
      alert(strDate1.name.toUpperCase() + " cannot be after " + strDate2.name.toUpperCase());
      setTimeout("focusElement('" + strDate1.form.name + "', '" + strDate1.name + "')", 0);
      return false;
   }
}

function isDateLarger(strDate1, strDate2) {

   answer = (compareDates(strDate1,strDate2))
   if (answer == 1) {
      return true;
   }
   else {
      alert(strDate1.name.toUpperCase() + " cannot be before " + strDate2.name.toUpperCase());
      setTimeout("focusElement('" + strDate1.form.name + "', '" + strDate1.name + "')", 0);
      return false;
   }
}

function isDateEqual(strDate1, strDate2) {

   answer = (compareDates(strDate1,strDate2))
   if (answer == 0) {
      return true;
   }
   else {
      alert(strDate1.name.toUpperCase() + " is not equal to " + strDate2.name.toUpperCase());
      setTimeout("focusElement('" + strDate1.form.name + "', '" + strDate1.name + "')", 0);
      return false;
   }
}


function compareNumbers(elem1, elem2) {
   var intLowNum = Number(elem1.value);
   var intHighNum = Number(elem2.value);

   if (intLowNum > intHighNum) {
      alert(elem1.name.toUpperCase() + " cannot be after " + elem2.name.toUpperCase());
      setTimeout("focusElement('" + elem1.form.name + "', '" + elem1.name + "')", 0);
      return false;
   }

   return true;
}

function formatCurrency(num) {

num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}


function makeDate() {
   now = new Date();
   return (now.getMonth()+1) + '/' + now.getDate() + '/' + now.getYear();
}


function updateTotal(elem) {

tmpamount = FileRequest.amount.value;
tst=typeof(tmpamount);
if (tst == "string") {
   if (tmpamount.charAt(0) == "$") {
      trimmedamount = tmpamount.substring(1,tmpamount.length);
      tmpamount = parseFloat(trimmedamount);
   }
amt = parseFloat(tmpamount);  
}
else {

amt = tmpamount

}

tmppostage = FileRequest.postage.value;
tst=typeof(tmppostage)
if (tst == "string") {
  if (tmppostage.charAt(0) == "$") {
     trimmedpostage = tmppostage.substring(1,tmppostage.length);
     tmppostage = parseFloat(trimmedpostage);
  }
postage = parseFloat(tmppostage);
}
else {
postage = tmppostage
}

total = amt + postage;

elem.value = formatCurrency(elem.value);
FileRequest.total.value = total;
FileRequest.total.value = formatCurrency(FileRequest.total.value);

}  //end of function
