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

<?php
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(); ?>">
		<?php echo $this->form->id; ?>
    <div class="total-form-controller">
	<div class="new-form-ui clearb">
            <label>Business Unit</label>
            <div class="division loca_state_div"><?php echo $this->form->business_unit_id; ?></div>
            <span class="errors" id="errors-business_unit_id">
			<?php	if(empty($this->bunits_options))
					echo "Business units are not added yet.";
			?>
			</span>
        </div>		
        <div class="new-form-ui clearb">
            <label class="required">Group</label>
            <div class="division loca_state_div"><?php echo $this->form->group_id; ?></div>
            <div class="errors" id="errors-group_id"></div>
        </div>			
        <div class="new-form-ui clearb">
            <label class="required">Group Email </label>
            <div class="division"><?php echo $this->form->groupEmail; ?></div>
            <div class="errors" id="errors-groupEmail" ></div>
        </div>
        <div class="new-form-ui-submit">
            <?php echo $this->form->submit; ?>
            <input type="button" id="submitbutton_cpy" value="Save" />
            <button onclick="window.location.href='<?php echo BASE_URL; ?>emailcontacts';" type="button" id="Cancel" name="Cancel">Cancel</button>
        </div>
    </div>
</form>


<?php if(!empty($this->msgarray))	{	?>
<script type="text/javascript" language="javascript">
	var group_idError = "<?php echo (isset($this->msgarray["group_id"]) && $this->msgarray["group_id"] != "")?$this->msgarray["group_id"] : ''; ?>";
        var bunit_idError = "<?php echo (isset($this->msgarray["business_unit_id"]) && $this->msgarray["business_unit_id"] != "")?$this->msgarray["business_unit_id"] : ''; ?>";
	var grpEmailError = "<?php echo (isset($this->msgarray["groupEmail"]) && $this->msgarray["groupEmail"] != "")? $this->msgarray["groupEmail"] : ''; ?>";
	
	if(group_idError != "")          $("#errors-group_id").text(group_idError);
	else                                    $("#errors-group_id").text("");
	
        if(grpEmailError != "")           
            $("#errors-groupEmail").text(grpEmailError);
	else
            $("#errors-groupEmail").text("");
        
        if(bunit_idError != "")           
            $("#errors-business_unit_id").text(bunit_idError);
	else
            $("#errors-business_unit_id").text("");
	
</script>
<?php	}	?>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
    $('#submitbutton').hide();
    $('#submitbutton_cpy').click(function(){
        var bunit = $('#business_unit_id').val();
        var group = $('#group_id').val();
        var email = $('#groupEmail').val();
        if(bunit != '' && group != '' && email != '')
        {
            $.post(base_url+"/emailcontacts/getmailcnt",{bunit_id:bunit,group_id:group},function(data){
                if(data.result == 'yes')
                {
                    jConfirm("Are you sure you want to overwrite the group email ?", "Confirmation", function(r) {
                        if(r==true)
                        { 
                            $('#submitbutton').trigger('click');
                        }
                    });
                }
                else 
                {
                    $('#submitbutton').trigger('click');
                }
            },"json");
        }
        else 
        {
            $('#submitbutton').trigger('click');
        }
    });
     
});//end of ready function
</script>