//###############################
//# Declarations                #
//###############################

//###############################
//# Functions                   #
//###############################
function _Litter_mfLitterSearch(pstrComponentName, pstrWebPage)
{

  var strTown = document.getElementById('txtLitterTown_' + pstrComponentName).value;
  var strCountyId = document.getElementById('cboCounty_' + pstrComponentName).value;
  var strLitterType = "";
  var strDateFrom = document.getElementById('txtLitterDateFrom_' + pstrComponentName).value;
  var strDateTo = document.getElementById('txtLitterDateTo_' + pstrComponentName).value;
  var strErrors = ''; 
  var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
  

  
  if (strDateFrom == 'dd/mm/yyyy' || strDateFrom == '')
     strDateFrom = '';
     
  if (strDateTo == 'dd/mm/yyyy' || strDateTo == '')
     strDateTo = '';




  if (strDateFrom != '')
  {

    var monthfield=strDateFrom.split("/")[1]
    var dayfield=strDateFrom.split("/")[0]
    var yearfield=strDateFrom.split("/")[2]
    var dayobj = new Date(yearfield, monthfield-1, dayfield)

    if (!validformat.test(strDateFrom))
       strErrors = strErrors + "Invalid Date Format. Please correct the 'Date From' field and submit again. Must be dd/mm/yyyy\n";
    else{ //Detailed check for valid date ranges

    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
     strErrors = strErrors + "Invalid Day, Month, or Year range detected. Please correct the 'Date From' field and submit again. An example is dd/mm/yyyy\n";
     }
  }

  if (strDateTo != '')
   {
    var monthfield=strDateTo.split("/")[1]
    var dayfield=strDateTo.split("/")[0]
    var yearfield=strDateTo.split("/")[2]
    var dayobj = new Date(yearfield, monthfield-1, dayfield)

    if (!validformat.test(strDateTo))
       strErrors = strErrors + "Invalid Date Format. Please correct the 'Date To' field and submit again. Must be dd/mm/yyyy\n";
    else{ //Detailed check for valid date ranges

    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
       strErrors = strErrors + "Invalid Day, Month, or Year range detected. Please correct the 'Date To' field and submit again. An example is dd/mm/yyyy\n";
     }
   }

  if (strErrors == "")
    {
     if (strTown == '' && strCountyId == '' && strLitterType == '' && strDateFrom == '' && strDateTo == '')
       window.location.href=pstrWebPage;
     else
       window.location.href=pstrWebPage + '?LitterSearch=1&Town=' + strTown + '&CountyId=' + strCountyId + '&LitterType=' + strLitterType + '&DateFrom=' + strDateFrom + '&DateTo=' + strDateTo;
    }
    else
     alert(strErrors);
    }




function _Litter_mfButton(pstrComponentName)
{
  if(document.getElementById('txtLitterTown_' + pstrComponentName).value != "" || document.getElementById('cboCounty_' + pstrComponentName).value != "" 
     || document.getElementById('txtLitterType_' + pstrComponentName).value != "" || document.getElementById('txtLitterDateFrom_' + pstrComponentName).value != ""
     || document.getElementById('txtLitterDateTo_' + pstrComponentName).value != "")
    document.getElementById('cmdLitterSearch_' + pstrComponentName).value = "Search >";   
  else
    document.getElementById('cmdLitterSearch_' + pstrComponentName).value = "Return All >";
    
       

}

