<?php
/**
* @package ccNewsletter
* @version 1.0.9
* @author  Chill Creations <info@chillcreations.com>
* @link    http://www.chillcreations.com
* @copyright Copyright (C) 2008 - 2010 Chill Creations-All rights reserved
* @copyright Copyright (C) Copyright 2010 Elodig- All rights reserved
* @license GNU/GPL, see LICENSE.php for full license.
* See COPYRIGHT.php for more copyright notices and details.

This file is part of ccNewsletter.
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.

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.
**/


// no direct access
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );
// The toolbar is already set in admin.ccnewsletter.php
// The main view
JRequest::setVar( 'hidemainmenu', 1 );
if ($this->ccdata['ajax'])
{
	?>
	<script type="text/javascript">
		// Initializes ajax
		function ajax_post(xmlHttp)
		{
			xmlHttp.open("POST", '<?php echo $this->baseurl; ?>/index.php', true);
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
			xmlHttp.send('option=com_ccnewsletter&controller=sendNewsletter&task=ccsend_all&id=<?php echo $this->ccdata['message_id']; ?>');
		}

		// Initializes ajax
		function ajax_create()
		{
			var xmlHttp;
			try
			{
				// Firefox, Opera 8.0+, Safari
				xmlHttp = new XMLHttpRequest();
			}
			catch (e)
			{
				// Internet Explorer
				try
				{
					xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e)
				{
					try
					{
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e)
					{
						throw "ai_ajax : ajax failure";
					}
				}
			}
			return xmlHttp;
		}

		// Display addresses to which th e-mail was sent and start the countdown before the next sending.
		// Called when the result of the ajax request is received (= when the previous batch sending is complete).
		function display_addresses ( responseText )
		{
			//document.getElementById('result_tbody').innerHTML += responseText;
			responseText = responseText.substr(21);
			arData=responseText.match(/<td[^>]*>(.|\n)*?(?=<\/td>)/ig);
			col = 0;
			var myRow = null;
			for(i=0,len=arData.length;i<len;i++) {
			    if(col%3 == 0) myRow = document.getElementById('result_tbody').insertRow(-1);
			    var myCell = myRow.insertCell(-1);
			    if(col%3 != 1) myCell.style.textAlign = 'center';
			    myCell.innerHTML = arData[i].replace(/<td[^>]*>/i,'');
			    col++;
			}
			if(responseText.indexOf('<input id="stop" value="stop" type="hidden" />') != -1) {
			    myRow = document.getElementById('result_tbody').insertRow(-1);
			    var myCell = myRow.insertCell(-1);
			    myCell.colSpan = arData.length;
			    myCell.innerHTML = '<input id="stop" value="stop" type="hidden" />';
			}
			document.getElementById('counter').innerHTML = <?php echo $this->ccdata['batchinterval']; ?>;
			window.setTimeout("fire_ajax_request();", 1000);
			document.getElementById('display_sending').style.display = "none";
			document.getElementById('display_countertab').style.display = "";
			document.getElementById('display_counter').style.display = "";
		}

		// Check if it is time to send a new batch. If it is not, start a one second timeout before checking again. If it is, send an ajax request to the server so that it send a nw batch.
		function fire_ajax_request()
		{
			if ( document.getElementById('stop') == null )
			{
				var timeout = document.getElementById('counter').innerHTML;
				timeout = timeout * 1; //Ensure integer
				timeout--;
				document.getElementById('counter').innerHTML = timeout;

				if (timeout <= 0)
				{
					var xmlHttp;
					try
					{
						xmlHttp = ajax_create();
					}
					catch (err)
					{
						throw "ajax failure";
					}
					xmlHttp.onreadystatechange=function()
					{
						if(xmlHttp.readyState==4)
							display_addresses(xmlHttp.responseText);
					}
					ajax_post(xmlHttp);
					document.getElementById('display_sending').style.display = "";
					document.getElementById('display_counter').style.display = "none";
					document.getElementById('display_countertab').style.display = "none";
				}
				else
					window.setTimeout("fire_ajax_request();", 1000);
			}
			else
			{
				document.getElementById('display_success').style.display = "";
				document.getElementById('display_counter').style.display = "none";
				document.getElementById('display_countertab').style.display = "none";
				document.getElementById('ajaxmsg').style.display = "none";
				document.getElementById('canceldivtag').style.display = "none";
			}
		}
	</script>
	<?php
}
//width: 280px;
?>
<form action="index.php" method="post"  name="adminForm">

	<?php if($this->ccdata['ajax']){ ?>
		<table width="100%"><tr><td align="center"><div id="ajaxmsg" style="width: 290px;background-color: #efe7b8;border-style: solid; border-color: #f0dc7e; padding:0px; text-align: center;"><img src="<?php echo JURI::root()."administrator/components/com_ccnewsletter/assets/warning.png"; ?>">&nbsp;&nbsp;<?php echo JText::_("CC_PLEASE_WAIT");?></div></td></tr></table>
		<table width="100%"><tr><td align="center"><div id="display_success" style="width: 290px;background-color: #f0ffe9;border-style: solid;border-color: #b4ffa3;display:none; color: green;  text-align: center;"><img src="<?php echo JURI::root()."administrator/components/com_ccnewsletter/assets/tick1.png"; ?>"> &nbsp; <?php echo JText::_('CC_SUCCESS'); ?> <a href="index.php?option=com_ccnewsletter&controller=newsletter"><br/><?php echo JText::_('CC_SUCCESS_CONTINUE_LINK'); ?></a></div></td></tr></table>
		<div id="display_countertab"><table width="100%"><tr><td align="center" ><div id="display_counter" style="width: 290px;background-color: #efe7b8;border-style: solid;color:red; border-color: #f0dc7e;  text-align: center;"><img src="<?php echo JURI::root()."administrator/components/com_ccnewsletter/assets/ajax-loader.gif"; ?>"> &nbsp; <?php echo JText::_( 'CC_COUNTER' ); ?> <span id="counter">0</span>  <?php echo JText::_( 'CC_SECONDS' ); ?></div></td></tr></table></div>
		<table width="100%"><tr><td align="center"><div id="display_sending" style="width: 290px;display:none;color:red;background-color: #efe7b8;border-style: solid; border-color: #f0dc7e;  text-align: center;"><img src="<?php echo JURI::root()."administrator/components/com_ccnewsletter/assets/ajax-loader.gif"; ?>"> &nbsp; <?php echo JText::_( 'CC_SENDING_BATCH' ); ?></div></td></tr></table>
	<?php }else{ ?>
		<table width="100%"><tr><td align="center"><div id="ajaxmsg" style="display:none;background-color: #efe7b8;width: 290px;border-style: solid; border-color: #f0dc7e; padding:0px; text-align: center;"><img src="<?php echo JURI::root()."administrator/components/com_ccnewsletter/assets/warning.png"; ?>">&nbsp;&nbsp;<?php echo JText::_("CC_PLEASE_WAIT");?></div></td></tr></table>
		<table width="100%"><tr><td align="center"><div id="display_success" style="display:block;background-color: #f0ffe9;border-style: solid;border-color: #b4ffa3;width: 290px;color: green;  text-align: center;"><img src="<?php echo JURI::root()."administrator/components/com_ccnewsletter/assets/tick1.png"; ?>"> &nbsp; <?php echo JText::_('CC_SUCCESS'); ?> <a href="index.php?option=com_ccnewsletter&controller=newsletter"><br/><?php echo JText::_('CC_SUCCESS_CONTINUE_LINK'); ?></a></div></td></tr></table>
		<div id="display_countertab" style="display:none;"><table width="100%"><tr><td align="center" ><div id="display_counter" style="display:none;background-color: #efe7b8;width: 290px;border-style: solid;color:red; border-color: #f0dc7e;  text-align: center;"><img src="<?php echo JURI::root()."administrator/components/com_ccnewsletter/assets/ajax-loader.gif"; ?>"> &nbsp; <?php echo JText::_( 'CC_COUNTER' ); ?> <span id="counter">0</span>  <?php echo JText::_( 'CC_SECONDS' ); ?></div></td></tr></table></div>
		<table width="100%"><tr><td align="center"><div id="display_sending" style="display:none;color:red;background-color: #efe7b8;width: 290px;border-style: solid; border-color: #f0dc7e;  text-align: center;"><img src="<?php echo JURI::root()."administrator/components/com_ccnewsletter/assets/ajax-loader.gif"; ?>"> &nbsp; <?php echo JText::_( 'CC_SENDING_BATCH' ); ?></div></td></tr></table>
	<?php } ?>
	<br>
	<table id="maintablelist" style="margin: auto; width: 290px;" class="adminlist" cellspacing="1">
		<thead>
			<tr>
				<th width="10" class="title">
					<?php echo JText::_( 'NUM' ); ?>
				</th>
				<th class="title">
					<?php echo JText::_( 'CC_EMAIL' ); ?>
				</th>
				<th class="title">
					<?php echo JText::_( 'CC_SENT' ); ?>
				</th>
			</tr>
		</thead>
		<tbody id="result_tbody">
			<?php
			foreach( $this->ccdata['results'] as $result )
			{
				?>
				<tr>
					<td style="text-align: center;"><?php echo $result['index']; ?></td>
					<td><?php echo $result['email']; ?></td>
					<td style="text-align: center;"><?php echo $result['sent']; ?></td>
				</tr>
				<?php
			}
			?>
		</tbody>
	</table>

        <?php $id = JRequest::getInt( 'id', '', 'get', ''); ?>
	<input type="hidden" name="option" value="com_ccnewsletter" />
	<input type="hidden" name="id" value="<?php echo $id;?>" />
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="controller" value="newsletter" />
	<?php echo JHTML::_( 'form.token' ); ?>
</form>
<?php
if ($this->ccdata['ajax'])
{
	?>
	<script type="text/javascript">
		document.getElementById('counter').innerHTML = <?php echo $this->ccdata['batchinterval']; ?>;
		window.setTimeout("fire_ajax_request();", 1000);
	</script>
	<?php
}
?>
<div style="padding-top:50px;">
<p class="copyright" style="text-align:center;" >
<?php echo $this->name; ?>&nbsp;<?php echo $this->version; ?>. Copyright (C) 2006 - <?php echo $curYear = date('Y'); ?>  Chill Creations<br/>Joomla! component by <a href="http://www.chillcreations.com" target="_blank">Chill Creations</a>
</p>
</div>
