<?php
if(!isset($_SESSION["seas_brd_language"]))    $_SESSION["seas_brd_language"]=1;
if(isset($_REQUEST['lan'])) $_SESSION["seas_brd_language"]=$_REQUEST['lan'];

include_once("pcd/config.php");
include_once("pcd/data_definitions.php");
include_once("pcd/print_section1.php");
error_reporting(0);
//Handle sessions
if(!isset($_SESSION['seas_user_id'])) $_SESSION['seas_user_id']=0;
if($_SESSION['seas_user_id'] <=0) {
	//$_SESSION['PCD_page_ref']="brandanalysis";
	header("location:home");
}

$font_increase_value2 = 0;
if($_SESSION["seas_brd_language"]==2) {
	$font_increase_value=3;
	$font_increase_value2=1;
}
else {
	$font_increase_value=0;
}



$usr_opts = explode(",",$_SESSION['PCD_user_opts']);

//if(!isset($_SESSION["seas_brd_filter_prof"]))    
	$_SESSION["seas_brd_filter_prof"]=array();
//if(!isset($_SESSION["seas_brd_filter_hlth"]))    
	$_SESSION["seas_brd_filter_hlth"]=array();
//if(!isset($_SESSION["seas_brd_filter_sel_target"]))  
	$_SESSION["seas_brd_filter_sel_target"]=0;
//if(!isset($_SESSION["seas_brd_filter_sel_bnch"]))    
	$_SESSION["seas_brd_filter_sel_bnch"]=0;

$_SESSION["seas_brd_filter_text"]="";
$_SESSION["seas_brd_target_sample_text"]="";
$_SESSION["seas_brd_benchmark_sample_text"]="";
						  

if(!isset($_SESSION["seas_brd_filter_topic"]))   $_SESSION["seas_brd_filter_topic"]=99;
if(!isset($_SESSION["seas_brd_filter_export"]))  $_SESSION["seas_brd_filter_export"]=0;

if(!isset($_SESSION["seas_brd_filter_condition"]))  $_SESSION["seas_brd_filter_condition"]="";
if(!isset($_SESSION["seas_brd_bnch_filter_condition"]))  $_SESSION["seas_brd_bnch_filter_condition"]="";

if(isset($_REQUEST['sel_prof_catg']))     $_SESSION["seas_brd_filter_prof"]   = $_REQUEST['sel_prof_catg'];
if(isset($_REQUEST['sel_health_fac']))    $_SESSION["seas_brd_filter_hlth"]   = $_REQUEST['sel_health_fac'];
if(isset($_REQUEST['sel_prof_catg']))     $_SESSION["seas_brd_filter_prof"]   = $_REQUEST['sel_prof_catg'];
if(isset($_REQUEST['sel_target']))     	  $_SESSION["seas_brd_filter_sel_target"]   = $_REQUEST['sel_target'];
if(isset($_REQUEST['sel_bnch']))     	  $_SESSION["seas_brd_filter_sel_bnch"]   = $_REQUEST['sel_bnch'];
if(isset($_REQUEST['sel_survey_topic']))  $_SESSION["seas_brd_filter_topic"]  = $_REQUEST['sel_survey_topic'];
if(isset($_REQUEST['sel_export']))        $_SESSION["seas_brd_filter_export"] = $_REQUEST['sel_export'];


if(isset($_REQUEST['clear_filter'])) {
	$_SESSION["seas_brd_filter_condition"]="";
	$_SESSION["seas_brd_filter_hlth"]="";
	$_SESSION["seas_brd_filter_prof"]="";
	$_SESSION["seas_brd_filter_sel_target"]=0;
	$_SESSION["seas_brd_filter_sel_bnch"]=0;
}
/*
if($_SESSION["seas_brd_filter_sel_target"] >0 || $_SESSION["seas_brd_filter_sel_bnch"] >0) {
	$_SESSION["seas_brd_filter_prof"]=0;
	$_SESSION["seas_brd_filter_hlth"]=0;
}
*/

$filter_condition="";
$_SESSION["seas_brd_filter_condition"]="";
if(count($_SESSION["seas_brd_filter_prof"]) >0) {
	$prof_cond="";
	$prof_cond_text="";
	foreach($_SESSION["seas_brd_filter_prof"] as $prof) {
		if(strlen($prof_cond)>0) $prof_cond .= ",";
		if(strlen($prof_cond_text)>0) $prof_cond_text .= ", "; else $prof_cond_text .= $language_array['category'] . " =";
		 $prof_cond .=  $prof;
		 $prof_cond_text .=  $catg_filter[$prof];
	}
	
	if(strlen($prof_cond)>0)  {
		$_SESSION["seas_brd_filter_condition"] = " (V1 in (" . $prof_cond . ")) ";
		$_SESSION["seas_brd_filter_text"] = " [". $prof_cond_text  . "] ";
	}
}
if(count($_SESSION["seas_brd_filter_hlth"]) >0) {
	$hlth_cond="";
	$hlth_cond_text="";
	foreach($_SESSION["seas_brd_filter_hlth"] as $hlth) {
		if(strlen($hlth_cond)>0) $hlth_cond .= ",";
		if(strlen($hlth_cond_text)>0) $hlth_cond_text .= ", "; else $hlth_cond_text .= $language_array['facility'] . "=";
		 $hlth_cond .= $hlth;
		 $hlth_cond_text .= $fac_filter[$hlth];
	}
	
	if(strlen($hlth_cond)>0)  {
		if(strlen($_SESSION["seas_brd_filter_condition"]) >0) $_SESSION["seas_brd_filter_condition"] .= " and ";
		if(strlen($_SESSION["seas_brd_filter_text"]) >0) $_SESSION["seas_brd_filter_text"] .= " <br> ";
		$_SESSION["seas_brd_filter_condition"] .= "  (V5 in (" . $hlth_cond . ")) ";
		$_SESSION["seas_brd_filter_text"] .= " [". $hlth_cond_text  . "] ";
	}
}

if(strlen($_SESSION["seas_brd_filter_condition"]) >0) {
	$_SESSION["seas_brd_filter_condition"] = " (" . $_SESSION["seas_brd_filter_condition"] . ") ";
}
if(strlen($_SESSION["seas_brd_filter_text"]) >0) {
	$_SESSION["seas_brd_filter_text"] = " ". $language_array['data_filtered_by'] .": " . $_SESSION["seas_brd_filter_text"] . " ";
}

$filter_condition=$_SESSION["seas_brd_filter_condition"];

if($_SESSION["seas_brd_filter_sel_target"] >0) {
	$tr_condition = "";
	$tr_sql  = "select distinct dfields from seas_brd_target_dat where tar_id=" . $_SESSION["seas_brd_filter_sel_target"];
	$tr_rslt = mysql_query($tr_sql);
	while($tr_rw=mysql_fetch_assoc($tr_rslt)) {
		$tr_opts="";
		$tr_sql2  = "select distinct opts from seas_brd_target_dat where tar_id=" . $_SESSION["seas_brd_filter_sel_target"] . ' and dfields = "'. $tr_rw['dfields'].'"';
		$tr_rslt2 = mysql_query($tr_sql2);
		while($tr_rw2=mysql_fetch_assoc($tr_rslt2)) {
			if(strlen($tr_opts)>0) $tr_opts .= ",";
			$tr_opts .= $tr_rw2['opts'];
		}
		if(strlen($tr_condition)>0) $tr_condition .=  " AND ";
		$tr_condition .= " (" . $tr_rw['dfields'] ." in (" . $tr_opts . ")) ";
	}
	
	if(strlen($tr_condition)>0) $tr_condition = " (" . $tr_condition . ") ";
	if(strlen($filter_condition)>0) $tr_condition = $filter_condition . " AND " . $tr_condition;
	$_SESSION["seas_brd_filter_condition"] = $tr_condition;
}

$_SESSION["seas_brd_bnch_filter_condition"] = "";
if($_SESSION["seas_brd_filter_sel_bnch"] >0) {
	$bn_condition = "";
	$bn_sql  = "select distinct dfields from seas_brd_target_dat where tar_id=" . $_SESSION["seas_brd_filter_sel_bnch"];
	$bn_rslt = mysql_query($bn_sql);
	while($bn_rw=mysql_fetch_assoc($bn_rslt)) {
		$bn_opts="";
		$bn_sql2  = "select distinct opts from seas_brd_target_dat where tar_id=" . $_SESSION["seas_brd_filter_sel_bnch"] . ' and dfields = "'. $bn_rw['dfields'].'"';
		$bn_rslt2 = mysql_query($bn_sql2);
		while($bn_rw2=mysql_fetch_assoc($bn_rslt2)) {
			if(strlen($bn_opts)>0) $bn_opts .= ",";
			$bn_opts .= $bn_rw2['opts'];
		}
		if(strlen($bn_condition)>0) $bn_condition .=  " AND ";
		$bn_condition .= " (" . $bn_rw['dfields'] ." in (" . $bn_opts . ")) ";
	}
	
	if(strlen($bn_condition)>0) $bn_condition = " (" . $bn_condition . ") ";
	if(strlen($filter_condition)>0) $bn_condition = $filter_condition . " AND " . $bn_condition;
	$_SESSION["seas_brd_bnch_filter_condition"] = $bn_condition;
}
// End Sessions

// Computations
$rmode = true; // Computation mode: True for Weighted

// Total Samples
$t_sql = "select sum(weight) from seas_brd_dat";
$t_rslt= mysql_query($t_sql);
$t_array = mysql_fetch_array($t_rslt);
$tot_sample = round($t_array[0],0);

// Target Base
if(strlen($_SESSION["seas_brd_filter_condition"]) >0) {
	$sqlcond = $_SESSION["seas_brd_filter_condition"];
}
else {
	$sqlcond = "1=1";
}
$t_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond;
$t_rslt= mysql_query($t_sql);
$t_array = mysql_fetch_array($t_rslt);
$trg_sample = round($t_array[0],0);

// Benchmark Base
if(strlen($_SESSION["seas_brd_bnch_filter_condition"]) >0) {
	$sqlcond2 = $_SESSION["seas_brd_bnch_filter_condition"];
}
else {
	$sqlcond2 = "1=1";
}
$b_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond2;
$b_rslt= mysql_query($b_sql);
$b_array = mysql_fetch_array($b_rslt);
$bnch_sample = round($b_array[0],0);



$_SESSION["seas_brd_target_sample_text"]="";
if($_SESSION["seas_brd_language"]==2) {
	$_SESSION["seas_brd_target_sample_text"] .= "العينة المستهدفة = " . $trg_sample . ", ";
	$_SESSION["seas_brd_target_sample_text"] .=  $per_sample . " %  من إجمالي العينة";	
}
else  {
	$_SESSION["seas_brd_target_sample_text"] .= "Target Base = " . $trg_sample . ", ";
	$_SESSION["seas_brd_target_sample_text"] .=  $per_sample . " %  of Total Sample";	
}

$_SESSION["seas_brd_benchmark_sample_text"]="";
if(strlen($_SESSION["seas_brd_bnch_filter_condition"])>0) {
	if($_SESSION["seas_brd_language"]==2) {
		$_SESSION["seas_brd_benchmark_sample_text"] .= "عينة المؤشر =" . $bnch_sample . ", ";
		$_SESSION["seas_brd_benchmark_sample_text"] .=  $bper_sample . " %  من إجمالي العينة";
	}
	else {
		$_SESSION["seas_brd_benchmark_sample_text"] .= "Benchmark Base = " . $bnch_sample . ", ";
		$_SESSION["seas_brd_benchmark_sample_text"] .=  $bper_sample . " % of Total Sample";
	}
}

mysql_free_result($t_rslt);
// Percentage
$per_sample = round(($trg_sample/$tot_sample)*100,0);
$bper_sample = round(($bnch_sample/$tot_sample)*100,0);



$_SESSION["seas_brd_target_sample_text"]="";
if($_SESSION["seas_brd_language"]==2) {
	$_SESSION["seas_brd_target_sample_text"] .= "العينة المستهدفة = " . $trg_sample . ", ";
	$_SESSION["seas_brd_target_sample_text"] .=  $per_sample . "%  من إجمالي العينة";	
}
else  {
	$_SESSION["seas_brd_target_sample_text"] .= "Target Base = " . $trg_sample . ", ";
	$_SESSION["seas_brd_target_sample_text"] .=  $per_sample . "%  of Total Sample";	
}

$_SESSION["seas_brd_benchmark_sample_text"]="";
if(strlen($_SESSION["seas_brd_bnch_filter_condition"])>0) {
	if($_SESSION["seas_brd_language"]==2) {
		$_SESSION["seas_brd_benchmark_sample_text"] .= "عينة المؤشر =" . $bnch_sample . ", ";
		$_SESSION["seas_brd_benchmark_sample_text"] .=  $bper_sample . "%  من إجمالي العينة";
	}
	else {
		$_SESSION["seas_brd_benchmark_sample_text"] .= "Benchmark Base = " . $bnch_sample . ", ";
		$_SESSION["seas_brd_benchmark_sample_text"] .=  $bper_sample . "% of Total Sample";
	}
}


// Define Report Array
$Q2_a = getValues($rmode,"V9_1"); //array(60,20,82.4,49.7,71.26); // Any Agree, Any DisAgree, Strength, Character, Average, Nuetral, Total
$Q2_b = getValues($rmode,"V9_2"); //array(41,37,82.7,4.5,59.81);
$Q2_c = getValues($rmode,"V9_3"); //array(46,29,77.5,23.5,63.81);
$Q2_d = getValues($rmode,"V9_4"); //array(69,15,85.6,64.8,76.71);
$Q2_e = getValues($rmode,"V9_5"); //array(57,24,84.8,41.0,68.89);
$Q2_f = getValues($rmode,"V9_6"); //array(44,29,76.9,20.2,63.69);
$Q2_g = getValues($rmode,"V9_7"); //array(41,33,78.1,11.1,60.70);
$Q2 = array($Q2_a,$Q2_b,$Q2_c,$Q2_d,$Q2_e,$Q2_f,$Q2_g);
$Question_Array["Q2"][3]=$Q2;

$Q2_Value = array("V9_1","V9_2","V9_3","V9_4","V9_5","V9_6","V9_7");
$Q2_S_Avg = getAvg($Q2_Value,1,1); //getAvg($Q2,2);
$Q2_C_Avg = getAvg($Q2_Value,1,2); //getAvg($Q2,3);
$Q2_A_Avg = getAvg($Q2_Value,1,5); //getAvg_val(array("V9_1","V9_2","V9_3","V9_4","V9_5","V9_6","V9_7"),1); //getAvg($Q2,4);
$Q2_N_Avg = getAvg($Q2_Value,1,4); //getAvg($Q2,1);
$Q2_P_Avg = getAvg($Q2_Value,1,3); //getAvg($Q2,0);

$Q2_S_Avg2 = getAvg($Q2_Value,2,1); //getAvg($Q2,9);
$Q2_C_Avg2 = getAvg($Q2_Value,2,2); //getAvg($Q2,10);
$Q2_A_Avg2 = getAvg($Q2_Value,2,5); //getAvg_val(array("V9_1","V9_2","V9_3","V9_4","V9_5","V9_6","V9_7"),2); //getAvg($Q2,11);
$Q2_N_Avg2 = getAvg($Q2_Value,2,4); //getAvg($Q2,8);
$Q2_P_Avg2 = getAvg($Q2_Value,2,3); //getAvg($Q2,7);

$Question_Array["Q2"][5]=array($Q2_S_Avg,$Q2_C_Avg,$Q2_P_Avg,$Q2_N_Avg,$Q2_A_Avg,$Q2_S_Avg2,$Q2_C_Avg2,$Q2_P_Avg2,$Q2_N_Avg2,$Q2_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Q5_a = getValues($rmode,"V13_1"); //array(0,0,0,0,0);
$Q5_b = getValues($rmode,"V13_2"); //array(0,0,0,0,0);
$Q5_c = getValues($rmode,"V13_3"); //array(0,0,0,0,0);
$Q5_d = getValues($rmode,"V13_4"); //array(0,0,0,0,0);
$Q5 = array($Q5_a,$Q5_b,$Q5_c,$Q5_d);
$Question_Array["Q5"][3]=$Q5;

$Q5_Value = array("V13_1","V13_2","V13_3","V13_4");

$Q5_S_Avg = getAvg($Q5_Value,1,1); //getAvg($Q5,2);
$Q5_C_Avg = getAvg($Q5_Value,1,2); //getAvg($Q5,3);
$Q5_A_Avg = getAvg($Q5_Value,1,5); //getAvg_val(array("V13_1","V13_2","V13_3","V13_4"),1);
$Q5_N_Avg = getAvg($Q5_Value,1,4); //getAvg($Q5,1);
$Q5_P_Avg = getAvg($Q5_Value,1,3); //getAvg($Q5,0);

$Q5_S_Avg2 = getAvg($Q5_Value,2,1); //getAvg($Q5,9);
$Q5_C_Avg2 = getAvg($Q5_Value,2,2); //getAvg($Q5,10);
$Q5_A_Avg2 = getAvg($Q5_Value,2,5); //getAvg_val(array("V13_1","V13_2","V13_3","V13_4"),2); //getAvg($Q5,11);
$Q5_N_Avg2 = getAvg($Q5_Value,2,4); //getAvg($Q5,8);
$Q5_P_Avg2 = getAvg($Q5_Value,2,3); //getAvg($Q5,7);

$Question_Array["Q5"][5]=array($Q5_S_Avg,$Q5_C_Avg,$Q5_P_Avg,$Q5_N_Avg,$Q5_A_Avg, $Q5_S_Avg2,$Q5_C_Avg2,$Q5_P_Avg2,$Q5_N_Avg2,$Q5_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Q6_a = getValues($rmode,"V14_1"); //array(0,0,0,0,0);
$Q6_b = getValues($rmode,"V14_2"); //array(0,0,0,0,0);
$Q6_c = getValues($rmode,"V14_3"); //array(0,0,0,0,0);
$Q6 = array($Q6_a,$Q6_b,$Q6_c);
$Question_Array["Q6"][3]=$Q6;

$Q6_Value = array("V14_1","V14_2","V14_3");

$Q6_S_Avg = getAvg($Q6_Value,1,1); //getAvg($Q6,2);
$Q6_C_Avg = getAvg($Q6_Value,1,2); //getAvg($Q6,3);
$Q6_A_Avg = getAvg($Q6_Value,1,5); //getAvg_val(array("V14_1","V14_2","V14_3"),1);
$Q6_N_Avg = getAvg($Q6_Value,1,4); //getAvg($Q6,1);
$Q6_P_Avg = getAvg($Q6_Value,1,3); //getAvg($Q6,0);

$Q6_S_Avg2 = getAvg($Q6_Value,2,1); //getAvg($Q6,9);
$Q6_C_Avg2 = getAvg($Q6_Value,2,2); //getAvg($Q6,10);
$Q6_A_Avg2 = getAvg($Q6_Value,2,5); //getAvg_val(array("V14_1","V14_2","V14_3"),2); // getAvg($Q6,11);
$Q6_N_Avg2 = getAvg($Q6_Value,2,4); //getAvg($Q6,8);
$Q6_P_Avg2 = getAvg($Q6_Value,2,3); //getAvg($Q6,7);

$Question_Array["Q6"][5]=array($Q6_S_Avg,$Q6_C_Avg,$Q6_P_Avg,$Q6_N_Avg,$Q6_A_Avg, $Q6_S_Avg2,$Q6_C_Avg2,$Q6_P_Avg2,$Q6_N_Avg2,$Q6_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Q7_a = getValues($rmode,"V15_1"); //array(0,0,0,0,0);
$Q7_b = getValues($rmode,"V15_2",array(1,2,3,4,5,9)); //array(0,0,0,0,0);
$Q7_c = getValues($rmode,"V15_3"); //array(0,0,0,0,0);

//$Q7_d = getValues($rmode,"V15_4"); //array(0,0,0,0,0);
// $Q7_e = getValues($rmode,"V15_5"); //array(0,0,0,0,0);

$Q7 = array($Q7_a,$Q7_b,$Q7_c); //$Q7_d,$Q7_e);
$Question_Array["Q7"][3]=$Q7;

$Q7_Value = array("V15_1","V15_2","V15_3");

$Q7_S_Avg = getAvg($Q7_Value,1,1); //getAvg($Q7,2);
$Q7_C_Avg = getAvg($Q7_Value,1,2); //getAvg($Q7,3);
$Q7_A_Avg = getAvg($Q7_Value,1,5); //getAvg_val(array("V15_1","V15_2","V15_3"),1);
$Q7_N_Avg = getAvg($Q7_Value,1,4); //getAvg($Q7,1);
$Q7_P_Avg = getAvg($Q7_Value,1,3); //getAvg($Q7,0);

$Q7_S_Avg2 = getAvg($Q7_Value,2,1); //getAvg($Q7,9);
$Q7_C_Avg2 = getAvg($Q7_Value,2,2); //getAvg($Q7,10);
$Q7_A_Avg2 = getAvg($Q7_Value,2,5); //getAvg_val(array("V15_1","V15_2","V15_3"),2); //getAvg($Q7,11);
$Q7_N_Avg2 = getAvg($Q7_Value,2,4); //getAvg($Q7,8);
$Q7_P_Avg2 = getAvg($Q7_Value,2,3); //getAvg($Q7,7);

$Question_Array["Q7"][5]=array($Q7_S_Avg,$Q7_C_Avg,$Q7_P_Avg,$Q7_N_Avg,$Q7_A_Avg, $Q7_S_Avg2,$Q7_C_Avg2,$Q7_P_Avg2,$Q7_N_Avg2,$Q7_A_Avg2);
//***-------------------------------------------------------------------------------***//
/*
$Q8_a = getValues($rmode,"V16"); //array(0,0,0,0,0);
$Q8 = array($Q8_a);
$Question_Array["Q8"][3]=$Q8;

$Q8_Value = array("V16");

$Q8_S_Avg = getAvg($Q8_Value,1,1); //getAvg($Q8,2);
$Q8_C_Avg = getAvg($Q8_Value,1,2); //getAvg($Q8,3);
$Q8_A_Avg = getAvg($Q8_Value,1,5); //getAvg_val(array("V16"),1);
$Q8_N_Avg = getAvg($Q8_Value,1,4); //getAvg($Q8,1);
$Q8_P_Avg = getAvg($Q8_Value,1,3); //getAvg($Q8,0);

$Q8_S_Avg2 = getAvg($Q8_Value,2,1); //getAvg($Q8,9);
$Q8_C_Avg2 = getAvg($Q8_Value,2,2); //getAvg($Q8,10);
$Q8_A_Avg2 = getAvg($Q8_Value,2,5); //getAvg_val(array("V16"),2); //getAvg($Q8,11);
$Q8_N_Avg2 = getAvg($Q8_Value,2,4); //getAvg($Q8,8);
$Q8_P_Avg2 = getAvg($Q8_Value,2,3); //getAvg($Q8,7);

$Question_Array["Q8"][5]=array($Q8_S_Avg,$Q8_C_Avg,$Q8_P_Avg,$Q8_N_Avg,$Q8_A_Avg, $Q8_S_Avg2,$Q8_C_Avg2,$Q8_P_Avg2,$Q8_N_Avg2,$Q8_A_Avg2);
*/
//***-------------------------------------------------------------------------------***//


$Q9_a = getValues($rmode,"V17_1"); //array(0,0,0,0,0);
$Q9_b = getValues($rmode,"V17_2"); //array(0,0,0,0,0);
$Q9_c = getValues($rmode,"V17_3"); //array(0,0,0,0,0);
$Q9 = array($Q9_a,$Q9_b,$Q9_c);
$Question_Array["Q9"][3]=$Q9;

$Q9_Value = array("V17_1","V17_2","V17_3");

$Q9_S_Avg = getAvg($Q9_Value,1,1); //getAvg($Q9,2);
$Q9_C_Avg = getAvg($Q9_Value,1,2); //getAvg($Q9,3);
$Q9_A_Avg = getAvg($Q9_Value,1,5); //getAvg_val(array("V17_1","V17_2","V17_3"),1);
$Q9_N_Avg = getAvg($Q9_Value,1,4); //getAvg($Q9,1);
$Q9_P_Avg = getAvg($Q9_Value,1,3); //getAvg($Q9,0);

$Q9_S_Avg2 = getAvg($Q9_Value,2,1); //getAvg($Q9,9);
$Q9_C_Avg2 = getAvg($Q9_Value,2,2); //getAvg($Q9,10);
$Q9_A_Avg2 = getAvg($Q9_Value,2,5); //getAvg_val(array("V17_1","V17_2","V17_3"),2); //getAvg($Q9,11);
$Q9_N_Avg2 = getAvg($Q9_Value,2,4); //getAvg($Q9,8);
$Q9_P_Avg2 = getAvg($Q9_Value,2,3); //getAvg($Q9,7);

$Question_Array["Q9"][5]=array($Q9_S_Avg,$Q9_C_Avg,$Q9_P_Avg,$Q9_N_Avg,$Q9_A_Avg, $Q9_S_Avg2,$Q9_C_Avg2,$Q9_P_Avg2,$Q9_N_Avg2,$Q9_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Q10_a = getValues($rmode,"V18_1"); //array(0,0,0,0,0);
$Q10_b = getValues($rmode,"V18_2",array(1,2,3,4,5,9)); //array(0,0,0,0,0);
$Q10_c = getValues($rmode,"V18_3"); //array(0,0,0,0,0)
$Q10 = array($Q10_a,$Q10_b,$Q10_c);
$Question_Array["Q10"][3]=$Q10;

$Q10_Value = array("V18_1","V18_2","V18_3");

$Q10_S_Avg = getAvg($Q10_Value,1,1); //getAvg($Q10,2);
$Q10_C_Avg = getAvg($Q10_Value,1,2); //getAvg($Q10,3);
$Q10_A_Avg = getAvg($Q10_Value,1,5); //getAvg_val(array("V18_1","V18_2","V18_3"),1);
$Q10_N_Avg = getAvg($Q10_Value,1,4); //getAvg($Q10,1);
$Q10_P_Avg = getAvg($Q10_Value,1,3); //getAvg($Q10,0);

$Q10_S_Avg2 = getAvg($Q10_Value,2,1); //getAvg($Q10,9);
$Q10_C_Avg2 = getAvg($Q10_Value,2,2); //getAvg($Q10,10);
$Q10_A_Avg2 = getAvg($Q10_Value,2,5); //getAvg_val(array("V18_1","V18_2","V18_3"),2); //getAvg($Q10,11);
$Q10_N_Avg2 = getAvg($Q10_Value,2,4); //getAvg($Q10,8);
$Q10_P_Avg2 = getAvg($Q10_Value,2,3); //getAvg($Q10,7);

$Question_Array["Q10"][5]=array($Q10_S_Avg,$Q10_C_Avg,$Q10_P_Avg,$Q10_N_Avg,$Q10_A_Avg, $Q10_S_Avg2,$Q10_C_Avg2,$Q10_P_Avg2,$Q10_N_Avg2,$Q10_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Q11_a = getValues($rmode,"V19_1"); //array(0,0,0,0,0);
$Q11_b = getValues($rmode,"V19_2"); //array(0,0,0,0,0);
$Q11_c = getValues($rmode,"V19_3"); //array(0,0,0,0,0);
$Q11 = array($Q11_a,$Q11_b,$Q11_c);
$Question_Array["Q11"][3]=$Q11;

$Q11_Value = array("V19_1","V19_2","V19_3");

$Q11_S_Avg = getAvg($Q11_Value,1,1); //getAvg($Q11,2);
$Q11_C_Avg = getAvg($Q11_Value,1,2); //getAvg($Q11,3);
$Q11_A_Avg = getAvg($Q11_Value,1,5); //getAvg_val(array("V19_1","V19_2","V19_3"),1);
$Q11_N_Avg = getAvg($Q11_Value,1,4); //getAvg($Q11,1);
$Q11_P_Avg = getAvg($Q11_Value,1,3); //getAvg($Q11,0);

$Q11_S_Avg2 = getAvg($Q11_Value,2,1); //getAvg($Q11,9);
$Q11_C_Avg2 = getAvg($Q11_Value,2,2); //getAvg($Q11,10);
$Q11_A_Avg2 = getAvg($Q11_Value,2,5); //getAvg_val(array("V19_1","V19_2","V19_3"),2); //getAvg($Q11,11);
$Q11_N_Avg2 = getAvg($Q11_Value,2,4); //getAvg($Q11,8);
$Q11_P_Avg2 = getAvg($Q11_Value,2,3); //getAvg($Q11,7);

$Question_Array["Q11"][5]=array($Q11_S_Avg,$Q11_C_Avg,$Q11_P_Avg,$Q11_N_Avg,$Q11_A_Avg, $Q11_S_Avg2,$Q11_C_Avg2,$Q11_P_Avg2,$Q11_N_Avg2,$Q11_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Q12_a = getValues($rmode,"V20_1"); //array(0,0,0,0,0);
$Q12_b = getValues($rmode,"V20_2"); //array(0,0,0,0,0);
$Q12_c = getValues($rmode,"V20_3"); //array(0,0,0,0,0);
$Q12_d = getValues($rmode,"V20_4"); //array(0,0,0,0,0);
$Q12_e = getValues($rmode,"V20_5"); //array(0,0,0,0,0);
$Q12_f = getValues($rmode,"V20_6"); //array(0,0,0,0,0);
$Q12 = array($Q12_a,$Q12_b,$Q12_c,$Q12_d,$Q12_e,$Q12_f);
$Question_Array["Q12"][3]=$Q12;

$Q12_Value = array("V20_1","V20_2","V20_3","V20_4","V20_5","V20_6");

$Q12_S_Avg = getAvg($Q12_Value,1,1); //getAvg($Q12,2);
$Q12_C_Avg = getAvg($Q12_Value,1,2); //getAvg($Q12,3);
$Q12_A_Avg = getAvg($Q12_Value,1,5); //getAvg_val(array("V20_1","V20_2","V20_3","V20_4","V20_5","V20_6"),1);
$Q12_N_Avg = getAvg($Q12_Value,1,4); //getAvg($Q12,1);
$Q12_P_Avg = getAvg($Q12_Value,1,3); //getAvg($Q12,0);

$Q12_S_Avg2 = getAvg($Q12_Value,2,1); //getAvg($Q12,9);
$Q12_C_Avg2 = getAvg($Q12_Value,2,2); //getAvg($Q12,10);
$Q12_A_Avg2 = getAvg($Q12_Value,2,5); //getAvg_val(array("V20_1","V20_2","V20_3","V20_4","V20_5","V20_6"),2); //getAvg($Q12,11);
$Q12_N_Avg2 = getAvg($Q12_Value,2,4); //getAvg($Q12,8);
$Q12_P_Avg2 = getAvg($Q12_Value,2,3); //getAvg($Q12,7);

$Question_Array["Q12"][5]=array($Q12_S_Avg,$Q12_C_Avg,$Q12_P_Avg,$Q12_N_Avg,$Q12_A_Avg, $Q12_S_Avg2,$Q12_C_Avg2,$Q12_P_Avg2,$Q12_N_Avg2,$Q12_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Q13_a = getValues($rmode,"V21_1"); //array(0,0,0,0,0);
$Q13_b = getValues($rmode,"V21_2"); //array(0,0,0,0,0);
$Q13 = array($Q13_a,$Q13_b);
$Question_Array["Q13"][3]=$Q13;

$Q13_Value = array("V21_1","V21_2");

$Q13_S_Avg = getAvg($Q13_Value,1,1); //getAvg($Q13,2);
$Q13_C_Avg = getAvg($Q13_Value,1,2); //getAvg($Q13,3);
$Q13_A_Avg = getAvg($Q13_Value,1,5); //getAvg_val(array("V21_1","V21_2"),1);
$Q13_N_Avg = getAvg($Q13_Value,1,4); //getAvg($Q13,1);
$Q13_P_Avg = getAvg($Q13_Value,1,3); //getAvg($Q13,0);

$Q13_S_Avg2 = getAvg($Q13_Value,2,1); //getAvg($Q13,9);
$Q13_C_Avg2 = getAvg($Q13_Value,2,2); //getAvg($Q13,10);
$Q13_A_Avg2 = getAvg($Q13_Value,2,5); //getAvg_val(array("V21_1","V21_2"),2); //getAvg($Q12,11);
$Q13_N_Avg2 = getAvg($Q13_Value,2,4); //getAvg($Q13,8);
$Q13_P_Avg2 = getAvg($Q13_Value,2,3); //getAvg($Q13,7);

$Question_Array["Q13"][5]=array($Q13_S_Avg,$Q13_C_Avg,$Q13_P_Avg,$Q13_N_Avg,$Q13_A_Avg, $Q13_S_Avg2,$Q13_C_Avg2,$Q13_P_Avg2,$Q13_N_Avg2,$Q13_A_Avg2);

//***-------------------------------------------------------------------------------***//

$Q14_a = getValues($rmode,"V22_1"); //array(0,0,0,0,0);
$Q14_b = getValues($rmode,"V22_2"); //array(0,0,0,0,0);
$Q14_c = getValues($rmode,"V22_3"); //array(0,0,0,0,0);
$Q14_d = getValues($rmode,"V22_4"); //array(0,0,0,0,0);
$Q14_e = getValues($rmode,"V22_5"); //array(0,0,0,0,0);
$Q14_f = getValues($rmode,"V22_6"); //array(0,0,0,0,0);
$Q14 = array($Q14_a,$Q14_b,$Q14_c,$Q14_d,$Q14_e,$Q14_f);
$Question_Array["Q14"][3]=$Q14;

$Q14_Value = array("V22_1","V22_2","V22_3","V22_4","V22_5","V22_6");

$Q14_S_Avg = getAvg($Q14_Value,1,1); //getAvg($Q14,2);
$Q14_C_Avg = getAvg($Q14_Value,1,2); //getAvg($Q14,3);
$Q14_A_Avg = getAvg($Q14_Value,1,5); //getAvg_val(array("V22_1","V22_2","V22_3","V22_4","V22_5","V22_6"),1);
$Q14_N_Avg = getAvg($Q14_Value,1,4); //getAvg($Q14,1);
$Q14_P_Avg = getAvg($Q14_Value,1,3); //getAvg($Q14,0);

$Q14_S_Avg2 = getAvg($Q14_Value,2,1); //getAvg($Q14,9);
$Q14_C_Avg2 = getAvg($Q14_Value,2,2); //getAvg($Q14,10);
$Q14_A_Avg2 = getAvg($Q14_Value,2,5); //getAvg_val(array("V22_1","V22_2","V22_3","V22_4","V22_5","V22_6"),2); //getAvg($Q14,11);
$Q14_N_Avg2 = getAvg($Q14_Value,2,4); //getAvg($Q14,8);
$Q14_P_Avg2 = getAvg($Q14_Value,2,3); //getAvg($Q14,7);

$Question_Array["Q14"][5]=array($Q14_S_Avg,$Q14_C_Avg,$Q14_P_Avg,$Q14_N_Avg,$Q14_A_Avg, $Q14_S_Avg2,$Q14_C_Avg2,$Q14_P_Avg2,$Q14_N_Avg2,$Q14_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Q17_a = getValues($rmode,"V25_1"); //array(0,0,0,0,0);
$Q17_b = getValues($rmode,"V25_2"); //array(0,0,0,0,0);
$Q17_c = getValues($rmode,"V25_3"); //array(0,0,0,0,0);
$Q17_d = getValues($rmode,"V25_4"); //array(0,0,0,0,0);
$Q17_e = getValues($rmode,"V25_5"); //array(0,0,0,0,0);
$Q17 = array($Q17_a,$Q17_b,$Q17_c,$Q17_d,$Q17_e);
$Question_Array["Q17"][3]=$Q17;

$Q17_Value = array("V25_1","V25_2","V25_3","V25_4","V25_5");

$Q17_S_Avg = getAvg($Q17_Value,1,1); //getAvg($Q17,2);
$Q17_C_Avg = getAvg($Q17_Value,1,2); //getAvg($Q17,3);
$Q17_A_Avg = getAvg($Q17_Value,1,5); //getAvg_val(array("V25_1","V25_2","V25_3","V25_4","V25_5"),1);
$Q17_N_Avg = getAvg($Q17_Value,1,4); //getAvg($Q17,1);
$Q17_P_Avg = getAvg($Q17_Value,1,3); //getAvg($Q17,0);

$Q17_S_Avg2 = getAvg($Q17_Value,2,1); //getAvg($Q17,9);
$Q17_C_Avg2 = getAvg($Q17_Value,2,2); //getAvg($Q17,10);
$Q17_A_Avg2 = getAvg($Q17_Value,2,5); //getAvg_val(array("V25_1","V25_2","V25_3","V25_4","V25_5"),2); //getAvg($Q17,11);
$Q17_N_Avg2 = getAvg($Q17_Value,2,4); //getAvg($Q17,8);
$Q17_P_Avg2 = getAvg($Q17_Value,2,3); //getAvg($Q17,7);

$Question_Array["Q17"][5]=array($Q17_S_Avg,$Q17_C_Avg,$Q17_P_Avg,$Q17_N_Avg,$Q17_A_Avg, $Q17_S_Avg2,$Q17_C_Avg2,$Q17_P_Avg2,$Q17_N_Avg2,$Q17_A_Avg2);
//***-------------------------------------------------------------------------------***//
/*
$Q18_a = getValues($rmode,"V26_1"); //array(0,0,0,0,0);
$Q18_b = getValues($rmode,"V26_2"); //array(0,0,0,0,0);
$Q18_c = getValues($rmode,"V26_3"); //array(0,0,0,0,0);
$Q18_d = getValues($rmode,"V26_4"); //array(0,0,0,0,0);
$Q18_e = getValues($rmode,"V26_5"); //array(0,0,0,0,0);
$Q18_f = getValues($rmode,"V26_6"); //array(0,0,0,0,0);
$Q18_g = getValues($rmode,"V26_7"); //array(0,0,0,0,0);
$Q18_h = getValues($rmode,"V26_8"); //array(0,0,0,0,0);
$Q18_i = getValues($rmode,"V26_9"); //array(0,0,0,0,0);
$Q18_j = getValues($rmode,"V26_10"); //array(0,0,0,0,0);
$Q18_k = getValues($rmode,"V26_11"); //array(0,0,0,0,0);
$Q18 = array($Q18_a,$Q18_b,$Q18_c,$Q18_d,$Q18_e,$Q18_f,$Q18_g,$Q18_h,$Q18_i,$Q18_j,$Q18_k);
$Question_Array["Q18"][3]=$Q18;

$Q18_Value = array("V26_1","V26_2","V26_3","V26_4","V26_5","V26_6","V26_7","V26_8","V26_9","V26_10","V26_11");

$Q18_S_Avg = getAvg($Q18_Value,1,1); //getAvg($Q18,2);
$Q18_C_Avg = getAvg($Q18_Value,1,2); //getAvg($Q18,3);
$Q18_A_Avg = getAvg($Q18_Value,1,5); //getAvg_val(array("V26_1","V26_2","V26_3","V26_4","V26_5","V26_6","V26_7","V26_8","V26_9","V26_10","V26_11"),1);
$Q18_N_Avg = getAvg($Q18_Value,1,4); //getAvg($Q18,1);
$Q18_P_Avg = getAvg($Q18_Value,1,3); //getAvg($Q18,0);

$Q18_S_Avg2 = getAvg($Q18_Value,2,1); //getAvg($Q18,9);
$Q18_C_Avg2 = getAvg($Q18_Value,2,2); //getAvg($Q18,10);
$Q18_A_Avg2 = getAvg($Q18_Value,2,5); //getAvg_val(array("V26_1","V26_2","V26_3","V26_4","V26_5","V26_6","V26_7","V26_8","V26_9","V26_10","V26_11"),2); //getAvg($Q18,11);
$Q18_N_Avg2 = getAvg($Q18_Value,2,4); //getAvg($Q18,8);
$Q18_P_Avg2 = getAvg($Q18_Value,2,3); //getAvg($Q18,7);

$Question_Array["Q18"][5]=array($Q18_S_Avg,$Q18_C_Avg,$Q18_P_Avg,$Q18_N_Avg,$Q18_A_Avg, $Q18_S_Avg2,$Q18_C_Avg2,$Q18_P_Avg2,$Q18_N_Avg2,$Q18_A_Avg2);
*/
//***-------------------------------------------------------------------------------***//

$Q22_a = getValues($rmode,"V30_1"); //array(0,0,0,0,0);
$Q22_b = getValues($rmode,"V30_2"); //array(0,0,0,0,0);
$Q22_c = getValues($rmode,"V30_3"); //array(0,0,0,0,0);
$Q22_d = getValues($rmode,"V30_4"); //array(0,0,0,0,0);
$Q22_e = getValues($rmode,"V30_5"); //array(0,0,0,0,0);
$Q22_f = getValues($rmode,"V30_6"); //array(0,0,0,0,0);
$Q22_g = getValues($rmode,"V30_7"); //array(0,0,0,0,0);
$Q22_h = getValues($rmode,"V30_8"); //array(0,0,0,0,0);
$Q22 = array($Q22_a,$Q22_b,$Q22_c,$Q22_d,$Q22_e,$Q22_f,$Q22_g,$Q22_h);
$Question_Array["Q22"][3]=$Q22;

$Q22_Value = array("V30_1","V30_2","V30_3","V30_4","V30_5","V30_6","V30_7","V30_8");

$Q22_S_Avg = getAvg($Q22_Value,1,1); //getAvg($Q22,2);
$Q22_C_Avg = getAvg($Q22_Value,1,2); //getAvg($Q22,3);
$Q22_A_Avg = getAvg($Q22_Value,1,5); //getAvg_val(array("V30_1","V30_2","V30_3","V30_4","V30_5","V30_6","V30_7","V30_8"),1);
$Q22_N_Avg = getAvg($Q22_Value,1,4); //getAvg($Q22,1);
$Q22_P_Avg = getAvg($Q22_Value,1,3); //getAvg($Q22,0);

$Q22_S_Avg2 = getAvg($Q22_Value,2,1); //getAvg($Q22,9);
$Q22_C_Avg2 = getAvg($Q22_Value,2,2); //getAvg($Q22,10);
$Q22_A_Avg2 = getAvg($Q22_Value,2,5); //getAvg_val(array("V30_1","V30_2","V30_3","V30_4","V30_5","V30_6","V30_7","V30_8"),2); //getAvg($Q22,11)
$Q22_N_Avg2 = getAvg($Q22_Value,2,4); //getAvg($Q22,8);
$Q22_P_Avg2 = getAvg($Q22_Value,2,3); //getAvg($Q22,7);

$Question_Array["Q22"][5]=array($Q22_S_Avg,$Q22_C_Avg,$Q22_P_Avg,$Q22_N_Avg,$Q22_A_Avg, $Q22_S_Avg2,$Q22_C_Avg2,$Q22_P_Avg2,$Q22_N_Avg2,$Q22_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Q23_a = getValues($rmode,"V31_1"); //array(0,0,0,0,0);
$Q23_b = getValues($rmode,"V31_2"); //array(0,0,0,0,0);
$Q23_c = getValues($rmode,"V31_3"); //array(0,0,0,0,0);
$Q23_d = getValues($rmode,"V31_4"); //array(0,0,0,0,0);
$Q23_e = getValues($rmode,"V31_5"); //array(0,0,0,0,0);
$Q23 = array($Q23_a,$Q23_b,$Q23_c,$Q23_d,$Q23_e);
$Question_Array["Q23"][3]=$Q23;

$Q23_Value = array("V31_1","V31_2","V31_3","V31_4","V31_5");

$Q23_S_Avg = getAvg($Q23_Value,1,1); //getAvg($Q23,2);
$Q23_C_Avg = getAvg($Q23_Value,1,2); //getAvg($Q23,3);
$Q23_A_Avg = getAvg($Q23_Value,1,5); //getAvg_val(array("V31_1","V31_2","V31_3","V31_4","V31_5"),1);
$Q23_N_Avg = getAvg($Q23_Value,1,4); //getAvg($Q23,1);
$Q23_P_Avg = getAvg($Q23_Value,1,3); //getAvg($Q23,0);

$Q23_S_Avg2 = getAvg($Q23_Value,2,1); //getAvg($Q23,9);
$Q23_C_Avg2 = getAvg($Q23_Value,2,2); //getAvg($Q23,10);
$Q23_A_Avg2 = getAvg($Q23_Value,2,5); //getAvg_val(array("V31_1","V31_2","V31_3","V31_4","V31_5"),2); //getAvg($Q23,11);
$Q23_N_Avg2 = getAvg($Q23_Value,2,4); //getAvg($Q23,8);
$Q23_P_Avg2 = getAvg($Q23_Value,2,3); //getAvg($Q23,7);

$Question_Array["Q23"][5]=array($Q23_S_Avg,$Q23_C_Avg,$Q23_P_Avg,$Q23_N_Avg,$Q23_A_Avg, $Q23_S_Avg2,$Q23_C_Avg2,$Q23_P_Avg2,$Q23_N_Avg2,$Q23_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Q24_a = getValues($rmode,"V32"); //array(0,0,0,0,0);
$Q24 = array($Q24_a);
$Question_Array["Q24"][3]=$Q24;

$Q24_Value = array("V32");

$Q24_S_Avg = getAvg($Q24_Value,1,1); //getAvg($Q24,2);
$Q24_C_Avg = getAvg($Q24_Value,1,2); //getAvg($Q24,3);
$Q24_A_Avg = getAvg($Q24_Value,1,5); //getAvg_val(array("V32"),1);
$Q24_N_Avg = getAvg($Q24_Value,1,4); //getAvg($Q24,1);
$Q24_P_Avg = getAvg($Q24_Value,1,3); //getAvg($Q24,0);

$Q24_S_Avg2 = getAvg($Q24_Value,2,1); //getAvg($Q24,9);
$Q24_C_Avg2 = getAvg($Q24_Value,2,2); //getAvg($Q24,10);
$Q24_A_Avg2 = getAvg($Q24_Value,2,5); //getAvg_val(array("V32"),2); //getAvg($Q24,11);
$Q24_N_Avg2 = getAvg($Q24_Value,2,4); //getAvg($Q24,8);
$Q24_P_Avg2 = getAvg($Q24_Value,2,3); //getAvg($Q24,7);

$Question_Array["Q24"][5]=array($Q24_S_Avg,$Q24_C_Avg,$Q24_P_Avg,$Q24_N_Avg,$Q24_A_Avg, $Q24_S_Avg2,$Q24_C_Avg2,$Q24_P_Avg2,$Q24_N_Avg2,$Q24_A_Avg2);
//***-------------------------------------------------------------------------------***//

$Strength_Avg  = array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
$Character_Avg = array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
$Average_Avg   = array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

$overall_array=array_merge($Q2_Value,$Q5_Value,$Q6_Value,$Q7_Value,$Q9_Value,$Q10_Value,$Q11_Value,$Q12_Value,$Q13_Value,$Q14_Value,$Q17_Value,$Q22_Value,$Q23_Value,$Q24_Value);

$Overall_S_Avg = getAvg($overall_array,1,1);
$Overall_C_Avg = getAvg($overall_array,1,2);
$Overall_A_Avg = getAvg($overall_array,1,5);
$Overall_N_Avg = getAvg($overall_array,1,4);
$Overall_P_Avg = getAvg($overall_array,1,3);

// Bench mark
$Overall_S_Avg2 = getAvg($overall_array,2,1);
$Overall_C_Avg2 = getAvg($overall_array,2,2);
$Overall_A_Avg2 = getAvg($overall_array,2,5);
$Overall_N_Avg2 = getAvg($overall_array,2,4);
$Overall_P_Avg2 = getAvg($overall_array,2,3);

//Calculate Index
if($_SESSION["seas_brd_language"]==2) {
	$qopts =array("ا","ب","ج","د","ه","و","ز","ح","ط","ي","ك","ل","م","ن","س","ع","ف","ص","ق","ر","ش","ت","ث","خ","ذ","ض");
}
else {
	$qopts =array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
}
$qst   =array("Q2","Q5","Q6","Q7","Q9","Q10","Q11","Q12","Q13","Q14","Q17","Q22","Q23","Q24");

foreach($qst as $qss) {
	$qsd = $$qss;
	for($is=0;$is<count($qsd);$is++) {
		$qo=$qopts[$is];
		$qvar = $qss . "_" . $qo;
		
		if(strlen($_SESSION["seas_brd_bnch_filter_condition"])>0) {
			${$qss."_IDX"}[$qo][0] =round((${$qvar}[2]/${$qvar}[9])*100,0);
			${$qss."_IDX"}[$qo][1] =round((${$qvar}[3]/${$qvar}[10])*100,0);
			${$qss."_IDX"}[$qo][2] =round((${$qvar}[4]/${$qvar}[11])*100,0);
			${$qss."_IDX"}[$qo][3] =round((${$qvar}[1]/${$qvar}[8])*100,0);
			${$qss."_IDX"}[$qo][4] =round((${$qvar}[0]/${$qvar}[7])*100,0);
		}
		else {
			${$qss."_IDX"}[$qo][0] =round((${$qvar}[2]/${$qss."_S_Avg"})*100,0);
			${$qss."_IDX"}[$qo][1] =round((${$qvar}[3]/${$qss."_C_Avg"})*100,0);
			${$qss."_IDX"}[$qo][2] =round((${$qvar}[4]/${$qss."_A_Avg"})*100,0);
			${$qss."_IDX"}[$qo][3] =round((${$qvar}[1]/${$qss."_N_Avg"})*100,0);
			${$qss."_IDX"}[$qo][4] =round((${$qvar}[0]/${$qss."_P_Avg"})*100,0);
		}
		
		$Question_Array[$qss][4][]=array(${$qss."_IDX"}[$qo][0],${$qss."_IDX"}[$qo][1],${$qss."_IDX"}[$qo][2],${$qss."_IDX"}[$qo][3],${$qss."_IDX"}[$qo][4]);
	}
}

//Calculate Over All Index
$qst   =array("Q2","Q5","Q6","Q7","Q9","Q10","Q11","Q12","Q13","Q14","Q17","Q22","Q23","Q24");

foreach($qst as $qss) {
	if(strlen($_SESSION["seas_brd_bnch_filter_condition"])>0) {
		${$qss."_IDX_OV"}[0] =round((${$qss."_S_Avg"}/$Overall_S_Avg2)*100,0);
		${$qss."_IDX_OV"}[1] =round((${$qss."_C_Avg"}/$Overall_C_Avg2)*100,0);
		${$qss."_IDX_OV"}[2] =round((${$qss."_A_Avg"}/$Overall_A_Avg2)*100,0);
		${$qss."_IDX_OV"}[3] =round((${$qss."_N_Avg"}/$Overall_N_Avg2)*100,0);
		${$qss."_IDX_OV"}[4] =round((${$qss."_P_Avg"}/$Overall_P_Avg2)*100,0);
	}
	else {
		${$qss."_IDX_OV"}[0] =round((${$qss."_S_Avg"}/$Overall_S_Avg)*100,0);
		${$qss."_IDX_OV"}[1] =round((${$qss."_C_Avg"}/$Overall_C_Avg)*100,0);
		${$qss."_IDX_OV"}[2] =round((${$qss."_A_Avg"}/$Overall_A_Avg)*100,0);
		${$qss."_IDX_OV"}[3] =round((${$qss."_N_Avg"}/$Overall_N_Avg)*100,0);
		${$qss."_IDX_OV"}[4] =round((${$qss."_P_Avg"}/$Overall_P_Avg)*100,0);
	}
	
	$Question_Array[$qss][6]=array(${$qss."_IDX_OV"}[0],${$qss."_IDX_OV"}[1],${$qss."_IDX_OV"}[4],${$qss."_IDX_OV"}[3],${$qss."_IDX_OV"}[2]);
}


$Question_Array["Q99"][4]=array(
							array($Q2_IDX_OV[0],$Q2_IDX_OV[1],$Q2_IDX_OV[2],$Q2_IDX_OV[3],$Q2_IDX_OV[4]),
							array($Q5_IDX_OV[0],$Q5_IDX_OV[1],$Q5_IDX_OV[2],$Q5_IDX_OV[3],$Q5_IDX_OV[4]),
							array($Q6_IDX_OV[0],$Q6_IDX_OV[1],$Q6_IDX_OV[2],$Q6_IDX_OV[3],$Q6_IDX_OV[4]),
							array($Q7_IDX_OV[0],$Q7_IDX_OV[1],$Q7_IDX_OV[2],$Q7_IDX_OV[3],$Q7_IDX_OV[4]),
							//array($Q8_IDX_OV[0],$Q8_IDX_OV[1],$Q8_IDX_OV[2],$Q8_IDX_OV[3],$Q8_IDX_OV[4]),
							array($Q9_IDX_OV[0],$Q9_IDX_OV[1],$Q9_IDX_OV[2],$Q9_IDX_OV[3],$Q9_IDX_OV[4]),
							array($Q10_IDX_OV[0],$Q10_IDX_OV[1],$Q10_IDX_OV[2],$Q10_IDX_OV[3],$Q10_IDX_OV[4]),
							array($Q11_IDX_OV[0],$Q11_IDX_OV[1],$Q11_IDX_OV[2],$Q11_IDX_OV[3],$Q11_IDX_OV[4]),
							array($Q12_IDX_OV[0],$Q12_IDX_OV[1],$Q12_IDX_OV[2],$Q12_IDX_OV[3],$Q12_IDX_OV[4]),
							array($Q13_IDX_OV[0],$Q13_IDX_OV[1],$Q13_IDX_OV[2],$Q13_IDX_OV[3],$Q13_IDX_OV[4]),
							array($Q14_IDX_OV[0],$Q14_IDX_OV[1],$Q14_IDX_OV[2],$Q14_IDX_OV[3],$Q14_IDX_OV[4]),
							array($Q17_IDX_OV[0],$Q17_IDX_OV[1],$Q17_IDX_OV[2],$Q17_IDX_OV[3],$Q17_IDX_OV[4]),
							//array($Q18_IDX_OV[0],$Q18_IDX_OV[1],$Q18_IDX_OV[2],$Q18_IDX_OV[3],$Q18_IDX_OV[4]),
							array($Q22_IDX_OV[0],$Q22_IDX_OV[1],$Q22_IDX_OV[2],$Q22_IDX_OV[3],$Q22_IDX_OV[4]),
							array($Q23_IDX_OV[0],$Q23_IDX_OV[1],$Q23_IDX_OV[2],$Q23_IDX_OV[3],$Q23_IDX_OV[4]),
							array($Q24_IDX_OV[0],$Q24_IDX_OV[1],$Q24_IDX_OV[2],$Q24_IDX_OV[3],$Q24_IDX_OV[4])
							);	

$Question_Array["Q99"][3]=array(
							array($Q2_P_Avg,$Q2_N_Avg,$Q2_S_Avg,$Q2_C_Avg,$Q2_A_Avg,0,0, $Q2_P_Avg2,$Q2_N_Avg2,$Q2_S_Avg2,$Q2_C_Avg2,$Q2_A_Avg2,0,0),
							array($Q5_P_Avg,$Q5_N_Avg,$Q5_S_Avg,$Q5_C_Avg,$Q5_A_Avg,0,0, $Q5_P_Avg2,$Q5_N_Avg2,$Q5_S_Avg2,$Q5_C_Avg2,$Q5_A_Avg2,0,0),
							array($Q6_P_Avg,$Q6_N_Avg,$Q6_S_Avg,$Q6_C_Avg,$Q6_A_Avg,0,0, $Q6_P_Avg2,$Q6_N_Avg2,$Q6_S_Avg2,$Q6_C_Avg2,$Q6_A_Avg2,0,0),
							array($Q7_P_Avg,$Q7_N_Avg,$Q7_S_Avg,$Q7_C_Avg,$Q7_A_Avg,0,0, $Q7_P_Avg2,$Q7_N_Avg2,$Q7_S_Avg2,$Q7_C_Avg2,$Q7_A_Avg2,0,0),
							//array($Q8_P_Avg,$Q8_N_Avg,$Q8_S_Avg,$Q8_C_Avg,$Q8_A_Avg,0,0, $Q8_P_Avg2,$Q8_N_Avg2,$Q8_S_Avg2,$Q8_C_Avg2,$Q8_A_Avg2,0,0),
							array($Q9_P_Avg,$Q9_N_Avg,$Q9_S_Avg,$Q9_C_Avg,$Q9_A_Avg,0,0, $Q9_P_Avg2,$Q9_N_Avg2,$Q9_S_Avg2,$Q9_C_Avg2,$Q9_A_Avg2,0,0),
							array($Q10_P_Avg,$Q10_N_Avg,$Q10_S_Avg,$Q10_C_Avg,$Q10_A_Avg,0,0, $Q10_P_Avg2,$Q10_N_Avg2,$Q10_S_Avg2,$Q10_C_Avg2,$Q10_A_Avg2,0,0),
							array($Q11_P_Avg,$Q11_N_Avg,$Q11_S_Avg,$Q11_C_Avg,$Q11_A_Avg,0,0, $Q11_P_Avg2,$Q11_N_Avg2,$Q11_S_Avg2,$Q11_C_Avg2,$Q11_A_Avg2,0,0),
							array($Q12_P_Avg,$Q12_N_Avg,$Q12_S_Avg,$Q12_C_Avg,$Q12_A_Avg,0,0, $Q12_P_Avg2,$Q12_N_Avg2,$Q12_S_Avg2,$Q12_C_Avg2,$Q12_A_Avg2,0,0),
							array($Q13_P_Avg,$Q13_N_Avg,$Q13_S_Avg,$Q13_C_Avg,$Q13_A_Avg,0,0, $Q13_P_Avg2,$Q13_N_Avg2,$Q13_S_Avg2,$Q13_C_Avg2,$Q13_A_Avg2,0,0),
							array($Q14_P_Avg,$Q14_N_Avg,$Q14_S_Avg,$Q14_C_Avg,$Q14_A_Avg,0,0, $Q14_P_Avg2,$Q14_N_Avg2,$Q14_S_Avg2,$Q14_C_Avg2,$Q14_A_Avg2,0,0),
							array($Q17_P_Avg,$Q17_N_Avg,$Q17_S_Avg,$Q17_C_Avg,$Q17_A_Avg,0,0, $Q17_P_Avg2,$Q17_N_Avg2,$Q17_S_Avg2,$Q17_C_Avg2,$Q17_A_Avg2,0,0),
							//array($Q18_P_Avg,$Q18_N_Avg,$Q18_S_Avg,$Q18_C_Avg,$Q18_A_Avg,0,0, $Q18_P_Avg2,$Q18_N_Avg2,$Q18_S_Avg2,$Q18_C_Avg2,$Q18_A_Avg2,0,0),
							array($Q22_P_Avg,$Q22_N_Avg,$Q22_S_Avg,$Q22_C_Avg,$Q22_A_Avg,0,0, $Q22_P_Avg2,$Q22_N_Avg2,$Q22_S_Avg2,$Q22_C_Avg2,$Q22_A_Avg2,0,0),
							array($Q23_P_Avg,$Q23_N_Avg,$Q23_S_Avg,$Q23_C_Avg,$Q23_A_Avg,0,0, $Q23_P_Avg2,$Q23_N_Avg2,$Q23_S_Avg2,$Q23_C_Avg2,$Q23_A_Avg2,0,0),
							array($Q24_P_Avg,$Q24_N_Avg,$Q24_S_Avg,$Q24_C_Avg,$Q24_A_Avg,0,0, $Q24_P_Avg2,$Q24_N_Avg2,$Q24_S_Avg2,$Q24_C_Avg2,$Q24_A_Avg2,0,0)
							);

$Question_Array["Q99"][5]=array($Overall_S_Avg,$Overall_C_Avg,$Overall_P_Avg,$Overall_N_Avg,$Overall_A_Avg, $Overall_S_Avg2,$Overall_C_Avg2,$Overall_P_Avg2,$Overall_N_Avg2,$Overall_A_Avg2);
$Question_Array["Q99"][6]=array(100,100,100,100,100);

//***-------------------------------------------------------------------------------***//
// Computations

// Handle Export to Excel Button
if(isset($_REQUEST['sel_export'])) {
	include_once("pcd/CreateExcel.php");
	exit;
}
// End Export to Excel Button

?>
<?php
function createGraph($qs,$rvopt=1) {

	if($_SERVER['HTTP_HOST']=="localhost") {
		$srvpath = "http://localhost/seas/";	
		$srvdir  = "C:/Program Files (x86)/Zend/Apache2/htdocs/seas/";
	}
	else {
		$srvpath = "http://www.arabdemog.com/jj/seas/";	
		$srvdir  = "/home/arabdemog/httpdocs/jj/seas/";
	}
	// Create graph Data
	$character=0;
	$strength=0;
	$bn_character=0;
	$bn_strength=0;
	
	if($rvopt==1) {
		$character=$qs[3];
		$strength =$qs[2];
		
		if(count($qs)>=10) {
			$bn_character=$qs[10];
			$bn_strength =$qs[9];
		}
	}
	else {
		$character=round($qs[3],0);
		$strength =round($qs[2],0);

		if(count($qs)>=10) {
			$bn_character=round($qs[10],0);
			$bn_strength =round($qs[9],0);
		}
	}
	// ---- //
	
			/* pChart library inclusions */
		 include_once("../pChart/class/pData.class.php");
		 include_once("../pChart/class/pDraw.class.php");
		 include_once("../pChart/class/pImage.class.php");
		
			
		 /* Create and populate the pData object */
		 $MyData = new pData();  
		 $MyData->addPoints($character,"Character"); 
		 $MyData->addPoints($strength,"Strength"); 
		
		 if(strlen($_SESSION["seas_brd_bnch_filter_condition"])>0) {
			 $MyData->addPoints($bn_character,"Benchmark Character"); 
			 $MyData->addPoints($bn_strength,"Benchmark Strength"); 
		 }
		
		 $MyData->setAxisName(0,"Age Pyramid");
		 //$MyData->addPoints(array("40+ Years","31-40 Years","21-30 Years"),"Age"); 
		 $MyData->setSerieDescription("Age","Age"); 
		 $MyData->setAbscissa("Age"); 
		 $MyData->setAxisDisplay(0,AXIS_FORMAT_CUSTOM,"YAxisFormat");
		 $serieSettings = array("R"=>145,"G"=>200,"B"=>255,"Alpha"=>100);
		 $MyData->setPalette("Strength",$serieSettings);
		
		 if($character>0) {
		 	$serieSettings = array("R"=>255,"G"=>0,"B"=>255,"Alpha"=>100);
		 }
		 else {
			$serieSettings = array("R"=>255,"G"=>0,"B"=>0,"Alpha"=>100);
		 }
		 $MyData->setPalette("Character",$serieSettings);
		
		 if(strlen($_SESSION["seas_brd_bnch_filter_condition"])>0) {
			 $serieSettings = array("R"=>145,"G"=>200,"B"=>255,"Alpha"=>100);
			 $MyData->setPalette("Benchmark Strength",$serieSettings);
			
			 if($bn_character>0) {
				$serieSettings = array("R"=>255,"G"=>0,"B"=>255,"Alpha"=>100);
			 }
			 else {
				$serieSettings = array("R"=>255,"G"=>0,"B"=>0,"Alpha"=>100);
			 }
			 $MyData->setPalette("Benchmark Character",$serieSettings);
		 }
		 
		 /* Create the pChart object */
		 $myPicture = new pImage(400,60,$MyData);
		 //$myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100));
		 //$myPicture->drawGradientArea(0,0,700,230,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20));

	
		 /* Set the default font properties */
		 $myPicture->setFontProperties(array("FontName"=>"../pChart/fonts/verdana.ttf","FontSize"=>8));
		
		 /* Draw the scale and the chart */
		 $AxisBoundaries = array(0=>array("Min"=>-100,"Max"=>100));
		 $myPicture->setGraphArea(-3,-3,390,60);
		 $myPicture->drawScale(array("DrawSubTicks"=>TRUE,"Mode"=>SCALE_MODE_MANUAL,"ManualScale"=>$AxisBoundaries,"Pos"=>SCALE_POS_TOPBOTTOM,'YMargin'=>0,'MinDivHeight'=>5, 'AxisR'=>255, 'AxisG'=>255,'AxisB'=>255 ));

		 //$myPicture->setShadow(FALSE);
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10)); 

		// $myPicture->drawBarChart(array("Gradient"=>TRUE,"GradientMode"=>GRADIENT_EFFECT_CAN,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayColor"=>DISPLAY_AUTO,"Gradient"=>TRUE,"Surrounding"=>-20,"InnerSurrounding"=>20));

		 /* Draw the chart */ 
		 $settings = array("Gradient"=>TRUE,"GradientMode"=>GRADIENT_EFFECT_CAN,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayR"=>0,"DisplayG"=>0,"DisplayB"=>0,"DisplayShadow"=>TRUE,"Surrounding"=>10);
		 $myPicture->drawBarChart($settings); 


		 /* Write the chart legend */
		 //$myPicture->drawLegend(365,320,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
		
		/* Render the picture (choose the best way) */ 
		$fileName= $_SERVER['DOCUMENT_ROOT'] . "/";
		$fname = "pcd/graphs/gp1_" . date("dmYHis") . rand() .".gif";
		
		$rmvfiles = "rm " . $_SERVER['DOCUMENT_ROOT'] . "/pcd/graphs/gp1*.gif";
		//shell_exec($rmvfiles);
		$myPicture->Render($fname);
		
		if($rvopt==1) {
			echo "<img src='". $srvpath .$fname. "?rnd=". rand() ."' align='center'>";				 
		}
		else {
   			return $srvdir .$fname;
		}
}
function YAxisFormat($Value) { return(abs($Value)); } 


function getAvg($qsarray,$typ,$rv_type,$debug=0) {

	//$rv_type=1  Strength
	//$rv_type=2  Tone
	//$rv_type=3  Positive
	//$rv_type=4  Negative
	//$rv_type=5  Average
	
	$SUM = 0;
	$N   = 0;
	$AnyAgree =0;
	$Nuetral  =0;
	$AnyDisAgree = 0;
	$Strength =0;
	$Tone =0;
	$Positive =0;
	$Negative =0;
	$Average =0;
	
	foreach($qsarray as $qs) {
		// get filter information
		$filter_sql ='select filter from seas_brd_dmap where dfields = "' . $qs . '"';
		$filter_rslt = mysql_query($filter_sql);
		$filter_text = "";
		if(mysql_num_rows($filter_rslt) >0) {
			$fil_array   = mysql_fetch_array($filter_rslt);
			$filter_text = $fil_array[0];
		}
		mysql_free_result($filter_rslt);
		
		$filter_text_condition = "";
		if(strlen(trim($filter_text)) >0) {
			$filter_text_ary = explode(",",$filter_text);
			$filter_text_ary[0] = trim(str_replace("FI","",$filter_text_ary[0]));
	
			// Need to check filter value is MV or SNG
			$filter_sql2 ='select mtype from seas_brd_dmap where dfields = "' . $filter_text_ary[0] . '"';
			$filter_rslt2 = mysql_query($filter_sql2);
			$fil_array2   = mysql_fetch_array($filter_rslt2);
			
			if(trim($fil_array2[0]) == "MV") {
				$filter_text_condition = "";
				$fldname = $filter_text_ary[0];
				unset($filter_text_ary[0]);
				foreach($filter_text_ary as $flttxt) {
					if(strlen($filter_text_condition) >0) $filter_text_condition .= " OR ";
					$filter_text_condition .= "SUBSTRING(" . $fldname . "," . $flttxt . ",1)=1";
				}
				if(strlen($filter_text_condition) >0) {
					$filter_text_condition = " (" . $filter_text_condition . ") ";
				}
			}
			else {
				$filter_text_condition = "(" . $filter_text_ary[0] . " in ("; 
				unset($filter_text_ary[0]);
				$filter_text_condition .= implode(",",$filter_text_ary) . ")) ";
			}
			mysql_free_result($filter_rslt2);
		}
		// end get filter info

		$sqlcond = "";
		if($typ==1) {		
			if(strlen($_SESSION["seas_brd_filter_condition"]) >0) {
				$sqlcond = $_SESSION["seas_brd_filter_condition"] . " and ";
			}
		}
		else {
			if(strlen($_SESSION["seas_brd_bnch_filter_condition"]) >0) {		// for benchmark
				$sqlcond = $_SESSION["seas_brd_bnch_filter_condition"] . " and ";
			}
		}
	
		if(strlen($filter_text_condition) >0) {
			$sqlcond .= $filter_text_condition . " and ";
		}
		
		$ExemptList = array("V15_2","V18_2");
		
		// N and SUM
		$sql = "select sum(weight),sum(weight*". $qs .") from seas_brd_dat where " .$sqlcond . $qs ." not in (0,9)";
		$rslt = mysql_query($sql);
		$mdat = mysql_fetch_array($rslt);
		$SUM  = $SUM + $mdat[1];
		$N    = $N   + $mdat[0];
		mysql_free_result($rslt);
		
		// Any Agree
		if(in_array(strtoupper(trim($qs)),$ExemptList)) {
			$sql = "select sum(weight) from seas_brd_dat where " .$sqlcond . $qs ." in (1,2)";
		}
		else {
			$sql = "select sum(weight) from seas_brd_dat where " .$sqlcond . $qs ." in (5,4)";		
		}
		$rslt = mysql_query($sql);
		$mdat = mysql_fetch_array($rslt);
		$AnyAgree  = $AnyAgree + $mdat[0];
		mysql_free_result($rslt);
		
		// Any Dis Agree
		if(in_array(strtoupper(trim($qs)),$ExemptList)) {
			$sql = "select sum(weight) from seas_brd_dat where " .$sqlcond . $qs ." in (4,5)";
		}
		else {
			$sql = "select sum(weight) from seas_brd_dat where " .$sqlcond . $qs ." in (2,1)";		
		}
		$rslt = mysql_query($sql);
		$mdat = mysql_fetch_array($rslt);
		$AnyDisAgree  = $AnyDisAgree + $mdat[0];
		mysql_free_result($rslt);		
		
		// Nuetral
		$sql = "select sum(weight) from seas_brd_dat where " .$sqlcond . $qs ." in (3)";
		$rslt = mysql_query($sql);
		$mdat = mysql_fetch_array($rslt);
		$Nuetral = $Nuetral + $mdat[0];
		mysql_free_result($rslt);	
		

	}
	
	$rv = 0;
	
	if($rv_type==1) { // Strength
		$rv = round((($AnyAgree + $AnyDisAgree) / $N) * 100,0);
	}
	if($rv_type==2) { // Tone
		$rv = round((($AnyAgree - $AnyDisAgree) / ($AnyAgree + $AnyDisAgree)) * 100,0);
	}
	if($rv_type==3) { // Positive
		$rv = round(($AnyAgree / $N) * 100,0);
	}
	if($rv_type==4) { // Negative
		$rv = round(($AnyDisAgree / $N) * 100,0);
	}
	if($rv_type==5) { // Average
		$rv = round((($SUM / $N) / 5) * 100,0);
	}
	
	
	return $rv;
}

/*
function getAvg_val($qsarray,$typ) {
	$SUM = 0;
	$N   = 0;
	foreach($qsarray as $qs) {
		
		// get filter information
		$filter_sql ='select filter from seas_brd_dmap where dfields = "' . $qs . '"';
		$filter_rslt = mysql_query($filter_sql);
		$filter_text = "";
		if(mysql_num_rows($filter_rslt) >0) {
			$fil_array   = mysql_fetch_array($filter_rslt);
			$filter_text = $fil_array[0];
		}
		mysql_free_result($filter_rslt);
		
		$filter_text_condition = "";
		if(strlen(trim($filter_text)) >0) {
			$filter_text_ary = explode(",",$filter_text);
			$filter_text_ary[0] = trim(str_replace("FI","",$filter_text_ary[0]));
	
			// Need to check filter value is MV or SNG
			$filter_sql2 ='select mtype from seas_brd_dmap where dfields = "' . $filter_text_ary[0] . '"';
			$filter_rslt2 = mysql_query($filter_sql2);
			$fil_array2   = mysql_fetch_array($filter_rslt2);
			
			if(trim($fil_array2[0]) == "MV") {
				$filter_text_condition = "";
				$fldname = $filter_text_ary[0];
				unset($filter_text_ary[0]);
				foreach($filter_text_ary as $flttxt) {
					if(strlen($filter_text_condition) >0) $filter_text_condition .= " OR ";
					$filter_text_condition .= "SUBSTRING(" . $fldname . "," . $flttxt . ",1)=1";
				}
				if(strlen($filter_text_condition) >0) {
					$filter_text_condition = " (" . $filter_text_condition . ") ";
				}
			}
			else {
				$filter_text_condition = "(" . $filter_text_ary[0] . " in ("; 
				unset($filter_text_ary[0]);
				$filter_text_condition .= implode(",",$filter_text_ary) . ")) ";
			}
			mysql_free_result($filter_rslt2);
		}
		// end get filter info
		
		if(strlen($_SESSION["seas_brd_filter_condition"]) >0) {
			$sqlcond = $_SESSION["seas_brd_filter_condition"] . " and ";
		}
		else {
			$sqlcond = "";
		}
		if(strlen($_SESSION["seas_brd_bnch_filter_condition"]) >0) {		// for benchmark
			$sqlcond2 = $_SESSION["seas_brd_bnch_filter_condition"] . " and ";
		}
		else {
			$sqlcond2 = "";
		}
		
		if(strlen($filter_text_condition) >0) {
			$sqlcond .= $filter_text_condition . " and ";
			$sqlcond2 .= $filter_text_condition . " and ";
		}
				
		
		if($typ==1) {
			$sql = "select sum(weight),sum(weight*". $qs .") from seas_brd_dat where " .$sqlcond . $qs ." not in (0,9)";
		}
		else {
			$sql = "select sum(weight),sum(weight*". $qs .") from seas_brd_dat where " .$sqlcond2 . $qs ." not in (0,9)";	
		}
		$rslt = mysql_query($sql);
		$mdat = mysql_fetch_array($rslt);
		$SUM  = $SUM + $mdat[1];
		$N    = $N   + $mdat[0];
		mysql_free_result($rslt);
	}
	
	$rv = round((($SUM / $N) / 5) * 100,0);
	
	return $rv;
}
*/

function getValues($weighted=false,$qs,$optary=array(5,4,3,2,1,9)) {
	
	// get filter information
	$filter_sql ='select filter from seas_brd_dmap where dfields = "' . $qs . '"';
	$filter_rslt = mysql_query($filter_sql);
	$filter_text = "";
	if(mysql_num_rows($filter_rslt) >0) {
		$fil_array   = mysql_fetch_array($filter_rslt);
		$filter_text = $fil_array[0];
	}
	mysql_free_result($filter_rslt);
	
	$filter_text_condition = "";
	if(strlen(trim($filter_text)) >0) {
		$filter_text_ary = explode(",",$filter_text);
		$filter_text_ary[0] = trim(str_replace("FI","",$filter_text_ary[0]));

		// Need to check filter value is MV or SNG
		$filter_sql2 ='select mtype from seas_brd_dmap where dfields = "' . $filter_text_ary[0] . '"';
		$filter_rslt2 = mysql_query($filter_sql2);
		$fil_array2   = mysql_fetch_array($filter_rslt2);
		
		if(trim($fil_array2[0]) == "MV") {
			$filter_text_condition = "";
			$fldname = $filter_text_ary[0];
			unset($filter_text_ary[0]);
			foreach($filter_text_ary as $flttxt) {
				if(strlen($filter_text_condition) >0) $filter_text_condition .= " OR ";
				$filter_text_condition .= "SUBSTRING(" . $fldname . "," . $flttxt . ",1)=1";
			}
			if(strlen($filter_text_condition) >0) {
				$filter_text_condition = " (" . $filter_text_condition . ") ";
			}
		}
		else {
			$filter_text_condition = "(" . $filter_text_ary[0] . " in ("; 
			unset($filter_text_ary[0]);
			$filter_text_condition .= implode(",",$filter_text_ary) . ")) ";
		}
		mysql_free_result($filter_rslt2);
	}
	// end get filter info
	
	if(strlen($_SESSION["seas_brd_filter_condition"]) >0) {
		$sqlcond = $_SESSION["seas_brd_filter_condition"] . " and ";
	}
	else {
		$sqlcond = "";
	}
	if(strlen($_SESSION["seas_brd_bnch_filter_condition"]) >0) {		// for benchmark
		$sqlcond2 = $_SESSION["seas_brd_bnch_filter_condition"] . " and ";
	}
	else {
		$sqlcond2 = "";
	}
	
	if(strlen($filter_text_condition) >0) {
		$sqlcond .= $filter_text_condition . " and ";
		$sqlcond2 .= $filter_text_condition . " and ";
	}
	
	$response = array(0,0,0,0,0,0,0,0,0,0,0,0,0,0);  // Any Agree, Any DisAgree, Strength, Character, Average, Nuetral, Total
												     // Any Agree, Any DisAgree, Strength, Character, Average, Nuetral, Total - for benchmark
	
	// Total
	if($weighted==false) {
		$s_sql = "select count(". $qs .") from seas_brd_dat where " . $sqlcond . $qs ." not in (". $optary[5] .") ";
	}
	else {
		$s_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond . $qs ." not in (". $optary[5] .") ";
	}
	
	$s_rslt = mysql_query($s_sql);
	$s_array= mysql_fetch_array($s_rslt);
	$response[6] = $s_array[0];
	mysql_free_result($s_rslt);

		// For Benchmark
		if($weighted==false) {
			$s_sql = "select count(". $qs .") from seas_brd_dat where " . $sqlcond2 . $qs ." not in (". $optary[5] .") ";
		}
		else {
			$s_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond2 . $qs ." not in (". $optary[5] .") ";
		}
		$s_rslt = mysql_query($s_sql);
		$s_array= mysql_fetch_array($s_rslt);
		$response[13] = $s_array[0];
		mysql_free_result($s_rslt);

		
	// Any Agree
	if($weighted==false) {
		$s_sql = "select count(". $qs .") from seas_brd_dat where " . $sqlcond . $qs ." in (". $optary[0] ."," . $optary[1] .") ";
	}
	else {
		$s_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond . $qs ." in (". $optary[0] ."," . $optary[1] .") ";
	}

	$s_rslt = mysql_query($s_sql);
	$s_array= mysql_fetch_array($s_rslt);
	$response[0] = round(($s_array[0]/$response[6])*100,0);
	$AgreeCount = $s_array[0];
	mysql_free_result($s_rslt);

		// For Benchmark
		if($weighted==false) {
			$s_sql = "select count(". $qs .") from seas_brd_dat where " . $sqlcond2 . $qs ." in (". $optary[0] ."," . $optary[1] .") ";
		}
		else {
			$s_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond2 . $qs ." in (". $optary[0] ."," . $optary[1] .") ";
		}

		$s_rslt = mysql_query($s_sql);
		$s_array= mysql_fetch_array($s_rslt);
		$response[7] = round(($s_array[0]/$response[13])*100,0);
		$AgreeCount2 = $s_array[0];
		mysql_free_result($s_rslt);

	// Nuetral
	if($weighted==false) {
		$s_sql = "select count(". $qs .") from seas_brd_dat where " . $sqlcond . $qs ." in (". $optary[2] .") ";
	}
	else {
		$s_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond . $qs ." in (". $optary[2] .") ";
	}
	$s_rslt = mysql_query($s_sql);
	$s_array= mysql_fetch_array($s_rslt);
	$response[5] = $s_array[0];
	mysql_free_result($s_rslt);
		
		// For Benchmark
		if($weighted==false) {
			$s_sql = "select count(". $qs .") from seas_brd_dat where " . $sqlcond2 . $qs ." in (". $optary[2] .") ";
		}
		else {
			$s_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond2 . $qs ." in (". $optary[2] .") ";
		}
		$s_rslt = mysql_query($s_sql);
		$s_array= mysql_fetch_array($s_rslt);
		$response[12] = $s_array[0];
		mysql_free_result($s_rslt);
		

	// Dis Agree
	if($weighted==false) {
		$s_sql = "select count(". $qs .") from seas_brd_dat where " . $sqlcond . $qs ." in (". $optary[3] ."," . $optary[4] .") ";
	}
	else {
		$s_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond . $qs ." in (". $optary[3] ."," . $optary[4] .") ";
	}
	$s_rslt = mysql_query($s_sql);
	$s_array= mysql_fetch_array($s_rslt);
	$response[1] = round(($s_array[0]/$response[6])*100,0);
	$DisagreeCount = $s_array[0];
	mysql_free_result($s_rslt);
		
		// For Benchmark
		if($weighted==false) {
			$s_sql = "select count(". $qs .") from seas_brd_dat where " . $sqlcond2 . $qs ." in (". $optary[3] ."," . $optary[4] .") ";
		}
		else {
			$s_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond2 . $qs ." in (". $optary[3] ."," . $optary[4] .") ";
		}
		$s_rslt = mysql_query($s_sql);
		$s_array= mysql_fetch_array($s_rslt);
		$response[8] = round(($s_array[0]/$response[13])*100,0);
		$DisagreeCount2 = $s_array[0];
		mysql_free_result($s_rslt);	
		
	// Average
	if($weighted==false) {
		$s_sql = "select sum(1),sum(1*" . $qs . ") from seas_brd_dat where " . $sqlcond. $qs." not in (0,". $optary[5] .")";
	}
	else {
		$s_sql = "select sum(weight),sum(weight*" . $qs . ") from seas_brd_dat where " . $sqlcond. $qs." not in (0,9)";
	}
	$s_rslt = mysql_query($s_sql);
	$s_array= mysql_fetch_array($s_rslt);
	$response[4] = round((($s_array[1] / $s_array[0]) / 5*100),0);
	mysql_free_result($s_rslt);	

		// For Benchmark
		if($weighted==false) {
			$s_sql = "select sum(1),sum(1*" . $qs . ") from seas_brd_dat where " . $sqlcond2. $qs." not in (0,". $optary[5] .")";
		}
		else {
			$s_sql = "select sum(weight),sum(weight*" . $qs . ") from seas_brd_dat where " . $sqlcond2. $qs." not in (0,9)";
		}
		$s_rslt = mysql_query($s_sql);
		$s_array= mysql_fetch_array($s_rslt);
		$response[11] = round((($s_array[1] / $s_array[0]) / 5*100),0);
		mysql_free_result($s_rslt);		
	
	// IMAGE STRENGTH
	if($response[6] >0) {
		$response[2] = round((($AgreeCount + $DisagreeCount) / $response[6])*100,0);
		$response[9] = round((($AgreeCount2 + $DisagreeCount2) / $response[13])*100,0); // For Benchmark
	}
	else {
		$response[2] = 0;
		$response[9] = 0;
	}
	// IMAGE CHARACTER
	if($AgreeCount + $DisagreeCount >0) {
		$response[3] = round((($AgreeCount - $DisagreeCount) / ($AgreeCount + $DisagreeCount))*100,0);	
		$response[10] = round((($AgreeCount2 - $DisagreeCount2) / ($AgreeCount2 + $DisagreeCount2))*100,0);	
	}
	else {
		$response[3] = 0;
		$response[10] = 0;
	}

	return $response;
}

function CreateRadar($Strength_Avg,$Character_Avg,$Positive_Avg,$Negative_Avg,$Average_Avg,$labls,$gtitle="") {
	
	if($_SESSION["seas_brd_language"]==2) {
		$font1 ="AGA-Rasheeq-Regular.ttf";
		$font2 ="pf_arma_five.ttf";
		$label_fontsize =16;
		$label_fontsize2 =12;
		$legend_fontsize =20;
		$positive_txt = "إيجابي";
		$negative_txt = "سلبي";
		
		try {
			include_once("pcd/I18N/Arabic.php"); 
			$Arabic = new I18N_Arabic('Glyphs'); 
			foreach($labls as $ky=>$lbls) {
				$labls[$ky] = $Arabic->utf8Glyphs($lbls); 
			}			
			$positive_txt = $Arabic->utf8Glyphs($positive_txt);
			$negative_txt = $Arabic->utf8Glyphs($negative_txt);
		}
		catch (Exception $e) {
			//echo 'Caught exception: ',  $e->getMessage(), "\n";
			//exit;
		}

	}
	else {
		$font1 ="verdana.ttf";
		$font2 ="pf_arma_five.ttf";
		$label_fontsize =9;
		$label_fontsize2 =8;
		$legend_fontsize =7;
	}
	
	$usr_opts = explode(",",$_SESSION['PCD_user_opts']);
	
	foreach($Strength_Avg as $ky=>$savg) {
			$Strength_Avg[$ky] = round($savg,0);
	}
	foreach($Character_Avg as $ky=>$savg) {
			$Character_Avg[$ky] = round($savg,0);
	}
	foreach($Positive_Avg as $ky=>$savg) {
			$Positive_Avg[$ky] = round($savg,0);
	}
	foreach($Negative_Avg as $ky=>$savg) {
			if($usr_opts[5]==1) {
				$Negative_Avg[$ky] = round($savg,0);
			}
			else {
					$Negative_Avg[$ky] = round($savg*-1,0);
			}
	}
	foreach($Average_Avg as $ky=>$savg) {
			$Average_Avg[$ky] = round($savg,0);
	}

	if($_SERVER['HTTP_HOST']=="localhost") {
		$srvpath = "http://localhost/seas/";	
	}
	else {
		$srvpath = "http://www.arabdemog.com/jj/seas/";	
	}
	


	// Create graph Data
	//$character=$qs[3];
	//$strength =$qs[2];
	// ---- //
	
			/* pChart library inclusions */
		 include_once("../pChart/class/pData.class.php");
		 include_once("../pChart/class/pDraw.class.php");
		 if($usr_opts[5]==1) {
			 include_once("../pChart/class/pRadar.class.php"); 
		 }
		 include_once("../pChart/class/pImage.class.php"); 		 
		
		 
		 /* Create and populate the pData object */
		 $MyData = new pData();  


		 if($_SESSION["seas_brd_language"]==2) {
			 //$MyData->addPoints($Strength_Avg,"Strength");   
			 //$MyData->addPoints($Character_Avg,"Character");  
			 $MyData->addPoints($Positive_Avg,"Positive");  
			 $MyData->addPoints($Negative_Avg,"Negative");  
			 //$MyData->addPoints($Average_Avg,"Average");  
			 
			 //$MyData->setSerieDescription("Strength","Opinion Strength"); 
			 //$MyData->setSerieDescription("Character","Opinion Character"); 
			 $MyData->setSerieDescription("Positive",$positive_txt); 
			 $MyData->setSerieDescription("Negative",$negative_txt); 

		 }
		 else {
			 //$MyData->addPoints($Strength_Avg,"Strength");   
			 //$MyData->addPoints($Character_Avg,"Character");  
			 $MyData->addPoints($Positive_Avg,"Positive");  
			 $MyData->addPoints($Negative_Avg,"Negative");  
			 //$MyData->addPoints($Average_Avg,"Average");  
			 
			 //$MyData->setSerieDescription("Strength","Opinion Strength"); 
			 //$MyData->setSerieDescription("Character","Opinion Character"); 
			 $MyData->setSerieDescription("Positive","Opinion Positive"); 
			 $MyData->setSerieDescription("Negative","Opinion Negative"); 
		 }
		 //$MyData->setSerieDescription("Average","Opinion Average"); 		 
		 $MyData->setSerieWeight("Strength",2); 

		 /* Define the absissa serie */ 
		 $MyData->addPoints($labls,"Labels"); 
		 $MyData->setAbscissa("Labels"); 

		 //$serieSettings = array("R"=>79,"G"=>129,"B"=>189,"Alpha"=>100);
		 //$MyData->setPalette("Strength",$serieSettings);
		 //$serieSettings = array("R"=>0,"G"=>176,"B"=>80,"Alpha"=>100);
		 //$MyData->setPalette("Character",$serieSettings);	
		 $serieSettings = array("R"=>0,"G"=>255,"B"=>0,"Alpha"=>100);
		 $MyData->setPalette("Positive",$serieSettings);	
		 $serieSettings = array("R"=>255,"G"=>0,"B"=>0,"Alpha"=>100);
		 $MyData->setPalette("Negative",$serieSettings);	
		 //$serieSettings = array("R"=>255,"G"=>153,"B"=>0,"Alpha"=>100);
		 //$MyData->setPalette("Average",$serieSettings);		 

	
		if($usr_opts[5]==1) {
			 /* Create the pChart object */
			 $myPicture = new pImage(1200,1000,$MyData); 
			 /* Set the default font properties */
			 $myPicture->setFontProperties(array("FontName"=>"../pChart/fonts/".$font1,"FontSize"=>$label_fontsize));
			 /* Enable shadow computing */  
			 $myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>9)); 
			 /* Create the pRadar object */  
			 $SplitChart = new pRadar(); 
			/* Draw a radar chart */  
			 $myPicture->setGraphArea(250,10,950,950); 
			 //$Options = array("DrawPoly"=>TRUE,"PolyAlpha"=>100,"AxisRotation"=>270,"WriteValues"=>TRUE,"WriteValuesInBubble"=>TRUE,"ValueFontSize"=>7,"Layout"=>RADAR_LAYOUT_STAR,"LabelPos"=>RADAR_LABELS_HORIZONTAL,"BackgroundGradient"=>array("StartR"=>255,"StartG"=>255,"StartB"=>255,"StartAlpha"=>100,"EndR"=>207,"EndG"=>227,"EndB"=>125,"EndAlpha"=>50), "FontName"=>"../pChart/fonts/pf_arma_five.ttf","FontSize"=>6); 
			 $Options = array("DrawPoly"=>FALSE,"AxisRotation"=>270,"FixedMax"=>100,"WriteValues"=>TRUE,"WriteValuesInBubble"=>TRUE,"ValueFontSize"=>$label_fontsize2,"Layout"=>RADAR_LAYOUT_STAR,"LabelPos"=>RADAR_LABELS_HORIZONTAL, "FontName"=>"../pChart/fonts/".$font2,"FontSize"=>9,"AxisAlpha"=>30); 
			 $SplitChart->drawRadar($myPicture,$MyData,$Options);
			 /* Write the chart legend */  
			 $myPicture->setFontProperties(array("FontName"=>"../pChart/fonts/".$font1,"FontSize"=>$legend_fontsize)); 
			 if($_SESSION["seas_brd_language"]==2) {
			 	$myPicture->drawLegend(550,900,array("Style"=>LEGEND_BOX,"Mode"=>LEGEND_HORIZONTAL)); 
			 }
			 else {
				 $myPicture->drawLegend(500,900,array("Style"=>LEGEND_BOX,"Mode"=>LEGEND_HORIZONTAL)); 
			 }
		}
		else {
			 /* Create the pChart object */
			 $myPicture = new pImage(1100,650,$MyData); 
			 $myPicture->drawGradientArea(0,0,1100,650,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>206,"EndG"=>206,"EndB"=>206,"Alpha"=>50)); 
			 $myPicture->drawGradientArea(0,0,1100,650,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>206,"EndG"=>206,"EndB"=>206,"Alpha"=>20)); 			 
			 /* Set the default font properties */
			 $myPicture->setFontProperties(array("FontName"=>"../pChart/fonts/".$font1,"FontSize"=>$label_fontsize));
			 /* Enable shadow computing */  
			 $myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10)); 			
			 /* Draw the chart */ 
			 $myPicture->setGraphArea(150,20,1000,400); 
			 $myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10,"LabelRotation"=>45));

			$settings = array("Gradient"=>TRUE,"GradientMode"=>GRADIENT_EFFECT_CAN,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayR"=>0,"DisplayG"=>0,"DisplayB"=>0,"DisplayShadow"=>TRUE,"Surrounding"=>10);
			 $myPicture->drawStackedBarChart($settings); 
			 /* Write the chart legend */  
			 $myPicture->setFontProperties(array("FontName"=>"../pChart/fonts/".$font1,"FontSize"=>$legend_fontsize)); 
			 $myPicture->drawLegend(1005,40,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_VERTICAL)); 
		}
		 

		/* Render the picture (choose the best way) */ 
		$fileName= $_SERVER['DOCUMENT_ROOT'] . "/";
		$fname = "pcd/graphs/gp1_" . date("dmYHis") . rand() .".gif";
		
		$rmvfiles = "rm " . $_SERVER['DOCUMENT_ROOT'] . "/pcd/graphs/gp3*.gif";
		//shell_exec($rmvfiles);
		$myPicture->Render($fname);
		
		if($usr_opts[5]!=1) {
			echo "<table border=5 style='border-color:#fff;border-collapse: collapse;' cellpadding=15 cellspacing=0 width='100%'>";
			echo "	<tr>";
			echo "		<td background='pcd/images/dash.png' align='center'>";
			echo 			"<strong><span class='question'>" . $gtitle . "</span></strong><br><br>";
		}

		echo "			<img src='". $srvpath .$fname. "?rnd=". rand() ."' border='0' width='100%'>";

		if($usr_opts[5]!=1) {
			echo "		</td>";
			echo "	</tr>";
			echo "</table>";
		}
}


/*
function getAvg($Q,$pos) {
	$cn = count($Q);
	$sm = 0;
	foreach($Q as $Qval) {
		$sm = $sm + $Qval[$pos];
	}
	$av = round($sm/$cn,0);
	return $av;
}
*/


function pr_index($Q,$language_array) {
	echo '<p class="indx">';
	echo '<a href="#" title="' .$language_array['index'] .'" class="indx">';
	echo $Q;
	echo '</a>';	
	echo '</p>';
	
}
function pr_benchmark($Q,$language_array) {
	if(strlen($_SESSION["seas_brd_bnch_filter_condition"])>0) {
		echo '<p class="bnch" >';
		echo '<a href="#" title="' .$language_array['benchmark'] .'" class="bnch">';
		echo $Q;
		echo '</a>';
		echo '</p>';
	}
}
function pr_target($Q,$language_array) {
	echo '<p class="trgt">';
	echo '<a href="#" title="' .$language_array['target'] .'" class="trgt">';
	echo $Q;
	echo '</a>';
	echo '</p>';
}
?>