﻿//
// get_platform
//
function get_platform()
{
	// If platform is unknown - assume Windows
	if (typeof navigator.platform == "undefined")
		return "win";

	// Get platform
	var txt = new String(navigator.platform);
	txt = txt.toLowerCase();
	return		(txt.indexOf("linux") >= 0)	? "linux"
			:	(txt.indexOf("win") >= 0)	? "win"
			:	(txt.indexOf("mac") >= 0)	? "mac"
			:	(txt.indexOf("sun") >= 0)	? "sun"
											: "unknown";
}



//
// get_browser
//
function get_browser()
{
	// Leave "gecko", ie. Mozilla last in list since many browsers contain "gecko" keyword!
	var txt = new String(navigator.userAgent);
	txt = txt.toLowerCase();
	return		(txt.indexOf("konqueror") >= 0)	? "konqueror"
			:	(txt.indexOf("firefox") >= 0)	? "firefox"
			:	(txt.indexOf("camino") >= 0)	? "camino"
			:	(txt.indexOf("safari") >= 0)	? "safari"
			:	(txt.indexOf("opera") >= 0)		? "opera"
			:	(txt.indexOf("netscape") >= 0)	? "netscape"
			:	(txt.indexOf("msie") >= 0)		? "msie"
			:	(txt.indexOf("gecko") >= 0)		? "mozilla"
												: "unknown";
}



//
// get_browser_version
//
// NOTE: the function only returns version in 2 levels (xx.yy) and not 3 (xx.yy.zz)!
//
function get_browser_version()
{
	var txt_browser = get_browser();
	var str	= (txt_browser == "msie")		? "msie "
			: (txt_browser == "netscape")	? "netscape/"
			: (txt_browser == "firefox")	? "firefox/"
			: (txt_browser == "mozilla")	? "rv:"
			: (txt_browser == "opera")		? "opera "
			: (txt_browser == "camino")		? "camino/"
											: "";
	if (str != "")
	{
		var txt = new String(navigator.userAgent);
		txt = txt.toLowerCase();
		if (txt.indexOf(str) >= 0)
			return parseFloat(txt.substr(txt.indexOf(str) + str.length));
	}
	return -1;
}



//
// get_element
//
function get_element(name)
{
    var id = name.replace(/\$/g, "_");
    if (document.getElementById)
        return document.getElementById(id);
    if (document.all)
        return document.all[id];
    return null;
}



//
// focus_field
//
function focus_field(obj_field)
{
	// Focus on field
	if (!obj_field)
		return false;
	obj_field.focus();
	if (obj_field.type == "text")
		obj_field.select();
	if ((obj_field.type == "select-one") || (obj_field.type == "select-multiple"))
		if ((obj_field.selectedIndex == -1) && (obj_field.length != -1))
			obj_field.selectedIndex = 0;
	return true;
}



//
// on_enter_run
//
function on_enter_run(event, method)
{
	// Accept all other characters than ENTER (and also SUBMIT on MAC)
	var char_code = (navigator.appName == "Netscape") ? event.which : event.keyCode;
	var is_ENTER = ((char_code == 13) || ((get_platform() == "mac") && (char_code == 3)));
	if (!is_ENTER)
		return true;

	// Run code
	method(event);
	return false;
}



//
// on_enter_focus
//
function on_enter_focus(event, obj_field)
{
	// Accept all other characters than ENTER (and also SUBMIT on MAC)
	var char_code = (navigator.appName == "Netscape") ? event.which : event.keyCode;
	var is_ENTER = ((char_code == 13) || ((get_platform() == "mac") && (char_code == 3)));
	if (!is_ENTER)
		return true;

	// Focus on field
	focus_field(obj_field);
	return false;
}



//
// on_enter_focus_next
//
function on_enter_focus_next(event, obj_field)
{
	// Accept all other characters than ENTER (and also SUBMIT on MAC)
	var char_code = (navigator.appName == "Netscape") ? event.which : event.keyCode;
	var is_ENTER = ((char_code == 13) || ((get_platform() == "mac") && (char_code == 3)));
	if (!is_ENTER)
		return true;

	// Focus on next field
	focus_field(get_next_focus_field(obj_field));
	return false;
}



//
// get_next_focus_field
//
function get_next_focus_field(obj_field)
{
	var obj_form = obj_field.form;
	var found = null;
	for (var i = 0; i < obj_form.elements.length; i++)
	{
		var field = obj_form.elements[i];
		if ((field.name != "") && (field.type != "hidden") && (!field.disabled))
			if (field == obj_field)
				found = field;
			else if ((obj_field == null) || ((found) && (found.name != field.name)))
				if (get_browser() != "msie")
					return field;
				else
				{
					// Check if field and all parents are visible
					var check_visible = field;
					while ((check_visible.currentStyle.display != "none")
						&& (check_visible.currentStyle.visibility != "hidden"))
					{
						// If field has no parent - we're done!
						if (check_visible.parentElement == null)
							break;

						// Check parent
						check_visible = check_visible.parentElement;
					}

					// If still visible
					if ((check_visible.currentStyle.display != "none")
						&& (check_visible.currentStyle.visibility != "hidden"))
						return field;
				}
	}
	return null;
}



//
// format_str
//
function format_str(str)
{
	var i = 1, pos = str.toUpperCase().indexOf("%S");
	while ((pos != -1) && (i < format_str.arguments.length))
	{
		var value = format_str.arguments[i++];
		if (value == null)
			value = "";
		str = str.substring(0, pos) + value + str.substring(pos + 2);
		pos = str.toUpperCase().indexOf("%S", pos + value.length);
	}
	return str;
}



//
// add_to_url
//
function add_to_url(url, name, value) {
    var anchor = "";
    var idxAnchor = url.indexOf("#");
    if (idxAnchor > 0)
        anchor = url.substr(idxAnchor);
    else if (idxAnchor == 0) {
        anchor = url;
        url = document.location.href;
        idxAnchor = url.indexOf("#");
    }
    if (idxAnchor > 0)
        url = url.substr(0, idxAnchor);
    var idxStart = url.indexOf("?" + name + "=");
    if (idxStart < 0)
        idxStart = url.indexOf("&" + name + "=");
    if (idxStart > 0) {
        var idxEnd = url.indexOf("&", idxStart + 1);
        if (idxEnd < 0)
            url = url.substr(0, idxStart);
        else
            url = url.substr(0, idxStart + 1) + url.substr(idxEnd + 1);
    }
    if (url.indexOf("?") < 0)
        url += "?";
    else if (url.indexOf("?") < url.length - 1)
        url += "&";
    return url + name + "=" + value + anchor;
}



//
// base64
//
var base64 =
{
    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input)
    {
        input = base64._utf8_encode(input);

        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;

        var i = 0;
        while (i < input.length)
        {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            }
            else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output += this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
        }

        return output;
    },

    // public method for decoding
    decode : function (input)
    {
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;

        var i = 0;
        while (i < input.length)
        {
            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) output += String.fromCharCode(chr2);
            if (enc4 != 64) output += String.fromCharCode(chr3);
        }

        return base64._utf8_decode(output);
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string)
    {
        string = string.replace(/\r\n/g,"\n");

        var utftext = "";

        for (var n = 0; n < string.length; n++)
        {
            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext)
    {
        var string = "";
        var c = c1 = c2 = 0;

        var i = 0;
        while ( i < utftext.length )
        {
            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }

        return string;
    }
}



//
// MakeSafeForHTML
//
function MakeSafeForHTML(value)
{
    return new String(value).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}



////////////////////////////
// set_cookie
////////////////////////////
// name		REQUIRED
// value	REQUIRED
// expires	OPTIONAL (don't specify for per-session cookie)
// path		OPTIONAL (default "/")
// domain	OPTIONAL
// secure	OPTIONAL
function set_cookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGTMString() : "") +
		((path) ? "; path=" + path : "; path=/") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}



////////////////////////////
// get_cookie
////////////////////////////
// name		REQUIRED
function get_cookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return get_cookie_val(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
}



////////////////////////////
// delete_cookie
////////////////////////////
// name		REQUIRED
// path		OPTIONAL (default "/")
// domain	OPTIONAL
function delete_cookie(name, path, domain) {
    if (get_cookie_val(name)) {
        document.cookie = name + "=" +
			((path) ? "; path=" + path : "; path=/") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GTM";
    }
}



////////////////////////////
// get_cookie_val 
////////////////////////////
// DONT USE
// --- UTILITY FUNCTION
function get_cookie_val(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}



