/**
	Name: 	    template.js
	Desc:       Generic javascript functions (user side)

	Orig Author: 	Istvan Horvath (ysu)

	Version info:
	=================================================================
	1.0	  01-JAN-2007 (Ysu) Script created

	(c) Areanet Pty. Ltd. All Rights Reserved. ABN: 84 114 634 561
*/

/* any vars */

var mainFormTabNum = 0;  //used at tabbed views
var lastCharCode = null; //used at cc checking functions

/**
* trim is now a method of any string
*/
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

/**
* add an onload event to the current ones
*/
function addLoadEvent(func) {

	if (!func) alert ('missing func!');

	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/**
 * submit button function
 * f    functionid
 * a    actionSupplementer
 * p    PageListAt
 * x    extra val
 */
function submitbutton(f,a,p,x) {
	submitform(f,a,p,x,'mainForm');
}

/**
 * select a row and submit the list (usually for edit)
 * used on the list form
 * if no id is passed, look for the selected one!
 */
function srow (f,id,a) {
	if (!id) {
		if (document.mainForm.boxchecked.value!=1) {
			alert ('Please select exactly one item from the list!');
			listSelDeselAll(-1);
			return;
		}
		strid = getSelectedBox(true);
	} else {
		var strid = 'selRow' + id;
	}
	listSelDeselAll(-1);
	document.mainForm.elements[strid].checked=1;
	sugebuli(f,a);
}


/**
 * submit the delete req
 * checks the selected rows beforehand
 */
function dRow (f) {
	if (document.mainForm.boxchecked.value == 0){
		alert('Please select at least one item from the list to delete');
	} else {
		if (confirm('Are you sure?')) {
			submitbutton(f,'');
		}
	}
}

/**
 * submit get button
 * using secondary form for submit a short get query instead of the huge post one
 * this one can be used when the action is simple and won't change the db (cancel or refresh for ex)
 */
function sugebu (f,a,p,x) {
	submitform(f,a,p,x,'mainGetForm');
}

/**
 * similar to submit get button, but used from form with selection
 */
function sugebuli (f,a,p,x) {
	var v = getSelectedBox();
	if ( !v ) { alert ('Selected box missing'); return; }
	document.mainGetForm.edVal.value = v;
	submitform(f,a,p,x,'mainGetForm');
}

/**
 * Submit the admin form
 * f    functionid
 * a    actionSupplementer
 * p    PageListAt
 * n    n3 (tabid) value
 */
function submitform(f,a,p,x,fn) {
	//get the function, the action supplementer and the pageListAt value
	frm = document.forms[fn];
	if ( typeof(frm._f) != 'undefined' )
		frm._f.value=f;
	if ( typeof(frm._a2) != 'undefined')
		frm._a2.value=a;
	if ( typeof(frm.pageListAt) != 'undefined')
		frm.pageListAt.value=p;
	if ( typeof(frm._xv)!='undefined' && x!='') //extra var
		frm._xv.value=x;

	//a little speciality used on tabbed Forms - select all related objects and stuff so they are passed eventually
	if (typeof(needToSelectAllSelects) != 'undefined' )  selectAllSelects(needToSelectAllSelects);

	//ok, let's go
	frm.submit();
}


/**
* cancel button, will call the get button but first check if there are any changes
*/
function cancelButton ( f,a,p,n ) {
	if (isFormModified('mainForm')) {
		if (confirm('Are you sure? (you will lose all unsaved changes)')) {
			//alert ('form changed'); return;
			sugebu (f,a,p,n);
		}
	} else {
		//alert ('form was not modified'); return;
		sugebu (f,a,p,n);
	}
}
/**
* cancel url, checks for changes as well
*/
function cancelURL ( u ) {
	if (isFormModified('mainForm')) {
		if (confirm('Are you sure? (you will lose all unsaved changes)')) {
			document.location.href=u;
		}
	} else {
		//alert ('form was not modified');
		document.location.href=u;
	}
}

/**
* simply does the confirmation and returns boolean T/F (true if we can go on, false if not)
*/
function cancelTF ( ) {
	if (isFormModified('mainForm')) {
		if (confirm('Are you sure? (you will lose all unsaved changes)')) {
			return true;
		}
	} else {
		//alert ('form was not modified');
		return true;
	}
	return false;
}


/**
*  Check Count INC / DEC
*  increment or decrement the checked box count
*/
function chkCID(isitchecked){
	if (isitchecked == true){
		document.mainForm.boxchecked.value++;
	} else {
		document.mainForm.boxchecked.value--;
	}
}

/**
* select/deselect all checkboxes
* the listSelNextState shows if next we'll check or uncheck all
*/
function listSelDeselAll(on) {
	if ( typeof(arrSD) == 'undefined' ) return ;
	if ( on==1 ) {
		listSelNextState = true;
	} else if (on==-1) {
		listSelNextState = false;
	}
	var n = arrSD.length;
	for ( i=0; i<n; i++ ) {
		if ( arrSD[i] ) {
			vn = 'selRow'+arrSD[i];
		} else {
			vn = '';
		}
		if ( typeof(document.mainForm[vn])!=='undefined' ) {
			document.mainForm[vn].checked = listSelNextState;
		}
	}
	document.mainForm.boxchecked.value = ( listSelNextState ) ? n : 0;
	listSelNextState = (listSelNextState) ? false : true;
}

/**
* return the seleted box from the list
* @input bool  getVal  if false it'll return the selected box's value, if true then the box's id (the variable name effectively)
* (this second functionality is needed at srow)
*/
function getSelectedBox( getVarName ) {
	if ( typeof(arrSD) == 'undefined' ) return ;
	var n = arrSD.length;
	for ( i=0; i<n; i++ ) {
		vn = 'selRow'+arrSD[i];
		if (document.mainForm[vn].checked) {
			if (getVarName) {
				return vn;
			} else {
				return document.mainForm[vn].value;
			}
		}
	}
	return 0;
}


/**
* update year-month select type element's hidden
* called on change
*/
function updateDateYM( elementName ) {
	var yearSel = document.getElementById(elementName + '_y');
	var monthSel = document.getElementById(elementName + '_m');
	var elementHidden = document.getElementById(elementName);
	if ( typeof(monthSel) == 'undefined'  || typeof(yearSel) == 'undefined' || typeof(elementHidden) == 'undefined' ) {
		return false;
	}
	//if ( monthSel.value<1 || yearSel.value<1 ) return false; //bad idea; we can't re-set it to 0 that way
	mval = monthSel.value.length<2 ? '0' + monthSel.value : monthSel.value;
	elementHidden.value = mval + '/' + yearSel.value;
	//alert ( 'Element Name: '+elementName+' -- year/month:' + yearSel.value + '/' + monthSel.value + ' -- m.length:' + monthSel.value.length + '-- Hidden updated to: ' + elementHidden.value );
	return true;
}

/**
* does the tab-swapping if there are tabs on the page.
* @param int tabid (the html id of the tab: 'tab' + number)
*/
function mainFormShowTab (tabid) {
	//need to hide the rest
	for ( i=1; i<mainFormTabNum+1; i++ ) {
		tabname = 'tab' + i;
		thname = 'tabhead' + i;
		t = document.getElementById(tabname);
		th = document.getElementById(thname);
		//alert ('total: '+mainFormTabNum+ ' working on -' +tabname+ '- ... is it like -'  +tabid +'- ?...');
		//alert ('head: '+ th + ' class: ' + th.className );
		if ( i == tabid ) {
			t.style.display = 'block';
			document.mainForm._tab.value=i; //set the tabid!
			th.className = 'on';
		} else {
			t.style.display = 'none';
			th.className = 'off';
		}
	}
}

/**
* simply sets the tabNumber on the page
*/
function mainFormSetTabNum (num) {
	mainFormTabNum = num;
}


/**
* @desc called when the rows to display's value is changed by the user on the admin form's header
*/
function rowsToDisplay_change ( loc, params, selected ) {
	document.location.href = loc + "?" + params + "&rowsToDisplay=" + selected;
}
/**
* @desc changes the image on the user form's object tag
*/
function updateImage ( tabid, imgid  ) {
	var imageHtmlId = 'image' + tabid;
	var linkHtmlId = 'imglink' + tabid;
	//alert ('image: '+imageHtmlId + '  link: ' + linkHtmlId );
	//the image
	if (document.images) {
		document.images[imageHtmlId].src = urlRoot + urlAdmin + "display.php?t=1&o=" + imgid;
	}
	//and the link
	if (typeof(document.links[linkHtmlId]) != 'undefined' ) { //correct way
		document.links[linkHtmlId].href = urlRoot + urlAdmin + "display.php?o=" + imgid;
	} else if (typeof(document.all[linkHtmlId]) != 'undefined') { //IE way
		document.all[linkHtmlId].href = urlRoot + urlAdmin + "display.php?o=" + imgid;
	}

	return true;
}
/**
* just this little message about nothing currently selected
*/
function displayMultiselMessage () {
	alert ('You need to select exacly one object from the list');
}

/**
* adds an item to the given multi-select
*/
function addSelList (selectId, elementId, elementName ) {
	//get the next element
	var n = document.mainForm[selectId].length;
	if (!n) n = 0;
	//add new element
	document.mainForm[selectId][n] = new Option( elementName,elementId, false, false);
	//desel all & select the last only
	selLastElement(selectId);
}
/**
* select only one element of the given multisel
* the imageid is actually the option's value
*/
function selLastElement (selid) {
	var n = document.mainForm[selid].length;
	for (i=0; i<n; i++) {
		//iid = document.mainForm[selid].options[i].value;
		state = (i==n-1) ? true : false;
		document.mainForm[selid].options[i].selected = state;
	}
}
/**
* get the selected element's position in the given (multi) select
*/
function getSelectedElement (selid) {
	var n = document.mainForm[selid].length;
	if (!n) return false;
	var sel = false;
	for (i=0; i<n; i++) {
		s = document.mainForm[selid].options[i].selected;
		if (s!==false) {
			if ( sel===false ) sel = i;
			else sel = -1;
		}
		//document.mainForm[selid].options[i].selected = state;
	}
	if (sel==-1) sel=false;
	if (sel===false) displayMultiselMessage();
	return sel;
}
/**
* control function - move element up
* direction is +1 for down, -1 for up
*/
function moveSelectedElement (selid,direction) {

	var pos = getSelectedElement(selid);
	if (pos===false) return; //nothing selected

	var n = document.mainForm[selid].length;
	if (n==1) return; //move 1 element?
	if (direction==-1 && pos==0) return; // first element - can't move any higher
	if (direction== 1 && pos==n-1) return; // last element - can't move any lower
	//get current
	cval = document.mainForm[selid].options[pos].value;
	ctxt = document.mainForm[selid].options[pos].text;
	//get other
	pval = document.mainForm[selid].options[pos+direction].value;
	ptxt = document.mainForm[selid].options[pos+direction].text;
	//swap
	document.mainForm[selid].options[pos].value = pval;
	document.mainForm[selid].options[pos].text  = ptxt;
	document.mainForm[selid].options[pos+direction].value = cval;
	document.mainForm[selid].options[pos+direction].text  = ctxt;
	//select - deselect
	document.mainForm[selid].options[pos].selected = false;
	document.mainForm[selid].options[pos+direction].selected = true;
}
/**
* control function - delete element
*/
function delSelectedElement (selid,imgid,linkid) {
	var pos = getSelectedElement(selid);
	if (pos===false) return; //nothing selected
	var n = document.mainForm[selid].length;
	//deselect, remove ...
	document.mainForm[selid].options[pos].selected = false;
	document.images[imgid].src = '';
	if (typeof(document.links[linkid]) != 'undefined' ) { //correct way
		document.links[linkid].href = '';
	} else if (document.all) { //IE way
		document.all[linkid].href = '';
	}

	//remove - move all up
	for (i=pos; i<n-1; i++) {
		//get next
		nval = document.mainForm[selid].options[i+1].value;
		ntxt = document.mainForm[selid].options[i+1].text;
		//insert
		document.mainForm[selid].options[i].text = ntxt;
		document.mainForm[selid].options[i].value = nval;
	}
	//remove last
	document.mainForm[selid].options.length=n-1;
}

/**
* selects all the multi selects
* called on submit (onsubmit) of the form
*/
function selectAllSelects(tabnum) {
	for (i=1; i<=tabnum; i++) {
		selid = 'objSelList' + i;
		if ( (typeof(document.mainForm[selid]) != 'undefined')  &&  (document.mainForm[selid].type != 'select-one')  ) {
			var n = document.mainForm[selid].length;
			for (j=0; j<n; j++) {
				document.mainForm[selid].options[j].selected = true;
			}
		}
	}
}

/**
* add some text at the cursor in a text area
*/
function insertAtCursor(myField, myValue) {
	if (document.selection) {
		// IE support
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	} else if (myField.selectionStart || myField.selectionStart == '0') {
		// MOZILLA/NETSCAPE support
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
			+ myValue
			+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}

/**
* extension to quicktags to display the artcile
* open up a new window and display the content
*/
function callViewWindow (targetFile) {
	//submit the form to the test window!
	var oriTarget = document.mainForm.target;
	var oriAction = document.mainForm.action;
	document.mainForm.target = '_blank';
	document.mainForm.action = urlRoot + urlAdmin + targetFile + '.php';

	//select the selects firs!
	if (typeof(needToSelectAllSelects) != 'undefined' )  selectAllSelects(needToSelectAllSelects);

	//submit it into the new window
	document.mainForm.submit();

	//reset target and action so the form will still work for saving
	document.mainForm.target = oriTarget;
	document.mainForm.action = oriAction;
}


/**
* set the function and action of the form
* used on the search field onChange, prevent no-action submit if enter is pressed on search
*/
function setFuncAct (f,a) {
	frm = document.forms['mainForm'];
	if ( typeof(frm._f) != 'undefined' )
		frm._f.value=f;
	if ( typeof(frm._a2) != 'undefined')
		frm._a2.value=a;
}

/**
* copies one (the first) selcted element from one select into another
*
*/
function copySelectedElement ( fromSelect, toSelect ) {
		var pos=getSelectedElement(fromSelect);
		if ( pos===false ) return false;

		var elementName = document.mainForm[fromSelect].options[pos].text;
		var elementId = document.mainForm[fromSelect].options[pos].value;
		//alert ('will add:' + elementId +' '+ elementName);
		addSelList (toSelect, elementId, elementName );
}

/**
* checkboxes are updated with a hidden field to pass value always, this value needs to be updated if the checked state changes
*/
function chkChng(fieldname) {
	var chkName = 'chk_'+fieldname;
	var chk = document.getElementById(chkName).checked;
	var toChange = document.getElementById(fieldname);
	if ( chk ) {
		toChange.value=1;
	} else {
		toChange.value=0;
	}
}


/**
* Check all elements of a form if any of them has been modified
*   You can pass a comma-separated list of field names to check in
*   addition to visible fields (for hiddens, etc).
*   You can also pass a comma-separated list of field names to be
*   ignored in the check.
*/
function isFormModified(formName){
	//alert ('isFormModified starts on form:' + formName);
	frm = document.getElementById(formName);
	if (!frm) return false;
	var i,field;
	for (i=0;i<frm.elements.length;i++) {
		var changed=false;
		var name=frm.elements[i].name;
		if(!isBlank(name)){
			changed=isFieldChanged(frm[name]);
		}
		if(changed){return true;}
	}
	return false;
}

/**
* check the changed status of one field compared to the default
*/
function isFieldChanged(obj){
	//alert ('checking next: '+obj.name);
	//exception handling...for the sake of IE of course
	if ( obj.name=='_tab' ) return;
	return(getInputValue(obj,false,',')!=getInputValue(obj,true,','));
}


/**
* get the input val of an element even if it's an array (checkboxes!)
*/
function getInputValue(obj,use_default,delimiter) {
	//alert ('obj: '+obj.name + ' , type:' +obj.type);
	if (isArray(obj) && (typeof(obj.type)=="undefined")) {
		//alert ('obj '+obj.name + ' is an array & type undefined');
		var values=new Array();
		for(var i=0;i<obj.length;i++){
			var v=getSingleInputValue(obj[i],use_default,delimiter);
			if(v!=null){values[values.length]=v;}
		}
		return implode(values,delimiter);
	}
	return getSingleInputValue(obj,use_default,delimiter);
}


/**
* get the default value of a single input object
*/
function getSingleInputValue ( obj,use_default,delimiter ) {
	//alert ('getInputValue running on:'+obj.name +' type:'+obj.type);
	switch(obj.type){
		case 'radio':
		case 'checkbox':
			if ( use_default ) {
				var ret = obj.defaultChecked ? obj.defaultValue : null;
			} else {
				var ret = obj.checked ? obj.value : null;
			}
			//var ret = (((use_default)?obj.defaultChecked:obj.checked)?obj.value:null) //this won't do for a radio - it'll return the value even if it has changed from the default...
			//alert ('getinput running on '+obj.type+':'+ obj.name + ' , defchk:' +obj.defaultChecked  +' chk:'+ obj.checked  +' val:'+ obj.value +', useDef?:' +use_default +', will return:'+ret);
			return ret;
		case 'text':
		case 'hidden':
		case 'textarea':
			var ret = (use_default)?obj.defaultValue : obj.value;
			//alert ('getinput running on '+obj.type+':'+ obj.name + ', defval:'+obj.defaultValue+', val:'+ obj.value +', useDef?:' +use_default + ' will return:'+ret);
			return ret
		case 'password':
			return(use_default)?obj.defaultValue:obj.value;
		case 'select-one':
			if (obj.options==null) { return null; }
			if (obj.options.length==0) { return null; }
			var o=obj.options;
			if(use_default){
				for(var i=0;i<o.length;i++){
					if(o[i].defaultSelected){
						return o[i].value;
					}
				}
				return o[0].value;
			} else {
				if (obj.selectedIndex<0){return null;}
				return(obj.options.length>0)?obj.options[obj.selectedIndex].value:null;
			}
		case 'select-multiple':
			if (obj.options==null) { return null; }
			var values=new Array();
			for(var i=0;i<obj.options.length;i++) {
				if((use_default&&obj.options[i].defaultSelected)||(!use_default&&obj.options[i].selected)) {
					values[values.length]=obj.options[i].value;
				}
			}
			return (values.length==0)?null:implode(values,delimiter);
		default:
			//silently drop
			//alert('Error @ object ['+obj.name+'] - Field type ['+obj.type+'] is not supported for this function');
	}
}

/**
* find the selected value of a radio group
* @param (str)frmName   - the form name
* @param (str)radioName - the name of the radio select
*/
function getRadioSelected(frmName,radioName) {
	//alert ('radiosel frm='+frmName+'  -- radioname='+radioName);
	var frm = document[frmName];
	for (i=0;i<frm[radioName].length;i++) {
		if (frm[radioName][i].checked) {
			user_input = frm[radioName][i].value;
		}
	}
	return (user_input);
}



/**
* implode(array[,delimiter])
*   Take an array of values and turn it into a comma-separated string
*   Pass an optional second argument to specify a delimiter other than comma.
*/
function implode(obj,delimiter){
	if (typeof(delimiter)=="undefined" || delimiter==null) {
		delimiter = ",";
	}
	var s="";
	if(obj==null||obj.length<=0){return s;}
	for(var i=0;i<obj.length;i++){
		s=s+((s=="")?"":delimiter)+obj[i].toString();
	}
	return s;
}

/**
* explode the string into an array
*/
function explode (str,delimiter) {
	if (typeof(delimiter)=="undefined" || delimiter==null) {
		delimiter = ",";
	}
	arr = str.split(delimiter);
	return arr;
}


/**
* check if a value of a variable is devoid of useful chars
*/
function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
	}
	return true;
}


/**
* simple check to see if object is an array
*/
function isArray(obj){
	if ( typeof(obj)!='undefined' ) return( typeof(obj.length)=='undefined' ) ?false:true;
	return false;
}


/**
* double check the double-email field type
* if called without the 2nd field filled, it'll move the focus on that field
* @param str  type      - 'e' or 'p' as in  email or password type. for customising error message if necessary
* @param str  fieldID   - the base field id. the 2nd field and the feedback field are derived from the orig id
*/
function dblCheckField (type, fieldID) {
	var f2ID = fieldID + '_dbc';
	var f1 = document.getElementById(fieldID);
	var f2 = document.getElementById(f2ID);
	var fbf = document.getElementById(fieldID + '_fb'); //feedback field

	//alert('f2.value = ' + f2.value + " - " + "f1.value = " + f1.value);

	//colors
	var clrOk = '#00CC99';
	var clrErr = '#FF0066';

	if ( !f1 || !f2 ) {
		alert ('Javascript error at field matching, one or more fields could not be found - please contact site administrators with the page URL and the below info: \ntype='+type+'\nf1='+fieldID+'\nf2='+f2ID);
		return;
	}

	fbf.style.fontWeight="bold";

	if ( f2.value == '' ) {
		fbf.innerHTML = 'Please fill in! ';
		f2.style.backgroundColor=clrErr;
		fbf.style.color=clrErr;
		return false;
	} else {
		if ( f2.value == f1.value ) {
			f2.style.backgroundColor= clrOk;
			fbf.innerHTML = 'Correct! ' ;
			fbf.style.color=clrOk;
			return true;
		} else {
			f2.style.backgroundColor=clrErr;
			fbf.innerHTML = 'Fields don\'t match! ';
			fbf.style.color=clrErr;
			return false;
		}
	}
}


/**
* check the input, allow numbers only and a few control chars
* @param event evt
*/
function checkCharCodeNumOnly ( evt ) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
	lastCharCode = charCode;
	// test; see the last pressed key code:
	// document.getElementById('lcc').value=lastCharCode;
	//key check
	if (evt.shiftKey) { //shift pressed
		if ( charCode!=9 ) { //tab ok everything else is bad
			return false;
		}
	} else if (evt.ctrlKey) { //ctrl pressed
		if ( charCode!=114 ) { //r ok, everything else is bad
			return false;
		}
	} else if ( charCode==9 || charCode==116 || (charCode>=37 && charCode<=40) //allowed:  tab,f5,arrows
			|| (charCode>=48 && charCode<=57) || charCode==8 || charCode==46   )  {  //allowed:  numbers, backspace,del
			return true;
	} else {
		//any other case is a disallowed char
		return false;
	}
}


/**
* check the cc field and update it's hidden
*/
function chkCCField (elementName, inField ) {

	if ( ! ( lastCharCode>=48 && lastCharCode<=57  || lastCharCode==8 || lastCharCode==46 ))  {
		lastCharCode = null;
		return;
	}
	lastCharCode = null;

	var activeField= document.getElementById(elementName + '_c' + inField);
	if ( activeField.value.length>3 ) {
		if ( inField<4 ){
			//skip to next?
			var nextFieldNo = parseInt(inField)+1;
			var nextField = document.getElementById(elementName + '_c' + nextFieldNo );
			nextField.focus();
		} else {
			//final check?
		}
	}
	//update hidden
	var f1 = document.getElementById(elementName + '_c1');
	var f2 = document.getElementById(elementName + '_c2');
	var f3 = document.getElementById(elementName + '_c3');
	var f4 = document.getElementById(elementName + '_c4');
	var elementHidden = document.getElementById(elementName);
	if ( typeof(f1) == 'undefined'  || typeof(f2) == 'undefined' || typeof(f3) == 'undefined'  || typeof(f4) == 'undefined' || typeof(elementHidden) == 'undefined' ) {
		return false;
	}

	elementHidden.value = f1.value + f2.value + f3.value + f4.value;

	// test; see the hidden's value real time
	// document.getElementById('cno').value = elementHidden.value ;

	return true;
}


/**
* open & close an ul
* used at sticky menus
*/
function ulOpenClose (id, urlToImg) {
	var ulID = 'submenu' + id;
	var imgID = 'openerImg' + id;
	var ulObj = document.getElementById(ulID);
	var imgObj = document.getElementById(imgID);
	if ( !ulObj || typeof(ulObj) == 'undefined' ) {
		//alert("can't find UL object with id " + ulID);
		return;
	} else if ( !imgObj || typeof(imgObj) == 'undefined' ) {
		//alert("can't find IMG object with id " + imgID);
		return;
	}
	//change visibility + the image
	if (ulObj.style.display == 'block') {
		ulObj.style.display='none';
		imgObj.src = urlToImg + 'add_sign.png';
	} else {
		ulObj.style.display='block';
		imgObj.src = urlToImg+'neg_sign.png';
	}
}


/**
* update phone field from two parts (area + phone)
*/
function updatePhoneField (elementName) {
	var f1 = document.getElementById(elementName + '_pa');  //area
	var f2 = document.getElementById(elementName + '_ph');  //phone
	var elementHidden = document.getElementById(elementName);
	if ( typeof(f1) == 'undefined'  || typeof(f2) == 'undefined' || typeof(elementHidden) == 'undefined' ) {
		return false;
	}
	//do not update the field with areacode by itself
	elementHidden.value = (f1.value && f2.value) ?  '(' + f1.value + ')' + f2.value : f2.value;
	//alert ('elementHidden now: ' + elementHidden.value);
}


/**
* cut & output the phone+area code into two separate fields
*/
function outputPhoneField(elementName,value) {
	var f1 = document.getElementById(elementName + '_pa');  //area
	var f2 = document.getElementById(elementName + '_ph');  //phone
	var elementHidden = document.getElementById(elementName);
	if ( typeof(f1) == 'undefined'  || typeof(f2) == 'undefined' || typeof(elementHidden) == 'undefined' ) {
		return false;
	}

	var arr = new Array('','');
	char1 = value.substring(0, 1);
	if ( char1 != '' && char1 == '(' ) {
		v = value.substring(1,value.length);
		arr=v.split(')');
		//alert (arr[0] + "\n" + arr[1]);
		f1.value = arr[0];
		f2.value = arr[1];
	} else {
		f2.value = value;
	}
	//alert ('elementHidden after load: ' + elementHidden.value);
}



/**
* pop up the help window
*/
function helpwin (helpfunc,gotofunc) {
	var wURL = "index.php?_f="+ helpfunc +"&f2="+ gotofunc;
	var wName = 'helpwindow';
	var wWidth = 760;
	var wHeight = 410;
	var wOptions = "left=0,top=0,height=" + wHeight + ",width=" + wWidth +",scrollbars=1,status=1,menubar=1,toolbar=1,location=1,resizable=1";
	var wHandle = window.open(wURL,wName,wOptions);
}



/**
* Launches a sized popup window - not search-engine friendly!
* @param str ref (the url to load)
* @param int width (the window width to load)
* @param int height (the window height to load)
*/
function popup(ref, width, height) {
	var strFeatures="toolbar=no,status=no,menubar=no,location=no,scrollbars=yes,resizable=yes,height="+height+",width="+width
	var newWin = window.open(ref, null, strFeatures);
	newWin.opener = top;
}



//--------------------------------------
//  the usual image swapping functions
//--------------------------------------

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_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];}}
}


