var msgToDel = '';

function tryRemoveComment( eid ) {
	if( confirm( 'Czy na pewno chcesz skasować ten komentarz ?' ) ) {
		var ajax = new myAjax();
		ajax.action = 'tryRemoveComment';
		ajax.post( 'id='+eid );
		ajax.onLoad = function() {
			window.location.reload();
		}
	}
}

function tryRemoveProfile() {
	if( confirm( "Czy jesteś pewien swojej decyzji ?\nProces usunięcia konta jest nieodwracalny" ) ) {
		window.location.href = '/profil/usun-konto/';
	}
}

function tryRemovePhoto() {
	if( confirm( 'Czy na pewno usunąć zdjęcie ?' ) ) {
		var ajax = new myAjax();
		ajax.action = 'tryRemovePhoto';
		ajax.post( 'id=1' );
		ajax.onLoad = function() {
			window.location.reload();
		}
	}
}

function removeEvent( eid ) {
	if( confirm( 'Czy na pewno usunąć ten wpis?' ) ) {
		var ajax = new myAjax();
		ajax.action = 'removeEvent';
		ajax.post( 'id='+eid );
		ajax.onLoad = function() {
			switch( this.response ) {
				case 'ok':
					window.location.reload();
					break;
				case 'error':
					alert( 'Nie możesz usunąć tego wpisu' );
					break;
				default:
					alert( this.response );
					break;
			}
		}
	}
}

function delMessage( mid ){
	msgToDel = mid;
	if( confirm( 'Czy jesteś pewien, że chcesz skasować tą wiadomość ?' ) ) {
		ajax_delMessages( msgToDel );
	}
}

function delMessages(){
	msgToDel = msgToDel.replace( '||', '|' );
	ajax_delMessages( msgToDel );
}

function addMeToDel( tid ){
	tid = tid.replace( 'message_', '' );
	if( msgToDel.indexOf('|'+tid+'|') == -1 ){
		msgToDel = msgToDel+'|'+tid+'|';
	}
	//alert( msgToDel );
}

function clearMe( tid ){
	tid = tid.replace( 'message_', '' );
	var t = '|'+tid+'|';
	if( msgToDel.indexOf( t ) > -1 ){
		msgToDel = msgToDel.replace( t, '' );
	}
	//alert( msgToDel );
}

function ajax_delMessages( arrays ){
	arrays = arrays+'';
	var kilka = false;
	if( arrays.indexOf('|') > -1 ){ kilka = true; }
	var ajax	= new myAjax();
	ajax.action = 'delMessages';
	ajax.post( 'msgs=' + arrays );
	ajax.onLoad = function() {
//		alert( this.response );
		if( this.response == 'ok' ) {
//			alert( 'Wiadomość usunięta.' );
			if( kilka )
				var loc = window.location+'';
			else
				var loc = '/profil/skrzynka';
			window.location.reload();
		} else {
			alert( this.response );
		}
	}
}

function tryRemoveWorkshop( id ) {
	var ajax = new myAjax();
	ajax.action = 'tryRemoveWorkshop';
	ajax.post( 'id='+id );
	ajax.onLoad = function() {
		alert( this.response );
		document.location.reload();
	}
}

function cantCitePost() {
	alert( 'Aby dodać post musisz dołączyć do grupy' );
}

function tryCitePost( id ) {
	var ajax = new myAjax();
	ajax.action = 'tryCitePost';
	ajax.post( 'id='+id );
	ajax.onLoad = function() {
		document.location.href = '#odpowiedz';
		$( '#post_response' ).text( this.response );
	}
}

function reloadWMC( city, href ) {
	if( href == undefined ) href = getHrefString();
	var c = getValue( city );
	var ajax = new myAjax();
	ajax.action = 'reloadWorkshopMenuCity';
	ajax.post( 'c='+c+'&href='+href );
	ajax.onLoad = function() {
//		alert( this.response );
		document.location.href = this.response;
	}
}

function reloadWM( prov, href ) {
	if( href == undefined ) href = getHrefString();
	var p = getValue( prov );
	var ajax = new myAjax();
	ajax.action = 'reloadWorkshopMenu';
	ajax.post( 'p='+p+'&href='+href );
	ajax.onLoad = function() {
//		alert( this.response );
		document.location.href = this.response;

		//alert( this.response );
	}
}

function getHrefString() {
	var loc = document.location.href.toString();
	loc = loc.slice( loc.indexOf( 'warsztat' ) );
	if( loc.substr( -1 ) != '/' ) loc += '/';
	loc = '/'+loc;
	while( loc.substr( -2 ) == '//' ) {loc = loc.substr( 0, loc.length - 1 );}
//	alert( loc );
	return loc;
}

function tryEditGroup() {
	var t = getValue( 'title' );
	var ot = getValue( 'oldTitle' );
	if( t == ot ) {
		document.forms[ 'editGroupForm' ].submit();
	} else {
		var ajax = new myAjax();
		ajax.action = 'tryAddGroup';
		ajax.post( 't='+t );
		ajax.onLoad = function() {
			switch( this.response ) {
				case 'ok':
					document.forms[ 'editGroupForm' ].submit();					
					break;
				case 'group_exists':
					alert( 'Już istnieje taka grupa, musisz wybrać inną nazwę dla grupy' );
					break;
			}
		}
	}
}

function tryRemoveTicket( id ) {
	if( confirm( 'Czy jesteś pewien, że chcesz usunąć ten mandat' ) ) {
		var ajax = new myAjax();
		ajax.action = 'tryRemoveTicket';
		ajax.post( 'id='+id );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				document.location.href = '/profil/mandaty/';
			} else {
				alert( this.response );
			}
		}
	}
}

function tryRemoveCarCard( id ) {
	if( confirm( 'Czy jesteś pewien, że chcesz usunąć tą autokartę' ) ) {
		document.location.href = '/profil/usun-autokarte/'+id;
	}
}

function getFavoritesGallery( id ) {
	var ajax = new myAjax();
	ajax.action = 'getFavoritesGallery';
	ajax.post( 'client='+id, 'gals_fav' );
	ajax.onLoad = function() {
		//hide( 'pokoz_wyniki3' );
	}
}

function trySendNoteWorkshop( obj, id, note ) {
	var ajax = new myAjax();
	ajax.action = 'trySendNoteWorkshop';
	ajax.post( 'obj='+obj+'&id='+id+'&note='+note );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			toggleWorkshopNote( id );
		}
	}
}

function trySendNote( obj, id, note ) {
	var ajax = new myAjax();
	ajax.action = 'trySendNote';
	ajax.post( 'obj='+obj+'&id='+id+'&note='+note, 'noteResponse' );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			window.location.reload();
		}
	}
}

function tryAddNote( note ) {
	var ajax = new myAjax();
	ajax.action = 'tryAddNote';
	var pc = getValue( 'note_class' );
	var pid = getValue( 'note_id' );
	ajax.post( 'pc='+pc+'&pid='+pid+'&note='+note );
	ajax.onLoad = function() {
		alert( this.response );
	}
}

function responseComment( tit ) {
	get( 'comment_title' ).value = 'Re: '+tit;
}

function tryAddComment() {
	var ajax = new myAjax();
	ajax.action = 'tryAddComment';
	var pc = getValue( 'comment_class' );
	var pid = getValue( 'comment_id' );
	var tit = getValue( 'comment_title' );
	var n = getValue( 'comment_nick' );
	var tex = getValue( 'titx' );
	ajax.post( 'pc='+pc+'&pid='+pid+'&title='+tit+'&n='+n+'&tex='+tex );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			window.location.reload();
		} else {
			alert( this.response );
		}
	}
}

function tryResponsePost( id ) {
	var b = getValue( 'post_response' );
	b = b.split( '&' ).join( '___AND___' );
	var ajax = new myAjax();
	hide( 'response' );
	show( 'message' );
	ajax.action = 'tryAddResponsePost';
	ajax.post( 'id='+id+'&b='+b );
	ajax.onLoad = function() {
		var loc = window.location;
		var resp = this.response;
		var pn = resp.substr( 0, resp.indexOf( '#' ) );
		if( loc.pathname == pn ) {
			loc.reload();
		} else {
			loc.href = resp;
		}
	}
}

function switchDocsPerPage( dpp ) {
	var ajax = new myAjax();
	ajax.action = 'switchDocsPerPage';
	ajax.post( 'p='+dpp+'&href='+document.location.href );
	ajax.onLoad = function() {
	//	alert( this.response );
		document.location.href = this.response;
	}
}

function tryAddResponsePost() {
	var b = getValue( 'body' );
	var id = getValue( 'threadId' );
	var ajax = new myAjax();
	ajax.action = 'tryAddResponsePost';
	ajax.post( 'b='+b+'&id='+id );
	ajax.onLoad = function() {
		switch( this.response ) {
			case 'ok':
				window.location.reload();
				break;
			default:
				get( 'errorMessage' ).innerHTML = this.response;
				break;
		}
	}
}

function tryAddPost() {
	var t = getValue( 'title' );
	var b = getValue( 'body' );
	var ajax = new myAjax();
	ajax.action = 'tryAddPost';
	ajax.post( 't='+t+'&b='+b );
	ajax.onLoad = function() {
		switch( this.response ) {
			case 'ok':
				document.forms[ 'addPostForm' ].submit();
				break;
			default:
				get( 'errorMessage' ).innerHTML = this.response;
				break;
		}
	}
}

function tryAddGroup() {
	var t = getValue( 'title' );
	var ajax = new myAjax();
	ajax.action = 'tryAddGroup';
	ajax.post( 't='+t );
	ajax.onLoad = function() {
		switch( this.response ) {
			case 'ok':
				document.forms[ 'form_1' ].submit();
				break;
			case 'no_title':
				alert( 'Musisz podać nazwę grupy' );
				break;
			case 'group_exists':
				alert( 'Już istnieje taka grupa, musisz wybrać inną nazwę dla grupy' );
				break;
		}
	}
}

function sendActivation( id ) {
    var ajax = new myAjax();
    ajax.action = 'sendActivation';
    ajax.post( 'id='+id, 'activationButton' );
}

function tryRegister() {
	var h		= get( 'hashcode' );	
	var l		= getValue( 'nick' );
	var p		= getValue( 'password' );
	var pr	= getValue( 'password_repeat' );
	var e 	= getValue( 'email' );
	var er	= getValue( 'email_repeat' );
	var r		= get( 'regulamin' );
	h.value = 'karta';
	hide( 'rejestracja' );

	var ajax = new myAjax();
	ajax.action = 'tryRegister';
	ajax.post( 'l='+l+'&p='+p+'&pr='+pr+'&e='+e+'&er='+er+'&hashcode='+h.value+'&regulamin='+r.checked, 'register_message' );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			hide( 'rejestracja' );
			show( 'register_end' );
			document.location.href = '/rejestracja-pomyslna/';
//			document.getElementById( 'register_end' ).innerHTML = 'Dziękujemy za rejestrację w serwisie, aktywuj swoje konto klikając link w mailu aktywacyjnym';
		} else {
			show( 'rejestracja' );
		}
	}
}
						 
function try_contact() {
	var h		= get( 'hashcode' );	
	var e 	= getValue( 'email' );
	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 + '&hashcode='+h.value, 'contact_message' );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				show( 'contact_message_send' );
			} else {
				show( 'div_contact' );
			}
		}
	}
}

function pollSave( id ) {
	id = id.toString();
	var poll = document.forms[ 'poll_' + id ];
	var bradio = poll.elements[ 'question_' + id ];
	var ret = 'poll_questions_' + id;
	var q = getRadioValue( bradio );
	var reg = new RegExp( '[0-9]+' );
	if( !q.match( reg ) ) {
		alert( 'Proszę wybrać którąś z odpowiedzi' );
	} else {
		hide( ret );
		var ajax = new myAjax();
		ajax.action = 'pollSave';
		ajax.post( 'poll='+id+'&answer=' + q, ret );
		ajax.onLoad = function() {
			show( ret );
		}
	}
}


function submitForm( f ) {
	get( f ).submit();
}

