/* usefull util functions */

function putHtml( id, str ) {
	var h = ( typeof( id ) == 'object' ) ? id : get( id );
	h.innerHTML = str;
}

function getSelected(opt) {
	var selected = new Array();
	var index = 0;
	for (var intLoop=0; intLoop < opt.length; intLoop++) {
		if ( opt[ intLoop ].selected ) {
			index = selected.length;
			selected[ index ] = new Object;
			selected[ index ].value = opt[ intLoop ].value;
			selected[ index ].index = intLoop;
	 }
  }
  return selected[ index ];
}

function addEvent(obj, type, fn){
	if (obj.addEventListener) {
		obj.addEventListener(type, fn, false);
	}
	else if (obj.attachEvent) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function() { obj['e'+type+fn](window.event); }
		obj.attachEvent('on'+type, obj[type+fn]);
	}
}

function checkNumber(e)
{
	var numbers = '0123456789- ';
	var key     = window.event ? window.event.keyCode : e ? e.which : null;
	var keychar = String.fromCharCode(key);
	return ( numbers.indexOf(keychar)!=-1 || key==null || key==0 || key==8 || key==9 || key==13 || key==27 );
}

function validateNumber(e){
	var numbers = new RegExp( "^[0-9]+$" );
	return numbers.test( e );
}

function clearInput( id ) {
	switch( id ) {
		case 'search_word':
			if( trim( getValue( id ) ) == 'Szukaj' ) {
				get( id ).value = '';
			}
			break;
		case 'n_email':
			if( trim( getValue( id ) ) == 'Twój adres e-mail' ) {
				get( id ).value = '';
			}
			break;
		case 'public_login':
			if( trim( getValue( id ) ) == 'login' ) {
				get( id ).value = '';
			}
			break;
		case 'public_password':
			if( trim( getValue( id ) ) == 'hasło' ) {
				//get( id ).type = 'password';
				get( id ).value = '';
			}
			break;
	}
}

function fillInput( id ) {
	switch( id ) {
		case 'search_word':
			if( trim( getValue( id ) ) == '' ) {
				get( id ).value = 'Szukaj';
			}
			break;
		case 'n_email':
			if( trim( getValue( id ) ) == '' ) {
				get( id ).value = 'Twój adres e-mail';
			}
			break;
		case 'public_login':
			if( trim( getValue( id ) ) == '' ) {
				get( id ).value = 'login';
			}
			break;
		case 'public_password':
			if( trim( getValue( id ) ) == '' ) {
				//get( id ).type = 'text';
				get( id ).value = 'hasło';
			}
			break;
	}
}


function get( id ) {
	return document.getElementById( id );
}

function getValue( str ) {
	var st = get( str );
	if( st != null ) {
		return trim( st.value );
	} else {
		return ' ';
	}
}


function swapHashCode() {
	var h		= get( 'hashcode' );
	h.value = 'foto';
}

function hide( str ) {
	var h = ( typeof( str ) == 'object' ) ? str : get( str );
	if( h != undefined ) {
		h.style.display = "none";
	} else {
//		alert( 'błąd (hide)' );
	}
}

function show( str ) {
	var h = ( typeof( str ) == 'object' ) ? str : get( str );
	if( h != undefined ) {
		h.style.display = "block";
	}
}

function changeVisible( str ) {
	var h = ( typeof( str ) == 'object' ) ? str : get( str );
	if( h != undefined ) {
		if( h.style.display == 'none' ) {
			h.style.display = 'block';
		} else {
			h.style.display = 'none';
		}
	} else {
		alert( 'błąd (changeVisible)' );
	}
}

function inArray( array, id ) {
	for( i in array ) {
			if( array[ i ]  == id ) {
					return true;
			}
	}
	return false;
}

function indexOfArray( array, id ) {
	for( i in array ) {
			if( array[ i ]  == id ) {
					return i;
			}
	}
	return -1;
}

function trimLeft( s ) {
	while( s.substring( 0, 1 ) == " " ){
		s = s.substr( 1 );
	}
	return s;
}

function trimRight( s ) {
	while( s.charAt( s.length - 1 ) == " " ) {
		s = s.substr( 0, s.length - 1 );
	}
	return s;
}

function trim( s ) {
	s = trimLeft( s );
	s = trimRight( s );
	return s;
}


function znaki(textar, liczba, div ) {
	if(liczba == '') { liczba = 350;}
	var ilosc = (liczba - textar.value.length);
	if(ilosc < 0) {
		textar.value = textar.value.substring(0,liczba);
		ilosc = 0;
	}
	if(div == '' || div == undefined ) { div ='znaczki';	}
	get( div ).innerHTML = ilosc;
}

function karta_skarbonka(element,element_text) {
	if(element.checked) {
		element_text.readonly = false;
	}
	if(element.checked == false) {
		element_text.readonly = true;
	}
}

function valid() {
	var form = document.getElementById('forma');
	var fake = 1;
	var faked = 0;
	var faking = new Array();
	for(i=0;i<form.length;i++) {
		if(form[i].tagName.toUpperCase() == 'INPUT' && form[i].name.substr(0,1) == 'v' && form[i].value == '1' ) {
			var qid = form[i].name.substring(1);
			faking[qid] = 1;
			for(ii=0;ii<form.length;ii++) {
				var re = new RegExp("(a[0-9]*)?_q"+qid+"$", "g");

				if(form[ii].name.match(re) != null ) {
					//alert(form[ii].type);
					switch(form[ii].type) {
						case 'radio':
							if (form[ii].checked == true) {
								faking[qid] = 0;
							}
							break;
						case 'checkbox':
							if (form[ii].checked == true) {
								faking[qid] = 0;
							}
							break;
						case 'text':
							if (form[ii].value != '') {
								faking[qid] = 0;
							}
							break;
					}
				}
			}
			if(faking[qid] == 1) {	faked++;	}
		}
	}
	if(faked > 0) {
		alert("Jedno z pytań jest wymagane.\nProszę wypełnić/zaznaczyć wymagane pola!")
		return false;
	}
	return true;
}

var redir = '';

function notPublicLogin() {
	get( 'public_login' ).value = getValue( 'notpublic_login' );
	get( 'public_password' ).value = getValue( 'notpublic_password' );
	get( 'public_password' ).type = 'password';
	publicLogin();
}
function publicLogin() {
	var l 	= getValue( 'public_login' );
	var p	= getValue( 'public_password' );

	get( 'pl_error' ).innerHTML = '';
	get( 'pl_error' ).style.display = 'none';

	var ajax = new myAjax();
	var pstr = 'login='+l+'&pass='+p+'&cn=Persons';
	ajax.action = 'publicLogin';
	
	ajax.post ( pstr );
	ajax.onLoad = function() {
		if( trim( this.response ) == 'ok' ) {
			publicForumLogin( l, p );
		}
		else {
			alert( this.response );
		}
		
	}
}

function publicForumLogin( l, p ) {
	var ajax = new myAjax();
	var posty = 'login=yes&username='+l+'&password='+p;
	ajax.uri = "/forum/ucp.php?mode=ajax_login";
	//ajax.action = '';
	ajax.post( posty );
	ajax.onLoad = function() {
		var ar = this.response;
		var ae = new Array();
		ae = ar.split( '|' );
		if( trim( ae[0] ) == 'ok' ){
			saveSession( ae[1] );
		}
		else {
			alert( 'Wpisano błędny login i/lub hasło.Spróbuj jeszcze raz' );
			ajax_logout();
		}
	}
}

function ajax_logout() {
	var ajax = new myAjax();
	ajax.uri = '/module/logout';
	ajax.post( '' );
	ajax.onLoad = function() {
		deleteSession();
	}
}


function saveSession( session ){
	if( session != undefined ){
		var ajax = new myAjax();
		ajax.action = 'saveSession';
		var posty = 'session='+session;
		ajax.post( posty );
		ajax.onLoad = function() {
			if( trim( this.response ) == 'ok' ){
				if( redir != '' && redir != undefined )
					var ol = redir;
				else
					var ol = window.location;

				window.location = ol;
			}
			else {
				alert( 'Błąd logowania.Odwież stronę i sprobuj ponownie' );
				ajax_logout();
			}
		}
		
	}
	else {
		alert( 'Błąd logowania.Odwież stronę i sprobuj ponownie' );
		ajax_logout();
	}
}

function deleteSession( ){
	var ajax = new myAjax();
	ajax.action = 'delSession';
	var posty = '';
	ajax.post( posty );
	ajax.onLoad = function() {
		if( trim( this.response ) == 'ok' ){
			var ol = window.location;
			window.location = ol;
		}
		else {
			alert( 'Błąd wylogowania.Odwież stronę i sprobuj ponownie' );
		}
	}
}

function upload_init( form_box, form_img, form ) {
	form = get( form );
	//dget( form_box ).style.display = "none";
	//dget( form_img ).style.display = "block";
	hide( form_box );
	show( form_img );
	form.submit();
}

function newsletter() {
	var a;
	if( get('na_zapisz').checked == true ) a = 'add';
	if( get('na_wypisz').checked == true ) a = 'del';
	var email = get( 'n_email' ).value;

	var ajax = new myAjax();
	ajax.action = a+'_newsletter';
	var posty = 'email='+email;
	ajax.post( posty );
	ajax.onLoad = function() {
		if( trim( this.response ) == 'ok' ){
			if( a == 'add' ) alert( 'Adres e-mail został dodany do listy odbiorców newslettera' );
			if( a == 'del' ) alert( 'Adres e-mail został wypisany z listy odbiorców newslettera' );
		}
		else {
			alert( this.response );
		}
	};
}

//sonda
function submit_sonda( id, type, count, form ) {
	hide( 'sonda_button' );
	var vals = '';
	var elem;

	if( type == 'radio' ) {
		var forma = get( form );
		var arr = new Array();
		arr = forma.radio_group;
		for( i=0; i<arr.length; i++ ){
			if( arr[i].checked ) {
				vals = arr[i].value;
				break;
			}
		}
	}
	if( type == 'checkbox' ) {
		for( var i=0; i<count; i++ ) {
			elem = get( type + i );
			if( elem.checked ) {
				vals += elem.value+"|";
			}
		}
	}

	if( vals != '' ){
		var ajax = new myAjax();
			ajax.action = 'probe_vote';
			ajax.onLoad = function() {
				var textA = this.response.split('|');
				if( textA[0] == 'ok' ) {
					var sonda = get( 'sonda' );
					sonda.innerHTML = textA[1];
				}
				else {
					alert( 'Wystapił problem w wysłaniu Twojego głosu!\nProsimy odśwież stronę i spróbuj ponownie.');
					show( 'sonda_button' );
				}
			}
			ajax.post( "doc_id="+id+"&type="+type+"&vals="+vals );
	}
	else {
		alert( 'Prosimy, zaznacz odpowiedź.');
		show( 'sonda_button' );
	}
}

//maps
function gotoMaps(){
	var lek = get( 'bmx_3_1' );
	var apt = get( 'bmx_3_2' );
	var tt = 'lekarz';
	if( lek.className == 'seld' )
		tt = 'lekarz';
	if( apt.className == 'seld' )
		tt = 'apteka';

	var link = '/znajdz/mapa/'+tt;
	window.location = link;
}

function drukuj(){
	var y='';
	var t=new String(document.location);
	if( t.charAt(t.length-1) !="/" )y="/";
	noweOkno = window.open(document.location+y+'action/print', 'okienko', 'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=yes, status=no, width=700, height=500');
}

function polec( link ){
	noweOkno = window.open( link , 'okienko', 'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=yes, status=no, width=700, height=500');
}

function przypomnij(){
	noweOkno = window.open( '/przypomnij-haslo/' , 'okienko', 'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=yes, status=no, width=700, height=300');
}

var cur = '';
function slideShow( id ) {
	$(".rule").hide(100);
	if(cur != id) {
		$("#r"+id).show(400);
		cur = id;
	}
	else cur = '';
}


