// This file contains javascript functions used by channel search forms
// Used by the main search forms and quick search forms on main website
// Alerts user if all mandatory fields have not been entered
// frm: The form object
// category: The category the search was made from 
// Return Value: true if all mandatory fields have been entered in search form, false otherwise
function validateMandatoryFields(frm, category)
{
        return validateMandatoryFields(frm, category, false);
}
// Used by the main search forms and quick search forms on main website
// Alerts user if all mandatory fields have not been entered
// frm: The form object
// category: The category the search was made from 
// isQuickSearch: (boolean) Did the search originate from a quick search form
// Return Value: true if all mandatory fields have been entered in search form, false otherwise
function validateMandatoryFields(frm, category, isQuickSearch)
{
    var postcodePopulated = true;
    var minpricePopulated = true;
    var maxpricePopulated = true;
    var makePopulated = true;
    // Bike quick search form doesn't have the min price on it, so we need different validation for
    // this case 
    if (isQuickSearch && category=='BIKE')
    {
            if(frm.postcode.value=="" || frm.postcode.value.toLowerCase()=="full postcode" || frm.postcode.value.length<=4)
            {
              postcodePopulated = false;
            }
            if(frm.make.value=="" || frm.make.value=="ANY")
            {
              makePopulated = false;
            }
            if (!postcodePopulated && !makePopulated)
            {
              alert("Please enter a full postcode and a make");
              return false;
            } 
            else
            {
              if (!postcodePopulated && makePopulated)
              {
                alert("Please enter a full postcode");
                return false;
              } 
              else
              {
                if (postcodePopulated && !makePopulated)
                {
                 alert("Please enter a make");
                 return false;
                }
              }
            }
    }
    else
    {
     switch(category)
     {
       // Agriculture channel does not have a make so we do not validate on that
       case "PLANT":
               if(frm.postcode.value=="" || frm.postcode.value.toLowerCase()=="full postcode" || frm.postcode.value.length<=4)
               {
                 postcodePopulated = false;
               }
               if(frm.min_pr.value=="" || frm.min_pr.value=="75")
               {
                 minpricePopulated = false;
               }
              if(frm.max_pr.value=="")
            {
              maxpricePopulated = false;
            }
 
               if (!postcodePopulated && (!minpricePopulated && !maxpricePopulated))
               {
                   alert("Please enter a full postcode and a max or min price");
                   return false;
               }
                  else
                  {
                    if (postcodePopulated && (!minpricePopulated && !maxpricePopulated))
                    {
                        alert("Please enter a max or min price");
                        return false;
                    }
                    else
                    {
                     if (!postcodePopulated && (minpricePopulated || maxpricePopulated))
                     {
                         alert("Please enter a full postcode");
                         return false;
                     }
                    }
                  }                  
       break;
 
       default:
 
             if(frm.postcode.value=="" || frm.postcode.value.toLowerCase()=="full postcode" || frm.postcode.value.length<=4)
             {
               postcodePopulated = false;
             }
             if(frm.min_pr.value=="" || frm.min_pr.value=="75" || frm.min_pr.value=="1")
             {
               minpricePopulated = false;
             }
                if(frm.max_pr.value=="")
                {
                  maxpricePopulated = false;
                }
              
             if(frm.make.value=="" || frm.make.value=="ANY")
             {
               makePopulated = false;
             }
 
             if (!postcodePopulated && (!minpricePopulated && !maxpricePopulated && !makePopulated))
             {
                alert("Please enter a full postcode and a make or a max or min price");
                return false;
             }
                else
                {
                  if (postcodePopulated && (!minpricePopulated && !maxpricePopulated && !makePopulated))
                  {
                     alert("Please enter a make or a max or min price");
                     return false;
                  }
                  else
                  {
                    if (!postcodePopulated && (minpricePopulated || maxpricePopulated || makePopulated))
                    {
                       alert("Please enter a full postcode");
                       return false;
                    }
                  }
                }                
         break;
     }
    }
    return true;
}