// check browser 
var ie = (document.all);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var nn4 = (document.layers);
var nn6 = (document.getElementById && !ie);
var mz = (document.getElementById && !ie);
//alert('Navigator\nappName = '+navigator.appName+'\nappCodeName = '+navigator.appCodeName+'\nappVersion = '+navigator.appVersion+'\nuserAgent = '+navigator.userAgent);
var bVersion = parseInt(navigator.appVersion);


// **********************************************************************************
// ====================== UNIVERSAL FUNCTIONS ==================================
// **********************************************************************************

var $mainDivWidth = 780; // ******* change main DIV width 

// ALL MEASURES OF SCREEN, WINDOW, FRAME etc. 
var frameWidth = 0;
var frameHeight = 0;
var $innerW = 0;
var $innerH = 0;
var screenWidth = 0;
var screenHeight = 0;
var $mainDivLeft = 0;
var $mainDivTop = 0;

function allMeasures() {
	if (mz) { // MZ... 
		frameWidth = window.innerWidth;
		frameHeight = window.innerHeight;
		$innerW = window.innerWidth;
		$innerH = window.innerHeight;
	} else if (self.innerWidth) { // all except IE = FireFox, MZ, NN... 
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
		$innerW = self.innerWidth;
		$innerH = self.innerHeight;
	} else if (document.documentElement && document.documentElement.offsetWidth) { // IE 6 
		frameWidth = document.documentElement.offsetWidth;
		frameHeight = document.documentElement.offsetHeight;
		$innerW = document.documentElement.offsetWidth;
		$innerH = document.documentElement.offsetHeight;
	} else if (document.body) { // other IE 
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
		$innerW = document.body.clientWidth;
		$innerH = document.body.clientHeight;
	}
	if (self.screen) {
		screenWidth = screen.width;
		screenHeight = screen.height;
	}
	$mainDivLeft = $innerW > $mainDivWidth ? Math.ceil(($innerW - $mainDivWidth) / 2) : 0;
	$mainDivTop = 0;
	//alert(screenWidth + ' * ' + screenHeight + ' | ' + frameWidth + ' * ' + frameHeight);
	//setStatusOuter();
}
window.onresize = allMeasures;
allMeasures();

function setStatValues()	{
	document.cookie = "cnt_monitor="+ screenWidth +"x"+ screenHeight;
//	document.cookie="cnt_window="+ frameWidth +"x"+ frameHeight;
	document.cookie="cnt_color="+ (screen.pixelDepth ? screen.pixelDepth : screen.colorDepth);
}

// FOR TESTS... 
// show outer width and height in status 
function setStatusOuter() {
	$measuresMessage = '';
	//$measuresMessage += 'window outer width and height = ' + window.outerWidth + ' * ' + window.outerHeight;
	$measuresMessage += 'window inner width and height = ' + window.innerWidth + ' * ' + window.innerHeight;
	//$measuresMessage += ' :: screen width & height = ' + window.screen.availWidth + ' * ' + window.screen.availHeight;
	$measuresMessage += ' :: window from top and left = ' + window.screenY + ' * ' + window.screenX;
	$measuresMessage += ' :: main div from top and left = ' + $mainDivTop + ' * ' + $mainDivLeft;
	window.status = $measuresMessage;
	//alert($measuresMessage);
}

var $windowFromTop = window.screenY;
var $windowFromLeft = window.screenX;

// shortcut for "getElementById" (only for mz) 
function gid(what)	{
	if (document.getElementById(what)) return document.getElementById(what);
	else if (eval(what)) return eval(what);
	else return false;
}

// shortcut for "getElementById" or for ie4 or for nn4 
function elem(what)	{
	if(mz || ie5) {
		if (document.getElementById(what)) return document.getElementById(what);
		else if (eval(what)) return eval(what);
		else return false;
	}
	if(ie4) {
		if (document.all.what) return document.all.what;
		else if (eval(what)) return eval(what);
		else return false;
	}
	if(nn4) {
		if (document.layers[what]) return document.layers[what];
		else if (eval(what)) return eval(what);
		else return false;
	}
}

// if the number is smaller than 10, add in front of it "0"	[+ parameter how should be display zero "0" (e.g. "00")]
function toTen($str, $oo) {
	if ($str==0) { return $oo; }
	else if ($str>0 && $str<10) { return '0' + $str; }
	else { return $str; }
}

// **** POPUPS  **** //
// INIT 
document.onmousemove = getMouseXY;
if (nn4 || mz) document.captureEvents(Event.CLICK | Event.MOUSEMOVE)

var $axisX = 0;
var $axisY = 0;

function getMouseXY(e) {
	if (ie)	{
		$axisX = event.clientX + document.body.scrollLeft;
		$axisY = event.clientY + document.body.scrollTop;
	} else {
		$axisX = e.pageX;
		$axisY = e.pageY;
	}  
	if ($axisX < 0) { $axisX = 0; }
	if ($axisY < 0) { $axisY = 0; }
	//window.status = 'mouse x * y = ' + $axisX + ' * ' + $axisY;
	return $axisX;
	return $axisY;
}

/*
// FOR TEST MOUSE X & Y 
var mX = 0;
var mY = 0;
//if (!ie) document.captureEvents(Event.MOUSEMOVE);
function mouseXY(e) {
	if (ie) { // if browser is IE 
		mX = event.clientX + document.body.scrollLeft;
		mY = event.clientY + document.body.scrollTop;
	} else {  // if browser is other as IE 
		mX = e.pageX;
		mY = e.pageY;
	}
	if (mX < 0) { mX = 0; }
	if (mY < 0) { mY = 0; }
	window.status = 'mouse x * y = ' + mX + ' * ' + mY;
	return true;
}
// show mouse coordinate in status 
//document.onmousemove = mouseXY;
*/
/* // ORIG
function show(str)	{
	if (ie5 || nn6) {
		document.getElementById(str).style.left = axisX;
		document.getElementById(str).style.top = axisY;
	}
	if (ie4) {
		document.all.str.style.left = axisX;
		document.all.str.style.top = axisY;
	}
	if (nn4) {
		document.layers[str].style.left = axisX;
		document.layers[str].style.top = axisY;
	}
	if (ie5 || nn6)	{	document.getElementById(str).style.display = "block";	}
	if (ie4)	{	document.all.str.style.display = "block";	}
	if (nn4)	{	document.layers[str].style.display = "block";	}
}
*/

// NEW DX 
// elementId, slide in x, slide in y, abs. pos. elem. from left, abs. pos. elem. from top, elem. width, elem.height 
function show($elemId, $x, $y, $absX, $absY, $elemW, $elemH) {
	if (mz) {
		// coord of mouse x|y + abs. pos. elem. from left|top + slide x|y + element W|H + 15 scroll 
		if($axisX + $x + $elemW + 25 > $innerW) {
			$totalX = $axisX - $absX - $x - $elemW + 'px';
		} else {
			$totalX = $axisX - $absX + $x + 'px';
		}
		elem($elemId).style.left = $totalX;
		if($axisY + $y + $elemH + 25 > $innerH) {
			$totalY = $axisY - $absY + $y - $elemH;
			//elem($str).style.bottom = $totalY;
			elem($elemId).style.top = $totalY + 'px';
		} else {
			$totalY = $axisY - $absY + $y;
			elem($elemId).style.top = $totalY + 'px';
		}
		elem($elemId).style.display = "block";
		// status bar 
		//*
		window.status = ''
			//+ 'outer W|H = ' + $outerW + '|' + $outerH 
			+ ' :: inner W|H = ' + $innerW + '|' + $innerH 
			+ ' :: $axisX + $x = ' + $axisX + ' + ' + $x + ' = ' + ($axisX + $x) + ' :: $axisY + $y = ' + $axisY + ' + ' + $y + ' = ' + ($axisY + $y) 
			+ ' :: element W|H = ' + $elemW + '|' + $elemH
			+ ' :: $axisX - $absX + $x + $elemW = ' + ($axisX - $absX + $x + $elemW);
			//*/
	}
}

// COLOR BUTTON BackGround 
function colorButtonBg($id) { document.getElementById($id).style.background = '#FF3'; }
function colorOutButtonBg($id) { document.getElementById($id).style.background = '#FFF'; }

// ************************  ADMINISTRATION  ********************************************

// show Language Box 
function showLangBox($lng) {
	for($g=0; $g<$LNG_TO_SEE.length; $g++) {
		document.getElementById($LNG_TO_SEE[$g] + 'Flag').style.background = '#FFF';
		document.getElementById($LNG_TO_SEE[$g] + 'Table').style.display = 'none';
	}
	document.getElementById($lng + 'Flag').style.background = '#999';
	document.getElementById($lng + 'Table').style.display = 'block';
}


// Round Button Over/Out 
function buttonTransitOverColor($buttonId, $actio, $setColor) {
	$oldClass = document.getElementById($buttonId).className.split(" "); // array 
	if($setColor) $setColorTo = ' ' + $setColor; else $setColorTo = '';
	if($actio) {
		document.getElementById($buttonId).className = $oldClass[0] + $setColorTo;
	} else {
		document.getElementById($buttonId).className = $oldClass[0] +  $setColorTo;
	}
}
// onclick Settings Button 
function onclickSettingsButton($buttonId) {
	if(document.getElementById($buttonId + 'Div').style.display == 'none') {
		document.getElementById($buttonId).className = 'transitRound bgOrange';
		document.getElementById($buttonId + 'Div').style.display = 'block';
		document.getElementById($buttonId).childNodes[1].firstChild.nodeValue = LNG_CLOSE_SETTINGS;
	} else {
		document.getElementById($buttonId).className = 'transitRound bgGray';
		document.getElementById($buttonId + 'Div').style.display = 'none';
		document.getElementById($buttonId).childNodes[1].firstChild.nodeValue = LNG_SETTINGS;
	}
}

// COLOR BUTTON BackGround 
function colorButtonBg($id) { document.getElementById($id).style.background = '#FF3'; }
function colorOutButtonBg($id) { document.getElementById($id).style.background = '#FFF'; }

// Active / Inactive Section and Languages 
function changeActiveSection($active) {
	if($active=='0' || $active=='1') {
		document.getElementById('checkLngActive_all').checked = false;
	}
	if($active=='2') {
		document.getElementById('checkLngActive_all').checked = true;
	}
	if($active=='0' || $active=='1' || $active=='2') {
		colorActive($active);
	}
	if($active=='all') {
		if(document.getElementById('checkLngActive_all').checked == false) { // main ACTIVE is unchecked 
			document.getElementById('checkLngActive_all').checked = true;
			colorActive('2');
		} else {
			document.getElementById('checkLngActive_all').checked = false;
			colorActive('1');
		}
	}
	for($g=0; $g<$LNG.length; $g++) { colorLngActive($LNG[$g]); }
}
// Active / Inactive Section and Languages 
function changeLngActive($thisLng) {
	if(document.getElementById('checkLngActive_all').checked == true) { // main ACTIVE is checked 
		if(document.getElementById('checkLngActive_' + $thisLng).checked == false) { // is unchecked 
			document.getElementById('checkLngActive_' + $thisLng).checked = true;
		} else {
			document.getElementById('checkLngActive_' + $thisLng).checked = false;
		}
	}
	colorLngActive($thisLng);
}
// Color Active  
function colorActive($active) {
	if($active=='0') {
		document.getElementById('eyeLngActive_all').src = '../img/ik_eye_green.gif';
		document.getElementById('divLngActive_all').style.background = '#CCC'; // gray bg 
		document.getElementById('spanLngActive_all').firstChild.nodeValue = LNG_DELETED;
	}
	if($active=='1') {
		document.getElementById('eyeLngActive_all').src = '../img/ik_eye_gray.gif';
		document.getElementById('divLngActive_all').style.background = '#FFF'; // white bg 
		document.getElementById('spanLngActive_all').firstChild.nodeValue = LNG_INACTIVE;
	}
	if($active=='2') {
		document.getElementById('eyeLngActive_all').src = '../img/ik_eye_blue.gif';
		document.getElementById('divLngActive_all').style.background = '#FF9'; // yellow bg 
		document.getElementById('spanLngActive_all').firstChild.nodeValue = LNG_ACTIVE;
	}
}
// Color Languages 
function colorLngActive($thisLng) {
	if(document.getElementById('checkLngActive_all').checked == true) { // main ACTIVE is checked 
		if(document.getElementById('checkLngActive_' + $thisLng).checked == true) { // is checked 
			document.getElementById('eyeLngActive_' + $thisLng).src = '../img/ik_eye_blue.gif';
			document.getElementById('divLngActive_' + $thisLng).style.background = '#FF9'; // yellow bg 
		} else {
			document.getElementById('eyeLngActive_' + $thisLng).src = '../img/ik_eye_gray.gif';
			document.getElementById('divLngActive_' + $thisLng).style.background = '#FFF'; // white bg 
		}
	} else {
		document.getElementById('divLngActive_' + $thisLng).style.background = '#CCC'; // gray bg 
		if(document.getElementById('checkLngActive_' + $thisLng).checked == true) { // is checked 
			document.getElementById('eyeLngActive_' + $thisLng).src = '../img/ik_eye_blue.gif';
		} else {
			document.getElementById('eyeLngActive_' + $thisLng).src = '../img/ik_eye_gray.gif';
		}
	}
}

// SWITCH BETWEEN PICTURES PREVIEW (used in 'adm_shop_products') 
function switchToPic($nr, $max) {
	for($i=1; $i<=$max; $i++) {
		if($i==$nr) {
			document.getElementById('picturePreview' + $i).style.display = 'block';
		} else {
			document.getElementById('picturePreview' + $i).style.display = 'none';
		}
	}
}


// **********************************************************************************
// ====================== PROJECT SPEC. FUNCTIONS ===============================
// **********************************************************************************


// SHOW on CLIENT WEB 
function showNoOrderMessage($elemId)	{
	elem($elemId).style.left = '86px';
	elem($elemId).style.top = '30px';
	elem($elemId).style.display = "block";
}
function showChangeTerminMessage($elemId, $x, $y, $elemW, $elemH)	{
	$totalX = $axisX - $mainDivLeft;
	$totalY = $axisY - $mainDivTop;
	//alert('total X|Y = ' +$totalX + '|' +$totalY); // TEST 
	// coord of mouse x|y + abs. pos. elem. from left|top + slide x|y + element W|H + 20 scroll 
	if($axisX + $x + $elemW + 20 > $innerW - 20) {
		$totalX = $totalX - ($x +20 + $elemW);
	} else {
		$totalX = $totalX + $x + 20;
	}
	if($axisY + $y + $elemH + 20 > $innerH - 20) {
		$totalY = $totalY - ($y + 20 + $elemH);
	} else {
		$totalY = $totalY + $y;
	}
	elem($elemId).style.left = $totalX;
	elem($elemId).style.top = $totalY;
	elem($elemId).style.display = "block";
	$testMessage = ''
		+ ' :: inner W|H = ' + $innerW + '|' + $innerH 
		+ ' :: $axisX + $x = ' + $axisX + ' + ' + $x + ' = ' + ($axisX + $x) + ' :: $axisY + $y = ' + $axisY + ' + ' + $y + ' = ' + ($axisY + $y) 
		+ ' :: element W|H = ' + $elemW + '|' + $elemH
		+ ' :: total X|Y = ' + $totalX + '|' + $totalY;
	//window.status = $testMessage;
	//alert($testMessage); // TEST 
}

function hide($elemId)	{
	elem($elemId).style.display = "none";
}
// **** end POPUPS  **** //


// ADMIN - sections / rubrics 
function switchRows(trid,last)	{
	var dsp,ael=document.getElementById("tr"+trid);	
	if(navigator.appName=="Microsoft Internet Explorer") dsp="block"; else dsp="block";
	if (ael.style.display!="none")	{
		ael.style.display="none";
		document.getElementById("img"+trid).src="../img/ik_tree_p"+last+".gif";
	}
	else	{
		ael.style.display=dsp;
		document.getElementById("img"+trid).src="../img/ik_tree_m"+last+".gif";
	}
}

function switch_disp(trid,last)	{
	var dsp,ael=document.getElementById("tr"+trid);	
	if(navigator.appName=="Microsoft Internet Explorer") dsp="block"; else dsp="table-row";
	if (ael.style.display!="none")	{
		ael.style.display="none";
		document.getElementById("img"+trid).src="../img/ik_tree_p"+last+".gif";
	}
	else	{
		ael.style.display=dsp;
		document.getElementById("img"+trid).src="../img/ik_tree_m"+last+".gif";
	}
}

/*
function setStatValues()	{
	document.cookie="rozliseni_x="+ screen.width+"; path=/";
	document.cookie="rozliseni_y="+screen.height+"; path=/";
	document.cookie="velikost_okna_x="+ (self.innerWidth ? self.innerWidth : document.body.clientWidth)+"; path=/";
	document.cookie="velikost_okna_y="+ (self.innerHeight ? self.innerHeight : document.body.clientHeight)+"; path=/";
}
//setTimeout("setStatValues()",1000);
*/

// SPRINTF = like at PHP, just limited to max. 5 parts 
function sprintf($string, $part0, $part1, $part2, $part3, $part4) {
	$splitedString = $string.split("%s"); // array 
	$returnedText = $splitedString[0];
	for($i=0; $i<$splitedString.length; $i++) {
		if($i==1) { $returnedText += $part0 + $splitedString[$i]; }
		if($i==2) { $returnedText += $part1 + $splitedString[$i]; }
		if($i==3) { $returnedText += $part2 + $splitedString[$i]; }
		if($i==4) { $returnedText += $part3 + $splitedString[$i]; }
		if($i==5) { $returnedText += $part4 + $splitedString[$i]; }
	}
	return $returnedText;
}

// COUNT CHARACTER in fieldId, maxLength   
function countChar($fieldToCount, $maxLimit) {
	$alertText = sprintf(LNG_FIELD_CAN_CONTAIN_MAX_X_CHARACTER, $maxLimit);
	//alert($fieldToCount+' * '+$maxLimit);
	//$fieldToCount = document.getElementById($fieldToCount);
	if(!$fieldToCount) $fieldValue = ''; else $fieldValue = $fieldToCount.value;
	//alert($fieldValue);
	if ($fieldValue.length > $maxLimit) {
		window.alert($alertText);
		$fieldToCount.value = $fieldValue.substring(0, $maxLimit);
	}
}

// split and replace part of string or url  
function splitUrl($string, $p1, $p2, $replace) {
	if(parseInt($string.indexOf('?')) == -1) $join = '?'; else $join = '&';
	$pos1 = parseInt($string.indexOf($p1));
	$pos2 = parseInt($string.indexOf($p2, ($pos1+1)));
	//alert($pos1 + ' * ' + $pos2);
	//alert($pos1 + ' * ' + parseInt($pos1) + ' * ' + ($pos1+1) + ' * ' + $pos2);
	if($pos1==-1) {
		if($replace=='') {
			$url = $string;
		} else {
			$url = $string + $join + $replace;
		}
	} else if($pos1 && $pos2==-1) {
		if($replace=='') {
			$url = $string.substring(0, ($pos1-1)) + $replace;
		} else {
			$url = $string.substring(0, $pos1) + $replace;
		}
	} else {
		if($replace=='') {
			$url = $string.substring(0, ($pos1-1)) + $replace + $string.substring($pos2);
		} else {
			$url = $string.substring(0, $pos1) + $replace + $string.substring($pos2);
		}
	}
	//alert($url);
	return $url;
}

