/*
	Javascript
	source code fully copyright by www.flemishdreams.com
	terms of usage:
	free to use on non-commercial sites; in that case a searchbot-readable link to www.flemishdreams,com would be appreciated; don't remove this copyright notice; don't email for support; code provided 'as is'
	commercial sites contact me at flemishdreams [atsymbol] yahoo [dotsymbol] com - also for design & functionality customization & support
	code last modified on January 13, 2007
-*/

// ____ strings _____________________________________________________________________________________

function parseInt_ (s) {
	if ((s == null) || (s.length < 1)) return 0;
	var i = parseInt(s,10);
	if (isNaN(i)) return 0
	else return i;
};

function str_replace (s, oldc, newc) {
	var sn = new String("");
	if (s.length>0) {
		for (var i=0; i<s.length; i++)
			if (s.charAt(i)==oldc) sn = sn + newc
			else sn = sn + s.charAt(i);
	};
	return sn;
};


// ____ HTML tags _____________________________________________________________________________________

function table_begin (width, cellpadding, cellspacing, classname, bordercolor) {
	var s = new String("");
	s =	'<table cellspacing="';
	if (cellspacing == null)	s = s + '0'
	else						s = s + cellspacing.toString();
	s =	s + '" cellpadding="';
	if (cellpadding == null)	s = s + '0"'
	else						s = s + cellpadding.toString() + '"';
	if (bordercolor == null)	s = s + ' border="0"'
	else						s = s + ' border="5" bordercolor="' + bordercolor.toString() + '"';
	if (width != null)			s = s + ' width="' + width.toString() + '"';
	if (classname != null)		s = s + ' class="' + classname + '"';
	s =	s + '><tbody><tr>';
	return s;
};

function table_end () {
	return '</tr></tbody></table>';
};

function tdc_begin (content, align, valign, width, classname, colspan, rowflagbefore, nowrap, extra) {
	var s = new String("");
	if (rowflagbefore != null)	s = '<tr>';
	s =	s + '<td valign="';
	if (valign == null)			s = s + 'middle" align="'
	else						s = s + valign + '" align="';
	if (align == null)			s = s + 'left"'
	else						s = s + align + '"';
	if (classname != null)		s = s + ' class="' + classname + '"';
	if (extra != null)			s = s + ' ' + extra;
	if (colspan != null)		s = s + ' colspan="' + colspan.toString() + '"';
	if (nowrap != null)			s = s + ' nowrap';
	if (width == null)			s = s + '>'
	else						s = s + ' width="' + width.toString() + '">';
	if (content != null)		s = s + content;
	return s;
};

function td_end (rowflagafter) {
	var s = new String("");
	s = '</td>';
	if (rowflagafter != null) s = s + '</tr>';
	return s;
};

function td_empty (alternatecontent, classname, extra) {
	if (classname != null)			var s = '<td class="' + classname + '"';
	else							var s = '<td';
	if (extra != null)				s = s + ' ' + extra;
	if (alternatecontent != null)	s = s + '>' + alternatecontent + '</td>';
	else							s = s + '>&nbsp;</td>';
	return s;
};

function table_newrow () {
	return '</tr><tr>';
};

function div_begin (id, extra) {
	if ((id == null) || (id.length < 1))	var s = '<div'
	else									var s = '<div id=' + id;
	if (extra != null)						s = s + ' ' + extra;
	return (s + '>');
};

function div_end () {
	return '</div>';
};

function extra_txt (name, style, title, click, id, change, mouseover, mouseout) {
	var s = new String("");
	if (style != null)		s = s + ' style="' + style + '"';
	if (title != null)		s = s + ' title="' + title + '"';
	if (name != null)		s = s + ' name="' + name + '"';
	if (id != null)			s = s + ' id="' + id + '"';
	if (change != null)		s = s + ' onchange="' + change + '"';
	if (mouseover != null)	s = s + ' onmouseover="' + mouseover + '"';
	if (mouseout != null)	s = s + ' onmouseout="' + mouseout + '"';
	if (click != null)		s = s + ' onclick="' + click + '"';
	return s;
};

function img (imgsrc, alt, extra) {
	if (alt==null)		{ var a = ''; }
	else				{ var a = alt + '" title="' + alt; };
	if (extra==null)	{ var e = ''; }
	else				{ var e = ' ' + extra; };
	if ((imgsrc!=null) && (imgsrc.length>0))	return '<img src="' + imgsrc + '" border="0" alt="' + a + '"' + e + '>'
	else										return '';
};

function js (code, title) {
	if ((title == null) || (title.length < 1))		{ var t = '' }
	else											{ var t = title };
	if ((code != null) && (code.length>0))			return '<a href="javascript:' + code + ';">' + t + '</a>'
	else											return t;
};

function lnkdf (href, target) {
// default target = _blank
	if (href != null) {
		if ((target == null) || (target.length <= 0))	{ var t = 'blank' }
		else											{ var t = target };
		return '<a href="' + href + '" target="_' + t + '">';
	}
	else return '';
};

function lnk (href, title, target) {
	if ((title == null) || (title.length < 1))		{ var t = '' }
	else											{ var t = title };
	if ((href != null) && (href.length > 0) && (t.length > 0))	return lnkdf(href,target) + t + '</a>'
	else														return t;
};

function refto () {
	var orig_location = new String("");
	orig_location = location.href;
	var i = srchstrng (orig_location,"#",1);
	if (i >= 0) orig_location = orig_location.substring(0,i);
	return orig_location;
};
function mailto (name, domain, type, title) { return lnk('mailto:'+name+'@'+domain+'.'+type,title,'self'); };

function srchstrng (tx, sc, nth) {
// returns position of nth occurence of string sc in string tx; -1 if not found
	var l = tx.length;
	var lc = sc.length;
	if ((lc > 0) && (l > 0) && (nth > 0)) {
		var nc = 0; for (var ii = 0; ii<l; ii++) {
			if (tx.substring(ii, ii+lc) == sc) {
				nc = nc + 1; if (nc == nth) return ii;
	};	};	};  
	return -1;
};

// ____ HTML form objects _____________________________________________________________________________________

// textarea
function make_textarea (rows, cols, onchange, value, wrap, classname, extra) {
	var s = new String("");
	if (classname == null)	s = '<textarea'
	else					s = '<textarea class="' + classname + '"';
	if (rows != null)		s = s + ' rows="'	+ rows	+ '"'
	else					s = s + ' rows="5"';
	if (cols != null)		s = s + ' cols="'	+ cols	+ '"'
	else					s = s + ' cols="72"';
	if (wrap != null)		s = s + ' wrap="'	+ wrap	+ '"'
	else					s = s + ' wrap="virtual"';
	if (onchange != null)	s = s + ' onChange="' + onchange + '"';
	if (extra != null)		s = s + ' ' + extra;
	if (value != null)		s = s + ' value="' + value + '"';
	s = s + '></textarea>';
	return s;
};

// button by input control
function make_inputbutton (title, onclick, classname, extra) {
	var s = new String("");
	s = '<input type="button"';
	if (classname != null)	s = s + ' class="' + classname + '"';
	if (extra != null)		s = s + ' ' + extra;
	s = s + ' value="';
	if (title != null)		s = s + title + '"'
	else					s = s + 'OK"';
	s = s + ' onClick="';
	if (onclick != null)	s = s + onclick + '">'
	else					s = s + 'alert(&#39;What should I do?&#39;)">';
	return s;
};

// button
function make_button (caption, onclick, disabled, imgsrc, tooltip, type, classname, extra) {
	var s = new String("");
	s = '<button';
	if (classname != null)	s = s + ' class="' + classname + '"';
	if (extra != null)		s = s + ' ' + extra;
	s = s + ' onClick="';
	if (onclick != null)	s = s + onclick + '"'
	else					s = s + 'alert(&#39;What should I do?&#39;)"';
	if ((disabled != null) && disabled)	s = s + ' disabled';
	s = s + ' type="';
	if (type != null)		s = s + type + '"'
	else					s = s + 'button"';
	if (tooltip != null)	s = s + ' title="' + tooltip + '"';
	if (imgsrc != null)		var i = img(imgsrc,tooltip)
	else					var i = '';
	if (caption != null)	s = s + '>' + caption + i + '</button>'
	else					s = s + '>OK' + i + '</button>';
	return s;
};

// text input
function make_input (value, size, onchange, classname, extra) {
	var s = new String("");
	s = '<input type="text"';
	if (classname != null)	s = s + ' class="' + classname + '"';
	if (extra != null)		s = s + ' ' + extra;
	s = s + ' value="';
	if (value != null)		s = s + value + '"'
	else					s = s + '0"';
	if (onchange != null)	s = s + ' onchange="' + onchange + '"';
	s = s + ' size="';
	if (size != null)		s = s + size + '">'
	else					s = s + '3">';
	return s;
};

// checkbox
function make_checkbox (value, onclick, checked, classname, extra) {
// checked also works with true/false
	var s = new String("");
	s = '<input type="checkbox"';
	if (classname != null)				s = s + ' class="' + classname + '"';
	if (extra != null)					s = s + ' ' + extra;
	if (value != null)					s = s + ' value="' + value + '"';
	if (onclick != null)				s = s + ' onclick="' + onclick + '"';
	if ((checked != null) && checked)	s = s + ' checked>'
	else								s = s + '>';
	return s;
};

// radiobutton
function make_radiobutton (value, name, onclick, checked, classname, extra) {
// checked also works with true/false
	var s = new String("");
	s = '<input type="radio"';
	if (classname != null)				s = s + ' class="' + classname + '"';
	if (extra != null)					s = s + ' ' + extra;
	if (value != null)					s = s + ' value="' + value + '"';
	if (name != null)					s = s + ' name="' + name + '"';
	if (onclick != null)				s = s + ' onclick="' + onclick + '"';
	if ((checked != null) && checked)	s = s + ' checked>'
	else								s = s + '>';
	return s;
};

// option
function make_option (options, onchange, selectednum, selectedval, nentries, classname, optionclassname, extra) {
// options is an array of nentries entries for each option;
// if nentries is 1 (default) the option value is 0..n and options contains just the labels;
// if nentries is 2 or more, the first entry = the value, the second entry = the label; other entries are allowed but not used here
	var s = new String("");
	var label = new String("");
	var value = new String("");
	var sel = new String("");
	if (selectednum == null)	var selnum = 0
	else						var selnum = selectednum;
	s = '<select';
	if (classname != null)				s = s + ' class="' + classname + '"';
	if (extra != null)					s = s + ' ' + extra;
	if (onchange != null)				s = s + ' onchange="' + onchange + '"';
	s = s + '>';
	if ((nentries == null) || (nentries < 1))	var n = 1
	else										var n = nentries;
	for (var i=0; i<(options.length/n); i++) {
		if (optionclassname == null)	s = s + '<option'
		else							s = s + '<option class="' + optionclassname + '"';
		if (n > 1)						{ label = options[(i*n)+1];	value = options[(i*n)]; }
		else							{ label = options[i*n];		value = i.toString(); };
		if (selnum == i)				sel = ' selected'
		else							sel = '';
		if (selectedval != null) {
			if (value == selectedval)	sel = ' selected'
			else						sel = '';
		};
		s = s + sel + ' value="' + value + '">' + label + '</option>';
	};
	s = s + '</select>';
	return s;
};

// ____ Thesaurus strings _____________________________________________________________________________________

var letterindex = new String // Follows the order of the array.sort - when adding characters, make sure the value adheres to the array.sort order
	("'-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿŒœŠšŸ");
function compare_word (str1, str2) {
// returns -1 if str1 comes before str2, +1 when str1 comes after str2, and 0 when str1 == str2
	var k1 = 0; var k2 = 0;
	var strlen = str1.length; if (str2.length<strlen) strlen = str2.length;
	if (strlen>0) {
		for (var i=0; i<strlen; i++) {
			k1 = letterindex.indexOf(str1.charAt(i));
			k2 = letterindex.indexOf(str2.charAt(i));
			if (k1>k2) return 1
			else if (k1<k2) return -1;
	};	};
	if (str1.length>str2.length) return 1
	else if (str1.length<str2.length) return -1
	else return 0;
};
function isValidWord (word) {
	if (word.length>0) {
		for (var i=0; i<word.length; i++) if (letterindex.indexOf(word.charAt(i))<0) return false;
		return true;
	};
	return false;
};

function isInList (word, list) {	
// binary search tree of lookup word in sorted list (array of words) - returns true when present
// list MUST be sorted
	var m = 0; var n = 0;
	var i = Math.round(list.length/2); var ispan = i;
	if (list.length>0) do {
		if (i<0) i = 0; if (i>=list.length)	i = list.length - 1;
		m = compare_word(word,list[i]);
		ispan = Math.round(ispan/2);
		if (ispan<2) { n++; if (n>3) break; };
		if (m<0)		i = i - ispan
		else if (m>0)	i = i + ispan
		else			return true;
	} while (true);
	return false;
};

var upcaseindex = new String ("ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝŒŠŸ");
function anyUpperCase (word) {
// returns true if any uppercase letter in word
	if (word.length>0) for (var i=0; i<word.length; i++) if (upcaseindex.indexOf(word.charAt(i))>=0) return true;
	return false;
};

function StringToList (str, list, list_lc) {
// input: str; output in list (array)  - if list_lc (array) not null, it contains only words with any uppercase letter but converted to lowercase
	list.length = 0; if (list_lc!=null) list_lc.length = 0;
	if (str.length>0) {
		var w = ''; var n = 0; var c = ''; var nlc = 0;
		for (var i=0; i<str.length; i++) {
			c = str.charAt(i);
			if (isSeparator(c)) {
				if (isValidWord(w)) {
					list[n] = w; n++;
					if (list_lc!=null) { if (anyUpperCase(w)) { list_lc[nlc] = w.toLowerCase(); nlc++; }; };
				};
				w = '';
			} else w = w + c;
		};
		if (isValidWord(w)) {
			list[n] = w;
			if (list_lc!=null) { if (anyUpperCase(w)) list_lc[nlc] = w.toLowerCase();
};	};	};	};

var separatorindex = new String (" ,;:.!?()[]{}\b\f\n\r\t\\");
function isSeparator (c) { return (separatorindex.indexOf(c) >= 0); };

function SortRemoveDuplicates (list) {
	if (list.length>0) {
		list.sort();
		var n = 0; var cword = new String(""); var word = new String("");
		for (var i=0; i<list.length; i++) {
			word = list[i];
			if (word!=cword) { cword = word; list[n] = word; n++; };
		};
		list.length = n;
};	};

function RemoveDuplicatesFromUnsortedList (list) {
	var duplist = new String(""); var word = new String(""); var j = 0;
	if (list.length>1) { // one only cant be a dupe
// step 1: set dupes void in list, fill up duplist
		for (var i=0; i<(list.length-1); i++) { // the last one is never a dupe
			word = list[i];
			if (word.length>0) {
				for (j=i+1; j<list.length; j++) { if (list[j]==word) { list[j] = ''; duplist = duplist + word + ' '; }; };
		};	};
// step 2: clean list of empty set dupes.			
		var n = 0;
		for (i=0; i<list.length; i++) {
			word = list[i];
			if (word.length>0) { list[n] = word; n++; };
		};
		list.length = n; // adapt array size
	};
	return duplist;
};

function isInUnsortedList (word, list) {	
// returns position of word in list. < 0 when not there.
	if (list.length>0) for (var i=0; i<list.length; i++) if (word==list[i]) return i;
	return -1;
};
