//<![CDATA[

var avail_width = 800;
var avail_height = 600;
var scroll_bar_width = 16;
var browser_vendor = "";
var browser_full_name = navigator.appName;
var browser_version = parseFloat(navigator.appVersion);
var is_msie = false;
var user_agent = navigator.userAgent.toLowerCase();
var op_sys = "";

if (navigator.appName && navigator.appName.toUpperCase().indexOf("MICROSOFT")>-1) {
    is_msie = true;
    browser_vendor="MICR0SOFT";
}
else {
    is_msie = false;
    if (navigator.appName.toUpperCase().indexOf("NETSCAPE")>-1)
        browser_vendor="NETSCAPE";
    else
	browser_vendor=navigator.appName.toUpperCase();
}


function initBasic () {
    initBasicLib();
}

function initBasicLib () {
    var n = -1;
    if ((n=user_agent.indexOf("netscape/")) > -1) {
	is_msie = false;
	browser_vendor = "NETSCAPE";
	browser_version = parseFloat(user_agent.substring(n+9));
    }
    else if ((n=user_agent.indexOf("navigator/")) > -1) {
	is_msie = false;
	browser_vendor = "NETSCAPE";
	browser_version = parseFloat(user_agent.substring(n+10));
    }
    else if ((n=user_agent.indexOf("galeon/")) > -1) {
	is_msie = false;
	browser_vendor = "GALEON";
	browser_version = parseFloat(user_agent.substring(n+7));
    }
    else if ((n=user_agent.indexOf("firefox/")) > -1) {
	is_msie = false;
	browser_vendor = "FIREFOX";
	browser_version = parseFloat(user_agent.substring(n+8));
    }
    else if ((n=user_agent.indexOf("iceape/")) > -1) {
	is_msie = false;
	browser_vendor = "ICEAPE";
	browser_version = parseFloat(user_agent.substring(n+7));
    }
    else if ((n=user_agent.indexOf("icecat/")) > -1) {
	is_msie = false;
	browser_vendor = "ICECAT";
	browser_version = parseFloat(user_agent.substring(n+7));
    }
    else if ((n=user_agent.indexOf("opera/")) > -1) {
	is_msie = false;
	browser_vendor = "OPERA";
	browser_version = parseFloat(user_agent.substring(n+6));
    }
    else if ((n=user_agent.indexOf("safari/")) > -1) {
	is_msie = false;
	browser_vendor = "SAFARI";
	browser_version = parseFloat(user_agent.substring(n+7));
    }
    else if ((n=user_agent.indexOf("msie")) > -1) {
	is_msie = true;
	browser_vendor = "INTERNET EXPLORER";
	browser_version = parseFloat(user_agent.substring(n+5));
    }

    if (user_agent.indexOf("linux")>=0)
	op_sys = "LINUX";
    else if (user_agent.indexOf("windows")>=0)
	op_sys = "WINDOWS";
    else if (user_agent.indexOf("mac os x")>=0)
	op_sys = "OSX";
    if (is_msie) {
	scroll_bar_width = 20;
	if (document.body && typeof document.body != "undefined") {
	    avail_height = document.body.offsetHeight;
	    if (document.body.offsetHeight && document.body.offsetHeight > scroll_bar_width) {
		avail_height = document.body.offsetHeight-scroll_bar_width;
	    }
	    avail_width = document.body.offsetWidth-scroll_bar_width;
	}
	else {
	    avail_height=document.documentElement.offsetHeight;
	    avail_width=document.documentElement.offsetWidth;
	}
    }
    else {
	scroll_bar_width=16;
	avail_height = getAvailHeight();
	avail_width = getAvailWidth();
    }
}

function resizeImages() {
    if (typeof resizeImages.orig_width == "undefined")
	resizeImages.orig_width = getAvailWidth();
    if (typeof resizeImages.orig_width == "undefined") {
	 resizeImages.orig_width = new Array();
	 for (var x=0; x<document.images.length; x++) {
	    resizeImages.orig_width[resizeImages.length]=document.images[x].style.width;
	 }
    }
    else {
	var n = getAvailWidth()/resizeImages.orig_width;
	if (n<0.95) {
	    for (var x=0; x<document.images.length; x++) {
		var t=document.images[x].style.width;
		if (String(t).indexOf("px")>0) {
		    // adjust size
		}
	    }
	}
    }
}

function isMSIE() {
    return (is_msie);
}

function getScreenWidth() {
     var n = 0;
     if (browser_vendor.indexOf("MICROSOFT") > -1 || browser_vendor.indexOf("MSIE")>-1) {
         n = document.body.clientWidth; //trunc(screen.availWidth*0.95,0);
    }
    else {
         n = screen.width;
    }
    if (n<200) {
       n = 500;
    }
    return n;
}

function getScreenHeight() {
     var n = 0;
     if (browser_vendor.indexOf("MICROSOFT") > -1 || browser_vendor.indexOf("MSIE")>-1) {
        n =  document.body.clientHeight;
    }
    else {
         n = screen.height;
    }
    return n;
}
function getAvailWidth() {
    if (navigator.appName.toUpperCase().indexOf("MICROSOFT")>-1) {
	scroll_bar_width = 20;
	if (typeof document.body != "undefined") {
	    avail_width = document.body.offsetWidth-scroll_bar_width;
	}
	else {
	    avail_width=document.documentElement.offsetWidth;
	}
    }
    else {
	scroll_bar_width=16;
	avail_width = window.innerWidth-scroll_bar_width;
    }
    return avail_width;
}

function getAvailHeight() {
    if (navigator.appName.toUpperCase().indexOf("MICROSOFT")>-1) {
	scroll_bar_width = 20;
	if (document.body && typeof document.body != "undefined" && document.body.offsetHeight) {
	    avail_height = document.body.offsetHeight;
	    if (document.body.offsetHeight > scroll_bar_width) {
		avail_height = document.body.offsetHeight-scroll_bar_width;
	    }
	}
	else {
	    avail_height=document.documentElement.offsetHeight;
	}
    }
    else {
	scroll_bar_width=16;
	avail_height = window.innerHeight-scroll_bar_width;  // for the scroll bar
    }
    return avail_height;
}

function getTopOffset(){
    if (navigator.appName == "Microsoft Internet Explorer") {
        if (document.compatMode && document.compatMode == 'BackCompat') {
            if (document.body && typeof document.body.scrollTop != 'undefined') {
            return (document.body.scrollTop);
            }
        }
        else if (document.compatMode && document.compatMode == 'CSS1Compat') {
            if (document.documentElement && typeof document.documentElement.scrollTop != 'undefined')
                return (document.documentElement.scrollTop);
        }
    }
    else {
        return (window.pageYOffset);
    }
    return (0);
}

function getLeftOffset(){
    if (navigator.appName == "Microsoft Internet Explorer") {
        if (document.compatMode && document.compatMode == 'BackCompat') {
            if (document.body && typeof document.body.scrollLeft != 'undefined') {
                return (document.body.scrollLeft);
            }
        }
        else if (document.compatMode && document.compatMode == 'CSS1Compat') {
            if (document.documentElement && typeof document.documentElement.scrollLeft != 'undefined')
                return (document.documentElement.scrollLeft);
        }
    }
    else {
        return (window.pageXOffset);
    }
    return (0);
}

function freezeScreen(time_out) {
    var x=getLeftOffset();
    var y = getTopOffset();
    var delay=10;
    if (time_out)
	delay=time_out;
    window.setTimeout("window.scrollTo('"+x+"','"+y+"');",delay);
}

function getScrollPosition() {
    var x = getLeftOffset();
    var y = getTopOffset();
    return ({x:x, y:y});
}

function setScrollPosition (o) {
    window.scrollTo(o.x, o.y);
}

function getElementPosition (obj_or_id) {
    var objx = null;
    try {
	if (typeof obj_or_id == "string")
		objx = document.getElementById(obj_or_id);
	else
		objx = obj_or_id;
	if (!objx)
		return (null);
	if (!objx.offsetParent)
		return (null);
	var leftpos = 0;
	var toppos = 0;
	do {
		leftpos += objx.offsetLeft;
		toppos += objx.offsetTop;
	} while (objx == objx.offsetParent);
	return ({left:leftpos, top:toppos});
    }
    catch (ex) {
	return null;
    }
}

function moveElementTo (elem_id, pos) {
    try {
	var d = document.getElementById(elem_id);
	if (d && pos) {
	    var left = pos.left;
	    var top = pos.top;
	    if (left && top) {
		d.style.left=(left)+"px";
		d.style.top=(top)+"px";
	    }
	}
    }
    catch (ex) {
	;
    }
}

function newWindow2(s,t,l,pcnt) {
	return newWindow3(s,t,l,pcnt);
	/*
    var w = Math.round(getAvailWidth() * (pcnt/100));
    var h = Math.round(getAvailHeight() * (pcnt/100));
    var pwin = this;
    var xpos=getLeftOffset();
    var ypos=getTopOffset();
    this.setTimeout("window.scrollTo("+xpos+","+ypos+");",1000);
    try {
        var xwin = window.open(encodeURI(s),"","width="+w+",height="+h+",top=" + t + ",left=" + l + ",screenX="+l+",screenY=" + t + ",scrollbars=yes,menubar=yes,resizable=yes,toobar=yes,location=yes");
        if (!xwin) {
            alert ("You may have popup blocking enabled, preventing you from seeing this window.  Please disable popup blocking for this site and click the link again.");
        }
    }
    catch (ex) {
        alert (ex.toString());
    }
    return xwin;
	*/
}

function newWindow3(s,t,l,pcnt,frame_title) {
    var w = Math.round(getScreenWidth() * (pcnt/100));
    var h = Math.round(getScreenHeight() * ((pcnt-10)/100));
    var pwin = this;
    var xpos=getLeftOffset();
    var ypos=getTopOffset();
    this.setTimeout("window.scrollTo("+xpos+","+ypos+");",1000);
    try {
        var xwin = window.open(encodeURI(s),"","width="+w+",height="+h+",top=" + (t) + ",left=" + (l) + ",screenX="+(l)+",screenY=" + (t) + ",scrollbars=yes,resizable=yes,location=no,toolbar=no");
        if (!xwin) {
            alert ("You may have popup blocking enabled, preventing you from seeing this window.  Please disable popup blocking for this site and click the link again.");
        }
    }
    catch (ex) {
        alert (ex.toString());
    }
    return xwin;
}

function newWindow4(s,t,l,w,h,frame_title) {
    var pwin = this;
    var xpos=getLeftOffset();
    var ypos=getTopOffset();
    this.setTimeout("window.scrollTo("+xpos+","+ypos+");",1000);
    try {
        var xwin = window.open(encodeURI(s),"","width="+w+",height="+h+",top=" + (t) + ",left=" + (l) + ",screenX="+(l)+",screenY=" + (t) + ",scrollbars=yes,resizable=yes,location=no,toolbar=no");
        if (!xwin) {
            alert ("You may have popup blocking enabled, preventing you from seeing this window.  Please disable popup blocking for this site and click the link again.");
        }
    }
    catch (ex) {
        alert (ex.toString());
    }
    return xwin;
}

function toggleDisplay (elem) {
    var e = elem;
    var ret = false;
    if (typeof elem == "string")
        e = document.getElementById(elem);
    if (e) {
        if (e.style.display=="none") {
            ret = true;
            e.style.display = "block";
        }
        else
            e.style.display = "none";
    }
    return (ret);
}

function setDisplay (elem, dis) {
    var e = elem;
    var ret = false;
    if (typeof elem == "string")
        e = document.getElementById(elem);
    if (e) {
        e.style.display=dis;
        return true;
    }
    return false;
}


//*********************** string functions **********************

function displayDate(s) {
    // convert yyyymmdd to mm/dd/yyyy
    if (s.length>=6 && s.indexOf("/")<0 && s.indexOf(".")<0) {
	//verifycontains numbers only
	try {
	    var m = s.substring(4,6);
	    var d = s.substring(6);
	    if (d.length<2)
		d="0"+d;
	    var y = s.substring(0,4);
	    return (m+"/"+d+"/"+y);
	}
	catch (ex) {
	    return (ex);
	}
    }
    return (s);
}
function saveDate(s) {
    // convert from mm/dd/yyyy to yyyymmdd
    if (s.length>=6 && s.indexOf("/")>=0) {
	var n1 = s.indexOf("/");
	var n2 = s.indexOf("/",n1+1);
	if (n1>-1 && n2>-1) {
	    var y = (String(s)).substring(n2+1);
	    if (y.length<4)
		y="20"+y;
	    var m = (String (s)).substring(0,n1);
	    if (m.length<2)
		m="0"+m;
	    var d = (String(s)).substring(n1+1,n2);
	    if (d.length<2)
		d="0"+d;
	    return ((y)+(m)+(d));
	}
    }
    return (s);
}
function blankForm (form) {
    var prev_recid = form.RecID.value;
    for (var x=0; x<form.elements.length; x++) {
	if (form.elements[x].type == "button")
	    ;
	else if (form.elements[x].type=="checkbox")
	    form.elements[x].checked = false;
	else if (form.elements[x].type=="radio") {
	    for (var i=0; i<form.elements[x].length; i++)
		form.elements[x].checked = false;
	}
	else
	    form.elements[x].value="";
    }
    //showCurrentRecID();
    form.RecID.value="-1";
    return prev_recid;
}

function replaceStringAll(str, target, replacement) {
    var s = new String(str);
    var n = s.indexOf(target);
    while (n > -1) {
        var s1 = s.substring(0,n);
        var s2 = s.substring(n+target.length);
        s= new String(s1+s2);
        n=s.indexOf(target);
    }
    return (String(s));
}

function trim (str) {
    // remove leading and trailing whitespaces
    var s = new String(str);
    while (s.charAt(0)==' ' || s.charCodeAt(0)==10 || s.charCodeAt(0)==13 || s.charCodeAt(0)==0x4 || s.charCodeAt(0)==0x5)
        s = String(s).substring(1);
    while (s.charAt(s.length-1)==' ' || s.charCodeAt(s.length-1)==10 || s.charCodeAt(s.length-1)==13 || s.charCodeAt(0)==0x4 || s.charCodeAt(0)==0x5)
        s = String(s).substring(0,s.length-2);
    return (s);
}

function chomp (str) {
    // remove leading and trailing cr+lf
    var s = new String("");
    for (var x=0; x<str.length; x++)
        if (str.charCodeAt(x)!=0x10 && str.charCodeAt(x)!=0x13)
            s += str.charAt(x);
    return (s==null?"":s);
}

//****************** cookies **********************
function getCookieVal(offset) {
    var endstr=document.cookie.indexOf(";",offset);
    if (endstr==-1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset,endstr));
}
function getCookie (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 getCookieVal(j);
        }
        i=document.cookie.indexOf(" ",i)+1;
        if (i==0)
            break;
    }
    return null;
}
function getCookie2 (name){return getCookie(name);}
function checkCookie (name, path, domain) {
    if (getCookie(name)) {
		try {
			var a = document.cookie.split(";");
			alert (a[0]+"\n"+a[1]+"\n"+a[2]+"\n"+a[3]);
			/*
			document.cookie = name + "=" +
			((path) ? "; path=" + path :"") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Sun, 01-Jan-70 00:00:01 GMT";
			return true;
			*/
		}
		catch (ex) {
			alert (ex);
		}
    }
    return false;
}
function setCookie (name,value){setCookie2(name, value, null, "/", "umd.edu", false);}
function setSecureCookie (name,value){setCookie2(name, value, null, "/", "umd.edu", true);}
function setCookie2 (name, value, expires, path, domain, secure) {
    document.cookie=name+"="+escape(value)+
    ((expires) ? "; expires="+expires : "")+
    ((path) ? "; path=" +path : "")+
    ((domain) ? "; domain="+domain : "")+
    ((secure) ? "; secure" : "");
}
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
		try {
			document.cookie = name + "=" +
			((path) ? "; path=" + path :"") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Sun, 01-Jan-70 00:00:01 GMT";
			return true;
		}
		catch (ex) {
			alert (ex);
		}
    }
    return false;
}

//********************* MATH FUNCTIONS ***********************

function trunc(n,len) {
    var s = null;
    if (typeof n == "string")
        s = n;
    else
        s = Number(n).toString();
    return (parseInt(String(s).substr(0,len)));
}

function getPseudoRandom(max, exclude_list) {
    var places = Number(max).toString().length;
    var n = trunc(Math.random() * Math.pow(10,places),places);
    if (exclude_list) {
	do {
	    while (n  > max)
		n = trunc(Math.random() * Math.pow(10,places),places);
	} while (inList(n,exclude_list));
	exclude_list.push(n);
    }
    else {
	while (n  > max)
	    n = trunc(Math.random() * Math.pow(10,places),places);
    }
    return (n);
}

//************************** HTTP FUNCTIONS ********************
function getQueryString () {
    return (location.href.substring(location.href.indexOf("?")+1));
}

function getParameter (param_name) {
    var query_string = location.href.substring(location.href.indexOf("?")+1);
    var pname = null;
    if (query_string) {
        var plist = new Array();
    }
    return (pname);
}

//************* object tag functions ************************
// sample <object></object>
// <object name="content_obj" id="content_obj"  type="text/html" width="99%" height="99%" data="[url to dislay intially]"></object>
//
function changeObjectData (obj_container, obj_id, url) {
    var err = "";
    try {
	var o;
	if (typeof obj_id == "string")
	    o= document.getElementById(obj_id);
	else
	    o=obj_id;
	var o_parent;
	if (typeof obj_id=="string")
	    o_parent= document.getElementById(obj_container);
	else
	    o_parent=obj_container;
	// var url_path = o.data.replace(/[^\/]*$/, "");
	if (o && o_parent) {
	    var dolly = o.cloneNode(true);
	    try {
		dolly.data = url;
		try {
		    o_parent.removeChild(o);
		}
		catch (ex) {
		    changeObjectData.last_error_1=ex;
		}
		o_parent.appendChild(dolly);
		if (dolly.body && dolly.body.scroll)
		    dolly.body.scroll="no";
	    }
	    catch (ex) {
		err += "changeObjectData():\n"+ex;
	    }
	}
	else
	    err += "changeObjectData():\nERROR:: changing object data\n"+(o==null?"obj_id invalid.":"obj_container invalid.");
    }
    catch (ex) {
	err += ex;
    }
    return err;
}

// ************************ DOM functions ***************

function setHTMLContent(Elem,text) {
    // assign text to an element
    var e = Elem;
    if ((typeof "Elem").toUpperCase() == "STRING")
        e = document.getElementById(Elem);
    if (e) {
        // remove current content
        while (e.childNodes.length > 0) {
            e.removeChild(e.firstChild);
        }
        // add new content
        e.innerHTML=text;
    }
}

function insertIntoTextArea (text_area, insert_text) {
	if (text_area.selectionStart) {
		// firefox and other none-ie browsers
		var scrollPos = text_area.scrollTop;
		var startPos = text_area.selectionStart;
		var buff = String(text_area.value);
		text_area.value = buff.substring(0,startPos) + insert_text + buff.substring(startPos);
	}
}

function setText(Elem,text) {
    // assign text to an element
    var e = Elem;
    if ((typeof Elem).toUpperCase() == "STRING")
        e = document.getElementById(Elem);
    if (e) {
        var tnode = document.createTextNode(text);
        if (tnode) {
            while (e.childNodes.length > 0) {
		e.removeChild(e.firstChild);
            }
            e.appendChild(tnode);
        }
    }
}



//]]>


