var misio=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	misio=true;
@end @*/

var emode,eelem;

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + encodeURIComponent(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return decodeURIComponent(dc.substring(begin + prefix.length, end));
}

var cooks=new Array();
var this_cook=-1;

function store_elem(el)
{
	cooks[this_cook].mode=((el.className.indexOf('clicked')>=0)?'m':'k');
	cooks[this_cook].exit=el.id;
	setEInfo();
}

var lastmouser;

function relink(el,nr)
{
	if (el.id == "skiper") {
		el.onfocus=function() {
			this.className='fokus';
			if (lastmouser) {
				lastmouser.className=lastmouser.className.replace(/( ?clicked)/g,'');
				lastmouser=null;
			}
		}
		el.onblur=function() {this.className='blur';}
		return;
	}
	if (!el.id) el.id='autoid_'+nr;
	el.onmousedown=function() {
		this.className+=' clicked';
		lastmouser=this;
	}
	//el.onblur=function(){this.className=this.className.replace(/( ?clicked)/g,'');}
	var fun=el.onclick;
	if (!fun) el.onclick=function(){
		if (lastmouser && lastmouser != this) {
			lastmouser.className=lastmouser.className.replace(/( ?clicked)/g,'');
			lastmouser=null;
		}
		store_elem(this);
		return true;
	}
	else {
		if (lastmouser && lastmouder != this) {
			lastmouser.className=lastmouser.className.replace(/( ?clicked)/g,'');
			lastmouser=null;
			return fun();
		}
	}
	if (cooks[this_cook].mode=='m' && cooks[this_cook].exit == el.id) {
		el.className+=' clicked';
		lastmouser=el;
	}
		
}

function getEInfo()
{
	var e=getCookie('cooks');
	if (e) {
		e=e.split(';');
		var i;
		for (i=0;i<e.length;i++) {
			var s=e[i].split(':');
			if (s[0]==window.location.pathname) this_cook=i;
			cooks.push({path:s[0],mode:s[1].charAt(0),exit:s[1].substr(1)});
		}
	}
	if (this_cook<0) {
		this_cook=cooks.length;
		cooks.push({path:window.location.pathname,mode:'m',exit:''});
	}
}

function setEInfo()
{
	var e=new Array();
	var i;
	for (i=0;i<cooks.length;i++) e.push(cooks[i].path+':'+cooks[i].mode+cooks[i].exit);
	setCookie('cooks',e.join(';'),null,"/");
}
function startme()
{
	getEInfo();
	var i;
	var li=document.body.getElementsByTagName('a');
	for (i=0;i<li.length;i++) relink(li[i],i);
}

function voltage()
{
	var ti=document.getElementById('voltag');
	var sp=document.getElementById('voltsgl');
	var el=document.getElementById('volt3');
	if (el) {
		if (ti) ti.disabled=!el.checked;
		if (sp) sp.className=el.checked?'':'auraltext';
	}
}

function polaroid(n)
{
	var el=document.getElementById('wtyczka');
	if (!el) return;
	var t=(n==el.selectedIndex);
	var p;
	if (p=document.getElementById('polar1')) p.disabled=t;
	if (p=document.getElementById('polar2')) p.disabled=t;
	
}

function akuminny()
{
	var el=document.getElementById('akumsy');
	if (!el) return;
	var inny=(el.selectedIndex==el.options.length-1);
	el=document.getElementById('akinny');
	if (!el) return;
	el.disabled=!inny;
	if (el=document.getElementById('innak')) el.className=inny?'':'auraltext';
}

var mail_verify_badcontrol;

function mail_verify_it(forma,str)
{

	var verificator_nip=[6,5,7,2,3,4,5,6,7];
	var verificator_regon=[8,9,2,3,4,5,6,7]
	var verificator_pesel=[1,3,7,9,1,3,7,9,1,3]

	function verify_nip(a)
	{
		a=a.replace(/[^0-9]/g,'')
		if (a.length!=10) return false;
		var i,n;
		for (i=n=0;i<9;i++) n+=a.charAt(i)*verificator_nip[i];
		n%=11;
		return ( n == a.charAt(9));
	}

	function verify_regon(a)
	{
		if (a.search(/[^0-9]/)>=0 || a.length!=9) return false;
		var i,n;
		for (i=n=0;i<8;i++) n+=a.charAt(i)*verificator_regon[i];
		n%=11;
		return ( n == a.charAt(8));
	}

	function verify_pesel(a)
	{
		if (a.search(/[^0-9]/)>=0 || a.length!=11) return false;
		var i,n;
		for (i=n=0;i<10;i++) n+=a.charAt(i)*verificator_pesel[i];
		n%=10;
		return  ( (10-n)%10 == a.charAt(10));
	}

	function d2(str)
	{
		if (str.length<2) return '0'+str;
		return str;
	}
	
	function gempty(str)
	{
		str=str.split('|');
		return str[str.length-1];
	}	
	function gbad(str)
	{
		str=str.split('|');
		return str[0];
	}	

	var n,nam,typ,i,el,va,typek;
	n=str.indexOf(':');
	if (n<0) return false;
	nam=str.substr(0,n);
	str=str.substr(n+1);
	n=str.indexOf(':');
	if (n<0) return false;
	typ=str.substr(0,n);
	str=str.substr(n+1);
	n=typ.indexOf('?');
	if (n>=0) typ=typ.substr(0,n);
	typek=typ.toLowerCase();
	
	if (typek == 'select') {
		for (i=0;i<forma.length;i++) if (forma[i].name == nam && !forma[i].disabled) {
			if (forma[i].selectedIndex==0) {
				mail_verify_badcontrol=forma[i];
				alert(str);
				return false;
			}
		}
		return true;
	}
	if (typek == 'regon') {
		for (i=0;i<forma.length;i++) if (forma[i].name == nam && !forma[i].disabled) {
			va=forma[i].value.replace(/^\s*/g,'').replace(/\s*$/g,'');
			if (va == '') {
				if (typ.charAt(0)=='R') continue;
				alert(gempty(str));
				mail_verify_badcontrol=forma[i];
				return false;
			}
			if (verify_regon(va)) continue;
			alert(gbad(str));
			mail_verify_badcontrol=forma[i];
			return false;
		}
		return true;
	}			
	if (typek == 'pesel') {
		for (i=0;i<forma.length;i++) if (forma[i].name == nam && !forma[i].disabled) {
			va=forma[i].value.replace(/^\s*/g,'').replace(/\s*$/g,'');
			if (va == '') {
				if (typ.charAt(0)=='P') continue;
				alert(gempty(str));
				mail_verify_badcontrol=forma[i];
				return false;
			}
			if (verify_pesel(va)) continue;
			alert(gbad(str));
			mail_verify_badcontrol=forma[i];
			return false;
		}
		return true;
	}			
	if (typek == 'nip') {
		for (i=0;i<forma.length;i++) if (forma[i].name == nam && !forma[i].disabled) {
			va=forma[i].value.replace(/^\s*/g,'').replace(/\s*$/g,'');
			if (va == '') {
				if (typ.charAt(0)=='N') continue;
				alert(gempty(str));
				mail_verify_badcontrol=forma[i];
				return false;
			}
			if (verify_nip(va)) continue;
			alert(gbad(str));
			mail_verify_badcontrol=forma[i];
			return false;
		}
		return true;
	}			
	if (typek == 'radio') {
		for (i=0;i<forma.length;i++) if (forma[i].name == nam && !forma[i].disabled) {
			if (forma[i].checked) return true;
		}
		for (i=0;i<forma.length;i++) if (forma[i].name == nam && !forma[i].disabled) {
			mail_verify_badcontrol=forma[i];
			break;
		}
		alert(str);
		return false;
	}
	if (typek == 'kod') {
		for (i=0;i<forma.length;i++) if (forma[i].name == nam && !forma[i].disabled) {
			va=forma[i].value.replace(/^\s*/g,'').replace(/\s*$/g,'');
			if (va == '') {
				if (typ.charAt(0)=='K') continue;
				alert(gempty(str));
				mail_verify_badcontrol=forma[i];
				return false;
			}
			if (va.search(/^[0-9][0-9]-[0-9][0-9][0-9]$/)==0) continue;
			alert(gbad(str));
			mail_verify_badcontrol=forma[i];
			return false;
		}
		return true;
	}
	if (typek == 'num') {
		for (i=0;i<forma.length;i++) if (forma[i].name == nam && !forma[i].disabled) {
			va=forma[i].value.replace(/^\s*/g,'').replace(/\s*$/g,'');
			if (va == '') {
				if (typ.charAt(0)=='N') continue;
				alert(gempty(str));
				mail_verify_badcontrol=forma[i];
				return false;
			}
			if (va.search(/^[0-9]+$/)==0) continue;
			mail_verify_badcontrol=forma[i];
			alert(gbad(str));
			return false;
		}
		return true;
	}
	
	if (typek == 'email') {
		for (i=0;i<forma.length;i++) if (forma[i].name == nam && !forma[i].disabled) {
			va=forma[i].value.replace(/^\s*/g,'').replace(/\s*$/g,'');
			if (va == '') {
				if (typ.charAt(0)=='E') continue;
				alert(gempty(str));
				mail_verify_badcontrol=forma[i];
				return false;
			}
			if (va.search(/^[-a-z0-9._]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z][a-z]+$/i)<0) {
				alert(gbad(str));
				mail_verify_badcontrol=forma[i];
				return false;
			}
		}
		return true;
	}
	if (typek.substr(0,3)=="txt") {
		var s=typek.substr(3);
		var i=s.indexOf('-');
		if (i<0) return true;
		var mnie=s.substr(0,i);
		var wien=s.substr(i+1);
		for (i=0;i<forma.length;i++) if (forma[i].name == nam && !forma[i].disabled) {
			
			va=forma[i].value.replace(/^\s*/g,'').replace(/\s*$/g,'');
			if (typ.charAt(0)=='T') {
				if (va == '') continue;
				alert(gempty(str));
				mail_verify_badcontrol=forma[i];
				return false;
			}
			if (va.length<mnie || va.length>wien) {
				alert(gbad(str));
				mail_verify_badcontrol=forma[i];
				return false;
			}
		}
		return true;
	}
	//alert(typek)
	return false;
}
	
function mail_verify(iden)
{
	var forma=document.getElementById(iden);
	var el,i;
	for (i=0;i<forma.length;i++) {
		el=forma[i];
		if (el.name && el.name == 'checkit') {
			if (!mail_verify_it(forma,el.value)) {
				try {
					mail_verify_badcontrol.focus();
					mail_verify_badcontrol.scrollIntoView();
				}
				catch(e) {};

				return false;
			}
		}
	}
	return true;
}

var mail_button_press_id;
function mail_send(iden,but)
{
	if (!mail_verify(iden)) {
	if (misio) event.returnValue=false;
		
		return false;
	}
	mail_button_press_id=but;
	document.getElementById(but).disabled=true;
	return true;
}

function mail_button_enable()
{
	if (mail_button_press_id) {
		document.getElementById(mail_button_press_id).disabled=false;
	}
}

var popup_window;
function pokaz(el,w,h)
{
	try {
		w+=30;
		h+=30;

		if (popup_window && popup_window.close && !popup_window.closed) {
			popup_window.close();
		}
			
		var str="dependent=yes,toolbar=no,resizable=yes";
		if (w && h) {
			var sb=0;
			if (w>screen.availWidth) {w=screen.availWidth;sb=1;}
			if (h>screen.availHeight) {h=screen.availHeight;sb=1;}
			str+=",width="+w+",height="+h;
			if (sb) str+=",scrollbars=yes";
			var x,y;
			x=Math.floor((screen.availWidth-w)/2);
			y=Math.floor((screen.availHeight-h)/2);
			if (x>20) {
				str+=(misio?',left=':',screenX=')+x;
			}
			if (y>20) {
				str+=(misio?',top=':',screenY=')+y;
			}
		}
		popup_window=window.open(el.href,"_blank",str);
		if (!popup_window) return true;
		var doc=popup_window.document;
		doc.open();
		var alt="";
		if (el.alt) alt=el.alt;
		doc.write(
	  		'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' +
			'<html id="popup">' +
			'<head><head><title>' + alt + '</title>' +
				'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' +
				'<style type="text/css">' +
				'body,html {margin:0px;padding:0px}\n'+
				'p {text-align:center;margin:0px;padding:15px 0 0 0}\n'+
				'img {display:inline}\n'+
				'</style>'+
				'</head><body><p><img src="'+el.href+'" alt="'+alt+'"></p>' +
				'<script type="text/javascript"> document.body.onmouseup = function(e) {var ev = e||event; if ((ev.button||ev.which) < 2) self.close();} ' +
				'</script></body></html>'
				);
		doc.close();
		if (el.className.indexOf('click')>=0) el.blur();
		if (misio) event.returnValue=false;
		return false;
	}
	catch(e) {
		return true;
	}
}
