function dspSend_Open(oSetting, sWPQ, sDefXml) {
	var sContent = dspSend_BuildSendContent(sWPQ, sDefXml, oSetting.Caption, oSetting.TableStyleSheet);
	if (sContent != "" && dspApp_LoadApplication(oSetting))
		oDSP_App.SendWebPart(sContent);
}

function dspSend_BuildSendContent(sWPQ, sDefXml, sCaption, sTableStyleSheet) {
	var oTitleTag = document.all("WebPartTitle" + sWPQ); 
	var oHTMLTag = document.all("oDSP" + sWPQ + "divWebPart");
	var oXMLTag = document.all("oDSP"+ sWPQ + "Xml");
	var oScriptTag = document.all("oDSP"+ sWPQ + "Script");
	var oStyleSource = document.all("oDSP" + sWPQ + "StyleSource");
	var sEmbeddedScripts = dspSend_OnLoadScript(sWPQ) + dspSend_GetScripts(oScriptTag);
	var sLinkedScripts = dspSend_GetLinkedScripts(sDefXml);
	var sBody = "<div title='Click to launch Analysis Client' class='ms-WPTitle'><a href='javascript:fDSP" + sWPQ + "OpenWP()'>" + 
		sCaption + "</a></div>" + 
		"<div>" + oHTMLTag.outerHTML + "</div>";
	var sHTML = "<html xmlns:v=\"urn:schemas-microsoft-com:vml\"><title>" + sCaption + "</title><head>" + 
		dspSend_GetStyleSheet(oDSP_Style.href, oStyleSource, sDefXml, sTableStyleSheet) + 
		oXMLTag.outerHTML + sLinkedScripts + sEmbeddedScripts + 
		"<xml id='xmldef'>" + sDefXml + "</xml>" + 
		"</head><body onload='fDSP" + sWPQ + "InitWP()' style='padding:10'>" + 
		sBody + 
		"</body><script language='javascript'>PageOnLoad();</script></html>";
	return dspSend_RemoveHttpSrc(sHTML);
}

function dspSend_RemoveHttpSrc(s) {
	return s.replace(/src=/g, " sss=")
}

function dspSend_GetLinkedCustomScript(sXml) {
	var s = ""; var oXml = dspApp_CreateXMLDOM(sXml);
	try {s = oXml.selectSingleNode("DSPWebPart/Def/ContentServerProgID").text;}
	catch(e){s="";}
	if (s!="") {	
		s = s.substr(0, s.indexOf("."));
		if ("dspwsc011,dspwsc021,dspwsc031,dspwsc051,dspwsc091".indexOf(s) < 0)
			s += ".js";
		else
			s = "";
	}
	return s;
}

function dspSend_GetLinkedTableStyleSheet(sStyleSheetId) {
	var styleSheet = null;
	try {
		styleSheet = document.styleSheets("oDSP_" + sStyleSheetId);
	}
	catch(e) {
		styleSheet = null;
	}
	if (styleSheet != null)
		return styleSheet.href;
	else
		return "";
}

function dspSend_GetScripts(oScriptTag) {	
	var sScript= "";
	if (typeof(oScriptTag.outerHTML) == "undefined") {
		for (var i=0;i<oScriptTag.length;i++) {
			sScript += oScriptTag[i].outerHTML;
		}	
	}	
	else {
		sScript = oScriptTag.outerHTML;
	}		
	return sScript;
}

function dspSend_GetLinkedScripts(sDefXml) {
	var sPath = dspSend_GetPath(oDSP_Style.href);
	var aFiles = new Array("dspTrendLib_40.js", "dspChartLib_40.js", "dspTableLib_40.js", "dspDataLib_40.js", "dspUtilLib_40.js", "dspAppLib_40.js", dspSend_GetLinkedCustomScript(sDefXml), "dspFormatLib_40.vbs");
	var sContent = "";
	for (var i=0;i<aFiles.length;i++) {
		if (aFiles[i].length > 0 ) {
			s = dspSend_GetResourceContent(aFiles[i], sPath);
			if (s.length==0)
				return "";
			else
				sContent += s;
		}
	}			
	return sContent;
}

function dspSend_OnLoadScript(sWPQ) {
	var s = " <script language='javascript'>" + 
		" function PageOnLoad() {" + 
		" var oToHide = document.getElementsByTagName('img'); " + 
		"	for(var i=0;i<oToHide.length;i++) {" + 
		"		oToHide[i].style.display = 'none';" + 
		"		oToHide[i].src = 'http://';" + 
		"	}" + 
		"	var oToShow = document.getElementsByTagName('span');" + 
		"	for(var i=0;i<oToShow.length;i++) {" + 
		"		if (oToShow[i].imgReplacer != null)" + 
		"			oToShow[i].style.display = 'block';" + 
		"	}" + 
		"   document.all('ifraSubmit" + sWPQ + "').src='http://';" + 
		" }" + 
		"</script> ";
	return s;
}

function dspSend_GetStyleSheetObject() {
	var oCSS;
	try {
		oCSS = document.styleSheets("onetidThemeCSS");
	}
	catch(e) {
		oCSS = null;
	}
	if (oCSS==null)
		oCSS = document.styleSheets(0);
	return oCSS;
}

function dspSend_GetStyleSheet(sUrl, oStyleSource, sXml, sTableStyleSheet) {
	var s="", r;
	var aRules = Array("BODY", "TABLE", ".ms-underline", ".ms-WPBody", ".ms-SPLink", ".ms-WPTitle", ".ms-WPTitle A:hover", ".ms-WPTitle A:link", ".ms-WPTitle A:visited");
	var aStyles = new Array(aRules.length);
	for(var j=0;j<aRules.length;j++)
		aStyles[j] = "";
	var oCSS = dspSend_GetStyleSheetObject();
	for (var i=0;i<oCSS.rules.length;i++) {
		r = oCSS.rules.item(i);
		for(j=0;j<aRules.length;j++) {
			if (r.selectorText == aRules[j])
				aStyles[j] += r.style.cssText + ";";
		}
	}
	for(j=0;j<aRules.length;j++) {
		if (j == 0 || j == 1)
			aStyles[j] += aStyles[j] + "font-size:" + oStyleSource.currentStyle.fontSize + ";";
		s += aRules[j] + "{" + aStyles[j] + "}";
	}

	var sContent = dspSend_GetFileContent(sUrl);
	var sTableStyle = dspSend_GetFileContent(dspSend_GetLinkedTableStyleSheet(sTableStyleSheet));

	return "<style>" + sContent + s + sTableStyle + "v\\:* {behavior:url(#default#VML)}</style>";
}

function dspSend_GetResourceContent(sFile, sPath) {
	var sContent = dspSend_GetFileContent(sPath + sFile);
	if (sContent=="")
		return "";
	switch (sFile.substr(sFile.lastIndexOf("."))) {
	case ".js":
		sContent = "<script language='javascript'>" + sContent + "</script>";
		break;
	case ".vbs":
		sContent = "<script language='vbscript'>" + sContent + "</script>";
		break;
	default:
		alert("Unable to create content. Reason: Unknown extension " + fileExtension);
		return "";
	}
	return sContent;
}

function dspSend_GetPath(sUrl) {
	var x = sUrl.lastIndexOf("/");
	if (x > 0)
		return sUrl.substring(0, x+1);
	else
		return "";
}

function dspSend_GetFileContent(sUrl) {
	var sContent = "";
	if (sUrl != "") {
		try {
			var oRequest = new ActiveXObject("Microsoft.XMLHTTP");
			oRequest.Open("GET", sUrl, false);
			oRequest.setRequestHeader("Content-type", "text/xml");
			oRequest.send();
			if (oRequest.status == 200)
				sContent = oRequest.responseText;
			else
				alert("Unable to create content. Reason: " + oRequest.statusText);
		}
		catch (e) {
			alert("Unable to create content. Reason: " + e.message);
		}
	}
	return sContent;
}
