/**
* Add, remove and test (for) classnames from (HTML) DOM-objects Objects known by there identifier.
* The functions test if is o an object. If not, try get the element by id.
* @author: Jörg Reinholz, fastix WebDesign & Consult, Kassel - http://www.fastix.org/
* @version: 1.0.2
* code.fastix.org ist NOT a cdn and verry slow! Download this and use your OWN server to deliver this!
**/
function addClassName( o, cName ) {
o = getOrCheckObjekt( o );
if ( o && ( ! hasClassName( o, cName ) ) ) {
if ( o.className ) {
var s = o.className + ' ' + cName;
o.className = s.replace( /^ +/ ).replace( / +$/ );
} else {
o.className = cName;
}
return true;
}
return false;
}
function removeClassName( o, cName ) {
o = getOrCheckObjekt( o );
if ( o ) {
if( cName == o.className ) {
o.className = '';
return true;
} else {
var c = o.className;
var a = c.split( / +/ );
var aNew = new Array();
var m = 0;
for ( var i = 0; i < a.length; i++ ) {
if ( cName != a[i] ) {
aNew[++m] = a[i];
}
}
o.className = aNew.join(' ');
return true
}
}
return false;
}
function hasClassName( o, cName ) {
o = getOrCheckObjekt( o );
if ( o ) {
var c = o.className;
var a = c.split( / +/ );
for ( var i = 0; i < a.length; i++ ) {
if ( cName == a[i] ) {
return true;
}
}
}
return false;
}
function getOrCheckObjekt( o ) {
if ( ! 'object' == typeof( o ) ) {
o = document.getElementById( o );
}
if ( 'object' == typeof( o ) ) {
return o;
}
console.log( 'kein Objekt übergeben, "' + o + '" ist unbekannt"' );
return false;
}