﻿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;
}

/**
 * Returns true if it's MS IE
 */
function isIE() {
	var ua = window.navigator.userAgent.toUpperCase();
	return (ua.indexOf("MSIE") > 0);
}

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;
	}
}

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);	   
	}
}

function doPopup(url, height, width) {
    if (url) {
        if (!width) {
            width = 500;
        }
        
        if (!height) {
            height = 500;
        }
        var w = window.open(url, "none", "width=" + width + ",height=" + height + ",location=no, scrollbars=yes");
        w.focus();
	    return false;
    }
}

function scrollToObjectId(objId) {
    var obj = document.getElementById(objId);
    if (obj) {
        var yPosition = getYCoord(obj);
        setTimeout("window.scrollTo(0, " + yPosition + ");", 300);
    }
}
