

function checkRange(beginDate, endDate)
 {
   var retvalue = true;

   //retvalue = checkDate(beginDate);
   //retvalue = checkDate(endDate);
   if (!retvalue) { return false; };

   start = stringToDate(beginDate);
   stop = stringToDate(endDate);

   if (start.getTime() <= stop.getTime()) return true;
   alert("Invalid Range - End Date must be equal to or later than Begin Date");
   return false;
 }


function stringToDate (thisDate)
 {
  var monthStr = "";
  var dayStr = "";
  var yearStr = "";
  var month = 0;
  var day = 0;
  var year = 0;
  var leap = 1;
  var pos1 = 0;
  var pos2 = 0;
  var alertMsg = "";
  var tempStr = "";

  var newDate = new Date();

  pos1 = parseInt(thisDate.value.indexOf("/"));
  pos2 = parseInt(thisDate.value.lastIndexOf("/"));
  if (pos1 > 0 && pos2 > 2 && pos1 != pos2)
   {
    tempStr = rtrim( thisDate.value.substring(pos2+1, thisDate.value.length) );

    if ( tempStr.length  ==  4 )
     {
      // get month, day and year portions
      monthStr = thisDate.value.substring(0,pos1);
      dayStr = thisDate.value.substring(pos1+1, pos2);
      yearStr = thisDate.value.substring(pos2+1,thisDate.value.length);
      month = stripInt(monthStr);
      day = stripInt(dayStr);
      year = stripInt(yearStr);
     }
   };

  newDate.setYear(year);
  newDate.setMonth(month-1);
  newDate.setDate(day);
  return newDate;
}


function checkDate(thisDate)
 {
  var monthStr = "";
  var dayStr = "";
  var yearStr = "";
  var month = 0;
  var day = 0;
  var year = 0;
  var leap = 1;
  var pos1 = 0;
  var pos2 = 0;
  var alertMsg = "";
  var tempStr = "";
  retvalue = false;


  if (thisDate.length == 0) return true;
  pos1 = parseInt(thisDate.value.indexOf("/"));
  pos2 = parseInt(thisDate.value.lastIndexOf("/"));
  if (pos1 > 0 && pos2 > 2 && pos1 != pos2)
   {
    tempStr = rtrim( thisDate.value.substring(pos2+1, thisDate.value.length) );

    if ( tempStr.length  ==  4 )
     {
      // get month, day and year portions
      monthStr = thisDate.value.substring(0,pos1);
      dayStr = thisDate.value.substring(pos1+1, pos2);
      yearStr = thisDate.value.substring(pos2+1,thisDate.value.length);
      month = stripInt(monthStr);
      day = stripInt(dayStr);
      year = stripInt(yearStr);

      // determine if leap year
      if ( ((year % 4) == 0) && ((year % 100) != 0)) leap = 0;

      if (month < 1 || month > 12)
       {
        alertMsg = " The month " + month + " is invalid. It must be between 1 and 12.  ";
       }
      else if ((month == 9 || month == 4 || month == 6 || month == 11)
              && (day < 1 || day > 30))
       {
        alertMsg = " The day " + day + " is invalid for the month entered.  ";
       }
      else if ((month == 2) && (day < 1 || ((day > 28 && leap != 0)
              || (day > 29 && leap == 0))))
       {
        alertMsg = " The day " + day + " is invalid for the month entered.  ";
       }
      else if (day < 1 || day > 31)
       {
        alertMsg = " The day " + day + " is invalid for the month entered.  ";
       }
     }
    else
     {
      alertMsg = "The date you entered, " +  thisDate.value + ",  has a year less than 4 characters long.  ";
     }
   }
  else if (thisDate.value.length > 0)
   {
    alertMsg = thisDate.value + ": Invalid date or format. ";
   };

  if (alertMsg.length > 0)
   {
    alertMsg = alertMsg + "Please use the indicated format.";
    alert(alertMsg);
    thisDate.focus();
    retvalue = false;
   }
  else
   {
    retvalue = true;
   };

 return retvalue;
}


function rtrim(thisStr)
   {
      var returnStr = "";
      var len = 0;
      var pos = 0;
      var done = 0;

      len = thisStr.length;
      pos = len - 1;
      while ( done == 0 ) {
         if ( thisStr.charAt(pos) == " " )
         {
            pos = pos - 1;
         }
         else
         {
            done = 1;
         }
      }
      if ( pos != len - 1 )
      {
        returnStr = thisStr.substring(0, pos);
      }
      else
      {
         returnStr = thisStr;
      }
      return returnStr;
   }


function  stripInt(thisString)
   {
     var aString = "";
     var returnVar = 0;

     aString = thisString;
     if (aString.length == 0) {
       returnVar = 0;
     }
     else {
       while ((aString.length > 1) && (aString.charAt(0) == "0")) {
         aString = aString.substr(1,aString.length);
       }
       returnVar = parseInt(aString);
     }
     return returnVar;
}


function conditionalCheckRange(beginDate, endDate, value)
 {

  var retvalue = false;

  if (value == "UserDates")
   {
      retvalue = checkRange (beginDate, endDate);
   }
  else
   {
      retvalue = true;
   }
  return retvalue;
 }


function checkDateType (thisDate)
 {
  choice = thisDate.options[thisDate.selectedIndex].value;

  if (choice.toUpperCase() == "SHIPDATE")
    {
      if (document.Search.orderType[2].selected == "0")
       {
         document.Search.orderType[2].selected = "1";
        alert ("Ship Date searches can only be performed on orders that have been shipped.  Therefore, we have automatically set the Order Type to be restricted to Shipped Orders only.");
       };
    };
 }


function checkOrderType (thisOrderType)

{
  choice = thisOrderType.options[thisOrderType.selectedIndex].value;

  if ((choice.toUpperCase() == "ALL") || (choice.toUpperCase() == "OPEN"))
    {
    if (document.Search.dateType[0].selected == "0")
     {
      document.Search.dateType[0].selected = "1";
      alert ("Since you are no longer restricting your search to Shipped Orders, we've reset your Date Type to Order Date.");
     };
    };
}


function checkCustomerNumber(thisCustNumber)
 {
  var retvalue = true;
  
  if (thisCustNumber.value == null || thisCustNumber.value.length <= 0) {
    alert("Customer Number is required.");
    retvalue = false;
  }
  
  return retvalue;
 }


