<?php

class Errors
{
	public function getEm()
	{
		return new PgSql;
	}
	
	public function notNullJsApankForm($param)
	{
		if(empty($_POST[$param])){
			$this->getApankFormJsError($param, 'Tidak Boleh Kosong');
			return true;
		}else{
			return false;
		}
	}

	public function getAlertError($error)
	{
		echo "<script>alert('$error');</script>";
		return;
	}
	
	public function clearErrorDisplay()
	{
		echo "<script>";
		echo "$('.errorApankform').empty()";
		echo "</script>";
		return;
	}
	
	public function getNotAutorized($post, $kode)
	{
		$kode = str_ireplace('/', '', $kode);
	
		echo "<script>";

		$error = false;
		
		foreach($post as $key => $row){
			$a = strrpos($row, $kode);
			
			$b = md5($kode);
			if($b == '28d397e87306b8631f3ed80d858d35f0'){
				$lr = '&#92;';
			}elseif($b == '3590cb8af0bbb9e78c343b52b93773c9'){
				$lr = '&#39;';
			}else{
				$lr = $kode;
			}
			
			if($a === 0){
				echo '$("#errorApank'.$key.'").html("tanda '.$lr.' tidak di izinkan!");';
				$error = true;
			}elseif($a > 0){
				echo '$("#errorApank'.$key.'").html("tanda '.$lr.' tidak di izinkan!");';
				$error = true;
			}
		}
		echo "</script>";

		return $error;
	}
	
	public function getApankFormJsError($form, $message)
	{
		echo "<script>";
		echo "$('#errorApank$form').html('$message')";
		echo "</script>";
		return true;
	}
	
	public function apankPopUpSucess($message)
	{
		echo "<script>";
		echo "$('#errorDisplay').popUp({ top: 250, width: 300, typePopUp: 'alert_succes', title: '".$message."' })";
		echo "</script>";
		return;
	}
	
	public function apankPopUpError($message)
	{
		echo "<script>";
		echo "$('#errorDisplay').popUp({ top: 250, width: 300, typePopUp: 'alert_error', title: '".$message."' })";
		echo "</script>";
		return;
	}

	public function apankPopUpErrorFocus($message, $focus)
	{
		echo "<script>";
		echo "$('#errorDisplay').popUp({ top: 250, width: 300, typePopUp: 'alert_error_focus', title: '".$message."', focus : '".$focus."'})";
		echo "</script>";
		return;
	}

	public function apankPopUpErrorPilihan($message, $focus)
	{
		echo "<script>";
		echo "$('#errorDisplay').popUp({ top: 250, width: 300, typePopUp: 'alert_error_pilihan', title: '".$message."', focus : '".$focus."'})";
		echo "</script>";
		return;
	}
	
	public function emptyForm()
	{
		echo "<script>";
		echo "$('#noTransaksi, #referensi, #kepada, #ket, #cetak, #debet, #kredit').val('');";
		echo "$('#simpan').removeAttr('style');";
		echo "$('#update').css('display', 'none');";
		echo "makeLoadDetail();";
		echo "</script>";
		return;
	}
	
	public function notNull($form)
	{
		if(empty($_POST[$form])){
			echo "<script>";
			echo "$('#$form').css('background-color', '#F6E8E8')";
			echo "</script>";
			return true;
		}else{
			return false;
		}
	}
	
	public function dateFormat($form)
	{
		$date = new Date;
		$tgl  = $date->getConersiTgl($_POST[$form]);
	
		if($tgl === '1970-01-01'){
			$this->getApankFormJsError($form, "Format Tanggal salah");
			return true;
		}else{
			return false;
		}
	}
	
	public function clearError()
	{
		echo "<script>";
		echo "$('.inputGrid').css('background-color', '#fff')";
		echo "</script>";
		return;
	}
	
	public function notDouble($form, $table, $key, $act = false)
	{
		if($act == 'input'){
			$em = $this->getEm();
			$data = $em->getFind($table, $key, array("$key = '".$_POST[$form]."'"));
			if(!empty($data->$key)){
				$this->getApankFormJsError($form, "Sudah di gunakan");
				return true;
			}
		}
		return false;
	}
	
}