//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
$result = getAlertData();
$row = sqlFetchArray($result);
do {
    //while ($row = sqlFetchArray($result)) {
    $e_Alert = $ccr->createElement('Alert');
    $e_Alerts->appendChild($e_Alert);
    $e_CCRDataObjectID = $ccr->createElement('CCRDataObjectID', getUuid());
    $e_Alert->appendChild($e_CCRDataObjectID);
    $e_DateTime = $ccr->createElement('DateTime');
    $e_Alert->appendChild($e_DateTime);
    $date = date_create($row['date']);
    $e_ExactDateTime = $ccr->createElement('ExactDateTime', $date->format('Y-m-d\\TH:i:s\\Z'));
    $e_DateTime->appendChild($e_ExactDateTime);
    $e_IDs = $ccr->createElement('IDs');
    $e_Alert->appendChild($e_IDs);
    $e_ID = $ccr->createElement('ID', $row['pid']);
function doAlert($user_id, $lat, $lng, $alertType, $alertId)
{
    global $dbconn;
    $user = getAlertData($user_id, $alertId);
    $errors = array();
    foreach ($user['phones'] as $phone) {
        try {
            mBlox::sendSMS($phone, $user['name'], $user['phone'], $user['lat'], $user['lng'], $alertId);
        } catch (mBoxException $e) {
            $error = "Unknown error";
            switch ($e->getCode()) {
                case mBoxException::EX_INVALID_LOCATION:
                    $error = 'Location invalid';
                    break;
                case mBoxException::EX_INVALID_UKNUMBER:
                    $error = 'Invalid UK number';
                    break;
            }
            $errors[] = $error;
        }
    }
    $query = "UPDATE alerts SET date_complete = NOW(), alert_raised = 'Y' WHERE id = {$alertId}";
    $dbconn->query($query);
    if (count($errors) > 0) {
        $out = array('message' => implode(', ', $errors));
        error($out);
    }
}