/****************************************************************************************

    Copyright (c) 2004 Urchin Software Corporation. All rights reserved.   
    The javascript contained herein is the property of Urchin Software 
    Corporation, San Diego, CA. It may not be used outside the Urchin
    application without written consent.                      

*****************************************************************************************/

/*------------------------------------------------------------------
--- draw the flash graph
------------------------------------------------------------------*/
function urGraphKPI(urdata,conf) {
   var mye=null;
   var html="";
   if ((mye=document.getElementById("ur_kpi"+(urdata.idx+1))) == null) return;
   if (urdata.rtype == 24) {
      html = urGraphMap(urdata,conf,1);
   } else {
      html = urGraphDraw(urdata,conf,0,1);
   }
   mye.innerHTML = html;
}

function urGraphDraw(urdata,conf,popup,kpi) {
	
	for(var it=conf.gcol;it<urdata.ctypes.length;it++){
		if(urdata.ctypes[it]<100){
			conf.gcol=it;
			break;
		}
   }

   if (urconf.sreport == 4) return;
   var mye;
   var flash = "UrchinGraph.swf";
   var fwidth = 600;
   var fheight = 275;

   if (popup == 3) return;
   if (popup == 1) {
      if ((mye=document.getElementById("ur_popflash")) == null) return;
   } else if (popup == 2) {
      if ((mye=document.getElementById("ur_flash2")) == null) return;
   } else if (kpi) {
      if ((mye=document.getElementById("ur_kpi"+(urdata.idx+1))) == null) return;
      flash = "UrchinGraphMini.swf";
      fwidth = 350;
      fheight = 225;
   } else {
      if ((mye=document.getElementById("ur_flash")) == null) return;
   }
   var title = urdata.title;
   if (title.charAt(0) == "G" && (title.charAt(1) == "1" || title.charAt(1) == "2" || title.charAt(1) == "3" || title.charAt(1) == "4") && goals[title.charAt(1)-1]) {
      title = title+": "+goals[title.charAt(1)-1];
   }

   /*------------------------------------------------------------------
   --- format the data 
   ------------------------------------------------------------------*/
   var xdata="";
   for (var ii=0;ii<urdata.data.length;ii++) {
      var name = urdata.data[ii][0];
      var value = "";

      if (conf.level > 0 && conf.token && !conf.sreport) {
		if (!inMD)
         name = conf.token + name;
         if (name.charAt(name.length-1) == conf.sep)  name = name.substring(0,name.length-1);
      } else if (urdata.rtype == 5 || urdata == 6 || inMD) {
         if (name.charAt(name.length-1) == conf.sep)  name = name.substring(0,name.length-1);
      }
      if (urdata.rtype == 14) {
         name = name.replace(/\(no data\) -> /g,"");
      }
      name = encodeURIComponent(name);

      if (ucal_comp || urdata.rtype == 20) {
         if (urdata.rtype == 8) {
           var myd1=0,myd2=0;
           if (urdata.nvalues > 2 && urdata.ctypes[urdata.ctypes.length-1] != 30) {
               myd1 = urdata.data[ii][conf.gcol];
               myd2 = urdata.data[ii][conf.gcol+(urdata.nvalues-1)];
            } else {
               myd1 = urdata.data[ii][conf.gcol];
               myd2 = urdata.data[ii][conf.gcol+(urdata.nvalues-2)];
            }
            if (!myd1) myd1 = 0;
            if (!myd2) myd2 = 0;
            value += "\\t"+encodeURIComponent(myd1);
            value += "\\t"+encodeURIComponent(myd2);
        } else if (urdata.rtype == 4 || urdata.rtype == 5 || urdata.rtype == 6) {
            value += "\\t"+encodeURIComponent(urdata.data[ii][conf.gcol]);
        } else {
            for (var jj=1;jj<urdata.data[ii].length;jj++) {
               if (urdata.ctypes[jj] < 20) continue;
               value += "\\t"+encodeURIComponent(urdata.data[ii][jj]);
            }
        }
      } else {
         value += "\\t"+encodeURIComponent(urdata.data[ii][conf.gcol]);
      }

      xdata += name+value+"\\n";
   }

   var cnames="";
   if (urdata.rtype == 20) {
      for (var ii=0;ii<urdata.cnames.length;ii++) {
         if (urdata.ctypes[ii] == 30) continue;
         if (ii) cnames += "|";
         cnames += urdata.cnames[ii];
      }
   }

   if (ucal_comp) {
      cnames =  "Date Comp|";
      cnames += uformatDate(ucal_bdate.date)+"-";
      cnames += uformatDate(ucal_edate.date)+"|";
      cnames += uformatDate(ucal2_bdate.date)+"-";
      cnames += uformatDate(ucal2_edate.date);
   }

   var topt="";

   /*------------------------------------------------------------------
   --- handle totals and averages
   ------------------------------------------------------------------*/
   if ((urdata.sopt&1) == 1 && urdata.data[0]) { 
      topt = term315+": "; 
      if (urdata.rtype != 8) {
         for (var ii=1;ii<urdata.data[0].length;ii++) {
             if (ii != 1) topt += " | "
             topt += uformatValue(urdata.totals[ii],urdata.ctypes[ii]);
         }
      } else {
         topt += uformatValue(urdata.totals[conf.gcol],urdata.ctypes[conf.gcol]);
         if (ucal_comp) {
            if (ii != 1) topt += " | "
            if (urdata.nvalues > 2 && urdata.ctypes[urdata.ctypes.length-1] != 30) {
               topt += uformatValue(urdata.totals[conf.gcol+urdata.nvalues-1],urdata.ctypes[conf.gcol]);
            } else {
               topt += uformatValue(urdata.totals[conf.gcol+urdata.nvalues-2],urdata.ctypes[conf.gcol]);
            }
         }
      }
   }
   if ((urdata.sopt&2) == 2 && urdata.data[0]) {
      topt += "   "+term316+": "; 
      for (var ii=1;ii<urdata.data[0].length;ii++) {
          if (ii != 1) topt += " | "
          if (urdata.nrecs) { 
             if (urdata.ctypes[conf.gcol] >= 40) topt += uformatValue((urdata.totals[ii]/urdata.nrecs),urdata.ctypes[conf.gcol]);
             else topt += uformatValue((urdata.totals[ii]/urdata.nrecs),31);
         } else {
            topt += "0";
         }
      }
   }
   if (urdata.rtype == 20 && (urconf.id == 2001 || urconf.id == 2002 || urconf.id == 2101 || urconf.id == 2201)) {
      topt += term316+": "; 
      if (urconf.id == 2001 || urconf.id == 2101 || urconf.id == 2201) { 
         if (urdata.totals[2] != 0 && urdata.totals[1] != 0) topt += uformatValue((urdata.totals[2]/urdata.totals[1]),31);
         else topt += "0.00";
         topt += " P/V\n";
      }
      if (urconf.id == 2002) { 
         if (urdata.totals[2] != 0 && urdata.totals[1] != 0) topt += uformatValue((urdata.totals[1]/urdata.totals[2]),42);
         else topt += "0.00";
         topt += " R/T\n";
      }
      topt += urdata.cnames[1] + ": "+ uformatValue(urdata.totals[1],urdata.ctypes[1])+"\n";
      topt += urdata.cnames[2] + ": "+ uformatValue(urdata.totals[2],urdata.ctypes[2])+"";
   }

   /*------------------------------------------------------------------
   --- prepare the html
   ------------------------------------------------------------------*/
   var html = '';

   html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
   html += 'codebase="'+location.protocol+'//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" ';
   html += 'width="'+fwidth+'" height="'+fheight+'" id="UrchinGraph' +urdata.idx+'" align="middle">';
   html += '<param name="allowScriptAccess" value="sameDomain" />';
   html += '<param name="movie" value="uflash/'+flash+'" />';
   html += '<param name="quality" value="high" />';
   html += '<param name="bgcolor" value="#ffffff" />';
   html += '<param name="wmode" value="transparent" />';

   if (isIE) {
      html += '<param name="FlashVars" value="';
      html += 'ntitle='+encodeURIComponent(urdata.cnames[0])+'&';
      if (urdata.rtype == 3)  html += 'vtitle='+urdata.cnames[0]+'&';
      else                    html += 'vtitle='+urdata.cnames[conf.gcol]+'&';
      if (urdata.rtype == 20) html += 'vtitle2='+urdata.cnames[2]+'&';
      html += 'cnames='+cnames+'&';
      html += 'datatype='+urdata.ctypes[conf.gcol]+'&';
      html += 'rtitle='+title+'&';
      html += 'fsize='+conf.fsize+'&';
      if (urdata.ctypes[conf.gcol] != 20 && urdata.ctypes[conf.gcol] != 40 && 
          urdata.ctypes[conf.gcol] != 22 && urdata.ctypes[conf.gcol] != 26) html += 'gtypes='+urdata.gtypes.replace("pie","")+'&';
      else                                  html += 'gtypes='+urdata.gtypes+'&';
      if (topt && topt != "")html += 'toptitle='+topt+'&';
      if (urdata.rtype == 2) html += 'hist=1&';
      if (urdata.rtype == 3) html += 'stack=1&';
      if (conf.cgraph) html += 'cgraph='+conf.cgraph+'&';
      if (uloc) html += 'uloc='+uloc.region+'|'+uloc.cursym+'|'+uloc.curwhere+'|'+uloc.curprec+'&';
      if (urdata.totals[conf.gcol]) html += 'total='+urdata.totals[conf.gcol]+'&';
      html += 'xdata='+xdata+'" />';
   }

   html += '<embed src="uflash/'+flash+'" ';
   html += 'quality="high" bgcolor="#ffffff" ';
   html += 'width="'+fwidth+'" height="'+fheight+'" name="UrchinGraph" align="middle" wmode="transparent"';
   html += 'allowScriptAccess="sameDomain" type="application/x-shockwave-flash" ';
   html += 'pluginspage="http://www.macromedia.com/go/getflashplayer" ';
   if (!isIE) {
      html += 'FlashVars="';
      html += 'ntitle='+encodeURIComponent(urdata.cnames[0])+'&';
      if (urdata.rtype == 3) html += 'vtitle='+urdata.cnames[0]+'&';
      else                   html += 'vtitle='+urdata.cnames[conf.gcol]+'&';
      if (urdata.rtype == 20) html += 'vtitle2='+urdata.cnames[2]+'&';
      html += 'cnames='+cnames+'&';
      html += 'datatype='+urdata.ctypes[conf.gcol]+'&';
      html += 'rtitle='+title+'&';
      html += 'fsize='+conf.fsize+'&';
      if (urdata.ctypes[conf.gcol] != 20 && urdata.ctypes[conf.gcol] != 40 && 
          urdata.ctypes[conf.gcol] != 22 && urdata.ctypes[conf.gcol] != 26) html += 'gtypes='+urdata.gtypes.replace("pie","")+'&';
      else                                  html += 'gtypes='+urdata.gtypes+'&';
      if (topt && topt != "")html += 'toptitle='+topt+'&';
      if (urdata.rtype == 2) html += 'hist=1&';
      if (urdata.rtype == 3) html += 'stack=1&';
      if (urdata.totals[conf.gcol]) html += 'total='+urdata.totals[conf.gcol]+'&';
      if (conf.cgraph) html += 'cgraph='+conf.cgraph+'&';
      if (uloc) html += 'uloc='+uloc.region+'|'+uloc.cursym+'|'+uloc.curwhere+'|'+uloc.curprec+'&';
      html += 'xdata='+xdata+'"';
   }
   html += ' /></object>';

   if (kpi) {
      return html;
   } else if (urdata.rtype == 1 || urdata.rtype == 7) {
      var mydiv = document.createElement("div");
      if (!urdata.idx) mye.innerHTML = "";
      mye.appendChild(mydiv);
      mydiv.innerHTML = html;
   } else {
      mye.innerHTML = html
   }
}

/*------------------------------------------------------------------
--- draw the flash Map
------------------------------------------------------------------*/
function urGraphMap(urdata,conf,kpi) {
   var mye;
   var flash = "UrchinGraphMap.swf";
   var fwidth = 800;
   var fheight = 500;
   var title = urdata.title;
   if ((mye=document.getElementById("ur_map")) == null) return;

   if (kpi) {
      flash = "UrchinGraphMapMini.swf";
      fwidth = 350;
      fheight = 225;
   }

   /*------------------------------------------------------------------
   --- format the data 
   ------------------------------------------------------------------*/
   var xdata="";
   for (var ii=0;ii<urdata.data.length;ii++) {
      var name = urdata.data[ii][0];
      var value = "";
      var info = name.split("|");

      if (!info[0] || !info[1] || !info[2]) continue;
      for (var jj=1;jj<info.length;jj++) {
         value += "\\t"+info[jj];
      }

      for (var jj=1;jj<urdata.data[ii].length;jj++) {
         if (urdata.ctypes[jj] < 20) continue;
         value += "\\t"+urdata.data[ii][jj];
      }
      xdata += info[0]+value+"\\n";
   }

   var cnames="";
   var ctypes="";
   for (var ii=0;ii<urdata.cnames.length;ii++) {
      if (urdata.ctypes[ii] == 30) continue;
      if (ii) cnames += "|";
      if (ii) ctypes += "|";
      cnames += urdata.cnames[ii];
      ctypes += urdata.ctypes[ii];
   }

   /*------------------------------------------------------------------
   --- prepare the html
   ------------------------------------------------------------------*/
   var html = '';

   html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
   html += 'codebase="'+location.protocol+'//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" ';
   html += 'width="'+fwidth+'" height="'+fheight+'" id="UrchinGraph' +urdata.idx+'" align="middle">';
   html += '<param name="allowScriptAccess" value="sameDomain" />';
   html += '<param name="movie" value="uflash/'+flash+'" />';
   html += '<param name="quality" value="high" />';
   html += '<param name="bgcolor" value="#ffffff" />';
   html += '<param name="wmode" value="transparent" />';

   if (isIE) {
      html += '<param name="FlashVars" value="';
      html += 'cnames='+cnames+'&';
      html += 'ctypes='+ctypes+'&';
      html += 'rtitle='+title+'&';
      html += 'fsize='+conf.fsize+'&';
      html += 'total='+urdata.totals[1]+'&';
      if (uloc) html += 'uloc='+uloc.region+'|'+uloc.cursym+'|'+uloc.curwhere+'|'+uloc.curprec+'&';
      html += 'xdata='+xdata+'" />';
   }

   html += '<embed src="uflash/'+flash+'" ';
   html += 'quality="high" bgcolor="#ffffff" ';
   html += 'width="'+fwidth+'" height="'+fheight+'" name="UrchinGraph" align="middle" wmode="transparent"';
   html += 'allowScriptAccess="sameDomain" type="application/x-shockwave-flash" ';
   html += 'pluginspage="http://www.macromedia.com/go/getflashplayer" ';
   if (!isIE) {
      html += 'FlashVars="';
      html += 'cnames='+cnames+'&';
      html += 'ctypes='+ctypes+'&';
      html += 'rtitle='+title+'&';
      html += 'fsize='+conf.fsize+'&';
      html += 'total='+urdata.totals[1]+'&';
      if (uloc) html += 'uloc='+uloc.region+'|'+uloc.cursym+'|'+uloc.curwhere+'|'+uloc.curprec+'&';
      html += 'xdata='+xdata+'"';
   }
   html += ' /></object>';
   if (kpi) return html;
   else mye.innerHTML = html
}

function setGraphType(type) {
   if (urconf)   urconf.cgraph = type; 
   if (conf_seg) conf_seg.cgraph = type; 
   if (conf_dot) conf_dot.cgraph = type; 
}
