/* myAjax actions */

function updatePhotoRel( class_n, selform, target ) {
	var ajax = new myAjax();
	ajax.action = 'updatePhotoRelation';
	var sel = document[ 'form_1' ][ selform ];
	for( j = 0; j < sel.length; j++ ) {
		if( sel.options[ j ].selected ) {
			selected = sel.options[ j ].value;
		}
	}
	ajax.post( 'class='+class_n+'&id='+selected, target );	

}

function swapHashCode() {
	var h		= get( 'hashcode' );
	h.value = 'foto';
}

function try_contact() {
	var h		= get( 'hashcode' );	
	var e 	= getValue( 'email' );
	var l		= getValue( 'login' );
	var t		= getValue( 'title' );
	var b 	= getValue( 'body' );
	if( ( e == '' ) || ( b == '' ) ) {
		alert( 'Wprowadź swój adres e-mail i wpisz wiadomość' );
	} else {
		h.value = 'foto';
		var ajax	= new myAjax();
		hide( 'div_contact' );
		ajax.action = 'try_contact';
		ajax.post( 'email=' + e + '&body=' + b + '&login=' + l + '&title=' + t + '&hashcode='+h.value, 'contact_message' );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				show( 'contact_message_send' );
			} else {
				show( 'div_contact' );
			}
		}
	}
}

function try_remind() {
	var e 	= get( 'email' );
	e.value = trim( e.value );	
	if( e.value == '' ) {
		alert( 'Wprowadź swój adres e-mail' );
	} else {
		var ajax	= new myAjax();
		hide( 'div_reminder' );
		ajax.action = 'try_remind';
		ajax.post( 'email=' + e.value, 'reminder_message' );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				document.location.href = "/Haslo_zostalo_wyslane";
			} else {
				show( 'div_reminder' );
			}
		}
	}
}

function try_register() {
	var lq 		= get( 'login' );
	var p 	= get( 'password' );
	var pr 	= get( 'password_repeat' );
	var n 	= get( 'nick' );
	var e 	= get( 'email' );
	var h		= get( 'hashcode' );
	lq.value = trim( lq.value );
	p.value = trim( p.value );
	pr.value = trim( pr.value );
	n.value = trim( n.value );
	e.value = trim( e.value );	
	if( ( lq.value == '' ) || ( p.value == '' ) || ( pr.value == '' ) || ( e.value == '' ) || ( n.value == '' ) ) {
		alert( 'Musisz wypełnić wszystkie pola oznaczone gwiazdką' );
	} else if ( p.value != pr.value ) {
		alert( 'Hasło nie zgadza się z powtórzeniem hasła' );
	} else if( p.value == lq.value ) {
		alert( 'Hasło nie może być takie same jak login' );
	} else {
		var ajax	= new myAjax();
		hide( 'div_register' );
		ajax.action = 'try_register';
		ajax.post( 'nick=' + n.value + '&email=' + e.value + '&password='+p.value, 'reg_message' );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				h.value = 'foto';				
				document.forms[ 'register' ].submit();
			} else {
				show( 'div_register' );
			}
		}
	}
}

function showFaq( id ) {
	var span = 'faq_' + id.toString();
	var f = get( span );
	for( i in document.faqs ) {
		hide( 'faq_' + document.faqs[ i ].toString() );
	}
	if( f.innerHTML == '' ) {
		var ajax = new myAjax();
		ajax.post( 'action=faq&id='+id.toString(), span );
	} else {
		show( f );
	}
}

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:
				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 );
}