/* 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 clearLogin( id ) {
	switch( id ) {
		case 'public_login':
			if( trim( getValue( id ) ) == 'adres e-mail' ) {
				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 = 'adres e-mail';
			}
			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 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;
}

/*BKR - do eskok */
function showMyMenu( aidi ) {
	var id = "sub_"+aidi;
	var subm = get( id );
	subm.style.display = "block";
}

function hideMyMenu( aidi ) {
	var id = "sub_"+aidi;
	var subm = get( id );
	subm.style.display = "none";
}


function validateEmail( str ) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at) == -1)	{	return false;	}
	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at)  == lstr){	return false;	}

	if (str.indexOf(dot)== -1 || str.indexOf(dot)== 0 || str.indexOf(dot) == lstr){	return false;	}

	if (str.indexOf(at,(lat+1)) != -1) {	return false;	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){	return false;	}
	if (str.indexOf(dot,(lat+2))==-1)	{	return false;	}
	if (str.indexOf(" ")!=-1){	return false;	}

	return true;
}


