/* 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 clearField( id ) {
	get( id ).value = '';
}

function clearLogin( id ) {
	switch( id ) {
		case 'public_login':
			if( trim( getValue( id ) ) == 'login' ) {
				get( id ).value = '';
			}
			break;
		case 'public_password':
			if( trim( getValue( id ) ) == 'hasło' ) {
				get( id ).value = '';
			}
			break;
	}
}

function fillLogin( id ) {
	switch( id ) {
		case 'public_login':
			if( trim( getValue( id ) ) == '' ) {
				get( id ).value = 'login';
			}
			break;
		case 'public_password':
			if( trim( getValue( id ) ) == '' ) {
				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 getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


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 trimLeft( s ) {
	while( s.substring( 0, 1 ) == " " ){
		s = s.substr( 1 );
	}
	return s;
}

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 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) {
	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 ='znaczki';	}
	//document.getElementById(div).innerHTML = ilosc;
}

function getRadioValueByName( id ) {
	return getRadioValue( document.getElementsByName( id ) );
}

function getRadioValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function trace( obj ) {
	var str = '';
	for( i in obj ) {
		str += i+' = '+obj[ i ]+"<br/>\n";
	}
	document.write( str );
}

function getSelected(opt) {
	var selected = new Array();
	var index = 0;
	for ( 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;
}

function getSel( name ) {
	var sel = get( name );
	for( j = 0; j < sel.length; j++ ) {
		if( sel[ j ].selected ) {
			var obj = new Object();
			obj.value = sel.options[ j ].value;			
			obj.text = sel.options[ j ].innerHTML;
			return obj;
		}
	}
}


function getSelect( fn, selname ) {
	if( fn == '' ) {
		var sel = get( selname );
	} else {
		var sel = document[fn][selname];		
	}
	for( j = 0; j < sel.length; j++ ) {
		if( sel.options[ j ].selected ) {
			return sel.options[ j ].value;
		}
	}
}

function getDoc() {
		return document;
}

function trim( s ) {s = this.trimLeft( s );s = this.trimRight( s );return s;}
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 inArray( haystack, needle ) {for( i in haystack ) {var item = haystack[ i ];if( item == needle ) { return true;}}return false;}

function showFilter(){
	$( '#filtr_box' ).slideDown( '500' );
	$( '#filtr_link' ).html( 'Schowaj filtr' );

}
function hideFilter(){
	$( '#filtr_box' ).slideUp( '400' );
	$( '#filtr_link' ).html( 'Pokaż filtr' );
}


function showSelect( parent, clasa, box  ){
	var ajax = new myAjax();
	ajax.action = 'showSelect';
	ajax.post( 'classer=Medica&object='+clasa+'&parent_id='+parent );
	ajax.onLoad = function() {
		var resp = this.response + '';
		if( this.response != 'nok' ){
			get( box ).innerHTML = resp;
		}
		else {
			'Błąd w pobieraniu danych "ShowSelect"';
		}
	}
}

function intOnly( e ){
	floatOnly( e, '[\.\,\-]' );
}

function floatOnly(e,exception){
    var nowy='';
	var tekst = e.value;
	if( e ){
		for( var i=0; i<tekst.length; i++) {
			var z = tekst.charAt(i);
			if ( z!='1'&&z!='2'&&z!='3'&&z!='4'&&z!='5'&&z!='6'&&z!='7'&&z!='8'&&z!='9'&&z!='0'&&z!='.'&&z!=','&&z!='-') {nowy=nowy;} else {nowy=nowy+z;}
		}
		nowy = nowy.replace( ',', '.' );
		nowy = nowy.replace( '...', '.' );
		nowy = nowy.replace( '..', '.' );
		nowy = nowy.replace( '---', '-' );
		nowy = nowy.replace( '--', '-' );

		if( exception != undefined ){
			var regExp = new RegExp( exception );
			nowy = nowy.replace( regExp, '' );
		}

		e.value=nowy;
	}
}

function genMonthDays( month, year, select ){
	if( month == '' ) month = '01';
	
	var arr = new Array();
	arr[ '01' ] = 31;
	arr[ '02' ] = 28;
	arr[ '03' ] = 31;
	arr[ '04' ] = 30;
	arr[ '05' ] = 31;
	arr[ '06' ] = 30;
	arr[ '07' ] = 31;
	arr[ '08' ] = 31;
	arr[ '09' ] = 30;
	arr[ '10' ] = 31;
	arr[ '11' ] = 30;
	arr[ '12' ] = 31;

	if( year != '' && year%4 == 0 ) arr[ '02' ] = 29;

	var old = $( '#'+select ).val();
	var x = '00';
	for( var i = 1; i <= 31; i++ ){
		x = ( i < 10 )? '0'+i : ''+i;
		$( '#'+select+' option[value="'+x+'"]' ).attr( "disabled","" );
		$( '#'+select+' option[value="'+x+'"]' ).attr( "selected","" );
	}
	var st = arr[ month ];
	for( var j = st+1; j <= 31; j++ ){
		x = ( j < 10 )? '0'+j : ''+j;
		$( '#'+select+' option[value="'+x+'"]' ).attr( "disabled","disabled" );
	}
	if( parseInt( old ) > st ){
		x = ( st < 10 )? '0'+st : ''+st;
		$( '#'+select+' option[value="'+x+'"]' ).attr( "selected","selected" );
	}
	else{
		$( '#'+select+' option[value="'+old+'"]' ).attr( "selected","selected" );
	}

	//$("'theOptions' option[value='1']").attr("disabled","disabled");
}

function genDaysOpt(){
	var opt = '';
	for( var i = 1; i <= 31; i++ ){
		opt += '<option value="'+i+'">'+i+'</option>'+"\n";
	}
	return opt;
}
function genYearOpt(){
	var dat = new Date();
	var year = dat.getFullYear();
	var max_year = year - 100;
	var opt = '';
	for( var i = year; i >= max_year; i-- ){
		opt += '<option value="'+i+'">'+i+'</option>'+"\n";
	}
	return opt;
}

function genMonthsOpt() {
    return '<option value="01">styczeń</option>'
        +'<option value="02">luty</option>'
        +'<option value="03">marzec</option>'
        +'<option value="04">kwiecień</option>'
        +'<option value="05">maj</option>'
        +'<option value="06">czerwiec</option>'
        +'<option value="07">lipiec</option>'
        +'<option value="08">sierpień</option>'
        +'<option value="09">wrzesień</option>'
        +'<option value="10">październik</option>'
        +'<option value="11">listopad</option>'
        +'<option value="12">grudzień</option>';
}
