// file:        xmlhttp.js
// date:        2008-04-24
// notes:       most of these functions deal with the domain of origin workaround
//              and are (at the time of dev) tied into the inappropriately 
//              name test2-iframe_ffd2.html production DOO file
//
// name:        getXmlHttp
// purpose:     creates a xmlHttpRequest object in a cross-browser environment
// returns:     a new xmlHttpRequest object appropriate for the browser
// params:      none
// dev:         mcd, obtained from fetig.net who adapted from 
//              http://jibbering.com/2002/4/httprequest.html
// date:        2008-04-24
// notes:       I think this code is ok, but the ffd main web site
//              /myjs/xmlextras.js may have better code for this purpose
//
function getXmlHttp(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

// name:        getUrl
// purpose:     HTTP GET via XMLHttpRequest to a url 
// result:      returns all responseText, etc. to the callback function mapped
// params:      
//              url:            the url, must be to a subdomain and/or port of the parent window
//              cb:             the callback function usually "gotResult" in 
//                              test2-iframe_ffd2.html file (which maps the gotXml function
//                              in the parent window)
// date:        2008-06-25
// dev:         mcd
// notes:       see fettig.net for more info on this domain of origin workaround
//
function getUrl(url, cb) {
    if (url.length==0){
        url = AJAX_URL;
    }
    var xmlhttp = getXmlHttp();    
    xmlhttp.open("GET", url);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            //also: todo: need to wrap status property in catch due to a bug accessing status if server is down
            //see http://developer.mozilla.org/en/docs/AJAX:Getting_Started
            if(xmlhttp.status == 200) {
                cb(xmlhttp.status, xmlhttp.getAllResponseHeaders(), xmlhttp.responseText);
            }
        }
    }
    xmlhttp.send(null);
}

// name:        getUrlPost
// purpose:     post via XMLHttpRequest to a url with option to include 
//              SOAP headers to a subdomain-port of the parent window
// result:      returns all responseText, etc. to the callback function mapped
// params:      
//              url:            the url, must be to a subdomain and/or port of the parent window
//              cb:             the callback function usually "gotResultSoap" in 
//                              test2-iframe_ffd2.html file (which maps the gotXml function
//                              in the parent window)
//              thenamespace:   the SOAPAction header namespace
//              method:         the SOAPAction header action (mapped method)
//              thesoap:        valid SOAP xml string
// date:        2008-06-25
// dev:         mcd
// notes:       see fettig.net for more info on this domain of origin workaround
//
function getUrlPost(url, cb, thenamespace, method, thesoap) {
    var xmlhttp = getXmlHttp();
    xmlhttp.open("POST", url);
    var soapaction = thenamespace + "/" + method;
    xmlhttp.setRequestHeader("SOAPAction", soapaction);
    xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            //2008-06-09: tcm change responseXML to responseText for mozilla compatibility
            //if mimetype isnt xml or mimetype doesnt parse then responseXML doesnt work in moz
            //also: todo: need to wrap status property in catch due to a bug accessing status if server is down
            //see http://developer.mozilla.org/en/docs/AJAX:Getting_Started
            if(xmlhttp.status == 200) {
                cb(xmlhttp.status, xmlhttp.getAllResponseHeaders(), xmlhttp.responseText);
            }
        }
    }
    xmlhttp.send(thesoap);
}