<?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 addQuestionDiv()
{
	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'>Question</label>";
		html+="<input type='text' maxlength='100' value='' id='question_"+idcount+"' name='question[]' class='cls_service_request_name' onblur='validatequestionname(this)' onkeyup='validatequestionname(this)'>";
		html+="</div>";
		html+="<div class='form-ui form-ui-textarea'>";
		html+="<label>Description</label>";
		html+="<textarea maxlength='100' cols='50' rows='10' id='description_"+idcount+"' name='description[]' class='cls_address' ></textarea>";
		html+="</div>";
		html+="<span class='sprite remove-new remove-entry' title='Remove' onclick='removeDiv("+idcount+")'></span>";
		html+="</div>";
		$('#add-candidate').before(html);
		$('#idcount').val(idcount);
		
	}else
	{
		jAlert('You can add only 5 questions at a time.');
	}
	
}

function validateQuestionOnSubmit(ele)
{
    var parentdivlength = $('div[id^=parent]').length;
    var re = /^[a-zA-Z0-9\- ?'.,\/#@$&*()!]+$/;
    var errorcount = 0;
    var category_name_id = $('#category_name_id').val();
    $('#errors-category_name_id').remove();
	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();
            $('#errors-'+elementid).remove();
            
            if($(ele).val() == '')
            {
                $(ele).parent().append("<span class='errors' id='errors-"+elementid+"'>Please enter question.</span>");
                errorcount++;
            }
            else if(!re.test(reqValue))
            {
                $(ele).parent().append("<span class='errors' id='errors-"+elementid+"'>Please enter valid question.</span>");
                errorcount++;
            }
            else
            {
            	$('#errors-'+elementid).remove();
            }

        });
    }
    if(category_name_id == '')
    {
        $('#category_name_id').parent().append("<span class='errors' id='errors-category_name_id'>Please select parameter.</span>");
    }
    else
    {
        $('#errors-category_name_id').remove();
    }

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

function saveCategory()
{
	var category_name = $("#category_name").val();
	var description = $("#description").val();
	var errorcount = 0;
	var re = /^[a-zA-Z0-9\- ]+$/;
	$("#errors-category_name").remove();
	$("#errors-description").remove();
	$('#error_message').html('');
		if(category_name == '')
		{
			$("#category_name").parent().append("<span class='errors' id='errors-category_name'>Please enter parameter.</span>");
			errorcount++;
		}
		else if(!re.test(category_name))
		{
			$("#category_name").parent().append("<span class='errors' id='errors-category_name'>Please enter valid parameter.</span>");
			errorcount++;
		}
		
		if(errorcount==0)
		{
			$.ajax({
		     	url: base_url+"/hrwizard/savecategory/format/json",
		     	type : 'POST',	
				data : 'category_name='+category_name+'&description='+description,
				dataType: 'json',
				beforeSend: function () {
					$.blockUI({ width:'50px',message: $("#spinner").html() });
				},
				success : function(response){	
					$.unblockUI();
					if(response['msg'] == 'success')
					{
						$("#category_name_id").append("<option value='"+response['id']+"'>"+response['category_name']+"</option>");
						$("#contentdiv").hide();
						$("#successdiv").show();
						setTimeout(function(){
							closeDialogPopup('categorydiv');
						},3000);
						
					}else
					{
						$("#category_name").parent().append("<span class='errors' id='errors-category_name'>"+response['msg']+"</span>");
					}	
				}
			});
		
		}
}


</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 Performance Appraisal</h3>
<?php echo sapp_Helper::dispayAddEmployeeLink($this->hrWizardData,'hr');?>
	
<form name="wizard_performance" id="wizard_performance" action="<?php echo BASE_URL?>hrwizard/configureperformanceappraisal" method="POST" class="step_form">
			<div class="form-ui">
				  <label class="required">Appraisal Parameter</label>
					<select id="category_name_id" name="category_name_id">
					<option  value="">Select Parameter</option>
						<?php if(!empty($this->appraisalCategoriesData)) { ?>
						<?php 	foreach($this->appraisalCategoriesData as $key => $value) {
						?>
										<option value="<?php echo $value['id']; ?>"><?php echo $value['category_name'];?></option>
						<?php }}?>
					</select>
					
				  <?php if(isset($this->msgarray['category_name_error'])){?>
			   		<span class="errors" id="errors-category_name_id"><?php echo $this->msgarray['category_name_error'];?></span>
				<?php }?>
				<span onclick="displayPopup('categorydiv','Parameter');" class="add-coloum"> Add Parameter</span>			
			</div>
				
			<div id="multi_dept_div">
			<?php if(!$_POST){?>
				<div id='parent_0' class="add_request" >
					<div class="form-ui ">
		            <label class="required">Question </label>
							<input type="text" maxlength="100" value="" id="question_0" name="question[]" class='cls_service_request_name' onblur='validatequestionname(this)' onkeyup='validatequestionname(this)'>                            
		        	</div>
		        	
		        	<div class="new-form-ui textareaheight">
				            <label class="">Description </label>
					            <div class="division">
									<textarea name="description[]" id="description_0"></textarea>
							    </div>
				   </div>
	        	</div>
	        	<span id="add-candidate"><span class="sprite candidate-new" onclick="addQuestionDiv()">Add Question</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="validateQuestionOnSubmit(this)" type="button" id="submitbuttons" name="submitbutton">Save</button>
					
			</div>
</form>		
</div>		


<div id="categorydiv" style="display: none; ">	
				
					 <div class="total-form-controller" >	
					 <div id="successdiv" style="display:none;">
						<div class='ml-alert-1-success'>
						<div class='style-1-icon success'></div>
							Parameter added succesfully
					  </div>
					 </div>	
					 <div id="error_message" style="display:none;"></div>	
					  <div id="contentdiv"> 
					 		
							<div class="new-form-ui ">
				            	<label class="required">Parameter</label>
				            	<div class="division">
									<input type="text" onkeyup="validateTextInput(this,'parameter')" onblur="validateTextInput(this,'parameter')" name="category_name" id="category_name" value="" maxlength="100">                            
								</div>
				        	</div>
				        	
				        	<div class="new-form-ui textareaheight">
				            <label class="">Description </label>
					            <div class="division">
									<textarea name="description" id="description"></textarea>
							    </div>
				        	</div>
				        	
				        	<div class="new-form-ui-submit"  >
						
							<input type="button" value="Save" id="submitcategory" name="submitcategory" onclick="saveCategory()"/>																	
							<button name="Cancels" id="Cancels" type="button" onclick="closeDialogPopup('categorydiv')">Cancel</button>
							
							</div>
					 </div>  
					 </div>
</div>
	

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

$(document).ready(function(){
	$("[id^=description]").maxlength();

	var counter = 0;
	<?php if($_POST){
		    if(isset($this->msgarray['categoryerrorsize'])){
		    	for($i=0;$i<$this->msgarray['categoryerrorsize'];$i++)
		    	{
	?>
					var html = '';
					html+="<div id='parent_<?php echo $i;?>' class='add_request' >";
					html+="<div class='form-ui'>";
					html+="<label class='required'>Question </label>";
					html+="<input type='text' maxlength='100' value='<?php echo $_POST['question'][$i]?>' id='question_<?php echo $i;?>' name='question[]' class='cls_service_request_name' onblur='validatequestionname(this)' onkeyup='validatequestionname(this)'>";
					<?php if(isset($this->msgarray['question_name'][$i])){?>
					html+="<span id='errors-question_<?php echo $i;?>' class='errors'><?php echo $this->msgarray['question_name'][$i];?></span>";
					<?php }?>
					html+="</div>";
					html+="<div class='form-ui form-ui-textarea'>";
					html+="<label>Description</label>";
					html+="<textarea maxlength='100' cols='50' rows='10' id='description_<?php echo $i;?>' name='description[]' class='cls_address' ><?php echo $_POST['description'][$i];?></textarea>";
					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++;
					$("[id^=description]").maxlength();
					
					
	<?php } ?>
			$('#multi_dept_div').append("<span id='add-candidate'><span class='sprite candidate-new' onclick='addHolidayDiv()'>Add Holiday</span></span>");
			$('#idcount').val(counter);
	<?php }}?>

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

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








