// graph.js

var gappl_enabled=true; // onko gappl-sovellus enabloitu
var graph_enabled = true; // onko grafiikkapainike enabloitu
var gappl="/graph"; // grafiikkasovelluksen virtuaalihakemiston polku
var gdoc="/GraphPage.aspx"; //Oletusdokumentti


function toGraph(act) {
    if (!graph_enabled) return false;
    if (!gappl_enabled) return true; // px-web grafiikka enabloitu
    var doc;
    if (top.px.location.toString().toUpperCase().indexOf("SAVESHOW")>-1) doc = top.px.document;
    else {
        if (top.hide.location.toString().toUpperCase().indexOf("SAVESHOW")>-1) doc = top.hide.document;
    }
    if (!doc) return false;
    f= doc.forms[0];
    if (typeof(f)=="object") {
        var n = f.noofvar.value;
        var t = setVirtualPath(gappl + gdoc) // huom: graph.js.setVirtualPath()
            + "?lang=" + getLang()
            + "&matrix=" + escape(f.matrix.value)
            + "&timeid=" + f.timeid.value
            + "&root=" + escape(f.root.value);
        if (isGedit()) {
            t += "&Gedit=true";
            mb.setMsel();
            if (mb.isSelcmd()) t += "&Selcmd=" + escape(mb.getSelcmd());
            //t += "&selidx=" + escape(getSelForQue());
            }
        if (act) t += "&" + act;
        if (isVarval()) t += "&IsVarval=true";
            top.px.location = t;
    }	 
    return false;
}


function toStat() {
if (isVarval()) {
  	var s = getSearch();
		if (s.indexOf("ACTION")>-1) s = replaceHttpItem(s,"ACTION","ACTION=SUBMIT");
		else s += "&ACTION=SUBMIT";		 
	  //show("graph.toStat","hide.location.previous: " + hide.location,"hide.location.new: " + s );
		setSearch(s);
		top.hide.location = getSearch();
		}
else {	
		restoreSaveShow();
		return;
		}
}

function setVirtualPath(path) {
if (!path) alert(setVirtualPath.caller);
if (path.indexOf("..") == 0) path = path.substring(2);
var l = top.location.href;
if (l.indexOf("?") > -1) l = l.substring(0,l.indexOf("?"));
return l.substring(0,l.lastIndexOf("/")) + path;
}

// global plugin variables
var pluginspage = "www.adobe.com/svg/viewer/install/";
var pluginstype = "image/svg+xml";
var pluginsstyle = "margin:0px;";
var pluginswidth = "100%";
var pluginsheight = "95%";

function pictureCode(href,x,y) {
	if (isSvgCtlInstalled()) return embedCode(href,x,y);
  if (!y) {
		 y = getHttpItem(href,"y");				 
		 y = 0.95*y;
		 x = 1.35*y;
		 y = y-y%1;
		 x = x-x%1;
	}	 
	href = replaceHttpItem(href,"x","x=" + x);
	href = replaceHttpItem(href,"y","y=" + y);
	return svgLinkCode(href,x,y);
}

function embedCode(href,w,h){
if (!w) {
	 w=pluginswidth;
	 h=pluginsheight;
	 }
var plg = (pluginspage.length>0) ? ' pluginspage="' + pluginspage + '">\n' : '';
var html =  '<embed id="svgctl" name="svgctl"'
		+ ' width="' + w + '" height="' + h + '"\n'
		+ ' type="' + pluginstype + '" style="' + pluginsstyle + '"\n'
		+ ' src="' + href + '" \n'
		+ plg
		+ '</embed>';
return html;
} 
function objectCode(href,w,h) {
if (!w) {
	 w=pluginswidth;
	 h=pluginsheight;
	 }
var html = '<object id="svgctl0"\n type="' + pluginstype + '"\n'
		+ ' width="' + w + '" height="' + h + '"\n'
		+ ' data="' + href + '">\n'		 
		+ '<p>Your browser does not support svg graphics'
		+ '</object>\n'
    + '<![endif]>\n'; 
return html;
}

function imgCode(href,x,y) {
// imgCode for svgframe
href = href.replace(".svg",".png");
// adjust img size to svgframe
if (!y) {
	 y = getHttpItem(href,"y");
	 x = 1.35*y;
	 x = x-x%1;
	 }	 
return '<img src="'	+ href + '"\n alt="' + getHttpItem(href.toLowerCase(),"ifile") 
		+ '"\n width="' + x + '" height="' + y + '"/>\n';
}	

function isSvgCtlInstalled(){
    try {
        var asv = new ActiveXObject("Adobe.SVGCtl");
        return true;
    } catch(e){}
    return false;
}

function isMSIE() {
  return navigator.appName == "Microsoft Internet Explorer" 
			 && navigator.userAgent.indexOf("MSIE")>-1;
}

function isMSIE4up() {
	if (!isMSIE())return false;
	var ua = navigator.userAgent,idx=ua.indexOf("MSIE")+5;
	return ua.substr(idx,1)>4;
}

function isVml(){
	return isMSIE4up() && !isSvgCtlInstalled();
}

function isOpera9_1up(){ 
var ua = navigator.userAgent;
return navigator.appName == "Opera" && ua.substr(1+ua.indexOf("/"),3)>9.0;
}

function isGecko() {
return navigator.product == "Gecko";
}

function isGecko1_8up() {
if (!isGecko()) return false;
var ua = navigator.userAgent;
var i = ua.indexOf("rv:");
if (i == -1) return false;
return ua.substr(i+3,3)>=1.8; // support for svg is since rv:1.8
}

function svgLinkCode(href,x,y){
	var png = href.replace(".svg",".png");
	var vml = href.replace(".svg",".vml");
	var w,h;
	if (!y) {
	 	 w = x = getHttpItem(href,"x");
	 	 h = y = getHttpItem(href,"y");
	 	 w = Number(w) + 0.08 * Number(w); w=w-w%1;
	 	 h = Number(h) + 0.08 * Number(h); h=h-h%1;
	} else {
		 w = pluginswidth;
		 h = pluginsheight;
	}
	var alt = getHttpItem(href.toLowerCase(),"ifile");
	var html = '<!--[if IE]>\n'
        //+ '<iframe src="' + vml + '" height="' + h + '" width="' + w + '">\n'
        + '<iframe id="vmlctl" src="' + vml + '"\n'
        + 'frameborder="0" scrolling="no" marginwidth="0" marginheight="0" '
        + 'height="' + h + '" width="' + w + '">'
        + '</iframe>'
        + '<![endif]-->\n'
        + '<![if !IE]>\n'
	    + '<object id="svgctl" name="svgctl"'
		+ ' width="' + w + '" height="' + h + '"\n'
		+ ' type="' + pluginstype + '" style="' + pluginsstyle + '"\n'
		+ ' data="' + href + '">\n'
		+ ' <param name="src"\n'
		+ ' value="' + href + '">\n'
    	+ ' <object\n type="image/png"\n'
		+ ' width="' + x + '" height="' + y + '"\n'
		+ ' data="' + png + '">\n'		 
		+ '</object>\n'
		+ '<img src="' + png + '"\n'
 		+ 'alt="' + alt +'"\n'
 		+ 'width="' + x + '" height="' + y + '"/>\n'
		+ '</object>\n'
        + '<![endif]>\n';
		return html;
}

function variable() {

    this.data = {};
    this.vars = {} //MKOS

    this.Store = function(sel){
        for (var i in sel) {
            this.data[i] = sel[i];
        }
    }; 
    // MKOS: store sellist to reuse in varvm when returning to StatPage
    this.storeVar = function(sel){
        for (var i in sel) {
            this.vars[i] = sel[i];
        }
    }; 

    this.Length = function() {
        var count = 0;
        for (var i in this.data) count++;
        return count;
    };

    this.list = function(varname) {
        var msg = "";
        for (var i in this[varname]) {
            msg += i + "=" + this[varname][i] + "\n";
        }
        show(varname + ":", msg);
    };

};

function getHttpItem(query,key) {
// query = http-query
var r = "";
key = key.toUpperCase();
query = query.toString() + "&";
var i = query.toUpperCase().indexOf("&" + key + "=");
if (i == -1) i = query.toUpperCase().indexOf("?" + key + "=");
if (i == -1) return r;
r = query.substring(i + 2 + key.length);
r = unescape(r.substring(0,r.indexOf("&")));
r = r.replace(/\+/g," ");
return r;
}

function replaceHttpItem(lst,key,itm) {
//the whole item is replaced or appended, not only value
lst = unescape(lst);
var hash = "";
if (lst.indexOf("#")>-1) {
	 lst =lst.split("#");
	 hash = lst[1];
	 lst = lst[0];
	 }
var sep,v;
if (lst.indexOf("&" + key + "=") == -1 && lst.indexOf("?" + key + "=") == -1) {
	sep = (lst.indexOf("?")>-1) ? "&" : (lst.indexOf("&")>-1) ? "&" : "?";
  if (itm.length>0) lst = lst + sep + itm;
} 
else {
  v = getHttpItem(lst,key);
	if (itm.length==0) { 
		 sep = (lst.indexOf("?" + key + "=")>-1) ? "?" : "&";
		 lst = lst.replace(sep + key + "=" + v, "");
		 if (sep == "?") lst = lst.replace("&","?"); 
	}
	else { 	lst = lst.replace(key + "=" + v, itm);}
}	
if (hash.length>0) lst += "#" + hash;	
return lst;
}

// when graph is launched from PX-Web's ShowTableHtml2.asp, the method below replaces the orginal function
function LaunchNew(selvalue) 
{
if (selvalue.selectedIndex > 0) {
	var href,oldhref = selvalue.options[selvalue.selectedIndex].value;
	if (oldhref.indexOf("diagramloop.asp")>-1) {
	    href = "../graph?matrix=" + getHttpItem(oldhref,"matrix");
	    href += "&timeid=" + getHttpItem(oldhref,"timeid");
	    href += "&lang=" + getHttpItem(oldhref,"lang");
	} else href = oldhref;
	window.open(href);
	selvalue.selectedIndex=0;
	}
}