/*

*/
var currentLanguage = "pl"
// mainApplicationServlet
var mainApplicationServlet="/4ses/servlet/MainServlet";
var maxWidth=1016;

function checkCookie(){
  // check if cookie are enabled
  cookieBackup = document.cookie;
  document.cookie = "cookie=enabled"
  var cookieOK = false;
  if (document.cookie.indexOf("cookie=enabled") > -1) {
  	//alert ("Cookie sa wlaczone :)");
		document.getElementById("cookiesDivHead").style.visibility = "hidden";
		document.getElementById("cookiesDiv").style.visibility = "hidden";
  }else{
	  //alert("Cookie sa wylaczone! Nie bedzie mozna zalogowac sie do systemu.");
		document.getElementById("cookiesDivHead").style.visibility = "visible";
		document.getElementById("cookiesDiv").style.visibility = "visible";		
  }
	document.cookie = cookieBackup;
}

function openWin(lcUrl,lnWidth,lnHeight,lnResize,lnSroll,lcWinName,lnTopPosition) {
	if(!lnWidth)	lnWidth = 0;
	if(!lnHeight)	lnHeight = 0;
	if(!lnSroll)	lnSroll = 0;
	if(!lnResize)	lnResize = 0;
	if(!lcWinName)	lcWinName = "Win" + getRandomParam();
	if(!lnTopPosition)		lnTopPosition = 0;
	var lcParaExtra = "";
	var lnLeft = 0;
	var lnTop  = 0;
	
	var lnStatus = 0;
	var lnMenubar = 0;
	
	//opcja do wlaczania fullscreen
	//if(lnWidth<1 && lnHeight<1)lcParaExtra += ",fullscreen=Yes";
	
	if(lnWidth<1) {
		lnWidth = screen.availWidth - 10;
	}else{
		lnLeft  = screen.availWidth/2 - lnWidth/2;		
	}

	if(lnHeight>screen.availHeight){
		lnHeight = screen.availHeight - 40;
	}

	if(lnHeight<1){
		lnHeight = screen.availHeight - 30;		
	}else{
		lnTop    = (lnTopPosition>0)?lnTopPosition:screen.availHeight/2 - lnHeight/2;		
	}
	if (lnWidth > maxWidth) {
		lnWidth = maxWidth;
		lnLeft  = screen.availWidth/2 - lnWidth/2;
		lnResize = 0;
	}
	var lcPara = "directories=0,location=0,menubar="+lnMenubar+",resizable=" + lnResize +",scrollbars="+lnSroll+",status="+lnStatus+",toolbar=0";
	    lcPara += lcParaExtra + ",screenX="+lnLeft+",screenY="+lnTop+",top="+lnTop+",left="+lnLeft+",width="+lnWidth+",height="+lnHeight;

	var oWin = window.open(lcUrl,lcWinName,lcPara);
    try
    {
        if (oWin != null)oWin.focus();
    } catch(e) {}
	
	return oWin;
}
//-------------------------------------------------------------
function getRandomParam(){
	var now = new Date();
	return(""+now.getTime())
}
//-------------------------------------------------------------
function _alert(msg){
	//alert(msg);
}
//-------------------------------------------------------------
function closeWin(){
	top.window.close();	
	return true;
}
//-------------------------------------------------------------
function refresh(){
  top.window.document.location.reload()
  return true;
}
//-------------------------------------------------------------
function openHelpWindow(pageName){
	openWin(pageName,600,400,1,1,"helpWindow")
	return;
}
//-------------------------------------------------------------
// popup message
function goToMsg(){
	openWin(mainApplicationServlet+"?method=lms.GlobalMailerDisplayPopup",520,320,1,1,"popup_message");
}
//-------------------------------------------------------------
// open chat window
function goToChat(id_chat){
	if(!id_chat)id_chat=-1;
	openWin(mainApplicationServlet+"?method=lms.chat.Chat&id_chat="+id_chat,700,600,0,0,null,10)
	return;
}
//-------------------------------------------------------------
// open course window
function arg2str(arg){
	if(!arg || arg.length<1)return "Without args!"
	var lcS = "";
	for(var i=0;i<arg.length;i++){
		lcS+=arg[i] + "\n";
	}
	return lcS;
}
//uruchomienie kursu
function goToCourseByTraining(id_training,id_course,id_sco){
	var lcUrl = mainApplicationServlet+"?method=lms.StudentGoToCourse&noLink=0&id_training="+id_training+"&id_course="+id_course+"&id_sco="+id_sco+"&id_group=-1";
	document.location=lcUrl;
	return;
}

//uruchomienie kursu
function goToCourse(id_course,id_sco,id_group){
	if (id_group==-1) id_group="-1"; 
	var lcUrl = mainApplicationServlet+"?method=lms.StudentGoToCourse&noLink=0&id_course="+id_course+"&id_sco="+id_sco+"&id_group="+id_group;
	document.location=lcUrl;
	return;
}

//uruchomienie egzaminu
function goToExamByTraining(id_training,id_exam){
	var lcUrl = mainApplicationServlet+"?method=lms.StudentGoToExam&id_training="+id_training+"&id_exam="+id_exam+"&id_group=-1";
	document.location=lcUrl;
	return;
}
//uruchomienie egzaminu
function goToExam(idexam,id_group){
	if (id_group==-1) id_group="";
	var lcUrl = mainApplicationServlet+"?method=lms.StudentGoToExam&id_exam="+idexam+"&id_group="+id_group;
	document.location=lcUrl;
	return;
}

//uruchomienie encyklopedii
function goToEncyclopedia(id_course,id_sco,searchParam){
	var lcUrl = mainApplicationServlet+"?method=lms.StudentGoToEncyclopedia&id_course="+id_course+"&id_sco="+id_sco+"&search="+searchParam;
	document.location=lcUrl;
	return;
}

//-------------------------------------------------------------
function goToExamLinks(idexam, id_training,id_group){
	var lcUrl = mainApplicationServlet+"?method=lms.StudentExamResult&id_exam="+idexam+"&id_training="+id_training+"&id_group="+id_group;
	document.location=lcUrl;
	return;
}
//-------------------------------------------------------------
function goToFaq(idcourse){
	document.forms[0].gotofaq.value="yes";
	document.forms[0].gotofaqcourseid.value=idcourse;
	document.forms[0].submit();
}
//-------------------------------------------------------------
function courseAction(action, idcourse){
	document.forms[0].courseaction.value=action;
	document.forms[0].courseactionid.value=idcourse;
	document.forms[0].submit();
}
//-------------------------------------------------------------
function checkAll() {
	//alert("gg");
	//alert(document.forms[0].getElementById(checkbuton).value);
}
//-------------------------------------------------------------
function selectAllCheckbox(formNumber, checked) {
	chckB = document.forms[formNumber].elements;
	for(i=0; i < chckB.length; i++) {
		var obj = chckB[i];
		if(obj.type=='checkbox'){
			obj.checked = checked;
		}
	}
}
//-------------------------------------------------------------
function getUrlFromHtml(url, url_decode) {
	url_decode.value = encodeURI(url.value);
}
//-------------------------------------------------------------
function aspGoToCredits(){
	if(opener){
		opener.location.href = mainApplicationServlet+"?method=lms.ProductList";
	}	
	return;
}
//-------------------------------------------------------------
function showSCOComments(){
	var lcUrl = mainApplicationServlet+"?method=lms.StudentScoComments";
	openWin(lcUrl,450,380,0,0,'comments',0)
	return;
}
//-------------------------------------------------------------
function openExternalPage(lcUrl,lnResize){
	if(!lnResize)lnResize=0;
	openWin(lcUrl,780,500,lnResize,0,'ExternalPage',0)
	return;
}
//-------------------------------------------------------------
function getCookie(what){
	var result="nocookies";
	if (document.cookie.length>1){
		cookiesStuff=document.cookie;
		var iscookie=cookiesStuff.indexOf(what);
		
		if (iscookie!=-1){
			cookiesStuff = cookiesStuff.substring(iscookie,cookiesStuff.length);
			start=cookiesStuff.indexOf("=")+1;
			end=cookiesStuff.indexOf(";");
			if (end==-1){
				end = cookiesStuff.length;
			}
			result=cookiesStuff.substring(start,end);
		}
	}
	return result;
}

//-------------------------------------------------------------
function setCookie (name, value){
	var expire = new Date();
	expire.setTime(expire.getTime() + ( 7*24*60*60*1000 ) ); // expire in 1 week
	document.cookie = name+"="+value+"; expires=" + expire.toGMTString();
}

function openExtendChat(usid, unique, langVersion){
	var url = mainApplicationServlet+'?method=lms.chatextend.ChatExtendRedirect&language='+langVersion;
	if(usid != null){
		url += "&usid=" + usid;
	}
	openWin(url,730,530,0,0,'Chat'+unique,0);
}
//-------------------------------------------------------------
//global object (You can translate month&day names)
//alert("currentLanguage:" + currentLanguage);
var oCalLng = null;
oCalLng_pl={
	 day:['pn','wt','&#347;r','cz','pt','so','n'],
	 month:['Stycze&#324;','Luty','Marzec','Kwiecie&#324;','Maj','Czerwiec',
	 'Lipiec','Sierpie&#324;','Wrzesie&#324;','Pa&#378;dziernik','Listopad','Grudzie&#324;'],
	 D:new Date(),M:new Date().getMonth(),R:new Date().getFullYear()
}
oCalLng_de={
	 day:['mo','di','mi','do','fr','sa','so'],
	 month:['Januar','Februar','März','April','Mai','Juni',
	 'Juli','August','September','Oktober','November','Dezember'],
	 D:new Date(),M:new Date().getMonth(),R:new Date().getFullYear()
}
oCalLng_en={
	 day:['mo','tu','we','th','fr','sa','su'],
	 month:['January','February','March','April','May','June',
	 'July','August','September','October','November','December'],
	 D:new Date(),M:new Date().getMonth(),R:new Date().getFullYear()
}	

function getXXXXXXXXXXCalLng(lng){
var oCalLng = oCalLng_en;
if (currentLanguage=="pl"){
oCalLng = oCalLng_pl;
}else if (currentLanguage=="de"){
oCalLng = oCalLng_de;
}else {
oCalLng = oCalLng_en;
}
return oCalLng;
}
//Written by Bogdan Blaszczak/BlaTek
//blatek@25.pl
function Calendar(E,T){
 oCalLng = getXXXXXXXXXXCalLng();
 if(!document.getElementById||!document.body.appendChild)return
 var i,j,tBody,Row,od,Do,d,dt
 function cEl(t,p,h,w){
  p.appendChild(t=document.createElement(t))
  if(h)t.innerHTML=h;if(w)t.style.width='27px'
  return t}
 function cB(x){with(oCalLng){
  B=0;x==0?R--:x==1?R++:x==2?(!M?(M=11,R--):M--):(M==11?(M=0,R++):M++)}
 }
 with(oCalLng){
  od=Date.UTC(R,M,1);Do=Date.UTC(R,M+1,1)}
 E=document.getElementById(E)
 while(E.childNodes.length)E.removeChild(E.firstChild)
 tBody=cEl('tbody',cEl('table',E))
 tBody.onmouseup=function(){T.focus()}
 tBody.onmousemove=function(){
  if(window.getSelection)window.getSelection().removeAllRanges()}
 Row=cEl('tr',tBody)
 cEl('td',Row,'&#171;',1).onmousedown=function(){cB(0)}
 cEl('td',Row,oCalLng.R)
 cEl('td',Row,'&#187;',1).onmousedown=function(){cB(1)}
 Row=cEl('tr',tBody)
 cEl('td',Row,'&#171;',1).onmousedown=function(){cB(2)}
 cEl('td',Row,oCalLng.month[oCalLng.M])
 cEl('td',Row,'&#187;',1).onmousedown=function(){cB(3)}

 tBody=cEl('tbody',cEl('table',E))
 Row=cEl('tr',tBody)
 for(i=0;i<7;i++)
  with(cEl('th',Row,oCalLng.day[i]))
   if(i==6){
	 //style.backgroundColor='#990000'
	 //style.color='#ffffff'
	 }

 for(i=od;i<Do;i+=86400000){
  with(new Date(i)){d=getUTCDate();dt=getUTCDay()}
  if(dt==1||i==od)Row=cEl('tr',tBody)
  if(dt!=1&&d==1)for(j=1;j<(dt?dt:7);j++)cEl('th',Row)
  dt=cEl('td',Row,d);dt.onmousedown=function(x){
   x=+this.innerHTML
   with(oCalLng)
    T.value=(x<10?'0'+x:x)+'.'+((x=M+1)<10?'0'+x:x)+'.'+R
  }
  with(oCalLng)
  if(d==D.getDate()&&M==D.getMonth()&&R==D.getFullYear()){
	 dt.style.backgroundColor='#990000'
	 dt.style.color='#ffffff'
  }
 }
 T.onblur=function(){if(oCalLng.B)E.style.display='none'}
 oCalLng.B=1;E.style.display='block'
}

/*
jk
*/
var sColors = "000000,993300,333300,003300,003366,000080,333399,333333,800000,FF6600,808000,808080,008080,0000FF,666699,808080,FF0000,FF9900,99CC00,339966,33CCCC,3366FF,800080,999999,FF00FF,FFCC00,FFFF00,00FF00,00FFFF,00CCFF,993366,C0C0C0,FF99CC,FFCC99,FFFF99,CCFFCC,CCFFFF,99CCFF,CC99FF,FFFFFF";

var colorObj = null;

function colorDialogFire(color)
{
	if (colorObj) {
		colorObj.value = color;
		hideFloatingDiv();
		//document.body.style.backgroundColor = color;
	}
};

function ColorDialog(color, obj)
{
	makeFloatingDiv();
	
	obj.onblur = function() {
		window.setTimeout("hideFloatingDiv();", 150);
	}
	
	colorObj = obj;
	
	var ch = 0;
	var cols = 8;
	var s = '<table><tr><td>'
		+ '<table><tr><td id="__colorTable">';
	var colors = sColors.split(",");
	s += '<table>';
	while (ch < colors.length) {
		s += '<tr>';
		for (var i=0; i<cols; i++) {
			if (ch >= colors.length) break;
			
			s += '<td class="colorDOut" onmouseout="this.className=\'colorDOut\';" onmouseover="this.className=\'colorDOver\';" onclick="colorDialogFire(\'' + colors[ch] + '\');">';
			s += '<p style="background-color:' + colors[ch] + ';">&nbsp;</p>';
			s += '</td>';
			
			ch++;
		}
		s += '</tr>';
	}
	
	s += '</table>';
	s += '</td></tr></table>'
		+ '</td></tr><tr></tr></table>';
	
	if (obj) {
		var x = findPosX(obj);
		var y = findPosY(obj) + 20;
	} else {
		var x = 0;
		var y = 0;
	}
	showFloatingDiv(x, y, 160, 110, s);
}

function hideFloatingDiv()
{
	var obj = document.getElementById("__floatingDiv");
	if (obj) {
		obj.innerHTML = "";
		obj.style.visibility = "hidden";
	}
};

function showFloatingDiv(x, y, w, h, ih, imgName, action)
{
	var obj = document.getElementById("__floatingDiv");
	if (obj) {
		obj.style.left = x;
		obj.style.top = y;
		obj.style.width = w;
		obj.style.height = h;
		obj.style.height = h;
		obj.style.visibility = "inherit";
		obj.innerHTML = ih;
		
		//setOwner(obj);
	}
};

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}else if (obj.x){
		curleft += obj.x;
	};
	return curleft;
};

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		};
	} else if (obj.y) {
		curtop += obj.y;
	};
	return curtop;
};

function makeFloatingDiv()
{
	var fd = document.getElementById("__floatingDiv");
	if (fd) {
	} else {
		var floatingDiv = '<div id="__floatingDiv" style="visibility: hidden; position:absolute; left:0px; top:0px; width: 25px; height: 50px; background-color: #FFFFFF; border: solid 1px #716F64; overflow: auto;"></div>';
		if (document.all) {
			document.body.insertAdjacentHTML("beforeEnd", floatingDiv);
		} else {
			var rng = document.body.ownerDocument.createRange();
			rng.setStartBefore(document.body);
			var obj = rng.createContextualFragment(floatingDiv);
			document.body.appendChild(obj);
		}
	}
};

//end Color Dialog

function printPage(){
	document.getElementById("printPage").style.display="none";
	var a = window.print();
	document.getElementById("printPage").style.display="block";
}
// wykorzystywane do ustawiania sortowania 
// w DataListBaseContent (dla FastView)
function setFormVarsAndSubmit(){	
	var oForm = document.forms[0];
	if(!oForm)return;
	var oEl;
	for(var i=0;i<arguments.length;i++){
		var p = arguments[i].split("=");
		oEl = oForm.elements[p[0]];
		if(oEl)oEl.value=p[1];
	}
	oForm.submit();
}
function setElementVisibility(id,visible){
	if (visible==true){
		document.getElementById(id).style.display="block";
	}else{
		document.getElementById(id).style.display="none";
	}
}

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}

function buildPOST() {
theForm = document.forms[0];
var qs = ''
for (e=0;e<theForm.elements.length;e++) {
if (theForm.elements[e].name!='') {
var name = theForm.elements[e].name;
qs+=(qs=='')?'':'&'
qs+= name+'='+escape(theForm.elements[e].value);
}
}
return qs
}

function callMethod(method,params){
	xmlhttp=false;
	if (xmlhttp){
		var xmlMessage = buildPOST()+params;
		url = mainApplicationServlet+"?method="+method;
		xmlhttp.open("POST",url,false);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if (xmlhttp.status==404) {
					alert("An error occured! Cannot submit data.");
				}
			}
		}
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send(xmlMessage);
		document.open();
		document.write(xmlhttp.responseText);
		document.close();
		return true;
	}else{
		window.document.forms[0].submit();
	}
	return false;
}

function selectObjects(){
	var idList = "";
	var idList2 = "";
	var count = 0;
	for (var i=0; i<=document.forms[0].length; i++){
		if (document.forms[0].elements[i]!=null && document.forms[0].elements[i].type=='checkbox'){
			if (document.forms[0].elements[i].checked==true){
				count+=1;
				var checkboxId = document.forms[0].elements[i].id;
				if (checkboxId.indexOf("object_")>-1){
					var id = checkboxId.substring(checkboxId.indexOf("object_")+7,checkboxId.length);
					if (count==1){
						idList=""+id;
					}else{
						idList=idList+","+id;
					}
				}
			}else{
				count+=1;
				var checkboxId = document.forms[0].elements[i].id;
				if (checkboxId.indexOf("object_")>-1){
					var id = checkboxId.substring(checkboxId.indexOf("object_")+7,checkboxId.length);
					if (count==1){
						idList2=""+id;
					}else{
						idList2=idList2+","+id;
					}
				}
			}
		}
	}
	document.forms[0].objectsChecked.value=idList;
	document.forms[0].objectsNotChecked.value=idList2;
	document.forms[0].objectsCheckingActive.value="true";
}
