//czas na serwerze www
var d_serv = new Date();
//czas u klienta
var d_client = new Date();
//bieżący czas w szkole
var d_school = new Date();;
 //różnica czasu
var delta = 0;
//czas względem którego chodzi stoper
var stoper_end = new Date();
//blok ruchomy
var mrq;

function trim(str, chars)
 {
 return ltrim(rtrim(str, chars), chars);
 }
 
function ltrim(str, chars)
 {
 chars = chars || "\\s";
 return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
 }
 
function rtrim(str, chars)
 {
 chars = chars || "\\s";
 return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
 }

function zegar()
 {
 //var miesiace = new Array('stycznia', 'lutego', 'marca', 'kwietnia', 'maja', 'czerwca', 'lipca', 'sierpnia', 'września', 'października', 'listopada', 'grudnia');
 //aktualny czas w szkole (na serwerze)
 d_school = new Date();
 d_school.setTime(d_school.getTime() - delta);
 G = d_school.getHours();
 G = G<10?'0'+G:G;
 M = d_school.getMinutes();
 M = M<10?'0'+M:M;
 S = d_school.getSeconds();
 S = S<10?'0'+S:S;

// r = d_school.getFullYear();
// m = miesiace[d_school.getMonth()];
// d = d_school.getDate();

 //'Serwer: ' + d_serv + ' Klient: ' + d_client + ' Różnica: ' + delta + 
 document.getElementById('czas').innerHTML = '' + G + ':' + M + ':' + S; 
 }
 
function czas_start(dtphp)
 {
 d_serv.setTime(dtphp*1000);
 delta = d_client.getTime() - d_serv.getTime();
 setInterval('zegar()',1000);
 }

function stoper()
 {
 var zmp;
 var stoper_time;
 var showing_time;
 var styl = '<span>';
 //ile milisekund trwa lekcja
 var lesson_duration = 3600000;
 stoper_time = stoper_end.getTime() - d_school.getTime();
 
 if (document.getElementById('stoper'))
  {
	if (stoper_time > lesson_duration)
	 {
	 zmp = 'Do rozpoczęcia lekcji pozostało:';
	 showing_time = stoper_time - lesson_duration;
	 if (showing_time < (1000*60))
	 styl = '<span style=\'text-decoration: blink;\'>';
	 } else
	 {
	 if (stoper_time > 0)
	  {
	  zmp = 'Do końca lekcji pozostało:';
	  showing_time = stoper_time;
	  if (showing_time < (1000*60))
	  styl = '<span style=\'font-weight: bold; text-decoration: blink;\'>'; else
		styl = '<span style=\'font-weight: bold; color: yellow;\'>';
		} else
		{
	  zmp = 'Dodatkowy czas:';
	  showing_time = Math.abs(stoper_time);
		}
	 }

  D = Math.floor(showing_time/(1000*60*60*24));
	G = Math.floor((showing_time - D*(1000*60*60*24))/(1000*60*60));
	M = Math.floor((showing_time - D*(1000*60*60*24) - G*(1000*60*60))/(1000*60));
	S = Math.floor((showing_time - D*(1000*60*60*24) - G*(1000*60*60) - M*(1000*60))/(1000));	
	D = D<1?'':D+' dni ';
  G = G<10?'0'+G:G;
  M = M<10?'0'+M:M;
  S = S<10?'0'+S:S;	
	
	document.getElementById('stoper_opis').innerHTML = zmp;
  document.getElementById('stoper').innerHTML = styl + D + G + ':' + M + ':' + S + '</span>'; 
	}
 } 

function stoper_start(sephp)
 {
 stoper_end.setTime(sephp*1000);
 setInterval('stoper()',1000);
 }

function load(miejsce,zoom)
 {
 if (GBrowserIsCompatible())
  {
  var map = new GMap2(document.getElementById("map"));
	var geocoder = new GClientGeocoder();
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.addControl(new GScaleControl());
	geocoder.getLatLng(miejsce,
         function(pt)
				   {
          if (!pt)
					  {
           alert(miejsce + " - nie odnaleziony");
           } else
					  {
           map.setCenter(pt, zoom);
           }
          }
       );
  }
 }

function loadxy(x,y,zoom)
 {
 if (GBrowserIsCompatible())
  {
  var map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GScaleControl());
  map.setCenter(new GLatLng(x, y), zoom);
  }
 }

function loadxym(x,y,zoom)
 {
 if (GBrowserIsCompatible())
  {
	var info =  "Firma Usługowa e-Lengua";
  var point = new GLatLng(x, y);  
  var marker = new GMarker(point);  
	var map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GScaleControl());
  map.setCenter(point, zoom);
	GEvent.addListener(marker, "click", function() {  
  marker.openInfoWindowHtml(info);});  
  map.addOverlay(marker);  
  }
 }	

 
function okno(tytul,url,w,h)
 {
 var o = window.open(url,tytul,"width="+w+",height="+h+",scrollbars");
 }

function add(slid,taid)
 {
 document.getElementById(taid).innerHTML += document.getElementById(slid).value; 
 } 

function add_s(slid,taid)
 {
 document.getElementById(taid).value += document.getElementById(slid).value; 
 }

function add_z(znak,id)
 {
 document.getElementById(id).value += znak;
 document.getElementById(id).focus();
 } 

function set_ob(ob,id)
 {
 if (document.getElementById(ob))
  {
	document.getElementById(ob).value = id;
	}
 } 
 
function add_zv(znak,id)
 {
 document.getElementById(document.getElementById(id).value).value += znak;
 document.getElementById(document.getElementById(id).value).focus();
 } 

function insertAtCursor(myValue,id) 
 {
 var myField = document.getElementById(id);
 var znak = '^';
 //IE support
 if (document.selection) 
  {
  myField.focus();

//in effect we are creating a text range with zero
//length at the cursor location and replacing it
//with myValue
  sel = document.selection.createRange();
  sel.text = myValue;
  }

//Mozilla/Firefox/Netscape 7+ support
 else if (myField.selectionStart || myField.selectionStart == '0')
  {
//Here we get the start and end points of the
//selection. Then we create substrings up to the
//start of the selection and from the end point
//of the selection to the end of the field value.
//Then we concatenate the first substring, myValue,
//and the second substring to get the new value.
  var startPos = myField.selectionStart;
  var endPos = myField.selectionEnd;
  myField.value = myField.value.substring(0, startPos)+ myValue.substring(0,myValue.indexOf(znak))+ myField.value.substring(startPos, endPos)+ myValue.substring(myValue.indexOf(znak)+1,myValue.length)+ myField.value.substring(endPos, myField.value.length);
	myField.focus();
	myField.setSelectionRange(endPos+ myValue.length,endPos+ myValue.length);
  }
 else 
  {
  myField.value += myValue;
  }
 } 

function insertAtCursor2(myValue,id) 
 {
 var Field = document.getElementById(document.getElementById(id).value);
 //IE support
 if (document.selection) 
  {
  Field.focus();

//in effect we are creating a text range with zero
//length at the cursor location and replacing it
//with myValue
  sel = document.selection.createRange();
  sel.text = myValue;
  }

//Mozilla/Firefox/Netscape 7+ support
 else if (Field.selectionStart || Field.selectionStart == '0')
  {
//Here we get the start and end points of the
//selection. Then we create substrings up to the
//start of the selection and from the end point
//of the selection to the end of the field value.
//Then we concatenate the first substring, myValue,
//and the second substring to get the new value.
  var startPos = Field.selectionStart;
  var endPos = Field.selectionEnd;
  Field.value = Field.value.substring(0, startPos)+ myValue+ Field.value.substring(endPos, Field.value.length);
	Field.focus();
	Field.setSelectionRange(startPos+1,endPos+1);	
  }
 else 
  {
  Field.value += myValue;
  }
 } 

function insertAtCursorZD(myValue,id) 
 {
 alert(id);
 } 

 
function MP(link,autostart,type)
 {
 var file;
 var width;
 var height;
 var id = link;
 while(id.indexOf("/")>-1)
	{ 
  id = id.replace("/","_");
	}
 while(id.indexOf(".")>-1)
	{ 
  id = id.replace(".","_");
	}
 while(id.indexOf("-")>-1)
	{ 
  id = id.replace("-","_"); 
	}
 while(id.indexOf(" ")>-1)
	{ 
	id = id.replace(" ","_"); 
	}

 if(type=="maxi")
  {
	file = "player_mp3_maxi.swf";
	width = 100;
	height = 20;
	}
 else
  {
	file = "player_mp3_multi.swf";
	width = 400;
	height = 100;
	}
 var s1 = new SWFObject(file,"_"+id,width,height,"9","#ffffff");
 s1.addParam("repeat","true");
 s1.addParam("allowscriptaccess","always");
 s1.addParam("wmode","opaque");
 s1.addParam("FlashVars","mp3="+link+"&amp;width="+width);
 if (!document.getElementById(id))
 id = "player";
 s1.write(id);
 }
 
function insert_img(pole,link,tytul)
 {
 var code = '<img align="right" class="img1" src="'+link+'" title="'+tytul+'" />';
 if (self.opener && !self.opener.closed)
 self.opener.document.getElementById(pole).innerHTML += code;
 self.close();
 }

function insert_snd(pole,link,autostart,type)
 {
 var id = link;
 while(id.indexOf("/")>-1)
	{ 
  id = id.replace("/","_");
	}
 while(id.indexOf(".")>-1)
	{ 
  id = id.replace(".","_");
	}
 while(id.indexOf("-")>-1)
	{ 
  id = id.replace("-","_"); 
	}
 while(id.indexOf(" ")>-1)
	{ 
	id = id.replace(" ","_"); 
	}
	
 var code = "<script type='text/javascript' src='mediaplayer/swfobject.js'></script>\n<div class='mp' id='"+ id +"'></div>\n<script type='text/javascript'>MP('"+link+"','"+autostart+"','"+type+"')</script>";
	
 if (self.opener && !self.opener.closed)
  {
	var docelowe = self.opener.document.getElementById(pole); 
  var startPos = docelowe.selectionStart;
  var endPos = docelowe.selectionEnd;
  docelowe.value = docelowe.value.substring(0, startPos)+ code + docelowe.value.substring(endPos, docelowe.value.length);
	docelowe.focus();
	docelowe.setSelectionRange(startPos+1,endPos+1);	
	}
 self.close();
 }

function insert_miejsc(pole,kod,nazwa)
 {
 if (self.opener && !self.opener.closed)
  {
  self.opener.document.getElementById(pole).value = kod;
	self.opener.document.getElementById("N"+pole).value = nazwa;
	}
 self.close();
 }
 
function sumuj_pola(tabpol,sumpol)
 {
 var sum = parseFloat(0);
 for (var i=0; i<tabpol.length; i++)
 sum += parseFloat(document.getElementById(tabpol[i]).value);
 document.getElementById(sumpol).value = sum;
 } 

function rachunek(tabcen)
 {
 var wpom;
 var sum = parseFloat(0);
 lp = document.getElementById("LP").value
 for (var i=0; i<lp; i++)
  {
	wart = document.getElementById("WART"+i);
	cena = document.getElementById("CENA"+i);
	liczba = document.getElementById("_ILOSC_"+i);
	rabat = document.getElementById("_RABAT_"+i);
	usluga = document.getElementById("_ID_CN_"+i);
	cena.value = tabcen[parseFloat(usluga.value)];
	wpom = parseFloat(cena.value)*parseFloat(liczba.value)*((100-parseFloat(rabat.value))/100);
	wart.value = wpom.toFixed(2);
  sum += parseFloat(wpom);
	}
 document.getElementById("SR").value = sum.toFixed(2);
 }

function goLite(id,col)
 {
 document.getElementById(id).style.backgroundColor = col;
 }

function goDim(id,col)
 {
 document.getElementById(id).style.backgroundColor = col;
 } 

 
function togGroup(el, classes)
 {
 for (var els=document.getElementsByTagName(el.tagName), i = els.length-1; i > -1; --i)
 if (els[i]!==el&&els[i].className==classes.b)
 els[i].className=classes.a;
 
 el.className=el.className==classes.a? classes.b : classes.a;
 } 
 
function getCheckedValue(radioObj)
 {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
 }
 
function getXmlHttpObject()
 {
 if (window.XMLHttpRequest)
  {
  return new XMLHttpRequest();
  }
 if (window.ActiveXObject) 
  {
  return new ActiveXObject('Microsoft.XMLHTTP');
  }
 return null;
 } 
 
function ajaxSend(url,div,param)
 { 
 returnDiv = div; 
 xmlHttpEdi = getXmlHttpObject();
 if (xmlHttpEdi===null)
  {
  alert ("Przeglądarka nie obsługuje zapytań HTTP Request");
  return;
  }
 //var url="diagnostyka/wyniki/lista.php";
 var f = url+"?"+param;
 xmlHttpEdi.onreadystatechange = stateChanged;
 xmlHttpEdi.open("get",f,true);
 xmlHttpEdi.send(null);
 //alert("objSend: Objekt wysłany z parametrami "+param);
 }
 
function stateChanged()
 {
 if ((xmlHttpEdi.readyState==4) || (xmlHttpEdi.readyState=="complete"))
  {
  var zm = xmlHttpEdi.responseText;
	document.getElementById(returnDiv).innerHTML = zm;
	}
 }	


function insertArg(ffrom,fto)
 {
 var arg = self.document.getElementById(fto);
 var source; 
	
 if (self.opener && !self.opener.closed)
  {
	source = self.opener.document.getElementById(ffrom);
  var startPos = source.selectionStart;
  var endPos = source.selectionEnd;
	/*tylko wtedy gdy jest zaznaczenie*/
	if (endPos>startPos)
  arg.value = source.value.substring(startPos, endPos);
  }
 }

function rgb2hex(r, g, b)
{
var hex_alphabets = "0123456789ABCDEF";
var hex = "";
var triplet = new Array;
var int1;
var int2;

triplet.push(r);
triplet.push(g);
triplet.push(b);

for(var i = 0; i < 3; i++) {
int1 = triplet[i] / 16;
int2 = triplet[i] % 16;
hex += hex_alphabets.charAt(int1) + hex_alphabets.charAt(int2);
}

return(hex);
}
 
function setcolor(id)
 {
 //alert(id+'r');
 var r = document.getElementById(id+'r').value;
 var g = document.getElementById(id+'g').value;
 var b = document.getElementById(id+'b').value;
 var color = rgb2hex(r,g,b);
 var fontcolor = rgb2hex(255-r,255-g,255-b);
 document.getElementById(id).value = color;
 document.getElementById(id).style.backgroundColor = '#'+color;
 document.getElementById(id).style.color = '#'+fontcolor;
 }
 
//odpalanie ręczne zdarzenia
function dia_fireEvent(obj, eventName)
{
  try {
      obj.fireEvent('on'+eventName);
  }
  catch (evt) {
      var newEvt = document.createEvent("Events");
          newEvt.initEvent(eventName,false,true);
          obj.dispatchEvent(newEvt);
  }
} 
