// limits the counter
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";
		}
	}
}

// For saving the last scrolling before postback
function getScrollX() {
	var scrollX = 0;
	if (document.all) {
		if (!document.documentElement.scrollLeft) {
			scrollX = document.body.scrollLeft;
		} else {
			scrollX = document.documentElement.scrollLeft;
		}
	} else {

		scrollY = window.pageXOffset;
	}
	
	return scrollX;
}

// For saving the last scrolling before postback
function getScrollY() {
	var scrollY = 0;
	if (document.all) {
		if (!document.documentElement.scrollTop) {
			scrollY = document.body.scrollTop;
		} else {
			scrollY = document.documentElement.scrollTop;
		}
	} else {
		scrollY = window.pageYOffset;
	}
	
	return scrollY;
}

function centerObj(obj, width, height) {
	if(obj) {
		var scrollX = getScrollX();
		var scrollY = getScrollY();

		var pageLength = Math.floor(document.body.clientHeight, 0);
		var pageWidth = Math.floor(document.body.clientWidth, 0);

		var offSetY = Math.floor(pageLength / 2) - Math.floor(height / 2);
		var offSetX = Math.floor(pageWidth / 2) - Math.floor(width / 2);
		
		if (!isNaN(scrollY) && scrollY != "") {
			offSetY += parseInt(scrollY);
		}

		if (!isNaN(scrollX) && scrollX != "") {
			offSetX += parseInt(scrollX);
		}

		obj.style.top = offSetY;
		obj.style.left = offSetX;
	}
}

function getPageSizeWithScroll() {     
	if (window.innerHeight && window.scrollMaxY) {
		// Firefox         
		yWithScroll = window.innerHeight + window.scrollMaxY;         
		xWithScroll = window.innerWidth + window.scrollMaxX;     
	} else if (document.body.scrollHeight > document.body.offsetHeight) { 
		// all but Explorer Mac         
		yWithScroll = document.body.scrollHeight;         
		xWithScroll = document.body.scrollWidth;     
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari         
		yWithScroll = document.body.offsetHeight;         
		xWithScroll = document.body.offsetWidth;       
	}     
	
	arrayPageSizeWithScroll = new Array(xWithScroll, yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll; 
}


//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 onresize event onto the window
function addOnResizeEvent(func) {
	var oldFunc = window.onresize;
	if (typeof window.onresize != 'function') {
		window.onresize = func;
	} else {
		window.onresize = function() {
	    					oldFunc();
	    					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 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 playFlashAudio(objSender, locationId) {
    var objLocation = document.getElementById(locationId);
    if (objLocation) {
        var filePath = objSender.href;
        var so = new SWFObject('flash/player.swf', 'ply', '470', '24', '9', '#ffffff'); 
        so.addParam('allowfullscreen', 'true'); 
        so.addVariable('autostart', 'true'); 
        so.addParam('allowscriptaccess', 'always'); 
        so.addParam('wmode', 'opaque'); 
        so.addVariable('file', filePath); 
        so.write(locationId);
        
        return false;
    }
    return true;
}

function openWindow(url, width, height) {
    var w = window.open(url, "none", "width=" + width + ",height=" + height + ",location=no, scrollbars=yes");
    w.focus();
    return false;
}

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);	   
	}
}
