var corpse=null;
var report={};
var browser=[];
{//Library include--------------------------------------------------------------------------------------------------------------------------------------------------------------
	function include(file){
		var head=document.getElementsByTagName('head').item(0);     
		var script=document.createElement('script');
		script.src=file;
		script.type='text/javascript';
		script._onload=function(){ if(typeof(this.oncomplete)!="undefined" && this.oncomplete!=null){ this.oncomplete(); } }		

		if(browser.isOp){ setTimeout(function(){ script._onload(); },20); }
		else if(browser.isIE){ script.onreadystatechange=function(){ if(this.readyState=="loaded"||this.readyState=="complete"){ this._onload(); } } }
		else{ script.onload=script._onload; }

		head.appendChild(script);
		return script;
	}
}
{//Cookie get--------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function GetCookie(name){
		if(document.cookie.length>0){
			var posStart=document.cookie.indexOf(name+"=");
			if(posStart!=-1){
				posStart=posStart+name.length+1;
				var posEnd=document.cookie.indexOf(";",posStart);
				if(posEnd==-1){ posEnd=document.cookie.length; }
				return unescape(document.cookie.substring(posStart,posEnd));
			}
		}
		return null;
	}
}
{//Language string---------------------------------------------------------------------------------------------------------------------------------------------------------------
	function LngStr(value,points){
		return languageStrings[value]+((typeof(points)!="undefined" && points==true)?':':'');
	}
	var lngstr=LngStr;
}
{//Set opacity-------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function SetOpacity(element,opacity){
		element.style.opacity=(opacity/200);
		element.style.MozOpacity=(opacity/100);
		element.style.KhtmlOpacity=(opacity/100);
		if(browser.isIE){ element.style.filter="alpha(opacity="+opacity+")"; }
	}
}
{//Document height---------------------------------------------------------------------------------------------------------------------------------------------------------------
	function GetDocumentHeight(){
		return Math.max(Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
		Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
		Math.max(document.body.clientHeight, document.documentElement.clientHeight))-0;
	}
}
{//Set loading-------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function SetLoading(value){
		if(value){
			SetOpacity(fullscreen,70);
			fullscreen.style.display='block';
			loading.style.display='block';
			paper.style.display='none';
		}
		else{
			fullscreen.style.display='none';
			loading.style.display='none';
			paper.style.display='block';
		}
	}
}
{//Function body load------------------------------------------------------------------------------------------------------------------------------------------------------------
	function ondocumentload(){
		{//Detect browser app----------------------------------------------------------------------------------------------------------------------------------------------------
			browser.agent=navigator.userAgent.toLowerCase();
			browser.isIE=!!top.execScript;
			browser.isFF1=/a/[-1]=='a';
			browser.isFF2=(function x(){})[-6]=='x';
			browser.isFF3=(function x(){})[-5]=='x';
			browser.isFF=browser.agent.indexOf("firefox")!=-1;
			browser.isSaf=browser.agent.indexOf('safari')!=-1;
			browser.isChr=browser.agent.indexOf('chrome')!=-1;
			browser.isOp=browser.agent.indexOf('opera')!=-1;
		//	/^function \(/.test([].sort);
		}
		var language=GetCookie('lng');
		if(typeof(language)=="undefined"){ language=GetCookie('language'); }
		var sc=include('../languages/'+language+'/reports.lng');
		sc.oncomplete=function(){
			var sc=include('report.'+POST['_target']+'.js');
			sc.oncomplete=function(){ include('control.paper.js'); }
		}

	}
}
{//Add event---------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function AddEvent(obj,event,fn){
		if(obj.addEventListener){ obj.addEventListener(event,fn,false); }
		else if (obj.attachEvent){ obj.attachEvent('on'+event,fn); }
		else { obj['on'+event]=fn; }
	}
}
{//Convert functions-------------------------------------------------------------------------------------------------------------------------------------------------------------
	var Convert = {
		Int:function(value){
			return parseInt(value, 10); 
		},
		Float: function(value){
			return parseFloat(value); 
		},
		Weekday:function(value){
			var day = value.getDay();
			if(day==0){ day=7; }
			return day;
		},
		Date:function(value, showWeekDay, separator){
			if(typeof(separator)=="undefined"){ separator = '/'; }
			if(typeof(showWeekDay)=="undefined"){ showWeekDay = false; }
			var year=value.substring(0,4);
			var month=value.substring(4,6);
			var day=value.substring(6,8);
			var weekday='';
			if(showWeekDay){ weekday+=' ['+LngStr('wday_'+Convert.Weekday(new Date(year,month-1,day)))+']'; }
			return day+separator+month+separator+year+weekday;
		},
		Time:function(value, seconds){
			if(typeof(seconds)=="undefined"){ seconds=false; }
			var strValue=value.substring(0,2)+':'+value.substring(2,4);
			if(seconds){ strValue += ':'+value.substring(4,6); }
			return strValue;
		},
		Minutes:function(value){
			var hours=Convert.Int(Math.abs(parseInt(value/60,10)));
			var minutes=Convert.Int(Math.abs(value%60));
			return (value<0 ? '-':'')+Convert.PadLeft(hours,'0',2)+':'+Convert.PadLeft(minutes,'0',2);
		},
		Now:function(){ return new Date(); },
		DateNow:function(){
			var value = Convert.Now();
			return Convert.PadLeft(value.getDate(),'0',2)+'/'+Convert.PadLeft(value.getMonth()+1,'0',2)+'/'+value.getFullYear();
		},
		WeekDay:function(value){
			value=value.getDay();
			return iif(value==0,6,value-1);
		},
		FromStoreDate:function(value){
			return new Date(value.substr(0,4),value.substr(4,2)-1,value.substr(6,2));
		},
		ToStoreDate:function(value){
			return value.getFullYear()+Convert.PadLeft(value.getMonth()+1,'0',2)+Convert.PadLeft(value.getDate(),'0',2);
		},
		TimeNow:function(){
			var value=Convert.Now();
			return Convert.PadLeft(value.getHours(),'0',2)+':'+Convert.PadLeft(value.getMinutes(),'0',2)+':'+Convert.PadLeft(value.getSeconds(),'0',2);
		},
		PadLeft:function(value,chr,length){
			value=value+'';
			if(value.length<length){
				for(var i=0;i<length;i++){ value = chr+value; }
				return value.substring(value.length-length);
			}
			else { return value; }
		},
		PadRight:function(value,chr,length){
			value=value+'';
			if(value.length<length) {
				for(var i=0;i<length;i++) { value = value+chr; }
				return value.substring(value.lenght-length);
			}
			else{ return value; }
		},
		Format:function(){
			var num = arguments.length; 
			var str = arguments[0];   
			for(var i=1;i<num;i++){ 
				var pattern="\\{" + (i-1) + "\\}"; 
				var reg=new RegExp(pattern, "g"); 
				str=str.replace(reg, arguments[i]); 
			}
			return str; 
		},
		DayMonth: function(date){
			return Convert.PadLeft(date.getDate(),'0',2)+' '+LngStr('of')+' '+LngStr('month_'+(date.getMonth()+1));
		},
		AddDays:function(value,days){
			var date=new Date(value);
			date.setDate(date.getDate()+days);
			return date;
		}
	}
}
{//Ajax--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	var Ajax=function(query,wait){
		if(typeof(wait)=="undefined"){ wait=false; }
		var obj={};
		if (!(window.XMLHttpRequest!=null && (obj.xmlhttp=new XMLHttpRequest())!=null
		|| window.ActiveXObject!=null && (obj.xmlhttp=new ActiveXObject("MSXML2.XMLHTTP"))!=null
		|| window.ActiveXObject!=null && (obj.xmlhttp=new ActiveXObject("MSXML2.XMLHTTP.3.0"))!=null
		|| window.ActiveXObject!=null && (obj.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"))!=null)){
			alert("Your browser don't support XMLHTTP");
		}
		obj.text=null;
		obj.json=null;
		obj.query=query;
		obj.wait=wait;
		obj.Send=function(){
			report.ajaxs.push(obj);
			this.xmlhttp.open('POST','../lib/php/ajax_db.php',!wait);
			this.xmlhttp.onreadystatechange=this._statechange;
			this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.xmlhttp.send('&jTokeN='+GetCookie('jTokeN')+'&query='+this.query);
		}
		obj._statechange=function(){
			if(obj.xmlhttp.readyState==4){
				obj.text=obj.xmlhttp.responseText;
				try{ obj.json=eval("("+obj.text+")"); }
				catch(e){ obj.json={ 'error':e.message, length: 0 }; }
				if(typeof(obj.oncomplete)!="undefined"){ obj.oncomplete(); }
				if(typeof(obj._manager_complete)!="undefined"){ obj._manager_complete(); }
			}
		}
		if(wait){
			obj.Send();
		}
		return obj;
	}
	var AjaxManager={
		group: {},
		Create: function(key){ this.group[key]=new AjaxGroup(); }
	}
	var AjaxGroup=function(){
		var obj={};
		obj.scripts=[];
		obj.progressive=false
		obj.Add=function(object){
			object.index_manager=this.scripts.length;
			this.scripts.push(object);
			object._manager_complete=function(){
				var state=true;
				for(var i=0,iLen=obj.scripts.length;i<iLen;i++){
					if(obj.scripts[i].xmlhttp.readyState!=4){ state=false;break; }
				}
				if(obj.progressive){
					var next=this.index_manager+1;
					if(next<obj.scripts.length) { obj.scripts[next].Send(); }
				}
				if(state && typeof(obj.oncomplete)!="undefined"){ obj.oncomplete(); }
			}
		}
		obj.Start=function(){
			this.progressive=false;
			if(this.scripts.length==0 && typeof(this.oncomplete)!="undefined") { this.oncomplete(); }
			for(var i=0,iLen=this.scripts.length;i<iLen;i++){ this.scripts[i].Send(); }
		}
		obj.ProgressiveStart=function(){
			this.progressive=true;
			if(this.scripts.length>0){ this.scripts[0].Send(); }
		}
		return obj;
	}
}
{//Functions---------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function IsDefined(value){
		return (value!=undefined && typeof(value)!="undefined" && value!=null);
	}
	function iif(condition,value_true,value_false){
		return condition?value_true:value_false;
	}
	function IsInEmployees(code){
		if(USER.root==1 || USER.empl_acc_all==1){ return true; }
		for(var i=0,iLen=EMPLOYEES.length;i<iLen;i++){ if(code==EMPLOYEES[i]){ return true; } }
		return false;
	}
	function FunctionExist(fn){ return typeof(fn)!="undefined"?true:false; }
	function WeekNumber(value){
		var date= Convert.FromStoreDate(value);
		var first = new Date(date.getFullYear(),0,1);
		return Math.ceil((((date - first) / 86400000) + first.getDay())/7);
	}
	function FormatWeek(week,year){
		var first=new Date(year,0,1)
		var day=Convert.WeekDay(first);
		var date=Convert.AddDays(first,-day);
		week--;
		return Convert.Format("{0} {1}: {2} - {3}",LngStr('week'),week+1,Convert.DayMonth(Convert.AddDays(date,7*week)),Convert.DayMonth(Convert.AddDays(date,(7*week)+6)));
	}
	function atext(object, index){
		if(typeof(object)=="undefined"){
			return LngStr('undefined');
		}
		else{
			if(typeof(object[index])!="undefined"){
				return object[index].text;
			}
			else{
				return index+' - '+LngStr('unknown');
			}
		}
	}
	function Size(value){
		var length = 0;
		for(var key in value){
			if(value.hasOwnProperty(key)){
				++length;
			}
		}
		return length;
	}	
}
{//Check ajax finish---------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function CheckAjaxFinish(fn){ setTimeout(function(){ _CheckAjaxFinish(fn); },500); }
	function _CheckAjaxFinish(fn){
		var exit=true;
		for(var i in report.ajaxs){
			if(report.ajaxs[i].xmlhttp.readyState!=4){ exit=false; break; }
		}
		if(exit){ fn(); }
		else{ CheckAjaxFinish(fn); }
	}
}
{//Measure functions
	function CreateRuler(){
		var span = document.createElement('span');
		span.id='ruler';
		span.style.visibility='hidden';
		span.style.whiteSpace='nowrap';
		body.appendChild(span);
	}
	function MeasureRow(value,width){
		var height=0;
		var ruler = document.getElementById('ruler');
		ruler.style.width=width+'px';
		ruler.style.height=1+'px';
		ruler.innerHTML=value;
		return ruler.offsetHeight;
	}
}
AddEvent(window,'load',ondocumentload);