function FlashXmlHttpRequest() {};

FlashXmlHttpRequest.prototype.init = function() {
	this.flashLoaded = false;
	this.documentLoaded = true;
};
FlashXmlHttpRequest.prototype.isFlashInstalled = function() {
	var ret;

	if (typeof(this.isFlashInstalledMemo) != "undefined") { return this.isFlashInstalledMemo; }

	if (typeof(ActiveXObject) != "undefined") 
	{
		try {
			var ieObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
		} catch (e) { }
		ret = (ieObj != null);
	} else {
		var plugin = navigator.mimeTypes["application/x-shockwave-flash"];
		ret = (plugin != null) && (plugin.enabledPlugin != null);
	}
	this.isFlashInstalledMemo = ret;
	return ret;
};

FlashXmlHttpRequest.prototype.getFlash = function() {
	return document.getElementById("storage");
};

FlashXmlHttpRequest.prototype.checkFlash = function() {
  try {
     return (this.getFlash().ping() == "pong");
  }
  catch (e) { return false; }
};

FlashXmlHttpRequest.prototype.load = function() {
  if (typeof(this.onload) != "function") { return; } 

  if (this.isFlashInstalled()) {
    var finishedLoading = this.flashLoaded && this.documentLoaded;
    if (!finishedLoading) { return; }
  }
  
  var fs = this.getFlash();
  if ((!this.isFlashInstalled() || this.flashLoaded) && fs) {
    if (this.checkFlash()) {
      callAppOnLoad(fs);
    } else {
			callAppOnLoad(null);
    }
  } else {
     callAppOnLoad(null);
  }
  
  function callAppOnLoad(fs) {
    if (this.onloadCalled) { return; }
    this.onloadCalled = true;
    this.onload(fs);
  }
};

FlashXmlHttpRequest.prototype.writeFlash = function(swfName) { 
	document.write('<div id="divStorage" style="POSITION:absolute;top:-2000px;height:0px;">');
  if (window.ActiveXObject && !this.isFlashInstalled())
  {
    document.write('<object id="storage" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
    document.write(' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,5,0,0"');
    document.write(' height="' + this.height + '" width="' + this.width + '">');
    document.write(' <param name="movie" value="' + swfName + '">');
    document.write(' <param name="quality" value="high">');
    document.write(' <param name="swliveconnect" value="true">');
    document.write('<\/object>');
  }
  else
  {
    document.write('<object id="storage" data="' + swfName + '"');
    document.write(' type="application/x-shockwave-flash" height="' + this.height + '" width="' + this.width + '">');
    document.write('<param name="movie" value="' + swfName + '">');
    document.write('<param name="quality" value="high">');
    document.write('<param name="swliveconnect" value="true">');
    document.write('<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer">');
    document.write('<param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer">');
    document.write('<p>You need Flash for this. Get the latest version from');
    document.write(' <a href="http://www.macromedia.com/software/flashplayer/">here<\/a>.');
    document.write('<\/p>');
    document.write('<\/object>'); 
  }
  document.write('</div>');
};

FlashXmlHttpRequest.prototype.createHeader = function(soapAction) 
{
	var headers = new Array();
	headers.push("Cache-Control");
	headers.push("public, no-cache, max-age=0");
	headers.push("Pragma");
	headers.push("public, no-cache");
	headers.push("expires");
	headers.push("0");
	headers.push("SOAPAction");
	headers.push(soapAction);
	return headers;
};
FlashXmlHttpRequest.prototype.createSoap = function(xmlnamespace, methodName, names, values) 
{
	var strEnvelope = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + 
	"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
	"<soap:Body><" + methodName + " xmlns=\"" + xmlnamespace + "\">";
	
	if (names !=null && values !=null) 
	{
		for (i = 0; i < names.length; i++)
		    strEnvelope += "<" + names[i] + ">" + values[i] + "</" + names[i] + ">";
	}

	return strEnvelope + "</" + methodName + "></soap:Body></soap:Envelope>";
};

FlashXmlHttpRequest.prototype.send = function(handler, url, soapAction, soapMessage) 
{
  var fs = this.getFlash();
  function callback(varName) {
    var response = fs.GetVariable(varName);
    handler.responseText = response;
    if (typeof(handler.responseLoad) == 'function') {
        handler.responseLoad();
    }
  }
  fs.XmlHttp(url, callbackManager.registerCallback(callback), soapMessage, this.createHeader(soapAction), "text/xml; charset=utf-8");
};

FlashXmlHttpRequest.prototype.createDocument = function(message) {
   var xmlDocument;
   var xmlParser;
   if(window.ActiveXObject) {
	  xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
	  xmlDocument.async = false;
	  xmlDocument.loadXML(message);
   } else if (window.XMLHttpRequest) {
	  xmlParser = new DOMParser();
	  xmlDocument = xmlParser.parseFromString(message, 'text/xml');
   } else {
	  return null;
   }
   return xmlDocument;	
};

FlashXmlHttpRequest.prototype.getNode = function(xml, node) {
    if(xml.getElementsByTagName(node).length == 0)
        return "";
	return (xml.getElementsByTagName(node)[0].firstChild ==null) ? "" : xml.getElementsByTagName(node)[0].firstChild.nodeValue;
}
FlashXmlHttpRequest.prototype.getcookie = function (name)
{
    var nameOfCookie = name + "=";
    var x = 0;
    while ( x <= document.cookie.length )
    {
        var y = (x+nameOfCookie.length);
        if ( document.cookie.substring( x, y ) == nameOfCookie ) 
        {
            if ( (endOfCookie=document.cookie.indexOf( ";", y )) == -1 )
                endOfCookie = document.cookie.length;
            return unescape( document.cookie.substring( y, endOfCookie ) );
        }
        x = document.cookie.indexOf( " ", x ) + 1;
        if ( x == 0 )
            break;
    }
    return "";
}

FlashXmlHttpRequest.prototype.setcookie = function (name, value, domain){
	document.cookie = name + "=" + escape(value) + ";domain=" + domain + ";path=/;";
}

FlashXmlHttpRequest.prototype.removecookie = function (name, domain){
	document.cookie = name + "=;domain=" + domain + ";path=/;expires=0";
}
FlashXmlHttpRequest.prototype.isfunction = function(functionObj, parameterName) 
{
	if(functionObj ==null || typeof(functionObj) != 'function')
	{
		alert(parameterName + " - Please specify the script function");
		return false;
	}
	else
		return true;
};
var callbackManager = new Object();
callbackManager.callbacks = new Array();
callbackManager.registerCallback = function(callback) {
    var length = this.callbacks.push(selfDeleteCallback);
    var callbackID = length - 1;
    
    return "callbackManager.callbacks[" + callbackID + "]";
    
    function selfDeleteCallback(obj) {
        delete callbackManager.callbacks[callbackID];
        setTimeout(function() { callback(obj); }, 0);
        return;
    } 
};

var xmlHttpRequest = new FlashXmlHttpRequest();
xmlHttpRequest.init();
xmlHttpRequest.load();
xmlHttpRequest.writeFlash("/HanbitSoftXHR.swf")
  
function storageOnLoad() { 
  xmlHttpRequest.flashLoaded = true;
  xmlHttpRequest.load();
};

function storageOnError() {
  alert("storageOnError"); 
  xmlHttpRequest.flashLoaded = true;
  xmlHttpRequest.load();
};
