/*
AmagaviHTML.js
Javascript functions to deal with creating HTML strings

Written by Marco A. Gonzalez
Amagavi, Inc.
Copyright (c) 2009 Amagavi, Inc.

*/


// replace newlines with <br />
function asHTML(theText){
	/*
		// the following didn't work, and I'm not sure why
		theText = escape(text);			// makes it platform safe... don't know if needed here
		if(theText.indexOf('%0D%0A') > -1){
			regEx_Newline = new RegExp("%0D%0A", "g");					// %0D%0A/g ;
			
		}else if(theText.indexOf('%0A') > -1){
			regEx_Newline = new RegExp("%0A", "g") ;			//  /%0A/g
			
		}else if(theText.indexOf('%0D') > -1){
			regEx_Newline = new RegExp('%0D', 'g');		// /%0D/g
		}
		return unescape(theText.replace(regEx_Newline,'<br />'));
	*/
	newLine = "\n";
	myRegEx = new RegExp(newLine, "g");
	return theText.replace(myRegEx, "<br />");
}




function asHTMLContainer(tagName, attribute, content){
	return asHTMLOpenTag(tagName, attribute) + content + asHTMLCloseTag(tagName);
}


// asHTMLAttribute("class", "test") returns
// class="test"
// See asHTMLOpenTag()
// prefix and suffix spaces allow for concatenating attribute calls
// value must be non-null and have string length > 0, or function result will be 
// the empty string
function asHTMLAttribute(name, value){
	attribute = "";
	if (value != undefined){
		attribute = " " + name + '="' + value + '"' + " ";
	}
	return attribute;
}

// asHTMLOpenTag("tr", asHTMLAttribute("style", "text-align: center;"))
// returns
// <tr style="text-align: center;">
// handles attribute==null correctly by returning <tr>
function asHTMLOpenTag(tagName, attribute){
	if (attribute != undefined && attribute.length > 0){
		result = "<"+tagName+" " +attribute+">";
	}else{
		result = "<"+tagName+">";
	}
	return result;
}



// asHTMLCloseTag("table")
// returns
// </table>
function asHTMLCloseTag(tagName){
	return "</"+tagName+">";
}

function asHTMLComment(theText){
	return "<!-- " + theText + " -->";
}


// split on newlines, and an HTML list with each line delimited
// by the newline as a list item (<li>)
// if content is undefined or empty string, no line will be output
// split can return the empty string if delimiter is at the end of a line
function asHTMLList(theText, listAttribute, isOrdered){
	if (isOrdered == undefined){
		isOrdered = false;
	}
	
	if (isOrdered){
		tagName = "ol";
	}else{
		tagName = "ul";
	}
	html = "";
	html += asHTMLOpenTag(tagName, listAttribute);
	
	if (theText.indexOf("\n") != -1){
		lines = theText.split("\n");
		for(i=0; i< lines.length; i++){
			if (lines[i].length > 0){
				html += asHTMLContainer("li", "", lines[i]);
			}
		}
	} else {
		html += asHTMLContainer("li", "", theText);
	}
	
	html += asHTMLCloseTag(tagName);
	return html;
}

function asHTMLOrderedList(theText, listAttribute){
	isOrdered = true;
	return asHTMLList(theText, listAttribute, isOrdered);
}

function asHTMLUnorderedList(theText, listAttribute){
	isOrdered = false;
	return asHTMLList(theText, listAttribute, isOrdered);
}

// not tested
// match returns null if pattern is not found
function stringEndsWith(s, endsWithTest){
	found = false;			// safe default
	reForEndsWith = new RegEx(endsWithTest, "$");
	matchedValue = s.match(reForEndsWith);
	if (matchedValue != null && matchedValue != ""){
		found = true;
	}
	return found;
}