var agt=navigator.userAgent.toLowerCase();
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

function sub(){
		//hidetrail();
		o=document.getElementById('in666').value;		
		newwindow=window.open('popup.php?id='+o,o,'toolbar=no,scrollbars=yes,width=580,height=600,top=10,left=150');
		
		if (window.focus) {
			newwindow.focus();
			
		}
		//return false;
	}

	
	
function val(){
	imie='';
	dataU='';
	pp=verifyPESEL(document.getElementById('id_pesel').value);
	if(pp){
		document.getElementById('id_pesel').style.background='#ffaeae';
	}
	else
		document.getElementById('id_pesel').style.background='white';
	
		
	if(document.getElementById('id_imie').value==''){
		document.getElementById('id_imie').style.background='#ffaeae';
		imie="Pola oznaczone gwiazdką są obowiązkowe!";
	}
	else
		document.getElementById('id_imie').style.background='white';
		
		
	if(document.getElementById('id_plec').value=='3'){
		document.getElementById('id_plec').style.background='#ffaeae';
		imie="Pola oznaczone gwiazdką są obowiązkowe!";
	}else
		document.getElementById('id_plec').style.background='white';
	
	d= document.getElementById('id_data_urodzenia').value;
	r = new RegExp("^[0-3][0-9].[0-1][0-9].[1-2][0-9][0-9][0-9]$");
	//alert(r.exec(d));
	
	if(!r.exec(d)){
		document.getElementById('id_data_urodzenia').style.background='#ffaeae';
		dataU="Wymagana data w formacie DD.MM.RRRR!";
	}
	else
		document.getElementById('id_data_urodzenia').style.background='white';
	if(imie || dataU || pp)
		alert(imie+'\n'+dataU+'\n'+pp);
	else
		if(imie=='' & dataU=='' && pp=='')
			document.getElementById('id_nowy_pac').submit();
		
}

function val_wywiad(){
	who='';
	if(document.getElementById('id_who').value==0){
		document.getElementById('id_who').style.background='#ffaeae';
		who="Pola oznaczone gwiazdką są obowiązkowe!";
	}
	else
		document.getElementById('id_who').style.background='white';
		
	if(who)
		alert(who);
	else
		document.getElementById('id_form_wywiad').submit();
}



function asciiCodeOf(znak) {
var A   = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    return  A.indexOf(znak);
} //fun codeOf()


function  DatawPeselu( tempStr ) {
var  century = new Array (1900,2000,2100,2200,1800);
var     dwm  = new Array (0,31,28,31,30,31,30,31,31,30,31,30,31,0);
	data = tempStr.substring(0,6);
        dd   = parseInt(data.substring(4,6),10);  // od do
        cc   = parseInt(data.substring(2,4),10);
        bb   = parseInt(data.substring(0,2),10);
        mm   = cc % 20;    
	rr   = century[Math.floor(cc/20)] + bb;
	if(((rr%4==0)&&(rr%100!=0))||(rr%400==0)) dwm[2]=29;
        temp = '';
        if ( mm<1 || mm>12 || dd<1 || dd>dwm[mm] )
           temp = '   błędna data: '+dd+'.'+mm+'.'+rr;
	return temp;
}


function verifyPESEL() {		/* copyright R.J.Żyłła 2000-2007 */
var  rodzajNum =0;
var  tempStr   =verifyPESEL.arguments[0];
var  nazwy = new Array ('PESEL','REGON','NIP','BANK','ISBN','CCARD',
			'IACS','IBAN','DowOsob','NrLekarza','IMEI');

var  dlug  = new Array  ( 11, 9,10, 8,10,16,12,28, 9, 7,15 );
var  mody  = new Array  ( 10,11,11,10,11,10,10,97,10,11,10 );
var  wagi  = new Array 
	     (  new Array( 1,3,7,9,1, 3,7,9,1,3, 0,0,0,0,0,0));  /* IMEI  */

var  cyfry = new Array  (0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0 );

  if (tempStr != '') { 			// zamiana znaków na liczby
	comment = ' ';			//  alert( tempStr );
	
	L = tempStr.length;
	for ( i=0; i<L; i++ ) {
	    znak = tempStr.charAt(i); 	//  alert( znak );
	    if (rodzajNum == 8) {
			if ((znak >='A') && (znak <='Z')){
		    	znak = asciiCodeOf(znak) % 10;
			}
	    }

	    if (znak == 'X') cyfry[i]= 10
	  	else 	cyfry[i] = parseInt(znak);
	}  
	// alert( 'L= '+L+'  dlug[tabl]='+dlug[rodzajNum] );
	// alert('nr rodz numeru= '+ rodzajNum );
	if ((L == dlug[rodzajNum]) || (rodzajNum == 5)){
	  suma = 0;
	  for (i=0; i<L-1; i++) {
		    suma = suma + (wagi[rodzajNum][i]*cyfry[i]);
		}
	  
	  
	  suma = suma % mody[rodzajNum];
	  if (rodzajNum == 0)  suma = (10-suma) % 10; // PESEL 
	  wynikB = ( suma == cyfry[L-1] );
	 
	  	comment='';
	  	if(DatawPeselu(tempStr)){
	  		wynikB=false;
	  		comment=' - Błędna data'
	  	}
	  	else
	  		comment='';

	  com=(wynikB ? '': 'Błędny PESEL')+comment;
	  
	} 
	  else {
		wynikB = false;
		com='PESEL liczy' + (L<dlug[rodzajNum]? ' zbyt mało': ' za dużo') + ' cyfr';
	}
    }
     else {
     	com='brak Nr PESEL';
    }

  return com;
} // fun verifyPESEL






function val_form3(){
	who='';
	if(document.getElementById('id_powod_zakonczenia').value==0){
		document.getElementById('id_powod_zakonczenia').style.background='#ffaeae';
		who="Pola oznaczone gwiazdką są obowiązkowe!";
	}
	else
		document.getElementById('id_powod_zakonczenia').style.background='white';
		
	if(who)
		alert(who);
	else
		document.getElementById('id_form3').submit();
}


function val_form1(){
		document.getElementById('id_form1').submit();
}

function val_form2(){
		document.getElementById('id_form2').submit();
}

function val_form4(){
		document.getElementById('id_form4').submit();
}
function val_form5(){
		document.getElementById('id_form5').submit();
}
function val_form6(){
	who='';
	if(document.getElementById('id_powod_zakonczenia').value==0){
		document.getElementById('id_powod_zakonczenia').style.background='#ffaeae';
		who="Pola oznaczone gwiazdką są obowiązkowe!";
	}
	else
		document.getElementById('id_powod_zakonczenia').style.background='white';
		
	if(who)
		alert(who);
	else
		document.getElementById('id_form6').submit();
}

function popup1(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=490,height=500,scrollbars=yes');
return false;
}


function popup_drukuj(mylink, windowname){
	window.open(mylink, windowname, 'width=800,height=800,scrollbars=yes');
}



/*
function popup(m1,m2, windowname){
	s='typo3conf/ext/pacjeci/pi1/powiazane_osrodki.php?pacjent='+m1+'&osrodek='+m2;
	alert(s);
	window.open('"'+s+'"', windowname, 'width=490,height=500,scrollbars=yes');
}
*/
function popup_operacje(mylink, windowname){
	window.open(mylink, windowname, 'width=860,height=320,scrollbars=yes');
}

function popup_osrodek(mylink, windowname){
	window.open(mylink, windowname, 'width=430,height=275,scrollbars=yes');
}


function addf(lnk){
	location.href=lnk+document.getElementById('id_add_form').value+"&opc=new";
}


function inner_span(txt){
	t=txt.replace(/##/g, "'");
	document.getElementById('inner').innerHTML=t;
}

function innero_span(txt){
	t=txt.replace(/##/g, "'");
	//alert(t);
	document.getElementById('innero').innerHTML=t;
}
function inner_osrodki_span(txt){
	t=txt.replace(/##/g, "'");
	document.getElementById('inner_osrodki').innerHTML=t;
}

function popup_obserwcje(mylink, windowname){
	window.open(mylink, windowname, 'width=850,height=230,scrollbars=yes');
}

/***********************************************
* Pop-it menu- ? Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var defaultMenuWidth="150px" //set default menu width.

var linkset=new Array()
//SPECIFY MENU SETS AND THEIR LINKS. FOLLOW SYNTAX LAID OUT
linkset[0]='<a href="#">szczegóły</a>'
linkset[0]+='<a href="#">edytuj</a>'
linkset[0]+=''

linkset[1]+='<a href="#">szczegóły</a>'
linkset[1]+='<a href="#">edytuj</a>'

////No need to edit beyond here

var ie5=document.all && !window.opera
var ns6=document.getElementById

if (ie5||ns6)
document.write('<div id="popitmenu" onMouseover="clearhidemenu();" onMouseout="dynamichide(event)"></div>')

function iecompattest(){
return (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body
}

function showmenu(e, which, id_pac,id_osrodka){
if (!document.all&&!document.getElementById)
return
clearhidemenu()
menuobj=ie5? document.all.popitmenu : document.getElementById("popitmenu")
//menuobj.innerHTML='	<a href="index.php?id=24&act=sp&pacjent='+id_pac+'">szczegóły</a><a href="index.php?id=24&act=sp&pacjent='+id_pac+'">edytuj</a><a href="index.php?id=24&act=sp&pacjent='+id_pac+'">powiązane ośrodki</a>';
																											

if(which){
	menuobj.innerHTML="	<a href=\"index.php?id=24&act=sp&dis=1&pacjent="+id_pac+"\">szczegóły</a><a href=\"index.php?id=24&act=sp&pacjent="+id_pac+"\">edytuj</a><A HREF=\"typo3conf/ext/pacjeci/pi1/powiazane_osrodki.php?pacjent="+id_pac+"&osrodek="+id_osrodka+"\" onClick=\"return popup1(this, 'notes')\">powiązane ośrodki</A>";
}
else{
	menuobj.innerHTML="	<a href=\"index.php?id=24&act=sp&dis=1&pacjent="+id_pac+"\">szczegóły</a><a href=\"index.php?id=24&act=sp&pacjent="+id_pac+"\">edytuj</a>";
}
off=-5
//menuobj.style.width=(typeof optWidth!="undefined")? optWidth : defaultMenuWidth
menuobj.style.width=defaultMenuWidth
menuobj.contentwidth=menuobj.offsetWidth
menuobj.contentheight=menuobj.offsetHeight
eventX=ie5? event.clientX : e.clientX
eventY=ie5? event.clientY : e.clientY
//Find out how close the mouse is to the corner of the window
var rightedge=ie5? iecompattest().clientWidth-eventX : window.innerWidth-eventX
var bottomedge=ie5? iecompattest().clientHeight-eventY : window.innerHeight-eventY
//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.contentwidth)
//move the horizontal position of the menu to the left by it's width
menuobj.style.left=ie5? iecompattest().scrollLeft+eventX-menuobj.contentwidth+off+"px" : window.pageXOffset+eventX-menuobj.contentwidth+off+"px" 
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.style.left=ie5? iecompattest().scrollLeft+eventX+off+"px" : window.pageXOffset+eventX+off+"px"
//same concept with the vertical position
if (bottomedge<menuobj.contentheight)
menuobj.style.top=ie5? iecompattest().scrollTop+eventY-menuobj.contentheight+off+"px" : window.pageYOffset+eventY-menuobj.contentheight+off+"px"
else
menuobj.style.top=ie5? iecompattest().scrollTop+event.clientY+off+"px" : window.pageYOffset+eventY+off+"px"
menuobj.style.visibility="visible"
return false
}

function contains_ns6(a, b) {
//Determines if 1 element in contained in another- by Brainjar.com
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function hidemenu(){
if (window.menuobj)
menuobj.style.visibility="hidden"
}

function dynamichide(e){
if (ie5&&!menuobj.contains(e.toElement))
hidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
hidemenu()
}

function delayhidemenu(){
delayhide=setTimeout("hidemenu()",500)
}

function clearhidemenu(){
if (window.delayhide)
clearTimeout(delayhide)
}

function lost(){
	x=0;
	t=0;
	while(x<5){
		temp=document.getElementById('id_kol_2'+x).value;
		t+=(temp==""?0:parseInt(temp));
		x++;
	}
	document.getElementById('id_sum').innerHTML=t;
}
