var actualChart = 0;
var imgNameArr=[];
var empcode = "";
var subjectName="";
function createFusionChart(jsonObj, containerId, chartContainer,width,height,imageName,userId,rptSubject)
	{
	//jsonObj='{"dataset":[{"valuePosition":"BELOW", "color":"#01DF3A", "data":[{"value":"19", "showValue":"1", "link":"p-detailsPopUp,width$1000,height$500,toolbar$no,scrollbars$yes,resizable$no-/ibase/dwh/GwtDashboardPage.html?metadataname=monthwiseactual&PRD_SALESDASH=201503&titleName=-Manufactured and Released Batches for Mar-2015&actual_batches_released=19&prd_code=Mar-2015"},{"value":"", "showValue":"1", "link":"p-detailsPopUp,width$1000,height$500,toolbar$no,scrollbars$yes,resizable$no-/ibase/dwh/GwtDashboardPage.html?metadataname=monthwiseactual&PRD_SALESDASH=201503&titleName=-Manufactured and Released Batches for In METIS Not in LIMS&actual_batches_released=&prd_code=In METIS Not in LIMS"}], "seriesname":"Released"},{"valuePosition":"BELOW", "color":"#FF0000", "data":[{"value":"33", "showValue":"1", "link":"p-detailsPopUp,width$1000,height$500,toolbar$no,scrollbars$yes,resizable$no-/ibase/dwh/GwtDashboardPage.html?metadataname=monthwisepending&PRD_SALESDASH=201503&titleName=-Manufactured and Released Batches for Mar-2015&pending_batches=33&prd_code=Mar-2015"},{"value":"", "showValue":"1", "link":"p-detailsPopUp,width$1000,height$500,toolbar$no,scrollbars$yes,resizable$no-/ibase/dwh/GwtDashboardPage.html?metadataname=monthwisepending&PRD_SALESDASH=201503&titleName=-Manufactured and Released Batches for In METIS Not in LIMS&pending_batches=&prd_code=In METIS Not in LIMS"}], "seriesname":"Not Released"},{"valuePosition":"BELOW", "color":"#FFFF00", "data":[{"value":"101", "showValue":"1", "link":"p-detailsPopUp,width$1000,height$500,toolbar$no,scrollbars$yes,resizable$no-/ibase/dwh/GwtDashboardPage.html?metadataname=tbdbatch&PRD_SALESDASH=201503&titleName=-Manufactured and Released Batches for Mar-2015&tbd_batches=101&prd_code=Mar-2015"},{"value":"149", "showValue":"1", "link":"p-detailsPopUp,width$1000,height$500,toolbar$no,scrollbars$yes,resizable$no-/ibase/dwh/GwtDashboardPage.html?metadataname=tbdbatch&PRD_SALESDASH=201503&titleName=-Manufactured and Released Batches for In METIS Not in LIMS&tbd_batches=149&prd_code=In METIS Not in LIMS"}], "seriesname":"In METIS Not in LIMS "}], "categories":{"category":[{"label":"Mar-2015"},{"label":"In METIS Not in LIMS"}]}, "chart":[{"caption":"HALOL - Manufactured & Released Batches-Timescale  (06/04/2015)", "bgcolor":"", "showLegend":"1", "useroundedges":"1", "rotateValues":"0", "decimals":"1", "formatNumberScale":"0", "formatNumber":"1", "exportEnabled":"1", "exportAtClient":"0", "exportHandler":"FCExporter", "exportAction":"save", "xAxisName":"Production Completion Month/Target Release Week ", "yAxisName":"Number Of Batches", "anchorRadius":"0", "lineAlpha":"1", "anchorBorderThickness":"3", "pyaxisname":"Number of Batches", "syaxisname":"Number of Batches"}]}';
	//off jsonObj='{ "chart": { "caption": "Sales Comparison by Country - 2011 vs 2012 vs 2013", "subcaption": "(Right-click to see the export modes available)", "exportEnabled": "1", "exportatclient": "0", "exportHandler":"FCExporter", "exportAction":"save",  "bgcolor": "FFFFFF", "numberprefix": "$", "showvalues": "0", "plotgradientcolor": "", "showalternatehgridcolor": "0", "showplotborder": "0", "labeldisplay": "WRAP", "divlinecolor": "CCCCCC", "canvasborderalpha": "0" }, "categories": [ { "category": [ { "label": "Austria" }, { "label": "Brazil" }, { "label": "France" }, { "label": "Germany" }, { "label": "USA" } ] } ], "dataset": [ { "seriesname": "2011", "color": "008ee4", "data": [ { "value": "25601.34" }, { "value": "20148.82" }, { "value": "17372.76" }, { "value": "35407.15" }, { "value": "38105.68" } ] }, { "seriesname": "2012", "color": "6baa01", "data": [ { "value": "57401.85" }, { "value": "41941.19" }, { "value": "45263.37" }, { "value": "117320.16" }, { "value": "114845.27" } ] }, { "seriesname": "2013", "color": "f8bd19", "data": [ { "value": "45000.65" }, { "value": "44835.76" }, { "value": "18722.18" }, { "value": "77557.31" }, { "value": "92633.68" } ] } ] }';
	 try
		{
			srcImagesPath="FusionChart/images/"+imageName+".swf";    		
    		if(imageName=="heatmap")
    		{
    			var myChart = new FusionCharts({ type: 'heatmap', dataFormat: 'json' }); 
    			myChart.resizeTo( height,width );
    		}
    		//start of india map condition by swati
    		if(imageName=="india")
    		{
    			var myChart = new FusionCharts({ type: 'india', dataFormat: 'json' }); 
    			myChart.resizeTo( height,width );		         		
    		}
    		//end of india map condition by swati
    		else if(imageName!="heatmap" && imageName!="india") 
    		{
    			/*var myChart = new FusionCharts({ type: imageName, dataFormat: 'json' });
    			myChart.resizeTo( height,width );*/
    			var myChart = new FusionCharts(srcImagesPath, "myChartId"+containerId+userId, height, width, "0", "0");
    			imgNameArr.push("myChartId"+containerId+userId);
    			empcode     = userId; 
    			subjectName =rptSubject;    			
    		}
            myChart.setJSONData(jsonObj);
    	    myChart.render(chartContainer);    	    
    	    if(userId !=null && userId!="")
    	    {
    	           myChart.addEventListener("Rendered", function () 
    				{ 
               	    	 actualChart++;
               	    	 
    	    	     	 var cObject = getChartFromId('myChartId'+containerId+userId);
    				 	 if(cObject.hasRendered()) 
    				        cObject.exportChart({exportFormat: 'png',exportFileName:"myChartId"+containerId+userId});
    				 	 
    				 	//imgNameArr[actualChart] = "myChartId"+chartContainer;
    				 	//imgNameArr.push("myChartId"+chartContainer);

    		 	 	}); 
    	    
		     }
		}
	 
	 
		catch(e)
		{
			alert( "Excpeiton :: createFusionChart  ["+e.message+"]" );
		}    	
	}

//var userID    ="E31810";

var remark    = "The left side of the graph contains the month of completion of the manufacturing process of the batches and the laboratory status, green contains the number of the batches that were released by the lab., the red contains the number of batches that weren't yet released, Yellow contains the number of batches that have finished the production and the samples didn't yet reached the lab.The right side of the graph contains the estimated week of release of the red batches that appear in the left side of the graph.";
function getXMLHttpRequest()
{  
	  var xmlHttpReq;  
	  // to create XMLHttpRequest object in non-Microsoft browsers  
	  if (window.XMLHttpRequest) {  
	    xmlHttpReq = new XMLHttpRequest();  
	  } else if (window.ActiveXObject) {  
	    try {  
	      //to create XMLHttpRequest object in later versions of Internet Explorer  
	      xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");  
	    } catch (exp1) {  
	      try {  
	        //to create XMLHttpRequest object in later versions of Internet Explorer  
	        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");  
	      } catch (exp2) {  
	        //xmlHttpReq = false;  
	        alert("Exception in getXMLHttpRequest()!");  
	      }  
	    }  
	  }  
	  return xmlHttpReq;  
	}  
	  
	/* 
	* AJAX call starts with this function 
	*/ 
	 
function makeRequest(userID,subjectName) 
{  
  if(userID!=""&& userID.length>0)
  {
	  var rptTitle       = ""+subjectName+"-Manufactured and Released Batches for ";		 
	  var xmlHttpRequest = getXMLHttpRequest();  
	  xmlHttpRequest.onreadystatechange = getReadyStateHandler(xmlHttpRequest);  
	  xmlHttpRequest.open("POST","/ibase/QCBatchExportPDFServlet?userID="+userID+"&remark="+remark+"&rptTitle="+rptTitle+"&imgNameList="+imgNameArr,true);    
	  xmlHttpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	  xmlHttpRequest.send(null);  
   	}  
	   	
}

	
function getReadyStateHandler(xmlHttpRequest) 
{  
	  return function() 
	  {  
	    if (xmlHttpRequest.readyState == 4) 
		    {  
			
	      if (xmlHttpRequest.status == 200) 
		    {  
	      } 
	      else 
		      {  
	           alert("Http error " + xmlHttpRequest.status + ":" + xmlHttpRequest.statusText);  
	           }  
	    }  
	  };  
}  

var chartCnt=0;
function FC_Exported(objRtn)
{
	
    if (objRtn.statusCode=="1")
    {
    	chartCnt++;		
    	if(chartCnt==actualChart)
    	{
        makeRequest(empcode,subjectName);
	    //window.open('', '_parent', '');       
        window.close();
    	}
    }
    else
    {
        alert("The chart with DOM Id " + objRtn.DOMId + " could not be saved on server. There was an error. Description : " + objRtn.statusMessage);
    }
}
 
