var chain = ['department','section','division','units','sub_section','sub_division','sub_unit','office','team'];
$(document).ready(function(){
    var site_host_path=$('#site_host_path_input').val();
	var system_language=$('#system_language_input').val();
	var system_company=$('#system_company_input').val();
	var system_branch=$('#system_branch_input').val();
	var is_system_name=$('#system_name_input').val();
	var forms=document.getElementsByTagName('form');
	for(var i in forms){
	if(typeof forms[i].name=='undefined') continue;
	if(typeof forms[i].setAttribute!='undefined'){
	forms[i].setAttribute("autocomplete", "off");
	}
	}
	anti_firebug(site_host_path);
 });

function detectKey(obj) 
{
	if(typeof jQuery != "undefined"){
	    the_event = $.browser.msie ? event : obj;
	}else{
		the_event = event;
	}
    k = the_event.keyCode;
    if (the_event.ctrlKey && document.getElementById('submit_login_validation') == null)
	{
	   if(k=='90'){
		   if(typeof document.form.Condition !="undefined"){
		   var str=document.form.Condition.value;
		   str=str.substr(0,str.length-2);
		   document.form.Condition.value=str;
		   }
		   
	   }
	   if(k=='83')
	   {
		
		if($.browser.msie){
			 top.menu.save.click(); 
		}else{
		 	$(top.menu.document).find('img[name=save]').parent(0).click();
		}
		  return false;
	   }
	   else if(k=='68')
	   {
		if($.browser.msie){
			 top.menu.delete1.click();
		}else{
		 	$(top.menu.document).find('img[name=delete1]').parent(0).click();
		}
		 return false;
	   }
     else if(k=='78')
	   {
		if($.browser.msie){
			 top.menu.new1.click();
		}else{
		 	$(top.menu.document).find('img[name=new1]').parent(0).click();
		}

		 return false;
	   } 
	else if(k=='65')
	   {
		if($.browser.msie){
			 top.menu.search1.click();
		}else{
		 	$(top.menu.document).find('img[name=search1]').parent(0).click();
		}

		 return false;
	  } 
	}
	else if(k=='121' && document.getElementById('submit_login_validation') ==null)
	  {
       show_fast_inquiry();		
  	   return false;
     } 
 	else if(k=='120' && document.getElementById('submit_login_validation') ==null)
	  {
       show_employee_cost();		
  	   return false;
	   } else if(k=='119')
	   {
		 show_org_chart();
	   }
	 else if(k=='13'){
	   if(document.getElementById('submit_login_validation') !=null){
		   go_check(document.form);
		   }
	 }
	logoff();
}
document.onkeydown = detectKey;

function include_dictionary(site_host_path,system_language,system_company,system_branch,is_system_name){
		var shared_path   = site_host_path +'DB/Include/Shared/';
	document.write('<script src="' +shared_path + 'dictionary.php?system_language='+system_language+'&system_company='+system_company+'&system_branch='+system_branch+'&is_system_name='+is_system_name+'"></script>');
   }

function exit_frame(){
	  if (top != self)    {        top.location=self.location;    }
}
function submitForm(form){
	add('MENAHRMSPAGE');
	form.submit();
}
function anti_firebug(site_host_path){
if( window.console && window.console.firebug ){
parent.document.location.href=site_host_path+'DB/access.php?KEYMSG=You  Should Disable Firebug In Order To Use The System';
}

}
function add(name,type) {
		var forms=document.getElementsByTagName('form');
		if(type==null) type='hidden'
			for(var i in forms){
			if(typeof forms[i].name=='undefined') continue;
			
				var element = document.createElement("input");
					element.setAttribute("type", type);
					element.setAttribute("name", name);
					element.setAttribute("id", name);
					if(typeof forms[i].appendChild!='undefined')
					forms[i].appendChild(element);
					
			}
	}


function hierarchy_selectagain(obj,index)
{

matched = obj.id.match(/([a-zA-Z_]*)_\d/);

if(matched == null ) return false;

var current_chain = matched[1];

if($.inArray(current_chain , chain)==-1) return false;


starting_index = $.inArray(current_chain , chain);
	for(var i = starting_index ; i < chain.length ; i++){
		obj_name = chain[i];
		next_obj_name = chain[i+1];
		if(get_by_id('position_'+index)) selectagain_position_dep(index);
		if(!get_by_id(next_obj_name+'_'+index)){return false;}
			if(obj.name==get_by_id(obj_name+'_'+index).name)
			{
				empty_drops(next_obj_name ,index);
					obj_name = chain[i];
					next_obj_name = chain[i+1];
				if(obj.value > 0) 
				{
					h_arr = get_chain_arr(obj_name,index);
						obj_name = chain[i];
						next_obj_name = chain[i+1];
					for(var j in h_arr)
					{ 
						var option1= new Option(h_arr[j]['system_desp'],h_arr[j]['system_code']);
						
						get_by_id(next_obj_name+'_'+index).options.add(option1);
					}
					$(get_by_id(next_obj_name+'_'+index)).trigger('change');
				    
					
				}


			}
	}
}


function get_chain_arr(last_chain , index){
		endding_index = $.inArray(last_chain , chain);
		var tmp_arr = null;
		for(var i = 0 ; i < endding_index + 1; i++){
			obj_name = chain[i];
			next_obj_name = chain[i+1];
			
			if(tmp_arr == null){
				tmp_arr = hierarchy_array[get_by_id(obj_name+'_'+index).value][next_obj_name];	
			}else{ 
			
			if(	!get_by_id(obj_name+'_'+index) 	|| !get_by_id(obj_name+'_'+index).value ) break ;
				tmp_arr = tmp_arr[get_by_id(obj_name+'_'+index).value][next_obj_name];	
			}
		}
		return  tmp_arr;
}
function empty_drops(first_chain , index){

		starting_index = $.inArray(first_chain , chain);
		for(var i = starting_index ; i < chain.length ; i++){
			obj_name = chain[i];
			if(get_by_id(obj_name+'_'+index)) 
				if(get_by_id(obj_name+'_'+index).options.length>0 && ( get_by_id(obj_name+'_'+index).options[0].value<=0)  )
					get_by_id(obj_name+'_'+index).options.length=1;
				else get_by_id(obj_name+'_'+index).options.length=0;
		}
}




function get_by_id(name){
	
	if(typeof document.getElementById(name)=='undefined' ) return 0;
	return document.getElementById(name);
}	


function search_hierarchy(clicker_id,clicker_index,code,major_code,section_code,division_code,unit_code,sub_section_code,sub_division_code,sub_unit_code,office_code,team_code)
{
	
	major=-99;
	section=-99;
	division=-99;
	unit=-99;
	sub_section=-99;
	sub_division=-99;
	sub_unit=-99;
	office=-99;
	team=-99;

    if(major_code!=null)major=major_code.value;
	if(section_code!=null)section=section_code.value;
	if(division_code!=null)division=division_code.value;
	if(unit_code!=null)unit=unit_code.value;
	if(sub_section_code!=null)sub_section=sub_section_code.value;
	if(sub_division_code!=null)sub_division=sub_division_code.value;
	if(sub_unit_code!=null)sub_unit=sub_unit_code.value;
	if(office_code!=null)office=office_code.value;
	if(team_code!=null)team=team_code.value;
	
	skip_next_one=0;
    if(major_code==null)skip_next_one=1;
	if(section_code==null)skip_next_one=1;
	if(division_code==null)skip_next_one=1;
	if(unit_code==null)skip_next_one=1;
	if(sub_section_code==null)skip_next_one=1;
	if(sub_division_code==null)skip_next_one=1;
	if(sub_unit_code==null)skip_next_one=1;
	if(office_code==null)skip_next_one=1;
	if(team_code==null)skip_next_one=1;
	
		
	if(major==-1)major=-99;
	if(section==-1)section=-99;
	if(division==-1)division=-99;
	if(unit==-1)unit=-99;
	if(sub_section==-1)sub_section=-99;
	if(sub_division==-1)sub_division=-99;
	if(sub_unit==-1)sub_unit=-99;
	if(office==-1)office=-99;
	if(team==-1)team=-99;

    positions_related=0;
	if(typeof setup_positions_related!="undefined") 
	   if(setup_positions_related[clicker_index]) 	positions_related=1;
	
	window.open("../employees/code_table_search.php?code="+code+"&clicker_id="+clicker_id+"&clicker_index="+clicker_index+"&major_code="+major+"&section_code="+section+"&division_code="+division+"&unit_code="+unit+"&sub_section_code="+sub_section+"&sub_division_code="+sub_division+"&sub_unit_code="+sub_unit+"&office_code="+office+"&team_code="+team+"&positions_related="+positions_related+"&skip_next_one="+skip_next_one,"code_tabel","width=500,height=600,scrollbars=no");
}
function select_hierarchy(clicker_id,index,sys_code,code)
{

    var len=get_by_id(clicker_id).options.length;
	for (var i=0 ; i<len ; i++) 
	{
		if(get_by_id(clicker_id).options[i].value==sys_code)
		  {
		   get_by_id(clicker_id).value=sys_code;
		   break;
	      }
	}

	  
	  $(get_by_id(clicker_id)).trigger('change');
	  
	  if(typeof select_hierarchy_default_function!="undefined")select_hierarchy_default_function(clicker_id,index,sys_code,code);
}

function selectagain_position_dep(index)
  {



    if(!setup_positions_related[index]) return;

	get_by_id('position_'+index).options.length=1;
    j=1;
	var department=0,section=0,division=0,unit=0,sub_section=0,sub_division=0,sub_unit=0,office=0,team=0;
	if(get_by_id('department_'+index)) department=get_by_id('department_'+index).value;
	if(get_by_id('section_'+index))section=get_by_id('section_'+index).value;
	if(get_by_id('division_'+index))division=get_by_id('division_'+index).value;
	
	if(get_by_id('units_'+index))unit=get_by_id('units_'+index).value;
	else if(get_by_id('unit_'+index))unit=get_by_id('unit_'+index).value;
	
	if(get_by_id('sub_section_'+index))sub_section=get_by_id('sub_section_'+index).value;
	if(get_by_id('sub_division_'+index))sub_division=get_by_id('sub_division_'+index).value;
	if(get_by_id('sub_unit_'+index))sub_unit=get_by_id('sub_unit_'+index).value;
	if(get_by_id('office_'+index))office=get_by_id('office_'+index).value;
	if(get_by_id('team_'+index))team=get_by_id('team_'+index).value;
  
    if(department==-1)department=0;
	if(section==-1)section=0;
	if(division==-1)division=0;
	if(unit==-1)unit=0;
	if(sub_section==-1)sub_section=0;
	if(sub_division==-1)sub_division=0;
	if(sub_unit==-1)sub_unit=0;
	if(office==-1)office=0;
	if(team==-1)team=0;


	var hirarchy_path = '' + department + '_' + section + '_' + division + '_' + unit + '_' + sub_section + '_' + sub_division +
	                    '_' + sub_unit + '_' + office + '_' + team + ''; 

	
	if(typeof(array_pos[hirarchy_path]) != 'undefined')
	{	
		for(key in array_pos[hirarchy_path])
		{
			var option0 = new Option(array_pos[hirarchy_path][key], key);
			eval("get_by_id('position_'+index).options[j]=option0");
			get_by_id('position_'+index).options[j].value=key;
			get_by_id('position_'+index).options[j].text=array_pos[hirarchy_path][key];
			get_by_id('position_'+index).options[j].selected;
			j++;
		}
	}
	
	else
	{	
		select_position_last(index);	
	}

	
}

function select_position_last(index)
{
	var pos_id = 0;
	var pos_name = "";
	var hirarchy_path ="";
	
	var department=0,section=0,division=0,unit=0,sub_section=0,sub_division=0,sub_unit=0,office=0,team=0;
	if(get_by_id('department_'+index)) department=get_by_id('department_'+index).value;
	if(get_by_id('section_'+index))section=get_by_id('section_'+index).value;
	if(get_by_id('division_'+index))division=get_by_id('division_'+index).value;
	
	if(get_by_id('units_'+index))unit=get_by_id('units_'+index).value;
	else if(get_by_id('unit_'+index))unit=get_by_id('unit_'+index).value;
	
	if(get_by_id('sub_section_'+index))sub_section=get_by_id('sub_section_'+index).value;
	if(get_by_id('sub_division_'+index))sub_division=get_by_id('sub_division_'+index).value;
	if(get_by_id('sub_unit_'+index))sub_unit=get_by_id('sub_unit_'+index).value;
	if(get_by_id('office_'+index))office=get_by_id('office_'+index).value;
	if(get_by_id('team_'+index))team=get_by_id('team_'+index).value;

    if(department==-1)department=0;
	if(section==-1)section=0;
	if(division==-1)division=0;
	if(unit==-1)unit=0;
	if(sub_section==-1)sub_section=0;
	if(sub_division==-1)sub_division=0;
	if(sub_unit==-1)sub_unit=0;
	if(office==-1)office=0;
	if(team==-1)team=0;

	
	
	 var hirarchy_path_team =  department + '_' + section + '_' + division + '_' + unit + '_' + sub_section + '_' + sub_division +
	                              '_' + sub_unit + '_' + office + '_0' ; 
	var hirarchy_path_office =  department + '_' + section + '_' + division + '_' + unit + '_' + sub_section + '_' + sub_division +
	                              '_' + sub_unit + '_0_0' ; 
	var hirarchy_path_sub_un = 	department + '_' + section + '_' + division + '_' + unit + '_' + sub_section + '_' + sub_division  + '_0_0_0' ; 
	var hirarchy_path_sub_div = department + '_' + section + '_' + division + '_' + unit + '_' + sub_section + '_0_0_0_0' ; 
	var hirarchy_path_sub_sec =department + '_' + section + '_' + division + '_' + unit + '_0_0_0_0_0' ; 
	var hirarchy_path_unit 	  =  department + '_' + section + '_' + division+ '_0_0_0_0_0_0' ; 
	var hirarchy_path_div 	  = department + '_' + section  + '_0_0_0_0_0_0_0' ; 
	var hirarchy_path_section = department + '_0_0_0_0_0_0_0_0' ;
	
	


     if(typeof(array_pos[hirarchy_path_section]) != 'undefined')
	 {
		hirarchy_path = hirarchy_path_section;
	 }
	 if(typeof(array_pos[hirarchy_path_div]) != 'undefined')
	 {
		hirarchy_path = hirarchy_path_div;
	 }
	 if(typeof(array_pos[hirarchy_path_unit]) != 'undefined')
	 {
		hirarchy_path = hirarchy_path_unit;
	 }
	if(typeof(array_pos[hirarchy_path_sub_sec]) != 'undefined')
	 {
		hirarchy_path = hirarchy_path_sub_sec;
	 }
	 if(typeof(array_pos[hirarchy_path_sub_div]) != 'undefined')
	 {
		hirarchy_path = hirarchy_path_sub_div;
	 }
	 if(typeof(array_pos[hirarchy_path_sub_un]) != 'undefined')
	 {
		hirarchy_path = hirarchy_path_sub_un;
	 }
	 if(typeof(array_pos[hirarchy_path_office]) != 'undefined')
	 {
		hirarchy_path = hirarchy_path_office;
	 }
	 if(typeof(array_pos[hirarchy_path_team]) != 'undefined')
	 {
		hirarchy_path = hirarchy_path_team;
	 }
	

	var hirarchy_array = new Array();
	if(typeof(array_pos[hirarchy_path]) != 'undefined')
	{	
		for(key in array_pos[hirarchy_path])
		{
			var option0 = new Option(array_pos[hirarchy_path][key], key);
			eval("get_by_id('position_'+index).options[j]=option0");
			get_by_id('position_'+index).options[j].value=key;
			get_by_id('position_'+index).options[j].text=array_pos[hirarchy_path][key];
			get_by_id('position_'+index).options[j].selected;
			j++;
		}
	}
	
}



function unserialize(data){ 
    
    var error = function (type, msg, filename, line){throw new window[type](msg, filename, line);};
    var read_until = function (data, offset, stopchr){
        var buf = [];
        var chr = data.slice(offset, offset + 1);
        var i = 2;
        while (chr != stopchr) {
            if ((i+offset) > data.length) {
                error('Error', 'Invalid');
            }
            buf.push(chr);
            chr = data.slice(offset + (i - 1),offset + i);
            i += 1;
        }
        return [buf.length, buf.join('')];
    };
    var read_chrs = function (data, offset, length){
        var buf;
        
        buf = [];
        for(var i = 0;i < length;i++){
            var chr = data.slice(offset + (i - 1),offset + i);
            buf.push(chr);
        }
        return [buf.length, buf.join('')];
    };
    var _unserialize = function (data, offset){
        var readdata;
        var readData;
        var chrs = 0;
        var ccount;
        var stringlength;
        var keyandchrs;
        var keys;
 
        if(!offset) offset = 0;
        var dtype = (data.slice(offset, offset + 1)).toLowerCase();
        
        var dataoffset = offset + 2;
        var typeconvert = new Function('x', 'return x');
        
        switch(dtype){
            case "i":
                typeconvert = new Function('x', 'return parseInt(x)');
                readData = read_until(data, dataoffset, ';');
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "b":
                typeconvert = new Function('x', 'return (parseInt(x) == 1)');
                readData = read_until(data, dataoffset, ';');
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "d":
                typeconvert = new Function('x', 'return parseFloat(x)');
                readData = read_until(data, dataoffset, ';');
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "n":
                readdata = null;
            break;
            case "s":
                ccount = read_until(data, dataoffset, ':');
                chrs = ccount[0];
                stringlength = ccount[1];
                dataoffset += chrs + 2;
                
                readData = read_chrs(data, dataoffset+1, parseInt(stringlength));
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 2;
                if(chrs != parseInt(stringlength) && chrs != readdata.length){
                    error('SyntaxError', 'String length mismatch');
                }
            break;
            case "a":
                readdata = {};
                
                keyandchrs = read_until(data, dataoffset, ':');
                chrs = keyandchrs[0];
                keys = keyandchrs[1];
                dataoffset += chrs + 2;
                
                for(var i = 0;i < parseInt(keys);i++){
                    var kprops = _unserialize(data, dataoffset);
                    var kchrs = kprops[1];
                    var key = kprops[2];
                    dataoffset += kchrs;
                    
                    var vprops = _unserialize(data, dataoffset);
                    var vchrs = vprops[1];
                    var value = vprops[2];
                    dataoffset += vchrs;
                    
                    readdata[key] = value;
                }
                
                dataoffset += 1;
            break;
            default:
                error('SyntaxError', 'Unknown / Unhandled data type(s): ' + dtype);
            break;
        }
        return [dtype, dataoffset - offset, typeconvert(readdata)];
    };
    return _unserialize(data, 0)[2];
}
function pop_employee_search(index)
{
	window.open("../employees/employees_list.php?clicker_id="+index,"employee","width=500,height=635,scrollbars=no");
}


function set_searched_user_info(emp_code,emp_name,branch_code,index)
 {
   document.getElementById('employee_code_'+index).value=emp_code;
   document.getElementById('employee_name_'+index).value=emp_name;
	  
	  $(get_by_id('employee_code_'+index)).trigger('change');
	  
   return true;
  }
    
function set_time()
{
    var timing_h=document.getElementById('timing_h');
    var timing_i=document.getElementById('timing_i');
    var timing_s=document.getElementById('timing_s');
	timing_s.innerText=parseInt(timing_s.innerText)+1;
	
	if(parseInt(timing_s.innerText)==60)
	{
	timing_i.innerText=parseInt(timing_i.innerText)+1;
	timing_s.innerText='0'
	}
	if(parseInt(timing_i.innerText)==60)
	{
	timing_h.innerText=parseInt(timing_h.innerText)+1;
	timing_i.innerText='0'
	}
}
function disable_enter_TA(obj){
	if(typeof jQuery != "undefined"){
	    the_event = $.browser.msie ? event : obj;
	}else{
		the_event = event;
	}
    k = the_event.keyCode;
	   if(k=='13') return false;
	   else detectKey(obj);
}
