/*//////////////////////////////////////////////////////////////////
///////////////////////// TICKET SEARCH  //////////////////////////
////////////////////////////////////////////////////////////////*/ 

function ticketsearch_Validator(theForm)
{
  if (theForm.tn.value == "")
  {
    alert("Please enter a Ticket Number.");
    theForm.tn.focus();
    return (false);
  }

  var checkOK = "0123456789";
  var checkStr = theForm.tn.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Ticket Number may only contain numbers. Please try again.");
    theForm.tn.focus();
    return (false);
  }
  
    if (theForm.ts.value == "")
  {
    alert("Please select a State.");
    theForm.ts.focus();
    return (false);
  }

  return (true);
}

/*//////////////////////////////////////////////////////////////////
////////////////////////// SHORT SEARCH  //////////////////////////
////////////////////////////////////////////////////////////////*/ 

function shortsearch_Validator(theForm)
{
  if (theForm.df.value == "")
  {
    alert("Please enter a From Date.");
    theForm.df.focus();
    return (false);
  }

  if (!valdate(theForm.df))
  {
    theForm.df.focus();
    return (false);
  }

  if (theForm.dt.value == "")
  {
    alert("Please enter a To Date.");
    theForm.dt.focus();
    return (false);
  }

  if (!valdate(theForm.dt))
  {
    theForm.dt.focus();
    return (false);
  }

  if (theForm.as.value == "")
  {
    alert("Please enter a Street Name.");
    theForm.as.focus();
    return (false);
  }
  
  if (theForm.ac.value == "")
  {
    alert("Please select a County.");
    theForm.ac.focus();
    return (false);
  }

  return (true);
}

/*//////////////////////////////////////////////////////////////////
/////////////////////////// LONG SEARCH  //////////////////////////
////////////////////////////////////////////////////////////////*/ 

function longsearch_Validator(theForm)
{
  if (theForm.df.value == "")
  {
    alert("Please enter a From Date.");
    theForm.df.focus();
    return (false);
  }

  if (!valdate(theForm.df))
  {
    theForm.df.focus();
    return (false);
  }

  if (theForm.dt.value == "")
  {
    alert("Please enter a To Date.");
    theForm.dt.focus();
    return (false);
  }

  if (!valdate(theForm.dt))
  {
    theForm.dt.focus();
    return (false);
  }

  if ((theForm.cn.value == "") && (theForm.as.value == ""))
  {
    alert("Please enter either an Excavator Company Name or Street Name.");
    theForm.cn.focus();
    return (false);
  }
  
  if ((theForm.ts.value == "") && (theForm.ac.value == ""))
  {
    alert("Please select a County OR a State.");
    theForm.ac.focus();
    return (false);
  }

  return (true);
}

/*//////////////////////////////////////////////////////////////////
////////////////////////// VALIDATE DATE  /////////////////////////
////////////////////////////////////////////////////////////////*/ 

function valdate(the_date){
  var fieldname;
  if (the_date.name == "df"){fieldname = "From"};
  if (the_date.name == "dt"){fieldname = "To"};	
  if(!the_date.value.match(/\d{1,2}\/\d{1,2}\/\d{2,4}/)){
    alert("Please enter " + fieldname + " Date in MM/DD/YYYY format.")
    return false;
  }
  else{
    var thedate = new Date();
    var theyear = thedate.getFullYear();
    var theday = thedate.getDate();
    var themonth = thedate.getMonth() +1;
    var adate = the_date.value.split('/');
    var mm = (adate[0]*1);
    var dd = (adate[1]*1);
    var yyyy = (adate[2]*1);

    if(yyyy < 50 && yyyy < 1900){yyyy = 2000 + yyyy;}
    if(yyyy < 100 && yyyy >= 50){yyyy = 1900 + yyyy;}
    if(mm < 1 || mm > 12){
      alert("Please enter a valid " + fieldname + " Date in MM/DD/YYYY format.")
      return false;
    }
    if(dd < 1 || dd > 31){
      alert("Please enter a valid " + fieldname + " Date in MM/DD/YYYY format.")
      return false;
    }
    if (mm >= themonth && dd > theday && yyyy >= theyear){
      alert("The " + fieldname + " Date cannot be in the future.");
      return false;
    }
    if (mm <= 6 && dd <= 30 && yyyy <= 2002){
      alert("The " + fieldname + " Date cannot be older than 07/01/2002.");
      return false;
    }
  }
  return true;
}
