/** This function will enable/disable a list of controls based upon
 * the value of a checkbox.
 */
function cbEnableList(cbx,list)
{
	var itms = list.split(",");
	for(var i = 0; i < itms.length; i+=1) {
		var itm = getItem(itms[i]);
		if(itm) {
			itm.disabled = cbx.checked;
		}
	}	
}

/* This function will check all of the checkboxes associated
 * with list to the same value as the provided checkbox.
 * 
 * example:
 *	checkAll(document.adm_acc_lst_frm.selchk,document.adm_acc_lst_frm['selitems[]'])
 * 
 * parameters:
 * 	cbx - checkbox with the source value
 * 	list - list of checkboxes to check
 */
function checkAll(cbx,list) 
{ 
	var c = cbx.checked; 
	var a = list; 
	for (var i = 0; i < a.length; i+=1)
	{ 
		a[i].checked = c; 
	}
}

/* This will look up an item on the current page
 * by name or id in a way that should support 
 * most browsers
 *
 * example:
 *	var item = getItem('name');
 *
 * parameters:
 *	whichLayer - the name of the item we're looking for
 */
function getItem(whichLayer)
{	
	var item = null;
	if (document.getElementById)
	{
		// this is the way the standards work
		item = document.getElementById(whichLayer);
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		item = document.all[whichLayer];
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		item = document.layers[whichLayer];
	}
	if( item == null )
	{
		item = document.forms[whichLayer];
	}
		
	return item;
}

// simple helper
function show_props(obj, obj_name) { 
    var result = ""; 
    for (var i in obj) 
    {
        result += obj_name + "." + i + " = " + obj[i] + "\n"; 
    }
    return result; 
}


/* This will submit an event to the standard form
 *
 * example:
 *	submitForm('product_form','modifyEvent',12)
 *
 * parameters:
 *	formname - name of the form to submit
 *	event 	- name of the event to through
 *	param	- option event parameter
 */
function submitForm(formname,event,param)
{
	var form = getItem(formname);
	if(form) {
		// set the event name (see events.inc.php for corresponding tag name)
		form.mth.value = event;
		// set the event parameter
		form.eventParameter.value = param;
		// submit the form if we pass the validation
		//if(form['onsubmit'] == null || form.onsubmit()) {
			form.submit();
		//}
	} else {
			alert("Error: can't find form: "+formname);
	}
}
   
/* This will submit an event to the standard form
 * only after user approval.
 *
 * parameters:
 *	actiontxt - the text of the action to be performed (shown to the user)
 *	formname - name of the form to submit
 *	event 	- name of the event to through
 *	param	- option event parameter
 */
function confirmSubmit(actiontxt,formname,event,param)
{
	if(confirm("Are you sure you'd like to \""+actiontxt+"\"?")) {
		submitForm(formname,event,param);
	}
} 

/* This will show/hide the state select box when
 * the appropriate country is selected.
 *
 * NOTE: this is kinda hard coded for now.
 */
function swapCountry(countryid,rootname)
{
	if(countryid == 'United States') {
		getItem(rootname+'_edit').style.display = "none";
		getItem(rootname+'_select').style.display = "block";
	} else {
		getItem(rootname+'_edit').style.display = "block";
		getItem(rootname+'_select').style.display = "none";
	}
}   

/* This is a clever little way to popup a display window
 */
var newwindow = '';
function popitup(url) {
	if (newwindow.location && !newwindow.closed) {
    	newwindow.location.href = url; 
	    newwindow.focus(); 
	} else { 
    	newwindow=window.open(url,'htmlname','width=404,height=316,resizable=yes,scrollbars=yes');
	} 
}
 
/* This function will hide all of the divs with
 * the names in the "actions" array and show
 * the single div named in "action".
 */
function switchForm(action,actions)
{
	for (var i = 0; i < actions.length; i+=1) {
		var value = actions[i].value;
		var itm = getItem('action_form'+value);
		if( value != action && itm != null ) {
			itm.style.display = "none";
		}
	}
	var itm2 = getItem('action_form'+action);
	if( itm2 != null ) {
		itm2.style.display = "block";
	}
}   

/* This function will show a div by name only
 * when value == showValue, otherwise the div
 * will be hidden.
 */
function hideShowWhen(divName,value,showValue)
{
	var itm = getItem(divName);
	if( itm != null ) {
		if( value == showValue ) {
			itm.style.display = "block";
		} else {
			itm.style.display = "none";
		}
	}
}
/* This function will disable a form item by name only
 * when value == showValue, otherwise the div
 * will be hidden.
 */
function disableWhen(divName,value,showValue)
{
	var itm = getItem(divName);
	if( itm != null ) {
		if( value == showValue ) {
			itm.disabled = true;
		} else {
			itm.disabled = false;
		}
	}
}

function disableWhenArray(items,value,showValue)
{
	for (var i = 0; i < items.length; i+=1) {
		var itm = getItem(items[i]);
		if( itm != null ) {
			if( value == showValue ) {
				itm.disabled = true;
			} else {
				itm.disabled = false;
			}
		}
	}
}


/*
function highlight_div(checkbox_node)
{
    label_node = checkbox_node.parentNode;

    if (checkbox_node.checked)
	{
		label_node.style.backgroundColor='#0a246a';
		label_node.style.color='#fff';
	}
	else
	{
		label_node.style.backgroundColor='#fff';
		label_node.style.color='#000';
	}
}   
*/
