
// Macromedia utilities

function MM_goToURL() { //v3.0
	var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
	for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
	var obj = MM_findObj(objName);
	if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
		if (theValue == true || theValue == false)
			eval("obj."+theProp+"="+theValue);
		else eval("obj."+theProp+"='"+theValue+"'");
	}
}

function MM_callJS(jsStr) { //v2.0
	return eval(jsStr)
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_nbGroup(event, grpName) { //v6.0
	var i,img,nbArr,args=MM_nbGroup.arguments;
	if (event == "init" && args.length > 2) {
		if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
			img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
			if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
			nbArr[nbArr.length] = img;
			for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
				if (!img.MM_up) img.MM_up = img.src;
				img.src = img.MM_dn = args[i+1];
				nbArr[nbArr.length] = img;
		} }
	} else if (event == "over") {
		document.MM_nbOver = nbArr = new Array();
		for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
			if (!img.MM_up) img.MM_up = img.src;
			img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
			nbArr[nbArr.length] = img;
		}
	} else if (event == "out" ) {
		for (i=0; i < document.MM_nbOver.length; i++) {
			img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
	} else if (event == "down") {
		nbArr = document[grpName];
		if (nbArr)
			for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
		document[grpName] = nbArr = new Array();
		for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
			if (!img.MM_up) img.MM_up = img.src;
			img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
			nbArr[nbArr.length] = img;
	} }
}

// local utilities

/**
* @version 1.2
*/
function showElement(whichElement)
{
	o = vem_GetObj(whichElement);
	if (o) { /*alert('showing '+whichElement+' -> '+o.style.display);*/ o.style.display = "block"; } //else { alert('Trying to show non-existent object '+whichElement); }
}

/**
* @version 1.1
*/
function hideElement(whichElement) {
	o = vem_GetObj(whichElement);
	if (o) { /*alert('hiding '+whichElement+' -> '+o.style.display); */o.style.display = "none"; } //else { alert('Trying to hide non-existent object '+whichElement); }
}

/**
 * Inverse the visiblity of the specifed object
 *
 * @param	object	the object to show or hide
 * @author	greg
 * @version	1.0
 */

function toggleVisibility(obj)
{
	obj = vem_GetObj(obj);
	if (obj) {
		if (obj.style.display == "none") {
			obj.style.display = "block";
			return (true);
		} else {
			obj.style.display = "none";
			return (false);
		}
	}
}

// v 1.0
var moving = undefined;

/**
 * Make the browser go to a new location
 *
 * @param	string	the URL to go to
 * @return	boolean	always return false (don't rely on it)
 * @author	greg
 * @version	1.0
 */

function moveto(url)
{
	if (url && (moving == undefined)) {
		moving = url;
		MM_goToURL('parent','file:///C|/WORK/proj/Sites/Site_FHF/htdocs/url');
	}

	return (false);
}


/**
 * Inverse the expeansion of the specifed panel
 *
 * @param	object	the panel to open or close
 * @param	object	the button for opening
 * @param	object	the button for closing
 * @author	greg
 * @version	1.1
 */

function toggleElement(panel, openbtn, closebtn)
{
	if (vem_GetObj(panel).style.display == "none") {
		expand(panel, openbtn, closebtn);
		return (true);
	} else {
		collapse(panel, openbtn, closebtn);
		return (false);
	}
}

/**
 * Change the visibility of the specified object
 *
 * @param	condition	the condition for the object to be made visible
 * @param	object	the object to show or hide
 * @author	greg
 * @version	1.1
 */

function showHide(condition, panel)
{
	if (condition) { showElement(panel); } else { hideElement(panel); }
}

function expand(panel, openbtn, closebtn)
{
	showElement(panel);hideElement(openbtn);showElement(closebtn);
}

function collapse(panel, openbtn, closebtn)
{
	hideElement(panel);showElement(openbtn);hideElement(closebtn);
}

var overWho;


// v 1.0
function hilite_CellLine(whichObject)
{
	overWho = whichObject.className;
	whichObject.className = "FHFMouseOverTR";
}

function unhilite_CellLine(whichObject)
{
	whichObject.className = overWho;
}


// v 1.0
function synch_checkboxes(master, slave)
{
	if (MM_findObj(master).checked) {
		MM_changeProp(slave,'','checked',1);
		MM_changeProp(slave,'','disabled',1);
	} else {
		MM_changeProp(slave,'','disabled',0);
	}
}


/*var frmt = parseDateFormat("dddd d mmmm yyyy");*/

function fetch_date(field)
{
	result = '';
	//field = get_innertext(fieldname);
	/*field = MM_findObj(fieldname).value;
	if (field) {
		date = parseDate(field, frmt);
		if (date) result = Date.parse(date);
	}*/

	//alert(fieldname+' ('+field + ') -> '+result);

	value = vem_GetValue(field);
	if (value.match(/^.+ (\d{1,2}) (\S+) (\d\d\d\d)$/)) {
		for (m = 12;m;m--) { if (Calendar._MN[m-1] == RegExp.$2) break; }
		if (m > 0) {
			d = RegExp.$1; y = RegExp.$3;
			if ((d > 0) && (d < 32) && (y > 1970)) result = new Date(y,m-1,d);
		}
	}

	return result;
}

function format_date(dt)
{
	d = new Date(dt);
	result = Calendar._DN[d.getDay()] + ' ' + d.getDate() + ' ' + Calendar._MN[d.getMonth()] + ' ' + d.getFullYear();
	return result;
}

/**
* @version 1.1
*/
function get_innertext(ob_name)
{
	obj = vem_GetObj(ob_name);
	if (obj.innerText) { return obj.innerText; }
	if (obj.textContent) { return obj.textContent; }
}

/**
* @version 1.1
*/
function set_innertext(ob_name, text)
{
	obj = vem_GetObj(ob_name);
	if (obj) {
		if (obj.innerText != undefined) { obj.innerText = text; }
		if (obj.textContent != undefined) { obj.textContent = text; }
	} //else { alert('Trying to set text of non-existent object '+ob_name); }
}

/**
* @version 1.1
*/
function set_innerhtml(ob_name, text)
{
	obj = vem_GetObj(ob_name);
	if (obj) {
		if (obj.innerHTML != undefined) { obj.innerHTML = text; }
		//if (obj.textContent != undefined) { obj.textContent = text; }
	} //else { alert('Trying to set text of non-existent object '+ob_name); }
}


function getradiovalue(control)
{
	value = '';
	for(i = (control.length) - 1 ;i >= 0;i--) {
		el = control[i];
		if ((typeof(el) == 'object') && el.checked) { value = parseInt(i) + 1; }
	}
	return value;
}

// v 1.2
function vem_GetRadioValue(control)
{
	value = '';
	if (typeof(control) == 'string') control = document.getElementsByName(control);
	if (control) {
		for(i = (control.length) - 1 ;i >= 0;i--) {
			el = control[i];
			if ((typeof(el) == 'object') && el.checked) { value = el.value; }
		}
	}
	return value;
}

// v 1.1
function vem_SetRadioValue(control, value)
{
	value = '';
	if (typeof(control) == 'string') {
		control = document.getElementsByName(control);
		if (control) for(i = (control.length) - 1 ;i >= 0;i--) {
			el = control[i];
			if (typeof(el) == 'object') { el.checked = (value == parseInt(i) + 1); }
		}
	}
	return value;
}

// v 1.2
function vem_GetRadioIndex(control)
{
	index = '';
	if (typeof(control) == 'string') {
		control = document.getElementsByName(control);
		for(i = (control.length) - 1 ;i >= 0;i--) {
			el = control[i];
			if ((typeof(el) == 'object') && el.checked) { index = parseInt(i) + 1; }
		}
	}
	return index;
}

// v 1.1
function vem_SetRadioIndex(control, index)
{
	if (typeof(control) == 'string') {
		control = document.getElementsByName(control);
		if (control) for(i = (control.length) - 1 ;i >= 0;i--) {
			el = control[i];
			if (typeof(el) == 'object') { el.checked = (index == parseInt(i) + 1); }
		}
	}
	return index;
}

function changeProp(objName,x,theProp,theValue) {
	var obj = MM_findObj(objName);
	if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
		if (theValue == '') {
		eval("obj."+theProp+"=''");
	} else if (theValue == true || theValue == false)
			eval("obj."+theProp+"="+theValue);
		else eval("obj."+theProp+"='"+theValue+"'");
	}
}


function checkhour(s_field, t_field, value)
{
	var digits = new RegExp("[0-9]");
	var result = '';
	var input = s_field.value;
	var len = input.length;
	var h = '';

	for(x = 0; x < len; x++) {
		if (digits.test(input.charAt(x))) { result = result + input.charAt(x); }
	}

	len = result.length;

	if (len == 0) { h = '-'; }
	if (len == 1) { h = '0' + result + 'h 00 m'; }
	else if (len == 2) {
		if (parseInt(result) > 12) {
			m = result.substring(1);
			if (parseInt(m) < 6) {
				h = '0' + result.substring(0, 1) + 'h ' + m + '0 m';
			}
		}
		else { h = result + 'h 00 m'; }
	}
	else if (len == 3) {
		if (parseInt(result.substring(0, 2)) > 12) {
			m = result.substring(1);
			if (parseInt(m) < 60) {
				h = '0' + result.substring(0, 1) + 'h ' + m + ' m';
			}
		}
	}
	else if (len == 4) {
			t = parseInt(result.substring(0, 2));
			m = parseInt(result.substring(2));
			if ((t < 24) && (m < 60)) { h = t + 'h ' + m + ' m'; }
	}

	if (h) {
		value = result;
		set_innertext(t_field, h);
	}
	s_field.value = value;

	return value;
}


/**
* @version 1.2
*/
function vem_GetObj(obj)
{
	if (typeof(obj) == 'string') {
		o = document.getElementById(obj);
		if (o) obj = o;
		else obj = document.getElementsByName(obj)[0];
	}
	return obj;
}

function SetValue(objname, val)
{
	vem_SetValue(objname, val);
}

/**
* @version 1.0
*/
function vem_SetSrc(obj, val)
{
		return vem_SetProp(obj, 'src', val);
}

/**
* @version 1.2
*/
function vem_SetValue(obj, val)
{
		return vem_SetProp(obj, 'value', val);
}

/**
* @version 1.0
*/
function vem_SetProp(objname, prop, val)
{
		obj = vem_GetObj(objname);
		r = '?';
		/*if (obj) { obj.prop = val; return obj.prop; } else { return '?'; }*/
		if (obj) eval('r=obj.'+prop+'=val;');
		return r;
}

/**
* @version 1.1
*/
function vem_GetValue(obj)
{
	obj = vem_GetObj(obj);
	if (obj) { return(obj.value); }  else { return '?'; }
}

function GetValue(obj)
{
	return vem_GetValue(obj);
}

function vem_GetCheckBox(obj, idx)
{
	result = false;
	if (!idx) idx = 0;

	if (typeof(obj) == 'string')  {
		obj = document.getElementsByName(obj);
		if (obj) result = obj[idx].checked;
	}

	return result;
}

function vem_clickAndWait(who)
{
	who.form.submit();
	who.value='Patientez...';
	who.disabled=true;
}

function vem_AlterStyle(obj, style)
{
	obj = vem_GetObj(obj);

	if (obj.styleSheet) obj.styleSheet.cssText = style;
	else obj.innerHTML = style;
}



/*

1: logMngr = Components.classes["@mozmonkey.com/debuglogger/manager;1"] .getService(Components.interfaces.nsIDebugLoggerManager); 2: logger = logMngr.registerLogger("myapp"); 3: logger.log(3, "Hello World");

*/
