/* Javascript word wrap,
   Em Tonkin Aug 2006 */

function dowrap(mystring, mycutchars){
	// Here we do the actual word wrap
	// Set the NUMBER OF CHARS TO WORD WRAP HERE
	var howmanychars=35; // hard limit for wordwrap
	var margin=7; // look within this many chars of the word wrap
	if(mycutchars == undefined){
		var spacerchar="<br/>"; // you could also just make this " "; - cut by putting a space in
	} else {
		spacerchar=mycutchars;
	}
	var myreturnstring;
	var mycutlocation=howmanychars;
	var usecutchar=true;
	if(mystring.length>howmanychars){
		// try to cut about howmanychars off the beginning. Then call this function with the rest
		 
		// If we're going to be clever, let's see if we can find a preferred cutting character within n chars of the spacerchar
		// preferred cutting chars: "/" and "-"
		//if(mystring.charAt(howmanychars) == '/' || mystring.charAt(howmanychars) == '-'){

		// Find a / or a - within n chars, or just cut it straight.
		for(var count=howmanychars-margin;count<howmanychars;count++){
			if(mystring.charAt(count) == '/' || mystring.charAt(count)== '-'){
				mycutlocation= count;
				usecutchar=true;
			}
			if(mystring.charAt(count)==' ' || mystring.charAt(count)== ','){
				mycutlocation= count;
				usecutchar=false;
			}
		}
		if(usecutchar){	
			myreturnstring= mystring.substr(0, mycutlocation+1)+ spacerchar;
		} else {
			myreturnstring= mystring.substr(0, mycutlocation+1);

		}
		myreturnstring= myreturnstring + dowrap(mystring.substr(mycutlocation+1,mystring.length), spacerchar);

		
		
	} else {
		myreturnstring=mystring;
	}

	return myreturnstring;
}

function wordwrap(mytype,mystring, mycutchars){
	// mytype should be the tagname of the element you want to word wrap... (eg'span')
	// mystring should be the substring at the beginning of each id (eg urlspan)
	// search through for the element tagname we gave (mytype)
	if (document.getElementsByTagName)  // if we can't get elements by tagname, give up
		{
			var objAnchors = document.getElementsByTagName(mytype);
			for (var iCounter=0; iCounter<objAnchors.length; iCounter++) {
				var myid;
				myid=objAnchors[iCounter].id;
				if(myid.substr(0, mystring.length)==mystring){
					/* If that node isn't just a span or something, it might have child nodes */
					var mycontent = objAnchors[iCounter].innerHTML; // get current content 
					var mynewcontent=dowrap(mycontent, mycutchars); // Get new wrapped content
					objAnchors[iCounter].innerHTML=mynewcontent;	// Replace the original with the wrapped version
				}
			}
		}
	}