// javascript for common functions

function jsutils_toggleadiv(thedivname)
{
	var divnamesplit = thedivname.split(',');
	for (zz = 0; zz < divnamesplit.length; zz++)
	{
		var thisdivname = divnamesplit[zz];
		if (document.getElementById(thisdivname))
		{
			if (document.getElementById(thisdivname).style.display == 'none')
				document.getElementById(thisdivname).style.display = '';
			else
				document.getElementById(thisdivname).style.display = 'none';
		}
    }
    return false;
}

function jsutils_setclass(theid, mode, clname)
{
	if (theid && (theid != ''))
	{
		if (document.getElementById(theid))
		{
			curclass = document.getElementById(theid).className;

			if (mode)
			{
				// add a class
				if (curclass.indexOf(clname) == -1)
					curclass = curclass + ' ' + clname;
			}
			else
			{
				// remove a class
				curclass = curclass.replace(clname, '');
			}

			// clean up - remove double spaces
			curclass = curclass.replace('  ', ' ');
			// clean up - remove trailing space
			if (curclass.lastIndexOf(' ') == curclass.length-1)
				curclass = curclass.substring(0, curclass.length-1);

			document.getElementById(theid).className = curclass;
		}
	}
}

function jsutils_getcookie(cname)
{
	nameEQ = cname + '=';
	ca = document.cookie.split(';');
	for(i = 0;i < ca.length;i++)
	{
		c = ca[i];
		while (c.charAt(0) == ' ')
			c = c.substring(1, c.length);
			
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length);
	}
	return '';
}

function jsutils_setcookie(cname, cval)
{
	var expdate = new Date();
	expdate.setFullYear(2100,1,1)
	document.cookie = cname + '=' + cval + ';expires=' + expdate + ';';
}

function jsutils_setcookienoexpiry(cname, cval)
{
	document.cookie = cname + '=' + cval + ';';
}

function jsutils_usemodaldialogs(thebtype)
{
	var usemodal = false;
	if (thebtype == 'IE')
	{
		if (window.showModalDialog)
			usemodal = true;
	}
	else if (thebtype == 'FF3')
		usemodal = true;
		
	return usemodal;
}

function jsutils_formatstring(thetext, data0, data1, data2, data3, data4, data5, data6, data7, data8, data9)
{
	thetext = thetext.replace('{0}', data0);
	thetext = thetext.replace('{1}', data1);
	thetext = thetext.replace('{2}', data2);
	thetext = thetext.replace('{3}', data3);
	thetext = thetext.replace('{4}', data4);
	thetext = thetext.replace('{5}', data5);
	thetext = thetext.replace('{6}', data6);
	thetext = thetext.replace('{7}', data7);
	thetext = thetext.replace('{8}', data8);
	thetext = thetext.replace('{9}', data9);
	return thetext;
}

function jsutils_resizewindow(winsize_IE, winsize_MOZ)
{
	// reset window size
	var browsertype = jsutils_dobrowserdetect();
	if (browsertype == 'IE')
	{
		// IE
		thewindowsize = winsize_IE.toLowerCase();
		fidx = thewindowsize.indexOf('width=');
		eidx = thewindowsize.indexOf(';', fidx);
		thewinwidth = thewindowsize.substring(fidx+6, eidx);
		thewinwidth = thewinwidth.replace('px', '');
		window.dialogWidth = thewinwidth + 'px';

		fidx = thewindowsize.indexOf('height=');
		eidx = thewindowsize.indexOf(';', fidx);
		thewinheight = thewindowsize.substring(fidx+7, eidx);
		thewinheight = thewinheight.replace('px', '');
		window.dialogHeight = thewinheight + 'px';
	}
	else
	{
		// Mozilla
		thewindowsize = winsize_MOZ.toLowerCase();
		fidx = thewindowsize.indexOf('width=');
		eidx = thewindowsize.indexOf(',', fidx);
		thewinwidth = thewindowsize.substring(fidx+6, eidx);
		thewinwidth = thewinwidth.replace('px', '');
		
		fidx = thewindowsize.indexOf('height=');
		eidx = thewindowsize.indexOf(',', fidx);
		thewinheight = thewindowsize.substring(fidx+7, eidx);
		thewinheight = thewinheight.replace('px', '');
		
		if (browsertype == 'GOOCHRM' || browsertype == 'SAF' || browsertype == 'IES')
			window.resizeTo(thewinwidth, thewinheight);
		else	// other MOZ
		{
			window.innerWidth = thewinwidth;
			window.innerHeight = thewinheight;
		}
	}
}

function jsutils_parseRETURNTOlfcr(thetext)
{
	// replace chr(13) & chr(10) with &LF&
	thetext = thetext.replace(/\r\n/g, '&LF&');

	// replace chr(13) & chr(10) with &LF&
	fidx = thetext.indexOf(String.fromCharCode(13) + String.fromCharCode(10));
	while (fidx != -1)
	{
		thetext = thetext.substring(0,fidx) + '&LF&' + thetext.substring(fidx+1, thetext.length);
		fidx = thetext.indexOf(String.fromCharCode(13) + String.fromCharCode(10));
	}

	// replace chr(13) with &LF&
	fidx = thetext.indexOf(String.fromCharCode(13));
	while (fidx != -1)
	{
		thetext = thetext.substring(0,fidx) + '&LF&' + thetext.substring(fidx+1, thetext.length);
		fidx = thetext.indexOf(String.fromCharCode(13));
	}

	// replace chr(10) with &LF&
	fidx = thetext.indexOf(String.fromCharCode(10));
	while (fidx != -1)
	{
		thetext = thetext.substring(0,fidx) + '&LF&' + thetext.substring(fidx+1, thetext.length);
		fidx = thetext.indexOf(String.fromCharCode(10));
	}

	return thetext;
}

function jsutils_parseRETURNTObr(thetext)
{
	// replace chr(13) & chr(10) with <br />
	thetext = thetext.replace(/\r\n/g, '<br />');

	// replace chr(13) & chr(10) with <br />
	fidx = thetext.indexOf(String.fromCharCode(13) + String.fromCharCode(10));
	while (fidx != -1)
	{
		thetext = thetext.substring(0,fidx) + '<br />' + thetext.substring(fidx+1, thetext.length);
		fidx = thetext.indexOf(String.fromCharCode(13) + String.fromCharCode(10));
	}

	// replace chr(13) with <br />
	fidx = thetext.indexOf(String.fromCharCode(13));
	while (fidx != -1)
	{
		thetext = thetext.substring(0,fidx) + '<br />' + thetext.substring(fidx+1, thetext.length);
		fidx = thetext.indexOf(String.fromCharCode(13));
	}

	// replace chr(10) with <br />
	fidx = thetext.indexOf(String.fromCharCode(10));
	while (fidx != -1)
	{
		thetext = thetext.substring(0,fidx) + '<br />' + thetext.substring(fidx+1, thetext.length);
		fidx = thetext.indexOf(String.fromCharCode(10));
	}

	return thetext;
}

// browser detection
function jsutils_dobrowserdetect()
{
    var browserstuff = new Array();
    browserstuff = jsutils_getbrowserdetect();

    // make a simpler type
    var sbtype = '';
	if (browserstuff[0] == 'Explorer')
	{
		sbtype = 'IE';
		// IE9 compatability
		if (Number(browserstuff[1]) >= 9)
			sbtype = 'IES';
	}
	else if (browserstuff[0] == 'Firefox')
	{
		fidx = browserstuff[1].indexOf('.');
		if (fidx > -1)
		{
			theffnum = browserstuff[1].substring(0, fidx);
			if (Number(theffnum) >= 3)
				sbtype = 'FF3'; // Firefox 3
			else
				sbtype = 'MOZ'; // firefox 2
		}
		else
			sbtype = 'MOZ'; // firefox 2
	}
	else if (browserstuff[0] == 'Google Chrome')
		sbtype = 'GOOCHRM';
	else if (browserstuff[0] == 'Safari')
		sbtype = 'SAF';
	else
		sbtype = 'MOZ'; // other mozilla

	return sbtype;
}

function jsutils_getbrowserdetect()
{
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1)
				return '0';
			else
			{
				// guzz modified
				thedatastring = dataString.substring(index+this.versionSearchString.length);
				// check for starts with space
				if (thedatastring.substring(0,1) == ' ')
					thedatastring = thedatastring.substring(1);
				// check for end with ;
				fidx = thedatastring.indexOf(';');
				if (fidx > -1)
					thedatastring = thedatastring.substring(0, fidx);
				// check for end by space
				fidx = thedatastring.indexOf(' ');
				if (fidx > -1)
					thedatastring = thedatastring.substring(0, fidx);

				return thedatastring;
			}
		},
		dataBrowser: [
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox",
				versionSearch: "Firefox/"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Google Chrome",
				versionSearch: "Chrome/"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win32",
				identity: "Windows XP"
			},
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]

	};
	BrowserDetect.init();

	var browserstuff = new Array();
	browserstuff[0] = BrowserDetect.browser;
	browserstuff[1] = BrowserDetect.version;
	browserstuff[2] = BrowserDetect.OS;

    return browserstuff;
}

// addLoadEvent
function jsutils_addonLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
		window.onload = func;
	else
	{   
		window.onload = function() {
			if (oldonload)
				oldonload();

			func();
		}
	}
}

// addUnLoadEvent
function jsutils_addonUnLoadEvent(func)
{
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function')
		window.onunload = func;
	else
	{   
		window.onunload = function() {
			if (oldonunload)
				oldonunload();

			func();
		}
	}
}
