// Encodes HTML FORM data for posting
// Returns the encoded form data ready for posting to
// a POST url.
// Additional parameters above and beyond those contained
// in the FORM will be added to the end of the POST data.
// Additional parameters must already be in encoded form-encode
// post form.

/*internal*/
function encodeFormData(docForm, additionalParams)
{
    var     result = '';
    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':
                result += formElem.name + '=' + encodeURIComponent(formElem.value) + '&';
                break;

            // Radio buttons
            case 'radio':
                if (formElem.checked) {
                    result += formElem.name + '=' + encodeURIComponent(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 (result.lastIndexOf('&') == result.length-1) {
                            result = result.substr(0, result.length - 1);
                        }
                        // Append value as comma-delimited string
                        result += ',' + encodeURIComponent(formElem.value);
                    }
                    else {
                        result += formElem.name + '=' + encodeURIComponent(formElem.value);
                    }
                    result += '&';
                    strLastElemName = formElem.name;
                }
                break;

        }
    }
    if (additionalParams != null) {
        result = result + additionalParams;
    } else {
        result = result.substr(0, result.length - 1);
    }
    return result;
}

//internal
// Creates a new XMLHttpRequest object for sending.
// Returns false if the object cannot be created
// either because the XMLHttpRequest object does not
// exist or the Microsoft XMLHTTP ActiveX object
// is not available.
function createHTTPObject()
{
    var result;

    try {
        result = new XMLHttpRequest();
    } catch (error) {
        try {
            result = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (error) {
            return false;
        }
    }
    return result;
}

//internal
// Gets an HTTP object to be used to issue a dynamic request
function getHTTPObject()
{
  return createHTTPObject();
}

// internal
// Called when the state of the http object changes.
// When the ready state is "ready" (4) then this will
// call the response handler to process the data.
//
// The response handler should ensure that the status
// is non-error (200) before processing the data.
function readyStateChanged(http,responseHandler,forced) {
  if (http.readyState == 4 || forced) {
    try {
      var params=responseHandler.split('|');
      var parami='';
      if (params.length>1)
        for (var z=1;z<params.length;z++)
          parami+=','+params[z];
			if (params[0]) 		
        eval(params[0] + '(http'+parami+');');
    } catch (error) {
		  try {
        PARENT.debugger_debug(http.readyState+' '+params[0] + '(http'+parami+')'+error);
			} catch(er) {
			}
      // do nothing, no status indicates request cancelled
    }
  }
}


//USE THIS
// Performs a GET on the given URL string with the method name given
// specified as the "womid" parameter.  When the response is ready this will
// call the responseHandler function with the http object and the
// key parameter given here.
// glo modz
// if you want to use doGet as a preloader you must set enableCache to true
// so uris are not changed (by this way the browser will retrieve objects
// having the same uri from its cache)
function doGet(urlString, responseHandler, async,enableCache) {
    var urlo=urlString;
    //ENCODE PARAMS
		if (!enableCache) {
      if (urlo.indexOf('?')!=-1) {
        urlo=urlo.replace(/\?(.*)/,'?');
        urlo=urlo+encodeURIComponent(RegExp.$1);
        urlo=urlo.replace(/%3D/g,'=');
        urlo=urlo.replace(/%26/g,'&');
      } else {
       urlo=urlo+'?';
      }
		}	
    urlString=urlo;
    //alert(urlString);
    var http = getHTTPObject();
    if (http) {
		  if (!enableCache) {
        //GREAT BUG FIX BY GATO
        //add time component so request is never cached
        urlString += '&' + ((new Date()).getTime());
			}	
      http.open("GET", urlString, async);
			if (async) {
        http.onreadystatechange = function() { readyStateChanged(http, responseHandler); };
        http.send(null);
			} else {
        http.send(null);
				if (responseHandler)
				  readyStateChanged(http,responseHandler,1);
				else {
				  return http.responseText;
				}		
			}
    }
}
//internal
// POSTs to the given URL string with the given POST data in submitString.  The
// methodName is encoded as the form paraemter "womid" to communicate with the server
// which method is to execute.
//
// The key parameter will be passed to the responseHandler function.
//
// If the async flag is true the request will run asynchronously, else it will
// wait for a response before returning.
function doPostForm(urlString,submitString,responseHandler,async) {
  var http = getHTTPObject();
  //another encode by gato
    try { 
    var urlo=urlString;
    //ENCODE PARAMS
    if (urlo.indexOf('?')!=-1) {
      urlo=urlo.replace(/\?(.*)/,'?');
      urlo=urlo+encodeURIComponent(RegExp.$1);
      urlo=urlo.replace(/%3D/g,'=');
      urlo=urlo.replace(/%26/g,'&');
    } else {
     urlo=urlo+'?';
    }
    urlString=urlo;
    } catch(er) {
    }
  
  if (http) {
    //GREAT BUG FIX BY GATO
    //add time component so request is never cached
    urlString += '&' + ((new Date()).getTime());
 
    http.open("POST", urlString, async);
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		if (async) {
      if (responseHandler != null) {
        try {
          http.onreadystatechange = function() {
            try {
              readyStateChanged(http, responseHandler); //call so user defined func is called with http response as a parameter
            } catch(er) {
            }
          };
        } catch(er) {
        } 
      }
      http.send(submitString);
		} else {
      http.send(submitString);
			if (responseHandler) {
        try {
          readyStateChanged(http,responseHandler,1);
        } catch(er) {
        }
      }   
		}	
  }
  return http;
}

//internal
// Posts to a FORM URL with the given action, using the encoded values of the
// form given and methodName as POST parameters.
//
// The response handler given will be called when the response is ready for
// processing.
function doPostFormWithAction(action,aForm,responseHandler,async) {
  return doPostForm(action,encodeFormData(aForm, null),responseHandler,async);
}


//USE THIS
// Uses doPostFormWithAction using specified action or the form's action attribute.
// is action param is null.
// The response handler given will be called when the response is ready for
// processing.
function doPost(action,aForm,responseHandler,async) {
  if (!action)
    action=aForm.action
  return doPostFormWithAction(action,aForm,responseHandler,async);
}

//Flag indicating we have W3CDOM available to us as a minimum requirement
var   hasW3CDOM = (document.createElement && document.getElementsByTagName);

// Flag indicates whether AJAX capabilities are supported.
var   hasAJAX = hasW3CDOM && getHTTPObject();