<?php
/*
(c)ASTA Technology Group LLC 2003-2005.
*/



class index_page extends template
{
  var $home;
   function index_page($template = false)
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->index_page = true;
      $this->template($template);
      $this->title = $this->company_name;
      $this->title2 = "Welcome to " . $this->company_name;
      $this->home =$home1;
      //$this->go302(format_login_url());
   }//func
}
/**ULTIMATE STORE LOCATOR **/

class xmlEncoding{

}
class ultimate_dbconnect {
function locator_database_connect($query_fordb, $action=1) {
    $host = "localhost";//Ip of database, in this case my host machine
    $user = "root";    //Username to use
    $pass = "";//Password for that user
    $dbname = "ultimatestorelocatortest";//Name of the database
    try {
        $connection = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        if($action == 1) {
            $query = $connection->query($query_fordb)->fetchAll(PDO::FETCH_ASSOC);
            //print_r($query);
        }elseif($action == 2){
            $statement = $connection->prepare($query_fordb);
            if ($statement->execute()) {
                $query=true;
            }else{
                $query=false;
            }

        }
    } catch (PDOException $e) {
        //echo $e->getMessage();
        $query = $e->getMessage();
    }
    return $query;
}
};

class ultimate_login_form extends template
{


    function ultimate_login_form($template = "template_ultimate_login.html", $is_expired = false)
    {
        global $link, $xml_server_url, $affiliate_id, $languages;
        $this->skip_expires_check = true;

        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Welcome to " . $this->company_name;
        $this->index_page = true;

        $this->is_expired = $is_expired;

        foreach($languages as $language_ind => $language) {
            $this->language_index = $language_ind;
            $this->language_name = $language['name'];
            $this->language_selected = $language_ind==$this->language;
            $this->language_select .= $this->pattern("language");
        }
    }
}
class ultimate_login_page extends ultimate_login_form{

    function ultimate_login_page($template = "template_ultimate_login.html", $username = false, $password = false){
        $this->ultimate_login_form($template);

        if($username && $password){//LOG IN!
            //$id = urlencode(stripslashes($username));
            // $pin = urlencode(stripslashes($password));
            $query_fordb = "SELECT id, name, username, count(id) as countid FROM ul_users where UPPER(username)=".'"'.strtoupper($username).'"'.' '.'AND password='.'"'.sha1($password).'"';
            $dbconnect = new ultimate_dbconnect();
            $query = $dbconnect->locator_database_connect($query_fordb);

            if($query[0]['countid']>0){
                    $_SESSION["user"] = trim($query[0]['name']);
                    $_SESSION["username"] = trim($query[0]['username']);

                    if($_SESSION['snapshot_url'])
                    {
                        $url = $_SESSION['snapshot_url'];
                        unset($_SESSION['snapshot_url']);
                        $this->go302(format_ultimatestorelocator_url($url));
                    }
                    else
                    {
                        $this->go302(format_ultimatestorelocator_url());
                    }
                }else{
                    $this->error .= 'Invalid username or password';
                }



        }
    }
}

class ultimate_dashboard extends template
{
    function ultimate_dashboard($template = "template_dashboard.html")
    {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!

        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}

class ultimate_dashboard_dayweekyear extends template
{
    function ultimate_dashboard_dayweekyear($template = "template_dashboard_dayweekyear.html")
    {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!

        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}


class ultimate_dashboard_price extends template
{
    function ultimate_dashboard_price($template = "template_dashboard_price.html")
    {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!

        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}

class ultimate_dashboard_druggroups extends template
{
    function ultimate_dashboard_druggroups($template = "template_dashboard_druggroups.html")
    {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!

        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}

class ultimate_dashboard_doctor extends template
{
    function ultimate_dashboard_doctor($template = "template_dashboard_doctor.html")
    {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!

        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}

class ultimate_dashboard_method extends template
{
    function ultimate_dashboard_method($template = "template_dashboard_method.html")
    {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!

        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}

class ultimate_dashboard_thirdparty extends template
{
    function ultimate_dashboard_thirdparty($template = "template_dashboard_thirdparty.html")
    {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!

        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}

class ultimate_dashboard_patients extends template
{
    function ultimate_dashboard_patients($template = "template_dashboard_patients.html")
    {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!

        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}

class ultimate_dashboard_referAFriend extends template
{
    function ultimate_dashboard_referAFriend($template = "template_dashboard_referAFriend.html")
    {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!

        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}


/*class ultimatestorelocator_viewstores extends template {

    function ultimatestorelocator_viewstores($template = "template_ultimate_viewstores.html", $paginationnumber=1){

        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!
            $rowPerPage = 10;
            $start=($paginationnumber-1) * $rowPerPage;

            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://rest.method.me/api/v1/tables/Contacts/count",
                // table is Contacts, method record id is 544
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => array(
                    "Authorization: APIKey NjI5OGNkMmM2MjU3OGY0NWQ1MzFiYjQzLjcwQzU0RjVDNTNDNjQzMkU5NTkwQ0EzQTU3MUZBMThG"
                ),
            ));

            $Query_count = curl_exec($curl);
            // $DecodedResponse = json_decode($response);

            if($Query_count>= 0) {

                $curl = curl_init();
                curl_setopt_array($curl, array(
                    CURLOPT_URL => "https://rest.method.me/api/v1/tables/Contacts?"."skip=".$start,
                    // table is Contacts, method record id is 544
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 0,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_HTTPHEADER => array(
                        "Authorization: APIKey NjI5OGNkMmM2MjU3OGY0NWQ1MzFiYjQzLjcwQzU0RjVDNTNDNjQzMkU5NTkwQ0EzQTU3MUZBMThG"
                    ),
                ));

                $response = curl_exec($curl);
                $DecodedResponse = json_decode($response);

                curl_close($curl);

                foreach ($DecodedResponse->value as $store) {
                    $array = json_decode(json_encode($store), true);
                    //print_r($array);

                    $this->populate_from_node($array, "store");

                    $this->stores .= $this->pattern("store");

                }
            } else{
                $this->go404("[Stores is empty]");

            }

            $this->paginationnumber = $paginationnumber;
            $this->howmanypages = ceil($Query_count/10);
            $this->countofstores = $Query_count;

        }else{
            $this->go302(format_ultimateloginpage_url());

        }
    }
}

class ultimatestorelocator_searchstores extends template {

    /*function ultimatestorelocator_searchstores($template = "template_ultimate_searchstores.html", $paginationnumber=1, $searchquery, $searchby='name'){

        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!
            $query_fordb_forcount = "SELECT count(id) as countofstores  FROM location where ". $searchby . " like " . "'" . "%" . $searchquery . "%" . "'";
            $dbconnect = new ultimate_dbconnect();
            $queryforcount = $dbconnect->locator_database_connect($query_fordb_forcount, 1);

            if($queryforcount[0]['countofstores'] >= 0) {

                $rowPerPage = 25;
                $start = ($paginationnumber - 1) * $rowPerPage;
                $query_fordb = "SELECT * FROM location where " . $searchby . " like " . "'" . "%" . $searchquery . "%" . "'" . " ORDER BY name limit " . $start . "," . $rowPerPage;
                $query = $dbconnect->locator_database_connect($query_fordb);
                foreach ($query as $store) {
                    $this->populate_from_node($store, "store");

                    $this->stores .= $this->pattern("store");

                }
            }else{
                $this->go404("[Stores is empty]");

            }

            $this->paginationnumber = $paginationnumber;
            $this->searchquery = $searchquery;
            $this->searchby = $searchby;
            $this->howmanypages = ceil($queryforcount[0]['countofstores']/25);
            $this->countofstores = $queryforcount[0]['countofstores'];


        }else{
            $this->go302(format_ultimateloginpage_url());

        }
    }
}



    function ultimatestorelocator_searchstores($template = "template_ultimate_searchstores.html", $paginationnumber=1, $searchquery, $searchby='name', $orderby='Entity'){
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Stores";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!
            $rowPerPage = 10;
            $start = ($paginationnumber - 1) * $rowPerPage;

            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://rest.method.me/api/v1/tables/Contacts/count?filter=contains(".$searchby.","."'".$searchquery."'".")",
                // table is Contacts, method record id is 544
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => array(
                    "Authorization: APIKey NjI5OGNkMmM2MjU3OGY0NWQ1MzFiYjQzLjcwQzU0RjVDNTNDNjQzMkU5NTkwQ0EzQTU3MUZBMThG"
                ),
            ));

            $Query_count = curl_exec($curl);
           // $DecodedResponse = json_decode($response);
            curl_close($curl);

            if($Query_count>= 0) {

                if($orderby == 'LastModifiedDate'){
                    $ascOrDesc='desc';
                }else{
                    $ascOrDesc ='asc';
                }

            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://rest.method.me/api/v1/tables/Contacts?"."skip=".$start."&filter=contains(".$searchby.","."'".$searchquery."'".")"."&orderby=".$orderby."%20".$ascOrDesc,
                // table is Contacts, method record id is 544
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => array(
                    "Authorization: APIKey NjI5OGNkMmM2MjU3OGY0NWQ1MzFiYjQzLjcwQzU0RjVDNTNDNjQzMkU5NTkwQ0EzQTU3MUZBMThG"
                ),
            ));

            $response = curl_exec($curl);
            $DecodedResponse = json_decode($response);
                curl_close($curl);


                foreach ($DecodedResponse->value as $store) {
                    $array = json_decode(json_encode($store), true);
                    //print_r($array);

                    $this->populate_from_node($array, "store");

                    $this->stores .= $this->pattern("store");

                }
            } else{
            $this->go404("[Stores is empty]");

        }

            $this->paginationnumber = $paginationnumber;
            $this->searchquery = $searchquery;
            $this->searchby = $searchby;
            $this->orderby = $orderby;
            $this->howmanypages = ceil($Query_count/10);
            $this->countofstores = $Query_count;

            /* FUTURE MILLIE: what do you have to do:
            - add skip url variable and do pagination
            - go to template_ultimate_searchstores.html and change fields - get fields from postmanSS
            - change search fields url dropdown
            - do the same for displaying all stores with pagination add an order by field (can be last modified, alphabetical)
            - download file with russ and me's code in method version of the IDE




        }else{
            $this->go302(format_ultimateloginpage_url());

        }
    }
}

class ultimatestorelocator_viewindividualstore extends template
{
    function ultimatestorelocator_viewindividualstore($template="template_ultimate_editstore.html", $storeid){
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "View Individual Store";

        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!
            $query_storeid = "SELECT * FROM location where id= "."'".$storeid."'";
            $dbconnect = new ultimate_dbconnect();
            $query_result_store = $dbconnect->locator_database_connect($query_storeid, 1);

            foreach ($query_result_store as $store) {
                $this->populate_from_node($store, "store");

                $this->stores .= $this->pattern("store");

            }
        }else{
            $this->go302(format_ultimateloginpage_url());

        }

    }

}

class ultimatestorelocator_deleteindividualstore extends template
{
    function ultimatestorelocator_deleteindividualstore($template="template_ultimate_deletestore", $storeid){
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Delete Individual Store";
        if(isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!
            $query_storeid = "DELETE FROM location where id= "."'".$storeid."'";
            $dbconnect = new ultimate_dbconnect();
            $query_result_store = $dbconnect->locator_database_connect($query_storeid, 2);

            if($query_result_store == true){
                $this->result = 'Row: '.$storeid.' '.'Successfully Deleted';
            }else{
                $this->result = 'Row: '.$storeid.' '.'NOT successfully deleted';

            }
        }else{
            $this->go302(format_ultimateloginpage_url());

        }
    }
}

class ultimatestorelocator_editindividualstore extends template
{
    function ultimatestorelocator_editindividualstore($template = "template_ultimate_editstoreprocess.html", $storeid, $storename, $storeaddress1, $storeaddress2, $city, $state, $country, $zip, $telephone, $fax, $email, $website, $user1, $user2, $user3, $user4, $user5, $pref, $hide, $hits, $aprove, $visitorname, $visitoremail, $visitordate, $user6, $user7, $user8, $user9, $user10, $is_online, $image, $icon, $url_text)
    {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Edit Individual Store";
        if (isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!
            $query_storeid = "UPDATE location SET name=" . "'" . $storename . "'" . ", address1=" . "'" . $storeaddress1 . "'" . ", address2=" . "'" . $storeaddress2 . "'" . ", city=" . "'" . $city . "'" . ", state=" . "'" . $state . "'" . ", country=" . "'" . $country . "'" . ", zip=" . "'" . $zip . "'" . ", telephone=" . "'" . $telephone . "'" . ", fax=" . "'" . $fax . "'" . ", email=" . "'" . $email . "'" . ", website=" . "'" . $website . "'" . ", user1=" . "'" . $user1 . "'" . ", user2=" . "'" . $user2 . "'" . ", user3=" . "'" . $user3 . "'" . ", user4=" . "'" . $user4 . "'" . ", user5=" . "'" . $user5 . "'" . ", pref=" . "'" . $pref . "'" . ", hide=" . "'" . $hide . "'" . ", hits=" . "'" . $hits . "'" . ", aprove=" . "'" . $aprove . "'" . ", visitorname=" . "'" . $visitorname . "'" . ", visitoremail=" . "'" . $visitoremail . "'" . ", visitordate=" . "'" . $visitordate . "'" . ", user6=" . "'" . $user6 . "'" . ", user7=" . "'" . $user7 . "'" . ", user8=" . "'" . $user8 . "'" . ", user9=" . "'" . $user9 . "'" . ", user10=" . "'" . $user10 . "'" . ", is_online=" . "'" . $is_online . "'" . ", image=" . "'" . $image . "'" . ", icon=" . "'" . $icon . "'" . ", url_text=" . "'" . $url_text . "'" . " where id=" . $storeid;
            $dbconnect = new ultimate_dbconnect();
            $query_result_store = $dbconnect->locator_database_connect($query_storeid, 2);
            if ($query_result_store == true) {
                $this->result = 'Row: ' . $storeid . ' ' . 'Successfully Edited';
            } else {
                $this->result = 'Row: ' . $storeid . ' ' . 'NOT successfully Edited';

            }
        } else {
            $this->go302(format_ultimateloginpage_url());

        }
    }
}


class ultimatestorelocator_addindividualstore extends template
{
    function ultimatestorelocator_addindividualstore($template = "template_ultimate_newstore.html")   {
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Add Store";
        if (isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!

        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}


class ultimatestorelocator_addindividualstoreprocess extends template
{
    function ultimatestorelocator_addindividualstoreprocess($template = "template_ultimate_newstoreprocess.html", $storename, $storeaddress1, $storeaddress2, $city, $state, $country, $zip, $telephone, $fax, $email, $website, $user1, $user2, $user3, $user4, $user5, $pref, $hide, $hits, $aprove, $visitorname, $visitoremail, $visitordate, $user6, $user7, $user8, $user9, $user10, $is_online, $image, $icon, $url_text){
        $this->template($template);
        $this->title = $this->company_name;
        $this->title2 = "Add Store";
        if (isset($_SESSION["user"]) && isset($_SESSION["username"])) {//LOG IN!
            $query_storeid = "INSERT INTO `location`(`name`, `address1`, `address2`, `city`, `state`, `country`, `zip`, `telephone`, `fax`, `email`, `website`, `user1`, `user2`, `user3`, `user4`, `user5`, `pref`, `hide`, `hits`, `aprove`, `visitorname`, `visitoremail`, `visitordate`, `user6`, `user7`, `user8`, `user9`, `user10`, `is_online`, `image`, `icon`, `url_text`) VALUES (" . "'" . $storename . "'".","."'" . $storeaddress1 . "'" . "," . "'" . $storeaddress2 . "'" . "," . "'" . $city . "'" . "," . "'" . $state . "'" . "," . "'" . $country . "'" . "," . "'" . $zip . "'" . "," . "'" . $telephone . "'" . "," . "'" . $fax . "'" . "," . "'" . $email . "'" . "," . "'" . $website . "'" . "," . "'" . $user1 . "'" . "," . "'" . $user2 . "'" . "," . "'" . $user3 . "'" . "," . "'" . $user4 . "'" . "," . "'" . $user5 . "'" . "," . "'" . $pref . "'" . "," . "'" . $hide . "'" . "," . "'" . $hits . "'" . "," . "'" . $aprove . "'" . "," . "'" . $visitorname . "'" . "," . "'" . $visitoremail . "'" . "," . "'" . $visitordate . "'" . "," . "'" . $user6 . "'" . "," . "'" . $user7 . "'" . "," . "'" . $user8 . "'" . "," . "'" . $user9 . "'" . "," . "'" . $user10 . "'" . "," . "'" . $is_online . "'" . "," . "'" . $image . "'" . "," . "'" . $icon . "'" . "," . "'" . $url_text . "'".");";
            $dbconnect = new ultimate_dbconnect();
            $query_result_store = $dbconnect->locator_database_connect($query_storeid, 2);
            if ($query_result_store == true) {
                $this->result = 'Pharmacy: ' . $storename . ' ' . 'Successfully Added';
            } else {
                $this->result = 'Pharmacy: ' . $storename . ' ' . 'NOT successfully Added';

            }
        }else{
            $this->go302(format_ultimateloginpage_url());
        }
    }
}*/


class login_form extends template
{

	function login_form($template = "template_login.html", $is_expired = false)
   {
      global $link, $xml_server_url, $affiliate_id, $languages;
	  $this->skip_expires_check = true;

      $this->template($template);
      $this->title = $this->company_name;
      $this->title2 = "Welcome to " . $this->company_name;
      $this->index_page = true;

	  $this->is_expired = $is_expired;
	  
	  foreach($languages as $language_ind => $language) {
		$this->language_index = $language_ind;
		$this->language_name = $language['name'];
		$this->language_selected = $language_ind==$this->language;
		$this->language_select .= $this->pattern("language");
	  }
	}
}

class login_page extends login_form
{

   function login_page($template = "template_login.html", $id = false, $pin = false)
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->login_form($template);
	  
      if($id && $pin)
      {//LOG IN!
         $id = urlencode(stripslashes($id));
         $pin = urlencode(stripslashes($pin));

         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;
         $results = new xml("Amjaygetlogin", $params, true);
         if(!isset($results->error) && is_array($results->login[0]->patients[0]->row) && ($login_data = array_pop($results->login[0]->patients[0]->row)))
            {
               $_SESSION["user_id"] = trim($login_data->patientkey);
               $_SESSION["pin"] = trim($login_data->pin);

               if($_SESSION['snapshot_url'])
               {
                  $url = $_SESSION['snapshot_url'];
                  unset($_SESSION['snapshot_url']);
                  $this->go302(format_link($url));
               }
               else
               {
                  $this->go302(format_pharmacies_url());
               }
         } else { //error?
		 	if (isset($results->error[0]->row[0]->row[0]->error)) {
	            $this->error .= $results->error[0]->row[0]->row[0]->error;
			} else {
				$this->error .= "Unknown error";
            }
         }
      }
   }//func
}

class pharmacies_page extends template
{
   var $stores = "";
   var $special_url = "";
   var $mobile_url = "";
   var $agreed = 0;
   var $agreestat=""; 
   var $changestat="";
   var $passwordchange = 0; 
   var $resetpin="";		
   function pharmacies_page($template = "template_pharmacies.html",$agbutton="DisAgree",$changebutton="",$npin="-1")
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->template($template);
      $this->title = $this->company_name;
      $this->title2 = "My Pharmacies";

      if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
      {//LOG IN!
         $id = urlencode(stripslashes($_SESSION["user_id"]));
         $pin = urlencode(stripslashes($_SESSION["pin"]));
         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;
         $params .= "&agree=" . $agbutton;
         $params .= "&pinchange=" . $changebutton;
	     $params .= "&newpin=" . $npin;	
         $results = new xml("Amjaygetlogin", $params, true);
         if(isset($results->error))
         {
            $this->error .= $results->error[0]->row[0]->row[0]->error;
         }
         elseif(is_array($results->login[0]->patients[0]->row))
         {

            $this->populate_from_node($results->login[0]->patients[0]->row[0], "patient");
            foreach($results->login[0]->stores[0]->row  as $store)
            {
               $this->populate_from_node($store, "store");
               $this->special_url = format_special_url($this->store_storeidentifier);
               $this->mobile_url = format_viewpharmacies_url($this->store_storeidentifier);
               $this->store_bannerimage = $this->images_http_prefix . $this->store_bannerimage;
               $this->store_name = htmlspecialchars($this->store_name);
			   $this->agreed = $this->patient_agreement;
			   $this->passwordchange = $this->patient_passwordchange;		
			   $this->resetpin=$this->patient_pin;
               $this->stores .= $this->pattern("store");
            }

         }
         else
         {
            $this->go404("[patients is empty]");
         }
      }
      else
      {
         $this->go302(format_login_url(format_pharmacies_url()));
      }
   }//func
}

class orderchoosestore_page extends template
{
   var $stores = "";
   var $special_url = "";
   var $mobile_url = "";
   var $agreed = 0;
   var $agreestat=""; 
   var $changestat="";
   var $passwordchange = 0; 
   var $resetpin="";		
   function orderchoosestore_page($template = "template_choosestore.html")
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->template($template);
      $this->title = $this->company_name;
      $this->title2 = "E-refillpage";
         $params .="";
         $results = new xml("Amjaygetstorelist", $params, true);
         if(isset($results->error))
         {
            $this->error .= $results->error[0]->row[0]->row[0]->error;
         }
         else
		 {
			 if(is_array($results->storelist[0]->stores[0]->row))
	         {
    	        foreach($results->storelist[0]->stores[0]->row  as $store)
        	    {
            	   $this->populate_from_node($store, "store");
	               $this->special_url = format_special_url($this->store_storeidentifier);
    	           $this->mobile_url = format_viewpharmacies_url($this->store_storeidentifier);
        	       $this->store_bannerimage = $this->images_http_prefix . $this->store_bannerimage;
            	   $this->store_name = htmlspecialchars($this->store_name);
	               $this->stores .= $this->pattern("store");
    	      	}
		  	 }	
	         else
    	     {
        	    $this->go404("[storelist is empty]");
	         }


         }
    }//func
}

class agreement_page extends template
{
   var $stores = "";
   var $special_url = "";
   var $mobile_url = "";
   var $agreed = 0;
   var $agree_url="";
   var $disagree_url="";
   function agreement_page($template = "template_agreement.html")
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->template($template);
      $this->title = $this->company_name;
      $this->title2 = "Agreement";

      if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
      {//LOG IN!
         $id = urlencode(stripslashes($_SESSION["user_id"]));
         $pin = urlencode(stripslashes($_SESSION["pin"]));

         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;
         $agree_url=format_pharmacies_url();
		 $disagree_url=format_logout_url();
	  }
	}

}



class changepin_page extends template
{
   var $stores = "";
   var $special_url = "";
   var $mobile_url = "";
   var $agreed = 0;
   var $agree_url="";
   var $disagree_url="";
   function changepin_page($template = "template_changepin.html")
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->template($template);
      $this->title = $this->company_name;
      $this->title2 = "Agreement";

      if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
      {//LOG IN!
         $id = urlencode(stripslashes($_SESSION["user_id"]));
         $pin = urlencode(stripslashes($_SESSION["pin"]));

         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;
         $agree_url=format_pharmacies_url();
		 $disagree_url=format_logout_url();
	  }
	}

}






class viewpharmacies_page extends template
{
   var $store = "";
   var $nhid="";
   var $profile = "";
   var $patient="";
   var $main = "";
   var $counsell_url = "";
   var $orderrefills_url = "";
   var $backtonhpatient_url = "";

   var $mobile_url = "";
   var $orderimage = "";
   var $rxcount = 0; 
   var $rxs_exist = false;
   var $msg_exists="statusMessageNull";			
   var $state_text = "";
   var $label1exists="labelNoborder";	
   var $label2exists="labelNoborder";
   var $label3exists="labelNoborder";
   var $tmpdpic="";
   var $tmpdirpic="";
function viewpharmacies_page($template = "template_viewpharmacies.html", $store = false, $rxid = false, $patid= false,$pnhid=false)
   {     //***009***
      global $link, $xml_server_url, $affiliate_id;

      $this->template($template);
      $this->title = $this->company_name;
      $this->title2 = " View Prescriptions";

      if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
      {//LOG IN!    //***008***
         $id = urlencode(stripslashes($_SESSION["user_id"]));
         $pin = urlencode(stripslashes($_SESSION["pin"]));
         $store = urlencode(stripslashes($store));
         $nhid=urlencode(stripslashes($pnhid));
         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;
         $params .= "&store=" . $store;
         $params .= "&patid=" . $patid;
		 $params .= "&nhid=" . $pnhid;				
         $results = new xml("Amjaymultigetprofile", $params);
         if(isset($results->error))
         {         //***007***
            $this->error .= $results->error[0]->row[0]->row[0]->error;
         }       //***007***
         elseif(is_array($results->patientprofile[0]->store[0]->row))
         {      //***007***
			
            foreach($results->patientprofile[0]->store  as $i => $store)
            {   //***006***
               $this->rxcount=0;   
			   $this->rxs_exist=false;
			   $this->populate_from_node($store->row[0], "store");
               $this->store_bannerimage = $this->images_http_prefix . $this->store_bannerimage;
        	 $this->orderrefills_url = format_orderpharmacies_url($this->store_storeidentifier,$patid,$pnhid);
           $this->backtonhpatient_url = "viewnhpatients.php?nhid=".$pnhid."&store=".$this->store_storeidentifier;

               $this->orderimage = $this->images_http_prefix . "rxstat/OrderNow.png";
              
			   $this->store = $this->pattern("store");
               $this->profile = "";
               if(is_array($results->patientprofile[0]->patient[$i]->row))
               {
               foreach($results->patientprofile[0]->patient[$i]->row as $patient)
               {
                 $this->populate_from_node($patient, "patient");
                 $this->patient = $this->pattern("patient");
    		       
               }
               }
                  //Ajay Change
                  if(is_array($results->patientprofile[0]->profile[$i]->row))
                  {//Ajay change //***003***
                     foreach($results->patientprofile[0]->profile[$i]->row  as $profile)
                     {  //***002***
					 $this->label1exists="labelNoborder";	
					 $this->label2exists="labelNoborder";
   					 $this->label3exists="labelNoborder";
	
            			$this->populate_from_node($profile, "profile");
						if($this->profile_rxnumber!="") 
						{
			              $this->rxcount++;	
						}
			//*******************************************************************************
                     //   $this->profile_drugpic = $this->images_http_prefix . $this->profile_drugpic;
                     //   $this->profile_drugpic = str_replace("\\","/",$this->profile_drugpic);
                     //*******************************************************************************
                      $tmpdpic=str_pad($this->profile_din , 8, "0", STR_PAD_LEFT); 
                      $tmpdirpic="/home/erefi104/public_html/centralprofile/images/drugpics/" . $tmpdpic . ".jpg";
                      $tmpdpic=$this->images_http_prefix . "drugpics/" . $tmpdpic . ".jpg";
                      
                      if (file_exists($tmpdirpic)) {
                      
                        $this->profile_drugpic = $tmpdpic;
                      } else {
                         $tmpdpic=$this->images_http_prefix . "drugpics/noimage.jpg";
                         $this->profile_drugpic =$tmpdpic;
                      }                      



                        $this->state_text = str_replace(".png","",$this->profile_stateimage);
			$this->state_text = str_replace("rxstat/","",$this->state_text);
			$this->state_text = ucwords($this->state_text);
			
			$this->profile_stateimage = $this->images_http_prefix . $this->profile_stateimage;
                        $this->profile_stateimage = str_replace("\\","/", $this->profile_stateimage);
                        
                        $this->profile_labelnum1 = $this->images_http_prefix . $this->profile_labelnum1;
                        $this->profile_labelnum1 = str_replace("\\","/", $this->profile_labelnum1);
                        if (strripos($this->profile_labelnum1,"noimage")==false) 
						{
						   $this->label1exists="label1"	;
						}
                        $this->profile_labelnum2 = $this->images_http_prefix . $this->profile_labelnum2;
                        $this->profile_labelnum2 = str_replace("\\","/", $this->profile_labelnum2);
                         if (strripos($this->profile_labelnum2,"noimage")==false) 
						{
						   $this->label2exists="label1"	;
						}
                        $this->profile_labelnum3 = $this->images_http_prefix . $this->profile_labelnum3;
                        $this->profile_labelnum3 = str_replace("\\","/", $this->profile_labelnum3);
                         if (strripos($this->profile_labelnum3,"noimage")==false) 
						{
						   $this->label3exists="label1"	;
						}
                        $this->counsell_url = format_counselling_url($this->profile_din, $this->profile_storeidentifier);
                        
						$this->mobile_url = format_viewpharmacies_url($this->store_storeidentifier, $this->profile_rxid);
                        $this->profile_drugstrength = htmlspecialchars($this->profile_drugstrength);
                        $this->profile_drugname = htmlspecialchars($this->profile_drugname);
                        $this->rxs_exist = false;	
						 if ($this->rxcount>0)
						 {
			  			  $this->rxs_exist = true;	 
                   
    				     }

						if($this->profile_rxid == $rxid)
                        {    //***001***
                           $this->mobile_counsell_url = format_counselling_url($this->profile_din, $this->profile_storeidentifier, "main");
                           $this->mobile_order_url = format_order_url($this->store_storeidentifier, $rxid);
                           $this->mobile_drugpic_url = format_drugpic_url($this->store_storeidentifier, $rxid);
                           $this->profile_instructions = htmlspecialchars($this->profile_instructions);
                           $this->profile_doctor = htmlspecialchars($this->profile_doctor);
                           //$this->profile .= $this->pattern("detail");
                           break;
                        }  //***001***
                        else
                        {  //***001***
                           $this->profile .= $this->pattern("profile");
                        }  //***001***
                
					} //***002*** for each

                  }    //***003*** if
               $this->profiles .= $this->pattern("main");
            }//***006***
       } //***007***
        else
       { //***007***
         $this->go404("[store is empty]");
       } //***007***
   }    //***008***
   else
   {    //***008***
      $this->go302(format_login_url(format_viewpharmacies_url($store)));
   }    //***008***
}//func //***009***
} //class

class drugpic_page extends template
{
var $profile = "";

function drugpic_page($template = "template_drugpic.html", $store = false, $rxid = false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = " Drug Picture ";

   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));
      $store = urlencode(stripslashes($store));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;
      $params .= "&store=" . $store;

      $results = new xml("Amjaymultigetprofile", $params);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->patientprofile[0]->store[0]->row))
      {

         foreach($results->patientprofile[0]->store  as $i => $store)
         {
            $this->populate_from_node($store->row[0], "store");
            $this->profile = "";
            foreach($results->patientprofile[0]->profile[$i]->row  as $profile)
            {
               $this->populate_from_node($profile, "profile");
               if($this->profile_rxid == $rxid)
               {
                  $this->profile_drugpic = $this->images_http_prefix . $this->profile_drugpic;
                  $this->profile_drugname = htmlspecialchars($this->profile_drugname);
                  break;
               }
               else
               {
                  $this->profile .= $this->pattern("profile");
               }
            }
            $this->main .= $this->pattern("main");
         }
      }
      else
      {
         $this->go404("[store is empty]");
      }
   }
   else
   {
      $this->go302(format_login_url(format_viewpharmacies_url($store)));
   }
}//func
}


class counselling_page extends template
{
var $counsell = "";
var $video_url = "";

function counselling_page($template = "template_counselling.html", $store = false, $din = false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;

   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));
      $store = urlencode(stripslashes($store));
      $din = urlencode(stripslashes($din));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;
      $params .= "&store=" . $store;
      $params .= "&din=" . $din;

      $results = new xml("amjaygetcounsel", $params);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->couns[0]->counselling[0]->row))
      {
         foreach($results->couns[0]->counselling[0]->row  as $counselling)
         {
            $this->populate_from_node($counselling, "couns");
            $this->video_url = format_video_url($this->couns_din, $this->couns_store, $this->couns_category);
            $this->couns_categoryname = htmlspecialchars($this->couns_categoryname);
            $this->couns_counseltext = htmlspecialchars($this->couns_counseltext);
            $this->counsell .= $this->pattern("counselling");
         }
         $this->populate_from_node($results->couns[0]->counselling[0]->row[0], "couns");
      }
      else
      {
         $this->go404("[counselling id empty]");
      }
   }
   else
   {
      $this->go302(format_login_url(format_counselling_url($din, $store)));
   }
}//func
}


class video_page extends template
{
var $counsell = "";
var $video = "";
var $first_video_url = "";
var $video_url = "";

function video_page($template = "template_video.html", $store = false, $din = false, $category = false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;

   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));
      $store = urlencode(stripslashes($store));
      $din = urlencode(stripslashes($din));
      $category = urlencode(stripslashes($category));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;
      $params .= "&store=" . $store;
      $params .= "&din=" . $din;
      $params .= "&category=" . $category;

      $results = new xml("amjaygetvideocounsel", $params);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->couns[0]->vidcounselling[0]->row))
      {
         foreach($results->couns[0]->vidcounselling[0]->row  as $vidcounsell)
         {
            $this->populate_from_node($vidcounsell, "v");
            $this->video .= $this->pattern("vidtext");
         }
      }
      else
      {
         $this->error = "[vidcounselling id empty]";
      }
   }
   else
   {
      $this->go302(format_login_url(format_counselling_url($din, $store)));
   }
}//func
}

/*
class counselling_page extends template {
var $counsell = "";
var $video = "";
var $first_video_url = "";
var $video_url = "";
var $main_url = "";
var $logo_url = "templates/logo.html";

function counselling_page($template = "template_counselling_main.html", $store=false, $din=false, $category=false, $frame=false) {
global $link, $xml_server_url,$affiliate_id;

if ($frame == 'main') {
$template="template_counselling.html";
} elseif ($frame == 'video') {
$template="template_counselling_video.html";
}
$this->template($template);
$this->title = $this->company_name;
$this->main_url = format_counselling_url($din, $store, "main");

if (isset($_SESSION["user_id"]) && isset($_SESSION["pin"])) { //LOG IN!
$id = urlencode(stripslashes($_SESSION["user_id"]));
$pin = urlencode(stripslashes($_SESSION["pin"]));
$store = urlencode(stripslashes($store));
$din = urlencode(stripslashes($din));

$params .= "id=".$id;
$params .= "&pin=" . $pin;
$params .= "&store=" . $store;
$params .= "&din=" . $din;

if ($frame == 'main' || !$frame) {

$results = new xml("amjaygetcounsel", $params);
if (isset($results->error)) {
$this->error .= $results->error[0]->row[0]->row[0]->error;
} else {
if (is_array($results->couns[0]->counselling[0]->row)) {

foreach($results->couns[0]->counselling[0]->row as $counselling) {
$this->populate_from_node($counselling, "couns");
$this->video_url = format_counselling_url($this->couns_din, $this->couns_store, "video", $this->couns_category);
$this->couns_categoryname = htmlspecialchars($this->couns_categoryname);
$this->couns_counseltext = htmlspecialchars($this->couns_counseltext);
$this->counsell .= $this->pattern("counselling");
}
$this->populate_from_node($results->couns[0]->counselling[0]->row[0], "couns");
$this->first_video_url = format_counselling_url($this->couns_din, $this->couns_store, "video", $this->couns_category);
} else {
$this->go404("[counselling id empty]");
}
}
} else	{
if ($frame == 'video') {

$category = urlencode(stripslashes($category));
$params .= "&category=" . $category;

$results = new xml("amjaygetvideocounsel", $params);
if (isset($results->error)) {
$this->error .= $results->error[0]->row[0]->row[0]->error;
} else {
if (is_array($results->couns[0]->vidcounselling[0]->row)) {

foreach($results->couns[0]->vidcounselling[0]->row as $vidcounsell) {
$this->populate_from_node($vidcounsell, "v");
$this->video .= $this->pattern("vidtext");
}
} else {
$this->error = "[vidcounselling id empty]";
}
}
}
}
} else {
$this->go302(format_login_url(format_counselling_url($din, $store)));
}
} //func
}*/


class orderpharmacies_page extends template
{
var $store = "";
var $profile = "";
var $main = "";
var $date_select = "";
var $today = "";
var $counsell_url = "";
var $orderable = true;
var $rxs_exist = false;
var $rxcount = 0;
var $msg_exists="statusMessageNull";			
var $state_text="";
var $tmpdpic="";
var $tmpdirpic="";
function orderpharmacies_page($template = "template_orderpharmacies.html", $store = false,$patid =false,$pnhid=false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "Order Prescriptions";

   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));
      $store = urlencode(stripslashes($store));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;
      if ($patid) {
      	$params .= "&patid=" . $patid;
      }
      if ($pnhid)
      {
	$params .= "&nhid=" . $pnhid;				
      } 	
       

      $this->nhid=$pnhid;
      $this->patientid=$patid;
      $results = new xml("Amjaymultigetprofile", $params . "&store=" . $store);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->patientprofile[0]->store[0]->row))
      {

         $this->today = date("n/d/Y", time() + 3600 * 24 * $i + 3600 * $webportal_time_diff);
         for($i = 0; $i < 10; $i++)
         {
            $this->select_item = date("Y-m-d", time() + 3600 * 24 * $i + 3600 *$webportal_time_diff);
            $this->date_select .= $this->pattern("date_select");
         }
         foreach($results->patientprofile[0]->store  as $i => $store)
         {
            $this->rxcount=0;
			$this->rxs_exist=false;
			$this->populate_from_node($store->row[0], "store");
            $this->store = $this->pattern("store");
            $this->store_bannerimage = $this->images_http_prefix . $this->store_bannerimage;
            $this->profile = "";
            
            foreach($results->patientprofile[0]->patient[0]->row  as $patient)
            {
             $this->populate_from_node($patient, "patient");   
            }
            
            
            if(is_array($results->patientprofile[0]->profile[$i]->row))
            {
               foreach($results->patientprofile[0]->profile[$i]->row  as $profile)
               {
                  $this->populate_from_node($profile, "profile");
                  if ($this->profile_rxnumber!="")
					{
					  $this->rxcount++;
					}
				  if($this->profile_refillable == 'N')
                   {
                    $this->orderable = False;
                   }
                  else
                   {
                    $this->orderable = True;
                   }
				  $this->msg_exists="statusMessageNull";			
				  
				  if($this->profile_reason!="")
					{
					  $this->msg_exists="statusMessage";			
					}


		    //*****************************************************************************		   	
                  //$this->profile_drugpic = $this->images_http_prefix . $this->profile_drugpic;
                  //$this->profile_drugpic = str_replace("\\", "/", $this->profile_drugpic);
                  //*****************************************************************************
                      $tmpdpic=str_pad($this->profile_din , 8, "0", STR_PAD_LEFT); 
                      $tmpdirpic="/home/erefi104/public_html/centralprofile/images/drugpics/" . $tmpdpic . ".jpg";
                      $tmpdpic=$this->images_http_prefix . "drugpics/" . $tmpdpic . ".jpg";
                      
                      if (file_exists($tmpdirpic)) {
                      
                        $this->profile_drugpic = $tmpdpic;
                      } else {
                         $tmpdpic=$this->images_http_prefix . "drugpics/noimage.jpg";
                         $this->profile_drugpic =$tmpdpic;
                      }                      


                 
                  $this->state_text = str_replace(".png","",$this->profile_stateimage);
		  $this->state_text = str_replace("rxstat/","",$this->state_text);
		  $this->state_text = ucwords($this->state_text);  
		  $this->profile_stateimage = $this->images_http_prefix . $this->profile_stateimage;
                  $this->profile_stateimage = str_replace("\\", "/", $this->profile_stateimage);
                  
                  $this->counsell_url = format_counselling_url($this->profile_din, $this->profile_storeidentifier);
                  if($this->rxcount>0)   
					{
						$this->rxs_exist=true;
					}		
				  $this->profile .= $this->pattern("profile");
               }
            }
            $this->main .= $this->pattern("main");
         }
          $params ="";
          $params .= "id=" . $id;
          $params .= "&pin=" . $pin;

         $results = new xml("Amjaygetlogin", $params);
         if(isset($results->error))
         {
            $this->error .= $results->error[0]->row[0]->row[0]->error;
         }
         elseif(is_array($results->login[0]->patients[0]->row))
         {
            $this->populate_from_node($results->login[0]->patients[0]->row[0], "lpatient");
         }
         else
         {
            $this->error = "[patients is empty]";
         }
         
      }
      else
      {
         $this->error = "[store is empty]";
      }
   }
   else
   {
      $this->go302(format_login_url(format_orderpharmacies_url($store)));
   }
}//func
}


class orderrefills_page extends template
{
var $rxs_main = "";
var $rxs_color = "red";
var $backtonhpatient_url = "";


function orderrefills_page($template = "template_orderrefills.html", $store = false, $rxids = false, $email = false, $note = false, $pickdeldate = false, $pickdeltime = false, $pickupdelivery = false, $deladdress = false,$pnhid=false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "Order Confirmation";
   
   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));
      if(!$email) $email = "";
      $email = urlencode(stripslashes($email));
      if(!$note) $note = "";
      $note = urlencode(stripslashes($note));
      if(!$pickdeldate) $pickdeldate = date("Y-m-d", time() + 3600 * 24 * $i + 3600 * $webportal_time_diff);
      $pickdeldate = urlencode(stripslashes($pickdeldate));
      if(!$pickdeltime) $pickdeltime = "10:00";
      $pickdeltime = urlencode(stripslashes($pickdeltime));
      if(!$pickupdelivery) $pickupdelivery = "P";
      $pickupdelivery = urlencode(stripslashes($pickupdelivery));
      $store = urlencode(stripslashes($store));
      $rxids = urlencode(stripslashes($rxids));
      if(!$deladdress) $deladdress = " ";
      $deladdress = urlencode(stripslashes($deladdress));
      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;
      $params .= "&email=" . $email;
      $params .= "&note=" . $note;
      $params .= "&pickdeldate=" . $pickdeldate;
      $params .= "&pickdeltime=" . $pickdeltime;
      $params .= "&pickupdelivery=" . $pickupdelivery;
      $params .= "&store=" . $store;
      $params .= "&rxids=" . $rxids;
      $params .= "&deladdress=" . $deladdress;

      $results = new xml("Amjayorderrx", $params, true);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->order[0]->stores[0]->row))
      {
         $this->populate_from_node($results->order[0]->stores[0]->row[0], "store");
         $this->viewpharmacies_url=format_viewpharmacies_url()."?"."store[]=".$store;
         $this->backtonhpatient_url = "viewnhpatients.php?nhid=".$pnhid."&store=".$store;
       
		 $this->populate_from_node($results->order[0]->patient[0]->row[0], "patient");
         foreach($results->order[0]->rxs[0]->row  as $i => $rxs)
         {
            $this->populate_from_node($rxs, "rxs");
            $this->rxs_drugname = htmlspecialchars($this->rxs_drugname);
            if($this->rxs_status == 'Ordered')
            {
               $this->rxs_color = "green";
               $this->rxs_mobile_status = "successful";
            }
            else
            {
               $this->rxs_color = "red";
               $this->rxs_mobile_status = "failed";
            }
            $this->rxs_main .= $this->pattern("rxs");
         }
      }
      else
      {
         $this->error = "[stores is empty]";
      }
   }
   else
   {
      $this->go302(format_login_url(format_pharmacies_url()));
   }
}//func
}

class orderrefills_confirmation_page extends template
{
var $rxs_main = "";
var $rxs_color = "red";


function orderrefills_confirmation_page($template = "template_orderrefills_confirmation.html", $store = false, $rxids = false, $email = false, $note = false, $pickdeldate = false, $pickdeltime = false, $pickupdelivery = false, $deladdress = false,$nhid=false,$patid=false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "Order Confirmation";
   
      if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
      {//LOG IN!    //***008***
         $id = urlencode(stripslashes($_SESSION["user_id"]));
         $pin = urlencode(stripslashes($_SESSION["pin"]));
         $store = urlencode(stripslashes($store));

         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;
         $params .= "&store=" . $store;
         $params .= "&patid=" . $patid;
         $params .= "&nhid=" . $nhid;

         $results = new xml("Amjaymultigetprofile", $params);
         if(isset($results->error))
         {         //***007***
            $this->error .= $results->error[0]->row[0]->row[0]->error;
         }       //***007***
         elseif(is_array($results->patientprofile[0]->store[0]->row))
         {      //***007***
			
            
				if(is_array($results->patientprofile[0]->store[0]->row)) {
				   $this->populate_from_node($results->patientprofile[0]->store[0]->row[0], "store");
				   $this->store_bannerimage = $this->images_http_prefix . $this->store_bannerimage;
			    }
               if(is_array($results->patientprofile[0]->patient[0]->row))
               {
                 $this->populate_from_node($results->patientprofile[0]->patient[0]->row[0], "patient");
               }
			   
			   $this->customers_rxids = htmlspecialchars(stripslashes($rxids));
			   $this->customers_store = htmlspecialchars(stripslashes($store));
			   $rxids = explode(",", $rxids);
			   if(is_array($results->patientprofile[0]->profile[0]->row)) {
				foreach($results->patientprofile[0]->profile[0]->row as $profile) {
					if (in_array($profile->rxid, $rxids)) {
						$this->populate_from_node($profile, "profile");
						$this->profiles .= $this->pattern("profile");
					}
				}
			   }
            
			   
			  if(!$email) $email = "";
			  $this->customers_email = htmlspecialchars(stripslashes($email));
			  if(!$note) $note = "";
			  $addnote="";
              if ($nhid) $addnote=date('Y-m-d H:i:s'). "   ORDERED BY: " . $this->customers_email  . "   CARD #: " . $id . "<br>";
              $this->customers_note = $addnote . htmlspecialchars(stripslashes($note));
              $this->nhid = $nhid;
			  if(!$pickdeldate) $pickdeldate = date("Y-m-d", time() + 3600 * 24 * $i + 3600 * $webportal_time_diff);
			  $this->customers_pickdeldate = htmlspecialchars(stripslashes($pickdeldate));
			  if(!$pickdeltime) $pickdeltime = "10:00";
			  $this->customers_pickdeltime = htmlspecialchars(stripslashes($pickdeltime));
			  if(!$pickupdelivery) $pickupdelivery = "P";
			  $this->customers_pickupdelivery = htmlspecialchars(stripslashes($pickupdelivery));
			  $this->customers_ispickup = ($pickupdelivery == "P");
			  $store = htmlspecialchars(stripslashes($store));
			  $rxids = htmlspecialchars(stripslashes($rxids));
			  if(!$deladdress) $deladdress = " ";
			  $this->customers_deladdress = htmlspecialchars(stripslashes($deladdress));
			  
       } //***007***
        else
       { //***007***
         $this->go404("[store is empty]");
       } //***007***
   }    //***008***
   else
   {    //***008***
      $this->go302(format_login_url(format_viewpharmacies_url($store)));
   }    //***008***


}//func
}










class specialevents_page extends template
{
var $events = "";
var $book_url = "";

function specialevents_page($template = "template_specialevents.html", $store = false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "Special Events";

   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));
      $store = urlencode(stripslashes($store));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;
      $params .= "&store=" . $store;

      $results = new xml("amjaygetevents", $params);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->events[0]->storeevents[0]->row))
      {

         $this->populate_from_node($results->events[0]->patients[0]->row[0], "patients");
         $this->populate_from_node($results->events[0]->stores[0]->row[0], "stores");
         $this->stores_bannerimage = $this->images_http_prefix . $this->stores_bannerimage;

         foreach($results->events[0]->storeevents[0]->row  as $i => $storeevents)
         {
            $this->populate_from_node($storeevents, "event");
            $this->book_url = format_book_url($this->stores_storeidentifier, $this->event_eventid);
            $this->event_eventimage = $this->images_http_prefix . $this->event_eventimage;
            $this->events .= $this->pattern("event");
         }
      }
      else
      {
         $this->go404("[no events found]");
      }
   }
   else
   {
      $this->go302(format_login_url(format_special_url($store)));
   }
}//func
}

class bookspecial_page extends template
{
var $books = "";
var $bookmiddle_url = "";
var $rshow = false;


function bookspecial_page($template = "template_bookspecial.html", $store = false, $eventid = false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = " Special Events Available Appointments";

   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));
      $store = urlencode(stripslashes($store));
      $eventid = urlencode(stripslashes($eventid));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;
      $params .= "&store=" . $store;
      $params .= "&eventid=" . $eventid;
      // changed by Ajay
      //$results = new xml("amjaygetbookingslot", $params, true);
      $results = new xml("amjaygetappointments", $params, true);
      //Changed Ajay ends
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->appointments[0]->bookappintments[0]->row))
      {
         $this->populate_from_node($results->appointments[0]->patients[0]->row[0], "patients");
         $this->populate_from_node($results->appointments[0]->stores[0]->row[0], "stores");

         $only_checkbox = false;
         foreach($results->appointments[0]->bookappintments[0]->row  as $i => $bookappintment)
         {
            if($only_checkbox !== false && $bookappintment->bookingstatus == 'Open')
            {
               $only_checkbox = false;
               break;
            }
            if($only_checkbox === false && $bookappintment->bookingstatus == 'Open') $only_checkbox = $i;
         }

         foreach($results->appointments[0]->bookappintments[0]->row  as $i => $bookappintment)
         {
            $this->populate_from_node($bookappintment, "book");
            if($this->book_bookingstatus == 'Open')
            {
               $this->rshow = true;
            }
            else
            {
               $this->rshow = false;
            }
            if($i === $only_checkbox)
            {//the only checkbox!
               $this->book_selected = "checked";
            }
            else
            {
               $this->book_selected = "";
            }
            $this->books .= $this->pattern("book");
         }
      }
      else
      {
         $this->go404("[no bookappintments]");
      }
   }
   else
   {
      $this->go302(format_login_url(format_book_url($store, $eventid)));
   }
}//func
}


class bookfinish_page extends template
{
var $books = "";

function bookfinish_page($template = "template_bookmiddle.html", $store = false, $slot = false, $email = false, $eventid = false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "Special Events Booking Confirmation";

   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));
      $store = urlencode(stripslashes($store));
      $slot = urlencode(stripslashes($slot));
      $email = urlencode(stripslashes($email));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;
      $params .= "&store=" . $store;
      $params .= "&slotid=" . $slot;
      $params .= "&email=" . $email;

      $results = new xml("amjaybookslot", $params, true);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->appointments[0]->bookappintments[0]->row))
      {
         $this->populate_from_node($results->appointments[0]->patients[0]->row[0], "patients");
         $this->populate_from_node($results->appointments[0]->stores[0]->row[0], "stores");
         foreach($results->appointments[0]->bookappintments[0]->row  as $i => $bookappintment)
         {
            $this->populate_from_node($bookappintment, "book");
            $this->books .= $this->pattern("book");
         }
      }
      else
      {
         $this->error = "[bookappintments is empty]";
      }
   }
   else
   {
      $this->go302(format_login_url(format_book_url($store, $eventid)));
   }
}//func
}


class cholesterol_page extends template
{
var $cholesterol_add_url = "";
var $cholesterols = "";
var $today = "";
var $err = "";
var $NumberOfVals = "";
var $VAL_L1_MAIN = "";
var $VAL_L2_MAIN = "";
var $VAL_L3_MAIN = "";
var $NUM_L1 = "";
var $NUM_L2 = "";
var $NUM_L3 = "";
var $graph_url = "";

function cholesterol_page($template = "template_cholesterol.html", $rowid = false)
{
   global $link, $xml_server_url, $affiliate_id;


   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "My Cholesterol";

   $this->cholesterol_add_url = format_cholesterol_url("add");
   $this->graph_url = format_graph_url("cholesterol");

   if(isset($_SESSION['Err']))
   {
      $this->error = $_SESSION['Err'];
      unset($_SESSION['Err']);
   }
   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;

      $results = new xml("amjayviewchol", $params, true);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->cholesterol[0]->patients[0]->row))
      {

         $this->populate_from_node($results->cholesterol[0]->patients[0]->row[0], "patient");
         $this->today = date("n/d/Y", time() + 3600 * 24 * $i + 3600 * $webportal_time_diff);

         $this->NumberOfVals = count($results->cholesterol[0]->patientcholesterol[0]->row);


         $num = 0;
         $unique_count_array = array();
         if(is_array($results->cholesterol[0]->patientcholesterol[0]->row))
         {
            foreach($results->cholesterol[0]->patientcholesterol[0]->row  as $patientcholesterol)
            {
               $this->populate_from_node($patientcholesterol, "patientcholesterol");
               $var = "patientcholesterol_measuredby_" . $this->patientcholesterol_measuredby;
               $this->$var = true;
               $this->cholesterol_edit_url = format_cholesterol_url("edit", $this->patientcholesterol_id);
               $this->cholesterol_update_url = format_cholesterol_url("update", $this->patientcholesterol_id);
               $this->cholesterol_delete_url = format_cholesterol_url("del", $this->patientcholesterol_id);
               if($this->patientcholesterol_id == $rowid)
               {
                  $this->cholesterols .= $this->pattern("edit");
               }
               else
               {
                  $this->cholesterols .= $this->pattern("cholesterol");
               }
               $unique_count_array[$patientcholesterol->readingdate] = 1;
               $num++;
               $this->NUM_L1 = $num . "_L1";
               $this->NUM_L2 = $num . "_L2";
               $this->NUM_L3 = $num . "_L3";
               $this->VAL_L1_MAIN .= $this->pattern("VAL_L1");
               $this->VAL_L2_MAIN .= $this->pattern("VAL_L2");
               $this->VAL_L3_MAIN .= $this->pattern("VAL_L3");
            }
         }
         if(count($unique_count_array) > 1) $this->show_graph = true;
      }
      else
      {
         $this->go404("Info not found");
      }
   }
   else
   {
      $this->go302(format_login_url(format_cholesterol_url()));
   }
}//func
}


class bloodpressure_page extends template
{
var $bloodpressures = "";
var $today = "";
var $bloodpressure_add_url = "";
var $err = "";
var $NumberOfVals = "";
var $VAL_L1_MAIN = "";
var $VAL_L2_MAIN = "";
var $VAL_L3_MAIN = "";
var $NUM_L1 = "";
var $NUM_L2 = "";
var $NUM_L3 = "";
var $graph_url = "";

function bloodpressure_page($template = "template_bloodpressure.html", $rowid = false)
{
   global $link, $xml_server_url, $affiliate_id;


   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "My Blood Pressure";

   $this->bloodpressure_add_url = format_bloodpressure_url("add");
   $this->graph_url = format_graph_url("bloodpressure");

   if(isset($_SESSION['Err']))
   {
      $this->error = $_SESSION['Err'];
      unset($_SESSION['Err']);
   }
   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;

      $results = new xml("amjayviewbp", $params, true);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->bloodpressure[0]->patients[0]->row))
      {

         $this->populate_from_node($results->bloodpressure[0]->patients[0]->row[0], "patient");
         $this->today = date("n/d/Y", time() + 3600 * 24 * $i + 3600 * $webportal_time_diff);

         $this->NumberOfVals = count($results->bloodpressure[0]->patientbloodpressure[0]->row);


         $num = 0;
         $unique_count_array = array();
         if(is_array($results->bloodpressure[0]->patientbloodpressure[0]->row))
         {
            foreach($results->bloodpressure[0]->patientbloodpressure[0]->row  as $patientbloodpressure)
            {
               $this->populate_from_node($patientbloodpressure, "patientbloodpressure");
               $var = "patientbloodpressure_measuredby_" . $this->patientbloodpressure_measuredby;
               $this->$var = true;
               $this->bloodpressure_edit_url = format_bloodpressure_url("edit", $this->patientbloodpressure_id);
               $this->bloodpressure_update_url = format_bloodpressure_url("update", $this->patientbloodpressure_id);
               $this->bloodpressure_delete_url = format_bloodpressure_url("del", $this->patientbloodpressure_id);
               if($this->patientbloodpressure_id == $rowid)
               {
                  $this->bloodpressures .= $this->pattern("edit");
               }
               else
               {
                  $this->bloodpressures .= $this->pattern("bloodpressure");
               }
               $unique_count_array[$patientbloodpressure->readingdate] = 1;
               $num++;
               $this->NUM_L1 = $num . "_L1";
               $this->NUM_L2 = $num . "_L2";
               $this->NUM_L3 = $num . "_L3";
               $this->VAL_L1_MAIN .= $this->pattern("VAL_L1");
               $this->VAL_L2_MAIN .= $this->pattern("VAL_L2");
               $this->VAL_L3_MAIN .= $this->pattern("VAL_L3");
            }
         }
         if(count($unique_count_array) > 1) $this->show_graph = true;
      }
      else
      {
         $this->go404("Info not found");
      }
   }
   else
   {
      $this->go302(format_login_url(format_bloodpressure_url()));
   }
}//func
}

class vaccination_page extends template
{
var $vaccinations = "";
var $vaccpicks = "";
var $today = "";
var $vaccination_add_url = "";
var $err = "";
var $NumberOfVals = "";
var $VAL_L1_MAIN = "";
var $VAL_L2_MAIN = "";
var $VAL_L3_MAIN = "";
var $NUM_L1 = "";
var $NUM_L2 = "";
var $NUM_L3 = "";
var $graph_url = "";

function vaccination_page($template = "template_vaccination.html", $rowid = false)
{
   global $link, $xml_server_url, $affiliate_id;


   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "My Vaccinations";

   $this->vaccination_add_url = format_vaccination_url("add");
   $this->graph_url = format_graph_url("vaccination");

   if(isset($_SESSION['Err']))
   {
      $this->error = $_SESSION['Err'];
      unset($_SESSION['Err']);
   }
   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;

      $results = new xml("amjayviewvc", $params, true);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->vaccination[0]->patients[0]->row))
      {

         $this->populate_from_node($results->vaccination[0]->patients[0]->row[0], "patient");
         $this->today = date("n/d/Y", time() + 3600 * 24 * $i + 3600 * $webportal_time_diff);

         $this->NumberOfVals = count($results->vaccination[0]->patientvaccination[0]->row);


         $num = 0;
         $unique_count_array = array();
         if(is_array($results->vaccination[0]->patientvaccination[0]->row))
         {
            foreach($results->vaccination[0]->patientvaccination[0]->row  as $patientvaccination)
            {
               $this->populate_from_node($patientvaccination, "patientvaccination");
               $var = "patientvaccination_givenby_" . $this->patientvaccination_givenby;
               $this->$var = true;
               $this->vaccination_edit_url = format_vaccination_url("edit", $this->patientvaccination_id);
               $this->vaccination_update_url = format_vaccination_url("update", $this->patientvaccination_id);
               $this->vaccination_delete_url = format_vaccination_url("del", $this->patientvaccination_id);
               if($this->patientvaccination_id == $rowid)
               {
                  $this->vaccinations .= $this->pattern("edit");
               }
               else
               {
                  $this->vaccinations .= $this->pattern("vaccination");
               }
               $unique_count_array[$patientvaccination->healthdate] = 1;
               $num++;
               $this->NUM_L1 = $num . "_L1";

               $this->VAL_L1_MAIN .= $this->pattern("VAL_L1");

            }
         }
         if(is_array($results->vaccination[0]->vaccinationpicks[0]->row))
         {
            foreach($results->vaccination[0]->vaccinationpicks[0]->row  as $vaccinationpicks)
            {
               $this->populate_from_node($vaccinationpicks, "vaccinationpicks");
               $this->vaccpicks .= $this->pattern("vaccinationpicks");
    
               
            }
         }
         if(count($unique_count_array) > 1) $this->show_graph = true;
      }
      else
      {
         $this->go404("Info not found");
      }
   }
   else
   {
      $this->go302(format_login_url(format_vaccination_url()));
   }
}//func
}







class bloodsugar_page extends template
{
var $bloodsugars = "";
var $today = "";
var $bloodsugar_add_url = "";
var $err = "";
var $NumberOfVals = "";
var $VAL_L1_MAIN = "";
var $NUM_L1 = "";
var $graph_url = "";

function bloodsugar_page($template = "template_bloodsugar.html", $rowid = false)
{
   global $link, $xml_server_url, $affiliate_id;


   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "My Blood Sugar";

   $this->bloodsugar_add_url = format_bloodsugar_url("add");
   $this->graph_url = format_graph_url("bloodsugar");

   if(isset($_SESSION['Err']))
   {
      $this->error = $_SESSION['Err'];
      unset($_SESSION['Err']);
   }
   if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
   {//LOG IN!
      $id = urlencode(stripslashes($_SESSION["user_id"]));
      $pin = urlencode(stripslashes($_SESSION["pin"]));

      $params .= "id=" . $id;
      $params .= "&pin=" . $pin;

      $results = new xml("amjayviewbs", $params, true);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->bloodsugar[0]->patients[0]->row))
      {

         $this->populate_from_node($results->bloodsugar[0]->patients[0]->row[0], "patient");
         $this->today = date("n/d/Y", time() + 3600 * 24 * $i + 3600 * $webportal_time_diff);

         $this->NumberOfVals = count($results->bloodsugar[0]->patientbloodsugar[0]->row);

         $num = 0;
         $unique_count_array = array();
         if(is_array($results->bloodsugar[0]->patientbloodsugar[0]->row))
         {
            foreach($results->bloodsugar[0]->patientbloodsugar[0]->row  as $patientbloodsugar)
            {
               $this->populate_from_node($patientbloodsugar, "patientbloodsugar");
               $var = "patientbloodsugar_measuredby_" . $this->patientbloodsugar_measuredby;
               $this->$var = true;

               if(strpos($this->patientbloodsugar_unitofmeasure, "mmol") !== false)
               {
                  $this->patientbloodsugar_unitofmeasure_mmol = true;
               }
               else
               {
                  $this->patientbloodsugar_unitofmeasure_mg = true;
               }
               $this->bloodsugar_edit_url = format_bloodsugar_url("edit", $this->patientbloodsugar_id);
               $this->bloodsugar_update_url = format_bloodsugar_url("update", $this->patientbloodsugar_id);
               $this->bloodsugar_delete_url = format_bloodsugar_url("del", $this->patientbloodsugar_id);
               if($this->patientbloodsugar_id == $rowid)
               {
                  $this->bloodsugars .= $this->pattern("edit");
               }
               else
               {
                  $this->bloodsugars .= $this->pattern("bloodsugar");
               }
               $unique_count_array[$patientbloodsugar->readingdate] = 1;
               $num++;
               $this->NUM_L1 = $num . "_L1";
               $this->VAL_L1_MAIN .= $this->pattern("VAL_L1");
            }
         }
         if(count($unique_count_array) > 1) $this->show_graph = true;
      }
      else
      {
         $this->go404("Info not found");
      }
   }
   else
   {
      $this->go302(format_login_url(format_bloodsugar_url()));
   }
}//func
}



class choose_date extends index_page
{
var $back_url = "";

function choose_date($template = false, $back_url = "/")
{
   $this->index_page($template);
   $this->back_url = $back_url;
   $this->title2 = "Please, choose a date";
}//func
}

function max_paragraph($value, $maxlen)
{
$m = substr($value, 0, $maxlen);
$pos = strrpos($m, ".") + 1;
if($pos === false)
{
   return $m;
}
else
{
   return substr($m, 0, $pos);
}
}



function year_options($year, $select = true, $num_to_show = 2)
{

for($ix = date("Y"); $ix < date("Y") + $num_to_show; $ix++)
{
   $yy = $ix;
   if($yy == $year && $select)
   {
      $selected = "SELECTED ";
   }
   else
   {
      $selected = "";
   }

   $yy_ddown .= '<option ' . $selected . 'value="' . $yy . '">' . $yy . '</option>';
}

return $yy_ddown;
}

function day_options($day, $select = true)
{

for($ix = 1; $ix < 32; $ix++)
{
   $dd = $ix;
   if($dd == $day && $select)
   {
      $selected = "SELECTED ";
   }
   else
   {
      $selected = "";
   }

   $day_ddown .= '<option ' . $selected . 'value="';
   if($dd < 10) $dd = "0" . $dd;
   $day_ddown .= $dd . '">' . $dd . '</option>';
}

return $day_ddown;
}

function hr_options($hr, $select = true)
{

for($ix = 0; $ix < 24; $ix++)
{
   $dd = $ix;
   if(intval($dd) == intval($hr) && $select)
   {
      $selected = "SELECTED ";
   }
   else
   {
      $selected = "";
   }

   $day_ddown .= '<option ' . $selected . 'value="' . $dd;
   //if ($dd = 0) $dd=12;
   if($dd < 12) $dd .= " AM";
   else $dd = ($dd - 12) . " PM";
   if($dd == "0 AM") $dd = "12AM";
   if($dd == "0 PM") $dd = "12PM";
   $day_ddown .= '">' . $dd . '</option>';
}

return $day_ddown;
}

function min_options($min, $select = true)
{

for($ix = 0; $ix < 60; $ix += 10)
{
   $dd = $ix;
   if(intval($dd) == intval($min) && $select)
   {
      $selected = "SELECTED ";
   }
   else
   {
      $selected = "";
   }

   $day_ddown .= '<option ' . $selected . 'value="';
   if($dd < 10) $dd = "0" . $dd;
   $day_ddown .= $dd . '">' . $dd . '</option>';
}

return $day_ddown;
}

function month_options($month, $select = true)
{
$month_name = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

for($ix = 0; $ix < 12; $ix++)
{
   $mm = $ix + 1;
   if($mm == $month && $select)
   {
      $selected = "SELECTED ";
   }
   else
   {
      $selected = "";
   }

   $month_ddown .= '<option ' . $selected . 'value="';
   if($mm < 10) $mm = "0" . $mm;
   $month_ddown .= $mm . '">' . $month_name[$ix] . '</option>';
}

return $month_ddown;
}

function hotels_options($select = true)
{

for($ix = 0; $ix < 12; $ix++)
{
   $mm = $ix + 1;
   if($mm == $month && $select)
   {
      $selected = "SELECTED ";
   }
   else
   {
      $selected = "";
   }

   $month_ddown .= '<option ' . $selected . 'value="';
   if($mm < 10) $mm = "0" . $mm;
   $month_ddown .= $mm . '">' . $month_name[$ix] . '</option>';
}

return $month_ddown;
}

function populate_options(&$object)
{
global $sdate;
if($sdate)
{
   list($date, $time) = explode(" ", $sdate);
   list($ay, $am, $ad) = explode("-", $date);
   list($hr, $min) = explode(":", $time);
}
else
{
   $am = date("m");
   $ad = date("d");
   $ay = date("Y");
   $hr = "10";
   $min = "00";
}

$object->select_mm = month_options($am);
$object->select_dd = day_options($ad);
$object->select_yy = year_options($dm);

$object->select_hr = hr_options($hr);
$object->select_min = min_options($min);
}

function log_timestamp()
{
global $webportal_time_diff;

$thedate = wp_timestamp($webportal_time_diff);
$return = date("m-d-Y H:i a", $thedate);
return $return;

}
/*
function format_url($url, $SSL=false) {
global $request_ssl, $http_prefix, $https_prefix, $pass_sid_to_https;
if (defined("SID") && SID) {
$sid = SID;
}
//echo $url . " - " . intval($request_ssl);
if ($request_ssl!=$SSL) { //full link!
if ($SSL) $url = $https_prefix . $url; else $url = $http_prefix . $url;
if ($pass_sid_to_https && !$sid) $sid = session_name() . "=" . session_id();
}
if ($sid) {
if (strstr($url, "?")!==false) $url .= "&" . $sid; else $url .= "?" . $sid;
}
return $url;
}
*/

class orderrefillsbynumber_page extends template
{
var $orderitems_main = "";
var $rxs_color = "red";
var $statarray=array();

function orderrefillsbynumber_page($template = "template_orderbynumber.html", $rxnums = false, $telephone=false, $note = false, $storetelephone=false,$pdel=false,$email=false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "Order by Rx Number";
   if(!$note) $note = "";
     $note = urlencode(stripslashes($note));
     $rxnums = urlencode(stripslashes($rxnums));
     $telephone=urlencode(stripslashes($telephone));
     $storetelephone=urlencode(stripslashes($storetelephone));
     $params .= "rxnums=" . $rxnums;
     $params .= "&note=" . $note;
     $params .= "&telephone=" . $telephone;
     $params .= "&storetelephone=" . $storetelephone;
     $params .= "&pdel=" . $pdel;
     $params .= "&email=" . $email;

     $results = new xml("Amjayorderbyrxnum", $params, true);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->order[0]->stores[0]->row))
      {
         $this->populate_from_node($results->order[0]->stores[0]->row[0], "store");
         $this->viewpharmacies_url=format_viewpharmacies_url()."?"."store[]=".$store;
       
		 //$this->populate_from_node($results->order[0]->patient[0]->row[0], "patient");
         foreach($results->order[0]->orderitems[0]->row  as $i => $orderitems)
         {
            $this->populate_from_node($orderitems, "orderitems");
                        if($this->orderitems_status == 'Ordered')
            {
               $this->rxs_color = "green";
               $this->rxs_mobile_status = "successful";
               $this->statarray[]=$orderitems;
            }
            else
            {
               $this->rxs_color = "red";
               $this->rxs_mobile_status = "failed";
               $this->statarray[]=$orderitems;
            }

            $this->orderitems_main .= $this->pattern("orderitems");
         }
      }
      else
      {
         $this->error = "[stores is empty]";
      }
   
}//func
}


class secretcoderesult_page extends template
{
   var $stores = "";
   var $special_url = "";
   var $mobile_url = "";
   var $agreed = 0;
   var $agreestat=""; 
   var $changestat="";
   var $passwordchange = 0; 
   var $resetpin="";		
   function secretcoderesult_page($template = "template_secretcoderesult.html",$secretcode="",$email="",$refillreminder="")
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->template($template);
      $this->title = $this->company_name;
      $this->title2 = "Join Club";

         $params .= "secretcode=" . $secretcode;
         $params .= "&email=" . $email;
         $params .= "&refillreminder=" . $refillreminder;
         $results = new xml("amjayonlineenrol", $params, true);
         if(isset($results->error))
         {
            $this->error .= $results->error[0]->row[0]->row[0]->error;
         }
         elseif(is_array($results->onlineenrollcard[0]->card[0]->row))
         {

            $this->populate_from_node($results->onlineenrollcard[0]->card[0]->row[0], "patient");
         
         }
         else
         {
            $this->go404("[patients is empty]");
         }
   }//func
}

class logingroup_page extends login_form
{

   function logingroup_page($template = "template_logingroup.html", $id = false, $pin = false)
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->login_form($template);
	  
      if($id && $pin)
      {//LOG IN!
         $id = urlencode(stripslashes($id));
         $pin = urlencode(stripslashes($pin));

         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;

         $results = new xml("AmjayloginGroup", $params, true);
         if(!isset($results->error) && is_array($results->login[0]->patients[0]->row) && ($login_data = array_pop($results->login[0]->patients[0]->row)))
            {
		 $_SESSION["user_id"] = trim($login_data->patientkey);
               $_SESSION["pin"] = trim($login_data->pin);
		 $_SESSION["loginname"] = trim($login_data->firstname).",".trim($login_data->lastname);

               $_SESSION["nh"]='1';
 		


               if($_SESSION['snapshot_url'])
               {
                  $url = $_SESSION['snapshot_url'];
                  unset($_SESSION['snapshot_url']);
                  $this->go302(format_link($url));
               }
               else
               {
                  $this->go302(format_nh_url());
               }
         } else { //error?
		 	if (isset($results->error[0]->row[0]->row[0]->error)) {
	            $this->error .= $results->error[0]->row[0]->row[0]->error;
			} else {
				$this->error .= "Unknown error";
            }
         }
      }
   }//func
}

class nh_page extends template
{
   var $stores = "";
   var $special_url = "";
   var $mobile_url = "";
   var $agreed = 0;
   var $agreestat=""; 
   var $changestat="";
   var $passwordchange = 0; 
   var $resetpin="";		
   function nh_page($template = "template_nh.html",$agbutton="DisAgree",$changebutton="",$npin="-1")
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->template($template);
      $this->title = $this->company_name;
      $this->title2 = "My Nursing and Group Homes";

      if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
      {//LOG IN!
         $id = urlencode(stripslashes($_SESSION["user_id"]));
         $pin = urlencode(stripslashes($_SESSION["pin"]));
         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;
         $params .= "&agree=" . $agbutton;
         $params .= "&pinchange=" . $changebutton;
	     $params .= "&newpin=" . $npin;	
         $results = new xml("Amjaylogingroup", $params, true);
         if(isset($results->error))
         {
            $this->error .= $results->error[0]->row[0]->row[0]->error;
         }
         elseif(is_array($results->login[0]->patients[0]->row))
         {

            $this->populate_from_node($results->login[0]->patients[0]->row[0], "patient");
            foreach($results->login[0]->homes[0]->row  as $home)
            {
               $this->populate_from_node($home, "home");
               $this->special_url = format_special_url($this->home_nhid);
		       $this->mobile_url = format_viewnhpatients_url($this->home_storeidentifier,$this->home_storenhkey);
//             $this->store_bannerimage = $this->images_http_prefix . $this->store_bannerimage;
               $this->store_name = htmlspecialchars($this->home_nhname);
			   $this->agreed = $this->patient_agreement;
			   $this->passwordchange = $this->patient_passwordchange;		
			   $this->resetpin=$this->patient_pin;
               $this->homes .= $this->pattern("home");
            }

         }
         else
         {
            $this->go404("[patients is empty]");
         }
      }
      else
      {
         $this->go302(format_logingroup_url(format_nh_url()));
  
  
      }
   }//func
}

class viewnhpatients_page extends template
{
   var $patients = "";
   var $special_url = "";
   var $mobile_url = "";
   var $storeid = "";
   var $nhprofile_url="";
   function viewnhpatients_page($template = "template_viewnhpatients.html",$nhid="",$store="")
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->template($template);
	 // echo $template; 	
      $this->title = $this->company_name;
      $this->title2 = "Patients";

      if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
      {//LOG IN!
         $id = urlencode(stripslashes($_SESSION["user_id"]));
         $pin = urlencode(stripslashes($_SESSION["pin"]));
         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;
         $params .= "&nhid=" . $nhid;
         $params .= "&store=" . $store;
     //    echo $params;
         $results = new xml("Amjaygetnhpatients", $params, true);
         if(isset($results->error))
         {
            $this->error .= $results->error[0]->row[0]->row[0]->error;
         }
         elseif(is_array($results->nhpatients[0]->store[0]->row))
         {
             
            $this->populate_from_node($results->nhpatients[0]->store[0]->row[0], "astore");
            foreach($results->nhpatients[0]->patients[0]->row  as $patientnh)
            {
               $this->populate_from_node($patientnh, "patient");
               $this->mobile_url = format_viewnhpatients_url($this->store_storeidentifier);
               $this->special_url = format_special_url($this->home_nhid);
               $this->nhprofile_url = format_viewnhprofile_url($store,$nhid,$this->patient_id);
               $this->nhcomments_url = format_viewnhcomments_url($store,$nhid,$this->patient_id,"internetnote");
               $this->patients .= $this->pattern("patient");
               $this->storeid = $store;
              
            }

         }
         else
         {
            $this->go404("[patients is empty]");
         }
      }
      else
      {
         $this->go302(format_logingroup_url(format_viewnhpatients_url()));
  
  
      }
   }//func
}
class marprint_page extends template
{
var $orderitems_main = "";
var $rxs_color = "red";


function marprint_page($template = "template_marprint.html", $patientid = false, $startdate=false, $enddate = false, $storeid = false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "Print MAR";
   
     $patid = urlencode(stripslashes($patientid));
     $stdate = urlencode(stripslashes($startdate));
     $edate=urlencode(stripslashes($enddate));
     $id = urlencode(stripslashes($_SESSION["user_id"]));
     $pin = urlencode(stripslashes($_SESSION["pin"]));
     $strid = urlencode(stripslashes($storeid));



     $params .= "id=" . $id;
     $params .= "&pin=" . $pin;
     $params .= "&store=" . $strid;
     $params .= "&patid=" . $patid;
     $params .= "&startdate=" . $stdate;
     $params .= "&enddate=" . $edate;

     $results = new xml("Amjayprintmar", $params, true);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->mar[0]->rx[0]->row))
      {
         $this->populate_from_node($results->mar[0]->rx[0]->row[0], "rx");
      }
      else
      {
         $this->error = "[We are currently experiencing difficulties. Please try again later]";
      }
   
}//func
}




class viewnhcomments_page extends template
{
   var $patients = "";
   var $special_url = "";
   var $mobile_url = "";
   var $storeid = "";
   var $nhcomments_url="";
   function viewnhcomments_page($template = "template_viewnhcomments.html",$nhid="",$patid="",$topic="",$store="")
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->template($template);
	 // echo $template; 	
      $this->title = $this->company_name;
      $this->title2 = "Patient Comments";

      if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
      {//LOG IN!
         $id = urlencode(stripslashes($_SESSION["user_id"]));
         $pin = urlencode(stripslashes($_SESSION["pin"]));
         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;
         $params .= "&patid=" . $patid;
         $params .= "&topic=" . $topic;
         $params .= "&store=" . $store;
     //    echo $params;
         $results = new xml("Amjaygetpatnote", $params, true);
         if(isset($results->error))
         {
            $this->error .= $results->error[0]->row[0]->row[0]->error;
         }
         elseif(is_array($results->patnotes[0]->notes[0]->row))
         {
             
            $this->populate_from_node($results->patnotes[0]->notes[0]->row[0], "anote");
            foreach($results->patnotes[0]->notes[0]->row  as $patientnote)
            {
               $this->populate_from_node($patientnote, "note");
//               $this->mobile_url = format_viewnhcomments($this->store_storeidentifier);
               $this->special_url = format_special_url($nhid);
               $this->nhcomments_url = format_viewnhcomments_url($store,$nhid,$patid);
               $this->notes .= $this->pattern("note");
               $this->storeid = $store;
              
            }

         }
         else
         {
            $this->go404("[notes is empty]");
         }
      }
      else
      {
//         $this->go302(format_logingroup_url(format_viewnhcomments_url()));
  
  
      }
   }//func
}




class addnhcomments_page extends template
{
   var $patients = "";
   var $special_url = "";
   var $mobile_url = "";
   var $storeid = "";
   var $nhcomments_url="";
   function addnhcomments_page($template = "template_viewcomments.html",$nhid="",$patid="",$topic="",$store="",$pnote="")
   {
      global $link, $xml_server_url, $affiliate_id;

      $this->template($template);
	 // echo $template; 	
      $this->title = $this->company_name;
      $this->title2 = "Add Patient Comments";

      if(isset($_SESSION["user_id"]) && isset($_SESSION["pin"]))
      {//LOG IN!
         $id = urlencode(stripslashes($_SESSION["user_id"]));
         $pin = urlencode(stripslashes($_SESSION["pin"]));
         $pnote=urlencode(stripslashes($pnote));

         $params .= "id=" . $id;
         $params .= "&pin=" . $pin;
         $params .= "&patid=" . $patid;
         $params .= "&topic=" . $topic;
         $params .= "&store=" . $store;
         $params .= "&pnote=" . $pnote;


     //    echo $params;
         $results = new xml("Amjayinsertpatnote", $params, true);
         if(isset($results->error))
         {
            $this->error .= $results->error[0]->row[0]->row[0]->error;
         }
         elseif(is_array($results->patnotes[0]->notes[0]->row))
         {
             
            $this->populate_from_node($results->patnotes[0]->notes[0]->row[0], "anote");
            foreach($results->patnotes[0]->notes[0]->row  as $patientnote)
            {
               $this->populate_from_node($patientnote, "note");
//               $this->mobile_url = format_viewnhcomments($this->store_storeidentifier);
               $this->special_url = format_special_url($nhid);
               $this->nhcomments_url = format_viewnhcomments_url($store,$nhid,$patid);
               $this->notes .= $this->pattern("note");
               $this->storeid = $store;
              
            }

         }
         else
         {
            $this->go404("[notes is empty]");
         }
      }
      else
      {
//         $this->go302(format_logingroup_url(format_viewnhcomments_url()));
  
  
      }
   }//func
}


class registerstore2_page extends template
{
var $orderitems_main = "";
var $rxs_color = "red";
var $alreadyregistered = false;


function registerstore2_page($template = "template_registerstore2.html",$storename = false,$address=false,$city = false,$province=false,$postal=false,$contactname=false,$phone=false,$fax=false,$email=false,$dispensarysoftware=false,$efttransit=false,$eftaccountnumber=false,$eftpayor=false,$hoursofoperation=false,$dispensarysoftwareid=false,$bannerid=false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "Register Store";
   if(!$note) $note = "";
     $storename = urlencode(stripslashes($storename));
     $address = urlencode(stripslashes($address));
     $city=urlencode(stripslashes($city));
     $province=urlencode(stripslashes($province));
     $postal=urlencode(stripslashes($postal));
     $contactname=urlencode(stripslashes($contactname));
     $phone=urlencode(stripslashes($phone));
     $fax=urlencode(stripslashes($fax));
     $email=urlencode(stripslashes($email));
     $dispensarysoftware=urlencode(stripslashes($dispensarysoftware));
     $efttransit=urlencode(stripslashes($efttransit));
     $eftaccountnumber=urlencode(stripslashes($eftaccountnumber));
     $eftpayor=urlencode(stripslashes($eftpayor));
     $hoursofoperation=urlencode(stripslashes($hoursofoperation));
     $dispensarysoftwareid=urlencode(stripslashes($dispensarysoftwareid));
     $bannerid=urlencode(stripslashes($bannerid));

     $params .= "storename=" . $storename;
     $params .= "&address=" . $address;
     $params .= "&city=" . $city;
     $params .= "&province=" . $province;
     $params .= "&postal=" . $postal;
     $params .= "&contactname=" . $contactname;
     $params .= "&phone=" . $phone;
     $params .= "&fax=" . $fax;
     $params .= "&email=" . $email;
     $params .= "&dispensarysoftware=" . $dispensarysoftware;
     $params .= "&efttransit=" . $efttransit;
     $params .= "&eftaccountnumber=" . $eftaccountnumber;
     $params .= "&eftpayor=" . $eftpayor;
     $params .= "&hoursofoperation=" . $hoursofoperation;
     $params .= "&dispensarysoftwareid=" . $dispensarysoftwareid;
     $params .= "&bannerid=" . $bannerid;


     $results = new xml("amjayregisterstore", $params, true);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->storeorders[0]->store[0]->row))
      {
         $this->populate_from_node($results->storeorders[0]->store[0]->row[0], "store");
        
      }
      else
      {
         $this->error = "[store is empty]";
      }
   
}//func
}

class checkrxbdate_page extends template
{
var $rxbirth_main = "";
var $rxs_color = "red";
var $birtharray=array();

function checkrxbdate_page($template = "template_checkrxbdate.html", $rxnumber = false, $storetelephone=false)
{
   global $link, $xml_server_url, $affiliate_id;

   $this->template($template);
   $this->title = $this->company_name;
   $this->title2 = "Check Bdate";
     $rxnumber = urlencode(stripslashes($rxnumber));
     $storetelephone=urlencode(stripslashes($storetelephone));
     $params .= "rxnumber=" . $rxnumber;
     $params .= "&storetelephone=" . $storetelephone;

     $results = new xml("Amjaygetrxbirthverify", $params, true);
      if(isset($results->error))
      {
         $this->error .= $results->error[0]->row[0]->row[0]->error;
      }
      elseif(is_array($results->rxbirthverify[0]->rxbirth[0]->row))
      {
      
         foreach($results->rxbirthverify[0]->rxbirth[0]->row  as $i => $rxbirthitems)
         {
            $this->populate_from_node($rxbirthitems, "rxbirthitems");
            $this->birtharray[]=$orderitems;
   

            $this->rxbirth_main .= $this->pattern("rxbirthitems");
         }
      }
      else
      {
         $this->error = "[rxbirth is empty]";
      }
   
}//func
}


?>