<?php
date_default_timezone_set('America/New_York');
include("include/session.php");
include("utilities/UtilitiesClients.php");
include("utilities/UtilitiesReporting.php");
include("utilities/UtilitiesEncryption.php");
include("utilities/UtilitiesBackup.php");
class Process
{
   /* Class constructor */
   function Process(){
      global $session;
      /* User submitted login form */
      if(isset($_POST['sublogin'])){
         $this->procLogin();
      }
      /* User submitted registration form */
      else if(isset($_POST['subjoin'])){
         $this->procRegister();
      }
      /* User submitted forgot password form */
      else if(isset($_POST['subforgot'])){
         $this->procForgotPass();
      }
      /* User submitted edit account form */
      else if(isset($_POST['subedit'])){
         $this->procEditAccount();
      }
      else if(isset($_POST['subConfirm'])){
      	$this->procSendConfirm();
      }
      else if(isset($_POST['login_with_hash'])){
      	$this->procHashLogin($_POST['hash']);
      }
		//////////////////////////////
		//Added 2/12/2012
	 else if(isset($_POST['editClientRecord'])){
         $this->procEditClient();
      }
	 else if(isset($_POST['newClientRecord'])){
         $this->procAddClient();
      }
	 else if(isset($_POST['procManagersMessage'])){
         $this->procManagersMessage();
      }
	 else if(isset($_POST['deleteClient'])){
         $this->procDeleteClient();
      }
	  //////////////////////////////
		//Added 2/18/2012
		else if(isset($_POST['updateNote'])){
         $this->procUpdateNote();
      }
	 else if(isset($_POST['procDeleteUser'])){
         $this->procDeleteUser();
      }
	 else if(isset($_POST['procUpdateUserLevel'])){
         $this->procUpdateUserLevel();
      }
		else if(isset($_POST['procGenerateBackup'])){
			$this->procGenerateBackup();
		}

      else if($session->logged_in){
         $this->procLogout();
      }
	  
	  
	  
       else{
          header("Location: main.php");
       }
   }

   /**
    * procLogin - Processes the user submitted login form, if errors
    * are found, the user is redirected to correct the information,
    * if not, the user is effectively logged in to the system.
    */
   function procLogin(){
      global $session, $form;
      /* Login attempt */
      $_POST = $session->cleanInput($_POST);
      $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));
      
      /* Login successful */
      if($retval){
         header("Location: ".$session->referrer);
      }
      /* Login failed */
      else{
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
   }
   
   /**
    * procLogout - Simply attempts to log the user out of the system
    * given that there is no logout form to process.
    */
   function procLogout(){
      global $session;
      $retval = $session->logout();
      header("Location: main.php");
   }
   
   /**
    * procRegister - Processes the user submitted registration form,
    * if errors are found, the user is redirected to correct the
    * information, if not, the user is effectively registered with
    * the system and an email is (optionally) sent to the newly
    * created user.
    */
   function procRegister(){
      global $session, $form;
      $_POST = $session->cleanInput($_POST);
      /* Convert username to all lowercase (by option) */
      if(ALL_LOWERCASE){
         $_POST['user'] = strtolower($_POST['user']);
      }
      /* Registration attempt */
      $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'], $_POST['name']);
      
      /* Registration Successful */
      if($retval == 0){
         $_SESSION['reguname'] = $_POST['user'];
         $_SESSION['regsuccess'] = true;
         header("Location: index.php#dashboard.php");
      }
      /* Error found with form */
      else if($retval == 1){
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: index.php#admin.php");
      }
      /* Registration attempt failed */
      else if($retval == 2){
         $_SESSION['reguname'] = $_POST['user'];
         $_SESSION['regsuccess'] = false;
         header("Location: index.php#admin.php");
      }
   }
   
   /**
    * procForgotPass - Validates the given username then if
    * everything is fine, a new password is generated and
    * emailed to the address the user gave on sign up.
    */
   function procForgotPass(){
      global $database, $session, $mailer, $form;
      $_POST = $session->cleanInput($_POST);
      /* Username error checking */
      $subuser = $_POST['user'];
      $field = "user";  //Use field name for username
      if(!$subuser || strlen($subuser = trim($subuser)) == 0){
         $form->setError($field, "* Username not entered<br>");
      }
      else{
         /* Make sure username is in database */
         $subuser = stripslashes($subuser);
         if(strlen($subuser) < 5 || strlen($subuser) > 30 ||
            !ctype_alnum($subuser) ||
            (!$database->usernameTaken($subuser))){
            $form->setError($field, "* Username does not exist<br>");
         }
      }
      
      /* Errors exist, have user correct them */
      if($form->num_errors > 0){
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
      }
      /* Generate new password and email it to user */
      else{
         /* Generate new password */
         $newpass = $session->generateRandStr(8);
         
         /* Get email of user */
         $usrinf = $database->getUserInfo($subuser);
         $email  = $usrinf['email'];
         
         /* Attempt to send the email with new password */
         if($mailer->sendNewPass($subuser,$email,$newpass)){
            /* Email sent, update database */
            $database->updateUserField($subuser, "password", md5($newpass));
            $_SESSION['forgotpass'] = true;
         }
         /* Email failure, do not change password */
         else{
            $_SESSION['forgotpass'] = false;
         }
      }
      
      header("Location: ".$session->referrer);
   }
   
   /**
    * procEditAccount - Attempts to edit the user's account
    * information, including the password, which must be verified
    * before a change is made.
    */
   function procEditAccount(){
      global $session, $form;
      $_POST = $session->cleanInput($_POST);
      /* Account edit attempt */
      $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email'], $_POST['name']);

      /* Account edit successful */
      if($retval){
         $_SESSION['useredit'] = true;
         header("Location: index.php#dashboard.php");
      }
      /* Error found with form */
      else{
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
   }
   
   /**
   	* procSendConfirm - only needs to be used if the administrator
   	* changes the EMAIL_WELCOME from false to true and wants
   	* the users to confirm themselves. (why not?!)
   	*/
   function procSendConfirm(){
       global $session, $form, $database, $mailer;
       $_POST = $session->cleanInput($_POST);
       
       $user	=	$_POST['user'];
       $pass	=	$_POST['pass'];
       
      /* Checks that username is in database and password is correct */
      $user = stripslashes($user);
      $result = $database->confirmUserPass($user, md5($pass));

      /* Check error codes */
      if($result == 1){
         $field = "user";
         $form->setError($field, "* Username not found");
      }
      elseif($result == 2){
         $field = "pass";
         $form->setError($field, "* Invalid password");
      }
      
      /* Check to see if the user is already valid */
      $q = "SELECT valid FROM ".TBL_USERS." WHERE username='$user'";
      $valid = $database->query($q);
      $valid = mysql_fetch_array($valid);
      $valid = $valid['valid'];
      
      if($valid == 1){
         $field = 'user';
         $form->setError($field, "* Username already confirmed.");
      }
      
      /* Return if form errors exist */
      if($form->num_errors > 0){
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
      else{
	      $q = "SELECT username, userid, email FROM ".TBL_USERS." WHERE username='$user'";
	      $info = $database->query($q) or die(mysql_error());
	      $info = mysql_fetch_array($info);
	      
		      $username = $info['username'];
		      $userid = $info['userid'];
		      $email = $info['email'];
	      
	      if($mailer->sendConfirmation($username,$userid,$email)){
	      	  echo "Your confirmation email has been sent! Back to <a href='main.php'>Main</a>";
	      }
	  }
   }
   
   function procHashLogin($hash){
   		global $session, $database;
   		if(substr($hash,0,1) === "#"){
   			$hash = substr($hash,1);
   		}
   		
   		$user_info = $database->getUserInfoFromHash($hash);
		
   		if($user_info['hash_generated'] < (time() - (60*60*24*3))){
   			// if the hash was generated more than 3 days ago, the hash is invalid.
   			// let's invalidate and refuse the hash.
   			$database->updateUserField($user_info['username'], 'hash', $session->generateRandID());
	        $database->updateUserField($user_info['username'], 'hash_generated', time());
	        return false;
   		}
   		
   		if($user_info['username'] && $user_info['userid']){  
   			$_SESSION['username'] = $user_info['username'];
	   		$_SESSION['userid'] = $user_info['userid'];
	   		$session->checkLogin();
	   		die("Logging In...");
	   	} else {
	   		die();
	   	}
   }
   

	function procAddClient(){
		global $session, $database, $clients, $reporting, $encryption;
		$name 					= mysql_real_escape_string($_POST["name"]);
		$canAssist 				= mysql_real_escape_string($_POST["canAssist"]);
		
		$ssn 					= mysql_real_escape_string($_POST["ssn"]);
		$ssn 					= $encryption->encrypt($ssn);
		$aliasMaidenName 		= mysql_real_escape_string($_POST["aliasMaidenName"]);
		$SpouseOtherName 		= mysql_real_escape_string($_POST["SpouseOtherName"]);
		$Phone 					= mysql_real_escape_string($_POST["Phone"]);
		$WorkPhone 				= mysql_real_escape_string($_POST["WorkPhone"]);
		$Email 					= mysql_real_escape_string($_POST["Email"]);
		$DLNumber 				= mysql_real_escape_string($_POST["DLNumber"]);
		$PhoneComments 			= mysql_real_escape_string($_POST["PhoneComments"]);
		$Employer 				= mysql_real_escape_string($_POST["Employer"]);
		$Ethnicity 				= mysql_real_escape_string($_POST["Ethnicity"]);
		$Birthday 				= mysql_real_escape_string($_POST["Birthday"]);
		$PrimaryAddress 		= mysql_real_escape_string($_POST["PrimaryAddress"]);
		$OtherAddr1 			= mysql_real_escape_string($_POST["OtherAddr1"]);
		$OtherAddr2 			= mysql_real_escape_string($_POST["OtherAddr2"]);
		$OtherAddr3 			= mysql_real_escape_string($_POST["OtherAddr3"]);
		$OtherAddr4 			= mysql_real_escape_string($_POST["OtherAddr4"]);
		$City 					= mysql_real_escape_string($_POST["City"]);
		$State 					= mysql_real_escape_string($_POST["State"]);
		$Zip 					= mysql_real_escape_string($_POST["Zip"]);
		$Children 				= mysql_real_escape_string($_POST["Children"]);
		$TotalDollarAsisst 		= mysql_real_escape_string($_POST["TotalDollarAsisst"]);
		$LastResults 			= mysql_real_escape_string($_POST["LastResults"]);
		$FoodStamps 			= mysql_real_escape_string($_POST["FoodStamps"]);
		$DatesFoodGiven 		= mysql_real_escape_string($_POST["DatesFoodGiven"]);
		$NumberTimesFoodGiven 	= mysql_real_escape_string($_POST["NumberTimesFoodGiven"]);
		$ChildcareScholarship 	= mysql_real_escape_string($_POST["ChildcareScholarship"]);
		$MKScholarship 			= mysql_real_escape_string($_POST["MKScholarship"]);
		$OtherDorcasPrograms 	= mysql_real_escape_string($_POST["OtherDorcasPrograms"]);
		$EducationPrograms 		= mysql_real_escape_string($_POST["EducationPrograms"]);
		$ExternalPrograms 		= mysql_real_escape_string($_POST["ExternalPrograms"]);
		//NOTES AND HISTORY
		$notesAndHistory 		= mysql_real_escape_string($_POST["notesAndHistory"]);
		$TakeHomePay 			= mysql_real_escape_string($_POST["TakeHomePay"]);      
		$SpoouseOtherPay 		= mysql_real_escape_string($_POST["SpoouseOtherPay"]);    
		$AFDC 					= mysql_real_escape_string($_POST["AFDC"]);
		$ChildSupport 			= mysql_real_escape_string($_POST["ChildSupport"]);    
		$SocialSecurityIncome 	= mysql_real_escape_string($_POST["SocialSecurityIncome"]);    
		$WIC 					= mysql_real_escape_string($_POST["WIC"]);
		$SSIDIS 				= mysql_real_escape_string($_POST["SSIDIS"]);    
		$WorkFirst 				= mysql_real_escape_string($_POST["WorkFirst"]);    
		$FoodStampsInc 			= mysql_real_escape_string($_POST["FoodStampsInc"]);     
		$TotalIncome 			= mysql_real_escape_string($_POST["TotalIncome"]);  
		$RentMortgage 			= mysql_real_escape_string($_POST["RentMortgage"]); 
		$RMType 				= mysql_real_escape_string($_POST["RMType"]);    
		$Water 					= mysql_real_escape_string($_POST["Water"]);    
		$CableTV 				= mysql_real_escape_string($_POST["CableTV"]);    
		$DayCare 				= mysql_real_escape_string($_POST["DayCare"]);    
		$Electric 				= mysql_real_escape_string($_POST["Electric"]);    
		$PowerCompAcctNum  		= mysql_real_escape_string($_POST["PowerCompAcctNum"]);   
		$CostOfHeat 			= mysql_real_escape_string($_POST["CostOfHeat"]);    
		$TypeOfHeat 			= mysql_real_escape_string($_POST["TypeOfHeat"]);    
		$OtherFixedExpenses 	= mysql_real_escape_string($_POST["OtherFixedExpenses"]); 
		$PhoneBill 				= mysql_real_escape_string($_POST["PhoneBill"]);     
		$TotalFixedExpenses 	= mysql_real_escape_string($_POST["TotalFixedExpenses"]);     
		$Food 					= mysql_real_escape_string($_POST["Food"]);     
		$MedicalInsurance 		= mysql_real_escape_string($_POST["MedicalInsurance"]);     
		$NumberOfCars 			= mysql_real_escape_string($_POST["NumberOfCars"]);   
		$MakeModel1 			= mysql_real_escape_string($_POST["MakeModel1"]);     
		$CarPayment1 			= mysql_real_escape_string($_POST["CarPayment1"]);     
		$MakeModel2 			= mysql_real_escape_string($_POST["MakeModel2"]);    
		$CarPayment2 			= mysql_real_escape_string($_POST["CarPayment2"]); 
		$CarInsurancePayments 	= mysql_real_escape_string($_POST["CarInsurancePayments"]); 
		$TotalOtherExpenses 	= mysql_real_escape_string($_POST["TotalOtherExpenses"]); 
		$username 				= mysql_real_escape_string($_POST["username"]); 
		$ReferredTo 			= mysql_real_escape_string($_POST["ReferredTo"]); 
		$CreditCardPayments 	= mysql_real_escape_string($_POST["CreditCardPayments"]); 
		$Deficit 				= mysql_real_escape_string($_POST["Deficit"]); 
		$ChecksIssued 			= mysql_real_escape_string($_POST["ChecksIssued"]); 
		$ChildSupportExpense 		= mysql_real_escape_string($_POST["ChildSupportExpense"]); 
		$NaturalGasExpense 			= mysql_real_escape_string($_POST["NaturalGasExpense"]); 
		$CellPhoneExpense 			= mysql_real_escape_string($_POST["CellPhoneExpense"]); 
		$OtherMedicalExpense 		= mysql_real_escape_string($_POST["OtherMedicalExpense"]); 
		$GasExpense 				= mysql_real_escape_string($_POST["GasExpense"]); 
		$OtherTransportationExpense = mysql_real_escape_string($_POST["OtherTransportationExpense"]);
		$Gender 					= mysql_real_escape_string($_POST["Gender"]);
		
		$query="
			INSERT INTO ".TBLCLIENT."	
			(
				client_name, 			status, 				social, 	
				alias_MaidenName, 		spouse_OtherName, 		phone,
				work_phone, 			email_address, 			drivers_licenseNumber, 
				phone_comments, 		employer,				ethnicity, 				
				birthdate, 				primary_address, 		other_address, 
				other_address2, 		other_address3, 		other_address4,
				city, 					state, 					zip, 		
				children_info, 			total_assist, 			last_results, 
				food_stamps,			dates_foodGiven, 		times_foodGiven,
				childcareScholarship, 	mkScholarship, 			otherDorcasPrograms,
				educationCredits, 		otherExternalPrograms, 	notes_history, 
				takehome_pay, 			spouse_otherPay, 		afdc,
				child_support, 			social_securityReceived, wic, 
				ssi_dis, 				work_first, 			food_stampsIncome,	
				total_income,			rent_mortgage, 			rm_Type, 				
				water, 					cable_tv, 				daycare, 
				electric, 				cpl_account, 			cost_heat, 
				type_heat,				other_fixedExpenses, 	phone_bill, 			
				total_fixedExpenses, 	food, 					medical_insurance, 
				number_cars,			makemodel_car1, 		car_payment1, 			
				makemodel_car2, 		car_payment2, 			car_insurance, 
				total_otherExpenses,	username, 				date, 
				isactive, 				ReferredTo,				CreditCardPayments,
				Deficit,				ChecksIssued,			ChildSupportExpense,
				NaturalGasExpense,		CellPhoneExpense,		OtherMedicalExpense,	
				GasExpense,				Gender, 				OtherTransportationExpense
			)
			VALUES
			(
				'$name',					'$canAssist',			'$ssn',
				'$aliasMaidenName', 		'$SpouseOtherName', 		'$Phone',
				'$WorkPhone', 				'$Email',  					'$DLNumber',
				'$PhoneComments', 			'$Employer', 				'$Ethnicity',
				'$Birthday', 				'$PrimaryAddress', 			'$OtherAddr1',
				'$OtherAddr2', 				'$OtherAddr3', 				'$OtherAddr4',
				'$City', 					'$State', 					'$Zip',
				'$Children', 				'$TotalDollarAsisst', 		'$LastResults',
				'$FoodStamps', 				'$DatesFoodGiven', 			'$NumberTimesFoodGiven',
				'$ChildcareScholarship', 	'$MKScholarship', 			'$OtherDorcasPrograms',
				'$EducationPrograms', 		'$ExternalPrograms',		'$notesAndHistory',
				'$TakeHomePay',				'$SpoouseOtherPay', 		'$AFDC',
				'$ChildSupport', 			'$SocialSecurityIncome', 	'$WIC',
				'$SSIDIS', 					'$WorkFirst', 				'$FoodStamps', 			
				'$TotalIncome', 			'$RentMortgage', 			'$RMType', 				
				'$Water', 					'$CableTV', 				'$DayCare',
				'$Electric', 				'$PowerCompAcctNum', 		'$CostOfHeat',
				'$TypeOfHeat', 				'$OtherFixedExpenses', 		'$PhoneBill',
				'$TotalFixedExpenses', 		'$Food', 					'$MedicalInsurance',
				'$NumberOfCars', 			'$MakeModel1', 				'$CarPayment1',
				'$MakeModel2', 				'$CarPayment2', 			'$CarInsurancePayments',
				'$TotalOtherExpenses',		'$username',				NOW(),
				'1',						'$ReferredTo',				'$CreditCardPayments',
				'$Deficit',					'$ChecksIssued',			'$ChildSupportExpense',
				'$NaturalGasExpense',		'$CellPhoneExpense',		'$Gender',
				'$OtherMedicalExpense',		'$GasExpense',				'$OtherTransportationExpense'
				
			)
		";
		$result = $database->query($query);
		if(!$result){
			if(DEBUG == true)
			{
				printf("Error in query: %s", $query);
			}
		}
		//2012-01-19
		$now = date("Y-m-d");  	
		$insertedClientID = $clients->getCurrentClientID($username);

		$reporting->setActivityRecord($insertedClientID, CLIENTVISIT, $now, TYPEINFO, $username);
		$clients->setClientNotes($insertedClientID, $notesAndHistory, $now, "info", $username);
		
		
		
		$_SESSION['clientAddedSuccess'] = true;
		header("Location: index.php#dashboard.php");
	}//END procAddClient
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	function procEditClient(){
		global $session, $database, $reporting, $clients, $encryption;
		
		$clientID				= mysql_real_escape_string($_POST["clientID"]);
		$name 					= mysql_real_escape_string($_POST["name"]);
		$clientStatus 			= mysql_real_escape_string($_POST["canAssist"]);

		$ssn 					= mysql_real_escape_string($_POST["ssn"]);
		$ssn 					= $encryption->encrypt($ssn);
		$aliasMaidenName 		= mysql_real_escape_string($_POST["aliasMaidenName"]);
		$SpouseOtherName 		= mysql_real_escape_string($_POST["SpouseOtherName"]);
		$Phone 					= mysql_real_escape_string($_POST["Phone"]);
		$WorkPhone 				= mysql_real_escape_string($_POST["WorkPhone"]);
		$Email 					= mysql_real_escape_string($_POST["Email"]);
		$DLNumber 				= mysql_real_escape_string($_POST["DLNumber"]);
		$PhoneComments 			= mysql_real_escape_string($_POST["PhoneComments"]);
		$Employer 				= mysql_real_escape_string($_POST["Employer"]);
		$Ethnicity 				= mysql_real_escape_string($_POST["Ethnicity"]);
		$Birthday 				= mysql_real_escape_string($_POST["Birthday"]);
		$PrimaryAddress 		= mysql_real_escape_string($_POST["PrimaryAddress"]);
		$OtherAddr1 			= mysql_real_escape_string($_POST["OtherAddr1"]);
		$OtherAddr2 			= mysql_real_escape_string($_POST["OtherAddr2"]);
		$OtherAddr3 			= mysql_real_escape_string($_POST["OtherAddr3"]);
		$OtherAddr4 			= mysql_real_escape_string($_POST["OtherAddr4"]);
		$City 					= mysql_real_escape_string($_POST["City"]);
		$State 					= mysql_real_escape_string($_POST["State"]);
		$Zip 					= mysql_real_escape_string($_POST["Zip"]);
		$Children 				= mysql_real_escape_string($_POST["Children"]);
		$TotalDollarAsisst 		= mysql_real_escape_string($_POST["TotalDollarAsisst"]);
		$LastResults 			= mysql_real_escape_string($_POST["LastResults"]);
		$FoodStamps 			= mysql_real_escape_string($_POST["FoodStamps"]);
		$DatesFoodGiven 		= mysql_real_escape_string($_POST["DatesFoodGiven"]);
		$NumberTimesFoodGiven 	= mysql_real_escape_string($_POST["NumberTimesFoodGiven"]);
		$ChildcareScholarship 	= mysql_real_escape_string($_POST["ChildcareScholarship"]);
		$MKScholarship 			= mysql_real_escape_string($_POST["MKScholarship"]);
		$OtherDorcasPrograms 	= mysql_real_escape_string($_POST["OtherDorcasPrograms"]);
		$EducationPrograms 		= mysql_real_escape_string($_POST["EducationPrograms"]);
		$ExternalPrograms 		= mysql_real_escape_string($_POST["ExternalPrograms"]);
		//NOTES AND HISTORY
		$notesAndHistory 		= mysql_real_escape_string($_POST["notesAndHistory"]);
		$TakeHomePay 			= mysql_real_escape_string($_POST["TakeHomePay"]);      
		$SpoouseOtherPay 		= mysql_real_escape_string($_POST["SpoouseOtherPay"]);    
		$AFDC 					= mysql_real_escape_string($_POST["AFDC"]);
		$ChildSupport 			= mysql_real_escape_string($_POST["ChildSupport"]);    
		$SocialSecurityIncome 	= mysql_real_escape_string($_POST["SocialSecurityIncome"]);    
		$WIC 					= mysql_real_escape_string($_POST["WIC"]);
		$SSIDIS 				= mysql_real_escape_string($_POST["SSIDIS"]);    
		$WorkFirst 				= mysql_real_escape_string($_POST["WorkFirst"]);    
		$FoodStampsInc 			= mysql_real_escape_string($_POST["FoodStampsInc"]);     
		$TotalIncome 			= mysql_real_escape_string($_POST["TotalIncome"]);  
		$RentMortgage 			= mysql_real_escape_string($_POST["RentMortgage"]); 
		$RMType 				= mysql_real_escape_string($_POST["RMType"]);    
		$Water 					= mysql_real_escape_string($_POST["Water"]);    
		$CableTV 				= mysql_real_escape_string($_POST["CableTV"]);    
		$DayCare 				= mysql_real_escape_string($_POST["DayCare"]);    
		$Electric 				= mysql_real_escape_string($_POST["Electric"]);    
		$PowerCompAcctNum  		= mysql_real_escape_string($_POST["PowerCompAcctNum"]);   
		$CostOfHeat 			= mysql_real_escape_string($_POST["CostOfHeat"]);    
		$TypeOfHeat 			= mysql_real_escape_string($_POST["TypeOfHeat"]);    
		$OtherFixedExpenses 	= mysql_real_escape_string($_POST["OtherFixedExpenses"]); 
		$PhoneBill 				= mysql_real_escape_string($_POST["PhoneBill"]);     
		$TotalFixedExpenses 	= mysql_real_escape_string($_POST["TotalFixedExpenses"]);     
		$Food 					= mysql_real_escape_string($_POST["Food"]);     
		$MedicalInsurance 		= mysql_real_escape_string($_POST["MedicalInsurance"]);     
		$NumberOfCars 			= mysql_real_escape_string($_POST["NumberOfCars"]);   
		$MakeModel1 			= mysql_real_escape_string($_POST["MakeModel1"]);     
		$CarPayment1 			= mysql_real_escape_string($_POST["CarPayment1"]);     
		$MakeModel2 			= mysql_real_escape_string($_POST["MakeModel2"]);    
		$CarPayment2 			= mysql_real_escape_string($_POST["CarPayment2"]); 
		$CarInsurancePayments 	= mysql_real_escape_string($_POST["CarInsurancePayments"]); 
		$TotalOtherExpenses 	= mysql_real_escape_string($_POST["TotalOtherExpenses"]); 
		$username 				= mysql_real_escape_string($_POST["username"]); 
		$ReferredTo 				= mysql_real_escape_string($_POST["ReferredTo"]); 
		$CreditCardPayments 		= mysql_real_escape_string($_POST["CreditCardPayments"]); 
		$Deficit 					= mysql_real_escape_string($_POST["Deficit"]); 
		$ChecksIssued 				= mysql_real_escape_string($_POST["ChecksIssued"]); 
		$ChildSupportExpense 		= mysql_real_escape_string($_POST["ChildSupportExpense"]); 
		$NaturalGasExpense 			= mysql_real_escape_string($_POST["NaturalGasExpense"]); 
		$CellPhoneExpense 			= mysql_real_escape_string($_POST["CellPhoneExpense"]); 
		$OtherMedicalExpense 		= mysql_real_escape_string($_POST["OtherMedicalExpense"]); 
		$GasExpense 				= mysql_real_escape_string($_POST["GasExpense"]); 
		$OtherTransportationExpense = mysql_real_escape_string($_POST["OtherTransportationExpense"]); 
		$Gender 					= mysql_real_escape_string($_POST["Gender"]); 
		
		$query="
			UPDATE ".TBLCLIENT."
				SET	client_name 				= '$name'
					,status 					= '$clientStatus'
					,social 					= '$ssn'
					,alias_MaidenName 			= '$aliasMaidenName'
					,spouse_OtherName 			= '$SpouseOtherName'
					,phone 						= '$Phone'
					,work_phone 				= '$WorkPhone'
					,email_address 				= '$Email'
					,drivers_licenseNumber 		= '$DLNumber'
					,phone_comments 			= '$PhoneComments'
					,employer 					= '$Employer'
					,ethnicity 					= '$Ethnicity'
					,birthdate 					= '$Birthday'
					,primary_address 			= '$PrimaryAddress'
					,other_address 				= '$OtherAddr1'
					,other_address2 			= '$OtherAddr2'
					,other_address3 			= '$OtherAddr3'
					,other_address4 			= '$OtherAddr4'
					,city 						= '$City'
					,state 						= '$State'
					,zip 						= '$Zip'
					,children_info 				= '$Children'
					,total_assist 				= '$TotalDollarAsisst'
					,last_results 				= '$LastResults'
					,food_stamps 				= '$FoodStamps'
					,dates_foodGiven 			= '$DatesFoodGiven'
					,times_foodGiven 			= '$NumberTimesFoodGiven'
					,childcareScholarship 		= '$ChildcareScholarship'
					,mkScholarship 				= '$MKScholarship'
					,otherDorcasPrograms 		= '$OtherDorcasPrograms'
					,educationCredits 			= '$EducationPrograms'
					,otherExternalPrograms 		= '$ExternalPrograms'
					,notes_history 				= '$notesAndHistory'
					,takehome_pay 				= '$TakeHomePay'
					,spouse_otherPay 			= '$SpoouseOtherPay'    
					,afdc 						= '$AFDC'
					,child_support 				= '$ChildSupport' 
					,social_securityReceived 	= '$SocialSecurityIncome'   
					,wic 						= '$WIC'
					,ssi_dis 					= '$SSIDIS'
					,work_first 				= '$WorkFirst'
					,food_stampsIncome 			= '$FoodStampsInc'
					,total_income 				= '$TotalIncome' 
					,rent_mortgage 				= '$RentMortgage'
					,rm_Type 					= '$RMType'
					,water 						= '$Water'
					,cable_tv 					= '$CableTV'
					,daycare 					= '$DayCare'
					,electric 					= '$Electric'
					,cpl_account 				= '$PowerCompAcctNum'
					,cost_heat 					= '$CostOfHeat' 
					,type_heat 					= '$TypeOfHeat'
					,other_fixedExpenses 		= '$OtherFixedExpenses'
					,phone_bill 				= '$PhoneBill'
					,total_fixedExpenses 		= '$TotalFixedExpenses'
					,food 						= '$Food'
					,medical_insurance 			= '$MedicalInsurance'   
					,number_cars 				= '$NumberOfCars'
					,makemodel_car1 			= '$MakeModel1'     
					,car_payment1 				= '$CarPayment1'   
					,makemodel_car2 			= '$MakeModel2'    
					,car_payment2 				= '$CarPayment2' 
					,car_insurance  			= '$CarInsurancePayments'
					,total_otherExpenses 		= '$TotalOtherExpenses'
					,username 					= '$username'
					,isactive					= '1'
					,ReferredTo 				= '$ReferredTo'
					,CreditCardPayments 		= '$CreditCardPayments'
					,Deficit					= '$Deficit'
					,ChecksIssued				= '$ChecksIssued'
					,ChildSupportExpense 		= '$ChildSupportExpense'
					,NaturalGasExpense 			= '$NaturalGasExpense'
					,CellPhoneExpense 			= '$CellPhoneExpense'
					,OtherMedicalExpense 		= '$OtherMedicalExpense'
					,GasExpense 				= '$GasExpense'
					,OtherTransportationExpense = '$OtherTransportationExpense'
					,Gender 					= '$Gender'
			WHERE userid = '$clientID'
		";
		
		$result = $database->query($query);
		if(!$result){
			if(DEBUG == true)
			{
				printf("Error in query: %s", $query);
			}
		}
		$now = date("Y-m-d");
		$reporting->setActivityRecord($clientID, CLIENTINFORMATIONUPDATE, $now, TYPEWARNING, $username);
		
		if($notesAndHistory != "")
		{

			$clients->setClientNotes($clientID, $notesAndHistory, $now, "info", $username);
		}
		
		$_SESSION['clientUpdatedSuccess'] = true;
		header("Location: index.php#dashboard.php");
	}//END procEditClient
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
	function procManagersMessage(){
		global $session, $database, $reporting, $clients, $encryption;
		
		$managersMessage = mysql_real_escape_string($_POST["ManagersMessage"]);
		$username = mysql_real_escape_string($_POST["username"]);
		
		$query = "
			INSERT INTO ".TBLMANAGERSMESSAGE."
			(
				Message, IsActive
			)
			VALUES
			(
				'$managersMessage', '1'
			)
		";
		$result = $database->query($query);
		
		if(!$result){
			if(DEBUG == true)
			{
				printf("Error in query: %s", $query);
			}
		}
		
		$_SESSION['messagePosted'] = true;
		header("Location: index.php#dashboard.php");
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
	function procDeleteClient(){
		global $session, $database, $reporting, $clients, $encryption;
		
		$clientID = mysql_real_escape_string($_POST["clientID"]);
		
		
		$query = "
			UPDATE ".TBLCLIENT."
				SET isactive = '0'
			WHERE userid = '$clientID'
		";
		$result = $database->query($query);
		
		if(!$result){
			if(DEBUG == true)
			{
				printf("Error in query: %s", $query);
			}
		}
		
		$_SESSION['clientDeleted'] = true;
		header("Location: index.php#dashboard.php");
	
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
	function procUpdateNote(){
		global $session, $database, $reporting, $clients, $encryption;
		
		$noteID = mysql_real_escape_string($_POST["noteID"]);
		$clientID = mysql_real_escape_string($_POST["clientID"]);
		$username = mysql_real_escape_string($_POST["username"]);
		$Note = mysql_real_escape_string($_POST["Note"]);
		
		
		$query = "
			UPDATE ".TBLCLIENTNOTES."
				SET Notes = '$Note'
			WHERE 
				clientid = '$clientID'
			AND
				indexid = '$noteID'
			AND
				username = '$username'
		";
		
		$result = $database->query($query);
		
		if(!$result){
			if(DEBUG == true)
			{
				printf("Error in query: %s", $query);
			}
		}
		
		$now = date("Y-m-d");
		$reporting->setActivityRecord($clientID, CLIENTNOTESUPDATE, $now, TYPEINFO, $username);
		
		$_SESSION['clientDeleted'] = true;
		header("Location: index.php#clientForm.php?clientID=$clientID");
	
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
	function procDeleteUser(){
		global $session, $database, $reporting, $clients, $encryption;
		$delUsername = mysql_real_escape_string($_POST["delUsername"]);
		
		$query = "
			DELETE FROM ".TBL_USERS." WHERE username = '$delUsername'
		";
		
		$result = $database->query($query);
		
		if(!$result){
			if(DEBUG == true)
			{
				printf("Error in query: %s", $query);
			}
		}
		
		$_SESSION['userDeleted'] = true;
		header("Location: index.php#admin.php");
		
		
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function procUpdateUserLevel(){
		global $session, $database, $reporting, $clients, $encryption;
		$cUserLevel = mysql_real_escape_string($_POST["cUserLevel"]);
		$cUsername = mysql_real_escape_string($_POST["cUsername"]);
		
		$query = "
			UPDATE ".TBL_USERS."
				SET userlevel = '$cUserLevel'
			WHERE username = '$cUsername'
		";
		
		$result = $database->query($query);
		
		if(!$result){
			if(DEBUG == true)
			{
				printf("Error in query: %s", $query);
			}
		}
		
		$_SESSION['userLevelSuccess'] = true;
		header("Location: index.php#admin.php");
		
		
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function procGenerateBackup()
	{
		$dateStamp = date("mdYgis");
		$output = "views/backups/EmerasBackup_$dateStamp.sql";
		//---- The name of the file which you want to save backup file in it.
		//---- If this file does not exist it will be created.(over writed).
		//---- You need to use physical path to this file. e.g. ../data/backup.txt
	
		//---- If this is true, only tables' structure will be stored.
		//---- If this is false, tables' structure and all their data(everything) will be stored.
		$structure_only = false;


		//---- instantiating object.
		$backup = new mysql_backup(DB_SERVER,DB_NAME,DB_USER,DB_PASS,$output,$structure_only);    

		//---- calling the backup method finally creats a file with the name specified in $output
		//     and stors everythig so you can copy the file anywhere you want. This file will be
		//     restored with another method of this class named "restore" that is described in
		//     example-backup.php
		$backup->backup();
	
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

};

/* Initialize process */
$process = new Process;

?>
