<?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
if($this->ermsg != '')
{
?>
<div class="ml-alert-1-info m1-info-set-2">
    <div class="style-1-icon info"></div>
       No Data Found.
</div>
<?php 
}
else 
{ ?>

<script type="text/javascript">
function addLeaveTypeDiv()
{
	var divcount = $("#multi_dept_div > div").length;
	var idcount = parseInt($('#idcount').val());
	if(divcount < 5)
	{	
		idcount = idcount + 1;
		var html = '';
		html+="<div id='parent_"+idcount+"' class='add_request'>";
		html+="<div class='form-ui'>";
		html+="<label class='required'>Leave Type</label>";
		html+="<input type='text' maxlength='100' value='' id='leavetype_"+idcount+"' name='leavetype[]' class='cls_service_request_name' onblur='validateTextInput(this,\"leave type\")' onkeyup='validateTextInput(this,\"leave type\")'>";
		html+="</div>";
		html+="<div class='form-ui'>";
		html+="<label class='required'>Number of Days</label>";
		html+="<input type='text' maxlength='2' value='' id='numberofdays_"+idcount+"' name='numberofdays[]' class='cls_number_days' onblur='validateNumberInput(this)' onkeyup='validateNumberInput(this)'>";
		html+="</div>";
		html+="<span class='sprite remove-new remove-entry' title='Remove' onclick='removeDiv("+idcount+")'></span>";
		html+="</div>";
		$('#add-candidate').before(html);
		//$('#multi_dept_div').append(html);
		$('#idcount').val(idcount);
	}else
	{
		jAlert('You can add only 5 leave types at a time.');
	}
	
}

function validateLeaveTypeOnSubmit(ele)
{
    var parentdivlength = $('div[id^=parent]').length;
    var re = /^[a-zA-Z0-9\- ]+$/;
    var numre = /^[0-9]+$/;
    var errorcount = 0;
	if(parentdivlength > 0)
    {                    
        $('.add_request').each(function(i){                            
            var ele= $(this).find('.cls_service_request_name');                            
            var elementid = $(ele).attr('id');
            var reqValue = $(ele).val();

            var numele= $(this).find('.cls_number_days');                            
            var numelementid = $(numele).attr('id');
            var numreqValue = $(numele).val();
            
            $('#errors-'+elementid).remove();
            $('#errors-'+numelementid).remove();
            if($(ele).val() == '')
            {
                $(ele).parent().append("<span class='errors' id='errors-"+elementid+"'>Please enter leave type.</span>");
                errorcount++;
            }
            else if(!re.test(reqValue))
            {
                $(ele).parent().append("<span class='errors' id='errors-"+elementid+"'>Please enter valid leave type.</span>");
                errorcount++;
            }
            else
            {
            	$('#errors-'+elementid).remove();
            }

            if($(numele).val() == '')
            {
                $(numele).parent().append("<span class='errors' id='errors-"+numelementid+"'>Please enter number of days.</span>");
                errorcount++;
            }
            else if(!numre.test(numreqValue))
            {
                $(numele).parent().append("<span class='errors' id='errors-"+numelementid+"'>Please enter only number.</span>");
                errorcount++;
            }
            else
            {
            	$('#errors-'+numelementid).remove();
            }

        });
    }

    if(errorcount == 0)
    {
        $.blockUI({ width:'50px',message: $("#spinner").html() });
        document.getElementById("hrwizard_leavetypes").submit();
    }
}

</script>	

<div class="total-form-controller">
<div id="error_message" style="display:none;"></div>
<?php if (count($this->messages)) {?>
			<div id="dispmsgmenu">
		    <div class="ml-alert-1-success new_wizard_success">
			<div class="style-1-icon success"></div>
				<?php foreach ($this->messages as $message){?>
				 <?php echo $this->escape($message); ?>
				<?php } ?>
		       </div> 
			</div>	
	<?php } ?>
<h3 class="step_main_title">Configure Employee Leave Types</h3>
<?php echo sapp_Helper::dispayAddEmployeeLink($this->hrWizardData,'hr');?>
<form name="hrwizard_leavetypes" id="hrwizard_leavetypes" action="<?php echo BASE_URL?>hrwizard/configureleavetypes" method="POST" class="step_form">
				
			<div id="multi_dept_div">
			<?php if(!$_POST){?>
				<div id='parent_0' class="add_request" >
					<div class="form-ui ">
		            <label class="required">Leave Type </label>
							<input type="text" maxlength="100" value="" id="leavetype_0" name="leavetype[]" class='cls_service_request_name' onblur='validateTextInput(this,"leave type")' onkeyup='validateTextInput(this,"leave type")'>                            
		        	</div>
		        	
		        	<div class="form-ui ">
		            <label class="required">Number of Days </label>
							<input type="text" maxlength="2" value="" id="numberofdays_0" name="numberofdays[]" class='cls_number_days' onblur='validateNumberInput(this)' onkeyup='validateNumberInput(this)'>                            
		        	</div>
		        	
	        	</div>
	        	<span id="add-candidate"><span class="sprite candidate-new" onclick="addLeaveTypeDiv()">Add Leave Type</span></span>
	        <?php }?>
	        	
         	</div>
           	
        	<input type="hidden" value="0" id="idcount"/>
        	<span id="errors-maxdiv" class="errors"></span>	
        	
        	<div class="new-form-ui-submit mrgetop60">
				<button onclick="validateLeaveTypeOnSubmit(this)" type="button" id="submitbuttons" name="submitbutton">Save</button>
			</div>
</form>		
</div>		

<script type="text/javascript" language="javascript">

$(document).ready(function(){
	var counter = 0;
	<?php if($_POST){
		    if(isset($this->msgarray['leavetypesize'])){
		    	for($i=0;$i<$this->msgarray['leavetypesize'];$i++)
		    	{
	?>
					var html = '';
					html+="<div id='parent_<?php echo $i;?>' class='add_request' >";
					html+="<div class='form-ui'>";
					html+="<label class='required'>Leave Type </label>";
					html+="<input type='text' maxlength='100' value='<?php echo $_POST['leavetype'][$i]?>' id='leavetype_<?php echo $i;?>' name='leavetype[]' class='cls_service_request_name' onblur='validateTextInput(this,\"leave type\")' onkeyup='validateTextInput(this,\"leave type\")'>";
					<?php if(isset($this->msgarray['leave_type'][$i])){?>
					html+="<span id='errors-leavetype_<?php echo $i;?>' class='errors'><?php echo $this->msgarray['leave_type'][$i];?></span>";
					<?php }?>
					html+="</div>";
					html+="<div class='form-ui'>";
					html+="<label class='required'>Number of Days</label>";
					html+="<input type='text' maxlength='2' value='<?php echo $_POST['numberofdays'][$i]?>' id='numberofdays_<?php echo $i;?>' name='numberofdays[]' class='cls_number_days' onblur='validateNumberInput(this)' onkeyup='validateNumberInput(this)'>";
					html+="</div>";
					<?php if($i !=0){?>
					html+="<span class='sprite remove-new remove-entry' title='Remove' onclick='removeDiv(<?php echo $i;?>)'></span></div>";
					<?php }?>
					html+="</div>";
					$('#multi_dept_div').append(html);
					counter++;
					
					
	<?php } ?>
			$('#multi_dept_div').append("<span id='add-candidate'><span class='sprite candidate-new' onclick='addLeaveTypeDiv()'>Add Leave Type</span></span>");
			$('#idcount').val(counter);
	<?php }}?>

    <?php echo sapp_Helper::modifyClass($this->hrWizardData); ?>
});
</script>    

			<div class="nxt_pre_button">
				<button class="next_butt" name="next" id="next" type="button" onclick="window.location='<?php echo BASE_URL?>hrwizard/configureholidays';">Next</button>
			</div>
<?php 
}
?>








