/// subskrypcja...

var subscribe = {

	notLogged : function(addButton)
	{
		this.form = addButton.form;
		this.form.sEmail.value = this.form.sEmail.value.trim();
		if( this.form.sEmail.value == '' )
		{
			alert("Prosze podać adres e-mail");
			this.form.sEmail.focus();
			return false;
		}
		if( !this.form.sEmail.value.isEmail() )
		{
			alert(this.form.sEmail.value + ' nie jest poprawnym adresem e-mail');
			this.form.sEmail.focus();
			return false;
		}
		if( !this.form.sAccept.checked )
		{
			alert("Do kontynuacji niezbędne jest zaakceptowanie regulaminu");
			this.form.sAccept.focus();
			return false;
		}
		//this.showCaptchaForm();
		hideObj('dsBox');
		getObjId('sSecCodeImg').src='?secCode&t='+(new Date()).getTime();
		showObj('dsBox2');
	},

	notLoggedPost : function()
	{
		//alert('zaraz postne...');
		if( this.form.sSecCode.value.trim()=='' )
		{
			alert("Proszę podać kod bezpieczeństwa");
			this.form.sSecCode.focus();
			return;
		}
		var ajax = new AJAX( location.protocol+'//'+location.host+'?subscribe');
		ajax.setPostVar('sEmail', this.form.sEmail.value);
		ajax.setPostVar('sAccept', this.form.sAccept.checked?1:0);
		ajax.setPostVar('sSecCode', this.form.sSecCode.value);
		ajax.setPostVar('sUrl', location.href);
		ajax.setPostVar('ajaxRequest', '');
		ajax.onRequestComplete( this.notLoggedReply );
		ajax.execute();
	},

	notLoggedReply : function( http )
	{
		 var response = AJAX.http2obj(http);

		 switch( response.errno )
		 {
			 case 1 :
				 getObjId('sSecCodeImg').src = '?secCode&t='+rdmStr();
				 alert("Kod niepoprawny, proszę ponownie wprowdzić kod bezpieczeństwa.");
				 subscribe.form.sSecCode.value = '';
				 subscribe.form.sSecCode.focus();
				 break;

			 case 2 :
				 //alert( print_r(response) );
				 subscribe.notLoggedLoginRequest();
				 break;
			 
			 case 5 :
				 alert( "taki szablon juz istnieje");
				 hideObj('subscribeAddLink');
				 break;

			 case 0 :
				 //alert( print_r(response) );
				 subscribe.notLoggedComplete();
				 break;
		 }
	},

	notLoggedLoginRequest : function()
	{
		hideObj('dsBox2');
		showObj('dsBoxLogin');
		var form = getObjId('sLoginForm');
		form.fLogin.value = this.form.sEmail.value;
	},

	notLoggedComplete : function()
	{
		hideObj('dsBox2');
		hideObj('dsBoxLogin');
		showObj('dsBox3');
	},
	
	loggedPost : function(searchOnly)
	{
		var ajax = new AJAX( location.protocol+'//'+location.host+'?subscribe');
		ajax.setPostVar('sUrl', location.href);
		ajax.setPostVar('ajaxRequest', '');
		if(searchOnly) ajax.setPostVar('searchOnly', 1);
		ajax.onRequestComplete( this.loggedReply );
		ajax.execute();
	},
	
	loggedReply : function( http )
	{
		 var response = AJAX.http2obj(http);

		 switch( response.errno )
		 {
			 case 5 :
				 alert('taki szablon juz istnieje');
				 hideObj('subscribeAddLink');
				 break;
			 case 0 :
				 subscribe.addSLink( response.sUrl, response.tplName );
				 break;
			 default :
				break;
		 }
	},

	addSLink : function( href, caption )
	{
		var link = getObjId('hiddenSubscribe');
		link.href = href;
		link.innerHTML = '<b>'+caption+'</b>';
		showObj(link.parentNode);
		hideObj('subscribeAddLink');
	}
}


