<?php
error_reporting(0); //6dec25
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
	ob_start("ob_gzhandler");	
}else{
	ob_start();	
}

include('config/minho.php');
include('config/autoloadClass.php');

$autoLoad 			= new autoloadClass();
$formClass 			= new formClass();
$selType 			= isset($_POST['select_type']) ? trim(strip_tags($_POST['select_type'])) : "";
$selCondition 		= isset($_POST['select_condition_value']) ? trim(strip_tags(htmlentities(addslashes($_POST['select_condition_value'])))) : "";
$action				= isset($_POST['action']) ? trim(strip_tags(htmlentities(addslashes($_POST['action'])))) : "";
$mid 				= isset($_POST['mid']) ? trim(strip_tags(htmlentities(addslashes($_POST['mid'])))) : "";
$mtype 				= isset($_POST['mtype']) ? trim(strip_tags(htmlentities(addslashes($_POST['mtype'])))) : "";
$mmodel 			= isset($_POST['mmodel']) ? str_replace("_", " ", trim(strip_tags(htmlentities(addslashes($_POST['mmodel']))))) : "";
$mproj 				= isset($_POST['mproj']) ? str_replace("_", " ", trim(strip_tags(htmlentities(addslashes($_POST['mproj']))))) : "";
$mrdate 			= isset($_POST['mrdate']) ? trim($_POST['mrdate']) : "";
$reserveArr 		= array();
$promoArr 			= array();
$priceArr 			= array();
$brokerArr 			= array();
$rPhase 			= "";
$rBlock 			= "";
$rLot 				= "";
$rBroker 			= "";
$rAgent 			= "";
$rProject 			= "";
$rModel 			= "";
$rReserveDate 		= "";
$rEmployeeType 		= "";
$rEmployeePosition 	= "";
$rBuyerProfile 		= "";
$rEmployeeTypeCo1 	= "";
$rEmployeeTypeCo2 	= "";
$rEmployeeTypeCo3 	= "";
$coBorrowerDetails 	= "";
$rPositionCo1 		= "";
$rPositionCo2 		= "";
$rPositionCo3 		= "";
$rTerms 			= "";
$rPricelistDate 	= '';

if ($action == "edit" || $action == "view" || $action == "transfer") {

    if ($mtype == 'agent') {
    
        $brokerArr 	= $formClass->getBrokerAgent($mid);
        $rBroker 	= $brokerArr[0]['Broker'];
    
    } else if ($mtype == 'reservation') {

        $brokerArr 	= $formClass->getReservationDetails($mid);
        $rPhase 	= $brokerArr[0]['Phase'];
        $rBlock 	= $brokerArr[0]['Block'];
        $rLot 		= $brokerArr[0]['Lot'];
        $rBroker 	= str_replace("_", " ", $brokerArr[0]['Broker']);
        $rAgent 	= str_replace("_", " ", $brokerArr[0]['Agent']);
        $rProject 	= str_replace("_", " ", $brokerArr[0]['Project']);
        $rModel 	= str_replace("_", " ", $brokerArr[0]['Model']);
    }
}

if ($selType == "broker") {

    $brokerSelect = array("field" => "Broker", "field_value" => "Broker", "table" => "broker", "condition_field" => "", "condition_value" => "", "select_name" => "current_broker", "select_id" => "current_broker", "select_class" => "sel2broker", "select_default" => $rBroker, "action" => $action, "placeholder" => "- Select a Broker -");
    echo $formClass->selectBox($brokerSelect);

}elseif($selType == "agent") {
 
    $agentSelect = array("field" => "Agent", "field_value" => "Agent", "table" => "agent", "condition_field" => "Broker", "condition_value" => $selCondition, "select_name" => "current_agent", "select_id" => "current_agent", "select_class" => "sel2agent", "select_default" => $rAgent, "action" => $action, "placeholder" => "- Select an Agent -");
    echo $formClass->selectBox($agentSelect);

}

echo "<script>";
if ($mtype == "reservation") {
    echo "
    	$('#current_broker').on('change', function() {
    		var b = $('#current_broker').val().replace(/_/g, ' ');
    		loadSelectAgent('agent', '#div_agent', 'select.select2', b);
		});
	";

	if($selType=="broker"){
		echo "
			var b = $('#current_broker').val().replace(/_/g, ' ');
			loadSelectAgent('agent', '#div_agent', 'select.select2', b);
		";
	}

}

echo " </script>";