/*
	Basic javascript utilities.
*/

/* show a container by setting its display style to on. */
function toggleDisplay () {
	var d;
	for (var i = 0; i < arguments.length; i++) {
		try {
			d = document.getElementById (arguments[i]);
			new Effect.toggle (d, 'blind');
			return;
		} catch (E) {}
	}
}

function _show () {
	var d;
	for (var i = 0; i < arguments.length; i++) {
	    try {
    	    d = document.getElementById (arguments[i]);
        	d.style.display = "";
	    } catch (E) { }
	}
}

function _hide () {
	var d;
	for (var i = 0; i < arguments.length; i++) {
	    try {
    	    var d = document.getElementById (arguments[i]);
        	d.style.display = "none";
	    } catch (E) { }
	}
}

/* 
    Set the visibility of this container.
*/
function visibile () {
	var d;
	for (var i = 0; i < arguments.length; i++) {
	    try {
    	    d = document.getElementById (arguments[i]);
        	d.style.visibility = "visible";
	    } catch (E) {}
	}
}

function invisibile (id) {
	var d;
	for (var i = 0; i < arguments.length; i++) {
	    try {
        	d = document.getElementById (arguments[i]);
    	    d.style.visibility = "hidden";
	    } catch (E) {}
	}
}

/*
    Disable this button.
*/
function _disable (button_id) {
    try {
        document.getElementById (button_id).disabled = true;
    } catch (E) {}
}

/*
    Enable this button.
*/
function _enable (button_id) {
    try {
        document.getElementById (button_id).disabled = false;
    } catch (E) {}
}

/* */
function dynamicButtonValue (button_id, default_text, new_text, timeout) {
    try {
        // set to new value.
        setButtonText (button_id, new_text);

        // set timeout for resetting text.
        if (timeout > 0) {
            setTimeout ("setButtonText ('" + button_id + "', '" + default_text + "');", timeout);
        }
    } catch (E) {}
}

/*
    Set the value of the button with id 'id'.
*/
function setButtonText (id, value) {
    try {
        var button = document.getElementById (id);
        button.value = value;
    } catch (E) {}
}


/*
    Deselect the selected index of this list.
*/
function deselect (id) {
    try {
        var s = document.getElementById (id);
        if (s.type == "select")
            s.selected = false;
    } catch (E) {}
}

/*
    Change the class of the given container.
*/
function changeContainerClass (id, newclass) {
    try {
        var d = document.getElementById (id);

        // get the current class name.
        var curr = d.getAttribute ("class");
        if (curr != "")
            curr = d.getAttribute ("className");

        /* set for gecko. */
        d.setAttribute ("class", newclass);

        /* set for ie. */
        d.setAttribute ("className", newclass);
        
        return curr;
    } catch (E) {}
}

/* set the container contents. */
function setContents (c, contents) {
	try {
		if (! isObject (c))
			c = document.getElementById (c);
		c.innerHTML = contents;
	} catch (E) { alert (E.toString()); }
}

/*
	Basic javascript utilities.
*/

/* trim a string of white space. */
function trim (str) {
	str += "";
	return str.replace(/^\s*|\s*$/g,"");
}

/* trim whitespace from the left of a string. */
function ltrim (str) {
    str += "";
    return str.replace (/^\s*[^\w]*/, "");
}

/* trim whitespace from the right of a string. */
function rtrim (str) {
    str += "";
    return str.replace(/\s*$/, "");
}

/*
    Check if the given needle is found in the haystack.
*/
function inArray (haystack, needle) {
    try {
        for (var i = 0; i < haystack.length; i++) {
            if (trim (haystack[i]) == trim (needle)) {
                return true;
            }
        }
    } catch (E) { }

    return false;
}

/*
    Return the directory name of the given path. This function 
    uses '/' as directory seperators.
*/
function dirname (path, seperator) {
    path = path + "";
    if (seperator == null)
        seperator = "/";

    if (path == "" || path == seperator)
        return seperator;

    var pieces = path.split (seperator);
    if (pieces.length <= 1)
        return path;

    var pathstr = "";
    for (var i = 0; i < pieces.length - 1; i++) {
        if (pathstr != "")
            pathstr += seperator;
        pathstr += pieces[i];
    }

    if (seperator.toString () == path.charAt(0).toString ())
        pathstr = seperator + pathstr;
   
 
    pathstr += seperator;
    return pathstr;
}

/*
    Swap an image src from on to off depending on what it current
    has set.
*/  
function toggleImageSrc (id, on, off) {
    try {
        var img = document.getElementById (id);
        if (img.src.match (".*?" + off))
            setImageSrc (id, on);
        else
            setImageSrc (id, off);

        return getImageSrc (id);
    } catch (E) { }
}

/*
    Set an image src attribute to the specified src parameter.
*/
function setImageSrc (id, src) {
    try {
        var img = document.getElementById (id);
        img.src = src;
    } catch (E) {}
}

function setAtt (id, att, val) {
	try {
		var d = document.getElementById (id);
		d.setAttribute (att, val);
	} catch (E) {}
}

/* 
    get the current image src to this img container. 
*/
function getImageSrc (id) {
    try {
        var img = document.getElementById (id);
        return img.src;
    } catch (E) {}
}

/*
    set the focus on the given container.
*/
function setFocus (given_id) {
    try {
        var inp = document.getElementById (given_id);
        inp.focus ();
    } catch (E) { }
}

/*
    Change the class of the given container.
*/
function changeContainerClass (id, newclass) {
    try {
        var d = document.getElementById (id);

        // get the current class name.
        var curr = d.getAttribute ("class");
        if (curr != "")
            curr = d.getAttribute ("className");

        /* set for gecko. */
        d.setAttribute ("class", newclass);

        /* set for ie. */
        d.setAttribute ("className", newclass);
        
        return curr;
    } catch (E) {}
}

/*
    Check all checkbox inputs that have the specified pattern in their id.
*/
function checkInputByPattern (pattern) {
    var inputs = document.getElementsByTagName ("input");
    var id = "";

    // check all inputs
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox") {
            id = inputs[i].id;
            if (id.match (pattern)) {
                // check this input.
                inputs[i].checked = true;
            } 
        }
    }   
}

/*
    Uncheck all checkbox inputs that have the specified pattern in their id.
*/
function unCheckInputByPattern (pattern) {
    var inputs = document.getElementsByTagName ("input");
    var id = "";

    // check all inputs
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox") {
            id = inputs[i].id;
            if (id.match (pattern)) {
                // uncheck this input.
                inputs[i].checked = false;
            } 
        }
    }   
}

/*
    Set the cookie value.
*/
function setCookie (key, value, expires_on) {
    var default_year_offset = "2"

    if (expires_on) {
        var expires = expires_on;
    } else {
        var expires = new Date ();
        expires.setFullYear (eval (expires.getFullYear () + default_year_offset));
    }

    // set our cookie value.
    document.cookie = key + "=" + value + "; expires=" + expires.toGMTString ();
}

/*
    Get the cookie value.
*/
function getCookie (key) {
    var cookies = getCookies ();
    return cookies[key];
}

/*
    get all current cookies into an associtive array.
*/
function getCookies () {
    var cookies = new Object ();
    var name, value;
    var beginning, middle, end;
    for (name in cookies) {
        cookies = new Object ();
        break;
    }

    beginning = 0;
    while (beginning < document.cookie.length) {
        middle = document.cookie.indexOf ('=', beginning);
        end = document.cookie.indexOf (';', beginning);

        if (end == -1)
            end = document.cookie.length;
        if ((middle > end) || (middle == -1)) {
            name = document.cookie.substring (beginning, end);
            value = "";
        } else {
            name = document.cookie.substring (beginning, middle);
            value = document.cookie.substring (middle + 1, end);
        }

        cookies[name] = unescape (value);
        beginning = end + 2;
    }

    return cookies;
}

/*
    Display an inline popup container that is attached onto the given
    container.
*/
function print_hover_popup (title, body, title_class, body_class) {
    document.write ("<div style=\"\"></div>");

    

    if (title)
        document.write ("<div style=\"background-color: #fff; color: #000; border-bottom: 1px solid;\" class=\"" + title_class + "\">" + title + "</div>");

    if (body)
        innerHTML += "<div class=\"" + body_class + "\">" + body + "</div>";

    if (innerHTML != "") {
        var outerDiv = "<div";
        innerHTML = "<div style=\"position: absolute; z-index: 101; display: none;\">" + "<div style=\"background-color: white; color: black;\">" + innerHTML + "</div></div>";

        
    }

}

/* open a window in full screen. */
function openWindow_fs (url) {
	var width = 500;
	var height = 350;
	var x = 0;
	var y = 0;

	if (screen.availWidth) { width = screen.availWidth; }
	if (screen.availHeight) { height = screen.availHeight; }
	if (screen.availLeft) { x = screen.availLeft; }
	if (screen.availTop) { y = screen.availTop; }

	var popupWin = window.open(url,'remote_fs','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent');
	try {
		popupWin.resizeTo (width, height);
	} catch (E) {
		popupWin.outerHeight = height;
		popupWin.outerWidth = width;
	} finally { /* cannot seem to resize the new window. */ }

	popupWin.moveTo (x,y);
}

/* open a window at the size given or to 100%. */
function openWindow_auto (url, width, height) {
	var x = 0;
	var y = 0;

	if (width > screen.availWidth) { width = screen.availWidth; }
	if (height > screen.availHeight) { height = screen.availHeight; }
	if (screen.availLeft) { x = screen.availLeft; }
	if (screen.availTop) { y = screen.availTop; }

	var popupWin = window.open(url,'remote_auto','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent');
	try {
		popupWin.resizeTo (width, height);
	} catch (E) {
		popupWin.outerHeight = height;
		popupWin.outerWidth = width;
	} finally { /* cannot seem to resize the new window. */ }

	popupWin.moveTo (x,y);
}

function openWindow(url) { 
    popupWin = window.open(url,'remote','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=500,height=350,left=50,top=50');
}

function openWindowSmall (url) {
    popupWin = window.open(url,'remote','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=600,height=350,left=50,top=50');
}

function openWindowLarge (url) {
    popupWin = window.open(url,'remote','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=600,height=500,left=50,top=50');
}

function openWindow_resize (url) {
    popupWin = window.open(url,'remote','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=600,height=350,left=50,top=50');
}

function openWindow_Size (url, width, height) {
    popupWin = window.open(url,'remote','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=' + width + ',height=' + height);
}

function openNamedWindow (url, name) {
    var width, height;
    width = 600;
    height = 410;
    popupWin = window.open(url, name,'menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=' + width + ',height=' + height + ',left=50,top=50');
}

function openNamedWindow_Size (url, name, width, height) {
    popupWin = window.open(url, name,'menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',left=50,top=50');
}

function openGenericWindow (url, name, options) {
    popWin = window.open (url, name, options);
}

    /*
        Populate the select list with the given items array.
    */
    function populateSelectList (id, values, clearfirst) {
        if (clearfirst) {
            clearSelectList (id);
        }

        var s = document.getElementById (id);
        var node = null;
        for (var i = 0; i < values.length; i++) {

            values[i] = trim (values[i]);
            if (values[i] != "") {
                node = document.createElement ("option");

                // set some values on this option.
                node.text = values[i];
                node.value = values[i];
                node.setAttribute ("innerText", values[i]);
                node.setAttribute ("value", values[i]);
                s.appendChild (node);
                node = null;
            }
        }

        values = null;
    }

    /*
        Clear the select list options.
        Note that this may be a slow operation on large lists.
    */
    function clearSelectList (id) {
        var s = document.getElementById (id);
s.innerHTML = "";
return;
        try {
            while (s.length > 0) {
                s.removeChild (s[0]);
                s = document.getElementById (id);
            }
        } catch (E) {}
    }

	/*
		set the opacity of the given id to the given value.
	*/
	function setOpacity (id, opa) {
		try {
			var obj = document.getElementById (id);
			obj.style.opacity = opa;

			// set for ie
			if (!obj.style.filter) {
				obj.style.filter = "alpha (opacity=100)";
			}

			obj.filters.item("alpha").opacity = parseInt (opa * 100);
		} catch (E) { }
	}

	// fade into the max opacity value for the given id.
	function fadeIn (id, maxopa) {
		try {
			var co = getOpacity (id);
			var newopa = eval (opa + 0.05);
			if (co < maxopa)
				setTimeout ("fadeIn ('" + id +"', " + newopa + ");", 20);
			else
				setTimeout ("setOpacity ('" + id + "', 1.00);", 21);
		} catch (E) {}
	} 

	function fadeOut (id, minopa) {
		try {
				var co = getOpacity (id);
			var newopa = co - 0.05;
			setOpacity (id, newopa);
			if (co > minopa && newopa > 0)
				setTimeout ("fadeOut ('" + id + "', " + newopa + ");", 20);
			return;
		} catch (E) {}
	}

	// get the current opacity value for this item.
	function getOpacity (id) {
		// kinda cludgy as we are setting the opacity of this item with set opacity.
		var obj = document.getElementById (id);
		if (! obj.filters)
			return obj.style.opacity;
		return (obj.filters.item("alpha").opacity / 100);
	}

    function getCheckedList (name) {
        var c = "";
        var inputs = document.getElementsByTagName (name);

        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == "checkbox" && inputs[i].checked) {
                if (c != "")
                    c += "," + inputs[i].value;
                else
                    c = inputs[i].value;
            }
        }     

        return c;
    }

	/*
		check if the needle value is found in haystack.
	*/
	function in_array (needle, haystack) {
		try {
			var useCase = false;
			if (args[2]) {
				useCase = true;
				needle = needle.toLowerCase ();
			}

			var temp = "";
			for (var i = 0; i < haystack.length; i++) {
				if (useCase) {
					temp = haystack[i].value.toLowerCase ();
				} else {
					temp = haystack[i].value;
				}

				if (temp == needle) {
					return true; 
				}
			}

			return false;
		} catch (E) {
			return false;
		}	
	}

	// remove any dupesdupes
	function makeSelectListUnique (select_id) {
		try {
			var select_list = document.getElementById (select_id);

			for (var i = 0; i < select_list.length; i++) {
				for (var j = i+1; j < select_list.length; i++) {
					if (select_list[i].value == select_list[j].value && i < j)
						select_list.removeChild (select_list[j]);
				}
			}
		} catch (E) { alert (E.toString ()); }
	}

	// remove the given value(s) from the select list.
	function removeOptionFromSelect (select_id, value) {
		try {
			var select_list = document.getElementById (select_id);

			for (var i = 0; i < select_list.length; i++) {
				if (select_list[i].value == value) {
					select_list.removeChild (select_list[i]);
				}	
			}
		} catch (E) { 
			return false;
		}	
	}

	function getSelectValuesAsString (select_id) {
		try {
			var select_values = document.getElementById (select_id);

			var str = "";
			var temp = "";
			for (var i = 0; i < select_values.length; i++) {
				temp = select_values[i].value;
				if (temp != "") {
					if (str != "")
						str += ",";

					str += temp;	
				}
			}

			return str;
		} catch (E) { }
	}

	function getSelectedValue (select_id) {
		try {
			var select_values = document.getElementById (select_id);
		 	var str = select_values[select_values.selectedIndex].value;
			return str;
		} catch (E) {
			return "";
		}
	}

	/* 
		move a select option value from one list to the other. 
		the selected item(s) in 'from' list is moved to the 'to' list.
	
	*/
	function moveSelectedToList (from, to) {
		try {
			var fromlist = document.getElementById (from);
			var tolist = document.getElementById (to);

			if (fromlist.tagName.toLowerCase () != "select" || tolist.tagName.toLowerCase () != "select")
				return 0;

			tolist.appendChild (fromlist[fromlist.selectedIndex]);
		} catch (E) {
			return 0;
		}
	}

	function overlib_info (title, desc, position) {
		return overlib (desc, CAPTION, title, position, CSSCLASS,TEXTFONTCLASS,'ol_fontClass',FGCLASS,'ol_fgClass', BGCLASS,'ol_bgClass',CAPTIONFONTCLASS,'ol_capfontClass', CLOSEFONTCLASS, 'ol_capfontClass'); 
	}
	
	function setCaretTo(obj, pos) { 
	    if(obj.createTextRange) { 
	        /* Create a TextRange, set the internal pointer to
	           a specified position and show the cursor at this
	           position
	        */ 
	        var range = obj.createTextRange(); 
	        range.move("character", pos); 
	        range.select(); 
	    } else if(obj.selectionStart) { 
	        /* Gecko is a little bit shorter on that. Simply
	           focus the element and set the selection to a
	           specified position
	        */ 
	        obj.focus(); 
	        obj.setSelectionRange(pos, pos); 
	    } 
	}	
	
	/* load flash player. */
	function loadFLVplayer (id, file_url) {
		try {
			alert ('start');
			var FU = { 	movie:"flash/mediaplayer.swf",width:"400",height:"330",majorversion:"7",build:"0",bgcolor:"#FFFFFF",allowfullscreen:"true",
						flashvars:"file=" + file_url + "?autostart=true" };
			UFO.create(	FU, id);
		} catch (E) {
			document.getElementById (id).innerHTML = "Sorry, the requested file does not exist.";
		}
	}

	function isObject(a) {
		return (typeof a == 'object' && !!a) || isFunction(a);
	}
	
	function isFunction(a) {
		return typeof a == 'function';
	}	
	
	
	
	
	
	
	
	
	
	