<?php

function GETVAL($key){ $val=trim($_GET[$key]); if (!is_numeric($val)) apperror('apperror:invalid parameter '.$key); return $val;}
function QETVAL($key){ $val=trim($_POST[$key]); if (!is_numeric($val)) apperror('apperror:invalid parameter '.$key); return $val;}
function noapos($val){return addslashes($val);}
function GETSTR($key,$trim=1){$val=$_GET[$key];if ($trim) $val=trim($val);return noapos($val);}
function QETSTR($key,$trim=1){$val=$_POST[$key];if ($trim) $val=trim($val);return noapos($val);}

function GETCUR($key){$val=trim($_GET[$key]); $val=str_replace(_tr('currency_separator_thousands'),'',$val); $val=str_replace(_tr('currency_separator_decimal'),'.',$val); if (!is_numeric($val)) apperror('apperror:invalid parameter '.$key); return $val; }
function QETCUR($key){$val=trim($_POST[$key]); $val=str_replace(_tr('currency_separator_thousands'),'',$val); $val=str_replace(_tr('currency_separator_decimal'),'.',$val); if (!is_numeric($val)) apperror('apperror:invalid parameter '.$key); return $val; }

function SGET($key,$trim=1){$val=isset($_GET[$key])?$_GET[$key]:'';if ($trim) $val=trim($val);return $val;}
function SQET($key,$trim=1){$val=isset($_POST[$key])?$_POST[$key]:'';if ($trim) $val=trim($val);return $val;}

function tzconvert($stamp,$src,$dst){
	
	$tz=date_default_timezone_get();
	
		date_default_timezone_set($src);
		$y=date('Y',$stamp);
		$n=date('n',$stamp);
		$j=date('j',$stamp);
		$h=date('H',$stamp);
		$i=date('i',$stamp);
		$s=date('s',$stamp);
				
		date_default_timezone_set($dst);
		$nstamp=mktime($h,$i,$s,$n,$j,$y);
			
	date_default_timezone_set($tz);
	return $nstamp;	
}

function date2stamp($date,$hour=0,$min=0,$sec=0){
	$parts=explode('-',trim($date));
	if (count($parts)!=3) return null;
	$stamp=mktime($hour,$min,$sec,$parts[1],$parts[2],$parts[0]);
	if (date('Y-n-j',$stamp)!=($parts[0]+0).'-'.($parts[1]+0).'-'.($parts[2]+0) ) apperror('Invalid date');
	
	return $stamp;	
}

function apperror($str,$msg=null,$func=null){header('apperror: '.tabtitle($str));if (isset($func)) header('ERRFUNC: '.$func);if (isset($msg)) {echo $msg;die();} else die('apperror - '.$str);}

function tabtitle($str) {return rawurlencode($str);}

function makelookup($id,$fullscale=0){
?>
<div class="minilookup" id="<?echo $id;?>_lookup"><a id="<?echo $id;?>_lookup_closer" class="labelbutton closer" onclick="gid('<?echo $id;?>_lookup').style.display='none';"><?tr('lookup_closer')?></a>
<div id="<?echo $id;?>_lookup_view" class="lookupview"<?if ($fullscale) echo ' style="height:auto;overflow:normal;"';?>></div></div>
<?	
}



function cancelpickup($id){
?>
<a class="labelbutton" href=# onclick="cancelpickup('<?echo $id;?>');return false;"><?tr('pickup_edit');?></a>
<?	
}

function makechangebar($key,$action){
?>
<div id="changebar_<?echo $key;?>" class="changebar">
<div class="changebar_anchor">
<div class="changebar_view">
<div class="changebar_content">
	<acronym title="Ctrl/CMD + S"><button class="changebar_button" onclick="<?echo $action;?>">Save Changes</button></acronym>
</div>
</div>
<div class="changebar_shadow">
	<button>&nbsp;</button>
</div>
</div>
</div>
<?	
}

function makehelp($id,$text,$once=0,$dx=0){
	global $db;
	global $helpspots; //defined in dict.[lang].php
	global $userhelpspots;
	
	
	$topic='';
	if ($once){
		if (!isset($helpspots[$text])) {
			echo 'invalid helpspot configuration';
			return;	
		}
		
		if (!isset($userhelpsposts)){
			$user=userinfo();
			$userid=$user['userid']+0;
			$userhelpspots=cache_get('userhelpspots_'.$userid);
			if (!is_array($userhelpspots)){
				
				$userhelpspots=array();
				$query="select * from userhelpspots where userid=?";
				$rs=sql_prep($query,$db,$userid);
				while ($myrow=sql_fetch_assoc($rs)) array_push($userhelpspots,$myrow['helptopic']);
			
				cache_set('userhelpspots_'.$userid,$userhelpspots,3600*24*7);
			}
		}
		
		$topic=$text;
		$text=$helpspots[$text];
		
		if (in_array($topic,$userhelpspots)) return;
		
	}//once
?>
	<span id="phelpspot_<?echo $id;?>" helptopic="<?echo $topic;?>" class="phelpspot<?if (!$once) echo '_static';?>">
		<?if ($once){?>
		<span id="helpanchor_<?echo $id;?>" style="cursor:pointer;" onclick="showhelpspot('<?echo $id;?>',<?echo $once;?>);">
			<span class="helpdot"></span>
			<span class="helppulse"></span>
		</span>
		<?} else {?>
		<img id="helpanchor_<?echo $id;?>" class="helpanchor_static" onclick="showhelpspot('<?echo $id;?>',<?echo $once;?>);" src="imgs/dot.gif">		
		<?}?>	
		<?/*
		<img id="helpanchor_<?echo $id;?>" style="cursor:pointer;" onclick="showhelpspot('<?echo $id;?>',<?echo $once;?>);" src="imgs/<?echo $once?'helpspot':'dot';?>.gif" width="50">
		*/?>
		
		<div id="helpspot_<?echo $id;?>" class="helpspot<?if (!$once) echo '_static';?>" style="<?if ($once) echo 'left:'.(15+$dx).'px;'; else echo 'right:-80px;';?>;">
			<?echo $text;?>
			<?if ($once){?>
			<div class="helpack">
				<button onclick="hidehelpspot('<?echo $id;?>','<?echo $topic;?>',<?echo $once;?>,'<?emitgskey('ackhelpspot_'.$topic);?>');">Got it!</button>
			</div>
			<?}?>
		</div>
	</span>
<?
}

function logaction($message,$rawobj=null,$syncobj=null,$gsid=0){
	if (is_callable('userinfo')) {
		$user=userinfo();
		$userid=$user['userid']+0;
		$gsid=$user['gsid']+0;
		$logname=$user['login'];
		//$logname=str_replace("'",'',$logname);
	} else {
		$userid=0; $logname='';
	}

	global $db;
	$wssid=isset($_GET['wssid_'])?($_GET['wssid_']+0):0;

	if (!isset($rawobj)) $rawobj=array();
	//$message=noapos($message);

	$cobj=array();
	foreach ($rawobj as $k=>$v){
		if (is_array($v)) continue;
		$v=noapos($v);
		$v=str_replace('"','&quot;',$v);
		$cobj[$k]=$v;
	}
	
	$obj=json_encode($cobj);
	//$obj=str_replace("\\'","'",$obj);

	$now=time();
	
	if (isset($syncobj)){
		$sid=$wssid;
		$rectype=$syncobj['rectype'];
		$recid=$syncobj['recid']+0;
		$query="insert into ".TABLENAME_ACTIONLOG."(userid,gsid,logname,logdate,logmessage,rawobj,sid,rectype,recid) values (?,?,?,?,?,?,?,?,?)";
		sql_prep($query,$db,array($userid,$gsid,$logname,$now,$message,$obj,$sid,$rectype,$recid));
	} else {
		$query="insert into ".TABLENAME_ACTIONLOG."(userid,gsid,logname,logdate,logmessage,rawobj) values (?,?,?,?,?,?)";
		sql_prep($query,$db,array($userid,$gsid,$logname,$now,$message,$obj));
	}
}

function timeformat($sec){
	$sec_num = $sec+0;
	$hours = floor($sec_num / 3600);
	$minutes = floor(($sec_num - ($hours * 3600)) / 60);
	$seconds = $sec_num - ($hours * 3600) - ($minutes * 60);
		
	if ($hours   < 10) $hours = "0".$hours;
	if ($minutes < 10) $minutes = "0".$minutes;
	if ($seconds < 10) $seconds = "0".$seconds;
	$time  = "$hours:$minutes:$seconds";
	return $time;	
}

function currency_format($val,$digits=2){
	
	$separator_decimal=_tr('currency_separator_decimal');
	$separator_thousands=_tr('currency_separator_thousands');
	
	return number_format($val,$digits,$separator_decimal,$separator_thousands);
}

function dumpgsdbprofile($sort=0){

	global $gsdbprofile;
	if (!isset($gsdbprofile)) return;
	
	if ($sort){
	uasort($gsdbprofile,function($a,$b){
		$ta=$a['time']; $tb=$b['time'];
		if ($ta===$tb) return 0; if ($ta>$tb) return -1; else return 1;
	});		
	}
	
	echo '<pre>'; print_r($gsdbprofile); echo '</pre>';	
}

