/*
---------------------------------------------------------------------------

 * 	This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2, or (at your option)
 *      any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program (see the file COPYING); if not, write to the
 *      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *      Boston, MA  02111-1307, USA

 	This code is copyright 2003 by Matthew Eernisse

---------------------------------------------------------------------------
*/

// The var docForm should be a reference to a <form>

function formData2QueryString(docForm) {

	var strSubmitContent = '';
	var formElem;
	var strLastElemName = '';

	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':
				strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
				break;

			// Radio buttons
			case 'radio':
				if (formElem.checked) {
					strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
				}
				break;

			// Checkboxes
			case 'checkbox':
				if (formElem.checked) {
					// Continuing multiple, same-name checkboxes
					if (formElem.name == strLastElemName) {
						// Strip of end ampersand if there is one
						if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
							strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
						}
						// Append value as comma-delimited string
						strSubmitContent += ',' + escape(formElem.value);
					}
					else {
						strSubmitContent += formElem.name + '=' + escape(formElem.value);
					}
					strSubmitContent += '&';
				}
				break;

		}
		strLastElemName = formElem.name
	}

	// Remove trailing separator
	strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
	return strSubmitContent;
}


function xmlhttpPost(strURL, strSubmit, strResultFunc){
var xmlHttpReq = false;

  // Mozilla / Safari
  if(window.XMLHttpRequest){
    xmlHttpReq = new XMLHttpRequest();
    //xmlHttpReq.overrideMimeType('text/xml');
  }

// IE
  else if(window.ActiveXObject){
    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  }

  xmlHttpReq.open('POST', strURL, true);
  xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttpReq.onreadystatechange = function(){
    if(xmlHttpReq.readyState == 4){
      strResponse = xmlHttpReq.responseText;
      switch(xmlHttpReq.status){
        // Page-not-found error
        case 404:
          alert('Error: Not found. The Request URL ' + strURL + ' could not be found.');
          break;
        case 500:
          handleErrFullPage(strResponse);
          break;
        default:
          if(strResponse.indexOf('Error:') > -1 || strResponse.indexOf('Debug:') > -1){
            alert(strResponse);
          } else {
            eval(strResultFunc + '(xmlHttpReq.responseText);');
          }
          break;
      }
  }
  }
  xmlHttpReq.send(strSubmit);
}

function handleErrFullPage(strIn){
  var errorWin;

  try{
    errorWin = window.open('','errorWin');
    errorWin.document.body.innerHTML = strIn;
  }
  catch(e){
    alert('An error occurred, but the error message cannot be displayed because of your browser\'s pop-up blocker.');
  }
}