<?php

 /**

 * @package Form Maker

 * @author Web-Dorado

 * @copyright (C) 2011 Web-Dorado. All rights reserved.

 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html

 **/


defined('JPATH_BASE') or die;



jimport('joomla.form.formfield');



/**

 * Supports a modal contact picker.

 *

 * @package		Joomla.Administrator

 * @subpackage	com_contact

 * @since		1.6

 */

class JFormFieldModal_Contacts extends JFormField

{

	/**

	 * The form field type.

	 *

	 * @var		string

	 * @since	1.6

	 */

	protected $type = 'Modal_Contacts';

	/**

	 * Method to get the field input markup.

	 *

	 * @return	string	The field input markup.

	 * @since	1.6

	 */

	protected function getInput()

	{

		// Load the javascript

		JHtml::_('behavior.framework');
		JHtml::_('behavior.modal', 'a.modal');
		JHTML::_('behavior.calendar');
		JHTML::_('script', $filename = 'calendar-setup.js', $path = 'media/system/js/', $mootools = true );	
		JHTML::stylesheet('media/system/css/calendar-jos.css');

		// Build the script.

		$script = array();

		$script[] = '	function jSelectChart(id, name, object) {';

		$script[] = '		document.id("'.$this->id.'_id").value = id;';

		$script[] = '		document.id("'.$this->id.'_name").value = name;';

		$script[] = '		
		
		document.getElementById("jform_request_dates_range").value = "";		document.getElementById("form_fields").load("index.php?option=com_formmaker&task=view_submissions&tmpl=component&form_id="+id);	
		SqueezeBox.close();';

		$script[] = '	
		
		
		}';



		// Add the script to the document head.

		JFactory::getDocument()->addScriptDeclaration(implode("\n", $script));



		// Get the title of the linked chart

		$db = JFactory::getDBO();

		$db->setQuery(

			'SELECT title' .

			' FROM #__formmaker' .

			' WHERE id = '.(int) $this->value

		);

		$title = $db->loadResult();



		if ($error = $db->getErrorMsg()) {

			JError::raiseWarning(500, $error);

		}



		if (empty($title)) {

			$title = JText::_('Select a form');

		}



		$link = 'index.php?option=com_formmaker&amp;task=element&amp;tmpl=component&amp;function=jSelectChart_'.$this->id;



		$html = "\n".'<div class="fltlft"><input type="text" id="'.$this->id.'_name" value="'.htmlspecialchars($title, ENT_QUOTES, 'UTF-8').'" disabled="disabled" /></div>';

		$html .= '<div class="button2-left"><div class="blank"><a class="modal" title="'.JText::_('Select or Change form').'"  href="'.$link.'" rel="{handler: \'iframe\', size: {x: 800, y: 450}}">'.JText::_('Select / Change').'</a></div></div>'."\n";

		// The active contact id field.

		if (0 == (int)$this->value) {

			$value = '';

		} else {

			$value = (int)$this->value;

		}



		// class='required' for client side validation

		$class = '';

		if ($this->required) {

			$class = ' class="required modal-value"';

		}



		$html .= '<input type="hidden" id="'.$this->id.'_id"'.$class.' name="'.$this->name.'" value="'.$value.'" />';
	
		$html .='<div id="form_fields"></div>';
		$html .='<script>document.getElementById("form_fields").load("index.php?option=com_formmaker&task=view_submissions&after_save=1&tmpl=component&form_id='.$value.'");</script>';

		return $html;

	}

}

