var statusLoader = 0;
var statusProfil = 0;
function wczytajDzielnice()
{
	var select = document.getElementById('Profile_miasto');
	$("#Profile_dzielnica").load('./wczytajDzielnice.php?iid='+select.options[select.selectedIndex].value);
}

function dodajFotyStworzProfil(Profile_typ)
{
	var msg = '';
	if($("#zdjecie_glowne option:selected").attr("value")=='brak')
		msg+=brakZdjec;

	if($(":checkbox").is(":checked") && !$("textarea").attr("value"))
		msg+=korekta;
	
	var value = $("#zdjecie_glowne option:selected").attr("value");
	if(!parseInt(value))
		value=1;
		
	var file = $(":file[name='Profile_foto_"+value+"']");
	//alert($(file).attr("value"));
	if(msg=='')
	{
		if(statusLoader==0)
		{
			statusLoader = 1;
			jQuery.post('profilVirtual.php?Profile_typ='+Profile_typ, { korekta: $("textarea").attr("value") }, function (data)
			{
				var form = document.getElementById('formRejestracja');
				form.action = form.action+'?virtual='+data;
				//alert(form.action);
				submitform('formRejestracja');
				startRejestracja(Profile_typ, data, $(file).attr("value"));
			});
		}
		else
		{
			alert("Proszę czekać");
		}
	}
	else
		alert(msg);
}

function zmienFotoGlowne(elem)
{
	var tab = elem.name.split("_");
	var numerFile = parseInt(tab[2]);
	
	if(!elem.value)
	{
		if($("option[value='"+numerFile+"']"))
			$("option[value='"+numerFile+"']").remove();
		return;
	}
	
	for(var k = 1; k<13; k++)
	{
		if($(":file[name='Profile_foto_"+k+"']").attr("value")==elem.value && k!=numerFile)
		{
			$(":file[name='Profile_foto_"+numerFile+"']").attr("value", "")
			alert(wybraneZdjecie);
			return;
		}
	}
				
	if(("#zdjecie_glowne option:contains('brak')"))
		$("#zdjecie_glowne option:contains('brak')").remove();

	var option = document.createElement('option');
	$(option).attr('value', numerFile);
	$(option).html(numerFile);
	
	var select = document.getElementById('zdjecie_glowne');
	
	var status = 0;
	for(var i = 0; i < select.options.length; i++)
	{
		if(select.options[i].value==numerFile)
			status=1
		
		if(select.options[i+1])
		{
			if(parseInt(select.options[i+1].value)>numerFile && parseInt(select.options[i].value)<numerFile && status==0)
			{
				$(option).insertBefore("option:contains('"+select.options[i+1].value+"')");
				status=1;
			}
		}
	}
	
	if(status==0)
		$(option).appendTo('#zdjecie_glowne');
	
	select.selectedIndex = 0;
}

function startRejestracja(Profile_typ, Profile_id, glowneFo)
{
	$('#wgrywaj').slideDown("slow");
	$('#wgrywaj').html(uploadPlikow+' <br> <img src="images/loadfile.gif" border="0">');
	jQuery.post('./wczytajFormularz.php?Profile_typ='+Profile_typ+'&Profile_id='+Profile_id,  { glowneFoto: glowneFo }, function(data)
	{
		$('#rejestracja_contener').html(data);
	});
	
}

function wczytajFormularz(Profile_typ, Profile_id)
{
	/*$('#rejestracja_contener').load('./wczytajFormularz.php?Profile_typ='+Profile_typ+'&Profile_id='+Profile_id, { glowneFoto: glowneFo }, function()
	{
		
	});*/
}

function zatrzymajWgrywanie()
{
	$('#wgrywaj').html('Zdjęcia zostały wgrane, wypełnij formularz aby dodać ogłoszenie.');
	$('#wgrywaj').addClass('dzial_tytul');
}








///******************************* Funkcje notifications ******************************************///



function createNotification(repElement, tekstParam)
{
	if(document.getElementById('notifi'+repElement))
		return;
	
	var div = document.createElement("div");
	div.id = 'notifi'+repElement;
	var pos = $('#'+repElement).offset();
	
	$(div).html('<img src="images/notifi.gif" border="0">');
		
	$(div).addClass('notifi');
	
	$(div).css('top', pos.top+2+"px");
	$(div).css('left', pos.left-17+"px");
	
	if(repElement=='Profile_telefon')
	{
		$(div).css('top', pos.top+2+"px");
		$(div).css('left', pos.left+132+"px");
	}
	
	$(div).mouseover(function()
	{
		if(repElement=='Profile_telefon')
		{
			setRollOver(pos.top+2, pos.left+132, tekstParam)
		}
		else
		{
			setRollOver(pos.top+2, pos.left-17, tekstParam)
		}
	})
	
	$(div).mouseout(function()
	{
		$('.rollOverNotifi').remove();
	})	
	
	document.body.appendChild(div);
}

function removeNotification(repElement)
{
	$('#notifi'+repElement).remove();
}

function setRollOver(top, left, tekstParam)
{
	var div = document.createElement("div");
	$(div).addClass("rollOverNotifi")
	
	$(div).css('top', top+20+"px");
	$(div).css('left', left+"px");
	
	$(div).html(tekstParam);
	document.body.appendChild(div);
}


/*********** Sprawdzanie formularza **************/

function sprawdzLogin(rodzaj)
{
	if(rodzaj)
		var value = document.getElementById('Profile_tytul').value;
	else
		var value = document.getElementById('Profile_login').value;
		
	jQuery.post('sprawdzLogin.php?rodzaj='+rodzaj, { val: value }, function (data)
	{
		if(data=='true')
		{
			if(rodzaj)
				createNotification('Profile_tytul', istniejeTytul);
			else
				createNotification('Profile_login', istniejeLogin);
				
			return 0;
		}
		else
		{
			if(rodzaj)
				removeNotification('Profile_tytul');	
			else
				removeNotification('Profile_login');		
		}
	});
	return 1;
}

function sprawdzJezyk(idJezyk)
{
	var select = document.getElementById(idJezyk);
	if(select.options[select.selectedIndex].value!="brak")
	{
		if(idJezyk=='Profile_jezyk_1')
			document.getElementById('stopien_jezyk_3').checked = true;
		if(idJezyk=='Profile_jezyk_2')
			document.getElementById('stopien_jezyk_7').checked = true;
	}
	else
	{
		if(idJezyk=='Profile_jezyk_1')
		{
			document.getElementById('stopien_jezyk_1').checked = false;
			document.getElementById('stopien_jezyk_2').checked = false;
			document.getElementById('stopien_jezyk_3').checked = false;
			document.getElementById('stopien_jezyk_4').checked = false;
		}
		else
		{		
			document.getElementById('stopien_jezyk_5').checked = false;
			document.getElementById('stopien_jezyk_6').checked = false;
			document.getElementById('stopien_jezyk_7').checked = false;
			document.getElementById('stopien_jezyk_8').checked = false;
		}
	}
}

function sprawdzDzien(elem)
{
	if(document.getElementById("dni_wolne").checked==true)
	{
		elem.checked = false;
	}
}

function sprawdzHasla()
{
	if(document.getElementById('Profile_haslo').value != document.getElementById('Profile_haslo_powtorzenie').value)
		createNotification('Profile_haslo_powtorzenie', rozneHasla);
	else
		removeNotification('Profile_haslo_powtorzenie');
}

function sprawdzEmail(idEmail)
{
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
	if(!document.getElementById('Profile_mejl').value.match(emailPattern))
		createNotification('Profile_mejl', niepoprawnyEmail);
	else
		removeNotification('Profile_mejl');
}

function sprawdzTelefon()
{
	var telefonValue = document.getElementById('Profile_telefon').value;
	var dlugosc = 0;
	var notifi = '';
	for(var i = 0; i < telefonValue.length; i++)
	{
		if(telefonValue.charAt(i).match(/^[0-9]*$/))
			dlugosc++;
	}
	
	removeNotification('Profile_telefon');
	
	document.getElementById('Profile_telefon').value = document.getElementById('Profile_telefon').value.replace(/[a-z]/g, '');
			
	if(dlugosc!=9)
		notifi += zaDlugiTelefon;
	
	if(notifi!='')
	{
		createNotification('Profile_telefon', notifi);
		return true;
	}
	else
	{
		removeNotification('Profile_telefon');
		return false;
	}
}

function sprawdzCene(idCena)
{
	if(!document.getElementById(idCena).value.match(/^[0-9]*$/))
		createNotification(idCena, niepoprawnaCena);
	else
		removeNotification(idCena);
}

function sprawdzFormularz(valChetny)
{
	
		if(statusProfil==0)
			statusProfil=1;
		else {
			alert("Trwa zakładanie konta. Proszę czekać");
			return;
		}
	
	
	var msg = '';
	var value = document.getElementById('Profile_login').value;
	jQuery.post('sprawdzLogin.php', { val: value }, function (data)
	{
		if(data=='true')
		{
			createNotification('Profile_login', istniejeLogin);
			msg += istniejeLogin+"\n";
		}
		else
			removeNotification('Profile_login');		
			
		
		jQuery.get('sprawdzObrazek.php?kod='+document.getElementById('obrazek_tresc').value, function (data)
		{	
			//alert(data)
			if(data=='false')
			{
				msg += niepoprawnaTrescObrazka+"\n";
			}
			
			if(document.getElementById('Profile_tytul'))
				if(document.getElementById('notifiProfile_tytul') || !document.getElementById('Profile_tytul').value)
					msg += istniejeTytul+"\n";

			if(document.getElementById('Profile_login'))
				if(!document.getElementById('Profile_login').value)
					msg += pustyLogin+"\n";
				
			if(document.getElementById('Profile_wiek'))	
				if(document.getElementById('Profile_wiek').options[document.getElementById('Profile_wiek').selectedIndex].value=='brak')
					msg+=uzupelnijWiek+"\n";
	
			if(document.getElementById('Profile_miasto'))
				if($('#Profile_miasto option:selected').attr("value")=='brak' && $('#Profile_miasto_inne').attr("value")=='')
					msg+=WpiszMiasto+"\n";
					
			if(document.getElementById('Profile_miasto'))
				if($('#Profile_dzielnica option:selected').attr("value")=='brak' && $('#Profile_dzielnica_inne').attr("value")=='')
					msg+=WpiszDzielnice+"\n";
					
			if(document.getElementById('Profile_waga'))		
				if(document.getElementById('Profile_waga').options[document.getElementById('Profile_waga').selectedIndex].value=='brak')
					msg+=uzupelnijWage+"\n";
			
			if(document.getElementById('Profile_wzrost'))		
				if(document.getElementById('Profile_wzrost').options[document.getElementById('Profile_wzrost').selectedIndex].value=='brak')
					msg+=uzupelnijWzrost+"\n";
	
			if(document.getElementById('wgrywaj'))
				if(!document.getElementById('wgrywaj').innerHTML.match('Zdjęcia zostały wgrane'))
					msg+="Trwa wgrywanie zdjęć! \n";
				
			if(document.getElementById('Profile_rozmiar'))
				if(document.getElementById('Profile_rozmiar').options[document.getElementById('Profile_rozmiar').selectedIndex].value=='brak')
					msg+=uzupelnijRozmiar+"\n";
	
			if(document.getElementById('Profile_kolor_wlosow'))
				if(document.getElementById('Profile_kolor_wlosow').options[document.getElementById('Profile_kolor_wlosow').selectedIndex].value=='brak')
					msg+=uzupelnijKolor+"\n";
				
			if(document.getElementById('Profile_orientacja'))
				if(document.getElementById('Profile_orientacja').options[document.getElementById('Profile_orientacja').selectedIndex].value=='brak')
					msg+=uzupelnijOrientacje+"\n";

			if(document.getElementById('Profile_data_koniec'))
				if(document.getElementById('Profile_data_koniec').options[document.getElementById('Profile_data_koniec').selectedIndex].value=='brak')
					msg+=wybierzTermin+"\n";
				
			if(!document.getElementById("Profile_mejl").value || document.getElementById("notifiProfile_mejl"))	
				msg+=niepoprawnyEmail+"\n";
					
			var status = 0;
			if(document.getElementById('Profile_jezyk_1'))
				if(document.getElementById('Profile_jezyk_1').options[document.getElementById('Profile_jezyk_1').selectedIndex].value!='brak')
					for(var i = 1; i < 5; i++)
						if(document.getElementById('stopien_jezyk_'+i).checked==true)
						{
							status=0;
							break;
						}
						else
							status=1;
		
			if(status==1)
				msg+=okreslStopienZnajomosci+"\n";
	
			status=0;
			if(document.getElementById('Profile_jezyk_2'))
				if(document.getElementById('Profile_jezyk_2').options[document.getElementById('Profile_jezyk_2').selectedIndex].value!='brak')
					for(var i = 5; i < 9; i++)
						if(document.getElementById('stopien_jezyk_'+i).checked==true)
						{
							status=0;
							break;
						}
						else
							status=1;
		
			if(status==1)
				msg+=okreslStopienZnajomosci+"\n";
			
			if(document.getElementById('Profile_tresc'))
				if(document.getElementById('Profile_tresc').value=="")
					msg+=uzupelnijTresc+"\n";
		
				//alert(sprawdzTelefon())
			if(document.getElementById('Profile_telefon'))		
				if(sprawdzTelefon()==true || !document.getElementById('Profile_telefon').value)
					msg += zaDlugiTelefon+"\n";			
		
			if(msg!="")	{
				alert(msg);
				statusProfil = 0;
			}
			else
			{
				submitform('formDane')
			}
		});
	});
}