/*
2006-08-07
===
-Allows you to programatically add attributes to all existing elements in a given HTML page
-Not spiderable, as far as I can tell, but this is more for end-user stylization
-Keeps keyword density modest and should avoid any spam issues with alt and title tags (since it's presumably not spiderable)

USAGE:
window_onload = function() {
	add_attribute('img,a','title,alt','Some slogan here','-');	
}
*/
function add_attribute(str_tags, str_attr, str_to_insert, str_spacer_char,str_skip_class) {
	// Check to see if the function variables passed are valid
	// 'str_spacer_char' can be left null
	if(str_skip_class == undefined) { str_skip_class = ''; }
	if(str_tags == null || str_attr == null || str_to_insert == null) { return false; }
	var tags, attr, elements, skip;
	// Split passed strings into arrays
	tags = str_tags.split(',');
	attr = str_attr.split(',');
	skip = str_skip_class.split(',');
	// Clean up tags and attr arrays
	for(var i in tags) { tags[i] = strip_whitespaces(tags[i]); }
	for(var i in attr) { attr[i] = strip_whitespaces(attr[i]); }
	for(var i in skip) { skip[i] = strip_whitespaces(skip[i]); }
	// Search HTML elements/tags for attributes
	for(var i = 0; i < tags.length; i++) {
		elements = document.getElementsByTagName(tags[i]);
		// With each element...
		for(var q = 0; q < elements.length; q++) {
			for(var d = 0; d < attr.length; d++) {
				var hit = 0;
				// See if this element is part of ANY of the 'skip' array classes
				for(var w = 0; w < skip.length; w++) { if(elements[q].getAttribute('class') == skip[w]) { hit++; } }
				// Search for the current attribute
				if(elements[q].getAttribute(attr[d]) !== null && elements[q].getAttribute(attr[d]).length > 0 && hit == 0) {
					// If there's a pre-existing value, append 'str_to_insert' to the end (plus a space)
					var tmp_str = elements[q].getAttribute(attr[d]);
					tmp_str += (' ' + str_spacer_char + ' ' + str_to_insert);
					elements[q].setAttribute(attr[d], tmp_str);
				} else {
					// else add the attribute and make 'str_to_insert' its value
					elements[q].setAttribute(attr[d], str_to_insert);
				}
			}
		}
	}
	return true;
}
function strip_whitespaces(my_str) {
	var arr; var new_str = '';
	if(my_str.indexOf(' ') !== -1) {
		arr = my_str.split(' ');
		for(var i in arr) { new_str += arr[i]; }
		return new_str;
	} else {
		return my_str;
	}
}
