(function($){ 
     $.fn.extend({  
         limit: function(limit,element) {
			
			var interval, f;
			var self = $(this);
					
			$(this).focus(function(){
				interval = window.setInterval(substring,100);
			});
			
			$(this).blur(function(){
				clearInterval(interval);
				substring();
			});
			
			substringFunction = "function substring(){ var val = $(self).val();var length = val.length;if(length > limit){$(self).val($(self).val().substring(0,limit));}";
			if(typeof element != 'undefined')
				substringFunction += "if($(element).html() != limit-length){$(element).html((limit-length<=0)?'0':limit-length);}"
				
			substringFunction += "}";
			
			eval(substringFunction);
			
			
			
			substring();
			
        } 
    }); 
})(jQuery);

jQuery(document).ready(function($) {

var numberOfVisible_cq = 5;
var lastIndex= 4;

    $('.col_3 > input:checkbox').click(function() {
        if ( $(this).is(':checked') ) {
            $(this).parents('table').find('.dropdown').val('N/A');
        } else if ( $(this).not(':checked') ) {
            $(this).parents('table').find('.dropdown').val('No');
        }
    });

$('#load_induction').click(function() {
  var id = $('#induction_list').val();
  $('#job_task_specific').load('/default.aspx?d='+id+'&f=edit #job_task_specific');
});

$('#job_task_specific').ajaxComplete(function(){
      alert('Job Role Specific Inductions loaded successfully...');
      $('#load_induction').hide();
      lastIndex = hideEmptyJobTaskFields($,numberOfVisible_cq);
      $('#job_task_table .dropdown').val('No');
      $('#job_task_table .textarea').val('');   
});

$('#induction_list').change(function(){
     if($('#induction_list').val()){
       $('#load_induction').show();
     }
     else{
       $('#load_induction').hide();
     }   
  });

$('#load_induction').hide();

if($('#job_task_table')){
     lastIndex = hideEmptyJobTaskFields($, numberOfVisible_cq);
}

$('#add_question').click(function() {
   
   // setting next row visible if not reached max
   if(lastIndex>0 && lastIndex<19){
      $($('#job_task_table tr')[lastIndex+1]).show();
      $($('#job_task_table tr')[lastIndex+1]).css('visibility', 'visible');
     }
   

   if(lastIndex+1 == 19)
       $('#add_question').hide();
   
   lastIndex++;
});

});

function hideEmptyJobTaskFields($, numberOfVisible_cq){
 var lastIndex = 4;
 $('#job_task_table tr').each(function (index, domEle) {
       if(index >= numberOfVisible_cq && index<20){
           if(!$(domEle).find('input[type=text]').val()){
               $(domEle).hide();
           }else{
              lastIndex = index;
           }
       }

       var textField = $(domEle).find('input[type=text]');
       var id = $(textField).attr('id');
       var fieldClass = $(textField).attr('class');
       var textVal = $(textField).val();

       if(fieldClass === "text")
           fieldClass = "q_textarea";

       var textArea = "<textarea class='"+fieldClass+" limitchar-text' cols='43' rows='3' name='"+id+"' id='"+id+"'>"+textVal+"</textarea>";
       $(textField).parent().append(textArea);
       $(textField).remove();
   });

   // limit textareas
   $('.limitchar-text').each(function (index, domEle) {
       $(domEle).limit('255');
   });
   
   if(lastIndex<4)
      lastIndex = 4;
 return lastIndex;
}

// Declare namespace
function SmartOHS() { }

SmartOHS.openPrintableVersion= function(objLink) {
	var s_width = parseInt(parent.document.documentElement.clientWidth);
	if (s_width == 0) s_width = parseInt(parent.document.body.clientWidth);
	var s_height = parseInt(parent.document.documentElement.clientHeight);
	if (s_height == 0) s_height = parseInt(parent.document.body.clientHeight);

	var pWin = window.open(objLink.href,'_printVer','top=100,left=100,height=' + (s_height - 200) + ',width=' + (s_width - 200) + ',resizable=yes,scrollbars=yes,menubar=yes');
	pWin.focus();
	return false;
}

SmartOHS.openFirstLink = function(objDiv) {
	var aLinks = objDiv.getElementsByTagName("A");
	if (aLinks.length > 0) {
		document.location.href = aLinks[0].href;
	}
}
SmartOHS.insertTemplateField = function(mergeField, textField)
{
	var textBox = document.getElementById(textField + '_edit');
	var textBoxSource = document.getElementById(textField + '_editSource');

	if (textBoxSource.style.display == 'none') {
		alert('To insert a mail merge field you must select the "View HTML Source" option at the very bottom of the Email Text Editor. Once this option is selected proceed to insert the required mail merge field and then de-select the "View HTML Source" option to continue editing the message content as required');
	} else if (textBoxSource.style.display == 'block') {
		//Update the message display by adding the merge tag
		//IE support
		if (document.selection) {
			textBoxSource.focus();
			sel = document.selection.createRange();
			sel.text = mergeField;
		}
   		//MOZILLA/NETSCAPE support
   		else if (textBoxSource.selectionStart || textBoxSource.selectionStart == '0') {
			var startPos = textBoxSource.selectionStart;
			var endPos = textBoxSource.selectionEnd;
			textBoxSource.value = textBoxSource.value.substring(0, startPos)
			+ mergeField
			+ textBoxSource.value.substring(endPos, textBoxSource.value.length);
   		} else {
			textBoxSource.value += mergeField;
   		}
	} else {
		//Do Nothing
	}
   //Set the focus to the messageBox so the user can enter more characters
   textBoxSource.focus();
}
function ToggleFAQ(faqId) {
	var imgObj = document.getElementById('faqimg_' + faqId);
	var contentObj = document.getElementById('faq_' + faqId);
	var wrapperObj = document.getElementById('faqcont_' + faqId);
	var maxHeight = contentObj.offsetHeight;
	
	if (imgObj.setVisible != 1) {
		// set FAQ visible
		imgObj.setVisible = 1;
		imgObj.src = "/i/btn_close.gif";
		imgObj.alt = "Hide Response";
		//contentObj.style.display='block';
		cms_fadeRoll('faq_' + faqId, 0, 100, 400, 'faqcont_' + faqId, 0, maxHeight + 12);
	} else {
		// hide FAQ
		imgObj.setVisible = 0;
		imgObj.src = "/i/btn_view.gif";
		imgObj.alt = "View Response";
		//contentObj.style.display='none';
		cms_fadeRoll('faq_' + faqId, 100, 0, 400, 'faqcont_' + faqId, maxHeight + 12, 0);
	}
	
	return false;
}
function incidentFormSubmit() {
        var questionField = document.getElementById('data_text17');
	if (questionField && questionField.value == 'Yes')
	{
		document.getElementById('savebutton').style.display='none';
		document.getElementById('nextbutton').style.display='block';
	} else if (questionField && questionField.value == 'No')
	{
		document.getElementById('savebutton').style.display='block';
		document.getElementById('nextbutton').style.display='none';
	} else
	{
		document.getElementById('savebutton').style.display='none';
		document.getElementById('nextbutton').style.display='none';
	}
}
function emailInsurer() {
	if (document.getElementById('data_text08:17').value == 'Yes')
	{
		document.getElementById('insurer_email').style.display='table-row';
	} else
	{
		document.getElementById('insurer_email').style.display='none';
	}
}
function updateLTIFR(){
  if (document.getElementById('data_number10').value != null &&  

document.getElementById('data_number09').value != null)
  {

    var a = new Number(document.getElementById('data_number09').value);
    var b = new Number(document.getElementById('data_number10').value);
    if (a > 0 && b > 0)
    {
      var r = (a * 1000000) / b;
      if (r > 0 && r < 1000000000)
      {
        document.getElementById('data_text21').value = r.toFixed(1);

      }
    }
  }
} 
function confirmDelete(memberID) {

   var dataID = parseInt(memberID);
   if (dataID > 0) {
       var confirmResult = confirm("Are you sure you wish to delete this Member Area? This action can not be undone.");

        if (confirmResult === true) {
            window.location = "/default.aspx?d=" + dataID + "&f=execute&_cfn=1";
            return true;
        } else if (confirmResult === false) {
            return false;
        }
        
   } else {
       return false;
   }
   return false;
}
function getRangeObject(selectionObject) {
	if (selectionObject.getRangeAt)
		return selectionObject.getRangeAt(0);
	else { // Safari!
		var range = document.createRange();
		range.setStart(selectionObject.anchorNode,selectionObject.anchorOffset);
		range.setEnd(selectionObject.focusNode,selectionObject.focusOffset);
		return range;
	}
}
