$button = "<form  action='index.php' method='post' name='form' id='form'>\n";
$button .= "<input title='" . $app_strings['LBL_CLEAR_BUTTON_TITLE'] . "' accessKey='" . $app_strings['LBL_CLEAR_BUTTON_KEY'] . "' class='button' LANGUAGE=javascript onclick=\"window.opener.clear_email_addresses(); window.close()\" type='submit' name='button' value='  " . $app_strings['LBL_CLEAR_BUTTON_LABEL'] . "  '>\n";
$button .= "<input title='" . $app_strings['LBL_CANCEL_BUTTON_TITLE'] . "' accessKey='" . $app_strings['LBL_CANCEL_BUTTON_KEY'] . "' class='button' LANGUAGE=javascript onclick=\"window.close()\" type='submit' name='button' value='  " . $app_strings['LBL_CANCEL_BUTTON_LABEL'] . "  '>\n";
$button .= "</form>\n";
//$form->assign('INPUT_VALUE_NAME',$_REQUEST['input_value_name']);
//$form->assign('INPUT_VALUE_ID',$_REQUEST['input_value_id']);
$form->assign("SET_RETURN_JS", $the_javascript);
require_once 'modules/Contacts/ContactFormBase.php';
$formBase = new ContactFormBase();
if (isset($_REQUEST['doAction']) && $_REQUEST['doAction'] == 'save') {
    $formBase->handleSave('', false, true);
}
$lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
$lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
$lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
$formbody = $formBase->getFormBody('', '', $_REQUEST['form']);
$formbody = '<table><tr><td nowrap valign="top">' . str_replace('<br>', '</td><td nowrap valign="top">&nbsp;', $formbody) . '</td></tr></table>';
$formSave = <<<EOQ
<input title='{$lbl_save_button_title}' accessKey='{$lbl_save_button_key}' class='button' type='submit' name='button' value='  {$lbl_save_button_label}  ' >&nbsp;<input title='{$app_strings['LBL_CANCEL_BUTTON_TITLE']}' accessKey='{$app_strings['LBL_CANCEL_BUTTON_KEY']}' class='button'  onClick="toggleDisplay('addform');" type='button' name='button' value='{$app_strings['LBL_CANCEL_BUTTON_LABEL']}' >
EOQ;
$createContact = <<<EOQ
<input class='button' type='button' name='showAdd' value='{$mod_strings['LNK_NEW_CONTACT']}' onClick='toggleDisplay("addform");'>
EOQ;
$form->assign("CREATECONTACT", $createContact);
$form->assign("ADDFORMHEADER", get_form_header($mod_strings['LNK_NEW_CONTACT'], $formSave, false));
$form->assign("ADDFORM", $formbody);
$form->assign("THEME", $theme);
$form->assign("IMAGE_PATH", $image_path);
$form->assign("MODULE_NAME", $currentModule);
if (isset($_REQUEST['form_submit'])) {
    $form->assign("FORM_SUBMIT", $_REQUEST['form_submit']);
    /**
     *
     */
    function process_page_for_address()
    {
        global $theme;
        global $mod_strings;
        global $app_strings;
        global $currentModule;
        global $sugar_version, $sugar_config;
        $output_html = '';
        $where = '';
        $where = $this->_get_where_clause();
        $formBase = new ContactFormBase();
        if (isset($_REQUEST['doAction']) && $_REQUEST['doAction'] == 'save') {
            $formBase->handleSave('', false, true);
        }
        $first_name = empty($_REQUEST['first_name']) ? '' : $_REQUEST['first_name'];
        $last_name = empty($_REQUEST['last_name']) ? '' : $_REQUEST['last_name'];
        $account_name = empty($_REQUEST['account_name']) ? '' : $_REQUEST['account_name'];
        $request_data = empty($_REQUEST['request_data']) ? '' : $_REQUEST['request_data'];
        $hide_clear_button = empty($_REQUEST['hide_clear_button']) ? false : true;
        $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
        $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
        $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
        // TODO: cleanup the construction of $addform
        $formbody = $formBase->getFormBody('', '', 'EmailEditView');
        $addform = '<table><tr><td nowrap="nowrap" valign="top">' . str_replace('<br>', '</td><td nowrap="nowrap" valign="top">&nbsp;', $formbody) . '</td></tr></table>' . '<input type="hidden" name="action" value="Popup" />';
        $formSave = <<<EOQ
\t\t<input type="submit" name="button" class="button" title="{$lbl_save_button_title}" accesskey="{$lbl_save_button_key}" value="  {$lbl_save_button_label}  " />
\t\t<input type="button" name="button" class="button" title="{$app_strings['LBL_CANCEL_BUTTON_TITLE']}" accesskey="{$app_strings['LBL_CANCEL_BUTTON_KEY']}" value="{$app_strings['LBL_CANCEL_BUTTON_LABEL']}" onclick="toggleDisplay('addform');" />
EOQ;
        $createContact = <<<EOQ
\t\t<input type="button" id="showAdd" name="showAdd" class="button" value="{$mod_strings['LNK_NEW_CONTACT']}" onclick="toggleDisplay('addform');" />
EOQ;
        $addformheader = get_form_header($mod_strings['LNK_NEW_CONTACT'], $formSave, false);
        $button = "<form action='index.php' method='post' name='form' id='form'>\n";
        if (!$hide_clear_button) {
            $button .= "<input type='button' name='button' class='button' onclick=\"send_back('','');\" title='" . $app_strings['LBL_CLEAR_BUTTON_TITLE'] . "' accesskey='" . $app_strings['LBL_CLEAR_BUTTON_KEY'] . "' value='  " . $app_strings['LBL_CLEAR_BUTTON_LABEL'] . "  ' />\n";
        }
        $button .= "<input type='submit' name='button' class='button' onclick=\"window.close();\" title='" . $app_strings['LBL_CANCEL_BUTTON_TITLE'] . "' accesskey='" . $app_strings['LBL_CANCEL_BUTTON_KEY'] . "' value='  " . $app_strings['LBL_CANCEL_BUTTON_LABEL'] . "  ' />\n";
        $button .= "</form>\n";
        $form = new XTemplate('modules/Contacts/Address_picker.html');
        $form->assign('MOD', $mod_strings);
        $form->assign('APP', $app_strings);
        $form->assign('ADDFORMHEADER', $addformheader);
        $form->assign('ADDFORM', $addform);
        $form->assign('THEME', $theme);
        $form->assign('MODULE_NAME', $currentModule);
        $form->assign('FIRST_NAME', $first_name);
        $form->assign('LAST_NAME', $last_name);
        $form->assign('ACCOUNT_NAME', $account_name);
        $form->assign('request_data', $request_data);
        // fill in for mass update
        $button = "<input type='hidden' name='module' value='Contacts'>" . "<input type='hidden' id='form_action' name='action' value='CloseContactAddressPopup'>" . "<input type='hidden' name='massupdate' value='true'>" . "<input type='hidden' name='delete' value='false'>" . "<input type='hidden' name='mass' value='Array'>" . "<input type='hidden' name='Update' value='Update'>";
        if (isset($_REQUEST['mass']) && is_array($_REQUEST['mass'])) {
            foreach (array_unique($_REQUEST['mass']) as $record) {
                $button .= "<input style='display: none' checked type='checkbox' name='mass[]' value='{$record}'>\n";
            }
        }
        $button .= "<input type='hidden' name='query' value='true'>";
        $button .= "<input type='hidden' name='saved_associated_data' value=''>";
        $button .= "<input type='hidden' name='close_window' value='true'>";
        $button .= "<input type='hidden' name='html' value='change_address'>";
        $button .= "<input type='hidden' name='account_name' value='{$account_name}'>";
        // Added ID attribute to each element to use getElementById. To give ID attribute to an element is a good practice.
        $button .= "<span style='display: none'><textarea name='primary_address_street' id='primary_address_street'>" . str_replace("&lt;br&gt;", "\n", $_REQUEST["primary_address_street"]) . "</textarea></span>";
        $button .= "<input type='hidden' name='primary_address_city' id='primary_address_city' value='" . $_REQUEST["primary_address_city"] . "'>";
        $button .= "<input type='hidden' name='primary_address_state' id='primary_address_state' value='" . $_REQUEST["primary_address_state"] . "'>";
        $button .= "<input type='hidden' name='primary_address_postalcode' id='primary_address_postalcode' value='" . $_REQUEST["primary_address_postalcode"] . "'>";
        $button .= "<input type='hidden' name='primary_address_country' id='primary_address_country' value='" . $_REQUEST["primary_address_country"] . "'>";
        // Adding an onclick event to remove address for alternate address, as user has selected copy address to primary address
        $button .= "<input title='" . $mod_strings['LBL_COPY_ADDRESS_CHECKED_PRIMARY'] . "'  class='button' LANGUAGE=javascript type='submit' name='button' value='  " . $mod_strings['LBL_COPY_ADDRESS_CHECKED_PRIMARY'] . "  ' onclick='clearAddress(\"alt\");'>\n";
        // Adding a new block of code copy the address to alternate address for contacts
        $button .= "<span style='display: none'><textarea name='alt_address_street' id='alt_address_street'>" . str_replace("&lt;br&gt;", "\n", $_REQUEST["primary_address_street"]) . "</textarea></span>";
        $button .= "<input type='hidden' name='alt_address_city' id='alt_address_city' value='" . $_REQUEST["primary_address_city"] . "'>";
        $button .= "<input type='hidden' name='alt_address_state' id='alt_address_state' value='" . $_REQUEST["primary_address_state"] . "'>";
        $button .= "<input type='hidden' name='alt_address_postalcode' id='alt_address_postalcode' value='" . $_REQUEST["primary_address_postalcode"] . "'>";
        $button .= "<input type='hidden' name='alt_address_country' id='alt_address_country' value='" . $_REQUEST["primary_address_country"] . "'>";
        // Adding an onclick event to remove address for primary address, as user has selected copy address to alternate address
        // NOTE => You need to change the label as as per SugarCRM way..
        $button .= "<input title='" . $mod_strings['LBL_COPY_ADDRESS_CHECKED_ALT'] . "'  class='button' LANGUAGE=javascript type='submit' name='button' value='  " . $mod_strings['LBL_COPY_ADDRESS_CHECKED_ALT'] . "  ' onclick='clearAddress(\"primary\");'>\n";
        $button .= "<input title='" . $app_strings['LBL_CANCEL_BUTTON_TITLE'] . "' accessKey='" . $app_strings['LBL_CANCEL_BUTTON_KEY'] . "' class='button' LANGUAGE=javascript onclick=\"window.close()\" type='submit' name='button' value='  " . $app_strings['LBL_CANCEL_BUTTON_LABEL'] . "  '>\n";
        ob_start();
        insert_popup_header($theme);
        $output_html .= ob_get_contents();
        ob_end_clean();
        // Reset the sections that are already in the page so that they do not print again later.
        $form->reset('main.SearchHeader');
        // create the listview
        $seed_bean = new Contact();
        $ListView = new ListView();
        $ListView->show_export_button = false;
        $ListView->process_for_popups = true;
        $ListView->show_delete_button = false;
        $ListView->show_select_menu = false;
        $ListView->setXTemplate($form);
        $ListView->setHeaderTitle($mod_strings['LBL_LIST_FORM_TITLE']);
        $ListView->setHeaderText($button);
        $ListView->setQuery($where, '', '', 'CONTACT');
        $ListView->setModStrings($mod_strings);
        ob_start();
        $ListView->processListViewMulti($seed_bean, 'main', 'CONTACT');
        $output_html .= ob_get_contents();
        ob_end_clean();
        // Regular Expression to override sListView
        $exp = '/sListView.save_checks/si';
        $change = 'save_checks';
        $output_html = preg_replace(array($exp), array($change), $output_html);
        $output_html .= <<<EOJS
        <script type="text/javascript">
        <!--
        // Function to clear address according to the buttons clicked.
        function clearAddress(key)
        {
            document.getElementById(key+"_address_street").value = "";
            document.getElementById(key+"_address_city").value = "";
            document.getElementById(key+"_address_state").value = "";
            document.getElementById(key+"_address_postalcode").value = "";
            document.getElementById(key+"_address_country").value = "";
        }
        checked_items = Array();
        inputs_array = document.MassUpdate.elements;
        
        for(wp = 0 ; wp < inputs_array.length; wp++) {
            if(inputs_array[wp].name == "mass[]" && inputs_array[wp].style.display == "none") {
                checked_items.push(inputs_array[wp].value);
            } 
        }
        for(i in checked_items) {
            for(wp = 0 ; wp < inputs_array.length; wp++) {
                if(inputs_array[wp].name == "mass[]" && inputs_array[wp].value == checked_items[i]) {
                    inputs_array[wp].checked = true;
                }
            }
        }
        -->
        </script>
EOJS;
        $output_html .= insert_popup_footer();
        return $output_html;
    }
    /**
     *
     */
    function process_page_for_address()
    {
        global $theme;
        global $mod_strings;
        global $app_strings;
        global $currentModule;
        global $sugar_version, $sugar_config;
        $output_html = '';
        $where = '';
        $where = $this->_get_where_clause();
        $image_path = 'themes/' . $theme . '/images/';
        $formBase = new ContactFormBase();
        if (isset($_REQUEST['doAction']) && $_REQUEST['doAction'] == 'save') {
            $formBase->handleSave('', false, true);
        }
        $first_name = empty($_REQUEST['first_name']) ? '' : $_REQUEST['first_name'];
        $last_name = empty($_REQUEST['last_name']) ? '' : $_REQUEST['last_name'];
        $account_name = empty($_REQUEST['account_name']) ? '' : $_REQUEST['account_name'];
        $request_data = empty($_REQUEST['request_data']) ? '' : $_REQUEST['request_data'];
        $hide_clear_button = empty($_REQUEST['hide_clear_button']) ? false : true;
        $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
        $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
        $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
        // TODO: cleanup the construction of $addform
        $formbody = $formBase->getFormBody('', '', 'EmailEditView');
        $addform = '<table><tr><td nowrap="nowrap" valign="top">' . str_replace('<br>', '</td><td nowrap="nowrap" valign="top">&nbsp;', $formbody) . '</td></tr></table>' . '<input type="hidden" name="action" value="Popup" />';
        $formSave = <<<EOQ
\t\t<input type="submit" name="button" class="button" title="{$lbl_save_button_title}" accesskey="{$lbl_save_button_key}" value="  {$lbl_save_button_label}  " />
\t\t<input type="button" name="button" class="button" title="{$app_strings['LBL_CANCEL_BUTTON_TITLE']}" accesskey="{$app_strings['LBL_CANCEL_BUTTON_KEY']}" value="{$app_strings['LBL_CANCEL_BUTTON_LABEL']}" onclick="toggleDisplay('addform');" />
EOQ;
        $createContact = <<<EOQ
\t\t<input type="button" name="showAdd" class="button" value="{$mod_strings['LNK_NEW_CONTACT']}" onclick="toggleDisplay('addform');" />
EOQ;
        $addformheader = get_form_header($mod_strings['LNK_NEW_CONTACT'], $formSave, false);
        $button = "<form action='index.php' method='post' name='form' id='form'>\n";
        if (!$hide_clear_button) {
            $button .= "<input type='button' name='button' class='button' onclick=\"send_back('','');\" title='" . $app_strings['LBL_CLEAR_BUTTON_TITLE'] . "' accesskey='" . $app_strings['LBL_CLEAR_BUTTON_KEY'] . "' value='  " . $app_strings['LBL_CLEAR_BUTTON_LABEL'] . "  ' />\n";
        }
        $button .= "<input type='submit' name='button' class='button' onclick=\"window.close();\" title='" . $app_strings['LBL_CANCEL_BUTTON_TITLE'] . "' accesskey='" . $app_strings['LBL_CANCEL_BUTTON_KEY'] . "' value='  " . $app_strings['LBL_CANCEL_BUTTON_LABEL'] . "  ' />\n";
        $button .= "</form>\n";
        $form = new XTemplate('modules/Contacts/Address_picker.html');
        $form->assign('MOD', $mod_strings);
        $form->assign('APP', $app_strings);
        //$form->assign('CREATECONTACT', $createContact);
        $form->assign('ADDFORMHEADER', $addformheader);
        $form->assign('ADDFORM', $addform);
        $form->assign('THEME', $theme);
        $form->assign('MODULE_NAME', $currentModule);
        $form->assign('FIRST_NAME', $first_name);
        $form->assign('LAST_NAME', $last_name);
        $form->assign('ACCOUNT_NAME', $account_name);
        $form->assign('request_data', $request_data);
        // fill in for mass update
        $button = "<input type='hidden' name='module' value='Contacts'><input type='hidden' id='form_action' name='action' value='index'><input type='hidden' name='massupdate' value='true'><input type='hidden' name='delete' value='false'><input type='hidden' name='mass' value='Array'><input type='hidden' name='Update' value='Update'>";
        if (isset($_REQUEST['mass']) && is_array($_REQUEST['mass'])) {
            foreach (array_unique($_REQUEST['mass']) as $record) {
                $button .= "<input style='display: none' checked type='checkbox' name='mass[]' value='{$record}'>\n";
            }
        }
        $button .= "<input type='hidden' name='saved_associated_data' value=''>";
        $button .= "<input type='hidden' name='query' value='true'>";
        $button .= "<input type='hidden' name='close_window' value='true'>";
        $button .= "<input type='hidden' name='html' value='change_address'>";
        $button .= "<input type='hidden' name='account_name' value='{$account_name}'>";
        $button .= "<span style='display: none'><textarea name='primary_address_street'>" . str_replace("&lt;br&gt;", "\n", $_REQUEST["primary_address_street"]) . "</textarea></span>";
        $button .= "<input type='hidden' name='primary_address_city' value='" . $_REQUEST["primary_address_city"] . "'>";
        $button .= "<input type='hidden' name='primary_address_state' value='" . $_REQUEST["primary_address_state"] . "'>";
        $button .= "<input type='hidden' name='primary_address_postalcode' value='" . $_REQUEST["primary_address_postalcode"] . "'>";
        $button .= "<input type='hidden' name='primary_address_country' value='" . $_REQUEST["primary_address_country"] . "'>";
        $button .= "<input type='hidden' name='Contacts_CONTACT_offset' value=''>";
        $button .= "<input title='" . $mod_strings['LBL_COPY_ADDRESS_CHECKED'] . "'  class='button' LANGUAGE=javascript type='submit' name='button' value='  " . $mod_strings['LBL_COPY_ADDRESS_CHECKED'] . "  '>\n";
        $button .= "<input title='" . $app_strings['LBL_CANCEL_BUTTON_TITLE'] . "' accessKey='" . $app_strings['LBL_CANCEL_BUTTON_KEY'] . "' class='button' LANGUAGE=javascript onclick=\"window.close()\" type='submit' name='button' value='  " . $app_strings['LBL_CANCEL_BUTTON_LABEL'] . "  '>\n";
        ob_start();
        insert_popup_header($theme);
        $output_html .= ob_get_contents();
        ob_end_clean();
        //$output_html .= get_form_header($mod_strings['LBL_SEARCH_FORM_TITLE'], '', false);
        //$form->parse('main.SearchHeader');
        //$output_html .= $form->text('main.SearchHeader');
        $output_html .= get_form_footer();
        // Reset the sections that are already in the page so that they do not print again later.
        $form->reset('main.SearchHeader');
        // create the listview
        $seed_bean = new Contact();
        $ListView = new ListView();
        $ListView->show_export_button = false;
        $ListView->setXTemplate($form);
        $ListView->setHeaderTitle($mod_strings['LBL_LIST_FORM_TITLE']);
        $ListView->setHeaderText($button);
        $ListView->setQuery($where, '', '', 'CONTACT');
        $ListView->setModStrings($mod_strings);
        // Added
        $ListView->process_for_popups = true;
        ob_start();
        $ListView->processListViewMulti($seed_bean, 'main', 'CONTACT');
        $output_html .= ob_get_contents();
        ob_end_clean();
        // The following is a regular expression filter that will convert the nested <a>...</a> tags
        // for the Next and Previous button links.  The regular expression has 7 placeholder fields
        // Given sample HTML as follows
        /* -------------------------------------
                <a href="/maint_4_5_0/index.php?module=Contacts&action=Popup&query=true&account_name=360 Vacations 723351&html=change_address&primary_address_street=111 Silicon Valley Road&primary_address_city=Ohio&primary_address_state=CA&primary_address_postalcode=53430&primary_address_country=USA&mode=undefined&create=undefined&metadata=undefined&Contacts_CONTACT_offset=20" onclick="javascript:return sListView.save_checks(20, \'Contacts_CONTACT_offset\')" class="listViewPaginationLinkS1">Next&nbsp;
                <img src=\'themes/GoldenGate/images/next.gif\' width=\'8\' height=\'11\' alt=\'Next\'  border=\'0\' align=\'absmiddle\'></a>
                &nbsp;&nbsp;
                <a href="/maint_4_5_0/index.php?module=Contacts&action=Popup&query=true&account_name=360 Vacations 723351&html=change_address&primary_address_street=111 Silicon Valley Road&primary_address_city=Ohio&primary_address_state=CA&primary_address_postalcode=53430&primary_address_country=USA&mode=undefined&create=undefined&metadata=undefined&Contacts_CONTACT_offset=-100" onclick="javascript:return sListView.save_checks(980, \'Contacts_CONTACT_offset\')" class="listViewPaginationLinkS1">End&nbsp;
                <img src=\'themes/GoldenGate/images/end.gif\' width=\'13\' height=\'11\' alt=\'End\'  border=\'0\' align=\'absmiddle\'>
                </a>
                --------------------------------------
                The resulting preg_replace call should create Array of matches:
        	    [1] => Array
        	        (
        	            [0] => 20
        	        )
        	
        	    [2] => Array
        	        (
        	            [0] => Next&nbsp;
        	        )
        	
        	    [3] => Array
        	        (
        	            [0] => <img src='themes/GoldenGate/images/next.gif' width='8' height='11' alt='Next'  border='0' align='absmiddle'>
        	        )
        	
        	    [4] => Array
        	        (
        	            [0] => &nbsp;&nbsp;
        	        )
        	
        	    [5] => Array
        	        (
        	            [0] => 980
        	        )
        	
        	    [6] => Array
        	        (
        	            [0] => End&nbsp;
        	        )
        	
        	    [7] => Array
        	        (
        	            [0] => <img src='themes/GoldenGate/images/end.gif' width='13' height='11' alt='End'  border='0' align='absmiddle'>
        	        )
        	    */
        $exp = '/<a.*?checks\\((\\d+),.*?>(.*?)(<img.*>).*<\\/a>(.*?)<a.*?checks\\((\\d+),.*?>(.*?)(<img.*>).*<\\/a>/Ui';
        $change = '<a class="listViewPaginationLinkS1" onclick="javascript:save_checks(${1}, \'Contacts_CONTACT_offset\');" href="#">
        ${2}${3}</a>${4}<a class="listViewPaginationLinkS1" onclick="javascript:save_checks(${5}, \'Contacts_CONTACT_offset\');" href="#">${6}${7}</a>';
        $output_html = preg_replace(array($exp), array($change), $output_html);
        $output_html .= '<script>		
		checked_items = Array();
		inputs_array = document.MassUpdate.elements;

		for(wp = 0 ; wp < inputs_array.length; wp++) {
			if(inputs_array[wp].name == "mass[]" && inputs_array[wp].style.display == "none") {
				checked_items.push(inputs_array[wp].value);
			} 
		}
		for(i in checked_items) {
			for(wp = 0 ; wp < inputs_array.length; wp++) {
				if(inputs_array[wp].name == "mass[]" && inputs_array[wp].value == checked_items[i]) {
					inputs_array[wp].checked = true;
				}
			}
		}



        </script>';
        $output_html .= get_form_footer();
        $output_html .= insert_popup_footer();
        return $output_html;
    }