//========================================================
//= Lokace objektu podle id
//========================================================
function GetObj(obj_id) {
	return document.getElementById(obj_id);
}


function GetValue(obj_id) {
	return GetObj(obj_id).value;
}

function ToggleDisplay(obj_id) {
	var obj = GetObj(obj_id);
	if (obj.style.display == 'none') {
		obj.style.display = '';
		return true;
	}
	else {
		obj.style.display = 'none';
		return false;
	}
}

function ToggleObj(obj_id) {
	var obj = GetObj(obj_id);
	var index = obj.className.indexOf('Invisible');
	if (index > -1)
	{
		obj.className = obj.className.substr(0,index) + ' ' + obj.className.substr(index + 'Invisible'.length);				
		return true;
	}
	else
	{
		obj.className = obj.className + ' Invisible';
		return false;
	}
}

function IsZL(par) {
	if (par.replace(/ /g,'')=='')
		return true;
	else
		return false;
}

// IsWhiteSpace
function IsWS(par) {
	if (par.replace(/ /g,'')=='')
		return true;
	else
		return false;
}

function findNode(parentNode,nodeName) {	
	nodeName = nodeName.toLowerCase();
	for (var i = 0; i < parentNode.childNodes.length; i++) {
		if (parentNode.childNodes[i].nodeName.toLowerCase() == nodeName)
			return parentNode.childNodes[i];
	}								
}

function changeCss(obj,className) {
	obj.style.display = 'none';
	obj.className = className;
	obj.style.display = '';
}

//========================================================
//= Pridat metodu attachEvent a detachEvent, pokud neexistuje
//========================================================
if (!window.attachEvent) {
	if (window.addEventListener) {
		window.attachEvent = function (par_event_name,par_function)
			{ window.addEventListener(par_event_name.right(par_event_name.length-2),par_function,false); }
	}
}

if (!window.detachEvent) {
	if (window.removeEventListener) {
		window.detachEvent = function (par_event_name,par_function)
			{ window.removeEventListener(par_event_name.right(par_event_name.length-2),par_function,false); }
	}
}

isCharPressed = function (event, char_par)
	{
		var keyCode = (typeof event.charCode == 'number') ? event.charCode : event.keyCode;
		if (keyCode < 1)
			return false;
		if (String.fromCharCode(keyCode) == char_par)
			return true;
		else
			return false;
	};

function OnKeyPress_Eval(reg_pattern, evt)
{								
	var keyCode = (typeof evt.charCode == 'number') ? evt.charCode : evt.keyCode;
	if (keyCode < 1)
		return true;					
	var chr = String.fromCharCode(keyCode);						
	var re = new RegExp(reg_pattern,'igm');
	if (re.test(chr))
		return true;
	else
		return false;								
}


//========================================================
//= OSETRENI DIALOGOVYCH OKEN
//========================================================
var aryDialog = new Array();
var is_updated_blur = false;
var is_updated_focus = false;
 
window.showDialog = function (url,feature,refresh_function) {
	if ('showModalDialog' in window) {		
		window.showModalDialog(url,null,feature);
		if (typeof refresh_function == 'function' || typeof refresh_function == 'object')
			refresh_function();
	} else {
		feature = feature.replace(/ /gi,'').replace(/:/gi,'=').replace(/dialogHeight/gi,'height').replace(/dialogWidth/gi,'width').replace(/;/gi,',').replace(/scroll/gi,'scrollbars');
		feature = feature.replace(/px/gi,'');		
		if (feature.indexOf('center=yes') > -1) {
			feature = feature.replace(/,?center=yes,?/gi,',');			
			var width = null;
			var height = null;
			try 
			{ width = parseInt(feature.substr(feature.indexOf('width=')+6)); }
			catch (Exc) {}
			if (!isNaN(width))
				feature += ',left='+Math.round((screen.width-width)/2);
			
			try 
			{ height = parseInt(feature.substr(feature.indexOf('height=')+7)); }
			catch (Exc) {}
			if (!isNaN(height))
				feature += ',top='+Math.round((screen.height-height)/2);
			feature += ',directories=no,location=no,menubar=no,toolbar=no';	
			feature = feature.replace(/,,/gi,',');
		}
		window.attachEvent('onfocus',checkDialogWindowsHelper_focus);
		window.attachEvent('onblur',checkDialogWindowsHelper_blur);		
		var _window = window.open(url,null,feature);		
		is_updated_blur = false;
		is_updated_focus = true;				
		if (typeof refresh_function == 'function' || typeof refresh_function == 'object') {
			aryDialog.push(new Array(_window,refresh_function));			
			setTimeout('checkDialogWindows()',1000,'javascript');
		}		
	}	
}

function checkDialogWindows() {
	if (aryDialog.length == 0)
		return;
	var aryTemp = new Array();	
	for (var i in aryDialog) {		
		if (aryDialog[i][0] == null || aryDialog[i][0].closed || (!is_updated_blur && !is_updated_focus)) {
			aryDialog[i][1]();
			if (aryDialog[i][0] != null && !aryDialog[i][0].closed && (!is_updated_blur && !is_updated_focus))
				aryTemp.push(aryDialog[i]);
		} else {
			aryTemp.push(aryDialog[i]);
		}
	}
	aryDialog = aryTemp;	
	if (aryDialog.length > 0)
		setTimeout('checkDialogWindows()',1000,'javascript');
	else {
		window.detachEvent('onfocus',checkDialogWindowsHelper_focus);
		window.detachEvent('onblur',checkDialogWindowsHelper_blur);
	}
	if (!is_updated_blur && !is_updated_focus) {
		is_updated_focus = true;
		is_updated_blur = true;
	}
}

function checkDialogWindowsHelper_focus() {
	if (!is_updated_blur) {
		is_updated_focus = false;
		checkDialogWindows();
	}
}

function checkDialogWindowsHelper_blur() {
	is_updated_blur = false;
}

//========================================================
//= STRING OBJECT
//========================================================

String.prototype.left = function (length) {
	try {
		var len = this.length;	
		return this.substr(0,length);
	}
	catch (e)
	{ return this; }
}

String.prototype.right = function (length) {
	try {
		var len = this.length;	
		return this.substr(len-length);
	}
	catch (e)
	{ return this; }
}

String.prototype.trim = function (TrimStr) {
	if (TrimStr == null) TrimStr = ' ';
	StringPar = LTrim(StringPar,TrimStr);
	StringPar = RTrim(StringPar,TrimStr);
	return StringPar;
	try {
		var len = this.length;	
		return this.substr(len-length);
	}
	catch (e)
	{ return this; }
}

//========================================================
//= ZALOZKY
//========================================================
function zalozka_ucho_onClick(vybrana_zalozka,zalozka,master_id,reload){									
	var colZalozkyUcha = GetObj('zalozky_ucha_'+master_id).childNodes;
	var objZalozkaContainer = GetObj('zalozka_container_'+master_id);
	var str_vybrany_box = 'box_'+zalozka+'_'+master_id;
	var objZalozkaBox = GetObj(str_vybrany_box);
	// KLIK NA AKTIVNI ZALOZKU => SCHOVAT JI
	if (vybrana_zalozka.className == 'zalozka-ucho-aktivni') {
		changeCss(vybrana_zalozka,'zalozka-ucho');
		changeCss(objZalozkaContainer,'off');
		changeCss(objZalozkaBox,'off');					
		if (reload) objZalozkaBox.innerHTML = '';
		// HRANICE					
		for (var i=0; i < colZalozkyUcha.length; i++)
		{ 
			if (colZalozkyUcha[i].nodeName.toLowerCase() == 'td')
			{
				if (colZalozkyUcha[i].className == 'zalozka-hranice')
					changeCss(colZalozkyUcha[i],'zalozka-hranice-off');
				else
					changeCss(colZalozkyUcha[i],'zalozka-ucho');
			}
		}
		return false;
	}
	// SCHOVAT VSECHNY USISKA
	for (var i=0; i < colZalozkyUcha.length; i++)
	{ 
		if (colZalozkyUcha[i].nodeName.toLowerCase() == 'td')
		{												
			if (colZalozkyUcha[i].className.left(15) == 'zalozka-hranice')
				colZalozkyUcha[i].className = 'zalozka-hranice';
			else
				changeCss(colZalozkyUcha[i],'zalozka-ucho-1');
		}
	}
	// SCHOVAT VSECHNY BOXY AZ NA VYBRANY
	var box_container = findNode(objZalozkaContainer,'td');
	for (var i = 0; i < box_container.childNodes.length; i++) {
		var box = box_container.childNodes[i];
		if (Left(box.id,3)=='box') {						
			if (str_vybrany_box == box.id)
				changeCss(box,'zalozka-box');							
			else
				changeCss(box,'off');
		}
	}				
	// ZVYRAZNIT VYBRANOU ZALOZKU			
	changeCss(vybrana_zalozka,'zalozka-ucho-aktivni');
	changeCss(objZalozkaContainer,'zalozka-radek');
	if (reload)
	{					
		var asp_script = document.location.href.substr(0,document.location.href.indexOf('.asp'))+'.asp';
		asp_script = asp_script.left(asp_script.lastIndexOf('\/'))+'\/la_'+asp_script.substr(asp_script.lastIndexOf('\/')+1);
		get_server_data(asp_script+'?zalozka='+zalozka+'&master_id='+master_id);
	}
	return false;
}

//========================================================
//= Existuje datum a cas ve formatu 'dd.mm.rrrr hh:mm:ss'
//========================================================
function IsDateTime(strDate)
{
	try
	{
		var DatePart, TimePart, aryDate, aryTime, re;
		var year, month, day, hour, minute, second;						
		strDate = strDate.replace(/  /g,' ');
		//========================================================
		//= Test datumove casti
		DatePart = strDate.substring(0,(strDate+' ').indexOf(' '));
		DatePart = DatePart.replace(/ /g,'');					
		aryDate = DatePart.split('.');		
		aryDate[0] = LTrim(aryDate[0],'0');
		aryDate[1] = LTrim(aryDate[1],'0');
		aryDate[2] = LTrim(aryDate[2],'0');
		day = parseFloat(aryDate[0]);						
		month = parseFloat(aryDate[1]);
		year = parseFloat(aryDate[2]);												
		if (isNaN(day) || isNaN(month) || isNaN(year))
			return false;
		if (day.toString()!=aryDate[0] || month.toString()!=aryDate[1] || year.toString()!=aryDate[2])
			return false;			
		if (year < 999 || year > 9999)
			return false;
		var myDateChecked = new Date(year, month - 1, day);				
		if (!(myDateChecked.getDate() == day && myDateChecked.getMonth() == (month - 1) && myDateChecked.getFullYear() == year))
			return false;
		//========================================================
		//= Test casove casti						
		strDate += ' ';
		TimePart = strDate.substr(strDate.indexOf(' ')+1);		
		TimePart = TimePart.replace(/ /g,'');					
		if (TimePart=='')
			return true;
		else
			TimePart += ":0:0";			
		aryTime = TimePart.split(':',3);
		aryTime[0] = LTrim(aryTime[0],'0'); if (aryTime[0]=='') aryTime[0] = '0';
		aryTime[1] = LTrim(aryTime[1],'0'); if (aryTime[1]=='') aryTime[1] = '0';
		aryTime[2] = LTrim(aryTime[2],'0'); if (aryTime[2]=='') aryTime[2] = '0';
		hour = parseFloat(aryTime[0]);
		minute = parseFloat(aryTime[1]);
		second = parseFloat(aryTime[2]);
		if (isNaN(hour) || isNaN(minute) || isNaN(second))
			return false;
		if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59)
			return false;
		else
			return true;						
	}
	catch (e)
	{
		return false;			
	}
}											

//========================================
//= IsEmail
//========================================
function IsEmail(Value) {
	var re = /^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/i;
	if (!re.test(Value))
		return false;
	else
		return true;
}

//========================================
//= NormalizeDateTime
//========================================
function NormalizeDateTime(strDate) {
	if (!IsDateTime(strDate))
		return '';
	
	var oDate = ToDateTime(strDate);
	var year, month, day, hours, minutes, seconds;						
	var result;
	year = oDate.getFullYear();
	month = oDate.getMonth()+1;
	day = oDate.getDate();
	hours = oDate.getHours();
	minutes = oDate.getMinutes();
	seconds = oDate.getSeconds();
	result = day.toString()+'.'+month+'.'+year
	if (!(seconds == 0 && minutes == 0 && hours == 0)) {
		if (seconds < 10)
			seconds = '0'+seconds.toString();
		if (minutes < 10)
			minutes = '0'+minutes.toString();			
		result = result+' '+hours+':'+minutes+':'+seconds;
	}
	return result;
}

//=====================================
//= IsFloat - je Par cislo typu float?
//=====================================
function IsFloat(Par)
{
	try {
		var strNumber = Par.replace(/,/g,'.');	
		strNumber = strNumber.replace(/ /g,'');	
		var fltNumber = parseFloat(strNumber);	
		if (isNaN(fltNumber))
			return false;
		if (strNumber==fltNumber.toString())		
			return true;
		else
			return false;
	}
	catch (e)
	{ return false; }
}

//=====================================
//= IsInt - je Par cislo typu integer?
//=====================================
function IsInt(Par)
{	
	try {
		var strNumber = Par.replace(/ /g,'');			
		var intNumber = parseInt(strNumber);	
		if (isNaN(intNumber))
			return false;
		if (strNumber==intNumber.toString())		
			return true;
		else
			return false;
	}
	catch (e)
	{ return false; }
}

//========================================================
//= DYNAMIC LOADING
//========================================================
var ss_xmlhttp, ss_alert, ss_destination = null, ss_url = null, processing_request = false;

/*@cc_on @*/
/*@if (@_jscript_version >= 5);
	try 
	{ ss_xmlhttp=new ActiveXObject("Msxml2.XMLHTTP") }
	catch(e){
		try
		{ ss_xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") }
		catch(E){}
	};
@else;
	ss_xmlhttp=false;
	ss_alert=true;
@end @*/
	
if (!ss_xmlhttp && !ss_alert) {
	try 
	{ ss_xmlhttp=new XMLHttpRequest(); }
	catch (e) {}
}

function get_server_data(url) {
	if (processing_request)		
		window.setTimeout('get_server_data(\''+url+'\');',200);
	else {
		window.setTimeout('xmlhttp_request(\''+url+'\');',200);
		processing_request = true;
	}
}

function xmlhttp_request(url) {
	if(ss_xmlhttp) {
		ss_xmlhttp.open("GET",url,true);
		ss_xmlhttp.onreadystatechange=delivery;
		ss_xmlhttp.send(null);
	}
}

function delivery() {
	if (ss_xmlhttp.readyState==4) {
		try {						
			//alert(ss_xmlhttp.responseText);
			var ary_part = ss_xmlhttp.responseText.split('\x0b');
			var i;				
			for (i in ary_part) {
				var output, part = ary_part[i];				
				var destination = part.substr(0,part.indexOf('|'));
				output = part.substr(destination.length+1);
				if (!GetObj(destination)) 
					alert('Missing destination: '+destination+'\n\n'+part);
				else
					GetObj(destination).innerHTML = output;
			}						
		}
		finally
		{ processing_request = false; }
	}
}
