// A status indicator div that can contain information as to whether
// information is being loaded or saved.
var statusIndicator=document.createElement('div');
statusIndicator.id="statusIndicator";

var needSave = true;
function saveASPAJAX(formName){
   if(needSave){
      var form = gI(formName);
      var xmlReq = getXmlHttpRequestObject();
      var data = '';
      var eName = '';
      var type = '';
      for( var i=0; i< form.length; i++)
      {
         eName = form.elements[i].name;
         type = form.elements[i].type;

         if( eName && eName != '' && eName != '__EVENTTARGET' && eName != '__EVENTARGUMENT' && type != 'submit' && type != 'clear' && type != 'button' && ((type != 'checkbox' && type != 'radio') || form.elements[i].checked) )
         {
            data = data + escape(eName) + '=' + escape(form.elements[i].value).replace(new RegExp('\\+', 'g'), '%2b');
            if( i != form.elements.length - 1 ) data = data + '&';
         }
      }
      xmlReq.open("POST", form.action);
      xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      xmlReq.send("doAjaxSave=true&" + data);
   }
}
function encodeHTML(html) {
   encodedHtml=escape(html);
   return encodedHtml;
}
/**
 * In Internet Explorer, to create the object using new ActiveXObject("Msxml2.XMLHTTP") 
 * or new ActiveXObject("Microsoft.XMLHTTP")
 * depending on the version of MSXML installed. 
 * In Mozilla and Safari, you use new XMLHttpRequest()
 */
function getXmlHttpRequestObject() {
   if(window.XMLHttpRequest) {
      return new XMLHttpRequest();
   } else if(window.ActiveXObject) {
      return new ActiveXObject("Microsoft.XMLHTTP")
   } else {
      alert("can't make xmlhttprequest");
   }
}
/**
 * xmlData: xml data
 *   <?xml version="1.0" encoding="UTF-8"?>
 *   <request>
 *      <command>save|load</command> 
 *      <SID>SecureID</SID> 
 *      <CID>contractIdentifier</CID>
 *      <saveName>name for this save</saveName>
 *      <section>what sort of info</section>
 *      <other>additional metadata</other>
 *      <html (or data)>
 *         Data to save (make sure it is escaped: you can use encodeHTML())
 *      </html (or data)>
 *   </request>
 * 
 * destinationURL: where to post data to.
 * 
 * statusIndicatorParent: parent of where you want the status indicator to go.
 *   It will be added using insertBefore() and you should dispose of it.
 *
 * statusText: what text you want in the status indicator.
 *
 * callbackFunc: what function needs to be called when the request is
 *   answered. The parameters are the request object followed by the status
 *   indicator object.
 *
 *   If passed in as null (false), it will initiate a synchronous httprequest
 */
function httpRequest(xmlData,destinationURL,statusIndicatorParent,statusText,callbackFunc,usePost) {
   if(SID!=false) {
      statusIndicator.innerHTML=statusText;
      if(statusIndicatorParent)
         statusIndicatorParent.insertBefore(statusIndicator,statusIndicatorParent.firstChild);
      var req = getXmlHttpRequestObject();
      // only required if last open param is missing or true
      if(callbackFunc!=false)
         req.onreadystatechange = function() { httpRequestCallback(req,callbackFunc);}
      req.open("POST",destinationURL,callbackFunc!=false);
      //POST variables to php code only
      if(typeof(usePost)=='undefined' || usePost) {req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');}
      req.send(xmlData);
   } else {
      alert("There was a problem reading your user ID. Please go to our contact page and notify technical support.");
   }
}
function checkSaveBlank() {}
function httpRequestCallback(req,callbackFunc) {
   if(req && req.readyState==4) {
      if(req && req.status==200) {
         callbackFunc(req,statusIndicator,true);
      } else {
	//alert("There was a problem transfering information ("+req.status+": "+req.statusText+")");
         callbackFunc(req,statusIndicator,false);
      }
   }
}
function str2XML(str) {
   var xml;
   if(window.ActiveXObject) {
      xml = new ActiveXObject("MSXML2.DOMDocument");
      xml.loadXML(str);
      xml.setProperty("SelectionLanguage","XPath");
   } else {
      xml = (new DOMParser).parseFromString(str, "text/xml");
   }
   return xml;
}
function FormToString(formID){
   str = ''

   var myForm = getForm(formID);
   var el = myForm.elements;
   
   for (var elems=0; elems < myForm.length;elems++) {
      if (el[elems].name != ""){
         str += escape(el[elems].name) + '=';
         if ( el[elems].type == "checkbox") {
            str += el[elems].checked;
         }
         else if( el[elems].type == "radio") {
            var radiogroup = el[el[elems].name];
            for (var j=0; j<radiogroup.length; ++j) {
               if(radiogroup[j].checked) {
                 str += radiogroup[j].value;
               }
            }
            elems = elems + radiogroup.length - 1;
         } else {
             str += escape(el[elems].value);
         }
         str += '&';
      }
   }
   return str;
}
