var basicPATH =localStorage['basicPATH'];
var serviceURLHR =localStorage['serviceURLHR'];
function eventListnerPerfomance(){
	jQuery('.addMoreAttribute').live('click',addPerfomanceAttribute);
	jQuery('.cancelEmployeeDetails').live('click',function(){  $('.newEmployeeDetails').hide('clip',300,function(){ $(this).remove(); })  });
	/*jQuery('.saveEmployeeDetails').live('click',saveEmployeeDetails);*/
	jQuery('.saveSingleAttrDetails').live('click',saveAttributeDetails);
	//jQuery('.saveperformance_qns').live('click',saveperformance_qns);
}

jQuery(document).ready(function(){ 
								eventListnerPerfomance(); 
								add_performance_questions_form();
							});

function search_attribute(employee_id)
{
	review_questions();	
}

function add_performance_questions_form()
{
	$('.btn-tab-active').removeClass('btn-tab-active');
	$('.btn_performance_add').addClass('btn-tab-active');
	$.ajax({
		type: "POST",
		url: serviceURLHR + "managePerformance.php",
		data: "act=add_performance_qns",
		dataType:'json',
		success: function(result){
			$('#PerformanceContentDiv').html(result.add_performace_form);
			
		}
	});
}

function saveAttributeDetails(){
	var attr_name=$('#attribute').val();
	
	if(attr_name == ''){
		alert('Attribute Name is required')	;
	}else{
		$.ajax({
			type: "POST",
			url: serviceURLHR + "managePerformance.php",
			data: "act=add_new_attr_name&attribute="+attr_name,
			//dataType:'json',
			success: function(result){
				$('#attr_combo').append('<option value="'+result+'">'+attr_name+'</option');	
				$('.newEmployeeDetails').hide('clip',300,function(){ $(this).remove(); });
			}
		});
	}
}

function load_attr_details(attr_id)
{
	//$('.attr_table').html();
	$('.attr_table tbody').remove();
	$('.attr_table tfoot').remove();
	var attr_name=$('#attr_combo option:selected').text();
	$.ajax({
		type: "POST",
		url: serviceURLHR + "managePerformance.php",
		data: "act=attr_details&attr_id="+attr_id+"&attr_name="+attr_name,
		success: function(result){
			$('.attr_table').show();
			$('.attr_table').append(result);
			var total_weightage=0;
			$(".weightage_text").each(function() {
				total_weightage=total_weightage+Number($(this).val());
			});
			$('#weightage_total').val(total_weightage);
			$('#weightage_total_span').html(total_weightage);
		}
	});
}

function add_more_qns()
{
	if($('#attr_combo').val())
	{
		var row_count=$('#qns_count').val();
		row_count++;
		$('.attr_table tbody').append('<tr>'+
			'<td><textarea name="qns_area_'+row_count+'" id="qns_area_'+row_count+'" class="form-control" rows="3" placeholder="Enter ..."></textarea></td>'+
			'<td colspan="2"><input type="text" name="qns_weightage_'+row_count+'" id="qns_weightage_'+row_count+'" onchange="calculate_weightage()" class="weightage_text form-control numeric_filter" onkeypress="return onlyNos(event,this);" /></td>'+
			
		'</tr>');
		$('#qns_count').val(row_count);
	}
}

function calculate_weightage()
{
	var total_weightage=0;
	$(".weightage_text").each(function() {
    	total_weightage=total_weightage+Number($(this).val());
	});
	$('#weightage_total').val(total_weightage);	
	$('#weightage_total_span').html(total_weightage);
}

function saveperformance_qns()
{
	if($('#weightage_total').val()==100)
	{
		$.ajax({
			type: "POST",
			url: serviceURLHR + "managePerformance.php",
			data: $('.AddAttributeForm').serialize(),
			success: function(result){
				alert('Questions added successfully.');
				add_performance_questions_form();
			}
		});
	}
	else
	{
		alert('Weightage is not 100%.')	
	}
}

function view_total_qns()
{
	$('.btn-tab-active').removeClass('btn-tab-active');
	$('.btn_performance_view').addClass('btn-tab-active');
	$.ajax({
		type: "POST",
		url: serviceURLHR + "managePerformance.php",
		data: 'act=list_total_qns',
		//dataType:'json',
		success: function(result){
			$('#PerformanceContentDiv').html(result);
			$('.expand_parent_row').click(function () {
				var currentDetails = $(this).next('.row_to_expand');
				$('.row_to_expand').not(currentDetails).hide();
				currentDetails.toggle();
			});
		}
	});
}

/*function delete_questions(perform_qn_id)
{
	
}*/

function edit_questions(perform_qn_id)
{
	$('#edit_qns').remove();
	var qns_str=$('.qns_list_new'+perform_qn_id+' td:nth-child(3)').html();
	var qns_weightage=$('.qns_list_new'+perform_qn_id+' td:nth-child(4)').html();
	$('#view_qns_table').before('<form id="edit_qns">'+
		'<div class="clear-float"></div>'+
		'<table class="table">'+
			'<tr>'+
				'<td>Question:</td>'+
				'<td><textarea name="update_qns_str" id="update_qns_str" class="form-control" rows="3">'+qns_str+'</textarea></td>'+
			'</tr>'+
			'<tr>'+
				'<td>Weightage:</td>'+
				'<td><input type="text" name="edit_weightage" id="edit_weightage" class="form-control"  value="'+qns_weightage+'"></td>'+
			'</tr>'+	
			'<tr>'+
				'<td>&nbsp;</td>'+
				'<td><input class="add-bt-3 color-button" type="button" name="update_qns" id="update_qns" value="Update" onclick="update_qns_db('+perform_qn_id+')" /><input type="button" name="update_qns" id="update_qns" value="Cancel" class="add-bt-3 color-button" onclick="remove_update_qns_form()" /></td>'+
			'</tr>'+
		'</table>'+
	'</form>');
}

function update_qns_db(perform_qn_id)
{
	$('#edit_qns').append("<input type='hidden' name='act' id='act' value='update_qns' />");
	$('#edit_qns').append("<input type='hidden' name='update_id' id='update_id' value='"+perform_qn_id+"' />");
	$.ajax({
		type: "POST",
		url: serviceURLHR + "managePerformance.php",
		data: $('#edit_qns').serialize(),
		//dataType:'json',
		success: function(result){
			if(result=='success')
			{
				var qns_str=$('#update_qns_str').val();
				var qns_weightage=$('#edit_weightage').val();
				$('.qns_list_new'+perform_qn_id+' td:nth-child(3)').html(qns_str);
				$('.qns_list_new'+perform_qn_id+' td:nth-child(4)').html(qns_weightage);
				$('#edit_qns').remove();
				alert('Question updated successfully.')
			}
			else
			{
				alert('Weightage is not 100%.')	
			}
		}
	});
}

function remove_update_qns_form()
{
	$('#edit_qns').remove();
}

function review_questions()
{
	$('.btn-tab-active').removeClass('btn-tab-active');
	$('.btn_performance_review').addClass('btn-tab-active');
	if($('#filter_attribute').val())
	{
		var employee_id=$('#filter_attribute').val();	
	}
	else
	{
		var employee_id='';	
	}
	$.ajax({
		type: "POST",
		url: serviceURLHR + "managePerformance.php",
		data: 'act=review_qns&employee_id='+employee_id,
		//dataType:'json',
		success: function(result){
			$('#PerformanceContentDiv').html(result);
			$('.expand_parent_row').click(function () {
				var currentDetails = $(this).next('.row_to_expand');
				$('.row_to_expand').not(currentDetails).hide();
				currentDetails.toggle();
			});
		}
	});
}

function calculate_rate(emp_id)
{
	var rate_numerator=0;
	var rate_denumerator=0;
	$('.sub_rate_row_'+emp_id).each(function(){
		var weightage=$(this).find('.qns_weightage').html();
		var rate=$(this).find('.emp_rate .emp_rate_combo').val();
		rate_numerator=Number(rate_numerator)+(Number(weightage)*Number(rate));		
		rate_denumerator=Number(rate_denumerator)+Number(weightage);
	});
	
	var total_rate=rate_numerator/rate_denumerator;
	alert(total_rate);
	$('#main_rate_row_'+emp_id+' td:nth-child(3)').html(total_rate);
}

function fnFillReview() 
{
	$('#review_qstform').append("<input type='hidden' name='act' id='act' value='fill_review_qns' />");
	$.ajax({
		type: "POST",
		url: serviceURLHR + "managePerformance.php",
		data: $('#review_qstform').serialize(),
		//dataType:'json',
		success: function(result){
			alert('Questions reviews added successfully.');
			review_questions();
		}
	});	
}

function addPerfomanceAttribute(){
	
	$("body").append('<div class="newEmployeeDetails" style="display:none; padding:25px;">'+
					 '<form class="newSingleAttributeForm"><table width="800" height="200" border="0" cellpadding="0" cellspacing="0">'+
					  '<tr>'+
						'<td width="200"><translate>Attribute</translate></td>'+
						'<td width="400"><input type="text" name="attribute" value="" id="attribute" /></td>'+
						'<td width="200">&nbsp;</td>'+
						'<td width="200">&nbsp;</td>'+
					  '</tr>'+
					  '<tr>'+
					    '<td>&nbsp;</td>'+
						'<td>&nbsp;</td>'+
						'<td></td>'+
						'<td><input type="button" name="button" id="saveSingleAttrDetails"  class="color-button saveSingleAttrDetails" value="Submit" style="float:left; margine-left:5px" />'+
						'<input type="button" name="button" class="color-button2 cancelEmployeeDetails" style="float:left;" value="Cancel" /></td>'+
					  '</tr>'+
					'</table></form></div>');

	$('.newEmployeeDetails').show('fade',300);
	$('.newEmployeeDetails').center();
	
}

function onlyNos(e, t) {
            try {
                if (window.event) {
                    var charCode = window.event.keyCode;
                }
                else if (e) {
                    var charCode = e.which;
                }
                else { return true; }
                if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                    return false;
                }
                return true;
            }
            catch (err) {
                alert(err.Description);
            }
        }

jQuery.fn.center = function(parent) {
    if (parent) {
        parent = this.parent();
    } else {
        parent = window;
    }
    this.css({
        "position": "absolute",
        "top": ((($(parent).height() - this.outerHeight()) / 2) + $(parent).scrollTop() + "px"),
        "left": ((($(parent).width() - this.outerWidth()) / 2) + $(parent).scrollLeft() + "px")
    });
return 
}
