var ofc_get_data_vars = Array();
var ofc_get_data_timeid = Array();
function ofc_get_data_jsvar(id) {
	return ofc_get_data_vars[id];
}

function save_image(divName) {
	//alert(divName);
	var ofcDivName = divName;//+ "_ofc";
	var ofc = findSWF(ofcDivName);
	var imageStr = "<img src='data:image/png;base64," + ofc.get_img_binary() + "' />";
	//alert(imageStr);
	
  var img_win = window.open('', 'Charts: Export as Image');
  img_win.document.write('<html><head><title>Charts: Export as Image<\/title><\/head><body>' + imageStr + '<\/body><\/html>');
	// stop the 'loading...' message
	img_win.document.close();
}

function ofcCreate(divName, ofcWidth, ofcHeight, ofcData) 
{
	ofc_get_data_vars[divName] = JSON.stringify(ofcData);
	
    swfobject.wmode="transparent";
    swfobject.embedSWF("/IWM_CAT/js/openflashchart2.0/open-flash-chart.swf",
     divName, ofcWidth, ofcHeight,
     "9.0.0","expressInstall.swf",
     {"get-data" : ofc_get_data_jsvar, "id" : divName},
	 {"wmode" : "transparent"}
	 );	   
	
	 timerID = setInterval("reload('"+divName+"')",1500);
	 ofc_get_data_timeid[divName] = timerID;
	 //alert("divName:"+divName+",timeID:"+timerID);
	// window.clearInterval(timerID); 
	 
	 
}
function reload(divName){

	  var ofc = findSWF(divName);
	 	if(ofc ==null || ofc.type ==null ||(typeof ofc.load == 'undefined'))
	 	{
	 		//alert("sleep");
	 		ofc = findSWF(divName);
	 	}else
	 	{
	 		//alert("reload");	 		
	 		ofc.load(ofc_get_data_vars[divName]);	
	 		clearInterval(ofc_get_data_timeid[divName]);
	 	}
}


function ofcCreateLarge(divName, url) 
{
	var lond = "<img src='images/load56.gif' >";
	var ofc = new OfcAjaxObj();
	//var lond = "loading.....";
	ofc.ofcCallBackfunc(url,divName,lond,ofcLarger);	
}

function ofcCreateSmall(divName, url) 
{
	var lond = "<img src='images/load56.gif' >";
	//var lond = "loading.....";
	var ofc = new OfcAjaxObj();
	ofc.ofcCallBackfunc(url,divName,lond,ofcSmall);
	
}

var widthOfLarger = 750;
var heightOfLarger = 207;
function setSizeLarger(w,h)
{
	widthOfLarger = w;
	heightOfLarger = h;
}

function ofcLarger(divName,v)
{
	if(v.indexOf("JavaMessage:")>-1)
	{
		v=v.substring(v.indexOf("JavaMessage:")+12);
		alert(v);
		return ;
	}
	if(v.indexOf("Exception")>-1)
	{		
		if(document.getElementById(divname+"_londing") != null)
			{
				document.getElementById(divname+"_londing").innerHTML = londing_txt;	
			}		
		var ofcd = document.getElementById(divName);
		
		try{
			if(ofcd == null || ofcd.type ==null || (typeof ofcd.load == 'undefined'))
			{
				ofcd.innerHTML = "";	
			}else
			{
				ofcd.load(JSON.stringify(""));		
			}
			
		}catch(e) {}

		return;	
	}

	var date = JSON.parse(v);	
	var ofc = findSWF(divName);
	if(ofc == null || ofc.type ==null || (typeof ofc.load == 'undefined'))
	{
		//alert("create");
		ofcCreate(divName, widthOfLarger,heightOfLarger , date);		
	}else
	{
		//alert("reload");
		ofc.load(JSON.stringify(date));		
	}
}  
var widthOfSmall_List = Array();  
var heightOfSmall_List = Array();
var widthOfSmall = 360;
var heightOfSmall = 200;
function setSizeSmallDiv(divname,w,h)
{
	widthOfSmall_List[divname] = w;
	heightOfSmall_List[divname] = h;

}
function setSizeSmall(w,h)
{
	widthOfSmall = w;
	heightOfSmall = h;
}
function ofcSmall(divName,v)
{
	if(v.indexOf("JavaMessage:")>-1)
	{
		v=v.substring(v.indexOf("JavaMessage:")+12);
		alert(v);
		return ;
	}
	if(v.indexOf("Exception")>-1)
	{		
		
		return;	
	}
	var date = JSON.parse(v);
	
	var ofc = findSWF(divName);
	if(ofc ==null || ofc.type ==null || (typeof ofc.load == 'undefined'))
	{
		//alert("create");
		
		if(widthOfSmall_List[divName]!=null)widthOfSmall = widthOfSmall_List[divName];
		if(heightOfSmall_List[divName]!=null)heightOfSmall = heightOfSmall_List[divName];
		//heightOfSmall=138;		//Kim update for now size
		ofcCreate(divName, widthOfSmall, heightOfSmall, date);		
	}else
	{
		try{
			ofc.load(JSON.stringify(date));
		}catch(e)
		{
			if(widthOfSmall_List[divName]!=null)widthOfSmall = widthOfSmall_List[divName];
			if(heightOfSmall_List[divName]!=null)heightOfSmall = heightOfSmall_List[divName];
			ofcCreate(divName, widthOfSmall, heightOfSmall, date);
		}
				
	}
}  
  
function findSWF(movieName) 
{
	if (navigator.appName.indexOf("Microsoft")!= -1) 
	{
		//return window[movieName];
		return document.getElementById(movieName);
	} 
	else 
	{
		//return document[movieName];
		return document.getElementById(movieName);
	}
} 
  
function OfcAjaxObj() {
	var xmlHttp;
	var divname;
	var callback;
	this.createXMLHttpRequest = function () {
		if (window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
		} else {
			if (window.ActiveXObject) {
				try {
					xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
				}
				catch (e) {
					try {
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e) {
						newsstring = "error";
					}
				}
			}
		}
	};
	
	this.ofcCallBackfunc = function (url, londing_div, londing_txt, hcallback) {
		callback = hcallback;
		divname = londing_div;
		if (londing_txt != "") {
			if(document.getElementById(divname+"_londing") != null)
			{
				document.getElementById(divname+"_londing").innerHTML = londing_txt;	
			}
			
		}
		if (url.indexOf("?") > -1) {
			url = url + "&random=" + Math.random();
		} else {
			url = url + "?random=" + Math.random();
		}
		this.createXMLHttpRequest();
		xmlHttp.onreadystatechange = this.ofcHandleCallback;
		xmlHttp.open("GET", url);
		xmlHttp.send(null);    
		//return xmlHttp.responseText;
	};
	this.ofcHandleCallback = function () {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {				
		
				callback(divname,xmlHttp.responseText);
				if(document.getElementById(divname+"_londing") !=null)
				{
					document.getElementById(divname+"_londing").innerHTML = "";
				}
			}
		}
	};
	
}  

//-----relaod larger-------------

function ofcCreateReload(divName, ofcWidth, ofcHeight, ofcData) 
{
	
	ofc_get_data_vars[divName] = JSON.stringify(ofcData);
	
    swfobject.wmode="transparent";
    swfobject.embedSWF("/IWM_CAT/js/openflashchart2.0/open-flash-chart.swf",
     divName, ofcWidth, ofcHeight,
     "9.0.0","expressInstall.swf",
     {"get-data" : ofc_get_data_jsvar, "id" : divName},
	 {"wmode" : "transparent"}
	 );	   
	 
	 var ofc = findSWF(divName);
	 
	 timerID = setInterval(function reload(){
	 
	 	if(ofc ==null || ofc.type ==null ||(typeof ofc.load == 'undefined'))
	 	{
	 		//alert("sleep");
	 		ofc = findSWF(divName);
	 	}else
	 	{
	 		//alert("reload");
	 		ofc.load(JSON.stringify(ofcData));	
	 		clearTimeout(timerID);
	 	}
	 
	 },1500);	 
}
function ofcCreateLarge2(divName, url) 
{
	var lond = "<img src='../images2.0/load56.gif' >";
	var ofc = new OfcAjaxObj();
	//var lond = "loading.....";
	ofc.ofcCallBackfunc(url,divName,lond,ofcLarger2);	
}
function ofcLarger2(divName,v)
{
	if(v.indexOf("Exception")>-1)
	{
		alert(v);
		return ;
	}
	var date = JSON.parse(v);	
	var ofc = findSWF(divName);
	if(ofc == null || ofc.type ==null)
	{
		alert("create");
		ofcCreateReload(divName, widthOfLarger, heightOfLarger, date);		
	}else
	{
		alert("reload");
		ofc.load(JSON.stringify(date));		
	}
} 


function   sleep(n)   
{   
  var   start=new   Date().getTime();   
  while(true)   if(new   Date().getTime()-start>n)   break;   
}   
