Location: PHPKode > projects > Homeless Mangement Information System > hmis/include/client_id_class.inc
<?php

class Client_id {
	//var $birth_year;
	//var $client_id_value;
	var $vetted_form_value;
	var $questions;
	var $question_elements;
    var $questions_client_profile;
    var $question_elements_client_profile;
    var $answered_client_profile_questions;
	//no longer used...
    //var $sharing_permission_question;
    //var $sharing_permission_question_elements;
    var $anonymous;
    var $modded_info;
    
	function client_identifier_form_validation_setup ($form_answer, $copy_number)
	{
        settype ( $copy_number, 'integer' );
        
        if ( $copy_number < 1 )
		{
            $copy_number = 0;
        }
        
        /*
        //if ( !$form_answer['anon0']['anon_yes0']  )
		//{
            //$form_answer['ss_number0'] = $this->validate_and_convert_ss_num_for_db ( 'ss_number0', $form_answer['ss_number0'] );
            
		    //Pass all the questions related to the lead
			//echo pretty_var_dump($this->questions_client_profile);
			//echo pretty_var_dump($this->questions);
			//$return_questions['question_encrypted'] = $this->questions['question_encrypted'];
			
            $return_questions["client_gender0"] = $this->questions["client_gender0"];
		    $return_questions['name_first0'] = $this->questions['name_first0'];
            $return_questions['name_middle0'] = $this->questions['name_middle0'];
		    $return_questions['name_last0'] = $this->questions['name_last0'];
            $return_questions['name_suffix0'] = $this->questions['name_suffix0'];
		    $return_questions["birth_month0"] = $this->questions["birth_month0"];
		    $return_questions["birth_day0"] = $this->questions["birth_day0"];
		    $return_questions["client_year_born0"] = $this->questions["client_year_born0"];
            $return_questions['ss_number0'] = $this->questions['ss_number0'];
			$return_questions['client_status0'] = $this->questions['org_client_status0'];  //status
		    //$return_questions["client_ethnicity0"] = $this->questions["client_ethnicity0"];
		    $return_questions["org_generated_client_id0"] = $this->questions["org_generated_client_id0"];
            $return_questions["client_rpt_sharing_permission0"] = $this->questions["client_rpt_sharing_permission0"];
            
            //echo "Here are all answers";
            //var_dump ($form_answer);
            ////Validate the ss number
            //Take out the seperation chars
            
            //$return_questions['ss_number0'] = str_replace ( $return_questions['ss_number0']
            
		    //secondary_form_answer_validation ($element_title, $question_id, $form_answer_edit, $min_field_size="", $ereg="", $max_field_size="", $range_bottom="", $range_top="")
            
			// For names accept any alpha character or any number of - or ' characters. 
			// Could limit to two hyphens but you never know...
            
            secondary_form_answer_validation ( 'First name. Must only contain letters, apostraphes, dashes, or spaces.', 
			  'name_first0', $form_answer['name_first0'], '', 'A-Za-z\' \\-', '', '', '', 'name');
            secondary_form_answer_validation ( "Middle name. Must only contain letters, apostraphes, dashes, or spaces.".
			  " Use a \"-\" if client has no middle initial. ",
              'name_middle0', trim($form_answer['name_middle0']), '', 'A-Za-z\' \\-', '', '', '', 'name');
		    secondary_form_answer_validation ('Last name. Must be letters, apostraphes, dashes, or space. ', 
			  'name_last0', trim($form_answer['name_last0']), '', 'A-Za-z\' \\-', '', '', '', 'name');
        //}
        /*
        else {
            //echo "Yes, is anon<p>";
            $this->anonymous[0] = "yes";
            $return_questions["client_gender0"] = $this->questions["client_gender0"];
            $return_questions["client_year_born0"] = $this->questions["client_year_born0"];
            $return_questions["org_generated_client_id0"] = $this->questions["org_generated_client_id0"];
            $return_questions["client_rpt_sharing_permission0"] = $this->questions["client_rpt_sharing_permission0"];
			$return_questions['client_status0'] = $this->questions['org_client_status0'];  //status             
        }
        
        
        foreach($this->questions_client_profile['0'] AS $profile_quest_id => $current_question)   {
            //echo "Profile question: ".$profile_quest_id." was answered<br/>";
            $this->answered_client_profile_questions[$profile_quest_id] = $current_question;
            //echo "Profile value was: ".$this->answered_client_profile_questions[$profile_quest_id]."<br/>";                 
        }

*/
		//Loop through the form answers for non-leads, passing the variables only if there is a first initial value
		for( $i = 0; $i < $copy_number; $i++)	{

            /*
            if ( $form_answer["anon".$i]["anon_yes".$i]  )  {
                //echo "Yes, is anon<p>";
                $this->anonymous[$i] = "yes";
                $return_questions["client_gender".$i] = $this->questions["client_gender".$i];
                $return_questions["client_year_born".$i] = $this->questions["client_year_born".$i];
                $return_questions["client_relationship_to_lead".$i] = $this->questions["client_relationship_to_lead".$i];
                $return_questions["org_generated_client_id".$i] = $this->questions["org_generated_client_id".$i];
                $return_questions["client_rpt_sharing_permission".$i] = $this->questions["client_rpt_sharing_permission".$i];
                $return_questions['client_status'.$i] = $this->questions['org_client_status'.$i];  //status
                				
                foreach($this->questions_client_profile[$i] AS $profile_quest_id => $current_question)   {
                    //echo "Profile question: ".$profile_quest_id." was answered<br/>";
                    $this->answered_client_profile_questions[$profile_quest_id] = $current_question;
                    //echo "Profile value was: ".$this->answered_client_profile_questions[$profile_quest_id]."<br/>";                 
                }      
            
            
            }
            */

			//if ( $form_answer['name_first'.$i] || $form_answer['noan']['name_first'.$i] )	{
			    //echo "At hit on subform # $i<br/>"; //debug
				$return_questions['client_gender'.$i] = $this->questions['client_gender'.$i];
				$return_questions['name_first'.$i] = $this->questions['name_first'.$i];
                $return_questions['name_middle'.$i] = $this->questions['name_middle'.$i];
				$return_questions['name_last'.$i] = $this->questions['name_last'.$i];
				$return_questions["birth_month".$i] = $this->questions["birth_month".$i];
				$return_questions["birth_day".$i] = $this->questions["birth_day".$i];
				$return_questions["client_year_born".$i] = $this->questions["client_year_born".$i];
                $return_questions['ss_number'.$i] = $this->questions['ss_number'.$i];
				$return_questions['client_status'.$i] = $this->questions['org_client_status'.$i];  //status
				//$return_questions["client_ethnicity".$i] = $this->questions["client_ethnicity".$i];
				if ( $i != 0 )  {
                    $return_questions["client_relationship_to_lead".$i] = $this->questions["client_relationship_to_lead".$i];
                }
				$return_questions["org_generated_client_id".$i] = $this->questions["org_generated_client_id".$i];
                $return_questions["client_rpt_sharing_permission".$i] = $this->questions["client_rpt_sharing_permission".$i];
				
                if ( ereg ( '[^#]', $form_answer['name_first'.$i] ) )   {
	                secondary_form_answer_validation ( 'First name. Must only contain letters, apostraphes, dashes, or spaces.', 
				      'name_first0', $form_answer['name_first'.$i], '', 'A-Za-z\' \\-', '', '', '', 'name');
                }
	            if ( ereg ( '[^#]', $form_answer['name_middle'.$i] ) )   {
                    secondary_form_answer_validation ( "Middle name. Must only contain letters, apostraphes, dashes, or spaces.".
				      " Use a \"-\" if client has no middle initial. ",
	                  'name_middle0', trim($form_answer['name_middle'.$i]), '', 'A-Za-z\' \\-', '', '', '', 'name');
                }
			    if ( ereg ( '[^#]', $form_answer['name_last'.$i] ) )   {
                    secondary_form_answer_validation ('Last name. Must be letters, apostraphes, dashes, or space. ', 
				      'name_last0', trim($form_answer['name_last'.$i]), '', 'A-Za-z\' \\-', '', '', '', 'name');
                }
                
                foreach($this->questions_client_profile[$i] AS $profile_quest_id => $current_question)
				{
                    //echo "Profile question: ".$profile_quest_id." was answered<br/>"; //debug
                    $this->answered_client_profile_questions[$profile_quest_id] = $current_question;
                    //echo "Profile value was: ".$this->answered_client_profile_questions[$profile_quest_id]."<br/>"; //debug
                }
			 
			//}
		 
		}
        
		return $return_questions;
	}



    function client_report_form_validation_setup ($form_answer, $copy_number)
	{
        for($i = 0; $i < $copy_number; $i++)
		{
            foreach($this->questions_client_profile[$i] AS $profile_quest_id => $current_question)
			{
                //echo "Profile question: ".$profile_quest_id." was answered<br/>";
                $this->answered_client_profile_questions[$profile_quest_id] = $current_question;
            }
        }
    }


	
	function set_client_info_session_variables ( $vetted_value )
	{
		GLOBAL $client_info, $user_id, $unix_date;
		//unset($client_info); //Destroys its global nature to use unset in later versions of PHP
        $client_info = '';
        //echo "<p>In set_client_info_session_variables, value submitted is:<p>";
        //echo display_value($vetted_value);

        $clientcount = count ( $vetted_value );
        //echo "Client count is: ".$clientcount."<p>";
        foreach ( $vetted_value AS $value_key => $value )
		{
			//Pull out the client number at the end of form variable key
			preg_match_all ( '/\d+/', $value_key, $client_number );
           	$client_num = $client_number[0][0]; 
			settype ( $client_num, 'integer' );
            
			//Pull out a variable minus the number, leaving only the text 
			if (!strlen ($client_num) < 1)    {
                $key_number_removed = str_replace($client_num, '', $value_key);
            }
            //echo "client_num is: ".$client_num." Value_key is: ".$value_key." ,
            //Value is: ".$value."<br/>";
            
			//Set the session variable $client_info to the value
			$client_info[$client_num][$key_number_removed] = $value; 
			//echo $key_number_removed."--".$value."-<br>";
			//echo "<p>client_info:";
			//var_dump($client_info);
			//echo "</p>";
		}

		foreach ( $client_info AS $session_variable_key => $ignored_value )
		{
            //echo "Client id is: ".$session_variable_key."<p>";
            //if ( $this->anonymous[$session_variable_key] != 'yes' )
			//{
                //Make the initials upper case for consistency
                if ( ereg ( '[#]', $client_info[$session_variable_key]['name_first'] ) )   {
                    $anon = 'yes';
                    //$client_info[$session_variable_key]['name_first'] = '';
                }
                else {
                    $client_info[$session_variable_key]['initial_first'] = substr (
                      strtoupper ( $client_info[$session_variable_key]['name_first'] ), 0, 1 );
                } 
                                
                if ( ereg ( '[#]', $client_info[$session_variable_key]['name_middle'] ) )   {
                    $anon = 'yes';
                    //$client_info[$session_variable_key]['name_middle'] = '';
                }
                else {
                    $client_info[$session_variable_key]['initial_middle'] = substr (
                      strtoupper($client_info[$session_variable_key]['name_middle'] ), 0, 1 );
                }
                
                if ( ereg ( '[#]', $client_info[$session_variable_key]['name_last'] ) )   {
                    $anon = 'yes';
                    //$client_info[$session_variable_key]['name_last'] = '';
                }
                else {
                    $client_info[$session_variable_key]['initial_last'] = substr (
                      strtoupper($client_info[$session_variable_key]['name_last']), 0, 1 );
                }

                if ( ereg ( '[#]', $client_info[$session_variable_key]['birth_month'] ) )   {
                    $anon = 'yes';
                    //$client_info[$session_variable_key]['birth_month'] = '';
                }
                else {
                    //Convert month and year to two digit format
                    $birth_month = substr ( $client_info[$session_variable_key]['birth_month'] + 100, 1 );
                }
                
                if ( ereg ( '[#]', $client_info[$session_variable_key]['birth_day'] ) )   {
                    $anon = 'yes';
                    //$client_info[$session_variable_key]['birth_day'] = '';
                }
                else {
                    //Convert month and year to two digit format
                    $birth_day = substr ( $client_info[$session_variable_key]['birth_day'] + 100, 1 );
                }
                if ( ereg ( '[#]', $client_info[$session_variable_key]['birth_year'] ) )   {
                    $anon = 'yes';
                    //$client_info[$session_variable_key]['birth_year'] = '';
                }
                
                if ( ereg ( '[#]', $client_info[$session_variable_key]['client_gender'] ) )   {
                    $anon = 'yes';
                    //$client_info[$session_variable_key]['gender'] = '';
                }
                
                //Build Client Identifier and insert into session array
                if ( $anon != 'yes' )
				{
                    $client_info[$session_variable_key]['client_identifier'] = 
                    $client_info[$session_variable_key]['initial_first'].$client_info[$session_variable_key]['initial_middle'].$client_info[$session_variable_key]['initial_last'].$birth_month.$birth_day.$client_info[$session_variable_key]['client_year_born'].$client_info[$session_variable_key]['client_gender'];
                    //echo $client_info[$session_variable_key]["client_identifier"]."-Identifier<br>";
                    
                    //Find the matching client id, and modify the client identifier as necessary to make it unique
                    $this->client_id_find ( $client_info[$session_variable_key], 1 );
                    $client_info[$session_variable_key]['client_id'] = $this->modded_info['client_id'];
                    $client_info[$session_variable_key]['client_identifier'] = $this->modded_info['client_identifier'];
                }
                else {
                    //echo "client #  ". $session_variable_key. " is anon <p>";                
                    $anon_id = $unix_date + $session_variable_key;
                    $client_info[$session_variable_key]["initial_first"] = 'Anonymous';
                    $client_info[$session_variable_key]["client_identifier"] = "anon_".$user_id."_".$anon_id;
                    $client_info[$session_variable_key]["client_id"] = "0";
                }
            

            //Duplicate the client session variables used to assemble the unique client id with their mirror questions used to store client profile data 
			$client_info[$session_variable_key]["98"] = $client_info[$session_variable_key]["org_client_status"];
            $client_info[$session_variable_key]["100"] = $client_info[$session_variable_key]["client_year_born"];
            $client_info[$session_variable_key]["101"] = $client_info[$session_variable_key]["client_gender"];
		    
            $anon = '';
        }
        
		//Set the lead relationship to lead as "head of household"
		$client_info[0]['client_relationship_to_lead'] = 'head of household'; //echo " 232 ";
            
        $_SESSION['client_infoB'] = $client_info;
            
        //echo "Value at end of setting client session var is:<br>";
        //echo display_value ($_SESSION['client_infoB']); 
	}



    function set_client_info_session_variables_client_profile ($vetted_value)
	{
        GLOBAL $client_info;
    
        foreach ($vetted_value AS $key => $current_value)
		{
            //echo "Key is: ".$key."; Value is: ".$current_value."<br/>";
            
            $position_of_split_character = strpos ($key, "_");
            $question_number = substr ($key, 0, $position_of_split_character);
            //echo "Question number is: ".$question_number."<br/>"; 
            $client_number = substr ($key, $position_of_split_character + 1);
           //echo "Client number is: ".$client_number."<br/>";
            
            $client_info[$client_number][$question_number] = $current_value;
            $_SESSION['client_infoB'] = $client_info;
        }
    }



	//Find clients already in the database that match the client identifier, or mark them as new
	function client_id_find ( $client_info, $attempt_to_add_uniqueness = '' )
	{
        //Look for a matching identifier, return the existing client id if a match is found
        $client_info = $this->find_matching_client_identifiers ( $client_info );
        
        //If a match was found above, check to see if the ss numbers are different.
        if ( $client_info['client_id'] === -1 ) {
            //If they have different sysids, return -1 and tack the ss num to the end of the sys identifier, if matching, return client id
            $client_info = $this->resolve_client_identifier_conflict_using_ss_number ( $client_info );
            
            //If the clients with the same sys ID have different ss numbers...
            if ( $client_info['client_id'] === -1 ) {
                //...add the ss number to the end of the sys id... 
                $client_info['client_identifier'] = $client_info['client_identifier'].$client_info['ss_number'];
                //...and check for matches to the new sys id (e.g. tlk05281969male537875433)
                $client_info = $this->find_matching_client_identifiers ( $client_info );
                
                //If there were no matches the the new extended sys id (e.g. tlk05281969male537875433), mark this as a new client
                if ( $client_info['client_id'] === -1 ) {
                    $client_info['client_id'] = '0';    
                }
            }
        }
        
        $this->modded_info['client_id'] = $client_info['client_id'];
        $this->modded_info['client_identifier'] = $client_info['client_identifier'];
        return $client_info;
        
        //  If ss's are same, return the matching client id
        //  If differnt, return system identifier with the ss number tacked on
        
        
        /*
        switch ( $attempt_to_add_uniqueness ) {
            
            case 1:
                $client_info = $this->find_matching_client_identifiers ( $client_info );
                break;
            $this->modded_info['client_id'] = $client_info['client_id'];
            $this->modded_info['client_identifier'] = $client_info['client_identifier'];
            return $client_info;
            case 2:
                $client_info = $this->resolve_client_identifier_conflict_using_ss_number ( $client_info );
                //$client_info = $this->find_matching_client_identifiers ( $client_info );
                break;               
            
            case 3:
                $client_info = $this->resolve_client_identifier_conflict_by_adding ( $client_info, 'name_first' );
                $client_info = $this->find_matching_client_identifiers ( $client_info );
                 break;
                
            case 4:
                $client_info = $this->resolve_client_identifier_conflict_by_adding ( $client_info, 'name_middle' );
                $client_info = $this->find_matching_client_identifiers ( $client_info );
                 break;
                
            case 5:
                $client_info = $this->resolve_client_identifier_conflict_by_adding ( $client_info, 'name_last' );
                $client_info = $this->find_matching_client_identifiers ( $client_info );
                break;
            
            case 6:
                $client_info = $this->resolve_client_identifier_conflict_by_adding ( $client_info, 'ss_number' );
                $client_info = $this->find_matching_client_identifiers ( $client_info );
                break;
            
            case 7:
                $client_info = $this->resolve_client_identifier_conflict_by_adding_random_number ( $client_info );
                $client_info = $this->find_matching_client_identifiers ( $client_info );
                break;
               
            default:
                //
                echo 'Ran out of ability to ad uniqueness to client system id'; exit;               
        
        }
        
        //echo "Client identifier is: ".$client_info['client_identifier']."<p>";
        //$client_id = $this->find_matching_client_identifiers ( $client_info );
        
        
        $client_info = $this->find_matching_client_identifiers ( $client_info );
              
        
        if ( $client_info['client_id'] === -1 ) {
            //echo 'There is a conflict<p>';
            //$attempt_to_add_uniqueness++;
            $client_info = $this->resolve_client_identifier_conflict_using_ss_number ( $client_info );
               
        
        }
        
        else {
            //echo 'We have found an ID, and it is '.$client_info['client_id'].' - unique count is '.$attempt_to_add_uniqueness.'<p>';
            $this->modded_info['client_id'] = $client_info['client_id'];
            $this->modded_info['client_identifier'] = $client_info['client_identifier'];
            return $client_info;
        }
        */
        
    }



    function find_matching_client_identifiers ( $client_info )
	{
        GLOBAL $user_id, $unix_date;
        
        $en = new Encryption ();
        $cli_identifier = $en->encrypt_data ( $client_info['client_identifier'] );
        
        $sql = "SELECT client_id FROM client_system_ids WHERE client_identifier LIKE '".$cli_identifier."'";
		//echo $sql.'<p>';
        $matching_client_result = run_query ( $sql, 'Client MatchA' );
        
        $number_of_matches_duplicated = num_rows ( $matching_client_result );
        
        //Pull out the matches, but eliminate duplicate entries
        for ( $i = 0; $i < $number_of_matches_duplicated; $i++ )
		{
            $m = fetch_array ( $matching_client_result, 'Fetch matching client id', $i );
            $matches[$m[0]] = $m[0];
        }
        //var_dump ( $matches );
        
        $number_of_matches = count ( $matches );
        
        /*//////////
        //If only one match, use it		
        if ( $number_of_matches == 1 )	{
			//echo "There is a matching id".$copy_number."gg<br>";
			$client_id_array = fetch_array ( $matching_client_result, 'Fetch Matching Client ID B', '0' );
			$client_info['client_id'] = $client_id_array['client_id'];
        }
        
        
        //////*/
		//No matching client in database, so mark them as new with a "0"
		if ( $number_of_matches < 1 )	{
			//echo 'There is NOT a matching id '.$copy_number.'<br/>';
            $client_info['client_id'] = '0';
            //echo "The client_id is ".$client_info['client_id']."<p>";
           
		}
        //More than one matching system id, so resolve conflict
        elseif ( $number_of_matches >= 1 )	{
            //echo "There is more than one client identifier<br>";
            $client_info['client_id'] = -1;
            
        }
        
		return $client_info;
    }



    function resolve_client_identifier_conflict_using_ss_number ( $client_info )
	{
        $en = new Encryption ();
        $cli_identifier = $en->encrypt_data ( $client_info['client_identifier'] );
        $cli_id_ss = $en->encrypt_data ( $client_info['ss_number'] );        
        
        $sql = "SELECT client_id FROM client_system_ids WHERE client_identifier LIKE '".$cli_identifier."'";
		$matching_client_result = run_query ( $sql, 'Client MatchA' );
        
        $number_of_matches_duplicated = num_rows ( $matching_client_result );
        
        //Pull out the matches, but eliminate duplicate entries
        for ( $i = 0; $i < $number_of_matches_duplicated; $i++ )   {
            
            $m = fetch_array ( $matching_client_result, 'Fetch matching client id', $i );
            $matches[$m[0]] = $m[0];
                   
        }
        
        $sql = "
        SELECT client_id 
            FROM 
                org_generated_client_ids 
            WHERE 
                org_generated_client_id LIKE '".$cli_id_ss."'
                AND id_type LIKE 'ss_number'        
        ";
		$matching_client_ss = run_query_return_array ( $sql, 'Client Match by ss number' );
        //Find any matches between ids
        //echo "<p>";
        //var_dump ( $matching_client_ss );
         //echo "<p>";
        if ( is_array ( $matching_client_ss ) )  {
            foreach  ( $matching_client_ss AS $cl_id )    {
                //echo "cl_id is: ".$cl_id[0]." - Matches is: ".$matches[$cl_id[0]]."<p>";
                if ( $matches[$cl_id[0]] = $cl_id[0] )   {            
                    $ret_cl_id = $cl_id[0];
                    //echo 'A ss number matches for '.$ret_cl_id. '<p/>';
                    break;
                }
            }
        }
        //If no clients with the same system identifier have a matching social security number, add the ss num and try again 
        if ( !$ret_cl_id )  {
            
            $ret_cl_id = -1;
            //$client_info['client_identifier'] = $client_info['client_identifier'].$client_info['ss_number'];      
        }
        
        $client_info['client_id'] = $ret_cl_id;
        
        //echo "Client id returned from ss function is ".$client_info['client_id']."<p>";
        
        return $client_info;
   
    }



    function resolve_client_identifier_conflict_by_adding ( $client_info, $add_this_id_type )    {
    
        $client_info['client_identifier'] = $client_info['client_identifier'].$client_info[$add_this_id_type];             
        return $client_info;         
                 
    }

    
    
    function resolve_client_identifier_conflict_by_adding_random_number ( $client_info )    {
        $client_info['client_identifier'] = $client_info['client_identifier'].rand ( 1, 999999999 );             
        return $client_info;   
    
    }



	function client_id_insert ( $client_identifier, $keep_same_client_id_num = '' )	{
		GLOBAL $unix_date, $user_id;	
        
        //$client_identifier = addslashes( $client_identifier );
        $en = new Encryption ();
        $client_identifier = $en->encrypt_data ( $client_identifier );        
		
		if ( $keep_same_client_id_num ) {
            $sql = "
            INSERT INTO client_system_ids (
                client_id, 
                client_identifier, 
                date_stamp
                ) 
                VALUES (
                '".$keep_same_client_id_num."', 
                '".$client_identifier."', 
                '".$unix_date."'
                )";
		    run_query($sql, "Client ID insert");
            
            $client_id = $keep_same_client_id_num;           
        
        
        }
        else {
         
            $sql = "
            INSERT INTO clients (
                client_id,
                client_iden, 
                date_stamp
                ) 
                VALUES (
                '0',
                '".$client_identifier."',  
                '".$unix_date."'
            )";
		    run_query($sql, 'Client ID insert');

		    $sqlb = "
            SELECT clientrowid 
                FROM clients 
                WHERE 
                    client_id = '0'
                    AND client_iden = '".$client_identifier."'
                    AND date_stamp = '".$unix_date."'
                    
            ";
		    $result = run_query( $sqlb, 'Client ID insert B' );

		    $client_id = fetch_result($result, "Client ID insert C");

		    $sqlc = "
            UPDATE clients 
                SET client_id='".$client_id."' 
                WHERE clientrowid = '".$client_id."'";
		    run_query( $sqlc, 'Client ID insert D' );
            
            $sqld = "
            INSERT INTO client_system_ids (
                client_id, 
                client_identifier, 
                date_stamp
                ) 
                VALUES (
                '".$client_id."', 
                '".$client_identifier."', 
                '".$unix_date."'
                )";
		    run_query( $sqld, 'Client ID insert' ); 
            
            
            

		    //$sqld = "INSERT INTO client_profiles (client_id) VALUES ('".$client_id."')";
		    //run_query($sqld, "Client ID insert d");
            //echo "Client id ASSIGNEDDDDDDD is: ".$client_id."<p>";
        }

		return $client_id;
	}



    function client_id_assign ( $org_id, $keep_same_client_id_num = '' )
	{
        GLOBAL $client_info, $user_id, $unix_date, $unix_date_encrypted;
        
        $en = new Encryption ();
        $unix_date_encrypted = $en->encrypt_data ( $unix_date );
        
        transaction_begin ( 'Trans begin for client_id_assign' );

        foreach ( $client_info AS $key => $current_client_info )
		{
            //echo "The key is: ".$key."<br>";
            //$curr_client_id = $this->client_id_find ( $current_client_info );

            //If the client is not in the system, insert the id and put it in the global variable...
            if ( $current_client_info['client_id'] == '0' )	{
                
                $client_info[$key]['client_id'] = $this->client_id_insert ( $current_client_info['client_identifier'], $keep_same_client_id_num );              
                $_SESSION['client_infoB'][$key]['client_id'] = $client_info[$key]['client_id'];
                //echo "Assigned a new client id of: ".$client_info[$key]["client_id"]."<p>";
            }
            
            else {
                $client_info[$key]['client_id'] = $current_client_info['client_id'];
                $_SESSION['client_infoB'][$key]['client_id'] = $client_info[$key]['client_id'];
                //echo "ID already existed and is: ".$client_info[$key]["client_id"]."<p>";            
            }
            
            $cid = $client_info[$key]['client_id'];
            
            $this->client_identifier_insert ( $cid, 'name_first', $current_client_info['name_first'], $org_id );
            $this->client_identifier_insert ( $cid, 'name_middle', $current_client_info['name_middle'], $org_id );
            $this->client_identifier_insert ( $cid, 'name_last', $current_client_info['name_last'], $org_id );
            $this->client_identifier_insert ( $cid, 'name_suffix', $current_client_info['name_suffix'], $org_id );
            $this->client_identifier_insert ( $cid, 'client_gender', $current_client_info['client_gender'], $org_id );
            $this->client_identifier_insert ( $cid, 'birth_month', $current_client_info['birth_month'], $org_id );
            $this->client_identifier_insert ( $cid, 'birth_day', $current_client_info['birth_day'], $org_id );
            $this->client_identifier_insert ( $cid, 'client_year_born', $current_client_info['client_year_born'], $org_id );
            $this->client_identifier_insert ( $cid, 'ss_number', $current_client_info['ss_number'], $org_id );
            $this->client_identifier_insert ( $cid, 'org_generated_client_id', $current_client_info['org_generated_client_id'], $org_id );
            //status
            $this->client_status_insert ( $cid, $org_id, $current_client_info['org_client_status'] );
        }
        
        transaction_commit ( 'Trans commit for client_id_assign' );

	}

	//status
    function client_status_insert ( $client_id, $org_id, $status_id ) {
	    GLOBAL $unix_date_encrypted;
        
        $matched = 0;
        
        $sqly = "
        SELECT * 
            FROM org_client_status 
            WHERE 
                client_id = '".$client_id."'
                AND org_id = '".$org_id."'
        ";

        $result_matches = run_query ( $sqly, 'checking for matching identifiers/orgs in status table' );
        $res_count = num_rows ( $result_matches );
        
        //If an ID exists for this organization, check to see if it matches
        if ( $res_count > 0 )   {
            $old_cl_status_array = fetch_array ( $result_matches, 'Pulling arry for client id', 0 );
            //If it does not match, deprecate the old id
            if ( $old_cl_status_array['client_id'] != $client_id && $old_cl_status_array['org_id'] != $org_id)   {
                $matched = -1;
            }
            else {
                $matched = 1;    
            }
        }
		
        //If there is no existing client id + org id pair, or if this is a new id, insert
        if ( $res_count == 0 || $matched == -1 )    {
            
	        $sqls = "
	        INSERT INTO org_client_status
	            ( 
	                client_id,
	    			status_id,
	    			org_id,
	    			date_stamp
	            ) 
	            VALUES
	            ( '".$client_id."',
	                '".$status_id."',
	                '".$org_id."',
	                '".$unix_date_encrypted."'
	            )
			";
	        
	        run_query ( $sqls, 'inserting client status' );
        }
        
		//If there was a match, update status...
        if ( $matched == 1 )   {
            //Decided to simply use client_id and org_id as primary key making following code block obsolete 
			//but unsure if this was wise...
			//Pull out the new rowid for placement in the deprecated id
            /*$sqlc = "
            SELECT org_gen_rowid 
                FROM org_client_status 
                WHERE 
                    client_id = '".$client_id."' 
                    AND org_id = '".$org_id."'
            ";

            $resc = run_query ( $sqlc, 'Pulling rowid for cli id and org id pair in status table' );
            $new_rowid = fetch_result ( $resc, 'New rowid for cli id' );
            */
            //Postgresql 7.3 does not accept '' for NULLs
                            
            $sqld = "
            UPDATE org_client_status
                SET status_id = '".$status_id."'
                WHERE client_id = '".$old_cl_status_array['client_id']."'
				AND org_id =  '".$old_cl_status_array['org_id']."'
            ";
			
            run_query ( $sqld, 'Noting sucession of client status' );
        
        }

	}

    function client_identifier_insert ( $client_id, $id_type, $id_val, $org_id ) {
        GLOBAL $unix_date_encrypted;
        
		//echo "INSERT: client_identifier_insert($client_id, $id_type, $id_val, $org_id)<br>";
        //$en = new Encryption();
        //$unix_date_encrypted = $en->encrypt_data ( $unix_date );
        $matched = 0;
        
        if ( $id_val )  {
            $sqly = "
            SELECT * 
                FROM org_generated_client_ids 
                WHERE 
                    client_id = '".$client_id."' 
                    AND org_id = '".$org_id."'
                    AND id_type LIKE '".$id_type."'
                    AND succeeded_by IS NULL
            ";
            
            $result_matches = run_query ( $sqly, 'checking for matching identifiers' );
            $res_count = num_rows ( $result_matches );
            
            $en = new Encryption ();
            $id_val_en = $en->encrypt_data ( $id_val );
            
            //If an ID exists for this organization, check to see if it matches
            if ( $res_count > 0 )   {
                $old_cl_id_array = fetch_array ( $result_matches, 'Pulling array for client id', 0 );
                //If it does not match, deprecate the old id
                if ( $old_cl_id_array['org_generated_client_id'] != $id_val_en )   {
                    $matched = -1;
                    //echo "ID does not match ".$old_cl_id_array['org_generated_client_id']." -- ".$id_val_en."<p>";
                                   
                }
            }
            
            //If there is no existing id of this type from this org, or if this is a new id, insert
            if ( $res_count == 0 || $matched == -1 )    {

                //echo "Setting the org generated client id ".$sqly."<br/>";
                $id_char_one = substr ( $id_val, 0, 1 );
                $id_char_two = substr ( $id_val, 1, 1 );
                $id_char_three = substr ( $id_val, 2, 1 );
                $id_char_four = substr ( $id_val, 3, 1 );
                $id_char_five = substr ( $id_val, 4, 1 );
                $id_char_six = substr ( $id_val, 5, 1 );
                $id_char_seven = substr ( $id_val, 6, 1 );
                $id_char_eight = substr ( $id_val, 7, 1 );
                
                //Encrypt everything
                //$en = new Encryption ();
                
                $id_char_one = $en->encrypt_data ( $id_char_one );
                $id_char_two = $en->encrypt_data ( $id_char_two );
                $id_char_three = $en->encrypt_data ( $id_char_three );
                $id_char_four = $en->encrypt_data ( $id_char_four );
                $id_char_five = $en->encrypt_data ( $id_char_five );
                $id_char_six = $en->encrypt_data ( $id_char_six );
                $id_char_seven = $en->encrypt_data ( $id_char_seven );
                $id_char_eight = $en->encrypt_data ( $id_char_eight );
                
                $sqli = "
                INSERT INTO org_generated_client_ids 
                    ( 
                      client_id, id_type, org_generated_client_id, 
                      id_char_one,
                      id_char_two,
                      id_char_three,
                      id_char_four,
                      id_char_five,
                      id_char_six,
                      id_char_seven,
                      id_char_eight,     
                      org_id,
                      date_stamp 
                    ) 
                    VALUES
                    ( '".$client_id."', '".$id_type."', '".$id_val_en."', 
                      '".$id_char_one."',
                      '".$id_char_two."',
                      '".$id_char_three."',
                      '".$id_char_four."',
                      '".$id_char_five."',
                      '".$id_char_six."',
                      '".$id_char_seven."',
                      '".$id_char_eight."',
                      '".$org_id."',
                      '".$unix_date_encrypted."'  
                    )
                ";
                
                run_query ( $sqli, 'inserting client identifier' );
            }
            
            if ( $matched == -1 )
            {
                //Pull out the new rowid for placement in the deprecated id
                $sqlc = "
                SELECT org_gen_rowid 
                    FROM org_generated_client_ids 
                    WHERE 
                        client_id = '".$client_id."' 
                        AND org_id = '".$org_id."' 
                        AND id_type LIKE '".$id_type."'
                        AND date_stamp LIKE '".$unix_date_encrypted."'
                ";
                
                $resc = run_query ( $sqlc, 'Pulling new rowid for cli id' );
                $new_rowid = fetch_result ( $resc, 'New rowid for cli id' );
                
                $sqld = "
                UPDATE org_generated_client_ids
                    SET succeeded_by = '".$new_rowid."'
                    WHERE org_gen_rowid = '".$old_cl_id_array['org_gen_rowid']."'
                
                ";
                run_query ( $sqld, 'Noting sucession of client id' );
            }
            
        }
    
    }
    


	function assemble_client_id_entry_table ($client_report_type, $copy_number, $vetted_client_profile_answer, $vetted_form_answer="" )
    {
		GLOBAL $head_dynamic_style, $form_answer;
        
		$head_dynamic_style .= ".identrytop {color: #FFFFFF; font-size: 12; background-color: #000099; font-weight: bolder}"; 
        
		for($i = 0; $i < $copy_number; $i++)
		{
            if ( $client_report_type == "client_identifier" )
			{
			    $z = $i+1;
                
                $final_html .= "<table><tr>";
                
                $final_html .= "<td class=\"identrytop\" colspan=\"7\">Household Member #".$z."</td>";
                //$final_html .= "<td class=\"identrytop\" colspan=\"5\">Anonymous entry:<input type=\"checkbox\"
                //name=\"form_answer[anon".$i."][anon_yes".$i."]\" value=\"yes\" tabindex=\"".$z."103\"";
                //if ( $form_answer["anon".$i]["anon_yes".$i] == "yes" ) { $final_html .= " checked=\"checked\""; }
                //$final_html .= ">";
                $final_html .= "</td></tr>";
                
                $final_html .= "<tr>".display_text_question ( $this->questions['name_first'.$i], $vetted_form_answer, 'yes' );
                
                $final_html .= display_text_question ( $this->questions['name_middle'.$i], $vetted_form_answer, 'yes' ); 
                
			    $final_html .= display_text_question ( $this->questions['name_last'.$i], $vetted_form_answer, 'yes' );
                
                $final_html .= display_text_question ( $this->questions['name_suffix'.$i], $vetted_form_answer, 'yes' );
                
			    $final_html .= display_radio_buttons_question ( $this->questions['client_gender'.$i],
                 $this->question_elements['client_gender'.$i], $vetted_form_answer, 'yes' );
                $final_html .= "<tr><td class=\"bottomline\" colspan=\"5\">&nbsp;</td></tr>";
                $final_html .= '</tr></table>';
                
                $final_html .= "\n<table><tr>";
                
			    //$final_html .= display_dropdown_list_question ($this->questions["birth_month".$i], $this->question_elements["birth_month".$i], $vetted_form_answer, "yes");
                
                $final_html .= display_number_question ($this->questions["birth_month".$i], $vetted_form_answer, "yes");
                
			    $final_html .= display_number_question ($this->questions["birth_day".$i], $vetted_form_answer, "yes");
                
			    $final_html .= display_number_question ($this->questions["client_year_born".$i], $vetted_form_answer, "yes");
                
                $final_html .= display_text_question ( $this->questions['ss_number'.$i], $vetted_form_answer, 'yes' );
                
				$final_html .= display_dropdown_list_question ($this->questions["org_client_status".$i], $this->question_elements["org_client_status".$i], $vetted_form_answer, 'yes', "", FALSE); //status
				
                $final_html .= '</tr></table>';
            }
            
            $final_html .= "<table>";
            $td_count = 0;
            $final_html .= "<tr><td class=\"bottomline\" colspan=\"2\">&nbsp;</td></tr>";
            
            foreach ($this->questions_client_profile[$i] AS $key => $current_question)
			{
                //display_value ($current_question);
            
                //Make rows two questions long using <tr> and </tr>
			    if ($td_count == 2)	{
                    $final_html .= "</tr>\n<tr><td class=\"bottomline\" colspan=\"2\">&nbsp;</td></tr>"; $td_count = 0; 
                }
			    if ($td_count == 0) {
                    $final_html .= "<tr>\n";
                }
                //echo "Current_question id is: ".$current_question["question_id"]."</br>";
                $current_question["question_display_order"] = $z.$current_question["question_display_order"];                        
                $final_html .= question_display ($current_question,
                 $this->question_elements_client_profile[$current_question["question_id"]], $vetted_client_profile_answer);
                $td_count++;
            }
            
            if ( $client_report_type == "client_identifier" )
			{
			        if($i != "0")
					{
				        if ($td_count == 2)	{
                            $final_html .= "</tr>\n<tr><td class=\"bottomline\" colspan=\"2\">&nbsp;</td></tr>"; $td_count = 0;
                        }
			            if ($td_count == 0) {
                            $final_html .= "<tr>\n";
                        }
                        $final_html .= display_radio_buttons_question ($this->questions["client_relationship_to_lead".$i],
                        $this->question_elements["client_relationship_to_lead".$i], $vetted_form_answer, "yes");
                        $td_count++;
		            }

                    if ($td_count == 2)	{
                        $final_html .= "</tr>\n<tr><td class=\"bottomline\" colspan=\"2\">&nbsp;</td></tr>"; $td_count = 0;
                    }
			        if ($td_count == 0) {
                        $final_html .= "<tr>\n";
                    }
			        $final_html .= display_text_question ($this->questions["org_generated_client_id".$i], $vetted_form_answer, "yes");
                    $td_count++;

	                if ($td_count == 2)	{
	                    $final_html .= "</tr>\n<tr><td class=\"bottomline\" colspan=\"2\">&nbsp;</td></tr>"; $td_count = 0;
	                }
				    if ($td_count == 0) {
	                    $final_html .= "<tr>\n"; 
	                }
				    $final_html .= display_radio_buttons_question ($this->questions["client_rpt_sharing_permission".$i],
	                     $this->question_elements["client_rpt_sharing_permission".$i], $vetted_form_answer, "yes");
	                     $td_count++;
            }
            
            if($td_count == "1")    {
                $final_html .= "<td>&nbsp;</td></tr>\n";
            }
			if($td_count == "2"){   
                $final_html .= "</tr>\n";
            }
			$final_html .= "</table><p></p>";
		}

            if ( $client_report_type == "client_identifier" )   {
			    $final_html .= "<table><tr>".display_number_question ($this->questions["extra_client_id_forms"], "", "yes");
		        $final_html .= "</tr></table>";
            }
			
    	return $final_html;

	}


    //$search_form set to yes so [SELECT STATUS] option appears in status dropdown on search form only.
	function assemble_client_system_id_lookup_entry_table ( $vetted_form_answer, $title, $search_form='' )
	{
		$final_html .= "<table cellpadding=0><tr><td class=\"generictabletop\">".$title."</td></tr></table>";  //class=\"headline\"
        
        $i = '0';
		
		$final_html .= '<br><table cellspacing=0 cellpadding=10>';
        
		$final_html .= "<tr>".display_text_question ( $this->questions['name_first'.$i], $vetted_form_answer, 'yes' );
        
        $final_html .= display_text_question ( $this->questions['name_middle'.$i], $vetted_form_answer, 'yes' );  
        
		$final_html .= display_text_question ( $this->questions['name_last'.$i], $vetted_form_answer, 'yes' );
        
        $final_html .= display_radio_buttons_question ( $this->questions["client_gender".$i], $this->question_elements["client_gender".$i], $vetted_form_answer, "yes");
        
        $final_html .= display_text_question ( $this->questions['ss_number'.$i], $vetted_form_answer, 'yes' );
        
        $final_html .= "</tr><tr><td class=\"bottomline\" colspan=\"5\">&nbsp;</td></tr><tr>";
		
        $final_html .= display_number_question ($this->questions["birth_month".$i], $vetted_form_answer, "yes");
        
		$final_html .= display_number_question ($this->questions["birth_day".$i], $vetted_form_answer, "yes");
        
		$final_html .= display_number_question ($this->questions["client_year_born".$i], $vetted_form_answer, "yes");
        
		$final_html .= display_dropdown_list_question ($this->questions["org_client_status".$i], $this->question_elements["org_client_status".$i], $vetted_form_answer, 'yes', $search_form); //status
        
        $final_html .= display_text_question ( $this->questions['org_generated_client_id'.$i], $vetted_form_answer, 'yes' );
		
        $final_html .= "</tr></table>";  //<td class=\"tdq\">&nbsp;</td></tr>...
        
		return $final_html;
    }



    function pull_client_profile_questions ($copy_number, $questions, $question_elements)
	{
        foreach ($questions AS $key => $current_question)
		{
            $modified_id = $current_question["question_id"]."_".$copy_number;
        
            //echo "Question key is: ".$current_question["question_id"]."</br>";
        
            $this->questions_client_profile[$copy_number][$modified_id] = $current_question;
            $this->questions_client_profile[$copy_number][$modified_id]["question_id"] = $modified_id;          
            
           // if ($question_elements[$key])   {
                                              
                //$this->question_elements_client_profile[$modified_id] = $question_elements[$current_question["question_id"]];
                $this->question_elements_client_profile[$modified_id] = $question_elements[$current_question["question_id"]];
                //$this->question_elements_client_profile[$modified_id] = $question_elements[$current_question["question_id"]];
                //echo "Current question is : ".$current_question["question_id"]."<br>";
                //echo "Modified ID is: ".$modified_id."<p>";
                
             //                               }
        
        //display_value($this->question_elements_client_profile[$modified_id]); 
        }
    }





	function pull_client_id_entry_questions_array ( $copy_number='0', $questions_for_client_lookup = '' )		{
		GLOBAL $unix_date, $org_name;
        $srt = $copy_number + 1;
        
		$this->questions["name_first".$copy_number] = array ("question_id" => "name_first".$copy_number, "question_title" => "First name:", "question_type" => "text",  "question_field_size" => '30', 'question_display_size' => '8', "question_display_order" => $srt."103", "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes", 'question_no_answer_options' => '5' );
        
        $this->questions['name_middle'.$copy_number] = array ('question_id' => "name_middle".$copy_number, "question_title" => "Middle
        name:", "question_type" => "text",  "question_field_size" => '30', 'question_display_size' => '8', "question_display_order" => $srt."103", "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes", 'question_no_answer_options' => '5' );

		$this->questions["name_last".$copy_number] = array ("question_id" => "name_last".$copy_number, 'question_title' => 'Last
        name:', "question_type" => "text",  "question_field_size" => '30', 'question_display_size' => '8', "question_display_order" =>
        $srt.'103', "question_required" => 'yes', "question_default_value" => '', "question_visible" => 'yes', 'question_no_answer_options' => '5' );

        $this->questions['name_suffix'.$copy_number] = array ('question_id' => 'name_suffix'.$copy_number, 'question_title' => 'Suffix
        (Jr., II, etc.):', "question_type" => "text",  "question_field_size" => '10', 'question_display_size' => '3',
        'question_display_order' =>
        $srt.'103', 'question_required' => 'no', "question_default_value" => '', "question_visible" => 'yes',
        'question_no_answer_options' => '0' );

        $this->questions['client_gender'.$copy_number] = array ( 'question_id' => "client_gender".$copy_number, "question_title" => "Gender:", "question_type" => "radio", "question_field_size" => "", "question_range_bottom" => "", "question_range_top" => "", "question_display_order" => $srt."103", "question_required" => "yes", "question_default_value" => "male".$copy_number, "question_visible" => "yes", 'question_no_answer_options' => '5' );
 
		$this->question_elements["client_gender".$copy_number]["male".$copy_number] = array ("question_element_id" => "element_client_gender_male".$copy_number, "question_id" => "client_gender".$copy_number, "question_element_title" => "Male", "question_element_value" => "male");

		$this->question_elements["client_gender".$copy_number]["female".$copy_number] = array ("question_element_id" => "element_client_gender_female".$copy_number, "question_id" => "client_gender".$copy_number, "question_element_title" => "Female", "question_element_value" => "female");

		$this->question_elements["client_gender".$copy_number]["transgender_male".$copy_number] = array ("question_element_id" => "element_client_gender_transgender_male".$copy_number, "question_id" => "client_gender".$copy_number, "question_element_title" => "Transgender Male", "question_element_value" => "transgender_male");	

		$this->question_elements["client_gender".$copy_number]["transgender_female".$copy_number] = array ("question_element_id" => "element_client_gender_transgender_female".$copy_number, "question_id" => "client_gender".$copy_number, "question_element_title" => "Transgender Female", "question_element_value" => "transgender_female");	

		$this->question_elements["client_gender".$copy_number]["transgender".$copy_number] = array ("question_element_id" => "element_client_gender_transgender".$copy_number, "question_id" => "client_gender".$copy_number, "question_element_title" => "Transgender", "question_element_value" => "transgender");	

        $this->question_elements["client_gender".$copy_number]["dontknow".$copy_number] = array ( 'question_element_id' => 'element_client_gender_dontknow'.$copy_number, 'question_id' => 'client_gender'.$copy_number, 'question_element_title' => "Don't know", 'question_element_value' => '#88#' );
        
        $this->question_elements["client_gender".$copy_number]["refused".$copy_number] = array ( 'question_element_id' => 'element_client_gender_refused'.$copy_number, 'question_id' => 'client_gender'.$copy_number, 'question_element_title' => 'Refused', 'question_element_value' => '#99#' );

		$this->questions["birth_month".$copy_number] = array ("question_id" => "birth_month".$copy_number, "question_title" => "Birth Month:",
        "question_type" => "number", "question_field_size" => "2", "question_range_bottom" => "1", "question_range_top" => "12",
        "question_display_order" => $srt."103", "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes", 'question_no_answer_options' => '5' );

        $this->questions["birth_day".$copy_number] = array ("question_id" => "birth_day".$copy_number, "question_title" => "Birth Day:",
        "question_type" => "number", "question_field_size" => "2", "question_range_bottom" => "1", "question_range_top" => "31",
        "question_display_order" => $srt."103", "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes", 'question_no_answer_options' => '5' );

		$this->questions["client_year_born".$copy_number] = array ("question_id" => "client_year_born".$copy_number, "question_title" =>
        "Birth Year:", "question_type" => "number", "question_field_size" => "4", "question_range_bottom" => "1915", "question_range_top"
        => safe_date("Y", ($unix_date + 31622400)), "question_display_order" => $srt."103", "question_required" => "yes", "question_default_value" => "", "question_visible" => "yes", 'question_no_answer_options' => '5' );
        
        $this->questions['ss_number'.$copy_number] = array ("question_id" => 'ss_number'.$copy_number, 'question_title' => 'Social Security #:', 'question_type' => 'text',  'question_field_size' => '11', 'question_display_size' => '11', "question_display_order" => $srt."103", 'question_required' => 'yes', 'question_default_value' => '', 'question_visible' => 'yes', 'question_no_answer_options' => '13' );
        
	    $this->questions["org_client_status".$copy_number] = array ("question_id" => "org_client_status".$copy_number, 'question_title' => 'Status:', "question_type" => "dropdown", "question_owner_group_id" => "1", "question_owner_org_id" => "1", "question_field_size" => "0", "question_range_bottom" => "0", "question_range_top" => "0", "question_display_order" => $srt."103", "question_required" => "no", "report_type_id" => "client_identifier_entry", "question_default_value" => "no_status_set", "question_visible" => "yes", 'question_no_answer_options' => '0' );

		$this->question_elements["org_client_status".$copy_number]["1"] = array ("question_element_id" => "element_org_client_status".$copy_number, "question_id" => "org_client_status".$copy_number, "question_element_title" => "No Status Set", "question_element_value" => "1");  //Special Cased, overwritten to be [SELECT STATUS] on search form in display_dropdown_list_question()...

		$this->question_elements["org_client_status".$copy_number]["2"] = array ("question_element_id" => "element_org_client_status".$copy_number, "question_id" => "org_client_status".$copy_number, "question_element_title" => "Active", "question_element_value" => "2");

		$this->question_elements["org_client_status".$copy_number]["3"] = array ("question_element_id" => "element_org_client_status".$copy_number, "question_id" => "org_client_status".$copy_number, "question_element_title" => "Inactive/Dormant", "question_element_value" => "3");

		$this->question_elements["org_client_status".$copy_number]["4"] = array ("question_element_id" => "element_org_client_status".$copy_number, "question_id" => "org_client_status".$copy_number, "question_element_title" => "Exited", "question_element_value" => "4");

		$this->question_elements["org_client_status".$copy_number]["5"] = array ("question_element_id" => "element_org_client_status".$copy_number, "question_id" => "org_client_status".$copy_number, "question_element_title" => "Followup Required", "question_element_value" => "5");

		$this->question_elements["org_client_status".$copy_number]["6"] = array ("question_element_id" => "element_org_client_status".$copy_number, "question_id" => "org_client_status".$copy_number, "question_element_title" => "Followup Required - 3 month", "question_element_value" => "6");

		$this->question_elements["org_client_status".$copy_number]["7"] = array ("question_element_id" => "element_org_client_status".$copy_number, "question_id" => "org_client_status".$copy_number, "question_element_title" => "Followup Required - 6 month", "question_element_value" => "7");

		$this->question_elements["org_client_status".$copy_number]["8"] = array ("question_element_id" => "element_org_client_status".$copy_number, "question_id" => "org_client_status".$copy_number, "question_element_title" => "Followup Required - 9 month", "question_element_value" => "8");

		$this->question_elements["org_client_status".$copy_number]["9"] = array ("question_element_id" => "element_org_client_status".$copy_number, "question_id" => "org_client_status".$copy_number, "question_element_title" => "Followup Required - 12 month", "question_element_value" => "9");

		$this->question_elements["org_client_status".$copy_number]["10"] = array ("question_element_id" => "element_org_client_status".$copy_number, "question_id" => "org_client_status".$copy_number, "question_element_title" => "Deceased", "question_element_value" => "10");

 	    $this->questions["client_rpt_sharing_permission".$copy_number] = array ("question_id" => "client_rpt_sharing_permission".$copy_number,
        "question_title" => "Client Info Shared?:", "question_type" => "radio", "question_field_size" => "", "question_range_bottom" => "",
        "question_range_top" => "", "question_display_order" => $srt."155",
        "question_required" => "no", "question_default_value" => "yes".$copy_number, "question_visible" => "yes", "question_ecma" => "");

		$this->question_elements["client_rpt_sharing_permission".$copy_number]["yes".$copy_number] = array ("question_element_id" => "element_client_rpt_sharing_permission_yes".$copy_number, "question_id" => "client_rpt_sharing_permission".$copy_number, "question_element_title" => "Yes", "question_element_value" => "yes");
		
		$this->question_elements["client_rpt_sharing_permission".$copy_number]["no".$copy_number] = array ("question_element_id" => "element_client_rpt_sharing_permission_no".$copy_number, "question_id" => "client_rpt_sharing_permission".$copy_number, "question_element_title" => "No", "question_element_value" => "no");

        $this->questions["extra_client_id_forms"] = array ("question_id" => "extra_client_id_forms", "question_title" => "If you need
        additional entry blanks, enter the total number of household members here:", "question_type" => "number",
        "question_field_size" => "2", "question_range_bottom" => "0", "question_range_top" => "20", "question_display_order" => "19005", "question_required" => "no", "question_default_value" => "", "question_visible" => "yes", "question_ecma" => "");



		if($copy_number != 0)	{

			$this->questions["client_relationship_to_lead".$copy_number] = array ("question_id" =>
            "client_relationship_to_lead".$copy_number, "question_title" => "Relationship to HOH:", "question_type" => "radio",
            "question_field_size" => "", "question_range_bottom" => "", "question_range_top" => "", "question_display_order" => $srt."149", "question_required" => "yes", "question_default_value" => "child".$copy_number, "question_visible" => "yes", "question_ecma" => "");

				$this->question_elements["client_relationship_to_lead".$copy_number]["child".$copy_number] = array ("question_element_id" => "child".$copy_number, "question_id" => "client_relationship_to_lead".$copy_number, "question_element_title" => "Child", "question_element_value" => "child");

				$this->question_elements["client_relationship_to_lead".$copy_number]["spouse".$copy_number] = array ("question_element_id" => "element_relationship_spouse".$copy_number, "question_id" => "client_relationship_to_lead".$copy_number, "question_element_title" => "Spouse", "question_element_value" => "spouse");
				$this->question_elements["client_relationship_to_lead".$copy_number]["partner".$copy_number] = array ("question_element_id" => "element_relationship_spouse".$copy_number, "question_id" => "client_relationship_to_lead".$copy_number, "question_element_title" => "Partner", "question_element_value" => "partner");
				
				$this->question_elements["client_relationship_to_lead".$copy_number]["friend".$copy_number] = array ("question_element_id" => "element_relationship_friend".$copy_number, "question_id" => "client_relationship_to_lead".$copy_number, "question_element_title" => "Friend", "question_element_value" => "friend");

				$this->question_elements["client_relationship_to_lead".$copy_number]["sibling".$copy_number] = array ("question_element_id" => "element_relationship_sibling".$copy_number, "question_id" => "client_relationship_to_lead".$copy_number, "question_element_title" => "Sibling", "question_element_value" => "sibling");
				$this->question_elements["client_relationship_to_lead".$copy_number]["non_immediate_family".$copy_number] = array ("question_element_id" => "element_relationship_non_immediate_family".$copy_number, "question_id" => "client_relationship_to_lead".$copy_number, "question_element_title" => "Family, Not Immediate", "question_element_value" => "family, not immediate");
				
				
        }
	
		$this->questions["org_generated_client_id".$copy_number] = array ("question_id" => "org_generated_client_id".$copy_number,
               "question_title" => $org_name." Client Identifier:", "question_type" => "text",  "question_field_size" => "40",
               "question_display_order" => $srt."150", "question_required" => "no", "question_default_value" => "", "question_visible" => "yes", "question_display_size" => "20", "question_ecma" => "");

        //For the client lookup form, turn off the requirement for answers and default values
        if ( $questions_for_client_lookup )    {
            $this->questions['name_first'.$copy_number]['question_required'] = 'no';
            $this->questions['name_middle'.$copy_number]['question_required'] = 'no';
            $this->questions['name_last'.$copy_number]['question_required'] = 'no';
            $this->questions['client_gender'.$copy_number]['question_required'] = 'no';
            $this->questions['client_gender'.$copy_number]['question_default_value'] = '';
            $this->questions['birth_month'.$copy_number]['question_required'] = 'no';
            $this->questions['birth_day'.$copy_number]['question_required'] = 'no';
            $this->questions['client_year_born'.$copy_number]['question_required'] = 'no';
            $this->questions['ss_number'.$copy_number]['question_required'] = 'no';
            $this->questions['org_client_status'.$copy_number]['question_default_value'] = 'all';
        }


	}


/* no longer called
function pull_client_report_sharing_question ()
	{
        $this->sharing_permission_question = array ("question_id" => "client_rpt_sharing_permission", "question_title" => "Client Info Shared?:", "question_type" => "radio", "question_field_size" => "", "question_range_bottom" => "", "question_range_top" => "", "question_display_order" => "103", "question_required" => "no", "question_default_value" => "no", "question_visible" => "yes", "question_ecma" => "");
        
		$this->sharing_permission_question_elements["yes"] = array ("question_element_id" => "element_client_rpt_sharing_permission_yes", "question_id" => "client_rpt_sharing_permission", "question_element_title" => "Yes", "question_element_value" => "yes");
		
		$this->sharing_permission_question_elements["no"] = array ("question_element_id" => "element_client_rpt_sharing_permission_no", "question_id" => "client_rpt_sharing_permission", "question_element_title" => "No", "question_element_value" => "no");
    }
*/

    function validate_and_convert_ss_num_for_db ( $q_id, $num_from_form )  {
        
        $ret_val = str_replace ( '-', '', $num_from_form );
        $ret_val = str_replace ( ' ', '', $ret_val );
        $ret_val = str_replace ( '.', '', $ret_val );
        
        secondary_form_answer_validation ( 'Social Security number', $q_id, $num_from_form, 9, '0-9', 9 );
        echo "ret val is".$num_from_form.'<p>';
        return $ret_val;
    }
    
    
    
    function convert_ss_num_for_display ( $num_without_punctuation )  {
    
    
    }
    
    
    
    function insert_edited_client_id ( $client_id, $vetted_values, $org_id )
	{
        $this->set_client_info_session_variables ( $vetted_values );
        $this->client_id_assign ( $org_id, $client_id );
    }


}



?>
Return current item: Homeless Mangement Information System