﻿//  Reporting routines

fwGetParam = function(key, query) {
    if (!query)
        query = window.location.search;    
    var re = new RegExp("[?|&]" + key + "=(.*?)&");
    var matches = re.exec(query + "&");
    if (!matches || matches.length < 2)
        return "";
    return decodeURIComponent(matches[1].replace("+", " "));
}

fwSetParam = function(key, value, query) {
   
    query = query || window.location.search;
    var q = query + "&";
    var re = new RegExp("[?|&]" + key + "=.*?&");
    if (!re.test(q))
        q += key + "=" + encodeURI(value);
    else
        q = q.replace(re, "&" + key + "=" + encodeURIComponent(value) + "&");
    q = q.fwTrimStart("&").fwTrimEnd("&");
    return q;
}

String.prototype.fwTrimEnd = function(c) {
    if (c)        
        return this.replace(new RegExp(c.fwEscapeRegExp() + "*$"), '');
    return this.replace(/\s+$/, '');
}
String.prototype.fwTrimStart = function(c) {
    if (c)
        return this.replace(new RegExp("^" + c.fwEscapeRegExp() + "*"), '');
    return this.replace(/^\s+/, '');
}
String.prototype.fwEscapeRegExp = function() {
    return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0");
};

function setReportProperty(reportId,property,value) {
  var params=getElementById(reportId+'Params').value;
}

function fwShowLoading(containerId) {
    coords=fwGetCoordinates(document.getElementById(containerId));
    var divTag = document.createElement("div");
    divTag.style.height='50px'; divTag.style.width='130px';
    divTag.innerHTML = '<table><tr><td><img src="siteimages/ajax-loader.gif" style="border:none;margin:8px 0px 10px 8px"></td><td style="vertical-align:middle;font-size:14px">&nbsp;&nbsp;&nbsp;Loading...</td></tr></table>';
    divTag.style.borderWidth='1px';
    divTag.style.borderColor='black';
    divTag.style.borderStyle='solid';
    divTag.style.position='absolute';
    divTag.style.display='block';
    divTag.style.backgroundColor='white';
    divTag.style.top = coords.y+parseInt(coords.height/2-25)+'px';
    divTag.style.left = coords.x+parseInt(coords.width/2-65)+'px';
    document.getElementById(containerId).appendChild(divTag);
}

function fwFillReportContainer(containerId, url){ 
  fwShowLoading(containerId);
  setTimeout('fwFillReportContainer2(\''+containerId+'\', \''+fwSetParam('qpajaxupdate', '1', url).replace(' ', '+')+'\')', 10);
}

function fwFillReportContainer2(containerId, url){ 
  $telerik.$('#'+containerId).load(url); 
  document.getElementById(containerId).style.minHeight='';
}

//  Required function so dialogs can change the URL of the parent
function fwChangeUrl(url){ 
  document.location=url;
}

//  simple Ajax

function fwGetRequestObject() {
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest()
  }
  else
  {
    if (window.ActiveXObject) {
      return new ActiveXObject('MSXML2.XMLHTTP.3.0');
    }
  }
}

function fwGetText(page, async, callbackFunction) {
  var xmlHttp = fwGetRequestObject();
  if (xmlHttp.overrideMimeType) { xmlHttp.overrideMimeType('text/plain') }

  if (async) {
    xmlHttp.onreadystatechange = function() {
      if(xmlHttp.readyState==4) {
        callbackFunction(xmlHttp.responseText);
      }
    }
  }
  xmlHttp.open('GET', page, async);
  xmlHttp.send(null);

  if (!async) {
    return xmlHttp.responseText
  }
}


//  Context menus


fwActiveMenu=''; fwInMenu=false;

function fwMenuMouseOver(id) {
  fwInMenu=true;
  var m=eval('fwContextMenu_'+id),p=document.getElementById(m.parentElementId);

  if (fwActiveMenu!='') {
    document.getElementById(fwActiveMenu).style.display='none'
  }
  fwActiveMenu=m.parentElementId+'_cmenu';

  if (document.getElementById(m.parentElementId+'_cmenu') == null) {
    var divTag = document.createElement("div");
    divTag.id = m.parentElementId+'_cmenu';
    if (m.width) {divTag.style.width=m.width+'px'};
    divTag.className ="context-menu";
    var h='';

    var noBottom='';
    for (var i=0; i<m.items.length; i++) {
      if (i==m.items.length-1) {noBottom=';border-bottom:none'} else {noBottom=''}

      if (m.items[i].href.substr(0, 11)=='javascript:'){ 
        h += '<a onclick="document.getElementById(fwActiveMenu).style.display=\'none\';'+unescape(m.items[i].href.substr(11))+'" style=\"text-decoration:none\" href=\"#\"><div class=context-menu-row style="cursor:pointer'+noBottom+'" onmouseover="this.className=\'context-menu-row-highlighted\'" onmouseout="this.className=\'context-menu-row\'">'+
          m.items[i].label+'</div></a>';
      }
      else {
        h += '<a onclick="document.getElementById(fwActiveMenu).style.display=\'none\'" style=\"text-decoration:none\" href=\"'+m.items[i].href+'\"><div class=context-menu-row style="cursor:pointer'+noBottom+'" onmouseover="this.className=\'context-menu-row-highlighted\'" onmouseout="this.className=\'context-menu-row\'">'+
          m.items[i].label+'</div></a>';
      }
    }

    divTag.innerHTML = h;
    divTag.style.position='absolute';
    divTag.style.display='none';
    divTag.onmouseover=function() {fwInMenu=true};
    divTag.onmouseout=function() {fwMenuMouseOut()};
    document.body.appendChild(divTag);
  } 
  var e=document.getElementById(m.parentElementId+'_cmenu');
  coords=fwGetCoordinates(p);

  if (m.align!='left') {
    e.style.left = coords.x+coords.width+parseInt(m.offsetX)+'px';
  } else {
    e.style.left = coords.x+parseInt(m.offsetX)+'px';
  }
  e.style.top = coords.y+coords.height+parseInt(m.offsetY)+'px';
  e.style.display='block';
}

function fwMenuMouseOut() {
  fwInMenu=false;
  setTimeout('fwMenuDeactivate()',250);    
}

function fwMenuDeactivate() {
  if (!fwInMenu) {
    document.getElementById(fwActiveMenu).style.display='none'
  }
}

function fwCreateContextMenu(m) {
  var p=document.getElementById(m.parentElementId);
  eval('fwContextMenu_'+m.parentElementId+'=m');
  p.onmouseover=function() {fwMenuMouseOver(m.parentElementId)};
  p.onmouseout=function() {fwMenuMouseOut()};
}

function fwGetCoordinates(obj) 
{ 
  var newObj = new Object(); 
  
  newObj.x=obj.offsetLeft; 
  newObj.y=obj.offsetTop; 
  newObj.width=obj.offsetWidth;
  newObj.height=obj.offsetHeight;
  theParent = obj.offsetParent; 
  
  while(theParent != null)
  { 
    newObj.y += theParent.offsetTop; 
    newObj.x += theParent.offsetLeft; 
    theParent = theParent.offsetParent; 
  } 
  
  return newObj;
}

