function onpageshow()
{
   var i,theForm,j;
   for (i=0;i<document.forms.length;i++)
   {
      theForm=document.forms[i];
      for (j=0;j<theForm.elements.length;j++)
      {
         if (theForm.elements[j].restoreOnShow) theForm.elements[j].disabled=false;
      }
   }
}

function checkFieldForBlankness(field)
{
   if ((field.value=='') || (field.value=='****') || (field.value=="Make your post civil and relevant"))
   {
      window.alert("You did not fill in all fields on the form correctly");
      return false;
   }

   if ((!window.is_opera) || (!is_opera()))
   {
      var j;
      for (j=0;j<field.form.elements.length;j++)
      {
         if (field.form.elements[j].type=='submit')
         {
            addEventListenerAbstract(window,'pageshow',onpageshow,false);
            field.form.elements[j].setAttribute('disabled',true);
            field.form.elements[j].restoreOnShow=true;
         }
      }
   }

   return true;
}

function confirmDelete()
{
   return window.confirm("Are you sure you want to delete this?");
}

function setFieldError(theElement,errorMsg)
{
   var id=theElement.name;
   var errorElement=document.getElementById('error_'+id);
   if (errorElement)
   {
      errorElement.style.display=(errorMsg=='')?'none':'block';
      errorElement.innerHTML='';
      if (errorMsg!='')
      {
         var msgNode=document.createTextNode(errorMsg);
         errorElement.appendChild(msgNode);
      }
   }
   if (errorMsg!='') theElement.className=theElement.className+' input_erroneous';
}

function checkForm(theForm)
{
   var i,j,theClass,theElement,required,myValue,erroneous=false,errorMsg,regexp;
   for (j=0;j<theForm.elements.length;j++)
   {
      if (!theForm.elements[j]) continue;

      theElement=theForm.elements[j];
      errorMsg='';

      if (theElement.type=='hidden')
      {
         if (theElement.name=='js_on') theElement.value='1';
         continue;
      }

      // Fix up bad characters
      if (browser_matches('ie'))
      {
         bad_word_chars=[8216,8217,8220,8221];
         fixed_word_chars=["'","'",'"','"'];
         for (i=0;i<bad_word_chars.length;i++)
         {
            regexp=new RegExp(String.fromCharCode(bad_word_chars[i]),'gm');
            theElement.value=theElement.value.replace(regexp,fixed_word_chars[i]);
         }
      }

      // Skip hidden fields
      var p=theElement;
      var visible=true;
      while (p && p.tagName.toLowerCase()!='body')
      {
         if (p.style.display.toLowerCase()=='none')
         {
            visible=false;
            break;
         }
         p=p.parentNode;
      }
      if (!visible)
         continue;

      theClass=firstClassName(theElement.className);

      if ((theElement.name=='delete') && (theClass=='input_tick') && (theElement.checked)) return confirmDelete(); // Because we're deleting, errors do not matter

      // Find whether field is required and value of it
      required=theClass.indexOf('_required');
      myValue=theElement.value;
      if ((window.areaedit_editors) && (theElement.style.display=='none') && (areaedit_editors[theElement.id]))
      {
         myValue=areaedit_editors[theElement.id].getHTML();
         if (myValue=="\n") myValue="";
      }
      if (theElement.getAttribute('type')=='radio')
      {
         myValue='';
         for (i=0;i<=theForm.elements.length;i++)
         {
            if (!theForm.elements[i]) continue;

            if ((theForm.elements[i].checked) && (theForm.elements[i].name==theElement.name))
               myValue=theForm.elements[i].value;
         }
      }

      if ((required!=-1) && ((myValue=='') || (myValue=='****')))
      {
         errorMsg="This is a required field and must therefore be filled in";
      } else
      {
         if (((theClass=='input_email') || (theClass=='input_email_required')) && (myValue!='') && (myValue!='****') && (!myValue.match(/^[a-zA-Z0-9\._\-]+@[a-zA-Z0-9\._\-]+$/)))
         {
            errorMsg="You specified a non-valid e-mail address (e-mail addresses contain '@')";
         }
         if (((theClass=='input_username') || (theClass=='input_username_required')) && (myValue!='') && (myValue!='****') && (window.do_ajax_field_test) && (!do_ajax_field_test('http://accessible-property.org.uk/data/username_exists.php?username='+encodeURIComponent(myValue))))
         {
            errorMsg="A field that was supposed to be a username specified a non-existent one";
         }
         if (((theClass=='input_integer') || (theClass=='input_integer_required')) && (myValue!='') && (myValue!='****') && (parseInt(myValue)!=myValue-0))
         {
            errorMsg="A field that was supposed to be an integer (whole number) was not";
         }
         if (((theClass=='input_float') || (theClass=='input_float_required')) && (myValue!='') && (myValue!='****') && (parseFloat(myValue)!=myValue-0))
         {
            errorMsg="A field that was supposed to be a float (decimal number) was not";
         }
      }

      setFieldError(theElement,errorMsg);
      if ((errorMsg!='') && (!erroneous))
      {
         theElement.focus();
         erroneous=true;
      }
   }

   if (((!window.is_opera) || (!is_opera())) && (!erroneous) && (!theForm.getAttribute('target')))
   {
      for (j=0;j<theForm.elements.length;j++)
      {
         if (theForm.elements[j].type=='submit')
         {
            addEventListenerAbstract(window,'pageshow',onpageshow,false);
            theForm.elements[j].setAttribute('disabled',true);
            theForm.elements[j].restoreOnShow=true;
         }
      }
   }

   if (erroneous)
   {
      window.scrollTo(0,0);
      window.alert("You did not fill in all fields on the form correctly");
   }

   return !erroneous;
}

// Do dynamic setLocked/setRequired such that one of these must be set, but only one may be
function standardAlternateFields(_a,_b,_c,non_actually_required)
{
   if (!non_actually_required) non_actually_required=false; // Just to make sure it's a nice boolean

   // Set up listening if not already...
   var a=_standardAlternateFieldsGet(_a);
   var b=_standardAlternateFieldsGet(_b);
   var c;
   if (_c) c=_standardAlternateFieldsGet(_c); // Third alternate is optional
   if (((a) && (!a.alternating)) || ((b) && (!b.alternating)) || ((c) && (!c.alternating))) // It is actually allowed for a single alternator, if circumstances have made the other one non-present (such as having GD support meaning a thumbnail isn't needed)
   {
      var selfFunction=function (e) { standardAlternateFields(_a,_b,_c,non_actually_required); } ; // We'll recall ourself to do any fiddling

      _standardAlternateFieldEventSet(a,selfFunction);
      _standardAlternateFieldEventSet(b,selfFunction);
      _standardAlternateFieldEventSet(c,selfFunction);
   }

   // Now, look at what is set, and disable/enable/require/non-require appropriately
   if ((a) && (a.value!='') && (a.value!='-1')) return _standardAlternateFieldsSet(a,b,c,non_actually_required);
   if ((b) && (b.value!='') && (b.value!='-1')) return _standardAlternateFieldsSet(b,a,c,non_actually_required);
   if ((c) && (c.value!='') && (c.value!='-1')) return _standardAlternateFieldsSet(c,a,b,non_actually_required);
   // Nothing set...
   if (a) _standardAlternateFieldSet(a,false,true,non_actually_required);
   if (b) _standardAlternateFieldSet(b,false,true,non_actually_required);
   if (c) _standardAlternateFieldSet(c,false,true,non_actually_required);
   return null;
}

function _standardAlternateFieldEventSet(a,selfFunction)
{
   if (a)
   {
      if (a.name)
      {
         a.onkeyup=selfFunction;
         a.onchange=selfFunction;
         a.alternating=true;
      } else
      {
         var i;
         for (i=0;i<a.length;i++)
         {
            a[i].onkeyup=selfFunction;
            a[i].onchange=selfFunction;
            a[i].alternating=true;
         }
      }
   }
   return null;
}

function _standardAlternateFieldsGet(x)
{
   if (x.indexOf('*')==-1)
   {
      return document.getElementById(x);
   }
   x=x.substr(0,x.length-1);
   var _x=Array(),i,j,c=0,e;
   _x['value']='';
   for (i=0;i<document.forms.length;i++)
   {
      for (j=0;j<document.forms[i].elements.length;j++)
      {
         e=document.forms[i].elements[j];
         if (e.name==x)
         {
            _x[c]=e;
            if (e.checked)
            {
               _x['value']=e.value;
            }
            if (e.alternating) _x.alternating=true;
            c++;
         }
      }
   }
   return _x;
}

function _standardAlternateFieldsSet(a,b,c,non_actually_required)
{
   if (a) _standardAlternateFieldSet(a,false,true,non_actually_required);
   if (b) _standardAlternateFieldSet(b,true,false,non_actually_required);
   if (c) _standardAlternateFieldSet(c,true,false,non_actually_required);
}

function _standardAlternateFieldSet(a,locked,required,non_actually_required)
{
   if (a.name)
   {
      setLocked(a.name,locked);
      if (!non_actually_required) setRequired(a.name,required);
   } else
   {
      if (a[0])
      {
         if (!non_actually_required) setRequired(a[0].name,required);
      }

      var i;
      for (i=0;i<a.length;i++)
      {
         if (a[i].id) // If it is an object, as opposed to some string in the collection
         {
            setLocked(a[i].id,locked);
            if (!non_actually_required) setRequired(a[i].id,required);
         }
      }
   }
}

function setLocked(name,locked)
{
   // For All-and-not,Line-multi,Compound-Tick,Radio-List,Date/Time: setLocked assumes that the calling code is clever
   // special input types are coded to observe their master input field readonly status)
   var element=document.getElementById(name);
   if (element)
   {
      if (locked)
      {
         setFieldError(element,'Blank out the competing field (or set to N/A) if you wish to use this one instead');
      } else
      {
         setFieldError(element,'');
      }
      element.disabled=locked;
   }
}

function setRequired(name,required)
{
   var element=document.getElementById(name);
   var required_a=document.getElementById('requirea__'+name);
   var required_b=document.getElementById('requireb__'+name);
   var required_c=document.getElementById('requirec__'+name);
   var required_d=document.getElementById('required__'+name);
   if (element) element.className=element.className.replace(/(input\_[a-z\_]+)_required/g,'$1');
   if (required)
   {
      if (element) element.className=element.className.replace(/(input\_[a-z\_]+)/g,'$1_required');
      if (required_a) required_a.className='de_th dottedborder_barrier_a_required';
      if (required_d) required_d.className='dottedborder_barrier_b_required';
      if (required_b) required_b.style.display='inline';
      if (required_c) required_c.value=1;
   } else
   {
      var error=document.getElementById('error__'+name);
      if (error) error.style.display='none';
      if (required_a) required_a.className='de_th dottedborder_barrier_a_nonrequired';
      if (required_d) required_d.className='dottedborder_barrier_b_nonrequired';
      if (required_b) required_b.style.display='none';
      if (required_c) required_c.value=0;
   }
   if (element_global) element_global.style.height='auto';
}


