var SubmitCount = 0;

//function DeleteSubmission()
//{
//   if(confirm('You should only delete a submission if you\'ve made a mistake, etc.\n Once you\'ve deleted the submission it cannot be recovered.\nAre you sure you want to permanantly delete this submission?') == true)
//   {
//      if(confirm('If you continue now there is no going back. Are you absolutely sure you want to DELETE this submission? This is your last chance to turn back') == true)
//      {
//         document.forms.bleepfoo.confirmed.value = '1'; //set confirmed to 0
//         document.forms.bleepfoo.submit();
//         document.getElementById('DeleteLink').href='#';
//         return true;
//     }
//      else
//      {
//         document.forms.bleepfoo.confirmed.value = '0'; //set confirmed to 0
//         return false;
//      }
//   }
//   else
//   {
//      document.forms.bleepfoo.confirmed.value = '0'; //set confirmed to 0
//      return false;
//   }
//}

function CloseSubmission()
{
   if(confirm('Are you sure you want to close this submission?') == true)
   {
      document.forms.closesubmission.confirmed.value = '1'; //set confirmed to 0
      document.forms.closesubmission.submit();
      document.getElementById('CloseLink').href='#';
      return true;
   }
   else
   {
      document.forms.closesubmission.confirmed.value = '0'; //set confirmed to 0
      return false;
   }
}

function OpenSubmission()
{
   if(confirm('Are you sure you want to open this submission?') == true)
   {
      document.forms.closesubmission.confirmed.value = '1'; //set confirmed to 0
      document.forms.closesubmission.submit();
      document.getElementById('CloseLink').href='#';
      return true;
   }
   else
   {
      document.forms.closesubmission.confirmed.value = '0'; //set confirmed to 0
      return false;
   }
}

function ValidateSubmission()
{
   f = document.forms.submissions;

   var CommonNameLength = f.common_name.value.length;
   var BinomialLength = f.binomial.value.length;

   if (document.getElementById('offer').checked || document.getElementById('request').checked)
   {
      //...
   }
   else
   {
      alert("You must specify whether this is an offer or a request");
      return false;
   }

   if (document.getElementById('seed').checked || document.getElementById('bulb').checked || document.getElementById('unrooted').checked || document.getElementById('rooted').checked || document.getElementById('plant').checked)
   {
      //...
   }
   else
   {
      alert("You must select either Seed, Unrooted cutting, Rooted cutting, or Plant");
      return false;
   }

   if (CommonNameLength == 0 && BinomialLength == 0)
   {
      alert("You must enter at least a Common Name or a Binomial nomenclature");
      return false;
   }
   
   if (CommonNameLength < 3 && CommonNameLength != 0) { alert("Common Name too short"); return false; }
   if (BinomialLength < 3 && BinomialLength != 0) { alert("Binomial nomencloture too short"); return false; }
   
   if (f.formtype.value == 'advanced')
   {
         if(f.uses.options[f.uses.selectedIndex].value == "Other" && f.uses_other.value.length <= 0)
         {
            alert("The 'Other' option for 'What is this plant used for?' is selected but the field is blank. If you don't know the plants uses please select 'I'm not sure'");
            return false; 
         }

         if(f.typeofplant.options[f.typeofplant.selectedIndex].value == "Other" && f.type_other.value.length <= 0)
         {
            alert("The 'Other' option for 'What type of plant is this?' is selected but the field is blank. If you don't know what type of plant this is please select 'I'm not sure'");
            return false;
         }

   }
   if (f.userinput.value.length > 2048) { alert("'Description' field too large"); return false; } 

   if (SubmitCount == 0) //make sure form is only submitted once
   {
      SubmitCount++;
      
      //clear advanced variables if not using advanced mode
      if (f.formtype.value != 'advanced')
      {
         f.uses.value = '';
         f.uses_other.value = '';
         f.typeofplant.value = '';
         f.type_other.value = '';
         f.lifespan.value = '';
         f.location.value = '';
         f.growdifficulty.value = '';
         f.color.value = '';
      }      

      return true;
   }
}

function ValidateLogin()
{
   f = document.forms.login;
   if(f.wpe_username.value.length == 0)  { alert("Username was left blank"); return false; }
   if(f.wpe_username.value.length < 3)   { alert("Username must be at least 3 characters"); return false; }
   if(f.wpe_username.value.length > 16)  { alert("Username cannot be larger than 16 characters"); return false; }
   if(f.wpe_password.value.length == 0)  { alert("Password was left blank"); return false; }
   if(f.wpe_password.value.length < 6)   { alert("Password must be at least 6 characters"); return false; }
   if(f.wpe_password.value.length > 256) { alert("Password cannot be larger than 256 characters"); return false; }
   return true;
}

function ValidateSignup()
{
   f = document.forms.createaccount;
   if(f.username.value.length == 0)  { alert("Username was left blank"); return false; }
   if(f.username.value.length < 3)   { alert("Username must be at least 3 characters"); return false; }
   if(f.username.value.length > 16)  { alert("Username cannot be larger than 16 characters"); return false; }
   if(f.email.value.length < 5)      { alert("Please enter a valid email address"); return false; }
   if(f.password.value.length == 0)  { alert("Password was left blank"); return false; }
   if(f.password.value.length < 6)   { alert("Password must be at least 6 characters"); return false; }
   if(f.password.value.length > 256) { alert("Password cannot be larger than 256 characters"); return false; }
   if(f.password.value != f.cpassword.value) { alert("Passwords do not match"); return false; }
   return true;
}