//funkcje zwrotne

function axWalidFormWyslij(form, funkcja)
{
	if (form.sys_zatwierdz.value=='1')
		return true;
	var sForm=form.serialize(true);
	form.disable();
	sForm.ajax_spr_form='on';
	new Ajax.Request(glNazwaSkryptu, { parameters: sForm, onComplete: funkcja });
	return false;
}

function axWalidFormOdbierz(arg, nazwaForm, czyBrakSubmit, czyBrakKomunik, czyWpiszKomunikTresc)
{
	var form=$(nazwaForm);
	form.enable();

	var obZwrot=new Object();
	obZwrot.czySuccess=arg.request.success();
	//alert(arg.responseText);
	if (obZwrot.czySuccess)
	{
		var typBledu=arg.responseXML.getElementsByTagName('typBledu');
		var komunikat=arg.responseXML.getElementsByTagName('komunikat');
		obZwrot.komunikat=komunikat.length ? komunikat[0].childNodes[0].nodeValue : '';
		obZwrot.typBledu=typBledu.length ? typBledu[0].childNodes[0].nodeValue : '';

		var czyNieSubmit=false;
		
		//nowy token
		var nowyIdToken=arg.responseXML.getElementsByTagName('nowyIdToken');
		if (nowyIdToken.length)
		{
			nowyIdToken=nowyIdToken[0].childNodes[0].nodeValue;
			form.obrazToken.value=nowyIdToken;
			var psrc=new String($('iframe_obrazToken').getAttribute('src'));
			psrc=psrc.substr(0,psrc.lastIndexOf('=')+1);
			$('iframe_obrazToken').src=psrc+nowyIdToken;
		}
		
		if (typBledu.length)
		{
			czyNieSubmit=true;
			//if ((obZwrot.typBledu=='bledneDane') || (obZwrot.typBledu=='bledneHasla'))
			{
				obZwrot.pola=arg.responseXML.getElementsByTagName('pola');
				//alert(obZwrot.pola.length);
			}
			if ((obZwrot.komunikat!='') && !czyBrakKomunik)
			{
				if (obZwrot.typBledu=='confirm')
					czyNieSubmit=!confirm(obZwrot.komunikat);
				else if (czyWpiszKomunikTresc)
				{
					//var form=$(document.forms['formUzytDopis']);
					switch (obZwrot.typBledu)
					{
					default:
						/*var tabText=form.getInputs();
						//alert(obZwrot.pola);
						for (i=0;i<tabText.length;i++)
						{
							var typ=tabText[i].getAttribute('type');
							if ((typ!='text') && (typ!='password'))
								continue;
							var czyJest=false;
							if (obZwrot.pola)
								for (j=0;j<obZwrot.pola.length;j++)
								{
									if (obZwrot.pola[j].getAttribute('id')==tabText[i].getAttribute('name'))
									{
										tabText[i].className='logerror';
										czyJest=true;
										break;
									}
								}
							if (!czyJest)
								tabText[i].className='log';
						}		
						break;*/
					}
					if (obZwrot.typBledu!='')
					{
						alert(obZwrot.komunikat);
					}
				}
				else
				{
					alert(obZwrot.komunikat);
				}
			}
		}
		
		if (!czyNieSubmit && !czyBrakSubmit)
		{
			form.sys_zatwierdz.value='1';
			form.submit();
		}
	}
	return obZwrot;
}


function axWalidForm(arg, form)
{
	if (typeof arg=='boolean')
	{
		return axWalidFormWyslij(form,axWalidForm);
	}
	else
	{
		var wynik=axWalidFormOdbierz(arg, 'formGlowny',false,false,true);
		if (wynik.typBledu=='')
		{
			$('sys_komWysylanie').style.visibility='visible';
			var form=$('formGlowny');
			form.disable();
			//form.submit();
		}
	}
}

function axUzytDopis(arg, form)
{
	//t_cfg
	if (typeof arg=='boolean')
	{
		return axWalidFormWyslij(form,axUzytDopis);
	}
	else
	{
		var obBlad=axWalidFormOdbierz(arg, 'formUzytDopis',false,false,true);
	}
}

function axWalidLogowanie(arg, form)
{
	//t_cfg
	var login=new String(form.kon_login.value);
	var haslo=new String(form.kon_has.value);
	if ((login.length<4) || (haslo.length<4))
	{
		alert('Niepoprawny login lub hasło');
		return false;
	}
	return true;
}

function axAdminWalidForm(arg, form, idKat)
{
	switch (idKat)
	{
	}

	return true;
}

function axWalidKomentDodaj(arg, form)
{
	//t_cfg
	if (typeof arg=='boolean')
	{
		//$('komentDodaj').hide();
		//$('komentDodajOczek').show();
		return axWalidFormWyslij(form,axWalidKomentDodaj);
	}
	else
	{
		if (arg.request.success())
		{
			//alert(arg.responseText);
			var obZwrot=axWalidFormOdbierz(arg, 'formKomentDodaj',true);
			var form=$('formKomentDodaj');
			//$('komentDodajOczek').hide();
			//$('komentDodaj').show();

			if (obZwrot.typBledu=='')
			{
				//t_cfg
				//$('komentDodajKomunikat').insert(obZwrot.komunikat);
				if (form.obrazToken)
				{
					var obrazToken=form.obrazToken.value;
				}
				form.reset();
				if (form.obrazToken)
				{
					form.obrazToken.value=obrazToken;
				}
				komentPrzelaczStrone(1);
			}
		}
	}
}

function axKomentDodaj(arg,form)
{
	if (typeof arg=='boolean')
	{
		$('komentDodaj').style.visibility='hidden';
		$('komentDodajOczek').style.visibility='visible';
		return true;
	}
	else
	{
		$('komentDodajOczek').style.visibility='hidden';
		$('komentDodaj').style.visibility='visible';

		//t_cfg
		if (arg.request.success())
		{
			$('komentDodajKomunikat').innerHTML='';
			$('komentDodajKomunikat').insert(arg.responseText);
			$('formKomentDodaj').reset();
			komentPrzelaczStrone(1);
		}
		else
			$('komentDodajKomunikat').innerHTML='Operacja nie powiodła się!';
			
	}
}

function axKomentWyswietl(arg)
{
	if (typeof arg=='boolean')
	{
		$('komentWyswietl').style.visibility='hidden';
		$('komentWyswietlOczek').style.visibility='visible';
		return true;
	}
	else
	{
		$('komentWyswietlOczek').style.visibility='hidden';

		//t_cfg
		if (arg.request.success())
		{
			$('komentWyswietl').innerHTML='';
			$('komentWyswietl').insert(arg.responseText);
		}
		else
			$('komentWyswietl').innerHTML='Operacja nie powiodła się!';
		
		$('komentWyswietl').style.visibility='visible';
			
	}
}

function axSzukajWalid(arg,form)
{
	return form.formEnt.getAttribute('value')!='';
}
