<?php 
/********************************************************************************* 
 *  This file is part of Sentrifugo.
 *  Copyright (C) 2014 Sapplica
 *   
 *  Sentrifugo 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  Sentrifugo 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 Sentrifugo.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Sentrifugo Support <support@sentrifugo.com>
 ********************************************************************************/
if($this->ermsg == 'noprivileges')
{
?>
     <div class="ml-alert-1-info"><div class="style-1-icon info"></div>You are not authorized to access this page</div>

<?php
}
else if($this->popup == 'close') 
{ ?>	
	<div class="ml-alert-1-success">
	<div class="style-1-icon success"></div>
	Category <?php echo $this->eventact; ?> successfully.
	</div>

<?php 
} 
else
{
	sapp_Global::generateClientValidations($this->form);
?>
<div id="error_message" style="display:none;"></div>

<form name="<?php echo $this->form->getName(); ?>" id="<?php echo $this->form->getId(); ?>" action="<?php echo $this->form->getAction();?>" method="<?php echo $this->form->getMethod(); ?>">
	<div class="total-form-controller">
		<div class="new-form-ui clearb">
			<label class="required">Category</label>
   			<span id="no-results"></span>
			<div class="division"><?php echo $this->form->category; ?></div>
			<span class="errors" id="errors-<?php echo $this->form->category->getId(); ?>"><?php if(isset($this->msgarray['category'])){?>
			<?php echo $this->msgarray['category'];?>
			<?php }?></span>
		</div>
		<div class="new-form-ui clearb textareaheight">
			<label>Description</label>
			<div class="division"><?php echo $this->form->description; ?></div>
		</div>
	</div>
	<div class="new-form-ui-submit">
		<?php echo $this->form->submit; ?>
		<button onclick="closeiframepopup('categories','cancel');" id="Cancel" name="Cancel" type="button" >Cancel</button>
	</div>
</form>
<?php } ?>
<script language='javascript' type='text/javascript'>

	<?php if($this->popup != '') {
            ?>
			closeiframeAddPopup("<?php echo $this->categoriesData ;?>",'categories','category_id','Category',<?php echo $this->newCat;?>); 
	<?php } ?>
$(document).ready(function() { 
	<?php if (count($this->messages)) {?>
		  setTimeout(function(){
				$('#popupmessage').fadeOut('slow');
			},3000);

	<?php } ?>
});


/**
** to display remaining characters text below textarea
**/
$('#description').maxlength();

/**
** for categories autocomplete
** 
**/
var availableCategories = [<?php echo $this->categoriesStr; ?>];
	
$(function(){
	$('#category').autocomplete({
		
		source: function(req, response){
			var regex = /^[a-zA-Z0-9][\s+[a-zA-Z0-9]+]*$/;
			if(regex.test(req.term) && req.term!='')
			{
				var results = $.ui.autocomplete.filter(availableCategories,req.term);
				if(!results.length){
					$("#no-results").html("Category available");
				}
				else if(results.length)
				{
					$("#no-results").html("Category already exists");
				}
				response(results.slice(0,10));
			}
			else
			{
				$("#no-results").empty();
			}
			
		}
	});
});

/**
** on blur function to check already existing categories
**/
function chkCategory()
{
	var regex = /^[a-zA-Z0-9][\s+[a-zA-Z0-9]+]*$/;
	var categoryStr = $.trim($('#category').val());
	
	if(categoryStr && regex.test(categoryStr) && $.inArray(categoryStr,availableCategories) == 0)
	{
		$("#no-results").html('Category already exists');
	}
	else if(categoryStr && $.inArray(categoryStr,availableCategories) == -1)
	{
		$("#no-results").empty();
	}	
}
/*function validateForm()
{
	if($.trim($('#category').val()) == '')
	{
		$('#errors-category').html('Please enter category');
		return false;
	}	
	return true;
}*/
</script>