
function getXYPosition(e)
{
  myMouseX = (e || event).clientX;
  myMouseY = (e || event).clientY;
  if (myMouseX + 100 > document.documentElement.clientWidth)
  {
    myX = myMouseX - (myMouseX + 80 - (document.documentElement.clientWidth));
  }
  else 
  {
    myX = myMouseX + 20;
  }
  if (myMouseY + 54 > document.documentElement.clientHeight)
  {
    myY = myMouseY - (myMouseY + 34 - (document.documentElement.clientHeight));
  }
  else 
  {
    myY = myMouseY + 20;
  }
  if (document.documentElement.scrollTop > 0)
  {
    myY = myY + document.documentElement.scrollTop;
    myMouseY = myMouseY + document.documentElement.scrollTop;
  }
  document.getElementById('xy').style.top = myY + "px";
  document.getElementById('xy').style.left = myX + "px";
  if (xyOn)
  {
    document.getElementById('xy').style.visibility = "visible";
  }
  else 
  {
    document.getElementById('xy').style.visibility = "hidden";
  }
}

function toggleXY(myHeight)
{
  xyOn = !xyOn;
  if (!xyOn)
  {
    document.getElementById('xy').style.visibility = "hidden";
  }
  else 
  {
    document.getElementById('xy').innerHTML = myHeight;
    document.getElementById('xy').style.visibility = "visible";
  }
  return false;
}




function drawNewChart(elementName,chartHeading,yAxisName,xAxisName,maxNo, parameters)
{
  var xAxisSize = 10;
  var chartHeight = 100;
  var chartWidth = 400;

  if (!document.getElementById(elementName))
  {
    var myOut = "";
    myOut += '<a href="javascript:hide();" name="' + elementName + '_top"><span class="chart_heading">' + chartHeading + '&nbsp;&nbsp;<img src="img/Close-2-icon.png" height="10px;"/></span></a>';
    myOut += '<span class="legendY"><strong>' + yAxisName + ' (Y-Axis)</strong></span>';
    myOut += '<div class="axis" id="' + elementName + '">';

    var bigBar = 0;
	//Initially draw the bars
    for (i = 1;i < parameters.length;i = i + 2)
    {
      var myH = parameters[i];
      myXname = parameters[i - 1];
      myOut += '<div class="barVert" id="' + elementName + 'bar' + i + '" onmouseover="toggleXY(' + myH + ');" onmouseout="toggleXY();"></div>';
      myOut += '<div class="xName" id="' + elementName + 'xName' + i + '">' + myXname + '</div>';
    }

	//Make sure Max No. is in terms of axis size
    while (maxNo % xAxisSize != 0)
    {
      maxNo++;
    }
    //set the bar in html
    document.getElementById('chartHolder').innerHTML = document.getElementById('chartHolder').innerHTML + myOut;
	//y Axis Element display
    for (i = 1;i <= xAxisSize ;i++)
    {
      myOut = '<div class="rulesVert" style="bottom:' + (chartHeight * (i / xAxisSize) + (xAxisSize * i)) + 'px;">' + maxNo * (i / xAxisSize) + ' ----</div>';
      document.getElementById('chartHolder').innerHTML = document.getElementById('chartHolder').innerHTML + myOut;
    }
	
    document.getElementById('chartHolder').innerHTML = document.getElementById('chartHolder').innerHTML + '<div class="legendX"><strong>' + xAxisName + ' (X-Axis)</strong></div></div>';
	//set the bar heights properly
    for (i = 1;i < parameters.length;i = i + 2)
    {
		//alert(parameters[i-1] + "--> " + parameters[i] + " " + maxNo + "bigBar: " + bigBar);
      document.getElementById(elementName + 'bar' + i).style.height = (parameters[i]/maxNo)*chartHeight + "px";

      document.getElementById(elementName + 'xName' + i).style.top = chartHeight + "px";
    }
    var myBarDivs = document.getElementById(elementName).getElementsByTagName("div");

    //similarly set the width and margin distance 
    for (i = 0;i < myBarDivs.length;i++)
    {
      //document.getElementById(myBarDivs[i].id).style.marginLeft = i * 35 + 10 + "px";
	  document.getElementById(myBarDivs[i].id).style.marginLeft = ((chartWidth*2)/parameters.length)*i+10 + "px";
    }
    document.getElementById(elementName).style.height = chartHeight + "px";
	
    document.getElementById(elementName).style.visibility = "visible";
  }
  document.location = '#' + elementName + '_top';
}

function hide()
{
	document.getElementById('chartHolder').innerHTML = "";
	document.getElementById('xy').innerHTML = "";
}

/**
 * Draw the chart
 **/
function prepareNdrawChart (rootElement,fieldName)
{
	hide();
	var parameters = new Array(); //name -> value

	var listElement = rootElement.getElementsByTagName('li');
	var j=0;
	var maxNo = 0;
	for(var i=0; i<listElement.length; i++) 
	{
		//var anchorElement = listElement[i].get
		parameters[j] = listElement[i].getAttribute("name");
		parameters[j+1] = listElement[i].getAttribute("count");

		if (parseInt(parameters[j+1]) > parseInt(maxNo))
		{
			maxNo = parameters[j+1];
		}

		j = j + 2;
		//alert(listElement[i].getAttribute("name") + "::::" + listElement[i].getAttribute("count"));
	}
	drawNewChart('Level', fieldName + ' Chart','Results',  fieldName,maxNo,parameters);
}

function insertUpdateURL(key, value)
{
    key = escape(key); 
	value = escape(value);
    var kvp = document.location.search.substr(1).split('&');

    var i=kvp.length; var x; while(i--) 
    {
        x = kvp[i].split('=');
        if (x[0]==key)
        {
                x[1] = value;
                kvp[i] = x.join('=');
                break;
        }
    }

    if(i<0) 
	{
		kvp[kvp.length] = [key,value].join('=');
	}
    //this will reload the page, it's likely better to store this until finished
    document.location.search = kvp.join('&'); 
}


function similarResult(level, appName,user)
{
	var url = "";
	var firstTime = 1;
	if (level != null && level != '')
	{
		url = url + ' Level:"' + level + '"';
		firstTime = 0;
	}

	if (appName != null && appName != '')
	{
		if (firstTime == 0)
		{
			url= url + ' AND ';
		}
		url = url + ' ApplicationName:"' + appName + '"';
		firstTime = 0;
	}
	
	if (user != null && user != '')
	{
		if (firstTime == 0)
		{
			url= url + ' AND ';
		}
		url = url + ' User:"' + user + '"';
		firstTime = 0;
	}
	insertUpdateURL("fq",url);
}


function toggleElement(spanId)
{
	var element = document.getElementById(spanId);
	if (element != null)
	{
		if (element.style.display == 'none')
		{
			element.style.display = 'block';
		}
		else
			element.style.display = 'none';
	}

}

