<?php
/* $Id: setcompany.php 16 2006-06-08 08:31:06Z hodaka $
 *  ------------------------------------------------------------------------ *
 *                XOOPS - PHP Content Management System                      *
 *                    Copyright (c) 2000 XOOPS.org                           *
 *                       <http://www.xoops.org/>                             *
 *  ------------------------------------------------------------------------ *
 *                      MyAddress - XOOPS addressbook                        *
 *                        <http://www.kuri3.net/>                            *
 *              Copyright (c) 2004 by hodaka <hodaka@kuri3.net>              *
 * ------------------------------------------------------------------------- *
 * This program is free software; you can redistribute it and/or modify      *
 * it under the terms of the GNU General Public License as published by      *
 * the Free Software Foundation; either version 2 of the License, or         *
 * (at your option) any later version.                                       *
 *                                                                           *
 * You may not change or alter any portion of this comment or credits        *
 * of supporting developers from this source code or any supporting          *
 * source code which is considered copyrighted (c) material of the           *
 * original comment or credit authors.                                       *
 *                                                                           *
 * This program is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 * GNU General Public License for more details.                              *
 *                                                                           *
 * You should have received a copy of the GNU General Public License         *
 * along with this program; if not, write to the Free Software               *
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *
 */

include("header.php");
// include Smarty template engine and initialize it
include_once XOOPS_ROOT_PATH.'/class/template.php';
$xoopsTpl = new XoopsTpl();
$myts =& MyTextSanitizer::getInstance();

// reject no perm modified on v1.23
if( empty($xoopsModuleConfig['myaddress_guestperm']) && !is_object($xoopsUser) ) {
	redirect_header( XOOPS_URL."/user.php" , 2 , _MD_MYADDRESS_YOUMUSTREG );
	exit();
} elseif( !haveGroupPerm() ) {
	redirect_header( XOOPS_URL."/index.php", 2, _MD_MYADDRESS_YOUHAVENOPERM );
	exit();
}

$mode = 2;

$cid = isset( $_GET['cid'] ) ? $_GET['cid'] : 0;
$c_id = isset( $_GET['c_id'] ) ? $_GET['c_id'] : 0;
$from = isset( $_GET['from'] ) ? $_GET['from'] : 0;

$skey = isset($_GET['skey'])? ( is_numeric($_GET['skey'])? intval($_GET['skey']) : trim($_GET['skey'])) : "all";

// check if variable 'op' exist
// GET from myself and viewaddress.php, POST from myself in case of emplyee data maintenance
$op = isset( $_GET['op']) ? $_GET['op'] : (isset($_POST['op']) ? $_POST['op'] : "");

// Include the page header
xoops_header(false);
echo "
<SCRIPT LANGUAGE=\"JavaScript\">
<!--
function selectCompany(form) {
	var n = (form.elements.length-1)/6;
	if( n == 1 ) {
// changed at ver.1.1.3
//		opener.document.address.company.value = form.company.value;
		opener.document.getElementById('company_name').firstChild.nodeValue=form.company.value;
		opener.document.address.c_id.value = form.c_id.value;
		opener.document.address.cname.value = form.cname.value;
		opener.document.address.cdivision.value = form.cdivision.value;
		opener.document.address.cphone.value = form.cphone.value;
		opener.document.address.cfax.value = form.cfax.value;

	} else {
		for(i=0 ; i<n ; i++)	{
			if(form.c_id[i].checked) {
// changed at ver.1.1.3
//				opener.document.address.company.value = form.company[i].value;
			opener.document.getElementById('company_name').firstChild.nodeValue=form.company[i].value;
				opener.document.address.c_id.value = form.c_id[i].value;
				opener.document.address.cname.value = form.cname[i].value;
				opener.document.address.cdivision.value = form.cdivision[i].value;
				opener.document.address.cphone.value = form.cphone[i].value;
				opener.document.address.cfax.value = form.cfax[i].value;
			}
		}
	}
	opener.focus();
	window.close();
}
//-->
</SCRIPT>\n";
echo "
</HEAD>
<BODY>\n";

$xoopsTpl->assign( array(
	'lang_total' =>  _MD_MYADDRESS_TOTAL,
	'mod_url'=> $mod_url,
	'mod_copyright' => $mod_copyright,
	'mode' => $mode,
	'cid' => $cid,
	'op' => $op,
	'lang_subcategory' => _MD_MYADDRESS_SUBCATEGORY,
	'call_url' => "setcompany.php",
	'subcategories' => get_sub_categories( $mode, $cid ),
	'lang_add_data' => _MD_MYADDRESS_ADD_COMPANY,
	'add_data_url' => 'viewcompany.php?mode='.$mode.'&amp;cid='.$cid.'&amp;op=add&amp;pop=y',
	'lang_close' => _CLOSE
));

$cids = $cattree->getAllChildId( $cid );
$cids = push_gperm_array( $cids, "myadrs_category" );
array_push( $cids , $cid );
$address_total_sum = get_address_total_sum_from_cats( $mode, $cids );

$xoopsTpl->assign( 'mainpathstring', "<A HREF='setcompany.php?mode=2'>"._MD_MYADDRESS_MAIN."</A>" );
$xoopsTpl->assign( 'address_sub_title' , $cattree->getNicePathFromId( $cid , 'title' , "setcompany.php?mode=$mode" ) );

$prs = $xoopsDB->query( "SELECT COUNT(c_id) FROM $table_company WHERE cid=$cid" );
list( $address_small_sum ) = $xoopsDB->fetchRow( $prs ) ;
$xoopsTpl->assign( 'address_small_sum' , $address_small_sum ) ;
$xoopsTpl->assign( 'address_total_sum' , ( empty( $address_total_sum ) ? $address_small_sum : $address_total_sum ) );

//--------------------------------------------------------------------//
//                      show buttons for search                       //
//--------------------------------------------------------------------//

$xoopsTpl->assign( show_searchnav( $mode, $cid, "setcompany.php" ) );

//--------------------------------------------------------------------//
//                     show the results of search                     //
//--------------------------------------------------------------------//

if ( $op == "search" ) {
	include_once( "include/searchresults.php" );
	$xoopsTpl->assign( search_results( $mode, $cid, $skey ) );
} elseif ( $op == "list" && $c_id != 0) {
	include( "include/searchresults.php" );
	$xoopsTpl->assign( search_results( $mode, $cid, 0, $c_id, "n") );
}
$xoopsTpl->display('db:myaddress_get_company_in_box.html');
$xoopsTpl->display('db:myaddress_footer.html');
xoops_footer();
exit();
?>