// Loads the existing data from the DB and populates the form
// http://crosstec.de/forums/15-usage/11377-using-php-variables-to-populate-the-value-prop.html

// this line is required by breezingforms
$this->execPieceByName('ff_InitLib');

// Grab the form ID from the URL
$id = JRequest::getVar('id');

//Now to get the sales reps record using that  ID
$sql = "SELECT * FROM joomla_sales_reps WHERE id = " .$id . " LIMIT 1";
$rep = ff_select($sql);

// Make sure that a user came back. If a user hits update prior to entering their data - bad things happen
if($rep) {
	
	
	// check and make sure that the user had permissisons to access this entry. This means that they either must be the author, or they must be a super user
	$user =& JFactory::getUser();
	if( ($rep[0]->user_id == $user->id) || ($user->groups['Super Users']) ) {
	
		// In order for the form to have access to the DB values, we have to declare them as GLOBAL to allow them to jump from the DB object to the form object
		//Visible
		$GLOBALS['company'] = $rep[0]->company;
		$GLOBALS['first'] = $rep[0]->first;
		$GLOBALS['last'] = $rep[0]->last;
		$GLOBALS['email'] = $rep[0]->email;
		$GLOBALS['phone1'] = format_phone($rep[0]->phone1);
		$GLOBALS['phone2'] = format_phone($rep[0]->phone2);
		$GLOBALS['fax'] = format_phone($rep[0]->fax);
		$GLOBALS['serving'] = $rep[0]->serving;
		$GLOBALS['website'] = $rep[0]->website;
		$GLOBALS['courselevel'] = $rep[0]->courselevel;
		$GLOBALS['contentarea'] = $rep[0]->contentarea;
		$GLOBALS['territory'] = $rep[0]->territory;
		$GLOBALS['user_id'] = $rep[0]->user_id;
        $GLOBALS['dir_id'] = $id;

		
		//Set field values (update)
		
		//Visible
		ff_setValue('txtCompany', $GLOBALS['company']);
		ff_setValue('txtFirstName', $GLOBALS['first']);
		ff_setValue('txtLastName', $GLOBALS['last']);
		ff_setValue('txtEmail', $GLOBALS['email']);
		ff_setValue('txtPhone1', $GLOBALS['phone1']);
		ff_setValue('txtPhone2', $GLOBALS['phone2']);
		ff_setValue('txtFax', $GLOBALS['fax']);
		ff_setValue('txtServing', $GLOBALS['serving']);
		ff_setValue('txtWebsite', $GLOBALS['website']);
		ff_setValue('hdnUserID', $GLOBALS['user_id']);
		ff_setValue('hdnDirID', $GLOBALS['dir_id']);
		
		//Build out Territories
		$territories = explode(", ",$GLOBALS['territory']);
		foreach($territories as $state) {
                        // WA was broken into East WA and West WA. This selects both if WA is still in the DB
                        if($state == 'WA') {
                           ff_setSelected('selTerritory', 'East WA');
                           ff_setSelected('selTerritory', 'West WA');
                        }
                        else {
			   ff_setSelected('selTerritory', $state);
                        }
		}
		
		// Build out Content Area
		$areas = explode(", ", $GLOBALS['contentarea']);
		foreach($areas as $area) {
				ff_setSelected('selContentArea', $area);
		}
		
		// Build Out Course Level
		$levels = explode(", ", $GLOBALS['courselevel']);
		foreach($levels as $level) {
				ff_setSelected('selCourseLevel', $level);
		}
	} // end if user permission
} // close if user found statement

// Function to format phone values
function format_phone($phone)
{
	$temp = preg_replace("/[^0-9]/", "", $phone);
 
	if(strlen($temp) == 7)
		return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
	elseif(strlen($temp) == 10)
		return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
	else
		return $phone;
}