/**
* Prepare formular fields for hint tooltips.
*/

$(document).ready(function () {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			if(inputs[i].className == "text") {
				// the span exists!  on focus, show the hint
				inputs[i].onfocus = function () {
					if(this.parentNode.getElementsByTagName("span")[0].className == 'hint') {
						this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
					}

					if(this.value == this.getAttribute("defaultvalue")) this.value = "";
				}
				// when the cursor moves away from the field, hide the hint
				inputs[i].onblur = function () {
					if(this.parentNode.getElementsByTagName("span")[0].className == 'hint') {
						this.parentNode.getElementsByTagName("span")[0].style.display = "none";
					}
				}
			}
		}
	}

	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				if(this.parentNode.getElementsByTagName("span")[0].className == 'hint') {
					this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				}
			}
			selects[k].onblur = function () {
				if(this.parentNode.getElementsByTagName("span")[0].className == 'hint') {
					this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				}
			}
		}
	}
	
	(document.all && !window.print) ? null : replace_select_boxes();
});


/**
 * Default selectboxes are not stylable in IE 
 * so we need a hack here.
 */
function replace_select_boxes() {
	jQuery('body').find("select.selectbox").each(function() {
		$(this).selectbox({debug: false});
	});
}


/**
 * Popup.
 */
function display_popup(url, name, width, height, directories, toolbar, menubar, scrollbars, status, resizable, dependent, fullscreen) {
	var args = "width="+ width +",height="+ height;
	if(directories == true) args += ",directories"
	if(toolbar == true) args += ",toolbar"
	if(menubar == true) args += ",menubar"
	if(scrollbars == true) args += ",scrollbars"
	if(status == true) args += ",status"
	if(resizable == true) args += ",resizable"
	if(dependent == true) args += ",dependent"
	if(fullscreen == true) args += ",fullscreen"
	
	args += ",resizable"
	
	window.open(url, name, args);
}


/**
 * Copy to clipboard
 */
function copy_to_clipboard(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);	   
	}
}
