<?php
/**
* @package		Contact Enhanced
* @copyright	Copyright (C) 2006 - 2010 Ideal Custom Software Development. All rights reserved.
 * @author     Douglas Machado {@link http://ideal.fok.com.br}
 * @author     Created on 28-Jul-09
 * @license		GNU/GPL, see license.txt
 * Contact Enhanced  is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * @license		GNU/GPL, see license.txt
 * Contact Enhanced  is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses. */


// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class HTML_contact
{
	function showContacts( &$rows, &$pageNav, $option, &$lists, &$totalCategories )
	{
		$user =& JFactory::getUser();
		
		if($pageNav->total == 0 OR $totalCategories == 0){
			$html	= '';
			$html .= '<fieldset class="adminform"><legend>'.JText::_('Next Step').'</legend>';
			//echo $totalCategories; exit;
			if($totalCategories == 0){
				JHTML::_('behavior.mootools');
				JHTML::_('behavior.modal', 'a.modal');
				
				$html .= '<ul><li><a href="index.php?option=com_categories&section=com_contact_enhanced_details" 
							rel="{handler: \'iframe\', size: {x: 1000, y: 500}}" class="modal" title="'.JText::_('Create a category').'">'.JText::_('Create a category').'</a></li></ul>';
				$html .= JText::_('OR');
			}
			if($pageNav->total == 0){
				$html .= '<ul><li><a href="index.php?option=com_contact_enhanced&task=import" title="Import contacts from Joomla Default Contact Component">'.JText::_('Import contacts and contact categories from Joomla! standard contact manager (com_contact)').'</a></li></ul>';
			}	
			$html .= '</fieldset>';
			
			echo $html;
		}
		//Ordering allowed ?
		$ordering = ($lists['order'] == 'cd.ordering');

		JHTML::_('behavior.tooltip');
		?>
		<form action="index.php?option=com_contact_enhanced" method="post" name="searchForm">

		<table>
		<tr>
			<td align="left" width="100%">
				<?php echo JText::_( 'Filter' ); ?>:
				<input type="text" name="search" id="search" value="<?php echo $lists['search'];?>" class="text_area" onchange="document.searchForm.submit();" />
				<button onclick="this.form.submit();"><?php echo JText::_( 'Go' ); ?></button>
				<button onclick="document.getElementById('search').value='';this.form.getElementById('filter_catid').value='0';this.form.getElementById('filter_state').value='';this.form.submit();"><?php echo JText::_( 'Reset' ); ?></button>
			</td>
			<td nowrap="nowrap">
				<?php
				echo $lists['catid'];
				echo $lists['state'];
				?>
			</td>
		</tr>
		</table>
		<input type="hidden" name="option" value="com_contact_enhanced" />
		<input type="hidden" name="task" value="showContacts" />
		</form>
		<form action="index.php?option=com_contact_enhanced" method="post" name="adminForm">
			<table class="adminlist">
			<thead>
				<tr>
					<th width="10">
						<?php echo JText::_( 'Num' ); ?>
					</th>
					<th width="10" class="title">
						<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($rows); ?>);" />
					</th>
					<th class="title">
						<?php echo JHTML::_('grid.sort',   'Name', 'cd.name', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th width="5%" class="title" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Published', 'cd.published', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th nowrap="nowrap" width="8%">
						<?php echo JHTML::_('grid.sort',   'Order by', 'cd.ordering', @$lists['order_Dir'], @$lists['order'] ); ?>
						<?php echo JHTML::_('grid.order',  $rows ); ?>
					</th>
					<th width="8%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Access', 'cd.access', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th width="10%" class="title">
						<?php echo JHTML::_('grid.sort',   'Category', 'category', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th class="title" nowrap="nowrap" width="10%">
						<?php echo JHTML::_('grid.sort',   'Linked to User', 'user', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
					<th width="1%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'ID', 'cd.id', @$lists['order_Dir'], @$lists['order'] ); ?>
					</th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<td colspan="11">
						<?php echo $pageNav->getListFooter(); ?>
					</td>
				</tr>
			</tfoot>
			<tbody>
			<?php
			$k = 0;
			for ($i=0, $n=count($rows); $i < $n; $i++) {
				$row = $rows[$i];

				$link 		= JRoute::_( 'index.php?option=com_contact_enhanced&task=edit&cid[]='. $row->id );

				$checked 	= JHTML::_('grid.checkedout',   $row, $i );
				$access 	= JHTML::_('grid.access',   $row, $i );
				$published 	= JHTML::_('grid.published', $row, $i );

				$row->cat_link 	= JRoute::_( 'index.php?option=com_categories&section=com_contact_enhanced_details&task=edit&type=other&cid[]='. $row->catid );
				$row->user_link	= JRoute::_( 'index.php?option=com_users&task=editA&cid[]='. $row->user_id );
				?>
				<tr class="<?php echo "row$k"; ?>">
					<td>
						<?php echo $pageNav->getRowOffset( $i ); ?>
					</td>
					<td>
						<?php echo $checked; ?>
					</td>
					<td>
						<span class="editlinktip hasTip" title="<?php echo JText::_( 'Edit Contact' );?>::<?php echo $row->name; ?>">
						<a href="<?php echo $link; ?>">
							<?php echo $row->name; ?></a> </span>
					</td>
					<td align="center">
						<?php echo $published;?>
					</td>
					<td class="order">
						<span><?php echo $pageNav->orderUpIcon( $i, ( $row->catid == @$rows[$i-1]->catid ), 'orderup', 'Move Up', $ordering ); ?></span>
						<span><?php echo $pageNav->orderDownIcon( $i, $n, ( $row->catid == @$rows[$i+1]->catid ), 'orderdown', 'Move Down', $ordering ); ?></span>
						<?php $disabled = $ordering ?  '' : 'disabled="disabled"'; ?>
						<input type="text" name="order[]" size="5" value="<?php echo $row->ordering;?>" <?php echo $disabled ?> class="text_area" style="text-align: center" />
					</td>
					<td align="center">
						<?php echo $access;?>
					</td>
					<td>
						<a href="<?php echo $row->cat_link; ?>" title="<?php echo JText::_( 'Edit Category' ); ?>">
							<?php echo $row->category; ?></a>
					</td>
					<td>
						<a href="<?php echo $row->user_link; ?>" title="<?php echo JText::_( 'Edit User' ); ?>">
							<?php echo $row->user; ?></a>
					</td>
					<td align="center">
						<?php echo $row->id; ?>
					</td>
				</tr>
				<?php
				$k = 1 - $k;
			}
			?>
			</tbody>
			</table>

		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="filter_order" value="<?php echo $lists['order']; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $lists['order_Dir']; ?>" />
		<?php echo JHTML::_( 'form.token' ); ?>
		</form>
		<?php
	}
	
	
	function showImportBox(){
		$html	= '';
		$html .= '<fieldset class="adminform"><legend>'.JText('Next Step').'</legend>';
			$html .= '';
		$html .= '</fieldset>';
		
	}
	
	
	function editContact( &$row, &$lists, $option, &$params, &$config ) {
		global $mainframe;
		
		JRequest::setVar( 'hidemainmenu', 1 );

		if ($row->image == '') {
			$row->image = 'blank.png';
		}
		require_once( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_contact_enhanced'.DS."configuration.php" );
		$config	= new CEConfig();
		

		JHTML::_('behavior.tooltip');
		jimport('joomla.html.pane');
        // TODO: allowAllClose should default true in J!1.6, so remove the array when it does.
		$pane = &JPane::getInstance('sliders', array('allowAllClose' => true));

		JFilterOutput::objectHTMLSafe( $row, ENT_QUOTES, array('misc','sidebar') );
		$cparams = JComponentHelper::getParams ('com_media');
		?>
		<script language="javascript" type="text/javascript">
		<!--
		function submitbutton(pressbutton) {
			var form = document.adminForm;
			if (pressbutton == 'cancel') {
				submitform( pressbutton );
				return;
			}

			// do field validation
			if ( form.name.value == "" ) {
				alert( "<?php echo JText::_( 'You must provide a name.', true ); ?>" );
			} else if ( form.catid.value == 0 ) {
				alert( "<?php echo JText::_( 'Please select a Category.', true ); ?>" );
			} else if ( form.email_to.value == '' &&  $('user_id').value == 0) {
				alert( "<?php echo JText::_( 'Please either link the form to a user or enter an email', true ); ?>" );
			} else {
				submitform( pressbutton );
			}
		}
		//-->
		</script>

	<form action="index.php" method="post" name="adminForm">
		
		<div class="col width-60">
			<fieldset class="adminform">
				<legend><?php echo JText::_( 'Contact Details' ); ?></legend>
		<?php 
		echo $pane->startPane("ce-contact-info");
		echo $pane->startPanel( JText::_('Info (Publishing)'),  'contact-details');
		?>
							<table class="admintable">
				<tr>
					<td class="key">
						<label for="name">
							<?php echo JText::_( 'Name' ); ?>:
						</label>
					</td>
					<td >
						<input class="inputbox" type="text" name="name" id="name" size="60" maxlength="255" value="<?php echo $row->name; ?>" />
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="name">
							<?php echo JText::_( 'Alias' ); ?>:
						</label>
					</td>
					<td >
						<input class="inputbox" type="text" name="alias" id="alias" size="60" maxlength="255" value="<?php echo $row->alias; ?>" />
					</td>
				</tr>
				<tr>
					<td class="key">
						<?php echo JText::_( 'Published' ); ?>:
					</td>
					<td>
						<?php echo $lists['published']; ?>
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="catid">
							<?php echo JText::_( 'Category' ); ?>:
						</label>
					</td>
					<td>
						<?php echo $lists['catid'];?>
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="user_id">
							<?php echo JText::_( 'Linked to User' ); ?>:
						</label>
					</td>
					<td >
						<?php echo $lists['user_id'];?>
					</td>
				</tr>
				<tr>
					<td valign="top" class="key">
						<label for="ordering">
							<?php echo JText::_( 'Ordering' ); ?>:
						</label>
					</td>
					<td>
						<?php echo $lists['ordering']; ?>
					</td>
				</tr>
				<tr>
					<td valign="top" class="key">
						<label for="access">
							<?php echo JText::_( 'Access' ); ?>:
						</label>
					</td>
					<td>
						<?php echo $lists['access']; ?>
					</td>
				</tr>
				<?php
				if ($row->id) {
					?>
					<tr>
						<td class="key">
							<label>
								<?php echo JText::_( 'ID' ); ?>:
							</label>
						</td>
						<td>
							<strong><?php echo $row->id;?></strong>
						</td>
					</tr>
					<?php
				}
				?>
				</table>
		<?php 
		echo $pane->endPanel();
		echo $pane->startPanel( JText::_('Info (Contact)'),  'contact-main-info');
		?>
			<table class="admintable">
				<tr>
					<td class="key">
					<label for="con_position">
						<?php echo JText::_( 'Contact\'s Position' ); ?>:
						</label>
					</td>
					<td>
						<input class="inputbox" type="text" name="con_position" id="con_position" size="60" maxlength="255" value="<?php echo $row->con_position; ?>" />
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="email_to">
							<?php echo JText::_( 'E-mail' ); ?>:
						</label>
					</td>
					<td>
						<input class="inputbox" type="text" name="email_to" id="email_to" size="60" maxlength="255" value="<?php echo $row->email_to; ?>" />
					</td>
				</tr>
				<tr>
					<td class="key" valign="top">
						<label for="address">
							<?php echo JText::_( 'Street Address' ); ?>:
							</label>
						</td>
						<td>
 							<textarea name="address" id="address" rows="3" cols="45" class="inputbox address"><?php echo $row->address; ?></textarea>
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="suburb">
							<?php echo JText::_( 'Town/Suburb' ); ?>:
						</label>
					</td>
					<td>
						<input class="inputbox address" type="text" name="suburb" id="suburb" size="60" maxlength="100" value="<?php echo $row->suburb;?>" />
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="state">
							<?php echo JText::_( 'State/County' ); ?>:
						</label>
					</td>
					<td>
						<input class="inputbox address" type="text" name="state" id="state" size="60" maxlength="100" value="<?php echo $row->state;?>" />
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="postcode">
							<?php echo JText::_( 'Postal Code/ZIP' ); ?>:
						</label>
					</td>
					<td>
						<input class="inputbox address" type="text" name="postcode" id="postcode" size="60" maxlength="100" value="<?php echo $row->postcode; ?>" />
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="country">
							<?php echo JText::_( 'Country' ); ?>:
						</label>
					</td>
					<td>
						<input class="inputbox address" type="text" name="country" id="country" size="60" maxlength="100" value="<?php echo $row->country;?>" />
					</td>
				</tr>
				<tr>
					<td class="key" valign="top">
					<label for="telephone">
					<?php echo JText::_( 'Telephone' ); ?>:
						</label>
					</td>
					<td>
						<input class="inputbox" type="text" name="telephone" id="telephone" size="60" maxlength="255" value="<?php echo $row->telephone; ?>" />
  					</td>
				</tr>
				<tr>
					<td class="key" valign="top">
						<label for="mobile">
							<?php echo JText::_( 'Mobile' ); ?>:
						</label>
					</td>
					<td>
 						<input class="inputbox" type="text" name="mobile" id="mobile" size="60" maxlength="255" value="<?php echo $row->mobile; ?>" />
					</td>
				</tr>
				<tr>
					<td class="key" valign="top">
						<label for="fax">
							<?php echo JText::_( 'Fax' ); ?>:
						</label>
					</td>
					<td>
						<input class="inputbox" type="text" name="fax" id="fax" size="60" maxlength="255" value="<?php echo $row->fax; ?>" />
 					</td>
				</tr>
				<tr>
					<td class="key" valign="top">
						<label for="skype">
							<?php echo JText::_( 'Skype' ); ?>:
						</label>
					</td>
					<td>
 						<input class="inputbox" type="text" name="skype" id="skype" size="60" maxlength="255" value="<?php echo $row->skype; ?>" />
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="webpage">
							<?php echo JText::_( 'Webpage' ); ?>:
						</label>
					</td>
					<td>
						<input class="inputbox" type="text" name="webpage" id="webpage" size="60" maxlength="255" value="<?php echo $row->webpage; ?>" />
					</td>
				</tr>
			</table>
		<?php 
		echo $pane->endPanel();
		echo $pane->startPanel( JText::_('Info (Extra fields)'),  'contact-custom-fields');
			
			echo '<div>';
				echo '<strong>'.JText::_('CE_EXTRA_FIELDS_DESC').'</strong>';
			echo '</div>';
			echo '<table  class="admintable" style="width:100%">';
			for($custField = 1;$custField <=10;$custField++){
				$extrafield	= 'extra_field_'.$custField;
					echo '<tr>';
						echo '<td class="key">';
							echo '<label for="'.$extrafield	.'" title="'.JText::_('Extra field').' '.$custField.'">';
								echo JText::_( 'EXTRA_FIELD_'.$custField ).':';
							echo '</label>';
						echo '</td>';
						echo '<td>';
							echo '<textarea rows="2" class="inputbox textarea" name="'.$extrafield.'" id="'.$extrafield.'" style="width:95%" >'.$row->$extrafield.'</textarea>';
						echo '</td>';
					echo '</tr>';
			}
			echo '</table>';
		
		echo $pane->endPanel();
		echo $pane->startPanel( JText::_('Info (Miscellaneous)'),  'contact-misc-info');
		?>
			<?php echo '<strong>'.JText::_( 'This is shown on the top of the form' ).'</strong>'; ?>
			<br />
			<?php 
			if ($params->get('showeditor',1) == 1){
				$editor = &JFactory::getEditor();
				// parameters : areaname, content, width, height, cols, rows
				echo $editor->display( 'misc',  $row->misc , '90%', '200', '75', '20' ) ;
			}else{ ?>
				<textarea name="misc" id="misc" rows="5" cols="45" class="inputbox"><?php echo $row->misc; ?></textarea>
		<?php 	
			}
		echo $pane->endPanel();

		echo $pane->startPanel( JText::_('Sidebar'),  'contact-sidebar');
			echo '<strong>'.JText::_( 'This is shown either on the left or right. Choose its postion on the Advanced Parameters' ).'</strong>'; 
			echo '<br />';
			$editor = &JFactory::getEditor();
			// parameters : areaname, content, width, height, cols, rows
			echo $editor->display( 'sidebar',  $row->sidebar , '90%', '200', '75', '20' ) ;	
			
		echo $pane->endPanel();
		?>
		<?php 
		echo $pane->startPanel( JText::_('Image'),  'contact-image'); 		?>
			<div style="height:400px">
				<?php echo '<div style="padding:10px">'. $lists['image'].'</div>'; ?>
				<div>
					<script language="javascript" type="text/javascript">
						if (document.forms.adminForm.image.options.value!=''){
							jsimg='../<?php echo $config->image_path; ?>' + getSelectedValue( 'adminForm', 'image' );
						} else {
							jsimg='../images/M_images/blank.png';
						}
						document.write('<img src=' + jsimg + ' name="imagelib" border="2" alt="<?php echo JText::_( 'Preview' ); ?>" />');
					</script>
				</div>
			</div>

			
		<?php 
		echo $pane->endPanel();
	echo $pane->endPane();
		?>
			</fieldset>
		</div>
	
		<div class="col width-40">
			<fieldset class="adminform">
				<legend><?php echo JText::_( 'Parameters' ); ?></legend>

				<?php
					echo $pane->startPane("menu-pane");
						if($params->get('show_map',1)){
						echo $pane->startPanel(JText :: _('Maps'), "maps");
							echo '<input type="button" value="'.JText::_('Locate in map').'" onclick="ceMap.codeAddress();" />'; 
							echo ' <span id="googleaddress"></span>';
					//		echo '<div id="map_container">';
							JHTML::stylesheet( 'contact_enhanced.css', 'administrator/components/com_contact_enhanced/assets/css/' );
							require_once (JPATH_ROOT.DS.'components'.DS.'com_contact_enhanced'.DS.'helper'.DS.'gmaps.php');
		
							$map	= new GMaps($params);
						
							if($row->lat){
								$map->set('lat',(float)$row->lat);								
							}
							if($row->lng){
								$map->set('lng',(float)$row->lng);
							}
							if($row->zoom){
								$map->set('zoom',(int)$row->zoom);
							}
							
							$map->set('showCoordinates',true);
							$map->set('useDirections',	false);
							$map->set('editMode',		true);
							$map->set('companyMarkerDraggable',		true);
							if( trim($params->get('gmaps_icon'))){
								$map->set('markerImage',	JURI::root().$params->get('gmaps_icon') );
							}
							if ($params->get('gmaps_icon_shadow') ) {
								$map->set('markerShadow',JURI::root().$params->get('gmaps_icon_shadow'));
							}
							
							$map->set('input_lat',		'lat');
							$map->set('input_lng',		'lng');
							$map->set('input_zoom',		'zoom');
							$map->set('input_address',	'googleaddress');
							
							$map->set('editMode',		true);
							
							echo $map->create();
							
							$doc =& JFactory::getDocument();
		
$script	= "
window.addEvent('domready', function(){ 
	var list = $$('input.address');
	list.each(
		function(item, i) {
			item.addEvent('blur', function(e) {
					getAddress();
				}
			);
		}
	);
	getAddress();
});
function getAddress(){
	$('googleaddress').setText(
      			$('address').value.clean()
      			+', '+ $('suburb').value.clean()
      			+', '+ $('state').value.clean()
      			+', '+ $('postcode').value.clean()
      			+', '+ $('country').value.clean()
      			);
}
";
							$doc->addScriptDeclaration($script);
								
							echo $pane->endPanel();
						}
						
						echo $pane->startPanel(JText::_('Contact Parameters'), "param-page");
							echo $params->render();
						echo $pane->endPanel();
						echo $pane->startPanel(JText::_('Advanced Parameters'), "param-page");
							echo $params->render('params', 'advanced');
						echo $pane->endPanel();
						echo $pane->startPanel(JText::_('E-mail Parameters'), "param-page");
							echo $params->render('params', 'email');
						echo $pane->endPanel();
					echo $pane->endPane();
				?></fieldset>
		</div>
		<div class="clr"></div>
		
		<?php if(JRequest::getVar('author_id')):?>
			<input type="hidden" name="author_id" value="<?php echo JRequest::getVar('author_id'); ?>" />
		<?php endif; ?>
		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
		<input type="hidden" name="cid[]" value="<?php echo $row->id; ?>" />
		<input type="hidden" name="task" value="" />
		<?php echo JHTML::_( 'form.token' ); ?>
		</form>
		<?php
	}
	
	
	function doc(){
		$docsPath	= JPATH_COMPONENT.DS.'docs'.DS;
		jimport('joomla.html.pane');
		$paneParams	= array();
		$paneParams['startOffset']	= JRequest::getVar("startOffset");
		$pane =& JPane::getInstance('tabs',$paneParams);
		$docsPath	= JPATH_COMPONENT.DS.'docs'.DS;
		
		JHTML::_('behavior.mootools');
		JHTML::_('behavior.modal', 'a.modal');
		
		echo '<div>';
		echo $pane->startPane("ce-help-pane");
			
			echo $pane->startPanel( JText::_('About'),  'about-page');
				require_once($docsPath.'about.php');
			echo $pane->endPanel();
			
		echo $pane->endPane();
		echo '</div>';
			
	}


}


class HTML_customfield
{
	function showcf( &$rows, &$pageNav, $option, &$lists ) {
		$user =& JFactory::getUser();
		$task = JRequest::getVar("task");
		//Ordering allowed ?
		$ordering = ($lists['order'] == 'cf.ordering');

		JHTML::_('behavior.tooltip');
		JHTML::stylesheet( 'contact_enhanced.css', 'administrator/components/com_contact_enhanced/assets/css/' );
		$script = "window.addEvent('domready', function(){
		document.ceImportSliderBox = new Fx.Slide('topbox'); 
		document.ceImportSliderBox.toggle();
	});";
		$doc =& JFactory::getDocument();
		$doc->addScriptDeclaration($script);
		
		?>
		<div id="topbox">
			<div>
				<form enctype="multipart/form-data" action="index.php?option=com_contact_enhanced" method="post" name="import">
				<table>
				<tr>
					<td align="left" width="100%">
						<?php echo JText::_('SQL IMPORT WARNING'); ?>
					</td>
				</tr>
				<tr>
					<td align="left" width="100%">
						<label for="sql_file"> <?php echo JText::_( 'SQL file' ); ?>:</label>
						<input type="file" size="57" name="sql_file" id="sql_file" class="input_box"/>
						<input type="submit" value="<?php echo JText::_('Upload File & Install');?>" class="button"/>
					</td>
				</tr>
				</table>
				<input type="hidden" name="option" value="com_contact_enhanced" />
				<input type="hidden" name="task" value="importCF" />
				
				</form>
			</div>
		</div>
		
		<form action="index.php?option=com_contact_enhanced" method="post" name="searchForm">
		<table>
		<tr>
			<td align="left" width="100%">
				<?php echo JText::_( 'Filter' ); ?>:
				<input type="text" name="cfSearch" id="cfSearch" value="<?php echo $lists['search'];?>" class="text_area" onchange="document.searchForm.submit();" />
				<button onclick="this.form.submit();"><?php echo JText::_( 'Go' ); ?></button>
				<button onclick="document.getElementById('cfSearch').value='';this.form.getElementById('filter_catid').value='0';this.form.getElementById('filter_state').value='';this.form.submit();"><?php echo JText::_( 'Reset' ); ?></button>
			</td>
			<td nowrap="nowrap">
				<?php
				echo $lists['catid'];
				echo $lists['state'];
				?>
			</td>
		</tr>
		</table>
		<input type="hidden" name="option" value="com_contact_enhanced" />
		<input type="hidden" name="task" value="showCF" />
		</form>
		<form action="index.php?option=com_contact_enhanced" method="post" name="adminForm">

		<table class="adminlist">
			<thead>
				<tr>
					<th width="10">
						<?php echo JText::_( 'Num' ); ?>
					</th>
					<th width="10" class="title">
						<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($rows); ?>);" />
					</th>
					<th class="title"width="15%">
						<?php echo JText::_('Name'); ?>
					</th>
					<th class="title" nowrap="nowrap">
						<?php echo JText::_('Value'); ?>
					</th>
					<th width="5%" class="title" nowrap="nowrap">
						<?php echo JText::_('Published'); ?>
					</th>
					<th width="15%" class="title" nowrap="nowrap">
						<?php echo JText::_('Parameters'); ?>
					</th>
					<th nowrap="nowrap" width="11%">
						<?php echo JText::_('Order by'); ?>
						<?php echo JHTML::_('grid.order',  $rows,'filesave.png', 'saveorderCF' ); ?>
					</th>
					<th width="10%" class="title">
						<?php echo JText::_('Category' ); ?>
					</th>
					<th width="1%" nowrap="nowrap">
						<?php echo JText::_('ID'); ?>
					</th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<td colspan="11">
						<?php echo $pageNav->getListFooter(); ?>
					</td>
				</tr>
			</tfoot>
			<tbody>
		<?php
		$k = 0;
		for ($i=0, $n=count($rows); $i < $n; $i++) {
			$row = $rows[$i];
			JFilterOutput::objectHTMLSafe( $row );
	
			$link 		= JRoute::_( 'index.php?option=com_contact_enhanced&task=editCF&cid[]='. $row->id );
			$published 	= JHTML::_('grid.published', $row, $i,'tick.png','publish_x.png','CF' );
			$checked 	= JHTML::_('grid.id',  $i, $row->id );
			
			$row->cat_link 	= JRoute::_( 'index.php?option=com_categories&section=com_contact_enhanced_details&task=edit&type=other&cid[]='. $row->catid );
			
			?>
			<tr class="<?php echo "row$k"; ?>">
				<td>
				<?php echo $pageNav->getRowOffset( $i ); ?>
				</td>
				<td>
				<?php echo $checked; ?>
				</td>
				<td><span class="editlinktip hasTip" title="<?php echo JText::_( 'Edit Custom Field' );?>::<?php echo $row->name; ?>">
						<a href="<?php echo $link; ?>">
							<?php echo $row->name; ?></a> <?php if($row->required ==1) echo '<small>Required</small>';?></span>
				</td>
				<td><?php echo $row->value; ?></td>
				<td align="center">
					<?php echo $published;?>
				</td>
				<td><?php echo $row->attributes; ?></td>
				<td class="order">
					<span><?php echo $pageNav->orderUpIcon( $i, ( $row->catid == @$rows[$i-1]->catid ), 'orderupCF', 'Move Up', $ordering ); ?></span>
					<span><?php echo $pageNav->orderDownIcon( $i, $n, ( $row->catid == @$rows[$i+1]->catid ), 'orderdownCF', 'Move Down', $ordering ); ?></span>
					<?php $disabled = $ordering ?  '' : 'disabled="disabled"'; ?>
					<input type="text" name="order[]" size="5" value="<?php echo $row->ordering;?>" <?php echo $disabled ?> class="text_area" style="text-align: center" />
				</td>
				<td><?php if($row->catid != 0): ?>
						<a href="<?php echo $row->cat_link; ?>" title="<?php echo JText::_( 'Edit Category' ); ?>">
						<?php echo $row->category; ?></a>
					<?php else: ?>
						<?php echo JText::_( 'All categories' ); ?>
					<?php endif; ?>
				</td>
				<td align="center">
					<?php echo $row->id; ?>
				</td>
			</tr>
			<?php
			$k = 1 - $k;
		}
		?>
		</tbody>
		</table>
		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="task" value="showCF" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="filter_order" value="<?php echo $lists['order']; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $lists['order_Dir']; ?>" />
		<?php echo JHTML::_( 'form.token' ); ?>
		</form>
		
		<?php
	}
	
	function editCF( &$row, &$lists, $option, &$customFieldparams ) {

		JRequest::setVar( 'hidemainmenu', 1 );
		JHTML::_('behavior.tooltip');
		if ( isset($row->image) ){
			if ($row->image == '') {
				$row->image = 'blank.png';
			}
		}

		JFilterOutput::objectHTMLSafe( $row );
		?>
<script language="javascript" type="text/javascript">
<!--
function submitbutton(pressbutton) {
	var form = document.adminForm;
	if (pressbutton == 'cancelCF' || pressbutton == 'cancel') {
		submitform( pressbutton );
		return;
	}

	// do field validation
	if ( form.name.value == "" ) {
		alert( "You must provide a name." );
	} else if ( form.type.value == "" ) {
		alert( "You must set a type." );
	}else if ( form.catid.value == "" ) {
		alert( "You must select a category." );
	} else {
		submitform( pressbutton );
	}
}
function updateInputs(ftype) {
	var f = document.adminForm;
	if (ftype=='selectlist' || ftype=='recipient' || ftype=='selectmultiple' || ftype=='checkbox' 
			|| ftype=='radiobutton' || ftype=='number' || ftype=='numberrange'
			|| ftype=='freetext' 	|| ftype=='hidden' || ftype=='subject' || ftype=='sql' 
			|| ftype=='sqlmultiple' || ftype=='multitext'
		) {
		f.getElementById('value').disabled=false;
		f.getElementById('value').style.backgroundColor='#FFFFFF'; 
	} else {
		f.getElementById('value').style.backgroundColor='#f5f5f5'; 
		f.getElementById('value').disabled=true;
		f.type.value = '';
	}
}
//-->
</script>
	<form action="index.php" method="post" name="adminForm">
		<div class="col width-60">
			<fieldset class="adminform">
				<legend><?php echo JText::_( 'Custom Field Details' ); ?></legend>

				<table class="admintable" style="width:99%">
				<tr>
					<td class="key" valign="top" style="width:20%">
						<span class="editlinktip hasTip" title="<?php echo JText::_( 'CFNAME_DESCRIPTION' );?>">
							<?php echo JText::_('Name'); ?>:
						</span>
					</td>
					<td width="40%">
					<input type="text" name="name" value="<?php echo $row->name;?>" style="width:90%" />
					</td>
				</tr>
				<tr>
					<td class="key" valign="top">
					<?php echo JText::_('Field Type'); ?>:
					</td>
					<td >
					<?php echo $lists['field_type'];?>
					</td>
				</tr>
				<tr>
					<td class="key" valign="top">
					<span class="editlinktip hasTip" title="<?php echo JText::_( 'VALUE_DESCRIPTION' );?>">
						<?php echo JText::_('Value'); ?>:<br /><br />
						<?php echo JText::_('Enter elements separated by'); ?> |
					</span>
					</td>
					<td>
					<?php if($row->type == 'freetext'):?>
						<?php $editor = &JFactory::getEditor();
						echo $editor->display( 'value',  $row->value , '90%', '200', '75', '20' ) ; ?>
					<?php else:?>
						<textarea class="inputbox" name="value" id="value" style="width:90%;height:150px"><?php echo $row->value; ?></textarea>
					<?php endif; ?>
					</td>
				</tr>
				<tr>
					<td class="key" valign="top">
					<span class="editlinktip hasTip" title="<?php echo JText::_( 'TOOLTIP_DESCRIPTION' );?>"><?php echo JText::_('Tooltip'); ?>:</span>
					</td>
					<td>
					<textarea class="inputbox" name="tooltip" style="width:90%;height:80px"><?php echo $row->tooltip; ?></textarea>
					</td>
				</tr>
				<tr>
					<td class="key" valign="top">
					<span class="editlinktip hasTip" title="<?php echo JText::_( 'ATTRIBUTES_DESCRIPTION' );?>"><?php echo JText::_('Attributes'); ?>:</span>
					</td>
					<td>
					<textarea class="inputbox" name="attributes" style="width:90%;height:150px"><?php echo $row->attributes; ?></textarea>
					<br />
					<?php echo JHTML::_('link',JRoute::_('index.php?option=com_contact_enhanced&task=doc&startOffset=5'), JText::_('Attributes Help'),array('target'=>'_blank') ); ?>
					</td>
				</tr>
				</table>
				</fieldset>
			</div>
			<div class="col width-40">
				<fieldset class="adminform">
					<legend><?php echo JText::_( 'Publishing Info' ); ?></legend>
					<table class="admintable">
					<tr>
						<td class="key" valign="top">
						<?php echo JText::_('Published'); ?>:
						</td>
						<td>
						<?php echo $lists['published']; ?>
						</td>
					</tr>
					<tr>
						<td class="key" valign="top">
						<?php echo JText::_('Category'); ?>:
						</td>
						<td>
						<?php echo $lists['catid']; ?>
						</td>
					</tr>
					<tr>
						<td class="key" valign="top">
						<?php echo JText::_('Ordering'); ?>:
						</td>
						<td>
						<?php echo $lists['ordering']; ?>
						</td>
					</tr>
					<tr>
						<td class="key" valign="top">
						<?php echo JText::_('Required'); ?>:
						</td>
						<td>
						<?php echo $lists['required']; ?>
						</td>
					</tr>
					</table>
				</fieldset>
				
					<fieldset class="adminform">
						<legend><?php echo JText::_( 'Parameters' ); ?></legend>
						<?php if(!$row->id): ?>
							<?php echo JText::_("Save the Custom Field in order to view it's parameters"); ?>
						<?php elseif(isset( $customFieldparams['custom'])): ?>
							<div id="ce-cf-params-customfield"><?php echo $customFieldparams['custom']->render(); ?></div>
						<?php else:  ?>
							<?php echo JText::_('This Custom Field has no parameters'); ?>
						<?php endif; ?>
					</fieldset>
					<fieldset class="adminform">
						<legend><?php echo JText::_( 'Parameters (Default)' ); ?></legend>
							<div id="ce-cf-params-default"><?php echo $customFieldparams['default']->render(); ?></div>
					</fieldset>
				
		</div>

		<script language="Javascript">updateInputs('<?php echo $row->type; ?>');</script>
		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
		<input type="hidden" name="task" value="" />
		<?php echo JHTML::_( 'form.token' ); ?>
		</form>
	
	<?php 
	}
/**
	* Form to select Section/Category to copys item(s) to
	*/
	function copyCF( $option, $cid, $sectCatList, $items  )
	{
		?>
		<script language="javascript" type="text/javascript">
		function submitbutton(pressbutton) {
			var form = document.adminForm;
			if (pressbutton == 'cancel') {
				submitform( pressbutton );
				return;
			}

			// do field validation
			if (!getSelectedValue( 'adminForm', 'category' )) {
				alert( "<?php echo JText::_( 'Select a category to copy the Custom Fields to', true ); ?>" );
			} else {
				submitform( pressbutton );
			}
		}
		/**
		* Toggles the disable state of a group of inputs
		*
		* Inputs must have an id attribute in the form cb0, cb1...
		* @param The number of box to 'check'
		* @param An alternative field name
		*/
		function enableAll( n, fldName ) {
		  if (!fldName) {
		     fldName = 'cb';
		  }
			var f = document.adminForm;
			var c = f.toggle.checked;
			var n2 = 0;
			for (i=0; i < n; i++) {
				cb = eval( 'f.' + fldName + '' + i );
				if (cb) {
					if(c){
						cb.disabled = false;
					}else{
						cb.disabled = true;
					}
					
					n2++;
				}
			}
			if (c) {
				document.adminForm.enablestate.value = n2;
			} else {
				document.adminForm.enablestate.value = 0;
			}
		}
				
		</script>
		<form action="index.php" method="post" name="adminForm">
			<input type="hidden" name="enablestate" value="0" />
			
			<table class="adminform">
				<tr>
					<td  valign="top" width="30%">
					<strong><?php echo JText::_( 'Copy to Category' ); ?>:</strong>
					<br />
					<?php echo $sectCatList; ?>
					<br /><br />
					</td>
					<td  valign="top">
					<strong><?php echo JText::_( 'Custom fields being copied' ); ?>:</strong>
					<br />
					
					<?php
					echo '<input type="checkbox" onclick="enableAll('.count($items).');" value="" name="toggle" id="toggle"/> ';
						echo '<label for="toggle">'. JText::_( 'Edit name of fields to copy' ).'</label>';
					echo "<table>";
						echo '<tr>';
							echo '<th>'.JText::_('Name').'</th>';
							echo '<th>'.JText::_('Field type').'</th>';
						echo '</tr>';
					$i	= 0;
					foreach ( $items as $item ) {
						echo '<tr><td>'
								. '<input style="width:250px" maxlength="255" type="text" id="cb'.$i++.'" disabled name="name['.$item->id.']" value="'.$item->name.'" />'
								. "\n".'<input type="hidden" name="cid[]" value="'.$item->id.'" />'
							.'</td>';
						echo '<td>'
								. JText::_(ucfirst($item->type))
							.'</td>';
						echo '</tr>';
					}
					echo "</table>";
					?>
					</td>
				</tr>
			</table>
			<br /><br />

		<input type="hidden" name="option" value="<?php echo $option;?>" />
		<input type="hidden" name="task" value="showCF" />
		<?php echo JHTML::_( 'form.token' ); ?>
		</form>
		<?php
	}
}