// obecne funkce

function activeTestWarning(msg) {
  alert(msg);
  return false;
}


function getCertificate(width,scriptUrl) {

  if (document.getElementById('printname').value == '') {
    alert('Zadejte jméno, které se má vytisknout na certifikát!');
    return false;
  }
  else {

    var url = scriptUrl+'&course='+document.getElementById('course').value+'&name='+document.getElementById('printname').value;
    var name = 'cetificateWindow';
    if (!width) var width = 960;
  	var height = 660;
  	
  	// vycentrovani
  	var screenSize = ScreenSize();
  	var left = (screenSize[0] - width) / 2;
  	var top = (screenSize[1] - height) / 2;
  	
  	window.open(url, name, 'location=no, status=no, toolbar=no, menubar=yes, resizable=yes, scrollbars=yes, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + '');
  	
  	return true;
	}
  
}


function ScreenSize() {
	if (parseInt(navigator.appVersion)>3) {
		screenW = screen.width;
		screenH = screen.height;
	} else if (navigator.appName == "Netscape" 	&& parseInt(navigator.appVersion)==3 && navigator.javaEnabled()) {
		// stare browsery
		var jToolkit = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		screenW = jScreenSize.width;
		screenH = jScreenSize.height;
	}
	
	return Array(screenW, screenH);
}



// otevření okna s kurzem
function OpenCourseWin(url, name) {
	var width = 960;
	//var height = 750; // puvodne
	var height = 650;
	
	// vycentrovani
	var screenSize = ScreenSize();
	var left = (screenSize[0] - width) / 2;
	var top = (screenSize[1] - height) / 2;
	
	window.open(url, name, 'location=no, status=no, toolbar=no, menubar=no, resizable=no, scrollbars=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + '');
	
	return true;
}




// otevření okna s tiskem
function OpenPrintWin(url, name) {
	var width = 960;
	var height = 720;
	
	// vycentrovani
	var screenSize = ScreenSize();
	var left = (screenSize[0] - width) / 2;
	var top = (screenSize[1] - height) / 2;

	window.open(url, name, 'location=no, status=no, toolbar=no, menubar=yes, resizable=yes, scrollbars=yes, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + '');
	
	return true;
}



function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}


function setSize() {
  //return true;
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  // zohledneni vysky navigatoru
  var navigatorH = 0;
  navigatorH = document.getElementById('navigator').clientHeight;  

  document.getElementById('page').style.height = '' + (myHeight - 0) + 'px';
  document.getElementById('dolni-left').style.height = '' + (myHeight - 80) + 'px';
  document.getElementById('dolni-right').style.height = '' + (myHeight - 80) + 'px';
  document.getElementById('dolni-right-wysiwyg').style.height = '' + (myHeight - 175 - navigatorH + 28 ) + 'px';
  
}


// script slouzici ke skryvani a odkryvani casti textu

function view(text,kotvaA,kotvaB) {
	document.getElementById(text).style.display = "block";
	document.getElementById(kotvaA).style.display = "none";	
	document.getElementById(kotvaB).style.display = "block";
}

function hid(text,kotvaA,kotvaB) {
	document.getElementById(text).style.display = "none";
	document.getElementById(kotvaA).style.display = "block";	
	document.getElementById(kotvaB).style.display = "none";
}


function submitForm() {
	document.forms['questionForm'].submit();
}

// zaskrtne checkbox s danym ID
function CheckboxCheck(id) {
	if (!id) {return false;}
	return document.getElementById(id).checked = true;
}

// otevření dokumentu v novém okně
function OpenDocument(url,name) {
	window.open(url, name, '');
	return true;
}


function OpenImage(url,name,width,height) {

    if (!width)    var width = 960;
  	if (!height)   var height = 660;
  	
  	// vycentrovani
  	var screenSize = ScreenSize();
  	var left = (screenSize[0] - width) / 2;
  	var top = (screenSize[1] - height) / 2;
  	
  	window.open(url, name, 'location=no, status=no, toolbar=no, menubar=yes, resizable=yes, scrollbars=yes, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + '');
  	return true;

}


// otevření okna s doplnkem
function OpenVideoAddon(url, name, width, height) {

	// vycentrovani
	var screenSize = ScreenSize();
	var left = (screenSize[0] - width) / 2;
	var top = (screenSize[1] - height) / 2;
	
	window.open(url, name, 'location=no, status=no, toolbar=no, menubar=no, resizable=yes, scrollbars=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + '');
	
	return true;
}

// otevření okna s doplnkem
function OpenFlashAddon(url, name, width, height) {

	// vycentrovani
	var screenSize = ScreenSize();
	var left = (screenSize[0] - width) / 2;
	var top = (screenSize[1] - height) / 2;
	
	window.open(url, name, 'location=no, status=no, toolbar=no, menubar=no, resizable=yes, scrollbars=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + '');
	
	return true;
}


// overi zda je zadana hodnota platnym celym kladnym cislem
function IsValidInt(value) {
	value = Math.round(value + 0); // pretypovani na integer
	
	if (value && (value > 0)) return true;
	
	return false;
	
}

// onload akce na strance kurzu k kategorii
function onLoadCoursesInCategory() {
	var except = 'run-course';
	
	// najdeme vsechny odkazy a ke kazdemu pridame pri kliknuti kontrolu na nove zaskrtnute/odskrtnute polozky
	links = document.getElementsByTagName('a');
	form = document.getElementById('categories-form');
	if (!form) return false;
	if (!links) return false;
	
	for (i = 0; i < links.length; i++) {
		linkTag = links[i];
		//alert(linkTag.className);
		if ((linkTag.className != except) && (linkTag.href!='javascript://')) linkTag.onclick = function() {return checkNewChecks(form);}
	}
	
	return true;
	
}

/**
 * Kontrola nove zaskrtnutych polozek v seznamu
 *
 * @param form - element s formularem, v nemz jsou checkboxy, ktere chceme overovat
 *
 */
var newChecked = false;
function checkNewChecks(form) {
	if (!form) return true;
	if (newChecked) {
		saveNewChecked = confirm('Byly provedeny změny v zaškrtnutých položkách na stránce. Přejete si před přechodem na stránku uložit zaškrtnuté položky?');
		if (saveNewChecked) {form.submit();	return false; }
	}
	return true;
}

/**
 * Vyprazdneni hodnoty inputu, pokud ma jinou jinou nez vychozi hodnotu
 *
 * @param element - element jehoz obsah chceme vyprazdnit
 * @param defaulText - vychozi text v elementu. Jen pokud je element naplnen timto textem jej vyprazdnime.
 *
 */
function clearField(element, defaultText) {
	value = element.value;
	if (!value) return false;
	//alert('Hodnota: ' + value + ', default text: ' + defaultText);
	if (value == defaultText) element.value = '';
	
	return true;
}



/**
 * Zjisteni zda byly zaskrtnute nejake checkboxy s danym nazvem
 *
 * @param object form - element formulare, v nez se checkboxy nachazi
 * @param string chkgroupName - nazev pole chceckboxu, ktere kontrolujeme
 *
 */
function isChecked(form, chkgroupName) {
	if (!form) return false;
	
	var checked = false;
	//alert(form.elements.length);
	//for (input in form.elements) {
	for (i=0; i<form.elements.length; i++) {
		input = form.elements[i];
		//alert(input);
		//if (input.type == 'checkbox') alert(input.type + ' ' + input.name + ' ' + input.checked);
		
		if (input.type == 'checkbox' && input.name == chkgroupName + '[]') 
			if (input.checked) checked = true;
	}
	
	return checked;
}

/**
 * Zjisti, zda ma vybrany zaznam volne licence
 *
 * @param element - element select
 * @param licences - pole s pocty licenci pro jednotlive moznosti v selectu
 *
 */
function hasLicences(element, licences) {
	if (!licences) return true;
	if (!element.value) return true;
	
	if (licences[element.value] > 0) return true;
	
	return false;
}
