/* myAjax actions */

function markRelation( cn, cid, pc, pid ) {
	get( 'icon_'+cid ).innerHTML = '';
	var ajax = new myAjax();
	ajax.action = 'markRelation';
	ajax.post( 'cn='+cn+'&cid='+cid+'&pc='+pc+'&pid='+pid, 'icon_'+cid );
}

function unmarkRelation( cn, cid, pc, pid ) {
	get( 'icon_'+cid ).innerHTML = '';
	var ajax = new myAjax();
	ajax.action = 'unmarkRelation';
	ajax.post( 'cn='+cn+'&cid='+cid+'&pc='+pc+'&pid='+pid, 'icon_'+cid );	
}

function toggleCheckboxList( id, pc, pid ) {
	var obj = get( id ).value;
	get( 'list' ).innerHTML = '';
	if( obj != '' ) {
		var ajax = new myAjax();
		ajax.action = 'toggleCheckboxList';
		ajax.post( 'class='+obj+'&parent_class='+pc+'&parent_id='+pid, 'list' );
	}

}



function showComments( pc, id ) {
	var ajax = new myAjax();
	ajax.action = 'showComments';
	ajax.post( 'id='+id+'&class='+pc, 'comments' );
}

function addComment( name ) {
	var nick 		= get( 'author_nick' );
	var comment 	= get( 'comment' );
	var pid			= get( 'parent_id' ).value;
	var pc			= get( 'parent_class' ).value;
	var ac			= get( 'action' ).value;
	if( ( nick.value == '' ) || ( comment.value == '' ) ) {
		alert( 'Proszę wypełnić wszystkie wymagane pola' );
	} else {
		var ajax = new myAjax();
		ajax.action = ac;
		ajax.post( 'nick='+nick.value+'&comment='+comment.value+'&parent_id='+pid+'&parent_class='+pc, 'comments' );
		var commentForm = get( 'commentForm' );
		commentForm.innerHTML = '';			
		ajax.onLoad = function() {
			showComments( pc, pid );
		}
	}
}

function newsletterSubscribe() {
	var t = get( 'newsletter_message' );
	var email 	= getValue( 'newsletter_mail' );
	var type 	= getValue( 'newsletter_type' );
	var ajax 	= new myAjax();
	t.innerHTML = '';
	ajax.post( 'action=newsletter_subscribe&newsletter_mail='+email+'&newsletter_type='+type );
	ajax.onLoad = function() {
		show( 'newsletter_message' );
//		t.innerHTML = this.response;
		switch( trim( this.response ) ) {
			case 'ok':
				hide( 'newsletter' );
				show( 'newsletter_blank' );
				t.innerHTML += '<span style="color:#ffffff; ">Dziękujemy<br/>Twój adres e-mail został zapisany.</span>';	
				break;
			case 'bad_email':
				t.innerHTML += '<span style="color:#ffffff; ">Wpisano błędny adres email</span>';	
				break;
			default:
				t.innerHTML += trim( this.response );
				break;
		}
	}
}

function newsletterUnsubscribe() {
	var t = get( 'newsletter_message' );
	var email 	= getValue( 'newsletter_mail' );
	var ajax 	= new myAjax();
	t.innerHTML = '';
	ajax.post( 'action=newsletter_unsubscribe&newsletter_mail='+email );
	ajax.onLoad = function() {
		show( 'newsletter_message' );
//		t.innerHTML = this.response;
		switch( trim( this.response ) ) {
			case 'ok':
				hide( 'newsletter' );
				show( 'newsletter_blank' );
				t.innerHTML += '<span style="color:#ffffff; ">Twój adres e-mail został skasowany.</span>';	
				break;
			case 'bad_email':
				t.innerHTML += '<span style="color:#ffffff; ">Wpisano błędny adres email</span>';	
				break;
			default:
				break;
		}
	}
}

function updateAjaxIcon( target, sel ) {
	var s = getValue( sel );
	if( document.buffer_icon == undefined ) { document.buffer_icon = s; }
	if( document.buffer_icon != s ) {
		document.buffer_icon = s;
		var ajax = new myAjax();
		ajax.action = 'iconPreview';
		ajax.post( 'icon='+s, target );
	}
	setTimeout( 'updateAjaxIcon( \''+target+'\', \''+sel+'\' );', 1500 );
}

function niceUrl( str, target ) {
		var string = getValue( str );
		string = string.split( '&' ).join( '&amp;' );
		var ajax = new myAjax();
		ajax.action = 'nice_url';
		ajax.post( 'string='+string );
		ajax.onLoad = function() {
			var t = get( target );
			t.value = this.response;
		}
//		setTimeout( 'niceUrl( \''+str+'\', \''+target+'\' );', 1000 );
}