﻿// JScript File


if ((document.all) && (document.getElementById) && (document.body) && (document.body.style) && (document.body.style.filter))
{
	if (v_winloc_host == undefined) var v_winloc_host = window.location.host;
	if (v_winloc_pathname == undefined) var v_winloc_pathname = window.location.pathname;
	if (v_winloc_search == undefined) var v_winloc_search = window.location.search;
}
else
{
	var v_winloc_host = window.location.host;
	var v_winloc_pathname = window.location.pathname;
	var v_winloc_search = window.location.search;
}


function f_cbload(v_sK, v_sP, cHost)
{
	v_path = v_winloc_pathname+v_winloc_search;
	document.write("\n<style type=\"text/css\">\n<!--\n #cxkg {visibility:hidden; font-size:6px; position:relative; }\n-->\n</style>\n");
//null.d;
	if ((v_path.toUpperCase().indexOf("DP.ASP?APPKEY=") != -1)||(v_path.toUpperCase().indexOf("DP.ASP?APPSESSION=") != -1)) 
		document.write("<br/><strong>Error - Cannot display DataPage due to multiple embedded deployments.</strong></br>");
	else document.write("<scri"+"pt type=\"text/javascript\" src=\""+v_sP+"//"+cHost+"/dp.asp?AppKey="+v_sK+"&amp;js=true&amp;pathname="+window.location.protocol+"//"+v_winloc_host+v_winloc_pathname+"&amp;"+v_winloc_search+"\"></scr"+"ipt>");
}

function moveSpan(targetId)
{
	var caspio=document.getElementById("caspioform");
	var caspioDiv=document.getElementById(targetId);
	if (caspio==null)
	{
		caspioDiv.innerHTML="Caspio DataPage can not be loaded. Reason: incorrect appKey or connection does not exists";
		return;
	}
	caspioDiv.title=caspio.action;
	
	while(caspio.childNodes.length>0)
		caspioDiv.appendChild(caspio.removeChild(caspio.childNodes[0]));
		
	var inputs=caspioDiv.getElementsByTagName("input");
	for(var i=0;i<inputs.length;i++)
		if (inputs[i].type.toLowerCase()=="submit")
			xAddEventListener(inputs[i],"click",changeAction,true);

	var rawTag=document.getElementById("hiddenForm");
	rawTag.parentElement.removeChild(rawTag);
}

function changeAction(evt)
{
	//capture src element
	var e = evt || window.event;
	if (!e) return;
	var target = e.target || e.srcElement;
	var parent=target.parentNode;
	//search current container
	//alert("currentDiv= "+parent)
	while(parent!=null)
	{
		if (parent.id.indexOf("caspioFormDiv")>-1) break;
		parent=parent.parentNode;
	}
	
	//removing differentForms
	if (parent!=null)
	{
		var divs=document.getElementsByTagName("div");
		var divsToRemove=new Array();
		var tmp=0;
		for(var i=0;i<divs.length;i++)
			if (divs[i].id.indexOf("caspioFormDiv")>-1)
			{
				if (divs[i].id!=parent.id)
				{
					//alert("Founded div: "+divs[i].id+"\n and current is: "+parent.id+"\n with tagName="+parent.tagName);
					//alert("Removing content of "+divs[i].id);
					//divs[i].parentElement.removeChild(divs[i]);
					divsToRemove[tmp]=divs[i];
					tmp++;
				}
			}
		for(var i=0;i<divsToRemove.length;i++)
			divsToRemove[i].parentNode.removeChild(divsToRemove[i]);
		//alert("founded divs to remove: "+divsToRemove.length);
	}
	
	//alert("HELLO ");
	
	//change form action
	document.forms[0].action=parent.title;//dpProcessUrl+window.location.search;
	//alert("Action is: "+document.forms[0].action);
	document.forms[0].method="post";
	document.forms[0].encoding ="multipart/form-data";
}

function xAddEventListener(e,eT,eL,cap)
{
	eT=eT.toLowerCase();
	if(e.addEventListener) e.addEventListener(eT,eL,cap||false);
	else
		if (e.attachEvent) e.attachEvent('on'+eT,eL);
		else
		{
			var o=e['on'+eT];e['on'+eT]=typeof o=='function'?function(v){o(v);eL(v);}:eL;
		}
}

