function addClassToSubmitWhenClicked(e) {
 var el;
 if (!e) var e = window.event;
 if (e.target) el = e.target; else if (e.srcElement) el = e.srcElement;
 if (el.nodeType == 3) el = el.parentNode;
 el.savestate=el.disabled;
 el.isclicked=true;
}




function riegelSerialize(a) {
 var getEls=Form.getElements(a);
 for (i=0; i < getEls.length; i++) {
  getEls[i].savetate=getEls[i].disabled;
  if (getEls[i].type=='submit' && getEls[i].isclicked == false) {
   getEls[i].disabled=true;
  }
  if (getEls[i].type=='submit' && getEls[i].isclicked ) {
   window.status=getEls[i].value+' in process...';
   window.waiting=window.status;
  }
  if (getEls[i].type=='button') {
   getEls[i].disabled=true;
  }
 }
 var getserial=Form.serialize(a);
 for (i=0; i < getEls.length; i++) {
  getEls[i].disabled=getEls[i].savestate;
  getEls[i].isclicked=false;
 }
 return getserial;
}





function ajaxForms(c) {
 var inputArray =  document.getElementsByTagName('input');
 for (i=0; i < inputArray.length; i++) {
  if (inputArray[i].type == "submit") {
   inputArray[i].isclicked=false;
   inputArray[i].onclick=addClassToSubmitWhenClicked;
  }
 }
 var forms= document.getElementsByTagName('form');
 for (i=0; i<forms.length; i++)
 {
  if (forms[i].className.indexOf(c) != -1)
  {
   forms[i].onsubmit=function (e)
   {
    // First we need to identify the target div. That will get stored in el
    if (this.saveaction && this.action.substr(0,6) == "status") {this.action=this.saveaction;}
    var el;
    if (!e) var e = window.event;
    if (e.target) el = e.target; else if (e.srcElement) el = e.srcElement;
    if (el.nodeType == 3) el = el.parentNode;
    return runForm(el,el.action,el.method,'ajaxrequest=TRUE&'+riegelSerialize(el));
   }
  }
 }
}





function ajaxAnchors(c) {
 var overlayArray =  document.getElementsByTagName('a');
 for (i=0; i < overlayArray.length; i++)
 {
  if (overlayArray[i].className.indexOf(c) != -1)
  {
   overlayArray[i].onclick=function(e)
   {
    var el;
    if (!e) var e = window.event;
    if (e.target) el = e.target; else if (e.srcElement) el = e.srcElement;
    if (el.nodeType == 3) el = el.parentNode;
    if (el.savehref && el.href.substr(0,6) == "status") {el.href=el.savehref;}
    doAJAX(this,this.href,'GET','ajaxrequest=TRUE');
    return false;
   }
  }
 }
}





/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/
AIM = {
 frame : function(f,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(\''+f.id+'\',\''+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) {
  // Added By Terry Riegel
  document.getElementsByName('ajaxrequest')[0].value="IFRAME";
  AIM.form(f, AIM.frame(f,c));
  if (c && typeof(c.onStart) == 'function') {
   return c.onStart();
  } else {
   return true;
  }
 },
 loaded : function(f,id) {
  var i = document.getElementById(id);
  if (typeof i.contentDocument != "undefined" && i.contentDocument) {
   var d = i.contentDocument;
  } else if (typeof i.contentWindow != "undefined" && i.contentWindow) {
   var d = i.contentWindow.document;
  } else {
   var d = window.frames[id].document;
  }
  if (typeof d != "undefined" && d.location.href == "about:blank") {
   return;
  }
  var q='NOCONTENT';
  try {
   var x=d.body.getElementsByTagName("pre");
   if (x.length == 1) {q=x[0].innerHTML.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&");} else {q=d.body.innerHTML;}
   eval(q);
  } catch (error){}
  var tf=$(f);
  if(typeof tf.processing == "function") {
   tf.processing(false);
  } else {
   if(typeof processing == "function") {processing(false);}
  }
  if (window.waiting == window.status) {window.status='Done.'; window.waiting=false;}
  logit('ajaxForms',q);
  returnErrorCheck(q);
  clearImage="undefined";
  if (document.getElementById(id)) {
   setTimeout(function(){document.getElementById(id).parentNode.removeChild(document.getElementById(id));},15000);
  }
 }
}





function doAJAX(element,action,method,params){
 //-----------------------------------------------------------
 // alert(element+'|'+action+'|'+method+'|'+params);
 //-----------------------------------------------------------
     new Ajax.Request(
      action,
      {
       method: method,
       parameters: params,
       encoding: 'UTF-8',
       onCreate: function(){
        if(typeof element.processing == "function") {
         element.processing(true);
        } else {
         if(typeof processing == "function") {processing(true);}
        }
       },
       onException: function(){
        if(typeof element.processing == "function") {
         element.processing(false);
        } else {
         if(typeof processing == "function") {processing(false);}
        }
       },
       onFailure: function(){
        if(typeof element.processing == "function") {
         element.processing(false);
        } else {
         if(typeof processing == "function") {processing(false);}
        }
       },
       onComplete: function(t){
        if(typeof element.processing == "function") {
         element.processing(false);
        } else {
         if(typeof processing == "function") {processing(false);}
        }
        if (window.waiting == window.status) {window.status='Done.'; window.waiting=false;}
        logit('ajaxForms('+c+')',t.responseText);
        returnErrorCheck(t.responseText);
        clearImage="undefined";
       }
      }
     );
 //-----------------------------------------------------------
}





function runForm(el){
 el=$(el);
 if (el){
    if (el.saveaction && el.action.substr(0,6) == "status") {el.action=el.saveaction;}
    // Ok we first scan the form to see if it has any file elements. If so then we need an alternate plan
    var getEls=Form.getElements(el);
    var hasfilefield=false;
    for (i=0; i < getEls.length; i++) {
      if (getEls[i].type=='file') {
       if (getEls[i].value != ""){
        hasfilefield=true;
       }
      }
    }
    if(typeof useAIM == "undefined") useAIM=false;
    if (hasfilefield || useAIM){
     if(typeof el.processing == "function") {
      el.processing(true);
     } else {
      if(typeof processing == "function") {processing(true);}
     }
     if (typeof document.getElementsByName('ajaxrequest')[0] == 'undefined'){
      var newdiv = document.createElement("input");
      newdiv.setAttribute('type','hidden');
      newdiv.setAttribute('name','ajaxrequest');
      newdiv.setAttribute('value','error');
      el.insertBefore(newdiv,el.firstChild);
     }
     AIM.submit(el);
     return true;
    } else {
     doAJAX(el,el.action,el.method,'ajaxrequest=TRUE&'+riegelSerialize(el));
     return false;
    }
 }
}





function runOverlay(url,id){
 id=$(id);
 doAJAX(id,url,'GET','ajaxrequest=TRUE');
 return false;
}





function runAnchor(id){
 id=$(id);
 doAJAX(id,id.href,'GET','ajaxrequest=TRUE');
 return false;
}





function returnErrorCheck(t){
 if (t=="NOCONTENT"){
  report('error','IFRAME ERROR');
  return true;
 } else {
  if(typeof clearImage == "undefined" || clearImage == "undefined") {
   if (t.substr(0,10)=='clearImage') t='<PRE>'+t.replace(/\n/g,'<br>')+'</PRE>';
   report('error','ajaxForms() - Returned malformed response');
   document.body.saveHTML='<a href="javascript:document.body.innerHTML=document.body.errorHTML;" style="position: absolute; top: 0; left: 0; background-color: white; color: red; padding: 2px;">ERROR</a>'+document.body.innerHTML;
   document.body.errorHTML='<div style="margin-bottom: 15px; background-color: #eeeeee; padding:5px; border:1px solid white;"><a href="javascript:document.body.innerHTML=document.body.saveHTML;" style="position: absolute; top: 0; left: 0; background-color: white; color: red; padding: 2px;">BACK</a><h3 style="color: red;">ERROR: overlay returned HTML or invalid javascript.</h3><a href="javascript:window.location.reload();">RELOAD</a></div><div style="padding:5px; border:1px solid red; background-color: #cccccc;">'+t+'</div>';
   document.body.innerHTML=document.body.errorHTML;
   return true;
  } else if(clearImage == "START") {
   report('error','Javascript ERROR in \n'+t);
   return true;
  }
 }
 return false;
}





function logit(f,t) {
 if (typeof debug == "undefined" || debug != true) {} else {report('info',f); report('log',t);}
}





function report(t,m) {
 if (typeof console != "undefined") {
  if (t.toLowerCase() == 'error') console.error(m);
  if (t.toLowerCase() == 'info')  console.info(m);
  if (t.toLowerCase() == 'log')   console.log(m);
 } else {
  if (t.toLowerCase() == 'error') window.status='ERROR: '+m;
 }
}





function setDIV(a,b) {
 return function(q){
  if (q) {
   if (typeof $(a).saveHTML == "undefined" || $(a).saveHTML == "ERROR" ){
    $(a).saveHTML=$(a).innerHTML;
    $(a).innerHTML=b;
   }
  } else {
   if ($(a).saveHTML != "ERROR" ){
    $(a).innerHTML=$(a).saveHTML;
    $(a).saveHTML='ERROR';
   }
  }
 };
}