function dirname(path) {
    return path.match( /.*\// );
}

var SIU = {
	WSURL : self.location.protocol+'//'+self.location.hostname+'/irs/siu_ws.php5',
	siuDivLoginId : "siu_div_login",
	siuDivSelectId : "siu_div_select",
	siuSelectProfileId : "siu_select",
	siuDivNewUser : "siu_div_save_new",
	siuDivNewProfile : "siu_div_save_old",

	session : null,
	profiles : null,
	login : function(email,password)
			{
				advAJAX.post({
				    	url: SIU.WSURL,
						mimeType : "text/plain",
				    	parameters : {
					      "email" : email,
					      "password" : password,
					      "type" : "get_profiles"
					    },
				    	onSuccess : function(obj) { SIU.parseResponse(obj.responseText);   },
				    	onError : function(obj) { alert("Error: " + obj.status); }
					});
			},

	parseResponse : function(response)
			{
				responseObj = null;
				try 
				{
					responseObj = eval("(" + response + ")");
				}
				catch (e)
				{
					alert("Incorect response");
					return;
				}

				switch(responseObj["response"]["status"])
				{
					 case "0" : break;//- brak akcji
					 case "101" : //- poprawnie pobrane dane profili
					 				SIU.session = responseObj["response"]["session"];
					 				SIU.buildProfiles(responseObj["response"]["profiles"]);
					 				SIU.changeDisplay();
					 				break;
					 case "102" : //- poprawnie dodany user
					 				SIU.session = responseObj["response"]["session"];
					 				SIU.refreshProfiles();
					 				alert("Dodano nowy profil");
					 				break;
					 case "200" : alert("Błąd bazy danych"); break;//- blad bazy danych
					 case "201" : alert("Brak podanego typu operacji"); break;//- brak podanego typu operacji
					 case "202" : alert("Nieprawidłowy typ operacji"); break;//- nieprawidlowy typ operacji
					 case "211" : alert("Brak podanego emaila lug hasła"); break;//- brak podanego emaila lub hasla
					 case "212" : alert("Nieprawidłowy email lub hasło"); break;//- nieprawidlowy email lub haslo
					 case "213" : alert("Nieprawidłowe session Id"); break;//- nieprawidlowe sessionID
					 case "221" : alert("Podany email istnieje w bazie"); break;//- podany email juz istnieje w bazie
					 case "222" : alert("Brak nazwy profilu"); break;//- brak nazwy profilu
					 default: alert("Nieznany status odpowiedzi");
				}
			},

	changeDisplay : function()
			{
				document.getElementById(SIU.siuDivLoginId).style.display="none";
				document.getElementById(SIU.siuDivNewUser).style.display="none";
				document.getElementById(SIU.siuDivSelectId).style.display="block";
				document.getElementById(SIU.siuDivNewProfile).style.display="block";

			},

	buildProfiles : function(profiles)
			{
				SIU.profiles = profiles;
				select = document.getElementById(SIU.siuSelectProfileId);
				while (select.length > 1)
					select[select.length-1] = null;
				for (var i=0; i<profiles.length; i++)
					select[select.length] = new Option(profiles[i]["name"],i);
			},

	loadProflie : function()
			{
				select = document.getElementById(SIU.siuSelectProfileId);
				var i = select[select.selectedIndex].value;
				if (i==-1)
					return;
				profile = SIU.profiles[i];
				document.getElementById("etap_1_tyt_AD_1").value = profile["details"]["title"];
				document.getElementById("etap_1_imie_AD_1").value = profile["details"]["name"];
				document.getElementById("etap_1_nazw_AD_1").value = profile["details"]["surname"];
				document.getElementById("etap_2_ulica").value = profile["details"]["address"];
				document.getElementById("etap_2_kod").value = profile["details"]["zip"];
				document.getElementById("etap_2_miasto").value = profile["details"]["city"];
				document.getElementById("etap_2_panstwo").value = profile["details"]["country"];
				document.getElementById("etap_2_telefon").value = profile["details"]["phone"];
				//document.getElementById("telefonkom").value = profile["details"]["mobile"];
				document.getElementById("etap_2_email").value = profile["details"]["email"];
				document.getElementById("etap_1_dane_osobowe[kartaff_AD_1]").value = profile["details"]["ffcard"];
				document.getElementById("etap_1_dane_osobowe[pos_AD_1]").value = profile["details"]["meal"];
				document.getElementById("etap_2_uwagi_AD_1").value = profile["details"]["notes"];
				document.getElementById("etap_2_fak_nip").value = profile["details"]["i_nip"];
				document.getElementById("etap_2_fak_firma").value = profile["details"]["i_comp_name"];
				document.getElementById("etap_2_fak_imie").value = profile["details"]["i_name"];
				document.getElementById("etap_2_fak_nazw").value = profile["details"]["i_surname"];
				document.getElementById("etap_2_fak_ulica").value = profile["details"]["i_address"];
				document.getElementById("etap_2_fak_kod").value = profile["details"]["i_zip"];
				document.getElementById("etap_2_fak_miasto").value = profile["details"]["i_city"];
				//document.getElementById("etap_2_fak_panstwo").value = profile["details"]["i_country"];
				document.getElementById("siu_profile_name_old").value = profile["name"];
			},

	saveNewUser : function()
			{
			
				profileName = document.getElementById("siu_profile_name").value;
				password = document.getElementById("siu_password_save").value;
				email = document.getElementById("etap_2_email").value;
				if (profileName.length == 0)
				{
					alert("nie podano nazwy profilu");
					return;
				}
				if (password.length == 0)
				{
					alert("nie podano hasła do profilu");
					return;
				}
				if (email.length == 0)
				{
					alert("nie podano emaila");
					return;
				}
				advAJAX.post({
				    	url: SIU.WSURL,
						mimeType : "text/plain",
				    	parameters : {
					      "email" : email,
					      "password" : password,
					      "type" : "set_profile",
					      "profile_name" : profileName,
					      "title" : document.getElementById("etap_1_tyt_AD_1").value,
						  "name" : document.getElementById("etap_1_imie_AD_1").value,
						  "surname" : document.getElementById("etap_1_nazw_AD_1").value,
						  "address" : document.getElementById("etap_2_ulica").value,
						  "zip" : document.getElementById("etap_2_kod").value,
						  "city" : document.getElementById("etap_2_miasto").value,
						  "country" : document.getElementById("etap_2_panstwo").value,
	  					  "phone" : document.getElementById("etap_2_telefon").value,
						  "mobile" : '',
						  "ffcard" : document.getElementById("etap_1_dane_osobowe[kartaff_AD_1]").value,
						  "meal" : document.getElementById("etap_1_dane_osobowe[pos_AD_1]").value,
						  "notes" : document.getElementById("etap_2_uwagi_AD_1").value,
						  "i_nip" : document.getElementById("etap_2_fak_nip").value,
						  "i_comp_name" : document.getElementById("etap_2_fak_firma").value,
						  "i_name" : document.getElementById("etap_2_fak_imie").value,
						  "i_surname" : document.getElementById("etap_2_fak_nazw").value,
						  "i_address" : document.getElementById("etap_2_fak_ulica").value,
						  "i_zip" : document.getElementById("etap_2_fak_kod").value,
						  "i_city" : document.getElementById("etap_2_fak_miasto").value,
						  "i_country" : ''
					    },
				    	onSuccess : function(obj) {SIU.parseResponse(obj.responseText);   },
				    	onError : function(obj) { alert("Error: " + obj.status); }
					});					
			},

	addProfile : function ()
			{
				profileName = document.getElementById("siu_profile_name_old").value;
				email = document.getElementById("etap_2_email").value;
				if (profileName.length == 0)
				{
					alert("nie podano nazwy profilu");
					return;
				}
				if (email.length == 0)
				{
					alert("nie podano emaila");
					return;
				}

				advAJAX.post({
				    	url: SIU.WSURL,
						mimeType : "text/plain",
				    	parameters : {
					      "session" : SIU.session,
					      "type" : "set_profile",
					      "profile_name" : profileName,
					      "title" : document.getElementById("etap_1_tyt_AD_1").value,
						  "name" : document.getElementById("etap_1_imie_AD_1").value,
						  "surname" : document.getElementById("etap_1_nazw_AD_1").value,
						  "address" : document.getElementById("etap_2_ulica").value,
						  "zip" : document.getElementById("etap_2_kod").value,
						  "city" : document.getElementById("etap_2_miasto").value,
						  "country" : document.getElementById("etap_2_panstwo").value,
	  					  "phone" : document.getElementById("etap_2_telefon").value,
						  "mobile" : '',
						  "email" : document.getElementById("etap_2_email").value,
						  "ffcard" : document.getElementById("etap_1_dane_osobowe[kartaff_AD_1]").value,
						  "meal" : document.getElementById("etap_1_dane_osobowe[pos_AD_1]").value,
						  "notes" : document.getElementById("etap_2_uwagi_AD_1").value,
						  "i_nip" : document.getElementById("etap_2_fak_nip").value,
						  "i_comp_name" : document.getElementById("etap_2_fak_firma").value,
						  "i_name" : document.getElementById("etap_2_fak_imie").value,
						  "i_surname" : document.getElementById("etap_2_fak_nazw").value,
						  "i_address" : document.getElementById("etap_2_fak_ulica").value,
						  "i_zip" : document.getElementById("etap_2_fak_kod").value,
						  "i_city" : document.getElementById("etap_2_fak_miasto").value,
						  "i_country" : ''
					    },
				    	onSuccess : function(obj) { SIU.parseResponse(obj.responseText);   },
				    	onError : function(obj) { alert("Error: " + obj.status); }
					});
			},

	refreshProfiles : function ()
			{
				advAJAX.post({
				    	url: SIU.WSURL,
						mimeType : "text/plain",
				    	parameters : {
					      "session" : SIU.session, 
					      "type" : "get_profiles"
					    },
				    	onSuccess : function(obj) { SIU.parseResponse(obj.responseText);   },
				    	onError : function(obj) { alert("Error: " + obj.status); }
					});
			}
}