<?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'];
	
	//echo 'regid='.$regid;echo 'partid='.$_REQUEST['pid'];
	//echo $regid;
	$reg_qry = "SELECT reg_id 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'];
		
	$qry = "SELECT allocation FROM avenircms.custom_random_allocation WHERE regid = $regid";
	$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
	$rec = $common->sql->record;
	$custom_cpn_count = $rec[0]['allocation']; 
	
	if($custom_cpn_count == 0)
	{
		// standard arm
		$custom_cpn_count = 2;
		echo $custom_cpn_count;
	}
	else
	{
		// altered arm
	//$survey_id = '166474';
	$surveytable = "lime_survey_".$survey_id;
		
	//$c_pid = $common->getQuestionCode('ParticipantID');
	//echo 'qcode='.$c_pid;
	
	$pid = trim($_REQUEST['pid']);
	//$p_column = '166474X19X1162';
	//$p_column = trim($p_column);
	$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 
						lab_smple_part_id = '".$pid."' and 
						s.lab_smple_tst_id = 1 and 
						s.lab_smple_tst_id = m.lab_id"; 
						//and m.lab_test_name = 'HIV 1 Rapid'";
		//echo $hivquery;
		$common->sql->query($hivquery, SQL_ALL, SQL_ASSOC);
		$recvalues = $common->sql->record;
		//echo 'recval='.$recvalues[0][0];
		if($recvalues[0]['lab_smple_result_id'] == 'RES1') // result is postitive
			$hivrapid1 = 1;
		elseif($recvalues[0]['lab_smple_result_id'] == 'RES2') // result is negative
			$hivrapid1 = 0;
		elseif($recvalues[0]['lab_smple_result_id'] == 'RES3') // result is indeterminate
			$hivrapid1 = 2;
		elseif($recvalues[0]['lab_smple_result_id'] == 'RES4') // result is pending
			$hivrapid1 = 2;
		
		//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 = 7 and		
					s.lab_smple_tst_id = m.lab_id"; 
					//and m.lab_test_name = 'HCV RAPID'";
		$common->sql->query($hcvquery, SQL_ALL, SQL_ASSOC);
		$recvalues = $common->sql->record;
		if($recvalues[0]['lab_smple_result_id'] == 'RES1') // result is postitive
			$hcvrapid = 1;
		elseif($recvalues[0]['lab_smple_result_id'] == 'RES2') // result is negative
			$hcvrapid = 0;
		elseif($recvalues[0]['lab_smple_result_id'] == 'RES3') // result is indeterminate
			$hcvrapid = 2;
		elseif($recvalues[0]['lab_smple_result_id'] == 'RES4') // result is pending
			$hcvrapid = 2;
		
		//echo 'hcvrapid = '.$hcvrapid;
		
		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;
		
		echo 'lab_coeffs='.$coeffs;	
			// Qn DG2
		//$dg2 = trim('166474X3X33');
		$dg2 = trim($common->getQuestionCode('DG2'));
		
		$dg2val = $survalues[0][$dg2];
		if($dg2val == 2)
			$coeffs = $coeffs + 0.044998;
		
		//echo $dg2.$dg2val.$coeffs;
		
		// Variable sexpartners6
		$su42 = trim('166474X7X499');
		$su42val = $survalues[0][$su42];
		
		$su51 = trim('166474X7X388');
		$su51val = $survalues[0][$su51];
		
		$su62 = trim('166474X7X442');
		$su62val = $survalues[0][$su62];
		
		$sexpartners6 = $su42val + $su51val + $su62val;
		//echo 'sp6='.$sexpartners6;
		
		if($sexpartners6 == 0)
			$coeffs = $coeffs + 0;
		elseif($sexpartners6 == 1)
			$coeffs = $coeffs - 0.145929;
		elseif($sexpartners6 >= 2)
			$coeffs = $coeffs - 170326;
		
		//variable pharmdrugs	
		$su5a = trim('166474X7X309');
		$su5aval = $survalues[0][$su5a];
		
		$su6a = trim('166474X7X313');
		$su6aval = $survalues[0][$su6a];
		
		$su7a = trim('166474X7X317');
		$su7aval = $survalues[0][$su7a];
		
		$su8a = trim('166474X7X321');
		$su8aval = $survalues[0][$su8a];
		
		$pharmdrugs = $su5aval + $su6aval + $su7aval + $su8aval;
		echo 'pharmdrugs='.$pharmdrugs;
		// variable numdrugs
		
		$su2 = trim('166474X7X297');
		$su2val = $survalues[0][$su2];
		
		$su3 = trim('166474X7X301');
		$su3val = $survalues[0][$su3];
		
		$su4 = trim('166474X7X305');
		$su4val = $survalues[0][$su4];
		
		$su9 = trim('166474X7X325');
		$su9val = $survalues[0][$su9];
		
		$numdrugs = $su2val + $su3val + $su4val + $su9val; 
		//echo 'numdrugs='.$numdrugs;
		
		if($su2val == 1 && $numdrugs == 1)
			$coeffs = $coeffs + 0;
		
		if($pharmdrugs == 1 && $numdrugs == 1)
			$coeffs = $coeffs + 0.58491;
			
		$nw2 = trim('166474X4X1198');
		$nw2val = $survalues[0][$nw2];
		$log_nw2val = log($nw2val);
		//echo 'log_nw2val='.$log_nw2val;
		
		if($numdrugs > 1 || $su9val == 1)
			$coeffs = $coeffs + 0.317719;
		
		$su5c = trim('166474X7X311');
		$su5cval = $survalues[0][$su5c];
		if($su5cval == 1)
			$coeffs = $coeffs + 0.052741;
		
		$su8c = trim('166474X7X323');
		$su8cval = $survalues[0][$su8c];
		if($su8cval == 1)
			$coeffs = $coeffs - 0.001582;
		
		$su4c = trim('166474X7X307');
		$su4cval = $survalues[0][$su4c];
		if($su4cval == 1)
			$coeffs = $coeffs + 0.633447;
		
		$sv1 = trim('166474X8X569');
		$sv1val = $survalues[0][$sv1];
		if($sv1val == 1 || $sv1val == 2)
			$coeffs = $coeffs + 0.231356;
		
		$su38d = trim('166474X7X382');
		$su38dval = $survalues[0][$su38d];
		if($su38dval == 1 || $su38dval == 2)
			$coeffs = $coeffs + 0.092726;
		
		$su39b = trim('166474X7X38439a');
		$su39bval = $survalues[0][$su39b];
		if($su39bval == 1)
			$coeffs = $coeffs - 0.117843 ;
		
		$su39f = trim('166474X7X38439e');
		$su39fval = $survalues[0][$su39f];
		if($su39fval == 1)
			$coeffs = $coeffs + 0.12871  ;
		
		$su39h = trim('166474X7X38439g');
		$su39hval = $survalues[0][$su39h];
		if($su39hval == 1)
			$coeffs = $coeffs - 0.290038 ;
		
		$su39e = trim('166474X7X38439d');
		$su39eval = $survalues[0][$su39e];
		if($su39eval == 1)
			$coeffs = $coeffs - 0.089896 ;
		
		$su38b = trim('166474X7X380');
		$su38bval = $survalues[0][$su38b];
		if($su38bval == 1 || $su39bval == 2)
			$coeffs = $coeffs + 0.890101 ;
					
		$sv22 = trim('166474X8X1215');
		$sv22val = $survalues[0][$sv22];
		if($sv22val == 1)
			$coeffs = $coeffs + 0.06059 ;
		
		$coeffs = $coeffs + 0.08543; // log of NW2
		
		echo 'coeffs = '.$coeffs;
		
		$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 $custom_cpn_count;
		}
	}
	
	/* function getquestioncode($qn)
	{
		$code = '166474X19X1162';
		$qry = "SELECT * FROM avenirsurvey.lime_questions WHERE title = '".$qn."'";
		return $qry;
		//$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
		//$rec =  $common->sql->record;
		//$code = $rec[0]['sid'];
		//$code = $common->sql->record[0]['sid']."X".$common->sql->record[0]['gid']."X".$common->sql->record[0]['qid'];
		//return $code;
	} */
?>