

var isIE = document.all;
var isNN =! document.all && document.getElementById;
var isN4 = document.layers;

function getById(id) {
    var elem = null;
    if (document.getElementById) {
        elem = document.getElementById(id);
    } else if (document.all) {
        elem = document.all[id];
        if (!elem) {
            elem = eval('document.all.' + id);
        }
    } else if (document.layers) {
        elem = getObjNN4(document, id);
    }

    if (!elem) {
        elem = document.getElementById(id);
    }                   
    return elem;  
}

function getObjNN4(obj,name) {
    var x = obj.layers;
    var foundLayer;
    for (var i=0;i<x.length;i++) {
        if (x[i].id == name) {
            foundLayer = x[i];
        } else if (x[i].layers.length) {
            var tmp = getObjNN4(x[i],name);
            if (tmp) {
                foundLayer = tmp;
            }
        }
    }
    return foundLayer;
}


function getInnerHtml(element) {
    if (element.innerHTML != null) {
        return element.innerHTML;
    } else if (document.layers) {
        return element.document.read();
    }
}

function setInnerHtml(element, html) {
    if (element.innerHTML != null) {
        element.innerHTML = '';
        element.innerHTML = html;
    } else if (document.layers) {
        element.document.open();
        element.document.write(html);
        element.document.close();
    } else {
        element.innerHtml = '';
        element.innerHtml = html;
    }

    if (element.refresh) {
        element.refresh();
    }
    if (element.document && element.document.refresh) {
        element.document.refresh();
    }
}

function setVisible(element, vis) {
    if (isIE || isNN) {
        if (vis) {
            element.style.visibility = "visible";
        } else {
            element.style.visibility = "hidden";
        }
    } else if (isN4) {
        if (vis) {
            element.style.visibility = "show";
        } else {
            element.style.visibility = "hide";
        }
    } else {
        if (vis) {
            element.style.visibility = "visible";
        } else {
            element.style.visibility = "hidden";
        }
    }
}

function isVisible(element) {
    if (isIE || isNN) {
        return element.style.visibility == "visible";
    } else if (isN4) {
        return element.style.visibility == "show";
    } else {
        return element.style.visibility == "visible";
    }
}

function escape(str) {
    return str;
}

function getX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

function getY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
/*
        while (obj.parent && obj.parent != top) {
            curtop += obj.parent.offsetTop;
            obj = obj.parent;
        }
*/
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}



function setX(obj, x) {
    if (obj.style) {
        obj.style.left = x + 'px';
    } else if (obj.left) {
        obj.left = x + 'px';
    } else if (obj.x) {
        obj.x = x;
    }
}


function setY(obj, y) {
    if (obj.style) {
        obj.style.top = y + 'px';
    } else if (obj.top) {
        obj.top = y + 'px';
    } else if (obj.y) {
        obj.y = y;
    }
}


function getWidth(obj) {
    if (obj.clip && obj.clip.width) {
        return obj.clip.width;
    }
    if (obj.offsetWidth) {
        return obj.offsetWidth;
    }
    if (obj.width) {
        return obj.width;
    }
    if (obj.w) {
        return obj.w;
    }
    if (obj.style && obj.style.width) {
        return obj.style.width;
    }
    if (obj.style && obj.style.pixelWidth) {
        return obj.style.pixelWidth;
    }
    return -1;
}

function getHeight(obj) {
    if (obj.clip && obj.clip.height) {
        return obj.clip.height;
    }
    if (obj.offsetHeight) {
        return obj.offsetHeight;
    }
    if (obj.height) {
        return obj.height;
    }
    if (obj.h) {
        return obj.h;
    }
    if (obj.style && obj.style.height) {
        return obj.style.height;
    }
    if (obj.style && obj.style.pixelHeight) {
        return obj.style.pixelHeight;
    }
    return -1;
}





function setHeight(obj, h) {
    if (obj.clip && obj.clip.height) {
        obj.clip.height = h + "px";
        return;
    }
    if (obj.style && obj.style.height) {
        obj.style.height = h + "px";
        return;
    }
    if (obj.style && obj.style.pixelHeight) {
        obj.style.pixelHeight = h + "px";
        return;
    }
    if (obj.offsetHeight) {
        obj.offsetHeight = h;
        return;
    }
    if (obj.height) {
        obj.height = h;
        return;
    }
    if (obj.h) {
        obj.h = h;
        return;
    }
}


function setWidth(obj, w) {
    if (obj.clip && obj.clip.width) {
        obj.clip.width = w + "px";
        return;
    }
    if (obj.style && obj.style.width) {
        obj.style.width = w + "px";
        return;
    }
    if (obj.style && obj.style.pixelWidth) {
        obj.style.pixelWidth = w + "px";
        return;
    }
    if (obj.offsetWidth) {
        obj.offsetWidth = w;
        return;
    }
    if (obj.width) {
        obj.width = w;
        return;
    }
    if (obj.w) {
        obj.w = w;
        return;
    }
}







function encodeK(s, key) {

    if (s == null) {
        return s;
    }

    if (key == null) {
        key = 'z';
    }

    var t = null;
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (c == key) {
            if (t == null) t = s.substring(0, i);
            t += key + '' + key;
        } else if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
            if (t != null) {
                t += c;
            }
        } else {
            if (t == null) t = s.substring(0, i);
            t += key + s.charCodeAt(i) + key;
        }
    }
    if (t != null) {
        return t;
    } else {
        return s;
    }
}


function decodeK(s, key) {

    if (s == null) {
        return s;
    }

    if (key == null) {
        key = 'z';
    }

    var t = null;
    for (var i = 0; i < s.length; i++) {
        if (s.charAt(i) == key) {
            if (t == null) t = s.substring(0, i);
            var n = '';
            for (var j = i + 1; j < s.length; j++) {
                if (s.charAt(j) == key) {
                    if (n.length == 0) {
                        t += key;
                    } else {
                        t += String.fromCharCode(parseInt(n));
                    }
                    i = j;
                    j = s.length;
                } else {
                    n += s.charAt(j);
                }
            }
        } else if (t != null) {
            t += s.charAt(i);
        }
    }
    if (t != null) {
        return t;
    } else {
        return s;
    }
}










/*
 * Returns the overly complicated object tag used for rendering a flash movie.
 */
function getFlashML(url, width, height) {
    var ml = '';
    ml += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
    ml += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" ';
    ml += 'width="' + width + '" height="' + height + '" id="' + url + '" align="middle">';
    ml += '<param name="allowScriptAccess" value="sameDomain" />';
    ml += '<param name="movie" value="' + url + '" />';
    ml += '<param name="quality" value="high" />';
    ml += '<param name="bgcolor" value="#ffffff" />';
    ml += '<embed src="' + url + '" quality="high" bgcolor="#ffffff" width="' + width + '" ';
    ml += 'height="' + height + '" name="' + url + '" align="middle" allowScriptAccess="sameDomain" ';
    ml += 'type="application/x-shockwave-flash" ';
    ml += 'pluginspage="http://www.macromedia.com/go/getflashplayer" />';
    ml += '</object>';
    return ml;
}




/*
 * This is a function which pretty much takes any javascript event listeners away from
 * an element.
 */
function sterilize(e, clickonly) {
    if (!e) return;
    if (e.ondblclick) e.ondblclick = null;
    if (e.onclick) e.onclick = null;
    if (!clickonly) {
        if (e.onmouseover) e.onmouseover = null;
        if (e.onmouseout) e.onmouseout = null;
        if (e.onmousedown) e.onmousedown = null;
        if (e.onmouseup) e.onmouseup = null;
        if (e.onmouseenter) e.onmouseenter = null;
        if (e.onmouseleave) e.onmouseleave = null;
        if (e.onmousemove) e.onmousemove = null;
        if (e.onselectstart) e.onselectstart = null;
        if (e.oncontextmenu) e.oncontextmenu = null;
        if (e.onkeypress) e.onkeypress = null;
        if (e.onkeydown) e.onkeydown = null;
        if (e.onkeyup) e.onkeyup = null;
        if (e.onchange) e.onchange = null;
        if (e.onreset) e.onreset = null;
        if (e.onselect) e.onselect = null;
        if (e.onsubmit) e.onsubmit = null;
        if (e.onblur) e.onblur = null;
        if (e.onfocus) e.onfocus = null;
        if (e.onload) e.onload = null;
        if (e.onresize) e.onresize = null;
        if (e.onscroll) e.onscroll = null;
        if (e.onunload) e.onunload = null;
        if (e.onabort) e.onabort = null;
        if (e.onerror) e.onerror = null;
        if (e.onsubtreemodified)  e.onsubtreemodified = null;
    }

    if (!e.childNodes) return;

    for (var i = 0; i < e.childNodes.length; i++) {
        if (e.childNodes[i]) sterilize(e.childNodes[i]);
    }
}





/*
 * COOKIES
 *****************************************************************/

/*
 * name - name of the cookie
 * value - value of the cookie
 * [expires] - expiration date of the cookie
 *   (defaults to end of current session)
 * [path] - path for which the cookie is valid
 *   (defaults to path of calling document)
 * [domain] - domain for which the cookie is valid
 *   (defaults to domain of calling document)
 * [secure] - Boolean value indicating if the cookie transmission requires
 *   a secure transmission
 * * an argument defaults when it is assigned null as a placeholder
 * * a null placeholder is not required for trailing omitted arguments
 */

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}





/*
 * name - name of the desired cookie
 * return string containing value of specified cookie or null
 * if cookie does not exist
 */
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}





/*
 * name - name of the cookie
 * [path] - path of the cookie (must be same as path used to create cookie)
 * [domain] - domain of the cookie (must be same as domain used to
 *   create cookie)
 * path and domain default if assigned null or omitted if no explicit
 *   argument proceeds
 */
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}









/*
 * This extremely lame function opens a popup window, at the specified
 * url, with the specified window name, the specified width, and height.
 */
function spawn(url, nameW, w, h) {
    var foo = null;
    if (navigator.appVersion.indexOf('4') != -1) {
        // Vars for centering the new window on Version 4 Browsers
        x4 = screen.width/2 - (w/2);
        y4 = screen.height/2 - (h/2);
        foo = window.open(url, nameW, 'height='+h+',width='+w+',scrollbars=1,resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=' + x4 + ',top=' + y4 + '');
    } else {
        foo = window.open(url, nameW, 'height='+h+',width='+w+',scrollbars=1,resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=150,top=150');
    }
    foo.focus();
    return foo;
}







// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function rawUrlEncode(plaintext) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...

    if (plaintext == null) {
        return null;
    }

	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function rawUrlDecode(encoded) {
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
    return plaintext;
};


function addslashes(str) {
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}


function mlEncode(str) {
    var t = '';
    for (var i = 0; i < str.length; i++) {
        if (str.charAt(i) == '<') {
            t += '&lt;';
        } else if (str.charAt(i) == '>') {
            t += '&gt;';
        } else if (str.charAt(i) == '\'') {
            t += '&apos;';
        } else if (str.charAt(i) == '&') {
            t += '&amp;';
        } else if (str.charAt(i) == '"') {
            t += '&quot;';
        } else {
            t += str.charAt(i);
        }
    }
    return t;
}

function mlDecode(str) {
    var t = '';
    for (var i = 0; i < str.length; i++) {
        if (str.charAt(i) == '&') {
            if (i < str.length - 5
                && (str.charAt(i + 1) == 'a' || str.charAt(i + 1) == 'A')
                && (str.charAt(i + 2) == 'm' || str.charAt(i + 2) == 'M')
                && (str.charAt(i + 3) == 'p' || str.charAt(i + 3) == 'P')
                && str.charAt(i + 4) == ';'
            ) {
                t += '&';
            } else if (i < str.length - 6
                && (str.charAt(i + 1) == 'a' || str.charAt(i + 1) == 'A')
                && (str.charAt(i + 2) == 'p' || str.charAt(i + 2) == 'P')
                && (str.charAt(i + 3) == 'o' || str.charAt(i + 3) == 'O')
                && (str.charAt(i + 4) == 's' || str.charAt(i + 4) == 'S')
                && str.charAt(i + 5) == ';'
            ) {
                t += '\'';
            } else if (i < str.length - 6
                && (str.charAt(i + 1) == 'q' || str.charAt(i + 1) == 'Q')
                && (str.charAt(i + 2) == 'u' || str.charAt(i + 2) == 'U')
                && (str.charAt(i + 3) == 'o' || str.charAt(i + 3) == 'O')
                && (str.charAt(i + 4) == 't' || str.charAt(i + 4) == 'T')
                && str.charAt(i + 5) == ';'
            ) {
                t += '"';
            } else if (i < str.length - 4
                && (str.charAt(i + 1) == 'g' || str.charAt(i + 1) == 'G')
                && (str.charAt(i + 2) == 't' || str.charAt(i + 2) == 'T')
                && str.charAt(i + 3) == ';'
            ) {
                t += '>';
            } else if (i < str.length - 4
                && (str.charAt(i + 1) == 'l' || str.charAt(i + 1) == 'L')
                && (str.charAt(i + 2) == 't' || str.charAt(i + 2) == 'T')
                && str.charAt(i + 3) == ';'
            ) {
                t += '<';
            } else {
                t += str.charAt(i);
            }
        } else {
            t += str.charAt(i);
        }
    }
    return t;
}
