/** 
 * renderChart renders FusionCharts with JavaScript + HTML code required to embed a chart.
 *
 * This function returns an HTML with a DIV and a SCRIPT tag. The DIV contains an ID which 
 * is set in this format $chartId + "DIV". Thd DIV is followed by a JavaScript snippet
 * that contains FusionCharts JavaScript class' constructor function - new FusionCharts(),
 *
 * @param	chartSWF				String  - SWF File Name (and Path) of the chart which you intend to plot
 * @param	dataUrl				String  - If you intend to use dataUrl method (XML or JSON as Url), pass the URL 
 *											as this parameter. Otherwise, set it to "" (in case of dataStr method)
 * @param	dataStr				String  - If you intend to use dataStr method (embedded XML or JSON), pass the 
 *											XML/JSON data as this parameter. Otherwise, set it to "" (in case of dataUrl method)
 * @param	chartId				String  - Id for the chart, using which it will be recognized in the HTML page. 
 *														Each chart on the page needs to have a unique Id.
 * @param	chartWidth			String  - Intended width for the chart  (in pixels WITHOUT px suffix or in percent)
 * @param	chartHeight			String  - Intended height for the chart (in pixels WITHOUT px suffix or in percent)
 * @param	debugMode			Boolean - Whether to start the chart in debug mode
 * @param	registerWithJS		Boolean - Whether to ask chart to register itself with JavaScript
 * @param	allowTransparent	Boolean - Whether to allow the chart to have transparent background. Additionally this set the chart to get rendered in opaque mode
 *
 * @return	Chart HTML + JavaScript code to be added into web page as String
 */
function renderChart($chartSWF, $dataUrl, $dataStr, $chartId, $chartWidth, $chartHeight, $debugMode = false, $registerWithJS = true, $allowTransparent = false)
{
    $dataFormat = (FC_GetConfiguration("dataFormat") == "" ? "xml" : FC_GetConfiguration("dataFormat")) . ($dataStr == "" ? "url" : "");
    if (FC_GetConfiguration("renderAt") == "") {
        FC_SetConfiguration("renderAt", "{$chartId}Div");
    }
    FC_SetConfiguration("swfUrl", $chartSWF);
    FC_SetConfiguration("dataFormat", $dataFormat);
    FC_SetConfiguration("id", $chartId);
    FC_SetConfiguration("width", $chartWidth);
    FC_SetConfiguration("height", $chartHeight);
    if ($debugMode) {
        FC_SetConfiguration("debugMode", boolToNum($debugMode));
    }
    $wmode = FC_GetConfiguration("forcedwmode", "constants");
    if ($wmode == "") {
        $wmode = $allowTransparent ? "transparent" : "opaque";
    }
    if ($wmode != "") {
        FC_SetConfiguration("wmode", $wmode);
    }
    $dataSource = @preg_replace("/[\n\r]/", "", $dataStr == "" ? $dataUrl : $dataStr);
    $datasource_json = " \"dataSource\" : " . ($dataFormat == "json" ? $dataSource : "\"{$dataSource}\"");
    $chart_config_json = "{ " . fc_encode_json(FC_GetConfiguration("params")) . ", {$datasource_json} }";
    //First we create a new DIV for each chart. We specify the id of DIV as "chartId" + Div.
    //DIV names are case-sensitive.
    // The Steps in the script block below are:
    //
    //  1) In the DIV the text "Chart" is shown to users before the chart has started loading
    //    (if there is a lag in relaying SWF from server). This text is also shown to users
    //    who do not have Flash Player installed. You can configure it as per your needs.
    //
    //  2) The chart is rendered using FusionCharts Class. Each chart's instance (JavaScript) Id
    //     is named as chart_"chartId".
    //
    //  3) Check whether we've to provide data using dataStr method or dataUrl method
    //     save the data for usage below
    // generate js code for data provider
    // build FusionCharts HTML + JS
    $chart_HTML_JS = <<<HTML_JS
<!-- START Code Block for Chart {$chartId} -->
<div id="{$chartId}Div"> Chart </div>
<script type="text/javascript" ><!--
\t// Instantiate the Chart 
\tvar chart_{$chartId} = new FusionCharts( {$chart_config_json} ).render();
// --></script>
<!-- END Script Block for Chart {$chartId} -->

HTML_JS;
    __FC_INITIALIZE__();
    // return HTML + JS
    return $chart_HTML_JS;
}
/** 
 * renderChart renders FusionCharts with JavaScript + HTML code required to embed a chart.
 *
 * This function returns an HTML with a DIV and a SCRIPT tag. The DIV contains an ID which 
 * is set in this format $chartId + "DIV". Thd DIV is followed by a JavaScript snippet
 * that contains FusionCharts JavaScript class' constructor function - new FusionCharts(),
 *
 * @param	chartSWF				String  - SWF File Name (and Path) of the chart which you intend to plot
 * @param	dataUrl				String  - If you intend to use dataUrl method (XML or JSON as Url), pass the URL 
 *											as this parameter. Otherwise, set it to "" (in case of dataStr method)
 * @param	dataStr				String  - If you intend to use dataStr method (embedded XML or JSON), pass the 
 *											XML/JSON data as this parameter. Otherwise, set it to "" (in case of dataUrl method)
 * @param	chartId				String  - Id for the chart, using which it will be recognized in the HTML page. 
 *														Each chart on the page needs to have a unique Id.
 * @param	chartWidth			String  - Intended width for the chart  (in pixels WITHOUT px suffix or in percent)
 * @param	chartHeight			String  - Intended height for the chart (in pixels WITHOUT px suffix or in percent)
 * @param	debugMode			Boolean - Whether to start the chart in debug mode
 * @param	registerWithJS		Boolean - Whether to ask chart to register itself with JavaScript
 * @param	allowTransparent	Boolean - Whether to allow the chart to have transparent background. Additionally this set the chart to get rendered in opaque mode
 *
 * @return	Chart HTML + JavaScript code to be added into web page as String
 */
function renderChart($chartSWF, $dataUrl, $dataStr, $chartId, $chartWidth, $chartHeight, $debugMode = false, $registerWithJS = true, $allowTransparent = false)
{
    // select data format xml/json/xmlurl/jsonurl
    $dataFormat = (FC_GetConfiguration("dataFormat") == "" ? "xml" : FC_GetConfiguration("dataFormat")) . ($dataStr == "" ? "url" : "");
    // if renderAt is specified explicitely skip creation of DIV
    $renderAtDiv = "<!-- The chart will be rendered inside div having id = " . @FC_GetConfiguration("renderAt") . " -->";
    if (FC_GetConfiguration("renderAt") == "") {
        $renderAtDiv = "<div id=\"{$chartId}Div\">Chart</div>";
        FC_SetConfiguration("renderAt", "{$chartId}Div");
    }
    // set swf
    FC_SetConfiguration("swfUrl", $chartSWF);
    // set dataformat
    FC_SetConfiguration("dataFormat", $dataFormat);
    // set id
    FC_SetConfiguration("id", $chartId);
    //set width
    FC_SetConfiguration("width", $chartWidth);
    // set height
    FC_SetConfiguration("height", $chartHeight);
    // if debug mode is set on set it on
    if ($debugMode) {
        FC_SetConfiguration("debugMode", boolToNum($debugMode));
    }
    // selection of wmode
    // If wmode is set explicitely using FC_SetWindowMode it sets a reserved setting called forcedwmode
    // The setting forcedwmode takes priority over $allowTransparent parameter
    // If $allowTransparent parameter is set to true take transparent as wmode and set it
    $wmode = FC_GetConfiguration("forcedwmode", "constants");
    if ($wmode == "") {
        $wmode = $allowTransparent ? "transparent" : "";
    }
    if ($wmode != "") {
        FC_SetConfiguration("wmode", $wmode);
    }
    // remove all nwelines from dataSource
    $dataSource = @preg_replace("/[\n\r]/", "", $dataStr == "" ? $dataUrl : $dataStr);
    // Set dataSource JS property. If JSON string is passed the value is set directly without enclosing in quoted
    // This would provide a proper JSON to the chart
    // For the rest of the cases the values are set enclosed with quotes
    $datasource_json = " \"dataSource\" : " . ($dataFormat == "json" ? $dataSource : "\"{$dataSource}\"");
    // parse all chart config and set it to JSON Object for new JavaScript constructor
    $chart_config_json = "{ " . fc_encode_json(FC_GetConfiguration("params")) . ", {$datasource_json} }";
    //First we create a new DIV for each chart. We specify the id of DIV as "chartId" + Div.
    //DIV names are case-sensitive.
    // The Steps in the script block below are:
    //
    //  1) In the DIV the text "Chart" is shown to users before the chart has started loading
    //    (if there is a lag in relaying SWF from server). This text is also shown to users
    //    who do not have Flash Player installed. You can configure it as per your needs.
    //
    //  2) The chart is rendered using FusionCharts Class. Each chart's instance (JavaScript) Id
    //     is named as chart_"chartId".
    //
    //  3) Check whether we've to provide data using dataStr method or dataUrl method
    //     save the data for usage below
    // generate js code for data provider
    // build FusionCharts HTML + JS
    $chart_HTML_JS = <<<HTML_JS
<!-- START Code Block for Chart {$chartId} -->
{$renderAtDiv}
<script type="text/javascript" ><!--
\t// Instantiate the Chart 
\tif ( FusionCharts("{$chartId}") && FusionCharts("{$chartId}").dispose ) FusionCharts("{$chartId}").dispose();
\tvar chart_{$chartId} = new FusionCharts( {$chart_config_json} ).render();
// --></script>
<!-- END Script Block for Chart {$chartId} -->

HTML_JS;
    __FC_INITIALIZE__();
    // return HTML + JS
    return $chart_HTML_JS;
}