<?php
	$s_id = $_REQUEST['id'];
	$sur_qry = "SELECT sur_onlne_id FROM custom_survey_master WHERE sur_code = '".$s_id."'";
	$common->sql->query($sur_qry, SQL_ALL, SQL_ASSOC);
	$survey_id = $common->sql->record[0]['sur_onlne_id'];
	
	
	$reg_qry = "SELECT reg_id,reg_participant_type FROM avenircms.custom_participant_registration WHERE reg_participant_id = '".$_REQUEST['pid']."'";	
	
	$common->sql->query($reg_qry, SQL_ALL, SQL_ASSOC);
	$recvalues = $common->sql->record;
	$regid = $recvalues[0]['reg_id'];
	
		// Getting allocation from random allocation table
	$qry = "SELECT regid,allocation FROM custom_random_allocation WHERE used = 1 and regid = $regid";
	$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
	$rec = $common->sql->record;
	if(count($rec) == 1)
		$alloc = $rec[0]['allocation'];
	
	
	$custom_cpn_count = $alloc;
	
	if($custom_cpn_count == 0)
	{
		// standard arm
		$custom_cpn_count = 2;
		echo 'S'.$custom_cpn_count;
	}
	else
	{
		// altered arm
	//$survey_id = '166474';
	$surveytable = "lime_survey_".$survey_id;
		
	$pid = trim($_REQUEST['pid']);

	$p_column = trim($common->getQuestionCode('ParticipantID'));
	$qry = "SELECT * FROM avenirsurvey.$surveytable WHERE $p_column = '$pid' and submitdate != ''";
	
	$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
	$survalues = $common->sql->record;
	
	if($survalues[0]['submitdate'] != '')  // proceed only if submitdate is present
		{
		$coeffs = -2.886112;
		
		//$hivquery = "SELECT lab_smple_result_id FROM avenircms.custom_lab_samples as s,avenircms.custom_lab_master as m WHERE 
		$hivquery = "SELECT * FROM avenircms.custom_lab_samples as s,avenircms.custom_lab_master as m WHERE
						lab_smple_part_id = '".$pid."' and 
						s.lab_smple_tst_id = 1 and 
						s.lab_smple_tst_id = m.lab_id"; 
	
		$common->sql->query($hivquery, SQL_ALL, SQL_ASSOC);
		$recvalues = $common->sql->record;
		
		if(count($recvalues) == 0)
		{
			//Sending message 'Lab test not completed for this participant';
			echo 'A111';
			exit;
		}
		
		if($recvalues[0]['lab_smple_result_id'] == 1) // result is postitive
			$hivrapid1 = 1;
		elseif($recvalues[0]['lab_smple_result_id'] == 0) // result is negative
			$hivrapid1 = 0;
		elseif($recvalues[0]['lab_smple_result_id'] == 2) // result is indeterminate
			$hivrapid1 = 2;
		elseif($recvalues[0]['lab_smple_result_id'] == 9) // result is pending
			{
				
				echo 'A111';
				exit;
			}
		else 
		{
			//Sending message 'Lab test not completed for this participant';
			echo 'A111';
			exit;
		}
		
		//echo 'hivrapid='.$hivrapid1;
		
		$hcvquery = "SELECT lab_smple_result_id FROM avenircms.custom_lab_samples as s,avenircms.custom_lab_master as m WHERE 
					lab_smple_part_id = '".$pid."' and
					s.lab_smple_tst_id = 2 and		
					s.lab_smple_tst_id = m.lab_id"; 
				
		$common->sql->query($hcvquery, SQL_ALL, SQL_ASSOC);
		$recvalues = $common->sql->record;
		
		if(count($recvalues) == 0)
		{
			//Sending message 'Lab test not completed for this participant';
			echo 'A111';
			exit;
		}
		
		if($recvalues[0]['lab_smple_result_id'] == 1) // result is postitive
			$hcvrapid = 1;
		elseif($recvalues[0]['lab_smple_result_id'] == 0) // result is negative
			$hcvrapid = 0;
		elseif($recvalues[0]['lab_smple_result_id'] == 2) // result is indeterminate
			$hcvrapid = 2;
		elseif($recvalues[0]['lab_smple_result_id'] == 9) // result is pending
		{
		
			echo 'A111';
			exit;
		}
		else
		{
			//Sending message 'Lab test not completed for this participant';
			echo 'A111';
			exit;
		}
		
		
		if($hivrapid1 != 1 && $hcvrapid != 1)
			$coeffs = $coeffs + 0;
		elseif($hivrapid1 == 1 && $hcvrapid != 1)
			$coeffs = $coeffs + 0.264022;
		elseif($hivrapid1 != 1 && $hcvrapid == 1)
			$coeffs = $coeffs + 0.210931;
		elseif($hivrapid1 == 1 && $hcvrapid == 1)
			$coeffs = $coeffs + 0.383665;
		
			
			// Qn DG2
		$dg2 = trim($common->getQuestionCode('DG2'));
		$dg2val = $survalues[0][$dg2];
		if($dg2val == 2)
			$coeffs = $coeffs + 0.044998;
		
	 	// Variable sexpartners6
		$su42 = trim($common->getQuestionCode('SU42'));
		$su42val = $survalues[0][$su42];
		
		$su51 = trim($common->getQuestionCode('SU51'));
		$su51val = $survalues[0][$su51];
		
		$su62 = trim($common->getQuestionCode('SU62'));
		$su62val = $survalues[0][$su62];
		
		$sexpartners6 = $su42val + $su51val + $su62val;
		
		if($sexpartners6 == 0)
			$coeffs = $coeffs + 0;
		elseif($sexpartners6 == 1)
			$coeffs = $coeffs - 0.145929;
		elseif($sexpartners6 >= 2)
			$coeffs = $coeffs - 0.170326;
		
		//variable pharmdrugs	
		$su5a = trim($common->getQuestionCode('SU5a'));
		$su5aval = $survalues[0][$su5a];
		
		$su6a = trim($common->getQuestionCode('SU6a'));
		$su6aval = $survalues[0][$su6a];
		
		$su7a = trim($common->getQuestionCode('SU7a'));
		$su7aval = $survalues[0][$su7a];
		
		$su8a = trim($common->getQuestionCode('SU8a'));
		$su8aval = $survalues[0][$su8a];
		
		$pharmdrugs = $su5aval + $su6aval + $su7aval + $su8aval;
		
		// variable pharmdrugs_cat  
		if($pharmdrugs >= 1)
			$pharmdrugs_cat = 1;
		elseif($pharmdrugs == 0)
			$pharmdrugs_cat = 0;
		
		// variable numdrugs
		
		$su2 = trim($common->getQuestionCode('SU2a'));
		$su2val = $survalues[0][$su2];
		
		$su3 = trim($common->getQuestionCode('SU3a'));
		$su3val = $survalues[0][$su3];
		
		$su4 = trim($common->getQuestionCode('SU4a'));
		$su4val = $survalues[0][$su4];
		
		$su9 = trim($common->getQuestionCode('SU9a'));
		$su9val = $survalues[0][$su9];
		
		//$numdrugs = $su2val + $su3val + $su4val + $su9val; 
		$numdrugs = $pharmdrugs_cat + $su2val + $su3val + $su4val + $su9val;
		
		if($su2val == 1 && $numdrugs == 1)
			$coeffs = $coeffs + 0;
		
		//if($pharmdrugs == 1 && $numdrugs == 1)
		if($pharmdrugs_cat == 1 && $numdrugs == 1)
			$coeffs = $coeffs + 0.58491;
			
		$nw2 = trim($common->getQuestionCode('NW2'));
		$nw2val = $survalues[0][$nw2];
		$log_nw2val = log($nw2val);
	
		
		if($numdrugs > 1 || $su9val == 1)
			$coeffs = $coeffs + 0.317719;
		
		$su5c = trim($common->getQuestionCode('SU5c'));
		$su5cval = $survalues[0][$su5c];
		if($su5cval == 1)
			$coeffs = $coeffs + 0.052741;
		
		$cf_su5c = $coeffs;	
			
		$su8c = trim($common->getQuestionCode('SU8c'));
		$su8cval = $survalues[0][$su8c];
		if($su8cval == 1)
			$coeffs = $coeffs - 0.001582;
		
		$cf_su8c = $coeffs;
			
		$su4c = trim($common->getQuestionCode('SU4c'));
		$su4cval = $survalues[0][$su4c];
		if($su4cval == 1)
			$coeffs = $coeffs + 0.633447;
		
		$cf_su4c = $coeffs;	
			
		$sv1 = trim($common->getQuestionCode('SV1'));
		$sv1val = $survalues[0][$sv1];
		if($sv1val == 1 || $sv1val == 2)
			$coeffs = $coeffs + 0.231356;
		
		$cf_sv1 = $coeffs;	
			
		$su38d = trim($common->getQuestionCode('SU38d'));
		$su38dval = $survalues[0][$su38d];
		if($su38dval == 1 || $su38dval == 2)
			$coeffs = $coeffs + 0.092726;
		
		$cf_su38d = $coeffs;	
			
		//$su39b = trim($common->getQuestionCode('SU39b'));
		$su39b = '166474X7X38439b';
		$su39bval = $survalues[0][$su39b];
		$varsu39b = $su39bval;
		if($su39bval == 'Y')
			$coeffs = $coeffs - 0.117843 ;
		
		$cf_su39b = $coeffs;	
		
		// this is hardcoded
		//$su39f = trim($common->getQuestionCode('SU39f'));
		$su39f = '166474X7X38439f';
		$su39fval = $survalues[0][$su39f];
		$varsu39f = $su39fval;
		if($su39fval == 'Y')
			$coeffs = $coeffs + 0.12871  ;
		
		$cf_su39f = $coeffs;	
		
		// this is hardcoded
		//$su39h = trim($common->getQuestionCode('SU39h'));
		$su39h = '166474X7X38439h';
		$su39hval = $survalues[0][$su39h];
		if($su39hval == 'Y')
			$coeffs = $coeffs - 0.290038 ;
		
		$cf_su39h = $coeffs;	
		
		// this is hardcoded
		//$su39e = trim($common->getQuestionCode('SU39e'));
		$su39e = '166474X7X38439e';
		$su39eval = $survalues[0][$su39e];
		if($su39eval == 'Y')
			$coeffs = $coeffs - 0.089896 ;
		
		$cf_su39e = $coeffs;	
			
		$su38b = trim($common->getQuestionCode('SU38b'));
		$su38bval = $survalues[0][$su38b];
		//if($su38bval == 1 || $su39bval == 2)
		if($su38bval == 1 || $su38bval == 2)
			$coeffs = $coeffs + 0.890101 ;
		
		$cf_su38b = $coeffs;	
			
		$sv22 = trim($common->getQuestionCode('SV22'));
		$sv22val = $survalues[0][$sv22];
		if($sv22val == 1)
			$coeffs = $coeffs + 0.06059 ;
		
		$cf_sv22 = $coeffs;
		
		//$coeffs = $coeffs + 0.08543; // log of NW2
		$log_nw2val = $log_nw2val * 0.08543;
		$coeffs = $coeffs + $log_nw2val;
				
		$exp = exp($coeffs);
		//echo '*exp='.$exp;
	
		$prob = $exp/(1+$exp);
		//echo '*prob='.$prob;
		
		if($prob > 0.11)
			$custom_cpn_count = 5;
		else 
			$custom_cpn_count = 2;
		
			
			
			echo 'A,'.$sexpartners6.','.$pharmdrugs.','.$pharmdrugs_cat.','.$numdrugs.','.$log_nw2val.','.$coeffs.','.$prob.','.$cf_su5c.','.$cf_su8c.','.$cf_su4c.','.$cf_sv1.','.$cf_su38d.','.$cf_su39b.','.$cf_su39f.','.$cf_su39h.','.$cf_su39e,','.$cf_su38b.','.$cf_sv22.','.$custom_cpn_count;
		}
	else 
		{
			//Sending message 'Survey not completed for this Participant';
			echo 'A222';
			exit;
		}
	}
	
	
?>