/*
	Pick Professionals, Inc
	all rights reserved
          Misc functions and utilities
*/
function hideShowByName(ctrlName){
	var ctrl=document.all(ctrlName)
	if(!ctrl){
		alert('hideShowByName cannot locate control "' + ctrlName + '".');
		return false
	}
	try {
		return hideShow(ctrl);
	} catch (e){
		alert("failed to hide control: " + ctrlName)
		return false;
	}
}
function hideShow(ctrl){
	// Generic hide/show control based on style
	if(!ctrl){
		alert("HideShow cannot locate control!");
		return false
	}
	with(ctrl.style){
		if(display==''||display=='block'){
			display='none';
			visibility='hidden';
		} else {
			display='block';
			visibility='visible';
		}
	}
	return false;
}
function hideShowState(ctrl,hiddenVar){
	if(!ctrl){
		alert('Missing control for "hideShowState" util function.');
		return false
	}
	if(!hiddenVar){
		alert('Missing hidden var for "hideShowState" util function.');
		return false;
	}
	if(hiddenVar.value==''||hiddenVar.value=='block'){
		hide(ctrl);
		hiddenVar.value='none';
	} else {
		show(ctrl);
		hiddenVar.value='block'
	}
}
function hide(ctrl){
	with(ctrl.style){
		display='none';
		visibility='hidden';
	}
}
function show(ctrl){
	with(ctrl.style){
		display='';
		visibility='visible';
	}
}
function hideByName(ctrlName){
	var ctrl=document.all(ctrlName)
	hide(ctrl)
}
function showByName(ctrlName){
	var ctrl=document.all(ctrlName)
	show(ctrl)
}
function hideShowEnsureOnScreen(ctrl){
	hideShow(ctrl)
	return ensureOnScreen(ctrl.id)
}
function xxensureOnScreen(ctrlName){
	var ctrl=document.all(ctrlName);
	if(!ctrl){
		alert('Cannot locate control "' + ctrlName + '" for [ensureOnScreen()].');
		return false;
	}
	var tblLeft=ctrl.offsetLeft		// ctrl.clientLeft
	var tblTop=ctrl.offsetTop;

	var tblWidth=ctrl.offsetWidth		// ctrl.clientWidth; 
	var tblHeight=ctrl.clientHeight;
	var bodyWidth=document.body.clientWidth
	var bodyHeight=document.body.clientHeight
	if(tblLeft+tblWidth>bodyWidth-50){
		var iLeft=bodyWidth-tblWidth-50;
		if(iLeft<50)iLeft=50;
		with(ctrl.style){
			left=iLeft;
			status="Move left to " + iLeft
		}
	}
	if(tblLeft < 25){
		with(ctrl.style){
			left=25
		}
	}
	if(tblTop+tblHeight>bodyHeight-25){
		var iTop=bodyHeight-tblHeight-25
		if(iTop<25)iTop=25;	
		ctrl.style.top=iTop;
	}
}
function ensureOnScreen(ctrlName){
	var ctrl=document.all(ctrlName);
	if(!ctrl){
		alert('Cannot locate control "' + ctrlName + '" for [ensureOnScreen()].');
		return false;
	}
	var tblLeft=ctrl.offsetLeft		// ctrl.clientLeft
	var tblTop=ctrl.offsetTop;

	var tblWidth=ctrl.offsetWidth		// ctrl.clientWidth; 
	var tblHeight=ctrl.clientHeight;
	var bodyWidth=document.body.clientWidth
	var bodyHeight=document.body.clientHeight

	if(tblLeft+tblWidth>bodyWidth-50){
		var iLeft=bodyWidth-tblWidth-50;
		if(iLeft<50)iLeft=50;
		with(ctrl.style){
			left=iLeft;
//			status="Move left to " + iLeft
		}
	}
	if(tblTop+tblHeight>bodyHeight-25){
		var iTop=bodyHeight-tblHeight-25
		if(iTop<25)iTop=25;	
		ctrl.style.top=iTop;
	}
	return false
}

// --- Set focus on a specific control //
function ScrollIntoView(ctrl){
	if(!ctrl){
		alert('Cannot locate CONTROL for [ScrollIntoView]');
		return false
	}
	ctrl.scrollIntoView(true);
}
function focusControl(ctrl, selectFx)
{
	// typical use:  OnLoad="focusControl(document.all.txtName, true/false);"
	if(ctrl)
	{
		try
		{
			if(ctrl.focus){ctrl.focus()} else {alert('canot focus');};
			if(selectFx&&ctrl.select)ctrl.select();
		} catch(e) {
// if control is not visible - ignore the message, otherwise, show the alert.
			if((e.number&0xffff)!=2110 && e.number&0xffff != -2146826178)	{
				alert('[' + (e.number & 0xffff) + '] ' + e.description + ' ');
			} else { 
				//alert(e.number + ' !! ' + e.description); 
			}
		}
	} else {
		if(ctrl != null)
			alert('\tJS\\UTILS.JS\nfocusControl(): invalid control supplied:\n\t' + ctrl + '\n--------------------------------------------------\n"' + document.location.toString() + '"');
	}
}

// --- Toggle read-only fields
function toggleReadOnly(ctrl)
{
	if(!ctrl){
		alert('toggleReadonly - invalid control passed.');
		return false;
	}
//	if(ctrl.readOnly)ctrl.readOnly=''; else ctrl.readOnly='readOnly'
	ctrl.readOnly = !ctrl.readOnly;
	ctrl.className=ctrl.readOnly?'formDataReadOnly':'formData'
	return false;
}


function setBiTabFocus(shortName)
{	
	document.body.onload='';
	if(typeof selectedTabVar == 'undefined') return false;
	if(!selectedTabVar)
		alert('cannot locate selectedTabName HIDDEN FIELD.');

	if(!shortName)shortName=selectedTabVar.value;
	// hide 'previous' Table, adjust button style
	var btn=document.all.item(baseCtrlName + selectedTabVar.value);
	var fullName = selectedTabVar.value + '_tbl';
	var tbl=document.all(fullName);
	if(btn){
		btn.className='tabNormal';
		btn.defaultClass='tabNormal';
		if(!tbl)alert('1) Cannot locate table "' + fullName + '"');
	}
	if(tbl){
		tbl.style.display='none';				//hide previous
	}

	// show 'current' selected table
	var fullName =baseCtrlName + shortName
	btn=document.all.item(fullName);
	if(!btn){
		alert('cannot locate button "' + fullName + '"');		// biMainMa_biMa_tabs_Basic
		return false;
	}
	btn.className='tabSelected';
	btn.defaultClass='tabSelected';				// highlight selected button
	selectedTabVar.value=shortName;				// to hold currently selected...
	
	fullName =  shortName + '_tbl'
	tbl = document.all.item(fullName);
	if(!tbl){
		alert('2) Cannot locate table "' + fullName + '"');
		return false;
	}
	tbl.style.display=''						// show
	if(false)btn.innerText = btnCtrl.id;		// debugging - replace button label with button ID
	
	return false;
}
function xplaySound(soundName){
	var ctrl=document.all._Img_Sound;
	if(!soundName)return false
	if(soundName=="")return false
	try {
		ctrl.dynsrc="sounds/" + soundName + ".wav";
	} catch (e) {
		alert('cannot play sound. [xplaysound(name)]');
	}
	return false;
}