function trySendQuestion( id ) {
	var subj = getValue( 'd_subject_' + id.toString() );
	var quest = getValue( 'd_question_' + id.toString() );
	var f = 'question_form_' + id.toString();
	var fr = f + '_response';
	var ajax = new myAjax();
	ajax.action = 'trySendQuestion';
	hide( f );
	ajax.post( 'subject='+subj+'&question='+quest+'&parent='+id.toString() );
	ajax.onLoad = function() {
		var ar = this.response.split( '|' );
		if( ar[ 0 ] == 'error' ) {
			show( f );
			putHtml( fr, ar[ 1 ] );
		} else {
			putHtml( fr, ar[ 0 ] );
		}		
	}
}

function toggleCities( province, cities ) {
	if( typeof( province ) != 'string' ) province = 'province';
	if( typeof( cities ) != 'string' ) cities = 'cities';
	var ajax = new myAjax();
	ajax.action = 'toggleCities';
	ajax.post( 'province='+getValue( province ), cities );
}

function submitPoll( id ) {
	var s = getRadioValueByName( 'sonda_'+id );
	var ajax = new myAjax();
	ajax.action = 'submitPoll';
	ajax.post( 'id='+id+'&value='+s, 'sonda' );
}

function toggleGroups( id, g ) {
	var ajax = new myAjax();
	ajax.action = 'toggleGroups';
	if( g != undefined ) {
		ajax.post( 'category='+id+'&g='+g, 'groups' );
	} else {
		ajax.post( 'category='+id, 'groups' );		
	}
	ajax.onLoad = function() {
		reloadLeftLinksGroups();
	}
}

function trySendNote( obj, id, note ) {
	var ajax = new myAjax();
	ajax.action = 'trySendNote';
	ajax.post( 'obj='+obj+'&id='+id+'&note='+note );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			window.location.reload();
		} else {
			alert( this.response );
		}
	}
}

function tryAddComment() {
	var ajax = new myAjax();
	ajax.action = 'tryAddComment';
	var pc = getValue( 'comment_class' );
	var pid = getValue( 'comment_id' );
	var tex = getValue( 'titx' );
	ajax.post( 'pc='+pc+'&pid='+pid+'&tex='+tex );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			window.location.reload();
		} else {
			alert( this.response );
		}
	}
}

function tryResponsePost( id ) {
	var b = getValue( 'post_response' );
	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 findFriend() {
	var f = getValue( 'friend' );
	var ajax = new myAjax();
	ajax.action = 'findFriend';
	ajax.post( 'f='+f, 'responses' );
	ajax.onLoad = function() {
		
	}
}

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 'group_exists':
				alert( 'Już istnieje taka grupa, musisz wybrać inną nazwę dla grupy' );
				break;
		}
	}
}

function tryRegister( script ) {
	var h		= get( 'hashcode' );	
	var l		= getValue( 'login' );
	var p		= getValue( 'password' );
	var pr	= getValue( 'password_repeat' );
	var e 	= getValue( 'email' );	
	var n 	= getValue( 'name' );
	var sn	= getValue( 'surname' );
	var r		= get( 'regulamin' );
	h.value = 'latajacepsy';
	hide( 'rejestracja' );
	var ajax = new myAjax();
	ajax.action = 'tryRegister';
	ajax.post( 'l='+l+'&p='+p+'&pr='+pr+'&e='+e+'&n='+n+'&sn='+sn+'&hashcode='+h.value+'&regulamin='+r.checked, 'register_message' );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			document.location.href = script;			
		} 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();
}

function validateDateSubmit( date_submit ) {
   var reg = /^[0-9]{2}-[0-9]{2}-[0-9]{4}$/;
   if( !reg.test( date_submit )) {
      return false;
   } 
    return true;
}


function validateFormConference() {
   
        var text='';
        var date_submit = $.trim($("#date").val());
        var city = $.trim($("#city").val());
        var main_name = $.trim($("#main_name").val());
        var main_surname = $.trim($("#main_surname").val());
        var main_nip = $.trim($("#main_nip").val());
        var main_phone = $.trim($("#main_phone").val());
        var main_mail = $.trim($("#main_mail").val());
        
        var name = new Array();
        var surname = new Array();
        var job = new Array();
        var phone = new Array();
        var sightseeing = $('#agree_visit').is(':checked');
        if (sightseeing === false) {sightseeing = 'nie';}
        else {sightseeing = 'tak';}
        
        var emptyRow = 0;
        
        
        $('#conf-person-table').find('tr.clientRow').each(function(){
            var inputs = $(this).find('td input');
            var c_err = 0;
            $(inputs).each(function(){
               if ($(this).val() == '') c_err++;
            });
            
            if(c_err==4) {
                $(this).remove();
            }
            
            if(c_err > 0 && c_err!=4) {
                $(inputs).each(function(){
                    if ($(this).val() == ''){
                        emptyRow += 1;
                    }
                });
            }
            if(c_err ==  0){
                    $(this).find('td:eq(0) input').each(function(){
                       name += $(this).val(); name += '|'; 
                    });
                    $(this).find('td:eq(1) input').each(function(){
                       surname += $(this).val(); surname += '|'; 
                    });
                    $(this).find('td:eq(2) input').each(function(){
                       job += $(this).val(); job += '|'; 
                    });
                    $(this).find('td:eq(3) input').each(function(){
                       phone += $(this).val(); phone += '|'; 
                    });
            }
        });
        
        if (emptyRow > 0){
           text+="Nie podano wszystkich danych dotyczacych uczestnika konferencji.\n";  
        }
        
        if ($('#conf-person-table').find('tr.clientRow').length == 0){
            $('#conf-person-table')
            .append('<tr class="clientRow"><td><div class="input-medium"><input type="text" name="name1" id="name1" value="" /></div></td><td><div class="input-medium"><input type="text" name="surname1" id="surname1" value="" /></div> </td><td><div class="input-medium"><input type="text" name="job1" id="job1" value="" /></div>  </td><td><div class="input-medium"><input type="text" name="phone1" id="phone1" value="" /></div>  </td><td class="less-width"><span class="blue">795 zł</span></td>');  
            text+="Nie podano żadnego uczestnika.\n"; 
        }
        
        
        
        
        if( date_submit == '' || date_submit == 'dd-mm-rrrr'){
            text+="Nie podano daty.\n";
        }else if( !validateDateSubmit(date_submit) ){
           text+="Proszę podać poprawny format daty.\n"; 
        }      
        if(city == '')
                text+="Nie podano miejscowości.\n";
        if( main_name == '')
                text+="Nie podano imienia.\n";
        if( main_surname == '')
                text+="Nie podano nazwiska.\n";
        if( main_nip == '')
                text+="Nie podano numeru nip.\n";
        if( main_phone == '')
                text+="Nie podano telefonu.\n";
        if(main_mail == '')
                text+="Nie podano adresu e-mail.\n";
 
        if(text!='') {
                alert(text);
                return false;
        }
        var ajax	= new myAjax();
        ajax.action = 'try_conference_apply';
		ajax.post( 'date_submit=' + date_submit + '&city=' + city + '&main_name=' + main_name + '&main_surname=' + main_surname + '&main_nip=' + main_nip + '&main_phone=' + main_phone + '&main_mail=' + main_mail + '&name=' + name + '&surname=' + surname + '&job=' + job + '&phone=' + phone + '&sightseeing=' + sightseeing);
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
                            alert('Zgłoszenie zostało wysłane.');
                            $("#date").val('');
                            $("#city").val('');
                            $("#main_name").val('');
                            $("#main_surname").val('')
                            $("#main_nip").val('');
                            $("#main_phone").val('');
                            $("#main_mail").val('');
                            $('#conf-person-table tr.clientRow td input').each(function(){
                                $(this).attr('value','');
                            });
                            $('#agree_visit').attr('checked','');
			} else {
                            alert(this.response);
			}
		}
        
        
        return false;
}
