function setAllCheckBoxes(objCheckBoxes, checkValue) {
	if (objCheckBoxes && objCheckBoxes.length != null) {
	    var countCheckBoxes = objCheckBoxes.length;
	    if(!countCheckBoxes) {
	    	if (!objCheckBoxes.disabled) {
		        objCheckBoxes.checked = checkValue;
		    }
	    } else {
	        // set the check value for all check boxes
	        for(var i = 0; i < countCheckBoxes; i++) {
	        	if (!objCheckBoxes[i].disabled) {
		            objCheckBoxes[i].checked = checkValue;
				}
	        }
	    }
	}
}

function limitCount(fromObj, counter, maxLen) {
	if (fromObj) {
		if (fromObj.value.length > maxLen) {
			fromObj.value = fromObj.value.substring(0, maxLen);
		}
		
		if (counter) {
			counter.innerHTML = maxLen - fromObj.value.length 
				+ "&nbsp;characters remaining";
		}
	}
}

//Add an onload event onto the window
function addOnloadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
	    					oldonload();
	    					func();
	    				}
	}
}

//Add an onUnload event onto the window
function addOnUnloadEvent(func) {
	var oldUnOnload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload = function() {
	    					oldUnOnload();
	    					func();
	    				}
	}
}

//Add an onUnload event onto the form
function addOnSubmitEvent(frm, func) {
	var oldOnSubmit = frm.onsubmit;
	if (typeof frm.onsubmit != 'function') {
		frm.onsubmit = func;
	} else {
		frm.onsubmit = function() {
						oldOnSubmit();
						func();
					}
	}
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a 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));
}

/**
 * Trims white spaces before and after a string
 */
function trim(str) {
	return str.replace(/^\s*|\s*$/g, "");
}

/**
 * Round the input to the numDecimalPlaces
 */
function round(input, numDecimalPlaces) {
	var multiplier = 1;
	for(var ii=0; ii<numDecimalPlaces; ii++) {
		multiplier *= 10;
	}
	return Math.round(input * multiplier) / multiplier;
}

var m_names = new Array("January", "February", "March", 
	    "April", "May", "June", "July", "August", "September", 
	    "October", "November", "December");
	    
var d_names = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

	    
function getFormattedDate(d) {
	var curr_date = d.getDate();
	var curr_month = d.getMonth();
	var curr_year = d.getFullYear();
	var curr_day = d.getDay();
	return (d_names[curr_day] + ", " + m_names[curr_month] + ", " + curr_date + " " + curr_year);
}

function getFormattedTime(d) {
	var hours = d.getHours();
	var minutes = d.getMinutes();
	var seconds = d.getSeconds();
	var am = "AM"
	
	if (minutes < 10) {
	    minutes = "0" + minutes;
	}
	if (seconds < 10) {
	    seconds = "0" + seconds;
	}
	if (hours >= 12) {
	    am = "PM"
	}	
	if (hours > 12) {
	    hours -= 12;
	}
	return hours + ":" + minutes + ":" + seconds + am;
}

function getFormattedDateTime(d) {
	return getFormattedDate(d) + " " + getFormattedTime(d);
}

//StringBuffer
function StringBuffer() { 
    this.buffer = []; 
}
 
StringBuffer.prototype.append = function(string) { 
    this.buffer.push(string); 
    return this; 
}

StringBuffer.prototype.toString = function() {
    return this.buffer.join("");
}

/**
 * Gets the y coord of an object, given the object
 */
function getXCoord(obj) {
	var xCoord = 0;
	
	if (obj) {
		xCoord = obj.offsetLeft;
		var tempEl = obj.offsetParent;
		while (tempEl != null) {
			xCoord += tempEl.offsetLeft;
			tempEl = tempEl.offsetParent;
		}
	}
	return xCoord;
}

/**
 * Gets the y coord of an object, given the object
 */
function getYCoord(obj) {
	var yCoord = 0;
	
	if (obj) {
		yCoord = obj.offsetTop;
		var tempEl = obj.offsetParent;
		while (tempEl != null) {
			yCoord += tempEl.offsetTop;
			tempEl = tempEl.offsetParent;
		}
	}
	return yCoord;
}

function expand(objId) {
    var obj = document.getElementById(objId);
    if (obj) {
        obj.style.overflow = "visible";
    }
}

function collapse(objId) {
    var obj = document.getElementById(objId);
    if (obj) {
        obj.style.overflow = "auto";
    }
}

function checkChanged(objSender) {
    var objParent = objSender.offsetParent;
    if (objParent && objParent.nodeName == "TD") {
        if (objSender.checked) {
            objParent.className = "selected_checkbox";
        } else {
            objParent.className = "textmain";
        }
    }
}

/**
 * Returns true if it's MS IE
 */
function isIE() {
	var ua = window.navigator.userAgent.toUpperCase();
	return (ua.indexOf("MSIE") > 0);
}

function handleSearchWebsiteOnKeyDown(objSender) {
    if (event.which || event.keyCode) {
        if ((event.which == 13) || (event.keyCode == 13)) {
            doSearchWebsite();
            return false;
        }
    } else {
        return true;
    } 
}

function doSearchWebsite() {
    __doPostBack("SEARCH_WEBSITE", null);
}

function fieldOnFocus(field, defaultValue) {
	var dValue = defaultValue;
	if (!defaultValue) {
		dValue = field.defaultValue;
	}
	
	if (dValue == field.value) {
		field.value = "";
	}
}

function fieldOnBlur(field, defaultValue) {
	var dValue = defaultValue;
	if (!defaultValue) {
		dValue = field.defaultValue;
	}
	
	if (field.value == "") {
		field.value = dValue;
	}
}

//Audio player links
var nowPlayingLink = null;
function playAudio(objSender, objPlayerDiv, path, title, objCaptionSpan) {
    if (nowPlayingLink != null) {
        nowPlayingLink.innerHTML = "Listen&nbsp;Now!";
        nowPlayingLink.className = "red";
    }
    
    nowPlayingLink = objSender;
    objSender.innerHTML = "Now&nbsp;Playing";
    objSender.className = "emphasis";
    
    path = "resources/" + path;
    
	objPlayerDiv.innerHTML = "<object width='450' height='65' classid='CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6' id='winPlayer'><param name='URL' value='" + path + "'/><param name='AutoStart' value='True'/><param name='ShowControls' value='False'/><param name='ShowStatusBar' value='True'/><param name='ShowDisplay' value='False'/><param name='AutoRewind' value='True'/><embed type='application/x-mplayer2' pluginspage='http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/' width='450' height='65' src='" + path + "' filename='" + path + "' autostart='True' showcontrols='True' showstatusbar='True' showdisplay='False' autorewind='True'></embed></object>";
	
	if (objCaptionSpan) {
		objCaptionSpan.innerHTML = "Now playing &quot;" + title + "&quot;";
    }
}

function playAlbum(objSender, objPlayerDiv, asxPath, albumTitle, objCaptionSpan) {
    if (!isIE()) {
        alert("This feature is only available in Microsoft Internet Explorer");
    } else {
        if (nowPlayingLink != null) {
            nowPlayingLink.innerHTML = "Listen&nbsp;Now!";
            nowPlayingLink.className = "red";
        }
        
        asxPath = "resources/" + asxPath;
	    objPlayerDiv.innerHTML = "<object width='450' height='65' classid='CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6' id='winPlayer'><param name='URL' value='" + asxPath + "'/><param name='AutoStart' value='True'/></object>";
	    
        if (objCaptionSpan) {
            objCaptionSpan.innerHTML = "Now playing all tracks from &quot;" + albumTitle + "&quot;";
        }
    }
}

function saveCurUrl(dest) {
    var val = dest;
    if (val && val.len > 0) {
        
    } else {
        val = window.location;
    }
    var c = "last_url=" + val + "; path=/";
    document.cookie = c;
}

function saveCurUrlThenRedirect(dest) {
    saveCurUrl(dest);
    window.location = dest;
}

addOnloadEvent(saveCurUrl);

function copyToClipboard(s) {
	if( window.clipboardData && clipboardData.setData ) {
		clipboardData.setData("Text", s);
	} else {
		// You have to sign the code to enable this or allow the action in about:config by changing
		user_pref("signed.applets.codebase_principal_support", true);
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

		var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;

		// create a transferable
		var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;

		// specify the data we wish to handle. Plaintext in this case.
		trans.addDataFlavor('text/unicode');

		// To get the data from the transferable we need two new objects
		var str = new Object();
		var len = new Object();

		var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);

		var copytext=meintext;

		str.data = copytext;

		trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);

		var clipid = Components.interfaces.nsIClipboard;

		if (!clip) return false;

		clip.setData(trans, null, clipid.kGlobalClipboard);	   
	}
}