// Author:  Magne Myrtveit, 2002
//

// Color helpers
function RgbStr(num) {
	var Res = Number(num).toString(16);
	while(Res.length < 6)
		Res = "0" + Res;
	return Res;
}

function MaxRgb(num) {
	var B = num & 0xff;
	num >>>= 8;
	var G = num & 0xff;
	num >>>= 8;
	var R = num & 0xff;
	var Max =
		R>=G ?
			(R>=B ? R : B) :
			(G>=B ? G : B);
	return Max;
}


function ContrastRgb(num) {
	return MaxRgb(num) > 127 ? 0x000000 : 0xffffff;
}


// Object helpers

function IsDefined(obj)
/* Purpose:    Is 'obj' defined?
   Note:       IE5.0 does not accept undefined as a reserved word.
					For NN obj != null is false if obj is undefined.
*/ {
	//return obj != null;
	//return !((obj == null) || (obj == undefined));
	return (typeof obj != "undefined") && (obj != null);
}


function IsUndefined(obj) {
	//return obj == null;
	//return (obj == null) || (obj == undefined);
	return (typeof obj == "undefined") || (obj == null);
}


// String helpers

function EmptyStr(S) {
// Purpose: See if string is empty
	return IsUndefined(S) || S == "";
}


function Capitalize(S) {
/* Purpose: Make initial letter upper case
	In:      S - string (HTML-format, i.e., with æ, ø, å mapped to &-sequences
	Return:  S with first letter capitalized.
	History	When     Who   What
				20020410 magmy Added test for &...;... in order to deal with &oslash; and similar characters
*/
	if((S.length >= 3) && (S.substr(1) == "&")) {
		return "&" + S.CharAt(1).toUpperCase() + S.substr(2);
	} else {
		return S.substr(0,1).toUpperCase() + S.substr(1);
	}
}


var HtmlCharMap = {
	'æ' : 'aelig',
	'ø' : 'oslash',
	'å' : 'aring',
	'à' : 'agrave',
	'á' : 'aacute',
	'è' : 'egrave',
	'é' : 'eacute',
	'Æ' : 'AElig',
	'Ø' : 'Oslash',
	'Å' : 'Aring',
	'À' : 'Agrave',
	'Á' : 'Aacute',
	'È' : 'Egrave',
	'É' : 'Eacute',
	'«' : 'laquo',
	'»' : 'raquo',
	'&' : 'amp',
	'>' : 'gt',
	'<' : 'lt',
	'"' : 'quot',
	'´' : 'acute',
	'©' : 'copy',
	'°' : 'deg',
	'·' : 'middot',
	' ' : 'nbsp' // magmy 20020410 Added #160 - non-breaking space
};


function HtmlString(Str) {
// Map æ.ø.å etc.
	var Res = "";
	if(typeof Str == "string") {
		var Repl;
		for(var i = 0; i < Str.length; ++i) {
			Repl = HtmlCharMap[Str.charAt(i)];
			if(IsDefined(Repl))
				Res += '&' + Repl + ';';
			else
				Res += Str.charAt(i);
		} // end for
	} else {
		Res += Str;
	}
	return Res;
} // end function


function UrlSafeString(Namn, Path, Ext) {
	var URL = "";
	var Len = Namn.length;
	Namn = Namn.toLowerCase();
	for(var Idx = 0; Idx < Len; Idx++) {
		var C = Namn.charAt(Idx);
		if(C == 'æ') {
			URL += 'ae';
		} else if(C == 'ø') {
			URL += 'oe';
		} else if(C == 'å') {
			URL += 'aa';
		} else if(C == ' ') {
			URL += '_';
		} else if(C == '-') {
			URL += '_';
		} else if(C == '.') {
			;
		} else {
			URL += C;
		}
	} // end for

	return Path + URL + Ext;

} // end function



function DayAndMonth(Dag) {

	if(IsUndefined(Dag))

		alert("DayAndMonth(null)");

	return Dag.getDate() + ". " + MonthName[Dag.getMonth()];

}



function WeekdayDayAndMonth(Dag) {

	if(IsUndefined(Dag))

		alert("WeekdayDayAndMonth(null)");

	return DayName[Dag.getDay()] + " " + Dag.getDate() + ". " + MonthName[Dag.getMonth()];

}



function CellStr(S) {

	return EmptyStr(S) ? "&nbsp;" : S;

}



// Month names

var MonthName = ["januar", "februar", "mars",

	"april", "mai", "juni",

	"juli", "august", "september",

	"oktober", "november", "desember"];



// Day names

var DayName = ["søndag", "mandag", "tirsdag",

	"onsdag", "torsdag", "fredag", "laurdag"];



// HTML-writer

function HtmlWriter() {

	this.IndentLevel = 0;

	this.TagStack = [];

	this.PosStartContents = 0; // Position after closing > of <tag ...>.

	this.Output = "";

	this.Indent = HtmlIndent;

	this.StartTag = HtmlStartTag;

	this.EndTag = HtmlEndTag;

	this.EndImplicitTag = HtmlEndImplicitTag;

	this.ContainerElement = HtmlContainerElement;

	this.SingleElement = HtmlSingleElement;

	this.Text = HtmlText;

	this.RawText = HtmlRawText;

	this.Flush = HtmlFlush;

}



function HtmlIndent() {

// Purpose: Add intentation to current level

	for(var i = 1; i <= this.IndentLevel; ++i)

		this.Output += "\t";

}



function HtmlText(Text) {

// Purpose: Add text to HTML document

	this.Output += HtmlString(Text);

}





function HtmlRawText(Text) {

// Purpose: Add text to HTML document

	this.Output += Text;

}





function HtmlStartTag(Name, Parameters) {

// Purpose: Start a tag

// In:      Name - Name of tag

//          Parameters - Associative array with key/value pairs

// Example: HtmlStartTag("TABLE", {border:"0"});



	//this.TagStack.push(Name);

	this.TagStack[this.TagStack.length] = Name;

	this.Indent();

	++this.IndentLevel;

	this.Output += "<" + Name;

	if(IsDefined(Parameters)) {

		for(var p in Parameters) {

			this.Output += " " + p;

			if(Parameters[p] != null)

				this.Output += "='" + HtmlString(Parameters[p]) + "'";

		}

	}

	this.Output += ">\n";

	this.PosStartContents = this.Output.length;

}



function HtmlEndTag() {

// Purpose: End tag started by 'HtmlStartTag'.

// Note:    Will replace empty contents of table cell with nbsp.

//			   <td ...></td> => <td...>&nbsp;</td>

	--this.IndentLevel;

//	var Name = this.TagStack.pop();

	var Name = this.TagStack[this.TagStack.length-1];

	this.TagStack.length--;

	if((this.PosStartContents == this.Output.length) && (Name.toLowerCase() == "td"))

		this.Output += "&nbsp;"; // Empty cell

	this.Indent();

	this.Output += "</" + Name + ">\n";

}



function HtmlEndImplicitTag() {

// Purpose: End implicit tag started with 'HtmlStartTag'

// Use:		Use with tags that do not require end tag, such as <P> and <LI>.

	--this.IndentLevel;

//	this.TagStack.pop();

	this.TagStack.length--;

	this.Indent();

	this.Output += "\n";

}



function HtmlContainerElement(Name, Contents, Parameters) {

// Purpose: Create tag contents and end-tag.

// Example: HtmlContainer("P", "Tekst");

	this.Indent();

	this.Output += "<" + Name;

	if(IsDefined(Parameters)) {

		for(var p in Parameters) {

			this.Output += " " + p;

			if(Parameters[p] != null)

				this.Output += "='" + HtmlString(Parameters[p]) + "'";

		} // end for

	} // end if

	this.Output += ">";

	if(IsUndefined(Contents))

		Contents = "";

	if((Contents == "") && (Name.toLowerCase() == "td"))

		this.Output += "&nbsp;";

	else

		this.Output += HtmlString(Contents);

	this.Output += "</" + Name + ">\n";

} // end function





function HtmlSingleElement(Name, Parameters) {

// Purpose: Create element without end-tag.

// Example: HtmlSingleElement("IMG", {src:"bilde.jpg;"});

	this.Indent();

	this.Output += "<" + Name;

	if(IsDefined(Parameters))

		for(var p in Parameters)

			this.Output += " " + p + "='" + HtmlString(Parameters[p]) + "'";

	this.Output += ">\n";

} // end function





function HtmlFlush() {

// Purpose: Send HTML-text to document.

	document.write(this.Output);

	this.Output = "";

}


