<?php 
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
set_magic_quotes_runtime(0);

include("include/dbcommon.php");
include("include/pf_configuracion_variables.php");



$filename="";	
$message="";
$error_happened=false;
$readavalues=false;

//connect database
$conn = db_connect();

// insert new record if we have to

if(@$_POST["a"]=="added")
{
	$afilename_values=array();
	$avalues=array();
	$files_move=array();
//	processing id_configuracion - start

	$value = postvalue("value_id_configuracion");
	$type=postvalue("type_id_configuracion");
	if (in_assoc_array("type_id_configuracion",$_POST) || in_assoc_array("value_id_configuracion",$_POST) || in_assoc_array("value_id_configuracion",$_FILES))
	{
		$value=prepare_for_db("id_configuracion",$value,$type);
	}
	else
		$value=false;
	if(!($value===false))
	{
		$avalues["id_configuracion"]=$value;
	}

//	processibng id_configuracion - end
//	processing valor - start

	$value = postvalue("value_valor");
	$type=postvalue("type_valor");
	if (in_assoc_array("type_valor",$_POST) || in_assoc_array("value_valor",$_POST) || in_assoc_array("value_valor",$_FILES))
	{
		$value=prepare_for_db("valor",$value,$type);
	}
	else
		$value=false;
	if(!($value===false))
	{
		$avalues["valor"]=$value;
	}

//	processibng valor - end





//	add filenames to values
	foreach($afilename_values as $akey=>$value)
		$avalues[$akey]=$value;
//	make SQL string
	$strSQL = "insert into ".AddTableWrappers($strOriginalTableName)." ";
	$strFields="(";
	$strValues="(";
	
//	before Add event
	$retval = true;
	if(function_exists("BeforeAdd"))
		$retval=BeforeAdd($avalues);
	if($retval)
	{
		foreach($avalues as $akey=>$value)
		{
			$strFields.=AddFieldWrappers($akey).", ";
			$strValues.=add_db_quotes($akey,$value).", ";
		}
		if(substr($strFields,-2)==", ")
			$strFields=substr($strFields,0,strlen($strFields)-2);
		if(substr($strValues,-2)==", ")
			$strValues=substr($strValues,0,strlen($strValues)-2);
		$strSQL.=$strFields.") values ".$strValues.")";
		LogInfo($strSQL);
		set_error_handler("add_error_handler");
		db_exec($strSQL,$conn);
		set_error_handler("error_handler");
//	move files
		if(!$error_happened)
		{
			foreach ($files_move as $file)
			{
				move_uploaded_file($file[0],$file[1]);
				if(strtoupper(substr(PHP_OS,0,3))!="WIN")
					chmod($file[1],0777);
			}
		
			$message="<div class=message><<< "."El registro fue aņadido"." >>></div>";
//	after add event		
			if(function_exists("AfterAdd"))
				AfterAdd();
		}
	}
	else
		$readavalues=true;
}

$defvalues=array();


$keys=array();
$keys["id_configuracion"]=postvalue("copyid1");

//	copy record
if(array_key_exists("copyid1",$_REQUEST))
{
	$strWhere=KeyWhere($keys);
	$strSQL=$gstrSQL;
	$strSQL=AddWhere($strSQL,$strWhere);
	LogInfo($strSQL);
	$rs=db_query($strSQL,$conn);
	$defvalues=db_fetch_array($rs);
//	clear key fields
	$defvalues["id_configuracion"]="";
//call CopyOnLoad event
	if(function_exists("CopyOnLoad"))
		CopyOnLoad($defvalues,$strWhere);
}
else if(!count($defvalues))
{
}
if($readavalues)
{
	$defvalues["id_configuracion"]=@$avalues["id_configuracion"];
	$defvalues["valor"]=@$avalues["valor"];
}


include('libs/Smarty.class.php');
$smarty = new Smarty();

foreach($defvalues as $key=>$value)
	$smarty->assign("value_".GoodFieldName($key),$value);


//	include files

$includes="";

//	validation stuff
$bodyonload="";
$onsubmit="";

if($bodyonload)
{
	$onsubmit="return validate();";
	$bodyonload="onload=\"".$bodyonload."\"";
}

if ($useAJAX) {
	$includes.="<script language=\"JavaScript\" src=\"include/jquery.js\"></script>\r\n";
	$includes.="<script language=\"JavaScript\" src=\"include/ajaxsuggest.js\"></script>\r\n";
}
$includes.="<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n";
$includes.="<script language=\"JavaScript\">\r\n".
"var locale_dateformat = ".$locale_info["LOCALE_IDATE"].";\r\n".
"var locale_datedelimiter = \"".$locale_info["LOCALE_SDATE"]."\";\r\n".
"var bLoading=false;\r\n".
"var TEXT_PLEASE_SELECT='".jsreplace("Por favor seleccione")."';\r\n";
if ($useAJAX) {
	$includes.="var AUTOCOMPLETE_TABLE='pf_configuracion_autocomplete.php';\r\n";
	$includes.="var SUGGEST_TABLE='pf_configuracion_searchsuggest.php';\r\n";
	$includes.="var SUGGEST_LOOKUP_TABLE='pf_configuracion_lookupsuggest.php';\r\n";
}
$includes.="</script>\r\n";
if ($useAJAX) {
	$includes.="<div id=\"search_suggest\"></div>\r\n";
}





$smarty->assign("includes",$includes);
$smarty->assign("bodyonload",$bodyonload);
if(strlen($onsubmit))
	$onsubmit="onSubmit=\"".$onsubmit."\"";
$smarty->assign("onsubmit",$onsubmit);

$smarty->assign("message",$message);


$readonlyfields=array();

//	show readonly fields

$linkdata="";

if ($useAJAX) {
	$linkdata .= "<script type=\"text/javascript\">\r\n";
	$linkdata .= "$(document).ready(function(){ \r\n";
	$linkdata .= "
function loadSelectContent(txt, selectControl, selectValue) 
{
	$('#'+selectControl).get(0).options[0]=new Option(TEXT_PLEASE_SELECT,'');
	var str = txt.split('\\n');
	var index = 0;
	for(i=0,j=0; i < str.length - 1; i=i+2, j++) {
		$('#'+selectControl).get(0).options[j+1]=new Option(unescape(str[i+1]),unescape(str[i]));
		if ( unescape(str[i]) == selectValue ) {index = j+1;}
	}
	$('#'+selectControl).get(0).selectedIndex = index;
	if(index==0 && j==1)
		$('#'+selectControl).get(0).selectedIndex = 1;
}"."\r\n";
	$linkdata .= "});\r\n";
	$linkdata .= "</script>\r\n";
} else {
}

$smarty->assign("linkdata",$linkdata);

$templatefile = "pf_configuracion_add.htm";
if(function_exists("BeforeShowAdd"))
	BeforeShowAdd($smarty,$templatefile);

$smarty->display($templatefile);


function add_error_handler($errno, $errstr, $errfile, $errline)
{
	global $readavalues, $message, $error_happened;
	$message="<div class=message><<< "."El registro no ha sido aņadido"." >>><br><br>".$errstr."</div>";
	$readavalues=true;
	$error_happened=true;
}

?>
