<?
define('ROOT','..');
define('AJAX',true);
ob_start('ob_gzhandler');

require_once(ROOT.'/.config.php');
require_once(LIBS_PATH.'/system/sys_start.php');
require_once(LIBS_PATH.'/system/load_libs.php');
$X->STR->post_filter();


class ajax
{
	function ajax() {
		$this->FILE_FIELDS = array('file','image');

		switch($_GET['type']) {
			default:				die();					break;
			case 'form':		$this->_form();		break;
			case 'content':	$this->_content();	break;
		}
	}


	function _form() {
		global $X;

		$this->CONTENT_TYPE='js';
		if(empty($_GET['id'])) $this->write(' alert("No form_id presented!"); ');
		$this->ID=preg_replace('#(.*)__([0-9]+)#', '$1', str_replace(array('..','_form','form_'), array('','',''), addslashes(strip_tags(trim($_GET['id'])))));
		$this->FORM=$_GET['form']=$this->ID.'_form';
		if($_SESSION['form_pre_validation'][$this->ID]!==md5('dh@'.$this->ID.' >> '.$X->CONF->BROWSER['name'].' '.$X->CONF->BROWSER['major_version'].' << ajax_form')) $this->write(' alert("Form transaction is not started in browser!"); ');


		if($_GET['inline_req']==1 || isset($_POST[$this->ID.'_req_data'])) {
			$REQ=@unserialize($X->STR->show(strip_tags(stripslashes($_POST[$this->ID.'_req_data']))));
			if(!is_array($REQ) || count($REQ)==0) $this->write(' alert("No form config data specified! ('.$this->ID.')"); ');
		} else {
			$cfg_file=($_SESSION['form_pre_validation_backend'][$this->ID]===true ? '../admin/includes/':'').'forms/'.$this->ID.'.php';
			if(!file_exists($cfg_file)) $this->write(' alert("No form config file specified'.($_SESSION['form_pre_validation_backend'][$this->ID]===true ? ' in admin':'').'! ('.$this->ID.'.php)"); ');
			require_once($cfg_file);
		}


		if(is_array($REQ) && count($REQ)>0) foreach($REQ as $req) {
			if(@in_array($req[0],$this->FILE_FIELDS)) $req[1].='_';
			$PASS=$this->check_field($req);
			if(@in_array($req[0],$this->FILE_FIELDS)) $req[1]=substr($req[1],0,-1);

			if($PASS==false) {
				$this->ERROR = array(
					'type' 			=> $req[0],
					'name' 			=>	$req[2],
					'min_len' 		=> $req[1],
					'optional' 		=> ($req[1]==0 ? true:false),
					'label' 			=> $req[3],
					'message_type' => $this->MESSAGE_TYPE,
					'message_data' => $r[4],
					'focus_data' 	=> $this->FOCUS_DATA,
				);
				break;
			}
		}

		if(is_array($this->ERROR)) {
			$this->handle_error();
		} else {
			unset($_SESSION['form_pre_validation'][$this->ID]);
			unset($_SESSION['form_pre_validation_backend'][$this->ID]);

			if(function_exists('callback')) call_user_func('callback');
			$_SESSION['form_validation'][$this->ID]=true;

			$this->HTML='
				//<!-- FORM_SUBMIT -->
				f=$("#'.$this->ID.'_form");
				if(f.attr("action")=="/X/") f.attr("action","'.$form_action.'");

				f.unbind("submit").removeAttr("onsubmit").submit();
				f.bind("submit", function(){ return false; });
			';
		}
		$this->write();
	}


	function check_field($req) {
		$type 		= $req[0];
		$min_len		= $req[1];
		$post_name 	= $req[2];
		$value 		= @trim($_POST[$post_name]);
		$message 	= $req[3];
		$config 		= $req[4];

		return require(LIBS_PATH.'/_form/validation_methods.php');
	}


	function handle_error() {
		global $X;

		unset($_SESSION['form_validation']);
		$err=$this->ERROR;
		$focus=$this->FOCUS_DATA;
/*
		switch($err['type']) {
			default:
			case 'text':
			case 'textarea':
				if($err['optional']==true) {
					$message=$X->LANG['form_bledna_wartosc'].' "$LABEL"';
				} else {
					$message=$X->LANG['form_wypelnij_pole'].' "$LABEL"';
				}
			break;

			case 'checkbox':
				$message=$X->LANG['form_zaznacz_pole'].' "$LABEL"';
			break;

			case 'checkbox_array':
			case 'radio':
			case 'select':
			case 'multiselect':
				$message=$X->LANG['form_dokonaj_wyboru'].'dupa aaa "$LABEL"';
			break;

			case 'image':
			case 'file':
				$message=$X->LANG['form_wybierz_plik'].' "$LABEL"';
	//			$special_focus = '$("#'.$error['field_name'].'_focus").focus(); $("#'.$error['field_name'].'_focus").blur();';
			break;
		}
*/


		if(!empty($err['message_type'])) {
			switch($err['message_type']) {
				default:					$message=$err['message_type']; break;
				case 'extension':		$message=$X->LANG['form_filetype'].' "$LABEL". '.$X->LANG['form_dozwolone_to'].': '.strtoupper(str_replace('|',', ',$err['message_data']['extensions'])); break;
				case 'email':			$message=$X->LANG['form_bledny_email']; break;
				case 'link':			$message=$X->LANG['form_bledny_link']; break;
				case 'potw_hasla':	$message=$X->LANG['form_potw_hasla']; break;
				case 'unique':			$message='Wartość w polu "<b>$LABEL</b>" jest już używana w systemie. Podaj inną'; break;
			}
		}

		//$message='Ten adres E-mail w polu <b>$LABEL</b> jest już zajęty!';
//		'.(!empty($special_focus) ? $special_focus : 'document.'.$_GET['form_name'].'.'.$error['field_name'].'.focus();').'
		$message=(!empty($err['label']) && empty($message) ? $err['label'] : $message);

		if($this->MESSAGE_TIME>0) {
			$if_time='"TIM":"'.$this->MESSAGE_TIME.'", ';
			$this->MESSAGE_TIME=0;
		}


		$this->HTML='
			message = \''.$message.'\';
			'.$this->ID.'_xForm.field_error({'.$if_time.'"TYP":"'.$err['type'].'", "NAM":"'.$err['name'].'", "LEN":"'.$err['min_len'].'"'.(!empty($message) ? ', "LAB":message':'').'});
		';
	}


	function _content() {
		$this->CONTENT_TYPE='html';

	}


	function write($html=false) {
		switch($this->CONTENT_TYPE) {
			default:
			case 'js':			header('Content-type: text/javascript; charset=utf-8');		break;
			case 'html':		header('Content-type: text/html; charset=utf-8');				break;
		}

		if(!empty($html)) die($html);
		die($this->HTML);
	}


/*
	function import_default_lang() {
		global $X;

		$X->LANG['form_wypelnij_pole']	= 'Wypełnij pole';
		$X->LANG['form_bledna_wartosc']	= 'Błędna wartość w polu';
		$X->LANG['form_zaznacz_pole']		= 'Zaznacz pole';
		$X->LANG['form_dokonaj_wyboru']	= 'Dokonaj wyboru w polu';

		$X->LANG['form_wybierz_plik']		= 'Wybierz plik w polu';
		$X->LANG['form_filetype']			= 'Nieprawidłowe rozszerzenie pliku w polu';
		$X->LANG['form_dozwolone_to']		= 'Dozwolone to';

		$X->LANG['form_haslo']				= 'Hasło';
		$X->LANG['form_haslo2']				= 'Potwierdzenie hasła';
		$X->LANG['form_potw_hasla']		= 'Hasło i jego potwierdzenie muszą być identyczne!';
		$X->LANG['form_bledny_email']		= 'Błędny adres E-mail!';
		$X->LANG['form_bledny_link']		= 'Podany link jest nieprawidłowy!';
	}
*/


}
$AJAX=new ajax();
?>