<?php
require_once(getabspath("include/xml.php"));
require_once(getabspath("classes/cipherer.php"));
require_once(getabspath("classes/searchclause.php"));
function getReportArray($name)
{
	$arr = array();
	$xml = new xml();
	$rpt_strXML = LoadSelectedReport($name);
	$arr=$xml->xml_to_array($rpt_strXML);
	if(count($arr))
	{
		$_SESSION["webobject"]["table_type"]=$arr["table_type"];
		$_SESSION["object_sql"]=$arr["sql"];
		if($arr["table_type"]=="custom")
		{
			global $conn;
			$tables_query=$arr["tables"][0];
			$strSQL = "SELECT ".AddFieldWrappers("sqlcontent")." FROM ".AddTableWrappers("webreport_sql")." WHERE ".AddFieldWrappers("sqlname")."='".$tables_query."'";
			$rs = db_query($strSQL,$conn);
			if($row = db_fetch_numarray($rs))
				$_SESSION["object_sql"]=$row[0];
		}
	}
	return $arr;
}

function getChartArray($name)
{
	$arr = array();
	$xml = new xml();
	$chrt_strXML = LoadSelectedChart($name);
	$arr=$xml->xml_to_array($chrt_strXML);
	if(count($arr))
	{
		$_SESSION["webobject"]["table_type"]=$arr["table_type"];
		$_SESSION["object_sql"]=$arr["sql"];
		if($arr["table_type"]=="custom")
		{
			global $conn;
			$tables_query=$arr["tables"][0];
			$strSQL = "SELECT ".AddFieldWrappers("sqlcontent")." FROM ".AddTableWrappers("webreport_sql")." WHERE ".AddFieldWrappers("sqlname")."='".$tables_query."'";
			$rs = db_query($strSQL,$conn);
			if($row = db_fetch_numarray($rs))
				$_SESSION["object_sql"]=$row[0];
		}
	}
	return $arr;
}

function GetUserGroups() {
global $wr_is_standalone,$conn;
		$arr = array(
		array(-1, "<"."Administrador".">"),
		array(-2, "<"."Por defecto".">"),
		array(-3, "<"."Invitado".">")
	);
	$rs=db_query("select ".AddFieldWrappers("GroupID").",".AddFieldWrappers("Label")." from `minigest_uggroups` order by ".AddFieldWrappers("Label"),$conn);
	while($data = db_fetch_numarray( $rs )) 
		$arr[]=array($data[0],$data[1]);
	return $arr;
}

function GetUserGroup() 
{
global $conn,$wr_is_standalone;
	
	if(@$_SESSION["UserID"]!="Guest")
	{
		ReadUserPermissions();
		return $_SESSION["UserRights"][$_SESSION["UserID"]][".Groups"];
	}
	else 
		return array(-3);
	return array();
}

function CheckLastID($type) {
	global $conn;
	
	$strSQL = "SELECT ".AddFieldWrappers("rpt_id")." FROM ".AddTableWrappers("webreports")." WHERE ".AddFieldWrappers("rpt_type")." = '".$type."'";
	$rs = db_query($strSQL,$conn);
	$maxID = 0;
	
	while( $row = db_fetch_numarray( $rs ) ) {
		if ( $maxID < $row[0] ) {
			$maxID = $row[0];
		}
	}
	
	return ++$maxID;
}

function GetNumberFieldsList($table) {
	$t = WRGetFieldsList($table);
	$arr=array();
	foreach($t as $f)
	{
		if(!is_wr_custom())
			$ftype=WRGetFieldType($table.".".$f);
		else
			$ftype=WRCustomGetFieldType($table,$f);
		if(IsNumberType($ftype))
			$arr[]=$f;
	}
	return $arr;
}

function WRGetNBFieldsList($table) {
	$t = WRGetFieldsList($table);
	$arr=array();
	foreach($t as $f)
	{
		if(!is_wr_custom())
			$ftype=WRGetFieldType($table.".".$f);
		else
			$ftype=WRCustomGetFieldType($table,$f);
		if(!IsBinaryType($ftype))
			$arr[]=$f;
	}
	return $arr;
}

function GetChartsList() {
	global $conn;

	$xml = new xml();
	$arr=array();
	$strSQL = "SELECT ".AddFieldWrappers("rpt_name").", ".AddFieldWrappers("rpt_title").", ".AddFieldWrappers("rpt_owner").", ".AddFieldWrappers("rpt_status").", ".AddFieldWrappers("rpt_content");
	$strSQL .= " FROM ".AddTableWrappers("webreports")." WHERE ".AddFieldWrappers("rpt_type")." = 'chart' order by ".AddFieldWrappers("rpt_title");
	$rsChart = db_query($strSQL,$conn);
	$arrUserGroup = GetUserGroup();
	
	while( $row = db_fetch_numarray( $rsChart ) ) {
		$chart_arr = $xml->xml_to_array( escapeEntities($row[4]) );
        $view = 0;
		$edit = 0;
        
		if ( isset($chart_arr['permissions']) ) {
			foreach ( $chart_arr['permissions'] as $arr_prm ) {
				if (in_array($arr_prm['id'], $arrUserGroup)) {
					$view = ( $arr_prm['view'] == "true" ) ? 1 : 0;
					$edit = ( $arr_prm['edit'] == "true" ) ? 1 : 0;
				}
			}
		}
		else
		{
			$view=1;
		}
		if(!$chart_arr["tmp_active"])
		{
		$arr[] = array(
			"name"		=> $row[0],
			"title"		=> $row[1],
			"owner"		=> $row[2],
			"status"	=> $row[3],
			"view"		=> $view,
			"edit"		=> $edit
		);
	}
	}

	return $arr;
}

function LoadSelectedChart($report) {
	global $conn;
	$strSQL = "SELECT ".AddFieldWrappers("rpt_content")." FROM ".AddTableWrappers("webreports")." WHERE ".AddFieldWrappers("rpt_name")."=".db_prepare_string($report)." and ".AddFieldWrappers("rpt_type")."='chart'";
	$rsReport = db_query($strSQL,$conn);
	$rptContent = db_fetch_numarray($rsReport);
	return escapeEntities($rptContent[0]);	
}

function SaveChart($reportname, $report, $rtitle, $rstatus, $strXML, $saveas) {
	global $conn;
	if(!@$_SESSION["UserID"])
		$_SESSION["UserID"]=" ";
	$reportname=GoodFieldName($reportname);
	$report=GoodFieldName($report);
	
	$strSQL = "SELECT ".AddFieldWrappers("rpt_id")." FROM ".AddTableWrappers("webreports")." WHERE ".AddFieldWrappers("rpt_name")."=".db_prepare_string($reportname)." and ".AddFieldWrappers("rpt_type")."='chart'";
	$rsReport = db_query($strSQL,$conn);
	$data=db_fetch_array($rsReport);
	if ( $data && (!$saveas || $reportname==$report)) {
		$strSQL = "UPDATE ".AddTableWrappers("webreports")." SET ".AddFieldWrappers("rpt_name")."=".db_prepare_string($report).",".AddFieldWrappers("rpt_title")."=".db_prepare_string($rtitle).", ".AddFieldWrappers("rpt_content")."=".db_prepare_string($strXML).", ".AddFieldWrappers("rpt_status")."=".db_prepare_string($rstatus).", ".AddFieldWrappers("rpt_mdate")."='".now()."' WHERE ".AddFieldWrappers("rpt_name")."=".db_prepare_string($reportname)." and ".AddFieldWrappers("rpt_type")."='chart'";
		$rsReport = db_exec($strSQL,$conn);
	} else {
		$strSQL = "INSERT INTO ".AddTableWrappers("webreports")." ( ".AddFieldWrappers("rpt_name").", ".AddFieldWrappers("rpt_title").", ".AddFieldWrappers("rpt_cdate").", ".AddFieldWrappers("rpt_mdate").", ".AddFieldWrappers("rpt_content").", ".AddFieldWrappers("rpt_owner").", ".AddFieldWrappers("rpt_status").", ".AddFieldWrappers("rpt_type")." )";
		$strSQL .= " VALUES(".db_prepare_string($report).", ".db_prepare_string($rtitle).", '".now()."', '".now()."', ".db_prepare_string($strXML).", ".db_prepare_string(@$_SESSION["UserID"]).", ".db_prepare_string($rstatus).", 'chart')";
		$rsReport = db_exec($strSQL,$conn);
	}
}

function DeleteChart($report) {
	global $conn;
	
	$strSQL = "DELETE FROM ".AddTableWrappers("webreports")." WHERE ".AddFieldWrappers("rpt_name")."=".db_prepare_string($report)." and ".AddFieldWrappers("rpt_type")."='chart'";
	$rsReport = db_exec($strSQL,$conn);
}

function GetReportsList() {
	global $conn;

	$xml = new xml();
	$arr=array();
	$strSQL = "SELECT ".AddFieldWrappers("rpt_name").", ".AddFieldWrappers("rpt_title").", ".AddFieldWrappers("rpt_owner").", ".AddFieldWrappers("rpt_status").", ".AddFieldWrappers("rpt_content");
	$strSQL .= " FROM ".AddTableWrappers("webreports")." WHERE ".AddFieldWrappers("rpt_type")." = 'report' order by ".AddFieldWrappers("rpt_title");
	$rsReport = db_query($strSQL,$conn);
	
	$arrUserGroup = GetUserGroup();
	while( $row = db_fetch_numarray( $rsReport ) ) {
		$report_arr = $xml->xml_to_array( escapeEntities($row[4]) );
        $view = 0;
		$edit = 0;

		if ( isset($report_arr['permissions']) ) {
			foreach ( $report_arr['permissions'] as $arr_prm ) {
				if (in_array($arr_prm['id'], $arrUserGroup)) {
					$view = ( $arr_prm['view'] == "true" ) ? 1 : 0;
					$edit = ( $arr_prm['edit'] == "true" ) ? 1 : 0;
				}
			}
		}
		else
		{
			$view=1;
		}

		if(!$report_arr["tmp_active"])
		{
		$arr[] = array(
			"name"		=> $row[0],
			"title"		=> $row[1],
			"owner"		=> $row[2],
			"status"	=> $row[3],
			"view"		=> $view,
			"edit"		=> $edit
		);
	}
	}
	
	return $arr;
}

function LoadSelectedReport($report) {
	global $conn;
	$strSQL = "SELECT ".AddFieldWrappers("rpt_content")." FROM ".AddTableWrappers("webreports")." WHERE ".AddFieldWrappers("rpt_name")."=".db_prepare_string($report)." and ".AddFieldWrappers("rpt_type")."='report'";
	$rsReport = db_query($strSQL,$conn);
	$rptContent = db_fetch_numarray($rsReport);
	return escapeEntities($rptContent[0]);	
}

function SaveReport($reportname, $report, $rtitle, $rstatus, $strXML, $saveas) {
	global $conn;
	$reportname=GoodFieldName($reportname);
	$report=GoodFieldName($report);
	if(!@$_SESSION["UserID"])
		$_SESSION["UserID"]=" ";
	$strSQL = "SELECT ".AddFieldWrappers("rpt_id")." FROM ".AddTableWrappers("webreports")." WHERE ".AddFieldWrappers("rpt_name")."=".db_prepare_string($reportname)." and ".AddFieldWrappers("rpt_type")."='report'";
	$rsReport = db_query($strSQL,$conn);
	$data=db_fetch_array($rsReport);
	if ( $data && (!$saveas || $reportname==$report)) {
		$strSQL = "UPDATE ".AddTableWrappers("webreports")." SET ".AddFieldWrappers("rpt_name")."=".db_prepare_string($report).", ".AddFieldWrappers("rpt_title")."=".db_prepare_string($rtitle).", ".AddFieldWrappers("rpt_content")."=".PrepareString4DB($strXML).", ".AddFieldWrappers("rpt_status")."=".db_prepare_string($rstatus).", ".AddFieldWrappers("rpt_mdate")."='".now()."' WHERE ".AddFieldWrappers("rpt_name")."=".db_prepare_string($reportname)." and ".AddFieldWrappers("rpt_type")."='report'";
		$rsReport = db_exec($strSQL,$conn);
	} else {
		$strSQL = "INSERT INTO ".AddTableWrappers("webreports")." ( ".AddFieldWrappers("rpt_name").", ".AddFieldWrappers("rpt_title").", ".AddFieldWrappers("rpt_cdate").", ".AddFieldWrappers("rpt_mdate").", ".AddFieldWrappers("rpt_content").", ".AddFieldWrappers("rpt_owner").", ".AddFieldWrappers("rpt_status").", ".AddFieldWrappers("rpt_type")." )";
		$strSQL .= " VALUES(".db_prepare_string($report).", ".db_prepare_string($rtitle).", '".now()."', '".now()."', ".PrepareString4DB($strXML).", ".db_prepare_string(@$_SESSION["UserID"]).", ".db_prepare_string($rstatus).", 'report')";		
		$rsReport = db_exec($strSQL,$conn);
	}
	$strSQL = "UPDATE ".AddTableWrappers("webreport_style")." set ".AddFieldWrappers("repname")."=".db_prepare_string($report)." where ".AddFieldWrappers("repname")."='".$_SESSION['webreports_oldname']."'";
	$rsReport = db_exec($strSQL,$conn);
}

function DeleteReport($report) {
	global $conn;
	
	$strSQL = "DELETE FROM ".AddTableWrappers("webreports")." WHERE ".AddFieldWrappers("rpt_name")."=".db_prepare_string($report)." and ".AddFieldWrappers("rpt_type")."='report'";
	$rsReport = db_exec($strSQL,$conn);
}

function testAdvSearch($table)
{
	if(is_wr_project())
	{
		if($table=="articulo")
		{
    			return 1;
		}
		if($table=="categoria")
		{
    			return 1;
		}
		if($table=="ciudad")
		{
    			return 1;
		}
		if($table=="subcategoria")
		{
    			return 1;
		}
		if($table=="cuenta")
		{
    			return 1;
		}
		if($table=="motivo_ajuste")
		{
    			return 1;
		}
		if($table=="ajuste")
		{
    			return 1;
		}
		if($table=="admin_rights")
		{
    			return 0;
		}
		if($table=="admin_members")
		{
    			return 1;
		}
		if($table=="admin_users")
		{
    			return 1;
		}
		if($table=="usuario")
		{
    			return 1;
		}
		if($table=="cliente")
		{
    			return 1;
		}
		if($table=="proveedor")
		{
    			return 1;
		}
		if($table=="v_articulo")
		{
    			return 1;
		}
		if($table=="v_cliente")
		{
    			return 1;
		}
		if($table=="venta")
		{
    			return 1;
		}
		if($table=="cobro")
		{
    			return 1;
		}
		if($table=="gasto")
		{
    			return 1;
		}
		if($table=="det_venta")
		{
    			return 1;
		}
		if($table=="det_movimiento")
		{
    			return 0;
		}
		if($table=="movimiento_caja")
		{
    			return 1;
		}
		if($table=="tipo_cuenta")
		{
    			return 1;
		}
		if($table=="enconstruccion")
		{
    			return 0;
		}
		if($table=="det_gasto")
		{
    			return 1;
		}
		if($table=="inf_ventas")
		{
    			return 1;
		}
		if($table=="ranking_cliente")
		{
    			return 1;
		}
		if($table=="reimprimir_mov_cajas")
		{
    			return 1;
		}
		if($table=="inf_mov_caja")
		{
    			return 1;
		}
		if($table=="v_det_movimiento")
		{
    			return 0;
		}
		if($table=="inf_gasto")
		{
    			return 1;
		}
		if($table=="cuentas_abiertas")
		{
    			return 1;
		}
		if($table=="inf_utilidades")
		{
    			return 1;
		}
		if($table=="inf_cobros")
		{
    			return 1;
		}
		if($table=="ranking_clientes_cant")
		{
    			return 1;
		}
		if($table=="concepto_gasto")
		{
    			return 1;
		}
	}
	elseif(is_wr_db())
	{
		global $dal;
		$table_list=WRGetTablesList();
		foreach($table_list as $key=>$value)
		{
			if($table==$value)
			{
				return 1;
			}
		}
	}
	elseif(is_wr_custom())
	{
		return 1;
	}
	return 0;
}

//	convert cars.Make to [cars].[Make]
function WRAddFieldWrappers($field)
{
	$t="";
	$f="";
	WRSplitFieldName($field,$t,$f);
	if(!$t)
	{
		return AddFieldWrappers($f);
	}
	return AddTableWrappers($t).".".AddFieldWrappers($f);
}

function WRSplitFieldName($str,&$table,&$field)
{
	$table="";
	$field=$str;
	$pos=strrpos($field,".");
	if($pos===false)
		return;
	$table=substr($str,0,$pos);
	$field=substr($str,$pos+1);
		
}


function is_groupby_chart()
{
	if(!count(@$_SESSION['webcharts']))
		return false;
	$root=&$_SESSION['webcharts'];
	if(!is_array($root["group_by_condition"]))
		return false;
	return ($root["group_by_condition"]["group_by_toggle"]=="true");
}

function WRChartLabel($str)
{
	$table="";
	$field="";
	WRSplitFieldName($str,$table,$field);
	return $field;
	if(!$table)
		return $field;
	return $table.".".$field;
}
function is_wr_db()
{
	if(@$_SESSION["webobject"]["table_type"]=="db")
		return true;
	else
		return false;
}
function is_wr_project()
{
	if(@$_SESSION["webobject"]["table_type"]=="project")
		return true;
	else
		return false;
}
function is_wr_custom()
{
	if(@$_SESSION["webobject"]["table_type"]=="custom")
		return true;
	else
		return false;
}
function WRGetTablesList()
{
	if(!isset($_SESSION["WRTableList"]))
		$_SESSION["WRTableList"] = db_gettablelist();
	return $_SESSION["WRTableList"];
}

function WRGetFieldsList($table)
{
	if(is_wr_project()){
		$pSet = new ProjectSettings($table);
		return $pSet->getFieldsList();
	}
	if(is_wr_db())
	{
		global $wr_is_standalone;
		if(!$wr_is_standalone)
		{
			global $dal;
			if($dal->Table($table))
				return $dal->GetFieldsList($table);
		}
		return dbinfoFieldsList($table);
	}
	if(is_wr_custom())
	{
		global $conn;
		$arr=array();
		$sql="";
		$res=array();
//		$rs=db_query("select * from ".AddTableWrappers("webreport_sql")." where ".AddFieldWrappers("sqlname")."='".$table."'",$conn);
//		if($data = db_fetch_array($rs))
//			$sql=$data["sqlcontent"];
		$sql=$_SESSION["object_sql"];
		$arr=db_getfieldslist($sql);
		foreach($arr as $val)
			$res[]=$val["fieldname"];
		return $res;
	}
}
function dbinfoFieldsList($table)
{
	$arr = array();
	$res = array();
	if(isset($_SESSION["WRFieldList"][$table]))
		return $_SESSION["WRFieldList"][$table];
	else
		$arr = db_getfieldslist("select * from ".AddTableWrappers($table)." where 1=0");
	foreach($arr as $val)
		$res[]=$val["fieldname"];
	$_SESSION["WRFieldList"][$table]=$res;
	return $res;
}
function WRCustomGetFieldType($table,$field)
{
	global $conn;
	if(is_wr_project())
	{
		$pSet = new ProjectSettings($_SESSION['webreports']['tables'][0]);
		$type = $pSet->getFieldType($field);
		if($type)
			return $type;
	}
	if(is_wr_db())
	{
		global $wr_is_standalone;
		if(!$wr_is_standalone)
		{
			global $dal;
			if($dal->Table($table))
				return $dal->GetFieldType($table,$field);
		}
		return dbinfoFieldsType($table,$field);
	}
	if(is_wr_custom())
	{
		$res="";
		$sql=$_SESSION["object_sql"];
		if($sql)
		{
			$arr=db_getfieldslist($sql);
			foreach($arr as $val)
				if($val["fieldname"]==$field)
					$res=$val["type"];
		}
		return $res;
	}
}
function WRGetAllCustomFieldType()
{
	$arr=array();
	$res=array();
	$sql=$_SESSION["object_sql"];
	$arr=db_getfieldslist($sql);
	foreach($arr as $val)
		$res[$val["fieldname"]]=$val["type"];
	return $res;
}
function WRGetFieldType($fld)
{
	$table="";
	$field="";
	WRSplitFieldName($fld,$table,$field);
	return WRCustomGetFieldType($table,$field);
}
function dbinfoFieldsType($table,$field)
{
	$arr=array();
	$res="";
	if(isset($_SESSION["WRFieldType"][$table][$field]))
		return $_SESSION["WRFieldType"][$table][$field];
	else
		$arr=db_getfieldslist("select * from ".AddTableWrappers($table)." where 1=0");
	foreach($arr as $val)
	{
		if($val["fieldname"]==$field)
			$res=$val["type"];
		$_SESSION["WRFieldType"][$table][$val["fieldname"]]=$val["type"]   ;
	}
	return $res;
}
function WRUseTimepicker($table,$field)
{
	return false;
}

function WRUseListLookup($table,$field)
{
	return false;
}
function getCaptionTable($table)
{
	global $strTableName;
	if(!$table)
		$table=$strTableName;
	if($table=="articulo")
	{
		return "Articulo";
	}
	if($table=="categoria")
	{
		return "Categoria";
	}
	if($table=="ciudad")
	{
		return "Ciudad";
	}
	if($table=="subcategoria")
	{
		return "Subcategoria";
	}
	if($table=="cuenta")
	{
		return "Cuenta";
	}
	if($table=="motivo_ajuste")
	{
		return "Motivo Ajuste";
	}
	if($table=="ajuste")
	{
		return "Ajuste";
	}
	if($table=="admin_rights")
	{
		return "Admin Rights";
	}
	if($table=="admin_members")
	{
		return "Admin Members";
	}
	if($table=="admin_users")
	{
		return "Add/Edit users";
	}
	if($table=="usuario")
	{
		return "Usuario";
	}
	if($table=="cliente")
	{
		return "Cliente";
	}
	if($table=="proveedor")
	{
		return "Proveedor";
	}
	if($table=="v_articulo")
	{
		return "V Articulo";
	}
	if($table=="v_cliente")
	{
		return "V Cliente";
	}
	if($table=="venta")
	{
		return "Venta";
	}
	if($table=="cobro")
	{
		return "Cobro";
	}
	if($table=="gasto")
	{
		return "Gasto";
	}
	if($table=="det_venta")
	{
		return "Det Venta";
	}
	if($table=="det_movimiento")
	{
		return "Det Movimiento";
	}
	if($table=="movimiento_caja")
	{
		return "Movimiento Caja";
	}
	if($table=="tipo_cuenta")
	{
		return "Tipo Cuenta";
	}
	if($table=="enconstruccion")
	{
		return "Enconstruccion";
	}
	if($table=="det_gasto")
	{
		return "Det Gasto";
	}
	if($table=="inf_ventas")
	{
		return "Inf Ventas";
	}
	if($table=="ranking_cliente")
	{
		return "Ranking Cliente";
	}
	if($table=="reimprimir_mov_cajas")
	{
		return "Reimprimir Mov Cajas";
	}
	if($table=="inf_mov_caja")
	{
		return "Inf Mov Caja";
	}
	if($table=="v_det_movimiento")
	{
		return "V Det Movimiento";
	}
	if($table=="inf_gasto")
	{
		return "Inf Gasto";
	}
	if($table=="cuentas_abiertas")
	{
		return "Cuentas Abiertas";
	}
	if($table=="inf_utilidades")
	{
		return "Inf Utilidades";
	}
	if($table=="inf_cobros")
	{
		return "Inf Cobros";
	}
	if($table=="ranking_clientes_cant")
	{
		return "Ranking Clientes Cant";
	}
	if($table=="concepto_gasto")
	{
		return "Concepto Gasto";
	}
	return $table;
}


function getChartTablesList()
{
	return WRGetQueryTables('webcharts');
}

function getReportTablesList()
{
	return WRGetQueryTables('webreports');
}

function WRGetQueryTables($type)
{
	$root=&$_SESSION[$type];
	$ret=array($root['tables'][0]);
	if (strlen(@$root['table_relations']["relations"]) && strpos($root['table_relations']['join_tables'], ",") !== false) 
	{
		$joined = explode(",", $root['table_relations']['join_tables']);
		foreach($joined as $t)
		{
			if(strlen($t))
				$ret[]=$t;
		}
	}
	return $ret;
}

function GetDefaultViewFormat($type)
{
	if(IsBinaryType($type))
		return FORMAT_DATABASE_IMAGE;
	elseif(IsDateFieldType($type))
		return FORMAT_DATE_SHORT;
	else
		return FORMAT_NONE;
}

function GetDefaultEditFormat($type)
{
	if(IsBinaryType($type))
		return EDIT_FORMAT_DATABASE_IMAGE;
	elseif(IsDateFieldType($type))
		return EDIT_FORMAT_DATE;
	else
		return EDIT_FORMAT_TEXT_FIELD;
}

function GetGenericViewFormat($table,$field)
{
	return GetDefaultViewFormat(WRGetFieldType($table.".".$field));
}


function GetGenericEditFormat($table,$field)
{
	return GetDefaultEditFormat(WRGetFieldType($table.".".$field));
}

function GenericStrWhereAdv($strTable, $strField, $SearchFor, $strSearchOption, $SearchFor2, $etype)
{
	global $dal;
	$sfield=$strField;
	$stable="";
	if(is_wr_db())
	{
		WRSplitFieldName($strField,$stable,$sfield);
		$type=WRGetFieldType($strField);
	}
	else
		$type=WRCustomGetFieldType($strTable,$strField);

if(GetDatabaseType()!=2) //MSSQLServer
	$ismssql=false;
else
	$ismssql=true;
	
	$btexttype=IsTextType($type);
if(GetDatabaseType()==0) //MySQL
	$btexttype=false;

	if(IsBinaryType($type))
		return "";
if(GetDatabaseType()==2) //MSSQLServer
{
	if($btexttype && $strSearchOption!="Contains" && $strSearchOption!="Starts with ..." )
		return "";
}
	if($strSearchOption=='Empty')
	{
		if(IsCharType($type) && (!$ismssql || !$btexttype))
			return "(".WRAddFieldWrappers($strField)." is null or ".WRAddFieldWrappers($strField)."='')";			
		elseif ($ismssql && $btexttype)	
			return "(".WRAddFieldWrappers($strField)." is null or ".WRAddFieldWrappers($strField)." LIKE '')";
		else
			return WRAddFieldWrappers($strField)." is null";
	}
	if(GetDatabaseType()==4) //PostgreSQL
		$like="ilike";
	else
		$like="like";
	if(GetGenericEditFormat($strTable,$sfield)==EDIT_FORMAT_LOOKUP_WIZARD)
	{
		$pSet = new ProjectSettings($strTable);
		if($pSet->multiSelect($sfield))
			$SearchFor=splitvalues($SearchFor);
		else
			$SearchFor=array($SearchFor);
		$ret="";
		foreach($SearchFor as $value)
		{
			if(!($value=="null" || $value=="Null" || $value==""))
			{
				if(strlen($ret))
					$ret.=" or ";
				if($strSearchOption=="Equals")
				{
					$value=WRmake_db_value($sfield,$value,$strTable);
					if(!($value=="null" || $value=="Null"))
						$ret.=WRAddFieldWrappers($strField).'='.$value;
				}
				else
				{
					if(strpos($value,",")!==false || strpos($value,'"')!==false)
						$value = '"'.str_replace('"','""',$value).'"';
					$ret.=WRAddFieldWrappers($strField)." = ".db_prepare_string($value);
					$ret.=" or ".WRAddFieldWrappers($strField)." ".$like." ".db_prepare_string("%,".$value.",%");
					$ret.=" or ".WRAddFieldWrappers($strField)." ".$like." ".db_prepare_string("%,".$value);
					$ret.=" or ".WRAddFieldWrappers($strField)." ".$like." ".db_prepare_string($value.",%");
				}
			}
		}
		if(strlen($ret))
			$ret="(".$ret.")";
		return $ret;
	}
	if(GetGenericEditFormat($strTable,$sfield)==EDIT_FORMAT_CHECKBOX)
	{
		if($SearchFor=="none")
			return "";
		if(NeedQuotes($type))
		{
			if($SearchFor=="on")
				return "(".WRAddFieldWrappers($strField)."<>'0' and ".WRAddFieldWrappers($strField)."<>'' and ".WRAddFieldWrappers($strField)." is not null)";
			else
				return "(".WRAddFieldWrappers($strField)."='0' or ".WRAddFieldWrappers($strField)."='' or ".WRAddFieldWrappers($strField)." is null)";
		}
		else
		{
			if($SearchFor=="on")
				return "(".WRAddFieldWrappers($strField)."<>0 and ".WRAddFieldWrappers($strField)." is not null)";
			else
				return "(".WRAddFieldWrappers($strField)."=0 or ".WRAddFieldWrappers($strField)." is null)";
		}
	}
	$value1=WRmake_db_value($sfield,$SearchFor,$strTable);
	$value2=false;
	if($strSearchOption=="Between")
		$value2=WRmake_db_value($sfield,$SearchFor2,$strTable);
	if($strSearchOption!="Contains" && $strSearchOption!="Starts with ..." && ($value1==="null" || $value2==="null" ))
		return "";

	if(IsCharType($type) && !$btexttype)
	{
		$value1=db_upper($value1);
		$value2=db_upper($value2);
		$strField=db_upper(WRAddFieldWrappers($strField));
	}
	elseif ($ismssql && !$btexttype && ($strSearchOption=="Contains" || $strSearchOption=="Starts with ..."))
		$strField="convert(varchar,".WRAddFieldWrappers($strField).")";
	elseif(GetDatabaseType()==4 && !$btexttype && ($strSearchOption=="Contains" || $strSearchOption=="Starts with ..."))
		$strField = "CAST(".WRAddFieldWrappers($strField)." AS TEXT)";
	else 
		$strField=WRAddFieldWrappers($strField);
	$ret="";
	if($strSearchOption=="Contains")
	{
		if(IsCharType($type) && !$btexttype)
			return $strField." ".$like." ".db_upper(db_prepare_string("%".$SearchFor."%"));
		else
			return $strField." ".$like." ".db_prepare_string("%".$SearchFor."%");
	}
	else if($strSearchOption=="Equals")
	{
		return $strField."=".$value1;
	}
	else if($strSearchOption=="Starts with ...")
	{
		if(IsCharType($type) && !$btexttype)
			return $strField." ".$like." ".db_upper(db_prepare_string($SearchFor."%"));
		else
			return $strField." ".$like." ".db_prepare_string($SearchFor."%");
	}
	else if($strSearchOption=="More than ...") return $strField.">".$value1;
	else if($strSearchOption=="Less than ...") return $strField."<".$value1;
	else if($strSearchOption=="Equal or more than ...") return $strField.">=".$value1;
	else if($strSearchOption=="Equal or less than ...") return $strField."<=".$value1;
	else if($strSearchOption=="Between")
	{
		$ret=$strField.">=".$value1;
		$ret.=" and ".$strField."<=".$value2;
		return $ret;
	}
	return "";
}



function GetAdvSearchOptions($table,$field)
{
	$format=GetGenericEditFormat($table,$field);
	$options=array();
	if($format==EDIT_FORMAT_DATE)
	{
		$options[]=array("type"=>"Equals","label"=>"Equivale");
		$options[]=array("type"=>"More than ...","label"=>"Más que");
		$options[]=array("type"=>"Less than ...","label"=>"Menos que");
		$options[]=array("type"=>"Equal or more than ...","label"=>"Igual o más");
		$options[]=array("type"=>"Equal or less than ...","label"=>"Igual o menos");
		$options[]=array("type"=>"Between","label"=>"Entre");
		$options[]=array("type"=>"Empty","label"=>"Vacio");
	}
	elseif($format==EDIT_FORMAT_LOOKUP_WIZARD)
	{
		if(Multiselect($field,$table))
			$options[]=array("type"=>"Contains","label"=>"Contiene");
		else
			$options[]=array("type"=>"Equals","label"=>"Equivale");
	}
	elseif($format==EDIT_FORMAT_TEXT_FIELD
	|| $format==EDIT_FORMAT_TEXT_AREA
	|| $format==EDIT_FORMAT_PASSWORD
	|| $format==EDIT_FORMAT_HIDDEN
	|| $format==EDIT_FORMAT_READONLY	)
	{
		$options[]=array("type"=>"Contains","label"=>"Contiene");
		$options[]=array("type"=>"Equals","label"=>"Equivale");
		$options[]=array("type"=>"Starts with ...","label"=>"Empieza con");
		$options[]=array("type"=>"More than ...","label"=>"Más que");
		$options[]=array("type"=>"Less than ...","label"=>"Menos que");
		$options[]=array("type"=>"Equal or more than ...","label"=>"Igual o más");
		$options[]=array("type"=>"Equal or less than ...","label"=>"Igual o menos");
		$options[]=array("type"=>"Between","label"=>"Entre");
		$options[]=array("type"=>"Empty","label"=>"Vacio");
	}
	else
	{
		$options[]=array("type"=>"Equals","label"=>"Equivale");
}
	return $options;
}

function CalcSearchParam($webreportsMode=false)
{
	global $sessPrefix, $strSQL, $strTableName;
	$sWhere="";
	$cipherer = new RunnerCipherer($strTableName);
	
	if(!$webreportsMode)
	{
		$params = array();
		$params['tName'] = $strTableName;
		$params['cipherer'] = $cipherer;
		$params['searchFieldsArr'] = WRGetFieldsList($strTableName);
		$searchClauseObj = new SearchClause($params);
		$searchClauseObj->parseRequest();
		return $searchClauseObj->buildItegratedWhere(WRGetFieldsList($strTableName));
	}
	
	if(@$_SESSION[$sessPrefix."_search"]==2)
//	 advanced search
	{
		foreach(@$_SESSION[$sessPrefix."_asearchfor"] as $f => $sfor)
		{
			$strSearchFor=trim($sfor);
			$strSearchFor2="";
			$type=@$_SESSION[$sessPrefix."_asearchfortype"][$f];
			if(array_key_exists($f,@$_SESSION[$sessPrefix."_asearchfor2"]))
				$strSearchFor2=trim(@$_SESSION[$sessPrefix."_asearchfor2"][$f]);
			if($strSearchFor!="" || true)
			{
				if (!$sWhere) 
				{
					if($_SESSION[$sessPrefix."_asearchtype"]=="and")
						$sWhere="1=1";
					else
						$sWhere="1=0";
				}
				$strSearchOption=trim($_SESSION[$sessPrefix."_asearchopt"][$f]);
				$where = GenericStrWhereAdv(@$_SESSION[$sessPrefix."_asearchtable"][$f], $f, $strSearchFor, $strSearchOption, $strSearchFor2,$type);
				if($where)
				{
					if($_SESSION[$sessPrefix."_asearchnot"][$f])
						$where="not (".$where.")";
					if($_SESSION[$sessPrefix."_asearchtype"]=="and")
	   					$sWhere .= " and ".$where;
					else
	   					$sWhere .= " or ".$where;
				}
			}
		}
	}
	return $sWhere;
}

function WRViewFormat($field,$table="")
{
	$pSet = new ProjectSettings($table);
	return $pSet->getViewFormat($field);
}

function get_chart_series_fields(&$arr_data_series,&$arr_label_series)
{
	if(is_groupby_chart())
		return get_chart_groupbyseries_fields($arr_data_series,$arr_label_series);
	$root=&$_SESSION['webcharts'];
	$arr_data_series=array();
	$arr_label_series=array();

	$arr_join_tables = getChartTablesList();

	for ($i=0; $i < count($arr_join_tables); $i++ ) 
	{
		$t = $arr_join_tables[$i];
		$arr_fields = GetNumberFieldsList($t);
		for ($j=0; $j < count($arr_fields); $j++) 
		{
			if(!is_wr_custom())
				$arr_data_series[] = array("field" => $t.".".$arr_fields[$j], "label" => WRChartLabel($t.".".$arr_fields[$j]));
			else
				$arr_data_series[] = array("field" => $arr_fields[$j], "label" => WRChartLabel($arr_fields[$j]));
		}
		$arr_fields = WRGetNBFieldsList($t);
		for ($j=0; $j < count($arr_fields); $j++) 
		{
			if(!is_wr_custom())
				$arr_label_series[] = array("field" => $t.".".$arr_fields[$j], "label" => WRChartLabel($t.".".$arr_fields[$j]));
			else
				$arr_label_series[] = array("field" => $arr_fields[$j], "label" => WRChartLabel($arr_fields[$j]));
		}	
	}
	if(!count($arr_data_series))
		$arr_data_series = $arr_label_series;
}

function get_chart_groupbyseries_fields(&$arr_data_series,&$arr_label_series)
{
	$root=&$_SESSION['webcharts'];
	$arr_data_series=array();
	$arr_label_series=array();
	for ($i=0; $i < count($root['group_by_condition'])-1; $i++) 
	{
		$arr = &$root['group_by_condition'][$i];
		$field=$arr["field_opt"];
		$strLabel=WRChartLabel($field);
		$isLabel=false;
		$isData=false;
		if($arr["group_by_value"]!="-1" && $arr["group_by_value"]!="GROUP BY")
		{
			$field=$arr["group_by_value"]."(".$field.")";
			$isData=true;
			$isLabel=true;
		}
		else if($arr["group_by_value"]=="GROUP BY")
		{
			$type = WRGetFieldType($field);
			if(IsNumberType($type))
				$isData=true;
			$isLabel=true;
		}
		
		$ret = array("field"=>$field,"label"=>$strLabel);
		if($isLabel)
			$arr_label_series[]=$ret;
		if($isData)
			$arr_data_series[]=$ret;
	}
	if(!count($arr_data_series))
		$arr_data_series = $arr_label_series;
}

function WRProcessLargeText($text,$field,$recid,$chars,$mode,$strLabel, $isProgectTable = false)
{
	if(!$chars)
		return $text;
	if($mode!=MODE_LIST && $mode!=MODE_PRINT || strlen($text)<$chars+10)
		return $text;
	
	if($isProgectTable)
		$shortening = GetShorteningForLargeText($text, $cNumberOfChars);
	else 
		$shortening = substr($text,0,$chars);
		
	if($mode==MODE_PRINT)
	{
		return $shortening."...";
	}
//	List page
	$id="textbox_".GoodFieldName($field).$recid;
	$textbox="<span style=\"display:none\" id=\"".$id."\">".htmlspecialchars($text)."</span>";
	$link="<a href=# onclick=\"
	
	var offset = $(this).offset();
	offset.left -= $(window).scrollLeft();
	offset.top -= $(window).scrollTop();
	$('#".$id."').clone().dialog(
	{
title: '".jsreplace($strLabel)."',
		draggable: true,
		resizable: true,
		bgiframe: true,
		modal: false,
		minheight:400,
		position:[offset.left-50,offset.top-50]
	}
	);
	return false;
	\">&nbsp;"."Más"."...</a>";
	return $shortening.$link.$textbox;
}
function JumpTo()
{
	return "$(\"#jumpto\").mouseover(function(){
		if(closetimer) {
			window.clearTimeout(closetimer);
			closetimer = null;
		}
		var jumpto = $(\"#jumpto\"), menujump = $(\"#menujump\"), framejump = $(\"#framejump\");
		if (jumpto.top + jumpto.height() + menujump.height() + $(window).scrollTop() > $(window).height()) {
			if(menujump.height() - jumpto.offset().top + $(window).scrollTop()>0)
			{
				menujump.css(\"top\", $(window).scrollTop()+\"px\");		
				menujump.css(\"left\", ($(this).offset().left - 6) + \"px\");					
				framejump.css(\"width\", menujump.width());
				framejump.css(\"height\", menujump.height());
				framejump.css(\"top\", $(window).scrollTop()+\"px\");		
				framejump.css(\"left\", ($(this).offset().left - 1) + \"px\");
			}
			else
			{
				menujump.css(\"top\", ($(this).offset().top - menujump.height()) + \"px\");		
				menujump.css(\"left\", ($(this).offset().left - 6) + \"px\");					
				framejump.css(\"width\", menujump.width());
				framejump.css(\"height\", menujump.height());
				framejump.css(\"top\", ($(this).offset().top - framejump.height()) + \"px\");		
				framejump.css(\"left\", ($(this).offset().left - 1) + \"px\");
			}
		} else {
			menujump.css(\"top\", ($(this).offset().top + $(this).height()) + \"px\");		
			menujump.css(\"left\", ($(this).offset().left - 6) + \"px\");			
			framejump.css(\"width\", menujump.width());
			framejump.css(\"height\", menujump.height());
			framejump.css(\"top\", ($(this).offset().top + $(this).height()) + \"px\");		
			framejump.css(\"left\", ($(this).offset().left - 1) + \"px\");		
		}
		framejump.show();
		menujump.show();
	});

	$(\"#jumpto\").mouseout(function(){
		closetimer = window.setTimeout(\"$('#menujump').hide();$('#framejump').hide();\", timeout);
	});
	
	$(\"#menujump td\").mouseover(function(){
		if(closetimer) {
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	});
		
	$(\"#menujump td\").mouseout(function(){
		closetimer = window.setTimeout(\"$('#menujump').hide();$('#framejump').hide();\", timeout);
	});
	
	$(document.body).click(function(){
		$(\"#menujump\").hide();
		$(\"#framejump\").hide();
	});	";
}
function alertDialog()
{
return	"$(\"#alert\").dialog({
		open: function(event,ui){
			var alertParent = $(\"#alert\").parent(\".ui-dialog\"), aframe = $(\"#aframe\"); 
			w = alertParent.width();
			h = alertParent.height();
			t = alertParent.offset().top;
			l = alertParent.offset().left;
			aframe.css(\"width\",w+6);
			aframe.css(\"height\",h+6);
			aframe.css(\"top\",t + \"px\");		
			aframe.css(\"left\",l + \"px\");	
			aframe.show();
		},
		beforeclose: function(event,ui){
			$(\"#aframe\").hide();
		},
		title: \"Message\",
		draggable: false,
		resizable: false,
		bgiframe: true,
		autoOpen: false,
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog(\"close\");
			}
		}
	});";
}

function DBGetTableKeys($table)
{
	global $dal,$wr_is_standalone;
	if(!$wr_is_standalone)
	{
		if($dal->Table($table))
			return $dal->GetDBTableKeys($table);
		return array();
	}
	else
		return array();
}

function colorPickerMouse()
{
return "

function GiveDec(Hex)
{
   if(Hex == \"A\")
	  Value = 10;
   else
   if(Hex == \"B\")
	  Value = 11;
   else
   if(Hex == \"C\")
	  Value = 12;
   else
   if(Hex == \"D\")
	  Value = 13;
   else
   if(Hex == \"E\")
	  Value = 14;
   else
   if(Hex == \"F\")
	  Value = 15;
   else
	  Value = eval(Hex);

   return Value;
}

function GiveHex(Dec)
{
   if(Dec == 10)
	  Value = \"A\";
   else
   if(Dec == 11)
	  Value = \"B\";
   else
   if(Dec == 12)
	  Value = \"C\";
   else
   if(Dec == 13)
	  Value = \"D\";
   else
   if(Dec == 14)
	  Value = \"E\";
   else
   if(Dec == 15)
	  Value = \"F\";
   else
	  Value = \"\" + Dec;

   return Value;
}

function HexToDec(value)
{
   Input = value.toUpperCase();

   a = GiveDec(Input.substring(0, 1));
   b = GiveDec(Input.substring(1, 2));
   c = GiveDec(Input.substring(2, 3));
   d = GiveDec(Input.substring(3, 4));
   e = GiveDec(Input.substring(4, 5));
   f = GiveDec(Input.substring(5, 6));

   x = (a * 16) + b; // Red
   y = (c * 16) + d; // Green
   z = (e * 16) + f; // Blue

	return [x,y,z]
}

function DecToHex(Red, Green, Blue)
{
   a = GiveHex(Math.floor(Red / 16));
   b = GiveHex(Red % 16);
   c = GiveHex(Math.floor(Green / 16));
   d = GiveHex(Green % 16);
   e = GiveHex(Math.floor(Blue / 16));
   f = GiveHex(Blue % 16);

   z = a + b + c + d + e + f;

	return z;
}

function rgbToHex(str)
{
	if(str==undefined)
		return \"\";
	if(str.substr(0,1)==\"#\")
		return str.substr(1);
	str=str.substring(4);
	str=str.replace(\")\",\"\");
	arr = new Array();
	arr=str.split(\",\");
	return DecToHex(arr[0],arr[1],arr[2]);
}

	$(\".ColorPickerDivSample\").css(\"cursor\",\"pointer\");
	
	$(\"#colorPickerVtd td\").each(function(){
        $(this).css(\"border\",\"1px solid \" + $(this).css(\"backgroundColor\"));
    })
		.css(\"cursor\",\"pointer\");
	
	$(\".selector,.ColorPickerDivSample\").click(function(){
	    click_color_event(this);
	});
	
	$(\"#colorPickerVtd\").mouseover(function(){
		if(closetimerpicker) {
			window.clearTimeout(closetimerpicker);
			closetimerpicker = null;
		}
	}).mouseout(function(){
		closetimerpicker = window.setTimeout(function (){
				$('#colorPickerVtd').hide();
				$(\"div.ColorPickerDivSample.active\").css(\"background-color\", $(\"div.ColorPickerDivSample.active\").attr(\"color1\"));
			}, timeoutpicker);
	});
		
	$(\"#colorPickerVtd td\").mouseover(function(){
		if(closetimerpicker) {
			window.clearTimeout(closetimerpicker);
			closetimerpicker = null;
		}
		$(this).css(\"border\", \"1px dotted #fff\");
		$(\"div.ColorPickerDivSample.active\").css(\"background-color\", $(this).css(\"background-color\"));
	});
	
	$(\"#colorPickerVtd td\").mouseout(function(){
		$(this).css(\"border\", \"1px solid \"+$(\"div.ColorPickerDivSample.active\").css(\"background-color\"));
	});	
	
	$(\"#colorPickerVtd td\").click(function(){
		if ( this.id == \"nocolor\" ) {
			$(\"div.ColorPickerDivSample.active\").attr(\"color1\", \"\");
			$(\"div.ColorPickerDivSample.active\").attr(\"color2\", \"\");
		} else {
			bgcol=$(this).css(\"backgroundColor\");
			if(bgcol.substring(0,1)!=\"#\")
				bgcol=rgbToHex(bgcol);
			else
				bgcol=bgcol.substring(1);
			$(\"div.ColorPickerDivSample.active\").attr(\"color1\", bgcol);
			arr = HexToDec(bgcol);
			red = parseInt( arr[0] * 0.85 );
			green = parseInt( arr[1] * 0.85 );
			blue = parseInt( arr[2] * 0.85 );
			hex = DecToHex( red, green, blue );
			$(\"div.ColorPickerDivSample.active\").attr(\"color2\", hex);
		}
		$(\"#colorPickerVtd\").hide();
	});
	
	function click_color_event(th)
	{
		if($(th).css(\"cursor\")==\"pointer\")
	    {
			if(closetimerpicker) {
				window.clearTimeout(closetimerpicker);
				closetimerpicker = null;
		}
		if($(th).attr(\"class\")==\"selector\")
			bc=$(th).parents(\"td:first\").prev(\"td\").find(\"div.ColorPickerDivSample\").css(\"background-color\");
		else
			bc=$(th).parents(\"td:first\").find(\"div.ColorPickerDivSample\").css(\"background-color\");
		
		var activeDiv = $(\"div.ColorPickerDivSample.active\"), colorPicker = $(\"#colorPickerVtd\");
		if(activeDiv.length){
			activeDiv.css(\"background-color\", activeDiv.attr(\"color1\"));
			activeDiv.removeClass(\"active\");
		}
		
		if($(th).attr(\"class\")==\"selector\")
			$(th).parents(\"td:first\").prev(\"td\").find(\"div.ColorPickerDivSample\").addClass(\"active\");
		else
			$(th).parents(\"td:first\").find(\"div.ColorPickerDivSample\").addClass(\"active\");
			
		
		colorPicker.css(\"top\", $(th).offset().top + \"px\");
		colorPicker.css(\"left\", $(th).offset().left + $(th).width() + 3 + \"px\");
		colorPicker.show();
		$(\"td\", colorPicker).each(function(){
			$(this).css(\"border\", \"1px solid \"+$(this).css(\"background-color\"));
			if(bc==$(this).css(\"background-color\"))
				$(this).css(\"border\", \"1px dotted #fff\");
		});
	    }
	}

	";
}

function MoveTdTotal()
{
return "
function total_td_move(th,direct)
{
	tr=$(th).parent().parent().parent();

	if(direct==\"up\")
		tr2=$(tr).prev();
	else
		tr2=$(tr).next();
		
	if(!$(tr2).find(\"td\").eq(4).find(\"input\").get(0))
		tr2=\"\";		
	else
		if($(tr2).find(\"td\").eq(4).find(\"input\").get(0).disabled || $(tr2).find(\"td\").eq(3).find(\"input\").get(0).type=='checkbox')
			tr2=\"\";		
	if(tr2!=\"\")
	{
		if(direct==\"up\")
			$(tr).insertBefore(tr2);
		else
			$(tr).insertAfter(tr2);
	}
}";
}

function PrepareString4DB($str)
{
	if(GetDatabaseType()!=nDATABASE_Oracle)
	{
		return db_prepare_string($str);
	}

	if(strlen($str)<4000)
		return db_prepare_string($str);
	$chunklen = 3900;
	$chunks = floor(strlen($str)/$chunklen);
	if(strlen($str) % $chunklen)
		$chunks++;
	$out="";
	for($i=0;$i<$chunks;$i++)
	{
		if(strlen($out))
			$out.="||";
		$out.="to_clob(";
		$out.=db_prepare_string(substr($str,$i*$chunklen,$chunklen));
		$out.=")";
	}
	return $out;
}
function WRmake_db_value($field,$value,$table="")
{	
	$ret=WRprepare_for_db($field,$value,$table);
	
	if($ret===false)
		return $ret;
	return WRadd_db_quotes($field,$ret,$table);
}

function WRadd_db_quotes($field,$value,$table="")
{
	$type = WRGetFieldType($table.".".$field);
	if(IsBinaryType($type))
		return db_addslashesbinary($value);
	if(($value==="" || $value===FALSE) && !IsCharType($type))
		return "null";
	if(NeedQuotes($type))
	{
		if(!IsDateFieldType($type))
			$value=db_prepare_string($value);
		else
			$value=db_datequotes($value);
	}
	else
	{
		$strvalue = (string)$value;
		$strvalue = str_replace(",",".",$strvalue);
		if(is_numeric($strvalue))
			$value=$strvalue;
		else
			$value=0;
	}
	return $value;
}

function WRprepare_for_db($field,$value,$table="")
{
	$type=WRGetFieldType($table.".".$field);
	if(is_array($value))
		$value=combinevalues($value);
	if(($value==="" || $value===FALSE) && !IsCharType($type))
		return "";
	if(IsDateFieldType($type))
		$value=localdatetime2db($value);
	return $value;
}

function DBGetTablesList()
{
	global $dal;
	$tables = WRGetTablesList();
	$ret=array();

	foreach($tables as $value)
	{
		$val_lower=strtolower($value);
		if(substr($val_lower,-6)!="_audit" && substr($val_lower,-8)!="_locking" && substr($val_lower,-9)!="_ugrights" && substr($val_lower,-9)!="_uggroups" 
		&& substr($val_lower,-10)!="_ugmembers" && $val_lower!="admin_rights" && $val_lower!="admin_users" 
		&& $val_lower!="admin_members" && $val_lower!="webreports" && $val_lower!="webreport_style" 
		&& $val_lower!="webreport_admin" && $val_lower!="webreport_settings" && $val_lower!="webreport_sql")
			$ret[]=$value;
	}
	return $ret;
}




function WRGetTableListAdmin($db_type)
{
	global $conn;
	$ret=array();
	$rs=db_query("select ".AddFieldWrappers("tablename").",".AddFieldWrappers("group_name")." from ".AddTableWrappers("webreport_admin")." where ".AddFieldWrappers("db_type")."='".$db_type."'",$conn);
	while($data = db_fetch_numarray( $rs )) 
		$ret[]=array("tablename"=>$data[0],"group"=>$data[1]);	
	return $ret;
}

function GetTablesListReport()
{
	$arr=array();
	$strPerm = GetUserPermissions("articulo");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="articulo";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="articulo";
	}
	$strPerm = GetUserPermissions("categoria");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="categoria";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="categoria";
	}
	$strPerm = GetUserPermissions("ciudad");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="ciudad";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="ciudad";
	}
	$strPerm = GetUserPermissions("subcategoria");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="subcategoria";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="subcategoria";
	}
	$strPerm = GetUserPermissions("cuenta");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="cuenta";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="cuenta";
	}
	$strPerm = GetUserPermissions("motivo_ajuste");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="motivo_ajuste";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="motivo_ajuste";
	}
	$strPerm = GetUserPermissions("ajuste");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="ajuste";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="ajuste";
	}
	$strPerm = GetUserPermissions("admin_rights");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="admin_rights";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="admin_rights";
	}
	$strPerm = GetUserPermissions("admin_members");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="admin_members";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="admin_members";
	}
	$strPerm = GetUserPermissions("admin_users");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="admin_users";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="admin_users";
	}
	$strPerm = GetUserPermissions("usuario");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="usuario";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="usuario";
	}
	$strPerm = GetUserPermissions("cliente");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="cliente";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="cliente";
	}
	$strPerm = GetUserPermissions("proveedor");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="proveedor";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="proveedor";
	}
	$strPerm = GetUserPermissions("v_articulo");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="v_articulo";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="v_articulo";
	}
	$strPerm = GetUserPermissions("v_cliente");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="v_cliente";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="v_cliente";
	}
	$strPerm = GetUserPermissions("venta");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="venta";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="venta";
	}
	$strPerm = GetUserPermissions("cobro");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="cobro";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="cobro";
	}
	$strPerm = GetUserPermissions("gasto");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="gasto";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="gasto";
	}
	$strPerm = GetUserPermissions("det_venta");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="det_venta";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="det_venta";
	}
	$strPerm = GetUserPermissions("det_movimiento");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="det_movimiento";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="det_movimiento";
	}
	$strPerm = GetUserPermissions("movimiento_caja");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="movimiento_caja";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="movimiento_caja";
	}
	$strPerm = GetUserPermissions("tipo_cuenta");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="tipo_cuenta";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="tipo_cuenta";
	}
	$strPerm = GetUserPermissions("enconstruccion");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="enconstruccion";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="enconstruccion";
	}
	$strPerm = GetUserPermissions("det_gasto");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="det_gasto";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="det_gasto";
	}
	$strPerm = GetUserPermissions("reimprimir_mov_cajas");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="reimprimir_mov_cajas";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="reimprimir_mov_cajas";
	}
	$strPerm = GetUserPermissions("v_det_movimiento");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="v_det_movimiento";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="v_det_movimiento";
	}
	$strPerm = GetUserPermissions("cuentas_abiertas");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="cuentas_abiertas";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="cuentas_abiertas";
	}
	$strPerm = GetUserPermissions("inf_utilidades");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="inf_utilidades";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="inf_utilidades";
	}
	$strPerm = GetUserPermissions("inf_cobros");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="inf_cobros";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="inf_cobros";
	}
	$strPerm = GetUserPermissions("concepto_gasto");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
		$value="concepto_gasto";
		if(substr($value,-6)!="_audit" && substr($value,-8)!="_locking" && substr($value,-9)!="_ugrights" && substr($value,-9)!="_uggroups" 
		&& substr($value,-10)!="_ugmembers" && $value!="admin_rights" && $value!="admin_users" 
		&& $value!="admin_members" && $value!="webreports" && $value!="webreport_style" && $value!="webreport_settings" && $value!="webreport_admin" && $value!="webreport_sql")
			$arr[]="concepto_gasto";
	}
	return $arr;
}
function GetTablesListCustom()
{
	global $conn;
	$arr=array();
	$rs=db_query("select * from ".AddTableWrappers("webreport_sql")." order by ".AddFieldWrappers("sqlname"),$conn);
	while($data = db_fetch_array($rs)) 
		$arr[]=array("sqlname"=>$data["sqlname"],"isStorProc"=>IsStoredProcedure($data["sqlcontent"]));
	return $arr;
}

function DBGetTablesListByGroup($db="db")
{
	if($db=="db")
		$tables = DBGetTablesList();
	elseif($db=="project")
		$tables = GetTablesListReport();
	else
		$tables = GetTablesListCustom();
	$ret=array();
	if($db=="db")
		$tables_admin = WRGetTableListAdmin("db");
	elseif($db=="project")
		$tables_admin = WRGetTableListAdmin("project");
	else
		$tables_admin = WRGetTableListAdmin("custom");

	$userGroups = GetUserGroup();

//	all tables
	foreach($tables as $table_name)
	{
		if($db=="custom")
			$tablename=$table_name["sqlname"];
		else
			$tablename=$table_name;
//	permissions
		foreach($tables_admin as $tablerights)
		{
			if($tablerights["tablename"]!=$tablename)
				continue;
//	user groups
			$found=false;
			if(!count($userGroups))
			{
//	no groups at all
				$found=true;
			}
			elseif($tablerights["group"]=="")
			{
//	initial table initialization
				$found=true;
			}
			else
			{
				foreach($userGroups as $group)
				{
					if((string)$group == $tablerights["group"])
					{
						$found=true;
						break;
					}
				}
			}
			if($found)
			{
				$ret[]=$table_name;
				break;
			}
		}
	}
	return $ret;
}


function isWRAdmin()
{
	$sUserGroup=@$_SESSION["GroupID"];
	return IsAdmin();
}
function sortUserGroup($a,$b)
{
	if($a[1]<$b[1])
		return -1;
	else
		return 1;
}
function Convert_Old_Chart($arr)
{
	switch($arr["chart_type"]["type"])
	{
		case "3d_column":
			$arr["chart_type"]["type"]="2d_column";
			$arr["appearance"]["is3d"]="true";
			$arr["appearance"]["isstacked"]="false";
			break;
		case "3d_bar":
			$arr["chart_type"]["type"]="2d_bar";
			$arr["appearance"]["is3d"]="true";
			$arr["appearance"]["isstacked"]="false";
			break;
		case "3d_column_stacked":
			$arr["chart_type"]["type"]="2d_column";
			$arr["appearance"]["is3d"]="true";
			$arr["appearance"]["isstacked"]="true";
			break;
		case "3d_bar_stacked":
			$arr["chart_type"]["type"]="2d_bar";
			$arr["appearance"]["is3d"]="true";
			$arr["appearance"]["isstacked"]="true";
			break;
		case "2d_column_stacked":
			$arr["chart_type"]["type"]="2d_column";
			$arr["appearance"]["isstacked"]="true";
			$arr["appearance"]["is3d"]="false";
			break;
		case "2d_column":
			$arr["chart_type"]["type"]="2d_column";
			if(!isset($arr["appearance"]["isstacked"]))
				$arr["appearance"]["isstacked"]="false";
			if(!isset($arr["appearance"]["is3d"]))
				$arr["appearance"]["is3d"]="false";
			break;
		case "2d_bar_stacked":
			$arr["chart_type"]["type"]="2d_bar";
			$arr["appearance"]["isstacked"]="true";
			$arr["appearance"]["is3d"]="false";
			break;
		case "2d_bar":
			$arr["chart_type"]["type"]="2d_bar";
			if(!isset($arr["appearance"]["isstacked"]))
				$arr["appearance"]["isstacked"]="false";
			if(!isset($arr["appearance"]["is3d"]))
				$arr["appearance"]["is3d"]="false";
			break;
		case "line":
			$arr["chart_type"]["type"]="line";
			if(!isset($arr["appearance"]["linestyle"]))
				$arr["appearance"]["linestyle"]=0;
			break;
		case "spline":
			$arr["chart_type"]["type"]="line";
			$arr["appearance"]["linestyle"]=1;
			break;
		case "step_line":
			$arr["chart_type"]["type"]="line";
			$arr["appearance"]["linestyle"]=2;
			break;
		case "area_stacked":
			$arr["chart_type"]["type"]="area";
			$arr["appearance"]["isstacked"]="true";
			break;
	}
	if(!isset($arr['appearance']["cscroll"]))
	{
		$arr['appearance']["cscroll"]="true";
		$arr['appearance']["autoupdate"]="false";
		$arr['appearance']["maxbarscroll"]="10";
		$arr['appearance']["update_interval"]="5";
	}

	for($i=0;$i<4;$i++)
	{
		if(isset($arr['appearance']['color'.($i+1).'1']))
			$arr['parameters'][$i]['series_color']=$arr['appearance']['color'.($i+1).'1'];
	}
	return $arr;
}
function WRGetListCustomSQL()
{
	global $conn;
	$arr = array();
	$rs=db_query("select * from ".AddTableWrappers("webreport_sql")." order by ".AddFieldWrappers("sqlname"),$conn);
	while($data = db_fetch_array($rs)) 
		$arr[]=array("id"=>$data["id"],"sqlname"=>$data["sqlname"],"sqlcontent"=>$data["sqlcontent"]);
	return $arr;
}
function WRgetCurrentCustomSQL($id)
{	
	if(!$id)
		return array(0,"","");
	global $conn;
	$rs=db_query("select * from ".AddTableWrappers("webreport_sql")." where ".AddFieldWrappers("id")."=".$id,$conn);
	if($data = db_fetch_array($rs)) 
		return array($data["id"],$data["sqlname"],$data["sqlcontent"]);
	return "";
}
function getCustomSQLbyName($sqlname)
{
	global $conn;
	$rs=db_query("select * from ".AddTableWrappers("webreport_sql")." where ".AddFieldWrappers("sqlname")."='".$sqlname."'",$conn);
	if($data = db_fetch_array($rs)) 
		return array($data["id"],$data["sqlname"],$data["sqlcontent"]);
	return "";
}
function update_report_totals()
{
	$root=&$_SESSION["webreports"];
//	ensure all fields in reports are listed in the tables	
	$tables=getReportTablesList();
	if(is_wr_custom())
		$fields = WRGetFieldsList('');
	$arr_unset=array();
	foreach($root["totals"] as $idx=>$fld)
	{
		if(array_search($fld["table"],$tables)!==false || is_null($fld["table"]) && is_wr_custom())
		{
			if(!is_wr_custom())
				$fields = WRGetFieldsList($fld["table"]);
			if(array_search($fld["name"], $fields)!==false)
				continue;
		}
//	remove $total if found
		$arr_unset[]=$idx;
	}
	foreach($arr_unset as $idx=>$fld)
	{
		unset($root["totals"][$fld]);
	}
	
//	ensure all fields appear in the totals
	$all_fields=array();
	foreach($tables as $t)
	{
		$fields=WRGetFieldsList($t);
		foreach($fields as $f)
		{
			if(is_wr_db())
				$all_fields[]=$t.".".$f;
			else
				$all_fields[]=$f;
		}
	}
//	ensure all series  fields appear in the totals
	foreach($all_fields as $f)
	{
		$table="";
		$fld="";
		if(is_wr_db())
			WRSplitFieldName($f,$table,$fld);
		else
		{
			$table=$tables[0];
			$fld=$f;
			$f=$table."_".$f;
		}
		if(array_key_exists(GoodFieldName($f),$root["totals"]))
			continue;
		$pSet = new ProjectSettings($table, PAGE_LIST);
        $root['totals'][GoodFieldName($f)] = array();
        $root['totals'][GoodFieldName($f)]["name"] = $fld;
        $root['totals'][GoodFieldName($f)]["table"] = $table;
		$root['totals'][GoodFieldName($f)]["label"] = $pSet->label($fld);
        $root['totals'][GoodFieldName($f)]["show"] = "true";
        $root['totals'][GoodFieldName($f)]["min"] = "false";
        $root['totals'][GoodFieldName($f)]["max"] = "false";
        $root['totals'][GoodFieldName($f)]["sum"] = "false";
        $root['totals'][GoodFieldName($f)]["avg"] = "false";
		$root['totals'][GoodFieldName($f)]["curr"] = "false";
        $root['totals'][GoodFieldName($f)]["search"] = "";
        $root['totals'][GoodFieldName($f)]["view_format"] = GetGenericViewFormat($table, $fld);
        $root['totals'][GoodFieldName($f)]["edit_format"] = GetGenericEditFormat($table, $fld);
        $root['totals'][GoodFieldName($f)]["display_field"] = $pSet->getLWDisplayField($fld);
        $root['totals'][GoodFieldName($f)]["linkfield"] = $pSet->getLWLinkField($fld);
        $root['totals'][GoodFieldName($f)]["show_thumbnail"] = $pSet->showThumbnail($fld);
        $root['totals'][GoodFieldName($f)]["need_encode"] = $pSet->NeedEncode($fld);
        $root['totals'][GoodFieldName($f)]["thumbnail"] = $pSet->getStrThumbnail($fld);
        $root['totals'][GoodFieldName($f)]["listformatobj_imgwidth"] = $pSet->getImageWidth($fld);
        $root['totals'][GoodFieldName($f)]["listformatobj_imgheight"] = $pSet->getImageHeight($fld);
        $root['totals'][GoodFieldName($f)]["hlprefix"] = $pSet->getLinkPrefix($fld);
        $root['totals'][GoodFieldName($f)]["listformatobj_filename"] = $pSet->getFilenameField($fld);
        $root['totals'][GoodFieldName($f)]["lookupobj_lookuptype"] = $pSet->getLookupType($fld);
        $root['totals'][GoodFieldName($f)]["editformatobj_lookupobj_customdispaly"] = $pSet->getLWDisplayField($fld);
        $root['totals'][GoodFieldName($f)]["editformatobj_lookupobj_table"] = $pSet->getLookupTable($fld);
        $root['totals'][GoodFieldName($f)]["editformatobj_lookupobj_where"] = GetLWWhere($fld,PAGE_REPORT);
	}
	$_SESSION["webreports"]=$root;
}
function Reload_Report($name)
{
	if(!$name)
	{
		if(isset($_SESSION['webreports']))
			return true;
		else
			header("location:webreport.php");
	}
	if(postvalue("edit")=="style" && isset($_SESSION['webreports']))
		return true;
	$arr=getReportArray($name);
	if(!count($arr))
		header("location:webreport.php");
	if(!$arr["table_type"])
		if($arr["db_based"])
			$arr["table_type"]="db";
		else
			$arr["table_type"]="project";
	$_SESSION['webreports'] = $arr;
	$_SESSION["webobject"]["table_type"]=$_SESSION['webreports']["table_type"];
	$_SESSION["webobject"]["name"]=$_SESSION['webreports']['settings']['name'];
}

function Reload_Chart($name)
{
	if(!$name)
	{
		if(isset($_SESSION['webcharts']))
			return true;
		else
			header("location:webreport.php");
	}
	$arr=getChartArray($name);
	if(!count($arr))
		header("location:webreport.php");
	if(!$arr["table_type"])
		if($arr["db_based"])
			$arr["table_type"]="db";
		else
			$arr["table_type"]="project";
	$_SESSION['webcharts'] = $arr;
	$_SESSION["webobject"]["table_type"]=$_SESSION['webcharts']["table_type"];
	$_SESSION["webobject"]["name"]=$_SESSION['webcharts']['settings']['name'];
}
/*function tableEventExists($p1,$p2)
{
	return false;
}*/
?>