<?php
if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
	ob_start("ob_gzhandler");
}else{
	ob_start();
}
session_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 == 'promo') {
        $promoArr 	= $formClass->getPromoDetails($mid);
        $rModel 	= str_replace("_", " ", $promoArr[0]['Model']);
        $rProject 	= $promoArr[0]['Project'];
    }elseif($mtype == 'pricelist') {
        $priceArr 	= $formClass->getPricelistDetails($mid);
        $rModel 	= str_replace("_", " ", $priceArr[0]['Model']);
        $rProject 	= str_replace("_", " ", $priceArr[0]['Project']);
        $rTerms 	= $priceArr[0]['Terms'];
    }elseif($mtype == 'reservation') {
        $reserveArr = $formClass->getReservationDetails($mid);
        $rPhase 	= $reserveArr[0]['Phase'];
        $rBlock 	= $reserveArr[0]['Block'];
        $rLot 		= $reserveArr[0]['Lot'];
        $rBroker 	= str_replace("_", " ", $reserveArr[0]['Broker']);
        $rAgent 	= str_replace("_", " ", $reserveArr[0]['Agent']);
        $rProject 	= str_replace("_", " ", $reserveArr[0]['Project']);
        $rModel 	= str_replace("_", " ", $reserveArr[0]['Model']);
        $rReserveDate 		= $reserveArr[0]['ReservationDate'];
        $rPricelistDate 	= $reserveArr[0]['PricelistDate'];
        $rEmployeeType 		= $reserveArr[0]['EmployeeType'];
        $rEmployeePosition 	= $reserveArr[0]['Position'];
        $rBuyerProfile 		= $reserveArr[0]['BuyerProfile'];
        $coBorrowerDetails 	= $reserveArr[0]['CoBorrowerDetails'];
        $coDetails 			= json_decode($coBorrowerDetails, true);
        if (count($coDetails['details']) != 0) { //fixed error 16DEC24
            $rEmployeeTypeCo1 = $coDetails['details'][0]['employee_type'];
            $rEmployeeTypeCo2 = $coDetails['details'][1]['employee_type'];
            $rEmployeeTypeCo3 = $coDetails['details'][2]['employee_type'];
            $rPositionCo1 = $coDetails['details'][0]['position'];
            $rPositionCo2 = $coDetails['details'][1]['position'];
            $rPositionCo3 = $coDetails['details'][2]['position'];
        }
    }
}

if($selType == "buyer_profile") {
    
    $employeePSelect = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => 
    	"buyer_profile", "select_name" => "reservation_buyer_profile", "select_id" => "buyer_profile", "select_class" => "", "select_default" => $rBuyerProfile, "action" => $action, 
    	"placeholder" => "- Select Buyer Profile -");
    echo $formClass->selectBox($employeePSelect);

}elseif($selType == "detail_terms2") {
    
    $employeePSelect = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => 
    	"payable_terms", "select_name" => "detail_terms[]", "select_class" => "detail_terms", "select_id" => "", "select_default" => $selCondition, "action" => $action, "placeholder" => 
    	"Select One");
    echo $formClass->selectBox($employeePSelect);

}elseif($selType == "detail_terms") {
    
    $employeePSelect = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => 
    	"payable_terms", "select_name" => "detail_terms[]", "select_class" => "detail_terms", "select_id" => "", "select_default" => $rTerms, "action" => $action, "placeholder" => "Select One");
    echo $formClass->selectBox($employeePSelect);

}elseif($selType == "terms") {
    
    $employeePSelect = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => 
    	"payable_terms", "not_condition_field" => "ReferenceValue", "not_condition_value" => "cash", "select_name" => "pricelist_terms", "select_id" => "pricelist_terms", "select_class" => "", "select_default" => $rTerms, "action" => $action, "placeholder" => "Select One");
    echo $formClass->selectBox($employeePSelect);

}elseif($selType == "employee_position") {
    
    $employeePSelect = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => "position", 
    	"select_name" => "reservation_position", "select_id" => "position", "select_class" => "", "select_default" => $rEmployeePosition, "action" => $action, "placeholder" => "- Select Employee Position -");
    echo $formClass->selectBox($employeePSelect);

}elseif($selType == "employee_type") {
    
    $employeeSelect = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => "employee_type", "select_name" => "reservation_employee_type", "select_id" => "employee_type", "select_class" => "", "select_default" => $rEmployeeType, "action" => $action, "placeholder" => "- Select Employee Type -");
    echo $formClass->selectBox($employeeSelect);

}elseif($selType == "employee_type_co1") {
    
    $employeeSelect1 = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => 
    	"employee_type", "select_name" => "reservation_employee_type_co1", "select_id" => "reservation_employee_type_co1", "select_class" => "", "select_default" => $rEmployeeTypeCo1, 
    	"action" => $action, "placeholder" => "- Select Employee Type -");
    echo $formClass->selectBox($employeeSelect1);

}elseif($selType == "employee_type_co2") {

    $employeeSelect2 = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => 
    	"employee_type", "select_name" => "reservation_employee_type_co2", "select_id" => "reservation_employee_type_co2", "select_class" => "", "select_default" => $rEmployeeTypeCo2, 
    	"action" => $action, "placeholder" => "- Select Employee Type -");
    echo $formClass->selectBox($employeeSelect2);

}elseif($selType == "employee_type_co3") {
    
    $employeeSelect3 = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => 
    	"employee_type", "select_name" => "reservation_employee_type_co3", "select_id" => "reservation_employee_type_co3", "select_class" => "", "select_default" => $rEmployeeTypeCo3, 
    	"action" => $action, "placeholder" => "- Select Employee Type -");
    echo $formClass->selectBox($employeeSelect3);

}elseif($selType == "position_co1") {

    $positionSelect1 = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => "position", 
    	"select_name" => "reservation_position_co1", "select_id" => "reservation_position_co1", "select_class" => "", "select_default" => $rPositionCo1, "action" => $action, "placeholder" => "- Select Position -");
    echo $formClass->selectBox($positionSelect1);

}elseif($selType == "position_co2") {

    $positionSelect2 = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => "position", 
    	"select_name" => "reservation_position_co2", "select_id" => "reservation_position_co2", "select_class" => "", "select_default" => $rPositionCo2, "action" => $action, "placeholder" => "- Select Position -");
    echo $formClass->selectBox($positionSelect2);

}elseif($selType == "position_co3") {

    $positionSelect3 = array("field" => "ReferenceValue", "field_value" => "ReferenceValue", "table" => "reference", "condition_field" => "ReferenceType", "condition_value" => "position", 
    	"select_name" => "reservation_position_co3", "select_id" => "reservation_position_co3", "select_class" => "", "select_default" => $rPositionCo3, "action" => $action, "placeholder" => "- Select Position -");
    echo $formClass->selectBox($positionSelect3);

}elseif($selType == "project") {

    $projectSelect = array("field" => "Project", "field_value" => "Project", "table" => "project", "condition_field" => "", "condition_value" => "", "select_name" => "current_project", 
    	"select_id" => "current_project", "select_class" => "sel2project", "select_default" => $rProject, "action" => "view", "placeholder" => "");
    echo $formClass->selectBox($projectSelect);

}elseif($selType == "model") {

    $modelSelect = array("field" => "Model", "field_value" => "Model", "table" => "model", "condition_field" => "Project", "condition_value" => $selCondition, "select_name" => 
    	"current_model", "select_id" => "current_model", "select_class" => "sel2model", "select_default" => $rModel, "action" => $action, "placeholder" => "- Select Model -");
    echo $formClass->selectBox($modelSelect);

}

echo "<script>";
if ($mtype == "pricelist") {

    echo "
    	$('#current_model').on('change', function() {			
    		var a = $('#current_project').val().replace(/_/g, ' ');
    		var c = $('#current_model').val().replace(/_/g, ' ');
    		getDefaultModelValues();
		});
	";

}else if($mtype=="reservation "){

	echo "
		$('#current_project').on('change', function () {
    		var b = $('#current_project').val().replace(/_/g, ' ');
    		loadSelect('model', '#div_model', 'select.select2', b);
		});
	";

	echo "
		$('#current_model').on('change', function () {
    		var a = $('#current_project').val().replace(/_/g, ' ');
    		var c = $('#current_model').val().replace(/_/g, ' ');
    		var r = $('#reservation_date').val();
    		loadSelect1('pricelist', '#div_pricelist', 'select.select2', r);
		});
	";

	echo "
			var r = $('#reservedate').val();
			var p = $('#reservepricedate').val();
			var r = '2013-05-05';
			loadSelect1('pricelist', '#div_pricelist', 'select.select2', r, p);
	";
}

if($selType=="project"){
	echo "
		var b = $('#current_project').val().replace(/_/g, ' ');
		loadSelect('model', '#div_model', 'select.select2', b);
	";
}

	echo "
		$('select[name=\"detail_terms[]\"]').change(function (e) {
    		e.preventDefault();
    		computePF();
		});
	";

	echo "
		$('select[name=\"pricelist_date\"]').change(function (e) {
    		e.preventDefault();
    		var pd = $('#pricelist_date').val();
		});
	";

	echo "
		$('select[name=\"pricelist_terms\"]').change(function (e) {
    		e.preventDefault();
    		computePF();
		});
	";

echo "</script>";