
   // Update Form Status
   // Check for changed form variables
   function formValueChanged(formID, statusID, formKey) {
      var form = document.getElementById( formID );
      var formValues = processFormData(form, '&');
      var keyValuePairs = formValues.split('&');
      var historical_keys = new Array();
      var historical_values = new Array();
      var field_keys = new Array();
      var field_values = new Array();

      var f_count = 0;
      var h_count = 0;
      for(i=0;i<keyValuePairs.length;i++) {
         var field_data = keyValuePairs[i].split('=');
         if(field_data[0].match(/^historical__/)) {
            // historical value
            historical_keys[h_count] = field_data[0];
            historical_values[h_count] = field_data[1];
            h_count++;
         }
         if(field_data[0].match(/^field__/)) {
            // field value
            field_keys[f_count] = field_data[0];
            field_values[f_count] = field_data[1];
            f_count++;
         }
      }
      var statusFlag = false;
      var status_m = document.getElementById(statusID);
      for(i=0;i<field_keys.length;i++) {
         var field_data = field_keys[i].split('__');
         var hKey = 'historical__' + field_data[1];
         // Get index of historical field
         for(j=0;j<historical_keys.length;j++) {
            if(historical_keys[j] == hKey) {
               var index = j;
            }
         }

         if(field_values[i] == historical_values[index]) {
            // Check class set to normal if 'changed_value'
            var item = document.getElementById(field_keys[i] + '__' + formKey);
            if(item) {
               if(item.type != "hidden") {
                  item.style.color = "#000000";
                  item.style.border = "1px solid #999999";
               }
            }
         }
         else {
            // Set class to 'changed_value'
            var item = document.getElementById(field_keys[i] + '__' + formKey);
            if(item) {
               if(item.type != "hidden") {
                  item.style.color = "#6666ff";
                  item.style.border = "1px dotted #4444ff";
               }
               statusFlag = true;
            }
         }
      }
      if(statusFlag) {
         status_m.innerHTML = 'This form has unsaved changes.';
         status_m.style.border = '1px solid red';
      }
      else {
         status_m.innerHTML = '&nbsp;&nbsp;';
         status_m.style.border = '1px solid white';
      }
   }


   /*
    * Copyright 2005 Matthew Eernisse (mde@fleegix.org)
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    * 
    *   http://www.apache.org/licenses/LICENSE-2.0
    * 
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    *
    * Original code by Matthew Eernisse (mde@fleegix.org)
    * Additional bugfixes by Mark Pruett (mark.pruett@comcast.net)
    *
   */

   // The var docForm should be a reference to a <form>

   function processFormData(docForm, delimiter) {
     var submitContent = '';
     var formElem;
     var lastElemName = '';

     for (i = 0; i < docForm.elements.length; i++) {
       formElem = docForm.elements[i];
       switch (formElem.type) {
         // Text fields, hidden form elements
         case 'text':
         case 'hidden':
         case 'password':
         case 'textarea':
         case 'select-one':
           submitContent += formElem.name + '=' + encodeURIComponent(formElem.value) + delimiter
           break;

         // Radio buttons
         case 'radio':
           if (formElem.checked) {
             submitContent += formElem.name + '=' + escape(formElem.value) +  delimiter
           }
           break;

         // Checkboxes
         case 'checkbox':
           if (formElem.checked) {
             // Continuing multiple, same-name checkboxes
             if (formElem.name == lastElemName) {
               // Strip of end ampersand if there is one
               if (submitContent.lastIndexOf(delimiter) == submitContent.length-1) {
                 submitContent = submitContent.substr(0, submitContent.length - 1);
               }
               // Append value as comma-delimited string
               submitContent += ',' + escape(formElem.value);
             }
             else {
               submitContent += formElem.name + '=' + escape(formElem.value);
             }
             submitContent += '&';
             lastElemName = formElem.name;
           }
           break;

       }
     }
     // Remove trailing separator
     submitContent = submitContent.substr(0, submitContent.length - 1);
     return submitContent;
   }




/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/

AIM = {

   frame : function(c) {

      var n = 'f' + Math.floor(Math.random() * 99999);
      var d = document.createElement('DIV');
      d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
      document.body.appendChild(d);

      var i = document.getElementById(n);
      if (c && typeof(c.onComplete) == 'function') {
         i.onComplete = c.onComplete;
      }

      return n;
   },

   form : function(f, name) {
      f.setAttribute('target', name);
   },

   submit : function(f, c) {
      AIM.form(f, AIM.frame(c));
      if (c && typeof(c.onStart) == 'function') {
         return c.onStart();
      } else {
         return true;
      }
   },

   loaded : function(id) {
      var i = document.getElementById(id);
      if (i.contentDocument) {
         var d = i.contentDocument;
      } else if (i.contentWindow) {
         var d = i.contentWindow.document;
      } else {
         var d = window.frames[id].document;
      }
      if (d.location.href == "about:blank") {
         return;
      }

      if (typeof(i.onComplete) == 'function') {
         i.onComplete(d.body.innerHTML);
      }
   }
}

function startCallback() {
   // make something useful before submit (onStart)
   return true;
}

function completeCallback(response) {
   // make something useful after (onComplete)
   document.getElementById('div_upload_result').innerHTML = response;
   $('upload_form').reset();
}

function notesCompleteCallback(response) {
   // make something useful after (onComplete)
   document.getElementById('div_notes').innerHTML = response;
   $('notes_form').reset();
}

function urlCompleteCallback(response) {
   // make something useful after (onComplete)
   document.getElementById('div_url').innerHTML = response;
   $('url_form').reset();
}

function confirmAction() {
   if(confirm('Are you sure you want to delete this item?')) {
      return true;
   }
   else {
      return false;
   }
}

