var DDSPEED = 1;
var DDTIMER = 1;
var ZINT = 100;

function ddMenu(idd,d,p){

  var h = document.getElementById('ddheader-' + idd);
  var c = document.getElementById('ddcontent-' + idd);
  clearInterval(c.timer);

	if(p == 1){
		OFFSET = -327;
	} else { OFFSET = -2;}
  if(d == 1){
    clearTimeout(h.timer);
    c.style.display = 'block';
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.left = (h.offsetWidth + OFFSET) + 'px';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    ZINT = ZINT + 1;
    c.style.zIndex = ZINT;
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

function cancelHide(id){
  var h = document.getElementById('ddheader-' + id );
  var c = document.getElementById('ddcontent-' + id );
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = Math.round((c.maxh - currh) / DDSPEED);
  }else{
    dist = Math.round(currh / DDSPEED);
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if(currh > (c.maxh - 2) && d == 1){
    clearInterval(c.timer);
  }else if(dist < 1 && d != 1){
    clearInterval(c.timer);
    c.style.display = 'none';
  }
}

	function insertAtCaret(obj, text,wstecz) {
		/* http://parentnode.org/javascript/working-with-the-cursor-position/ */

		if(document.selection) {
			obj.focus();
			var orig = obj.value.replace(/\r\n/g, "\n");
			var range = document.selection.createRange();

			if(range.parentElement() != obj) {
				return false;
			}

			range.text = text;

			var actual = tmp = obj.value.replace(/\r\n/g, "\n");

			for(var diff = 0; diff < orig.length; diff++) {
				if(orig.charAt(diff) != actual.charAt(diff)) break;
			}style.top

			for(var index = 0, start = 0;
				tmp.match(text)
					&& (tmp = tmp.replace(text, ""))
					&& index <= diff;
				index = start + text.length
			) {
				start = actual.indexOf(text, index);
			}
		} else if(obj.selectionStart) {
			var start = obj.selectionStart;
			var end   = obj.selectionEnd;

			obj.value = obj.value.substr(0, start)
				+ text
				+ obj.value.substr(end, obj.value.length);
		}

		if(start != null) {
			setCaretTo(obj, start + text.length );
		} else {
			obj.value += text;
		}
	//	setCaretTo(obj, start + text.length );
	}


	function setCaretTo(obj, pos,wstecz) {
		if(obj.createTextRange) {
			var range = obj.createTextRange();
			range.move('character', pos);
			range.select();
		} else if(obj.selectionStart) {
			obj.focus();
			obj.setSelectionRange(pos, pos);
		}
	}

function hidediv(id,parent) {

document.getElementById('submenu-' + id).style.visibility = 'hidden';


//ddMenu(parent,-1,0);
//cancelHide(parent);
}

function showdiv(id,parent) {

ddMenu(parent,1,0);

var toto=document.getElementById('submenu-' + id );
var naglowek = document.getElementById('ddheader-' + parent);
var tamto = document.getElementById( 'ddcontent-' + parent );
var poczatek=document.getElementById('poczatek');

var ile = poczatek.offsetLeft + tamto.offsetWidth + naglowek.offsetWidth - 6;

var offset_menu=document.getElementById('aktual').offsetHeight + document.getElementById('baner').offsetHeight + document.getElementById('ddheader-1').offsetHeight + document.getElementById('ddheader-31').offsetHeight + document.getElementById('aktual').offsetTop + document.getElementById('baner').offsetTop + document.getElementById('ddheader-1').offsetTop + document.getElementById('ddheader-31').offsetTop + document.getElementById('poczatek').offsetTop + 4;
//skąd te nadmiarowe 4 piksele?
toto.style.top= offset_menu + 'px';
//ile = 320;
toto.style.left= ile  + 'px';
toto.style.zIndex = 200;
toto.style.opacity=1.0;
toto.style.display='block';
toto.filter = 'alpha(opacity=100)';
toto.style.visibility='visible';


}


function showViaRegia(){
var ile;
var toto=document.getElementById('viaRegiaMenu');
var lewa = document.getElementById('leftcolumn');
//var dzieci=lewa.getElementsByTagName('dt');
/*for (var dziecko in dzieci ){
	ile=dziecko.offsetTop;
	alert(ile);
}
*/
//alert (toto.offsetHeight);
//alert (lewa.clientHeight);
toto.style.zIndex = 200;
toto.style.opacity=1.0;
toto.style.display='block';
toto.filter = 'alpha(opacity=100)';
toto.style.visibility='visible';
toto.style.top= lewa.clientHeight - document.getElementById('viaregia').clientHeight + document.getElementById('aktual').offsetHeight + document.getElementById('baner').offsetHeight + 20 + 'px';

var poczatek=document.getElementById('poczatek');

var ile = poczatek.offsetLeft + lewa.offsetWidth - 6;
toto.style.left= ile  + 'px';
	}

function hideViaRegia(){
	var toto=document.getElementById('viaRegiaMenu');
toto.style.display='none';
toto.filter = 'alpha(opacity=0)';
toto.style.visibility='hidden';
	}


function trzym(){
	var toto=document.getElementById('viaRegiaMenu');
toto.style.display='block';
toto.filter = 'alpha(opacity=100)';
toto.style.visibility='visible';

	}

function killMenu (){
	for(var i=0; i<arguments.length; i++) {
		ddMenu(arguments[i],-1,0);
	}
	}

function test(form){
	var ankieta=form.id.split('-');
	//alert(ankieta[1]);
	odczerwien(ankieta[1]);
	if (checkRadio(form)){
		return checkTekstowe(form);

	} else {
		return false;
	}

}


function sprawdz(nowa,stara){
	if(nowa.length != stara.length){
	return false;
	}

	for(a=0;a < nowa.length;a++){
		if(nowa[a] != stara[a]){
		return false;
		}

	}
	return true;
	}

function odczerwien(ankieta){
	var a=1;
	while(ten=document.getElementById('radio'+ankieta+'_'+a+'-span')){
		ten.style.color="black";
		a++;
	}
	document.getElementById('tekst1_1-span').style.color="black";
	document.getElementById('tekst1_3-span').style.color="black";


}

function checkTekst(form){
	var jest=0;
	for (i=0, n=form.elements.length; i<n; i++) {

	if(form.elements[i].value){
		jest++;
	}

	}
	if (jest==3){
		alert ("Prosimy wybrać i krótko uzasadnić jedną odpowiedź.");
		return false;
	}
	if (jest > 4){
		alert ("Prosimy wybrać tylko jedną odpowiedź.");
		return false;
	}
	return true;
}

function checkRadio(form,maks){
	var kolejny=0;
	var jest=0;
	var porownaj=[];
for (i=0, n=form.elements.length; i<n; i++) {
   if (form.elements[i].checked) {
     if(form.elements[i].name.indexOf('radio') > -1){
			kolejny++;
			var wybrano=form.elements[i].value;

			}
			if(form.elements[i].name.indexOf('check1') > -1){
			jest++;

			}
		 }
   }

	if(kolejny == 1){
	//mamy jeden - czy mamy checkboxy?
	if (document.getElementById('checkboksy')){
		if(document.getElementById('checkboksy').style.visibility=='visible'){
	if(jest >0){
		//alert(jest);
	if(maks){
		if(jest > maks){
		alert("Prosimy zaznaczyć maksymalnie " + maks + " odpowiedzi!");
		return false;
		}
	}
	return true;
	} else {
		alert("Prosimy zaznaczyć przynajmniej jedną z odpowiedzi!");
	return false;
}
	} else {
	return true;
	}
	} else {

		if(maks==4){
		if(wybrano==4){
			//nie sprawdzamy
			return true
		} else {
			var tekst=document.getElementById('komentarz');
			if(tekst.value==''){
				alert("Prosimy uzasadnić wybraną odpowiedź");
				return false;
			} else {
			return true;
			}
		}
		}

		return true;
	}
	} else {
		alert("Prosimy wybrać odpowiedź!");
		return false;
	}
	}

function usunCheck(){
	var form = document.getElementById('ankieta-1');

	for (i=0, n=form.elements.length; i<n; i++) {
   if (form.elements[i].checked) {

			if(form.elements[i].name.indexOf('check1') > -1){
			form.elements[i].checked = false;

			}
		 }
   }


	}

function checkTekstowePiec(form){

	//pierwsze tekstowe:
	if(form.tekst1_1_1.value=='' && form.tekst1_1_2.value=='' && form.tekst1_1_3.value=='' && form.tekst1_1_4.value=='' && form.tekst1_1_5.value=='' ){
		//focus - potem
		document.getElementById('tekst1_1-span').style.color="red";
		document.getElementById('tekst1_1_1').focus();
		alert('Proszę wpisać co najmniej jedną nazwę!');
		return false;
	}
	if(form.tekst1_2_1.value=='' && form.tekst1_2_2.value=='' && form.tekst1_2_3.value=='' && form.tekst1_2_4.value=='' && form.tekst1_2_5.value=='' ){
		//focus - potem
		document.getElementById('tekst1_1-span').style.color="red";
		document.getElementById('tekst1_2_1').focus();
		alert('Proszę wpisać co najmniej jedną nazwę!');
		return false;
	}

	return true;
	}


function checkTekstoweCztery(form){
		if(form.tekst1_3_1.value=='' && form.tekst1_3_2.value=='' && form.tekst1_3_3.value=='' && form.tekst1_3_4.value=='' && form.tekst1_3_5.value=='' ){
		//focus - potem
		document.getElementById('tekst1_3-span').style.color="red";
		document.getElementById('tekst1_3_1').focus();
		alert('Proszę wpisać co najmniej jedną nazwę!');
		return false;
	}
	if(form.tekst1_4_1.value=='' && form.tekst1_4_2.value=='' && form.tekst1_4_3.value=='' && form.tekst1_4_4.value=='' && form.tekst1_4_5.value=='' ){
		//focus - potem
		document.getElementById('tekst1_3-span').style.color="red";
		document.getElementById('tekst1_4_1').focus();
		alert('Proszę wpisać co najmniej jedną nazwę!');
		return false;
	}

	return true;

	}

function testSzesc(form){
var wartosci=[]
for(var i = 0; i < 6; i++) {
		if(form.radio1_19[i].checked) {

wartosci[0]=form.radio1_19[i].value;
break;
}
}
for(var i = 0; i < 6; i++) {
		if(form.radio1_20[i].checked) {

wartosci[1]=form.radio1_20[i].value;
break;
}
}
for(var i = 0; i < 6; i++) {
		if(form.radio1_21[i].checked) {

wartosci[2]=form.radio1_21[i].value;
break;
}
}
for(var i = 0; i < 6; i++) {
		if(form.radio1_22[i].checked) {

wartosci[3]=form.radio1_22[i].value;
break;
}
}
for(var i = 0; i < 6; i++) {
		if(form.radio1_23[i].checked) {

wartosci[4]=form.radio1_23[i].value;
break;
}
}
for(var i = 0; i < 6; i++) {
		if(form.radio1_24[i].checked) {

wartosci[5]=form.radio1_24[i].value;
break;
}
}


var suma = 1;
for (i=0; i<6; i++)
  {
  suma = suma * parseInt(wartosci[i]);
  }

if (suma==720){
return true;
} else {
	alert('Proszę wybrać różne wartości!');
	return false;
}
	}

function hideText5(){
var doSchowania = document.getElementById('tekst1_5');

if (doSchowania.style.display=='none'){
	doSchowania.style.display='block';
	doSchowania.style.visibility='visible';
}else{
	doSchowania.style.display='none';
	doSchowania.style.visibility='hidden';
	doSchowania.value='';

}

	}

function schowajTekst5(){
var doSchowania = document.getElementById('tekst1_5');

	doSchowania.style.display='none';
	doSchowania.style.visibility='hidden';
	doSchowania.value='';



	}

function pokazTip(id){
	var ten = document.getElementById(id);
	ten.style.display='block';
	ten.style.visibility='visible';
	}

function schowajTip(id){
	var ten = document.getElementById(id);
	ten.style.display='none';
	ten.style.visibility='hidden';
	}

function greyText(ile){
	var form = document.getElementById('ankieta-1');
	for (i=0, n=form.elements.length; i<n; i++) {
   if (form.elements[i].checked) {
     if(form.elements[i].name.indexOf('radio') > -1){

			if(form.elements[i].value == 3)
			{
				document.getElementById('szaro_2').style.color='gray';
				document.getElementById('szaro_3').style.color='gray';
				document.getElementById('szaro_1').style.color='black';
				document.getElementById('szaro_2').style.fontWeight='normal';
				document.getElementById('szaro_3').style.fontWeight='normal';
				document.getElementById('szaro_1').style.fontWeight='bold';
				if(ile==4){
				document.getElementById('szaro_4').style.color='gray';
				document.getElementById('szaro_4').style.fontWeight='normal';
				}
			}
			if(form.elements[i].value == 2)
			{
				document.getElementById('szaro_1').style.color='gray';
				document.getElementById('szaro_2').style.color='black';
				document.getElementById('szaro_1').style.fontWeight='normal';
				document.getElementById('szaro_2').style.fontWeight='bold';

				if(ile==2){
				return 1;
				}

				document.getElementById('szaro_3').style.fontWeight='normal';
				document.getElementById('szaro_3').style.color='gray';
				if(ile==4){
				document.getElementById('szaro_4').style.color='gray';
				document.getElementById('szaro_4').style.fontWeight='normal';
				}
			}
			if(form.elements[i].value == 1)
			{

				if(ile==2){

				document.getElementById('szaro_1').style.color='black';
				document.getElementById('szaro_2').style.color='gray';
				document.getElementById('szaro_1').style.fontWeight='bold';
				document.getElementById('szaro_2').style.fontWeight='normal';

				return 1;
				}

				document.getElementById('szaro_2').style.color='gray';
				document.getElementById('szaro_1').style.color='gray';

				document.getElementById('szaro_2').style.fontWeight='normal';
				document.getElementById('szaro_1').style.fontWeight='normal';
				document.getElementById('szaro_3').style.fontWeight='bold';
				document.getElementById('szaro_3').style.color='black';
				if(ile==4){
				document.getElementById('szaro_4').style.color='gray';
				document.getElementById('szaro_4').style.fontWeight='normal';
				}
			}
			if(ile==4){
			if(form.elements[i].value == 4){
				document.getElementById('szaro_4').style.color='black';
				document.getElementById('szaro_2').style.color='gray';
				document.getElementById('szaro_1').style.color='gray';
				document.getElementById('szaro_3').style.color='gray';
				document.getElementById('szaro_4').style.fontWeight='bold';
				document.getElementById('szaro_2').style.fontWeight='normal';
				document.getElementById('szaro_1').style.fontWeight='normal';
				document.getElementById('szaro_3').style.fontWeight='normal';
				}
			}
	}

	}
}
}

function osobowe(form){
	if(!form.imie.value){
	alert('Prosimy o podanie imienia');
	form.imie.focus();
	return false;
	}

	if(!form.nazwisko.value){
	alert('Prosimy o podanie nazwiska');
	form.nazwisko.focus();
	return false;
	}

	if(!form.stanowisko.value){
	alert('Prosimy o podanie stanowiska');
	form.stanowisko.focus();
	return false;
	}
	var ttelefon=form.telefon.value;
	if(!ttelefon){
	alert('Prosimy o podanie telefonu');
	form.telefon.focus();
	return false;
	}
	var email=form.mail.value;
	if(!email){
	alert('Prosimy o podanie adresu e-mail');
	form.mail.focus();
	return false;
	} else {
		if(email.indexOf('@') <= 0 || email.indexOf('.') <= 0  ){

			alert('Prosimy o podanie maila w formie "xxx@xxx(.xxx).xxx"');
			form.mail.focus();
			return false;
		}
	}

	var tu=email.indexOf('@');
	var b=0;
	while(b < email.length){
		ostatnia=email.indexOf('.',b);
		if(ostatnia > tu){
		break;
		}
		b++;

	}

	if (b==email.length){
		alert('Prosimy o podanie maila w formie "xxx@xxx(.xxx).xxx"');
			form.mail.focus();
			return false;
	}


	if(!form.id.value){
	alert('Prosimy o wybranie gminy');
	form.id.focus();
	return false;
	}

	if(! form.zgoda.checked){
	alert('Prosimy o zgodę na przetwarzanie danych osobowych!');
	form.zgoda.focus();
	return false;
	}
	return true;
	}

